p5.js | width variable

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
    
    //create Canvas of size 380*80  
    createCanvas(380, 80);
}
  
function draw() {
    
    // set background color
    background(220);
    
    // set text size 
    textSize(16);
    
    // set the text alignment 
    textAlign(CENTER);
    
    //set the fill color
    fill(color('Green'));
    
    //use of width variable
    text("Width of Canvas is : "
         + width, 180, 50);
}

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
    
    //create Canvas of size 380*80 
    width = windowWidth;
    createCanvas(width, 80);
}
  
function draw() {
    // set background color
    background(220);
    
    // set text size 
    textSize(16);
    
    // set the text alignment 
    textAlign(CENTER);
    
    //set the fill color
    fill(color('Green'));
    
    //use of width variable
    text("Width of Canvas is equal to windowWidth i.e. : "
         + width, 180, 50);
}

chevron_right


Output:

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



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.