Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Animating with draw() Loop | Animation and Time-Based Art
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookAnimating with draw() Loop

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 1
some-alt