This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

Name

pixelDensity()

Examples
size(100, 100);
pixelDensity(2);
noStroke();
background(0);
ellipse(30, 48, 36, 36);
ellipse(70, 48, 36, 36);

void setup() {
  size(100, 100);
  pixelDensity(2);
  noStroke();
}

void draw() {
  background(0);
  ellipse(30, 48, 36, 36);
  ellipse(70, 48, 36, 36);
}

void setup() {
  size(100, 100);
  // Pulling the display's density dynamically
  pixelDensity(displayDensity());
  noStroke();
}

void draw() {
  background(0);
  ellipse(30, 48, 36, 36);
  ellipse(70, 48, 36, 36);
}
Description This function is new with Processing 3.0. It makes it possible for Processing to render using all of the pixels on high resolutions screens like Apple Retina displays and Windows High-DPI displays. This function can only be run once within a program and it must be used right after size() in a program without a setup() and used within setup() when a program has one. The pixelDensity() should only be used with hardcoded numbers (in almost all cases this number will be 2) or in combination with displayDensity() as in the third example above. To use variables as the arguments to pixelDensity() function, place the pixelDensity() function within the settings() function. There is more information about this on the settings() reference page.
Syntax
pixelDensity(density)
Parameters
density int: 1 or 2
Returnsvoid
Updated on January 21, 2019 10:05:08am EST

Creative Commons License