← Drawing and animation with p5.js

Drawing and animation with p5.js: recap

For after the lessons: one card per lesson, to check what you remember. Answer each question in your head or on paper first, then check it. Nothing here is marked.

Shapes and movement

1. Shapes on a canvas

Try firstWhich corner is (0, 0), and which way does y count?

Check your answer

The top left; y counts downward.

  1. createCanvas(300, 200) makes the drawing area.
  2. A shape command gives its place and size: circle(150, 100, 80) is middle 150 across, 100 down, 80 wide.
  3. fill(r, g, b) colours the shapes drawn after it. Later shapes are drawn on top.
What this sketch draws
What this code makes
A canvas is a grid of pixels, and shapes go where you say · Sound on for the voiceover
Open the lesson →

2. Making it move

Try firstWhy is let x = 0; at the top and not inside draw()?

Check your answer

Inside draw() it would be made again every frame and go back to 0; at the top it keeps its value.

  1. draw() runs about 60 times a second, all by itself.
  2. A variable made at the top keeps its value between frames.
  3. Each frame changes it a little (x = x + 2) and draws with it, so the shape moves.
What this sketch draws
What this code makes
draw runs again and again, so changing a number moves the shape · Sound on for the voiceover
Open the lesson →

3. Deciding what to do

Try firstWhat is the difference between = and === ?

Check your answer

One = sets a variable to a value; three === asks whether two values are equal.

  1. if (question) { ... } runs the code only when the question is true; else is for when it is not.
  2. Compare with >, < and ===; join questions with || (or) and && (and).
  3. speed = -speed turns a movement round, so the circle bounces.
What this sketch draws
What this code makes
An if lets the sketch decide, and turns the circle round at an edge · Sound on for the voiceover
Open the lesson →

Building blocks

4. Lists and loops

Try firstIn heights[i], what is i, and where does it start?

Check your answer

i is the place number of the value in the array, and the first place is 0.

  1. An array holds many values: heights[2] is the value at place 2, counting from 0.
  2. for (let i = 0; i < heights.length; i++) runs its braces once for every place.
  3. Inside the loop heights[i] is the current value, so one rect line draws every bar.
What this sketch draws
What this code makes
A list holds many values, and a loop draws one bar for each · Sound on for the voiceover
Open the lesson →

5. Objects and stars

Try firstWhat does star.speed mean inside the loop?

Check your answer

The speed property of the star the loop is currently on.

  1. An object holds named values: { x: 20, y: 30, speed: 2 }.
  2. A property is read with a dot: star.y, and changed the same way.
  3. for (let star of stars) does the braces once for each object in the list.
What this sketch draws
What this code makes
An object keeps one star's values together · Sound on for the voiceover
Open the lesson →

6. Functions of your own

Try firstWhat is the difference between defining a function and calling it?

Check your answer

Defining writes the recipe and draws nothing; calling uses it with values and does the work.

  1. function drawFlower(x, y, size) { ... } defines the recipe and draws nothing.
  2. drawFlower(70, 100, 20); calls it, filling in x, y and size in order.
  3. return hands an answer back, so a call can sit inside an if.
What this sketch draws
What this code makes
A function gives code a name, and parameters make each use different · Sound on for the voiceover
Open the lesson →

Final challenge: Catch the circle

7. Final challenge: Catch the circle

Try firstWhy is the catch test dist(...) < 20 and not > 20?

Check your answer

Two things touch when the distance between them is small.

  1. mousePressed() is an event function: p5 runs it for each click.
  2. dist(mouseX, mouseY, ball.x, ball.y) < 20 is true when the click is near the ball.
  3. A catch changes score and calls restart(ball); missing the ball loses a life.
What this sketch draws
What this code makes
A click is an event, and a small distance means a catch · Sound on for the voiceover
Open the lesson →