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