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