Open In App
Related Articles

p5.js displayDensity() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

The displayDensity() function in p5.js is used to return the pixel density of the current display the sketch.

Syntax:

pixelDensity(density)

Parameters: This function accepts single parameter density which stores the display density.

Below program illustrates the displayDensity() function in p5.js:

Example:




function setup() {
      
    // Create canvas of given size
    createCanvas(windowWidth, windowHeight);
    
    // Call the displayDensity function
    let density = displayDensity(); 
    pixelDensity(density);
}
   
function draw() {
  
    // Set the background color
    background(0, 200, 0);
      
    // Set text color
    color("green");
      
    // Set font size
    textSize(30);
      
    // Set text alignment
    textAlign(CENTER);
      
    // Set text to be add
    text("GeeksForGeeks!", windowWidth/2, windowHeight/2);
      
    // Display pixelDensity on the screen
    text("PixelDensity is " + pixelDensity(), 
            windowWidth/2, windowHeight/2+40);
}


Output:

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

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
Previous
Next
Similar Reads
Complete Tutorials