p5.js Tutorial: Draw Interactive Sketches with Setup, State, and a Draw Loop
p5.js introduces creative coding through a canvas and a predictable sketch lifecycle. Establish the drawing surface in setup, then use draw to render changing state. A moving shape becomes a useful first exercise when its position, interaction, and appearance are represented deliberately rather than hidden inside repeated drawing commands.
Here's a popular p5.js video — watch it, then summarize it with one click. Or paste any other YouTube link below.
— or paste your own YouTube link —
Want to keep the key points from a YouTube video? Paste the link below to turn it into structured notes.
Why Use p5.js Tutorial: Draw Interactive Sketches with Setup, State, and a Draw Loop
- Initialize the drawing surface in setup
- Render evolving state through draw
- Understand canvas coordinate direction
- Update motion variables between frames
- Connect pointer interaction to visual changes
Frequently Asked Questions
Why does setup differ from draw?
Setup establishes initial sketch conditions before the repeated draw cycle begins. Put one-time initialization there and place ongoing state updates or rendering in the appropriate part of the repeated sketch behavior.
Why can a moving shape leave a trail?
Canvas drawing adds pixels to the existing surface unless the sketch clears or covers them. Choose whether to redraw the background each cycle according to whether persistent trails are part of the effect.
How do coordinate values determine a shape's location?
Drawing functions interpret positions within the current coordinate system and transformations. Learn the default canvas axes first, then track translations or rotations that change how later coordinates map to pixels.
Should an object's position be inferred from already drawn pixels?
Store position as sketch state and use it to draw the object. Pixel output is a rendering result and makes a poor substitute for the numeric state needed by movement and interaction rules.
What happens if rendering stops with noLoop?
The automatic repeated draw cycle stops until resumed or otherwise redrawn through supported mechanisms. Any behavior placed only in that cycle will no longer advance simply because time continues passing.