← Drawing and animation with p5.js

Moving art

Use sin and cos to move things in circles, map to turn one range into another, and make a pattern of your own.

A few lines of maths can make a spinning ring of colour, with no pictures drawn by hand.

cos(angle) and sin(angle) give the x and y of a point going round a circle, and map(value, a, b, c, d) turns a number in the range a to b into the matching number in the range c to d. Together with a loop, they make patterns that move.

The hard part is the loop variable. Each circle gets its own i, and that i changes its angle and its colour, so every circle is different.

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
function setup() {
  createCanvas(300, 300);
  background(20, 30, 60);
  for (let i = 0; i < 12; i++) {
    let angle = i * 30;
    circle(150 + cos(radians(angle)) * 100, 150 + sin(radians(angle)) * 100, 20);
  }
}

Twelve angles of 30 degrees make a full turn.

What shape do the 12 circles make?

RunRun it and compare the result with your prediction.

Run

frameCount goes up every frame, so each angle grows and the ring turns.

sketch.js
function setup() {
  createCanvas(300, 300);
  noStroke();
}

function draw() {
  background(20, 30, 60);
  for (let i = 0; i < 12; i++) {
    let angle = frameCount * 0.02 + i * 0.5;
    let x = 150 + cos(angle) * 100;
    let y = 150 + sin(angle) * 100;
    fill(map(i, 0, 11, 255, 120), map(i, 0, 11, 120, 220), 200);
    circle(x, y, 20);
  }
}
Try it yourselfYour edits are saved in this browser.

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

Investigate

sketch.js
function setup() {
  createCanvas(300, 300);
  noStroke();
}

function draw() {
  background(20, 30, 60);
  for (let i = 0; i < 12; i++) {
    let angle = frameCount * 0.02 + i * 0.5;
    let x = 150 + cos(angle) * 100;
    let y = 150 + sin(angle) * 100;
    fill(map(i, 0, 11, 255, 120), map(i, 0, 11, 120, 220), 200);
    circle(x, y, 20);
  }
}

The one number that changes.

Maths in the loop.

What makes the ring turn?

Match each piece with what it does.

cos(angle) * 100
150 + ...
map(i, 0, 11, 255, 120)
frameCount * 0.02

ModifyChange the code on purpose and check the result.

Modify

Leave trails behind the circles.

Need a hint?

background takes a fourth number for how solid it is, up to 255.

Another hint

Try background(20, 30, 60, 30) so old circles fade slowly.

Spread the circles into a spiral.

Need a hint?

Use a radius that grows with i instead of the fixed 100.

Another hint

let radius = 10 + i * 5; and use radius in place of 100.

Show the expected result
What the finished sketch looks like
What it should look like. Yours can be a little different, as long as it follows the task.
Try it yourselfYour edits are saved in this browser.

Leave trails behind the circles.

    Run your program. This ticks when your code does all of the above.

    Make the circles spiral outwards.

      Run your program. This ticks when your code does all of the above.

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

      Your own pattern

      Make a moving pattern of your own: a wave, a spiral, a spinning flower. Use a loop, sin or cos, and map to change a colour or size. The sketch on this page is one wave.

      Try it yourselfYour edits are saved in this browser.

      Info

      Where this leads

      Maths makes motion smooth. Artists use the same ideas for waves, orbits and growing plants.

      A friend's sketch uses sin(frameCount * 0.05). What does it do over time?

      For teachers

      Students place circles on a ring with sin and cos, make the ring turn with frameCount, colour it with map, add trails and a spiral, and make a moving pattern of their own. About 45 minutes.

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

      Students will be able to

      Before class

      Optional enrichment after the challenge. Students need no trigonometry beyond "sin and cos give the x and y of a point going round a circle"; angles here are in radians (frameCount * 0.02), apart from the Predict, which converts degrees with radians(). Skip the explanation of radians unless asked. map is the most useful new tool: it turns any range into any other.

      Running the lesson

      1. Predict: sketch a clock face and the 12 circles on it.
      2. Run: ask what would change if the 0.02 was 0.05.
      3. Investigate: ask which number makes it turn.
      4. Modify: let students guess the effect of the fourth number in background before trying.
      5. Make: encourage waves, orbits and flowers; share the best.

      The hard part (on purpose)

      Seeing why a loop with i makes different circles. Print i or draw just one circle first.

      When students get stuck

      Stretch and support

      Stretch: Make the circles change size with sin, or draw two rings turning opposite ways.

      Support: Give the Run sketch and ask for one change, such as colours or speed.

      Where it leads

      From here, students can start their own sketches or explore p5.js's reference.

      What each step is for

      Now you can
      You reached the endBack to Drawing and animation with p5.js