Save a sketch to image with p5.js

saveCanvas() is purpose-built for this. Here are some ways to use it:

saveCanvas("myCanvas", "jpg");
saveCanvas(c, "myCanvas.jpg");
saveCanvas("myCanvas.jpg");
saveCanvas();

I tend to nest it under a keyboard command function:

// Saves the canvas if the 's' key is pressed
function keyPressed() {
  if (keyCode === 83) {
    saveCanvas();
  }
}

You can find the keyCode value for keys on keycode.info.