]>
git.r.bdr.sh - rbdr/r.bdr.sh/blob - js/animation.js
fc6246ec2d6e914cd6e649909b33aca52816263a
3 var canvas
= document
.querySelector('canvas'),
4 context
= canvas
.getContext('2d'),
10 var clear
= !!(Math
.random() > 0.5);
11 var changeColor
= !!(Math
.random() > 0.5);
12 var items
= Array(rand(10) + 6).fill(null).map(() => ({
15 angle: Math
.random() * 2 * Math
.PI
,
16 color: Array(3).fill(rand(256))
18 var shapes
= [square
, circle
, heart
, chaos
, lineChaos
];
19 var positions
= [identity
, spin(5), spin(15)];
20 var draw
= shapes
[rand(shapes
.length
)];
21 var position
= positions
[rand(positions
.length
)];
24 return Math
.floor(Math
.random() * x
);
27 function square(item
) {
29 var p
= position(item
);
30 context
.fillRect(p
.x
, p
.y
, size
, size
);
33 function circle(item
) {
35 var p
= position(item
);
37 context
.arc(p
.x
, p
.y
, size
/ 2, 0, 2 * Math
.PI
);
41 function chaos(item
) {
43 var p
= position(item
);
45 context
.moveTo(p
.x
, p
.y
);
46 context
.lineTo(p
.x
* 10 * Math
.cos(item
.angle
), p
.y
* 10 * Math
.sin(item
.angle
));
47 context
.lineTo(rand(64), rand(64));
51 function lineChaos(item
) {
53 var p
= position(item
);
55 context
.lineWidth
= 5;
56 context
.strokeStyle
= `rgb(${item.color.join(',')})`;
57 context
.moveTo(p
.x
, p
.y
);
58 context
.lineTo(p
.x
* 10 * Math
.cos(item
.angle
), p
.y
* 10 * Math
.sin(item
.angle
));
62 function square(item
) {
64 var p
= position(item
);
65 context
.fillRect(p
.x
, p
.y
, size
, size
);
68 function heart(item
) {
70 var p
= position(item
);
71 context
.fillRect(p
.x
, p
.y
, size
, size
);
74 context
.arc(p
.x
+ size
/ 2, p
.y
, size
/ 2, 0, 2 * Math
.PI
, false);
79 context
.arc(p
.x
+ size
, p
.y
+ size
/ 2, size
/ 2, 0, 2 * Math
.PI
, false);
84 function identity(position
) {
89 function spin(radius
) {
90 return function (position
) {
93 x: position
.x
+ radius
* Math
.cos(lastFrame
),
94 y: position
.y
+ radius
* Math
.sin(lastFrame
)
101 item
.x
= item
.x
+ speed
* Math
.cos(item
.angle
);
102 item
.y
= item
.y
+ speed
* Math
.sin(item
.angle
);
104 if (item
.x
< 0 || item
.x
> 64) {
105 item
.angle
= Math
.atan2(Math
.sin(item
.angle
), -Math
.cos(item
.angle
))
108 if (item
.y
< 0 || item
.y
> 64) {
109 item
.angle
= Math
.atan2(-Math
.sin(item
.angle
), Math
.cos(item
.angle
))
113 function updateColor(item
) {
115 item
.color
= item
.color
.map((c
) => {
117 return c
> 255 ? 0 : c
;
121 function frame(time
) {
123 window
.requestAnimationFrame(frame
);
125 var delta
= time
- lastFrame
;
127 if (delta
> 1000 / fps
) {
128 clear
&& context
.clearRect(0, 0, 64, 64);
129 for (var item
of items
) {
130 context
.fillStyle
= `rgb(${item.color.join(',')})`;
132 changeColor
&& updateColor(item
);