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