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() {
createCanvas(displayWidth, 400);
textSize(20);
}
function draw() {
background(200);
rect(mouseX, mouseY, 30, 30);
text( "Display Width is " + windowWidth, 30, 40);
}
|
Output:

Example-2:
function setup() {
createCanvas(displayWidth, displayHeight);
textSize(20);
}
function draw() {
background(200);
rect(mouseX, mouseY, 30, 30);
text( "Canvas Display Height is " + displayHeight, 30, 40);
}
|
Output:

Reference: https://p5js.org/reference/#/p5/displayWidth
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks 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!
Last Updated :
10 Aug, 2023
Like Article
Save Article