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