]> git.r.bdr.sh - rbdr/r.bdr.sh/blob - js/animation.js
c1317587a50f29cd60cdff5eb3a0309398b7caf6
[rbdr/r.bdr.sh] / js / animation.js
1 (function () {
2
3 if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return
4
5 var K = document.querySelector('canvas'),
6 { random: R, PI, cos: C, sin: S, atan2: A } = Math,
7 id = p => p,
8 spn = rad => p => ({
9 x: p.x + rad * C(l),
10 y: p.y + rad * S(l)
11 }),
12 X = K.getContext('2d'), F = X.fill.bind(X), B = X.beginPath.bind(X),
13 f = 24, l = 0, s = 1, z = 5 + r(10),
14 k = R() > 0.5, c = R() > 0.5,
15 I = Array(r(10) + 6).fill().map(() => ({
16 x: r(64), y: r(64),
17 a: R() * 2 * PI, c: r(100)
18 })),
19 H = [sqr, crc, hrt, chs, lch], P = [id, spn(5), spn(15)],
20 draw = H[r(H.length)], pos = P[r(P.length)],
21 Z = 1
22
23 function r(x) {
24 return ~~(R() * x)
25 }
26
27 function sqr(i) {
28 var p = pos(i)
29 X.fillRect(p.x, p.y, z, z)
30 }
31
32 function crc(i) {
33 var p = pos(i)
34 B()
35 X.arc(p.x, p.y, z / 2, 0, 2 * PI)
36 F()
37 }
38
39 function chs(i) {
40 var {x, y} = pos(i)
41 B()
42 X.moveTo(x, y)
43 X.lineTo(x * 10 * C(i.a), y * 10 * S(i.a))
44 X.lineTo(r(64), r(64))
45 F()
46 }
47
48 function lch(i) {
49 var {x, y} = pos(i)
50 B()
51 X.lineWidth = 5
52 X.strokeStyle = `hsl(0,0%,${i.c}%)`
53 X.moveTo(x, y)
54 X.lineTo(x * 10 * C(i.a), y * 10 * S(i.a))
55 X.stroke()
56 }
57
58 function hrt(i) {
59 var {x, y} = pos(i)
60 X.fillRect(x, y, z, z)
61
62 B()
63 X.arc(x + z / 2, y, z / 2, 0, 2 * PI, false)
64 F()
65 X.closePath()
66
67 B()
68 X.arc(x + z, y + z / 2, z / 2, 0, 2 * PI, false)
69 F()
70 X.closePath()
71 }
72
73 function mov(i) {
74 i.x = i.x + s * C(i.a)
75 i.y = i.y + s * S(i.a)
76
77 if (i.x < 0 || i.x > 64) {
78 i.a = A(S(i.a), -C(i.a))
79 }
80 if (i.y < 0 || i.y > 64) {
81 i.a = A(-S(i.a), C(i.a))
82 }
83 }
84
85 function ucl(i) {
86 i.c = (i.c + 2) % 101
87 }
88
89 function frm(t) {
90 if (Z) requestAnimationFrame(frm)
91
92 var dt = t - l
93
94 if (dt > 1000 / f) {
95 k && X.clearRect(0, 0, 64, 64)
96 for (var i of I) {
97 X.fillStyle = `hsl(0,0%,${i.c}%)`
98 draw(i)
99 c && ucl(i)
100 mov(i)
101 }
102
103 l = t
104 }
105 }
106
107 K.addEventListener('click', () => (Z ? Z=0:(Z=1&&frm())))
108 frm()
109 }
110 )()