forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimated-p5.js
54 lines (46 loc) · 1.19 KB
/
animated-p5.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
/**
* A p5.js example integrating with canvas-sketch.
* Here, canvas-sketch handles the frame loop, resizing
* and exporting.
* @author Matt DesLauriers (@mattdesl)
*/
const canvasSketch = require('canvas-sketch');
// Grab P5.js from npm
const p5 = require('p5');
// Attach p5.js it to global scope
new p5()
const settings = {
// Tell canvas-sketch we're using p5.js
p5: true,
// Turn on a render loop (it's off by default in canvas-sketch)
animate: true,
// We can specify WebGL context if we want
context: 'webgl',
// Optional loop duration
duration: 6,
// Enable MSAA
attributes: {
antialias: true
}
};
// Optionally preload before you load the sketch
window.preload = () => {
// Preload sounds/images/etc...
};
canvasSketch(() => {
// Inside this is a bit like p5.js 'setup' function
// ...
// Attach events to window to receive them
window.mouseClicked = () => {
console.log('Mouse clicked');
};
// Return a renderer to 'draw' the p5.js content
return ({ playhead, width, height }) => {
// Draw with p5.js things
clear()
normalMaterial();
rotateX(playhead * 2 * PI);
rotateZ(playhead * 2 * PI);
cylinder(20, 50);
};
}, settings);