]>
Commit | Line | Data |
---|---|---|
21b1c71d RBR |
1 | (function () { |
2 | ||
3b6787e4 | 3 | var canvas = document.querySelector('canvas'), |
ce04a48f RBR |
4 | r = Math.random, |
5 | pi = Math.PI, | |
6 | cos = Math.cos, | |
7 | sin = Math.sin, | |
3b6787e4 | 8 | context = canvas.getContext('2d'), |
7eae2449 | 9 | fps = 24, |
3b6787e4 RBR |
10 | lastFrame = 0, |
11 | speed = 1, | |
9b9f84dd | 12 | size = 5 + rnd(10), |
83c0fa69 RBR |
13 | clear = !!(r() > 0.5), |
14 | changeColor = !!(r() > 0.5), | |
9b9f84dd RBR |
15 | items = Array(rnd(10) + 6).fill(null).map(() => ({ |
16 | x: rnd(64), | |
17 | y: rnd(64), | |
83c0fa69 | 18 | angle: r() * 2 * pi, |
9b9f84dd | 19 | color: Array(3).fill(rnd(256)) |
83c0fa69 | 20 | })), |
9b9f84dd RBR |
21 | shapes = [sqr, crc, hrt, chs, lch], |
22 | positions = [id, spn(5), spn(15)], | |
23 | draw = shapes[rnd(shapes.length)], | |
24 | pos = positions[rnd(positions.length)]; | |
21b1c71d | 25 | |
9b9f84dd | 26 | function rnd (x) { |
8f2ed424 | 27 | return Math.floor(r() * x); |
21b1c71d RBR |
28 | } |
29 | ||
9b9f84dd | 30 | function sqr(i) { |
21b1c71d | 31 | |
9b9f84dd | 32 | var p = pos(i); |
21b1c71d RBR |
33 | context.fillRect(p.x, p.y, size, size); |
34 | } | |
35 | ||
9b9f84dd | 36 | function crc(i) { |
21b1c71d | 37 | |
9b9f84dd | 38 | var p = pos(i); |
21b1c71d | 39 | context.beginPath(); |
8f2ed424 | 40 | context.arc(p.x, p.y, size / 2, 0, 2 * pi); |
21b1c71d RBR |
41 | context.fill(); |
42 | } | |
43 | ||
9b9f84dd | 44 | function chs(i) { |
21b1c71d | 45 | |
9b9f84dd | 46 | var p = pos(i); |
21b1c71d RBR |
47 | context.beginPath(); |
48 | context.moveTo(p.x, p.y); | |
9b9f84dd RBR |
49 | context.lineTo(p.x * 10 * cos(i.angle), p.y * 10 * sin(i.angle)); |
50 | context.lineTo(rnd(64), rnd(64)); | |
21b1c71d RBR |
51 | context.fill(); |
52 | } | |
53 | ||
9b9f84dd | 54 | function lch(i) { |
21b1c71d | 55 | |
9b9f84dd | 56 | var p = pos(i); |
21b1c71d RBR |
57 | context.beginPath(); |
58 | context.lineWidth = 5; | |
9b9f84dd | 59 | context.strokeStyle = `rgb(${i.color.join(',')})`; |
21b1c71d | 60 | context.moveTo(p.x, p.y); |
9b9f84dd | 61 | context.lineTo(p.x * 10 * cos(i.angle), p.y * 10 * sin(i.angle)); |
21b1c71d RBR |
62 | context.stroke(); |
63 | } | |
64 | ||
9b9f84dd | 65 | function sqr(i) { |
21b1c71d | 66 | |
9b9f84dd | 67 | var p = pos(i); |
21b1c71d RBR |
68 | context.fillRect(p.x, p.y, size, size); |
69 | } | |
70 | ||
9b9f84dd | 71 | function hrt(i) { |
21b1c71d | 72 | |
9b9f84dd | 73 | var p = pos(i); |
21b1c71d RBR |
74 | context.fillRect(p.x, p.y, size, size); |
75 | ||
76 | context.beginPath(); | |
8f2ed424 | 77 | context.arc(p.x + size / 2, p.y, size / 2, 0, 2 * pi, false); |
21b1c71d RBR |
78 | context.fill(); |
79 | context.closePath(); | |
80 | ||
81 | context.beginPath(); | |
8f2ed424 | 82 | context.arc(p.x + size, p.y + size / 2, size / 2, 0, 2 * pi, false); |
21b1c71d RBR |
83 | context.fill(); |
84 | context.closePath(); | |
85 | } | |
86 | ||
9b9f84dd | 87 | function id(p) { |
21b1c71d | 88 | |
9b9f84dd | 89 | return p; |
21b1c71d RBR |
90 | } |
91 | ||
9b9f84dd RBR |
92 | function spn(rad) { |
93 | return function (p) { | |
21b1c71d RBR |
94 | |
95 | return { | |
9b9f84dd RBR |
96 | x: p.x + rad * cos(lastFrame), |
97 | y: p.y + rad * sin(lastFrame) | |
21b1c71d RBR |
98 | } |
99 | } | |
100 | } | |
101 | ||
9b9f84dd | 102 | function mov(i) { |
21b1c71d | 103 | |
9b9f84dd RBR |
104 | i.x = i.x + speed * cos(i.angle); |
105 | i.y = i.y + speed * sin(i.angle); | |
21b1c71d | 106 | |
9b9f84dd RBR |
107 | if (i.x < 0 || i.x > 64) { |
108 | i.angle = Math.atan2(sin(i.angle), -cos(i.angle)) | |
21b1c71d RBR |
109 | } |
110 | ||
9b9f84dd RBR |
111 | if (i.y < 0 || i.y > 64) { |
112 | i.angle = Math.atan2(-sin(i.angle), cos(i.angle)) | |
21b1c71d RBR |
113 | } |
114 | } | |
115 | ||
9b9f84dd | 116 | function updateColor(i) { |
21b1c71d | 117 | |
9b9f84dd | 118 | i.color = i.color.map((c) => { |
21b1c71d RBR |
119 | c = c + 5; |
120 | return c > 255 ? 0 : c; | |
121 | }) | |
122 | } | |
123 | ||
124 | function frame(time) { | |
125 | ||
126 | window.requestAnimationFrame(frame); | |
127 | ||
128 | var delta = time - lastFrame; | |
129 | ||
130 | if (delta > 1000 / fps) { | |
e99de7cc | 131 | clear && context.clearRect(0, 0, 64, 64); |
9b9f84dd RBR |
132 | for (var i of items) { |
133 | context.fillStyle = `rgb(${i.color.join(',')})`; | |
134 | draw(i); | |
135 | changeColor && updateColor(i); | |
136 | mov(i); | |
21b1c71d RBR |
137 | } |
138 | ||
139 | lastFrame = time; | |
140 | } | |
141 | } | |
142 | ||
143 | frame(); | |
144 | } | |
145 | )(); |