The width variable in p5.js is a system variable that stores the width of the drawing canvas. This value is automatically initialised by the first parameter of the createCanvas() function as soon as the code is executed.
Syntax:
width
Parameters: This function does not accept any parameter.
Below program illustrates the width variable in p5.js:
Example-1:
function setup() {
createCanvas(380, 80);
}
function draw() {
background(220);
textSize(16);
textAlign(CENTER);
fill(color( 'Green' ));
text( "Width of Canvas is : "
+ width, 180, 50);
}
|
Output:
Example-2:
function setup() {
width = windowWidth;
createCanvas(width, 80);
}
function draw() {
background(220);
textSize(16);
textAlign(CENTER);
fill(color( 'Green' ));
text( "Width of Canvas is equal to windowWidth i.e. : "
+ width, 180, 50);
}
|
Output:
Reference: https://p5js.org/reference/#/p5/width
Last Updated :
10 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...