p5.js | windowResized() function

The windowResized() function in p5.js is called once every time the browser window is resized. It adjusts it height and width automatically whenever the size of the window is increased. This function is invoked automatically as soon as window is resized and then create a new canvas corresponding to it.

Syntax:

windowResized()

Parameters: This function does not accept any parameter.

Below program illustrates the windowResized() function in p5.js:
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
    
    createCanvas(windowWidth, windowHeight);
}
  
function draw() {
    background(0, 200, 0);
    color("green");
    textSize(30);
    textAlign(CENTER);
    text("GeeksForGeeks!"
         windowWidth / 2, 
         windowHeight / 2);
}
  
// Definition of windowResized Function
function windowResized() {
    resizeCanvas(windowWidth, windowHeight);
}

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
    createCanvas(windowWidth, windowHeight);
}
  
function draw() {
    background(0, 200, 0);
    color("green");
    textSize(30);
    textAlign(CENTER);
    text("GeeksForGeeks!"
         windowWidth / 2, 
         windowHeight / 2);
}
  
// Definition of windowResized Function
function windowResized() {
    resizeCanvas(windowWidth / 2, windowHeight / 2);
}

chevron_right


Output:
Before Resizing:

After Resizing:

Reference: https://p5js.org/reference/#/p5/windowResized



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.