]>
git.r.bdr.sh - rbdr/r.bdr.sh/blob - js/animation.js
3 var canvas
= document
.querySelector('canvas'),
8 context
= canvas
.getContext('2d'),
13 clear
= !!(r() > 0.5),
14 changeColor
= !!(r() > 0.5),
15 items
= Array(rnd(10) + 6).fill(null).map(() => ({
19 color: Array(3).fill(rnd(256))
21 shapes
= [sqr
, crc
, hrt
, chs
, lch
],
22 positions
= [id
, spn(5), spn(15)],
23 draw
= shapes
[rnd(shapes
.length
)],
24 pos
= positions
[rnd(positions
.length
)];
27 return Math
.floor(r() * x
);
33 context
.fillRect(p
.x
, p
.y
, size
, size
);
40 context
.arc(p
.x
, p
.y
, size
/ 2, 0, 2 * pi
);
48 context
.moveTo(p
.x
, p
.y
);
49 context
.lineTo(p
.x
* 10 * cos(i
.angle
), p
.y
* 10 * sin(i
.angle
));
50 context
.lineTo(rnd(64), rnd(64));
58 context
.lineWidth
= 5;
59 context
.strokeStyle
= `rgb(${i.color.join(',')})`;
60 context
.moveTo(p
.x
, p
.y
);
61 context
.lineTo(p
.x
* 10 * cos(i
.angle
), p
.y
* 10 * sin(i
.angle
));
68 context
.fillRect(p
.x
, p
.y
, size
, size
);
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);
96 x: p
.x
+ rad
* cos(lastFrame
),
97 y: p
.y
+ rad
* sin(lastFrame
)
104 i
.x
= i
.x
+ speed
* cos(i
.angle
);
105 i
.y
= i
.y
+ speed
* sin(i
.angle
);
107 if (i
.x
< 0 || i
.x
> 64) {
108 i
.angle
= Math
.atan2(sin(i
.angle
), -cos(i
.angle
))
111 if (i
.y
< 0 || i
.y
> 64) {
112 i
.angle
= Math
.atan2(-sin(i
.angle
), cos(i
.angle
))
116 function updateColor(i
) {
118 i
.color
= i
.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 i
of items
) {
133 context
.fillStyle
= `rgb(${i.color.join(',')})`;
135 changeColor
&& updateColor(i
);