← Teacher guides

Teacher guide: Drawing and animation with p5.js

Draw and animate with JavaScript and p5.js: shapes, movement, decisions, lists, objects and functions, ending in a small game you build yourself.

Open the student course

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

Shapes and movement

Draw on a canvas, make it move every frame, and make decisions.

Shapes and movement in 5 minutes (read before teaching this unit)

The idea in plain words

p5.js draws on a canvas: a rectangle of pixels where (0, 0) is the top left and y counts down. A sketch has two special functions that p5 runs for you. setup() runs once; draw() runs about 60 times a second, and each run is a frame. Movement is a small change to a variable every frame, drawn again where it now is. if and comparisons let the sketch decide, for example to turn round at an edge or to react to mouseX.

Why programmers use it

  • One loop for everything: games, animations and data pictures all repeat "change, then draw".
  • Variables as state: the values the sketch keeps between frames are how it remembers where things are.

An everyday picture, and where it breaks

A flick book: each page is a frame and the movement is the tiny change between them. It breaks because a flick book's pages are fixed, while draw() works out each page as it goes.

Try it yourself (5 minutes)

  1. Run the first sketch and change a number in circle(...).
  2. Move fill(...) below the shape. What happens, and why?
  3. Make a shape move by adding to a variable in draw().

Questions students ask

  • Why is my shape upside down? y counts down from the top, so a bigger y is lower on the canvas.
  • Why did the colour not change? fill only colours shapes drawn after it.
  • Where did my old picture go? background(...) at the start of draw() clears it each frame.

How it connects

This is turtle drawing and the events lesson in JavaScript: the same loops, variables and decisions, with a different way of saying them.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Building blocks

Lists and loops, objects, and functions of your own.

Lists, objects and functions in 5 minutes (read before teaching this unit)

The idea in plain words

Three tools for keeping a sketch short. An array is a list: [40, 90, 150], read by place (heights[0] is the first, because counting starts at 0). An object keeps the values of one thing together: { x: 20, y: 30, speed: 2 }, read with a dot (star.speed). A function is code with a name: you define it once and call it as often as you like, giving it values through parameters and getting an answer back with return. A list of objects, a loop over it, and a function that works on one object make the shape of most games.

Why programmers use it

  • Many things, one piece of code: a hundred stars are a list and a loop, not three hundred variables.
  • Names for ideas: drawFlower(x, y, size) says what the code is for.
  • One place to fix: change a function once and every call changes.

An everyday picture, and where it breaks

A recipe card: the recipe is written once (the definition) and cooked as many times as you like (the calls), with the amounts changed each time (the parameters). It breaks because a recipe is not cooked by writing it; a function does nothing until it is called.

Try it yourself (5 minutes)

  1. Run the stars sketch and add a size property to each star.
  2. Write a function that draws a face, then call it three times with different positions.
  3. Change one capital letter in a call. What does the error say?

Questions students ask

  • Why does it say speed is not defined? The speed belongs to the star: write star.speed.
  • Why does my function draw nothing? It is defined but never called.
  • What is i? The counter of a for loop: 0, then 1, then 2, and so on.

How it connects

These are the lists, objects (without classes) and functions of the Python units, in JavaScript. Next: the final challenge puts them all in one game.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Final challenge: Catch the circle

Build a click-to-catch game with a score, lives and a restart. One optional lesson makes moving art.

A game with a click in 5 minutes (read before teaching this unit)

The idea in plain words

An event function is one p5 calls when something happens: mousePressed() on each click, keyPressed() on each key. The game keeps a list of falling balls (objects). When the player clicks, the event function checks each ball: if dist(mouseX, mouseY, ball.x, ball.y) < 20 the click is close, so the score goes up and the ball restarts. A missed ball costs a life, and at zero lives the game shows GAME OVER until a key starts it again. The optional lesson uses sin, cos and map to make moving patterns.

Why programmers use it

  • Events are how every interactive program waits for people.
  • Distance checks decide what touched what in most 2D games.
  • Playtesting shows what is too hard, too easy or confusing.

An everyday picture, and where it breaks

A doorbell: nothing happens until someone presses it, and then a short piece of code runs. It breaks because the doorbell rings once, while draw() also keeps running between clicks.

Try it yourself (5 minutes)

  1. Play the game and change < 20 to < 40. Is it easier or harder?
  2. Add a lives variable and take one away when a ball falls off the bottom.
  3. Give one ball a property that makes it worth more points.

Questions students ask

  • Why does every click score? The test is probably > where it should be <.
  • Why does pressing R do nothing? Click the picture first so it receives the keys.
  • Why does the score jump by lots? The ball was not restarted, so it scores again.

How it connects

This is Catch the turtle in JavaScript, and a first game for the microStudio course. Beyond it: p5.js's own reference, or the DOM course for changing real web pages.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown