* @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)
| 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; |
no test coverage detected