Callback context

You can avoid needing to create global variables by using instance mode.

Try the following:

new p5((p) => {
  let x = 100;
  let img;
  p.preload = () => {
    img = p.loadImage("birch.png");
  };
  p.setup = () => {
    p.createCanvas(400, 400);
  };

  p.draw = () => {
    p.image(img,x,200,100,100);
  };
});

See this reference material:

1 Like