]>
git.r.bdr.sh - rbdr/r.bdr.sh/blob - js/animation.js
907df5db1c635442bf43002f352a236ac446b56c
3 var canvas
= document
.querySelector('canvas'),
4 context
= canvas
.getContext('2d'),
12 var clear
= !!(r() > 0.5);
13 var changeColor
= !!(r() > 0.5);
14 var items
= Array(rand(10) + 6).fill(null).map(() => ({
18 color: Array(3).fill(rand(256))
20 var shapes
= [square
, circle
, heart
, chaos
, lineChaos
];
21 var positions
= [identity
, spin(5), spin(15)];
22 var draw
= shapes
[rand(shapes
.length
)];
23 var position
= positions
[rand(positions
.length
)];
26 return Math
.floor(r() * x
);
29 function square(item
) {
31 var p
= position(item
);
32 context
.fillRect(p
.x
, p
.y
, size
, size
);
35 function circle(item
) {
37 var p
= position(item
);
39 context
.arc(p
.x
, p
.y
, size
/ 2, 0, 2 * pi
);
43 function chaos(item
) {
45 var p
= position(item
);
47 context
.moveTo(p
.x
, p
.y
);
48 context
.lineTo(p
.x
* 10 * Math
.cos(item
.angle
), p
.y
* 10 * Math
.sin(item
.angle
));
49 context
.lineTo(rand(64), rand(64));
53 function lineChaos(item
) {
55 var p
= position(item
);
57 context
.lineWidth
= 5;
58 context
.strokeStyle
= `rgb(${item.color.join(',')})`;
59 context
.moveTo(p
.x
, p
.y
);
60 context
.lineTo(p
.x
* 10 * Math
.cos(item
.angle
), p
.y
* 10 * Math
.sin(item
.angle
));
64 function square(item
) {
66 var p
= position(item
);
67 context
.fillRect(p
.x
, p
.y
, size
, size
);
70 function heart(item
) {
72 var p
= position(item
);
73 context
.fillRect(p
.x
, p
.y
, size
, size
);
76 context
.arc(p
.x
+ size
/ 2, p
.y
, size
/ 2, 0, 2 * pi
, false);
81 context
.arc(p
.x
+ size
, p
.y
+ size
/ 2, size
/ 2, 0, 2 * pi
, false);
86 function identity(position
) {
91 function spin(radius
) {
92 return function (position
) {
95 x: position
.x
+ radius
* Math
.cos(lastFrame
),
96 y: position
.y
+ radius
* Math
.sin(lastFrame
)
101 function move(item
) {
103 item
.x
= item
.x
+ speed
* Math
.cos(item
.angle
);
104 item
.y
= item
.y
+ speed
* Math
.sin(item
.angle
);
106 if (item
.x
< 0 || item
.x
> 64) {
107 item
.angle
= Math
.atan2(Math
.sin(item
.angle
), -Math
.cos(item
.angle
))
110 if (item
.y
< 0 || item
.y
> 64) {
111 item
.angle
= Math
.atan2(-Math
.sin(item
.angle
), Math
.cos(item
.angle
))
115 function updateColor(item
) {
117 item
.color
= item
.color
.map((c
) => {
119 return c
> 255 ? 0 : c
;
123 function frame(time
) {
125 window
.requestAnimationFrame(frame
);
127 var delta
= time
- lastFrame
;
129 if (delta
> 1000 / fps
) {
130 clear
&& context
.clearRect(0, 0, 64, 64);
131 for (var item
of items
) {
132 context
.fillStyle
= `rgb(${item.color.join(',')})`;
134 changeColor
&& updateColor(item
);