MCPcopy
hub / github.com/processing/p5.js / get

Method get

src/image/p5.Image.js:408–442  ·  view source on GitHub ↗

* @param {Number} x * @param {Number} y * @return {Number[]} color of the pixel at (x, y) in array format `[R, G, B, A]`.

(x, y, w, h)

Source from the content-addressed store, hash-verified

406 * @return {Number[]} color of the pixel at (x, y) in array format `[R, G, B, A]`.
407 */
408 get(x, y, w, h) {
409 // p5._validateParameters('p5.Image.get', arguments);
410 // return Renderer2D.prototype.get.apply(this, arguments);
411 const pixelsState = this._pixelsState;
412 const pd = this._pixelDensity;
413 const canvas = this.canvas;
414
415 if (typeof x === 'undefined' && typeof y === 'undefined') {
416 // get()
417 x = y = 0;
418 w = pixelsState.width;
419 h = pixelsState.height;
420 } else {
421 x *= pd;
422 y *= pd;
423
424 if (typeof w === 'undefined' && typeof h === 'undefined') {
425 // get(x,y)
426 if (x < 0 || y < 0 || x >= canvas.width || y >= canvas.height) {
427 return [0, 0, 0, 0];
428 }
429
430 return this._getPixel(x, y);
431 }
432 // get(x,y,w,h)
433 }
434
435 const region = new Image(w*pd, h*pd);
436 region.pixelDensity(pd);
437 region.canvas
438 .getContext('2d')
439 .drawImage(canvas, x, y, w * pd, h * pd, 0, 0, w*pd, h*pd);
440
441 return region;
442 }
443
444 _getPixel(x, y) {
445 let imageData, index;

Callers 3

loadingDisplayingFunction · 0.45
pixelsFunction · 0.45
constructorMethod · 0.45

Calls 2

_getPixelMethod · 0.95
pixelDensityMethod · 0.95

Tested by

no test coverage detected