Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt