]> git.r.bdr.sh - rbdr/r.bdr.sh/blame - js/animation.js
Add control
[rbdr/r.bdr.sh] / js / animation.js
CommitLineData
21b1c71d
RBR
1(function () {
2
92984c46 3 if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return
b8bcdb6d 4
3c30503b 5 var K = document.querySelector('canvas'),
672c9cfe 6 { random: R, PI, cos: C, sin: S, atan2: A } = Math,
5054447d
RBR
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)],
b8bcdb6d
RBR
20 draw = H[r(H.length)], pos = P[r(P.length)],
21 Z = 1
00bc4991 22
55ca9087 23 function r(x) {
672c9cfe 24 return ~~(R() * x)
55ca9087
RBR
25 }
26
27 function sqr(i) {
23398f9a
RBR
28 var p = pos(i)
29 X.fillRect(p.x, p.y, z, z)
55ca9087
RBR
30 }
31
32 function crc(i) {
23398f9a 33 var p = pos(i)
5054447d 34 B()
23398f9a 35 X.arc(p.x, p.y, z / 2, 0, 2 * PI)
5054447d 36 F()
55ca9087
RBR
37 }
38
39 function chs(i) {
e5d21bf4 40 var {x, y} = pos(i)
5054447d 41 B()
e5d21bf4
RBR
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))
5054447d 45 F()
55ca9087
RBR
46 }
47
48 function lch(i) {
e5d21bf4 49 var {x, y} = pos(i)
5054447d 50 B()
e5d21bf4 51 X.lineWidth = 5
a20337ef 52 X.strokeStyle = `hsl(0,0%,${i.c}%)`
e5d21bf4
RBR
53 X.moveTo(x, y)
54 X.lineTo(x * 10 * C(i.a), y * 10 * S(i.a))
55 X.stroke()
55ca9087
RBR
56 }
57
58 function hrt(i) {
e5d21bf4
RBR
59 var {x, y} = pos(i)
60 X.fillRect(x, y, z, z)
55ca9087 61
5054447d 62 B()
e5d21bf4 63 X.arc(x + z / 2, y, z / 2, 0, 2 * PI, false)
5054447d 64 F()
e5d21bf4 65 X.closePath()
55ca9087 66
5054447d 67 B()
e5d21bf4 68 X.arc(x + z, y + z / 2, z / 2, 0, 2 * PI, false)
5054447d 69 F()
e5d21bf4 70 X.closePath()
55ca9087
RBR
71 }
72
73 function mov(i) {
e5d21bf4
RBR
74 i.x = i.x + s * C(i.a)
75 i.y = i.y + s * S(i.a)
55ca9087
RBR
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) {
e5fa1fb8 86 i.c = (i.c + 2) % 101
55ca9087
RBR
87 }
88
89 function frm(t) {
b8bcdb6d 90 if (Z) requestAnimationFrame(frm)
55ca9087
RBR
91
92 var dt = t - l
93
94 if (dt > 1000 / f) {
e5d21bf4 95 k && X.clearRect(0, 0, 64, 64)
55ca9087 96 for (var i of I) {
a20337ef 97 X.fillStyle = `hsl(0,0%,${i.c}%)`
55ca9087 98 draw(i)
e5d21bf4 99 c && ucl(i)
55ca9087
RBR
100 mov(i)
101 }
102
103 l = t
104 }
105 }
21b1c71d 106
40af835c 107 canvas.addEventListener('click', () => (Z ? Z=0:(Z=1&&frm())))
98bec09c 108 frm()
21b1c71d 109}
98bec09c 110)()