Thursday, October 23, 2025
HomeLanguagesJavascriptp5.js resizeCanvas() Function

p5.js resizeCanvas() Function

The resizeCanvas() function is used to resize the canvas to the height and width given as parameters. The canvas is immediately redrawn after the resize takes place unless the optional ‘noRedraw’ parameter is set to true.

Syntax:

resizeCanvas(w, h, [noRedraw])

Parameters: This function accepts three parameters as mentioned above and described below:

  • w: It is a number representing the width of the new canvas.
  • h: It is a number representing the height of the new canvas.
  • noRedraw: It is a boolean value which specifies if the canvas is immediately redrawn when resized.

Below example illustrates the resizeCanvas() function in p5.js:

Example 1: In this example, we fixed the increase size.




function setup() {
  createCanvas(200, 200);
  textSize(16);
  
  rect(0, 0, height, width);
  background('green');
  text("This is the canvas area", 20, 80);
  text("Canvas height: " + height, 25, 100);
  text("Canvas width: " + width, 25, 120);
}
  
function mouseClicked() {
  resizeCanvas(300, 300, true);
  
  rect(0, 0, height, width);
  background('green');
  text("This is the canvas area", 50, 130);
  text("Canvas height: " + height, 50, 150);
  text("Canvas width: " + width, 50, 170);
}


Output:
output of above code
Example 2: In this example, size depends on the click position.




function setup() {
  createCanvas(200, 200);
  textSize(16);
  
  rect(0, 0, height, width);
  background('green');
  text("Press anywhere to resize", 10, 20);
  text("Canvas height: " + height, 10, 40);
  text("Canvas width: " + width, 10, 60);
}
  
function mouseClicked(event) {
  // resize canvas to the
  resizeCanvas(event.x, event.y);
  
  rect(0, 0, height, width);
  background('green');
  text("Press anywhere to resize", 10, 20);
  text("Canvas height: " + height, 10, 40);
  text("Canvas width: " + width, 10, 60);
}


Output:
output of avobe code

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

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

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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS