Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptp5.js pixelDensity() function

p5.js pixelDensity() function

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() {
      
    // Create canvas of window size
    createCanvas(windowWidth, windowHeight);
      
    // Set Pixel Density to 9
    pixelDensity(9);
}
   
function draw() {
      
    // Set the background color
    background(0, 200, 0);
    
    color("green");
      
    // Set the text size
    textSize(30);
      
    // Set the text align
    textAlign(CENTER);
      
    // Display text on the screen
    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() {
      
    // Create canvas of window size
    createCanvas(windowWidth, windowHeight);
      
    // Set Pixel Density to 9
    pixelDensity(3);
}
   
function draw() {
      
    // Set the background color
    background(160, 200, 50);
      
    // Set the text size
    textSize(30);
      
    // Set the text align
    textAlign(CENTER);
      
    // Display text on the screen
    text("GeeksForGeeks!", windowWidth/2,
                windowHeight/2);
    text("PixelDensity is " + pixelDensity(),
         windowWidth/2, windowHeight/2 + 40);
   
}


Output:

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

RELATED ARTICLES

Most Popular

Recent Comments