HomeLanguagesJavascriptp5.js windowResized() function

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:




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);
}


Output:

Example-2:




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);
}


Output:
Before Resizing:

After Resizing:

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32647 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7022 POSTS0 COMMENTS
Nicole Veronica
12159 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12254 POSTS0 COMMENTS
Shaida Kate Naidoo
7154 POSTS0 COMMENTS
Ted Musemwa
7419 POSTS0 COMMENTS
Thapelo Manthata
7114 POSTS0 COMMENTS
Umr Jansen
7117 POSTS0 COMMENTS