]> git.r.bdr.sh - rbdr/r.bdr.sh/blame - js/animation.js
Use Hoefler text
[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,
3b6787e4
RBR
8 context = canvas.getContext('2d'),
9 fps = 30,
10 lastFrame = 0,
11 speed = 1,
83c0fa69
RBR
12 size = 5 + rand(10),
13 clear = !!(r() > 0.5),
14 changeColor = !!(r() > 0.5),
15 items = Array(rand(10) + 6).fill(null).map(() => ({
16 x: rand(64),
17 y: rand(64),
18 angle: r() * 2 * pi,
19 color: Array(3).fill(rand(256))
20 })),
21 shapes = [square, circle, heart, chaos, lineChaos],
22 positions = [identity, spin(5), spin(15)],
23 draw = shapes[rand(shapes.length)],
24 position = positions[rand(positions.length)];
21b1c71d 25
3b6787e4 26 function rand (x) {
8f2ed424 27 return Math.floor(r() * x);
21b1c71d
RBR
28 }
29
30 function square(item) {
31
32 var p = position(item);
33 context.fillRect(p.x, p.y, size, size);
34 }
35
36 function circle(item) {
37
38 var p = position(item);
39 context.beginPath();
8f2ed424 40 context.arc(p.x, p.y, size / 2, 0, 2 * pi);
21b1c71d
RBR
41 context.fill();
42 }
43
44 function chaos(item) {
45
46 var p = position(item);
47 context.beginPath();
48 context.moveTo(p.x, p.y);
08418543 49 context.lineTo(p.x * 10 * cos(item.angle), p.y * 10 * sin(item.angle));
e99de7cc 50 context.lineTo(rand(64), rand(64));
21b1c71d
RBR
51 context.fill();
52 }
53
54 function lineChaos(item) {
55
56 var p = position(item);
57 context.beginPath();
58 context.lineWidth = 5;
59 context.strokeStyle = `rgb(${item.color.join(',')})`;
60 context.moveTo(p.x, p.y);
08418543 61 context.lineTo(p.x * 10 * cos(item.angle), p.y * 10 * sin(item.angle));
21b1c71d
RBR
62 context.stroke();
63 }
64
65 function square(item) {
66
67 var p = position(item);
68 context.fillRect(p.x, p.y, size, size);
69 }
70
71 function heart(item) {
72
73 var p = position(item);
74 context.fillRect(p.x, p.y, size, size);
75
76 context.beginPath();
8f2ed424 77 context.arc(p.x + size / 2, p.y, size / 2, 0, 2 * pi, false);
21b1c71d
RBR
78 context.fill();
79 context.closePath();
80
81 context.beginPath();
8f2ed424 82 context.arc(p.x + size, p.y + size / 2, size / 2, 0, 2 * pi, false);
21b1c71d
RBR
83 context.fill();
84 context.closePath();
85 }
86
87 function identity(position) {
88
89 return position;
90 }
91
92 function spin(radius) {
93 return function (position) {
94
95 return {
08418543
RBR
96 x: position.x + radius * cos(lastFrame),
97 y: position.y + radius * sin(lastFrame)
21b1c71d
RBR
98 }
99 }
100 }
101
102 function move(item) {
103
08418543
RBR
104 item.x = item.x + speed * cos(item.angle);
105 item.y = item.y + speed * sin(item.angle);
21b1c71d 106
e99de7cc 107 if (item.x < 0 || item.x > 64) {
08418543 108 item.angle = Math.atan2(sin(item.angle), -cos(item.angle))
21b1c71d
RBR
109 }
110
e99de7cc 111 if (item.y < 0 || item.y > 64) {
08418543 112 item.angle = Math.atan2(-sin(item.angle), cos(item.angle))
21b1c71d
RBR
113 }
114 }
115
116 function updateColor(item) {
117
118 item.color = item.color.map((c) => {
119 c = c + 5;
120 return c > 255 ? 0 : c;
121 })
122 }
123
124 function frame(time) {
125
126 window.requestAnimationFrame(frame);
127
128 var delta = time - lastFrame;
129
130 if (delta > 1000 / fps) {
e99de7cc 131 clear && context.clearRect(0, 0, 64, 64);
21b1c71d
RBR
132 for (var item of items) {
133 context.fillStyle = `rgb(${item.color.join(',')})`;
134 draw(item);
135 changeColor && updateColor(item);
136 move(item);
137 }
138
139 lastFrame = time;
140 }
141 }
142
143 frame();
144}
145)();