Wednesday, September 3, 2025
HomeLanguagesJavascriptp5.js displayWidth Variable

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:




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


Output:

Example-2:




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


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS