p5.js | displayDensity() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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



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.




Article Tags :

Be the First to upvote.


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