Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Responsive Canvas and Window Resizing | Transformations and Interactivity
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Generative Art with JavaScript and P5.js

bookResponsive Canvas and Window Resizing

index.html

index.html

copy

When you create generative art or interactive sketches, you want your work to look great on any device or browser window size. This is where responsive design principles come in. In creative coding, responsive design means your canvas and visuals automatically adjust to fit the available screen space, ensuring that shapes, colors, and interactions always appear as intended.

To achieve this in P5.js, you use the windowWidth and windowHeight variables to set your canvas size. But what happens if the user resizes their browser window after your sketch has started? Without extra handling, your canvas would stay the same size, possibly leaving empty space or cropping your artwork.

P5.js provides the windowResized() function for this purpose. By defining this function in your sketch, you can tell P5.js what to do whenever the window size changes. The most common action is to call resizeCanvas(windowWidth, windowHeight), which resizes the canvas to match the new window dimensions. This way, your artwork remains centered, scaled, or otherwise visually consistent, no matter how the user adjusts their browser.

Responsive design is especially important for interactive and generative art, where the experience should be seamless across desktops, laptops, tablets, and phones. You can also use the new canvas size inside your draw() function, so all positions and sizes are recalculated according to the current width and height. This approach gives your art a professional, polished feel and makes it accessible to everyone.

question mark

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

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

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

Suggested prompts:

Can you show me an example of how to use windowResized() in a P5.js sketch?

How do I make sure my shapes and interactions also adjust when the canvas resizes?

What are some best practices for responsive design in creative coding?

bookResponsive Canvas and Window Resizing

Svep för att visa menyn

index.html

index.html

copy

When you create generative art or interactive sketches, you want your work to look great on any device or browser window size. This is where responsive design principles come in. In creative coding, responsive design means your canvas and visuals automatically adjust to fit the available screen space, ensuring that shapes, colors, and interactions always appear as intended.

To achieve this in P5.js, you use the windowWidth and windowHeight variables to set your canvas size. But what happens if the user resizes their browser window after your sketch has started? Without extra handling, your canvas would stay the same size, possibly leaving empty space or cropping your artwork.

P5.js provides the windowResized() function for this purpose. By defining this function in your sketch, you can tell P5.js what to do whenever the window size changes. The most common action is to call resizeCanvas(windowWidth, windowHeight), which resizes the canvas to match the new window dimensions. This way, your artwork remains centered, scaled, or otherwise visually consistent, no matter how the user adjusts their browser.

Responsive design is especially important for interactive and generative art, where the experience should be seamless across desktops, laptops, tablets, and phones. You can also use the new canvas size inside your draw() function, so all positions and sizes are recalculated according to the current width and height. This approach gives your art a professional, polished feel and makes it accessible to everyone.

question mark

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

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt