A canvas and a loop
4 minEvery moving sketch runs one loop that clears the canvas, draws, and changes a value, many times a second. You'll find the line that makes a kite fly, then change it in a live canvas.
Lesson video
Tastebud · 3:48 · Captions on screen
A sketch redraws many times a second, and anything that moves is a value you change each frame.
- Frame
- One full drawing of the canvas. At 60 frames a second, each one gets about 16.7 milliseconds.
- State
- The values the loop remembers between frames, such as the kite's x position and its speed.
- Why. A canvas only remembers pixels. To move a kite you paint over the last frame and draw it one step along, so the step has to live in a variable.
- Real example. A new sketch opens with setup(), which makes a 400 by 400 canvas once, and draw(), which paints the background grey and repeats about 60 times a second. (p5.js Web Editor, Nov 2026)
- When to break it. A poster needs one frame. Call noLoop() and draw() stops repeating, so the frame you drew stays still and you can save it as an image.
- Common mistake. Adding a fixed step every frame with requestAnimationFrame. It fires once per screen refresh, so on a 120 Hz screen the kite flies twice as fast.
Our take. Before you reuse a sketch, find the line that changes its state each frame. A maker who can't point to it can't slow the piece down when the client asks.
setup runs once, draw runs every frame
setup() makes the canvas one time. draw() then runs again and again, about 60 times a second in p5.js, and each run paints one whole frame. Plain canvas code does the same with requestAnimationFrame, which fires once per screen refresh.
setup() runs once, then every frame draw() reads the state and changes it
- Browser to setup(): Run once: make the canvas. createCanvas(400, 400) in p5.js, or a canvas tag and getContext('2d') in plain code.
- Browser to draw(): Frame 1.
- draw() to State: x: Read x = 0, draw, set x = 2. The kite is drawn where x says, then x moves on by the speed.
- Browser to draw(): Frame 2, about 17 ms later. At 60 frames a second each frame gets about 16.7 milliseconds.
- draw() to State: x: Read x = 2, draw, set x = 4.
- Browser to draw(): Frame 3, and on until stopped. noLoop() stops the repeat; loop() starts it again.
State lives outside draw, or it resets
A value declared inside draw() is made fresh every frame, so it never gets anywhere. Declare it once above draw(), change it inside, and it carries from frame to frame. That change is the line that makes the sketch move.
- Made again at 0 on every frame
- x + 2 is thrown away when the frame ends
- The kite sits at the left edge forever
- Made once, when the page loads
- Each frame adds 2 to the same x
- The kite moves 2 pixels a frame, about 120 a second at 60 Hz
The clear step decides what stays on screen
Each frame starts by painting the background over the last one, so you only see the newest kite. Paint the background once in setup() instead, and every old frame stays under the new one as a trail.
Without a clear, every old frame stays under the newest one
- Frame 3: kite at x = 4. Painted last, so it sits on top.
- Frame 2: kite at x = 2. Still visible, because nothing painted over it.
- Frame 1: kite at x = 0. Still visible too. Together the frames read as a trail.
- Background, painted once. Painted in setup() only. Move it to the top of draw() and only the newest frame shows.
Try it
Make the kite fly
A plain canvas sketch: every frame it clears, draws a kite at x, then adds speed to x. p5.js (free, LGPL 2.1 licence) wraps this same loop as setup() and draw(). Change the three lines the checks name and watch the sky.
Tab adds two spaces. Press Esc, then Tab, to leave the editor.
- The kite flies faster: speed is 3 or more (not yet)
- The clear step is off, so old frames stay as a trail (not yet)
- The height follows time: y uses Math.sin(frame (not yet)
What to notice: Set speed to 3 and the kite crosses faster, because x now grows 3 pixels a frame. Put // in front of the clear line in step 1: nothing paints over the old frames, so every past kite stays as a red band. Keep the // and change drawKite(x, 100) to drawKite(x, 100 + Math.sin(frame * 0.05) * 30): the height now follows the frame count, so the trail draws a wave.
Quick check
You reuse a sketch where a circle drifts right. The client wants it slower. Which line do you change first?
Watch next
Watch it for How setup() runs once and draw() repeats, then at 6:05 the moment background() moves into setup() and the trail appears.
Watch it for Why the circle stays still at first, and how a variable declared above draw() and changed inside it makes it move.
Connects to: 2.3 JavaScript for interaction · 5.2 A scene in the browser · 5.6 Holding the frame rate
Sources · checked Nov 2026
- p5.js reference: draw()draw() begins looping after setup() runs and by default tries to run 60 times per second; noLoop() stops it and loop() resumes it
- p5.js tutorial: Get StartedNew Web Editor sketches start with setup() and createCanvas(400, 400), and draw() with background(220), a grey; setup() runs one time
- p5.js reference: background()background() is typically used within draw() to clear the canvas at the start of each frame
- p5.js reference: noLoop()noLoop() stops draw() from running repeatedly
- MDN: Window.requestAnimationFrame()The callback generally matches the display refresh rate (60 Hz most common, 75, 120 and 144 Hz also used), it must be called again for each frame, and animation that ignores elapsed time runs faster on high refresh-rate screens
- p5.js on GitHub: releases and licencep5.js v2.4.0 is the latest release (12 Nov 2026); the project is licensed LGPL 2.1
- The Coding Train: Using Variables mouseX & mouseY, and Define Your Own VariablesThe two watch videos and their chapter times: program flow at 0:35, background moved into setup at 6:05