Name

pixelHeight

Description

When pixelDensity(2) is used to make use of a high resolution display (called a Retina display on OS X or high-dpi on Windows and Linux), the width and height of the sketch do not change, but the number of pixels is doubled. As a result, all operations that use pixels (like loadPixels(), get(), set(), etc.) happen in this doubled space. As a convenience, the variables pixelWidth and pixelHeight hold the actual width and height of the sketch in pixels. This is useful for any sketch that uses the pixels[] array, for instance, because the number of elements in the array will be pixelWidth*pixelHeight, not width*height.

Examples

  • void setup() {
      size(600, 400);
      pixelDensity(2);
      println(width, height);
      println(pixelWidth, pixelHeight);
    }
    
  • void setup() {
      size(600, 400);
      pixelDensity(2);  // Double the pixel density
      println(width, height);
      println(pixelWidth, pixelHeight);
    }
    
    void draw() {
      loadPixels();
      // Fill all the pixels to blue with using
      // pixelWidth and pixelHeight
      for (int i = 0; i < pixelWidth * pixelHeight; i++) {
        pixels[i] = color(0, 0, 255);
      }
      // Fill one quarter of the pixels to yellow
      // because the pixel density is set to 2 in setup()
      // and 'width' and 'height' don't reflect the pixel 
      // dimensions of the sketch
      for (int i = 0; i < width * height; i++) {
        pixels[i] = color(255, 255, 0);
      }
      updatePixels();
      noLoop();
    }
    
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.