]> git.r.bdr.sh - rbdr/r.bdr.sh/blame - js/animation.js
Simplify reduce motion check
[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
b8bcdb6d
RBR
89 canvas.addEventListener('click', () => {Z ? Z=0:(Z=1;frm())})
90
55ca9087 91 function frm(t) {
b8bcdb6d 92 if (Z) requestAnimationFrame(frm)
55ca9087
RBR
93
94 var dt = t - l
95
96 if (dt > 1000 / f) {
e5d21bf4 97 k && X.clearRect(0, 0, 64, 64)
55ca9087 98 for (var i of I) {
a20337ef 99 X.fillStyle = `hsl(0,0%,${i.c}%)`
55ca9087 100 draw(i)
e5d21bf4 101 c && ucl(i)
55ca9087
RBR
102 mov(i)
103 }
104
105 l = t
106 }
107 }
21b1c71d 108
98bec09c 109 frm()
21b1c71d 110}
98bec09c 111)()