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