-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathanimated-simple-2d.js
46 lines (39 loc) · 1.25 KB
/
animated-simple-2d.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* A Canvas2D example of a basic animation.
* @author Matt DesLauriers (@mattdesl)
*/
const canvasSketch = require('canvas-sketch');
const settings = {
// Enable an animation loop
animate: true,
// Set loop duration to 3 seconds
duration: 3,
// Use a small size for our GIF output
dimensions: [ 512, 512 ],
// Optionally specify an export frame rate, defaults to 30
fps: 30
};
// Start the sketch
canvasSketch(() => {
return ({ context, width, height, playhead }) => {
// Fill the canvas with pink
context.fillStyle = 'pink';
context.fillRect(0, 0, width, height);
// Get a seamless 0..1 value for our loop
const t = Math.sin(playhead * Math.PI);
// Animate the thickness with 'playhead' prop
const thickness = Math.max(5, Math.pow(t, 0.55) * width * 0.5);
// Rotate with PI to create a seamless animation
const rotation = playhead * Math.PI;
// Draw a rotating white rectangle around the center
const cx = width / 2;
const cy = height / 2;
const length = height * 0.5;
context.fillStyle = 'white';
context.save();
context.translate(cx, cy);
context.rotate(rotation);
context.fillRect(-thickness / 2, -length / 2, thickness, length);
context.restore();
};
}, settings);