← Drawing and animation with p5.js

Making it move

p5 calls draw() about 60 times a second. A variable that changes a little each time makes a shape move.

An animation is a picture redrawn very quickly, a tiny bit different each time. p5 does the redrawing for you.

Last lesson's sketch drew once. function draw() { ... } is different: p5 runs it again and again, about 60 times a second, for as long as the sketch runs. Each pass is a frame.

Two parts make movement:

background(...) at the start of draw() paints over the last frame, so only the newest picture shows.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

sketch.js
let x = 0;

function setup() {
  createCanvas(300, 200);
}

function draw() {
  background(20, 30, 60);
  circle(x, 100, 40);
  x = x + 2;
}

You never call draw(). Who does, and how often?

What does the circle do?

RunRun it and compare the result with your prediction.

Run

The number in the corner is x. Press Run again to start from the beginning. Change x + 2 to x + 5, then run it again.

sketch.js
let x = 0;

function setup() {
  createCanvas(300, 200);
}

function draw() {
  background(20, 30, 60);
  fill(255, 210, 0);
  circle(x, 100, 40);
  fill(255);
  text(x, 10, 20);
  x = x + 2;
}
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

sketch.js
let x = 0;

function setup() {
  createCanvas(300, 200);
}

function draw() {
  background(20, 30, 60);
  fill(255, 210, 0);
  circle(x, 100, 40);
  fill(255);
  text(x, 10, 20);
  x = x + 2;
}

Follow x for four frames.

Why is there a background in draw?

A circle that stays still.

x starts at 0. Fill in where the circle is drawn, and what x is after each frame.

framecircle drawn at xx after the frame
102
2
3
4

What would happen if background(20, 30, 60) was removed from draw()?

The circle never moves, however long you wait. Click the line that is wrong.

ModifyChange the code on purpose and check the result.

Modify

Make the circle fall down the canvas instead of sliding across.

Need a hint?

Use a different variable for the other direction. y counts down.

Another hint

Make let y = 0; at the top, draw circle(150, y, 40), and add 2 to y each frame.

Add a second circle that moves at a different speed.

Need a hint?

A second shape needs its own variable, made at the top and changed in draw().

Another hint

Give it a different step, for example y = y + 4, so it moves faster, and a different place to start.

Try it yourselfYour edits are saved in this browser.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Two things that move

Make at least two things move in different ways: one faster than the other, one going the other direction, one growing. The sketch gives you t, a number that goes up by 1 every frame. circle(t, ...) slides, circle(300 - t, ...) slides the other way, and circle(150, 100, t) grows. A % operator is the remainder: t % 300 goes from 0 up to 299, then starts again.

Try it yourselfYour edits are saved in this browser.

Info

Where this leads

You have a loop that runs 60 times a second and numbers that change. Next: make the sketch decide, so a circle can bounce.

A circle slides off the right edge and is never seen again. What does the sketch need to do?

For teachers

Students meet draw() (about 60 times a second) and variables that keep their value between frames, follow a circle's x frame by frame, see why background belongs in draw, fix a variable made inside draw, then move things down and at different speeds. About 50 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Read the unit primer first. The big idea is that draw() runs by itself, again and again; students never call it. A variable made at the top (outside functions) keeps its value between frames, and x = x + 2 reads as "make x two bigger", not as a maths equation. Students who did Python Foundations will recognise x = x + 1 from the variables lesson; the new part is that the loop is built in. The let in the bug task is the classic mistake: a variable made inside draw() starts again every frame. % (remainder) appears only in the Make step's text. The playground's number in the corner is text(x, 10, 20).

Running the lesson

  1. Predict: ask a student to act as p5, saying 'frame' and stepping a classmate two steps right each time.
  2. Run: change the 2 to a 5 and ask why it is faster.
  3. Investigate: fill the trace together before students do: x is drawn first, then changed.
  4. Modify: ask which variable moves which circle before they run it.
  5. Make: choose the two ideas as a pair, then each makes one.

The hard part (on purpose)

Seeing that draw() repeats, so a variable made inside it starts again. The trace table and the let-in-draw bug are for that.

When students get stuck

Stretch and support

Stretch: Make a circle that moves in both directions at once (x and y), or one that grows and shrinks.

Support: Give students the sketch with let y = 0; already made, and ask for the draw() changes only.

Where it leads

Next lesson: if, so circles can bounce, and the mouse.

What each step is for

Now you can
Next lesson3. Deciding what to doUse if and else to bounce a circle off the edges, change its colour, and follow the mouse.