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