The pixelDensity() function in p5.js is used to set the pixel scaling for high pixel density displays. The default value of pixel density is set to match display density. The pixelDensity(1) is used to turn off display density. The pixelDensity() function without arguments returns the current pixel density of the sketch.
Syntax:
pixelDensity(c)
Parameters: This function accepts single parameter c which stores the value of the scale.
Below programs illustrate the pixelDensity() function in p5.js:
Example 1: This example uses pixelDensity() function to display the pixel density.
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(9);
}
function draw() {
background(0, 200, 0);
color( "green" );
textSize(30);
textAlign(CENTER);
text( "GeeksForGeeks!" , windowWidth/2,
windowHeight/2);
text( "PixelDensity is " + pixelDensity(),
windowWidth/2, windowHeight/2 + 40);
}
|
Output:

Example 2: This example uses pixelDensity() function to display the pixel density.
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(3);
}
function draw() {
background(160, 200, 50);
textSize(30);
textAlign(CENTER);
text( "GeeksForGeeks!" , windowWidth/2,
windowHeight/2);
text( "PixelDensity is " + pixelDensity(),
windowWidth/2, windowHeight/2 + 40);
}
|
Output:

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