]>
git.r.bdr.sh - rbdr/r.bdr.sh/blob - js/animation.js
f7642ee11bef970c718f5bcacd72159c6e3892b1
3 var canvas
= document
.querySelector('canvas'),
8 context
= canvas
.getContext('2d'),
13 clear
= !!(r() > 0.5),
14 changeColor
= !!(r() > 0.5),
15 items
= Array(rand(10) + 6).fill(null).map(() => ({
19 color: Array(3).fill(rand(256))
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
)];
27 return Math
.floor(r() * x
);
30 function square(item
) {
32 var p
= position(item
);
33 context
.fillRect(p
.x
, p
.y
, size
, size
);
36 function circle(item
) {
38 var p
= position(item
);
40 context
.arc(p
.x
, p
.y
, size
/ 2, 0, 2 * pi
);
44 function chaos(item
) {
46 var p
= position(item
);
48 context
.moveTo(p
.x
, p
.y
);
49 context
.lineTo(p
.x
* 10 * cos(item
.angle
), p
.y
* 10 * sin(item
.angle
));
50 context
.lineTo(rand(64), rand(64));
54 function lineChaos(item
) {
56 var p
= position(item
);
58 context
.lineWidth
= 5;
59 context
.strokeStyle
= `rgb(${item.color.join(',')})`;
60 context
.moveTo(p
.x
, p
.y
);
61 context
.lineTo(p
.x
* 10 * cos(item
.angle
), p
.y
* 10 * sin(item
.angle
));
65 function square(item
) {
67 var p
= position(item
);
68 context
.fillRect(p
.x
, p
.y
, size
, size
);
71 function heart(item
) {
73 var p
= position(item
);
74 context
.fillRect(p
.x
, p
.y
, size
, size
);
77 context
.arc(p
.x
+ size
/ 2, p
.y
, size
/ 2, 0, 2 * pi
, false);
82 context
.arc(p
.x
+ size
, p
.y
+ size
/ 2, size
/ 2, 0, 2 * pi
, false);
87 function identity(position
) {
92 function spin(radius
) {
93 return function (position
) {
96 x: position
.x
+ radius
* cos(lastFrame
),
97 y: position
.y
+ radius
* sin(lastFrame
)
102 function move(item
) {
104 item
.x
= item
.x
+ speed
* cos(item
.angle
);
105 item
.y
= item
.y
+ speed
* sin(item
.angle
);
107 if (item
.x
< 0 || item
.x
> 64) {
108 item
.angle
= Math
.atan2(sin(item
.angle
), -cos(item
.angle
))
111 if (item
.y
< 0 || item
.y
> 64) {
112 item
.angle
= Math
.atan2(-sin(item
.angle
), cos(item
.angle
))
116 function updateColor(item
) {
118 item
.color
= item
.color
.map((c
) => {
120 return c
> 255 ? 0 : c
;
124 function frame(time
) {
126 window
.requestAnimationFrame(frame
);
128 var delta
= time
- lastFrame
;
130 if (delta
> 1000 / fps
) {
131 clear
&& context
.clearRect(0, 0, 64, 64);
132 for (var item
of items
) {
133 context
.fillStyle
= `rgb(${item.color.join(',')})`;
135 changeColor
&& updateColor(item
);