Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Animating with draw() Loop | Animation and Time-Based Art
Generative Art with JavaScript and P5.js

bookAnimating with draw() Loop

index.html

index.html

copy

Animating graphics in P5.js relies on the draw() function, which acts as a continuous loop that refreshes the canvas many times per second. Each time draw() runs, the canvas is cleared (if you use background()), shapes are redrawn, and variables can be updated to create motion. This repeated execution is what enables animation and dynamic visual effects.

The frameRate() function determines how many times per second draw() is called. By default, P5.js aims for 60 frames per second, but you can adjust this with frameRate(). A higher frame rate means smoother motion, while a lower frame rate can create a choppier effect.

To animate an object, you store its position and speed in variables. Inside draw(), you update these variables on each frame. For example, a bouncing ball's position is updated by adding its speed to its coordinates, and when it hits the edge of the canvas, the direction is reversed by multiplying the speed by -1. This process of updating variables and redrawing shapes on every frame is the core of time-based art in P5.js.

question mark

What is the main purpose of the draw() function in P5.js?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookAnimating with draw() Loop

Svep för att visa menyn

index.html

index.html

copy

Animating graphics in P5.js relies on the draw() function, which acts as a continuous loop that refreshes the canvas many times per second. Each time draw() runs, the canvas is cleared (if you use background()), shapes are redrawn, and variables can be updated to create motion. This repeated execution is what enables animation and dynamic visual effects.

The frameRate() function determines how many times per second draw() is called. By default, P5.js aims for 60 frames per second, but you can adjust this with frameRate(). A higher frame rate means smoother motion, while a lower frame rate can create a choppier effect.

To animate an object, you store its position and speed in variables. Inside draw(), you update these variables on each frame. For example, a bouncing ball's position is updated by adding its speed to its coordinates, and when it hits the edge of the canvas, the direction is reversed by multiplying the speed by -1. This process of updating variables and redrawing shapes on every frame is the core of time-based art in P5.js.

question mark

What is the main purpose of the draw() function in P5.js?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt