Skip to content

Language Settings

Instantiation

Create a p5 instance, which keeps all variables out of the global scope of your page.

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
let sketch = function(p) {
  let x = 100;
  let y = 100;
  p.setup = function() {
    p.createCanvas(700, 410);
  };
  p.draw = function() {
    p.background(0);
    p.fill(255);
    p.rect(x, y, 50, 50);
  };
};
let myp5 = new p5(sketch);
// Compare to "global mode"
// let x = 100;
// let y = 100;
// function setup() {
//   createCanvas(200,200);
// }
// function draw() {
//   background(0);
//   fill(255);
//   ellipse(x,y,50,50);
// }
X

creative commons license