P5.js code for creating a calculator tip app confused

Basically in your code what happens is this:

var img;
image(img, 0, 0);

It doesn’t work because img is empty. One solution is to initialize img with an image that you preloaded:

function setup() {
  img = m;
}

But this would mean that there will be an image by default. Another solution is to “only” draw if img is not empty

function draw() {
  if (img !== undefined) {
    image(img, 0, 0);
  }
}

This won’t draw anything by default.

1 Like