p5.js Session 17: Names, Versions and What Comes Next

The two things that trip up every beginner, variable names p5.js has already taken and code written for an older version, then a look back over the series.

Sixteen sessions of drawing, and two things have interrupted us more than anything else. Neither is about art. Both are about the gap between the code you write and the code p5.js already has.

This last session covers both, then looks back at the ground we have crossed.

Words p5.js has already taken

p5.js gives you hundreds of functions, and a lot of them are ordinary English words. hue, red, green, blue, alpha, saturation, brightness, text, line, key, push, pop, step, size, scale. Every one of those is already a function.

Name a variable after one of them and you have taken the name away from p5.js.

function setup() {
  createCanvas(600, 600);
  colorMode(HSB, 360, 100, 100);
  noLoop();
  noStroke();
}

function draw() {
  background(0);
  let hue = 200;
  fill(hue, 80, 90);
  circle(300, 300, 200);
}

The p5.js editor with a blue circle on the canvas and a warning in the Console saying the function hue is being redeclared and conflicts with a p5.js function

The habit worth forming is small: read the Console even when the sketch looks fine. Nothing was visibly wrong with that circle, and p5.js was still telling us we had taken one of its names.

Telling which p5.js you are running

The other interruption is older code. p5.js 2.0 renamed and removed things, and the internet is full of tutorials written before it.

You will meet this most often with curveVertex(), which drew a curve through a set of points. Here is a sketch of exactly the kind you will find all over the web. It was correct for years.

function setup() {
  createCanvas(600, 600);
  noLoop();
  noStroke();
  fill(250, 250, 50);
}

function draw() {
  background(0);
  beginShape();
  curveVertex(150, 450);
  curveVertex(150, 450);
  curveVertex(300, 150);
  curveVertex(450, 450);
  curveVertex(450, 450);
  endShape();
}

The p5.js editor with an empty canvas and the console reading ReferenceError: curveVertex is not defined

So how do you know which p5.js you are working in, and whether a tutorial you have found is older than it?

Sixteen sessions, looking back

Nothing in that list is advanced. It is the whole vocabulary of an enormous amount of generative art, and every piece of it fits in one page of code.

What comes next

A second series is coming, on harder ground: Perlin noise, flow fields, recursion, and the algorithms that make generative work look organic rather than assembled.

Until then, the most useful thing you can do is stop following. Take the Session 14 grid, change the palette, change the shapes, break it, and see what falls out. That is the whole discipline.

The scripts in this session were written for p5.js 2.3.2, the version the web editor runs by default.


That is the series. Thank you for building along with it.