p5.js | displayWidth Variable

The displayWidth variable in p5.js is used to store the width of the screen display of the device and it is according to The default pixelDensity. This variable is used to run a full-screen program on any display size. Multiply this by pixelDensity to return actual screen size.

Syntax:

displayWidth()

Parameters: This function does not accept any parameter.

Below program illustrates the displayWidth variable in p5.js:
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  
    // use of displayWidth variable
    createCanvas(displayWidth, 400);
  
    // Set text size to 40px
    textSize(20);
}
  
function draw() {
  
    background(200);
    rect(mouseX, mouseY, 30, 30);
  
    //Use of windowWidth Variable
    text("Display Width is " + windowWidth, 30, 40);
}

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  
    // use of displayWidth variable
    createCanvas(displayWidth, displayHeight);
  
    // Set text size to 40px
    textSize(20);
}
  
function draw() {
    background(200);
    rect(mouseX, mouseY, 30, 30);
  
    //Use of windowWidth Variable
    text("Canvas Display Height is " + displayHeight, 30, 40);
}

chevron_right


Output:

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



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.



Improved By : shubham_singh



Article Tags :

Be the First to upvote.


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