forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animated-two-basic-rect.js
71 lines (56 loc) · 2.1 KB
/
animated-two-basic-rect.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const canvasSketch = require('canvas-sketch');
// Import Two.js - make sure to have greater than v0.7.0-alpha.1
// because previous versions don't support module loading or headless environments
const Two = require('two.js');
const settings = {
animate: true
};
const sketch = ({ canvas, width, height, pixelRatio }) => {
// Create the instance of Two.js with settings based on canvas-sketch
const two = new Two({
// Pass the canvas-sketch domElement into Two
domElement: canvas
});
// Create a background rectangle
const background = new Two.Rectangle(0, 0, two.width, two.height);
// Disable outline/stroke
background.noStroke();
// Fill the background with a radial gradient
background.fill = new Two.RadialGradient(0, 0, 1, [
new Two.Stop(0, 'hsl(0, 50%, 75%)'),
new Two.Stop(1, 'hsl(0, 50%, 50%)')
]);
// Create a rectangle element
const rectangle = new Two.Rectangle(0, 0, two.width / 5, two.width / 5);
rectangle.noStroke();
// Add both components to the scene
two.add(background);
two.add(rectangle);
return {
resize ({ pixelRatio, width, height }) {
// Update width and height of Two.js scene based on
// canvas-sketch auto changing viewport parameters
two.width = width;
two.height = height;
two.ratio = pixelRatio;
// This needs to be passed down to the renderer's width and height as well
two.renderer.width = two.width;
two.renderer.height = two.height;
// Reorient the scene to the center of the new canvas dimensions
two.scene.translation.set(two.width / 2, two.height / 2);
// Make the gradient fill the screen
background.fill.radius = Math.max(two.width, two.height) * 0.65;
// Update the background's width and height to adhere
// to the bounds of the canvas.
background.width = two.width;
background.height = two.height;
},
render ({ time }) {
// Rotate the rectangle
rectangle.rotation = time * 2;
// Update two.js via the `render` method - *not* the `update` method.
two.render();
}
};
};
canvasSketch(sketch, settings);