]>
git.r.bdr.sh - rbdr/r.bdr.sh/blob - js/animation.js
3 var canvas
= document
.querySelector('canvas'),
8 context
= canvas
.getContext('2d'),
14 var clear
= !!(r() > 0.5);
15 var changeColor
= !!(r() > 0.5);
16 var items
= Array(rand(10) + 6).fill(null).map(() => ({
20 color: Array(3).fill(rand(256))
22 var shapes
= [square
, circle
, heart
, chaos
, lineChaos
];
23 var positions
= [identity
, spin(5), spin(15)];
24 var draw
= shapes
[rand(shapes
.length
)];
25 var position
= positions
[rand(positions
.length
)];
28 return Math
.floor(r() * x
);
31 function square(item
) {
33 var p
= position(item
);
34 context
.fillRect(p
.x
, p
.y
, size
, size
);
37 function circle(item
) {
39 var p
= position(item
);
41 context
.arc(p
.x
, p
.y
, size
/ 2, 0, 2 * pi
);
45 function chaos(item
) {
47 var p
= position(item
);
49 context
.moveTo(p
.x
, p
.y
);
50 context
.lineTo(p
.x
* 10 * cos(item
.angle
), p
.y
* 10 * sin(item
.angle
));
51 context
.lineTo(rand(64), rand(64));
55 function lineChaos(item
) {
57 var p
= position(item
);
59 context
.lineWidth
= 5;
60 context
.strokeStyle
= `rgb(${item.color.join(',')})`;
61 context
.moveTo(p
.x
, p
.y
);
62 context
.lineTo(p
.x
* 10 * cos(item
.angle
), p
.y
* 10 * sin(item
.angle
));
66 function square(item
) {
68 var p
= position(item
);
69 context
.fillRect(p
.x
, p
.y
, size
, size
);
72 function heart(item
) {
74 var p
= position(item
);
75 context
.fillRect(p
.x
, p
.y
, size
, size
);
78 context
.arc(p
.x
+ size
/ 2, p
.y
, size
/ 2, 0, 2 * pi
, false);
83 context
.arc(p
.x
+ size
, p
.y
+ size
/ 2, size
/ 2, 0, 2 * pi
, false);
88 function identity(position
) {
93 function spin(radius
) {
94 return function (position
) {
97 x: position
.x
+ radius
* cos(lastFrame
),
98 y: position
.y
+ radius
* sin(lastFrame
)
103 function move(item
) {
105 item
.x
= item
.x
+ speed
* cos(item
.angle
);
106 item
.y
= item
.y
+ speed
* sin(item
.angle
);
108 if (item
.x
< 0 || item
.x
> 64) {
109 item
.angle
= Math
.atan2(sin(item
.angle
), -cos(item
.angle
))
112 if (item
.y
< 0 || item
.y
> 64) {
113 item
.angle
= Math
.atan2(-sin(item
.angle
), cos(item
.angle
))
117 function updateColor(item
) {
119 item
.color
= item
.color
.map((c
) => {
121 return c
> 255 ? 0 : c
;
125 function frame(time
) {
127 window
.requestAnimationFrame(frame
);
129 var delta
= time
- lastFrame
;
131 if (delta
> 1000 / fps
) {
132 clear
&& context
.clearRect(0, 0, 64, 64);
133 for (var item
of items
) {
134 context
.fillStyle
= `rgb(${item.color.join(',')})`;
136 changeColor
&& updateColor(item
);