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