[][src]Static p5_sys::global::get

pub static get: GetInternalType

Get a region of pixels, or a single pixel, from the canvas.

Returns an array of [R,G,B,A] values for any pixel or grabs a section of an image. If no parameters are specified, the entire image is returned. Use the x and y parameters to get the value of one pixel. Get a section of the display window by specifying additional w and h parameters. When getting an image, the x and y parameters define the coordinates for the upper-left corner of the image, regardless of the current imageMode().

Getting the color of a single pixel with get(x, y) is easy, but not as fast as grabbing the data directly from pixels[]. The equivalent statement to get(x, y) using pixels[] with pixel density d is

let x, y, d; // set these to the coordinates
let off = (y * width + x) * d * 4;
let components = [
  pixels[off],
  pixels[off + 1],
  pixels[off + 2],
  pixels[off + 3]
];
print(components);

See the reference for pixels[] for more information.

If you want to extract an array of colors or a subimage from an p5.Image object, take a look at p5.Image.get()

Examples

let img;
function preload() {
  img = loadImage('assets/rockies.jpg');
}
function setup() {
  image(img, 0, 0);
  let c = get();
  image(c, width / 2, 0);
}
let img;
function preload() {
  img = loadImage('assets/rockies.jpg');
}
function setup() {
  image(img, 0, 0);
  let c = get(50, 90);
  fill(c);
  noStroke();
  rect(25, 25, 50, 50);
}

Overloads

x x-coordinate of the pixel

y y-coordinate of the pixel

w width

h height



x x-coordinate of the pixel

y y-coordinate of the pixel