* Sets the color of one or more pixels within an image. * * `img.set()` is easy to use but it's not as fast as * img.pixels . Use * img.pixels to set many pixel values. * * `img.set()` interprets the first two parameter
(x, y, imgOrCol)
| 574 | * } |
| 575 | */ |
| 576 | set(x, y, imgOrCol) { |
| 577 | // Renderer2D.prototype.set.call(this, x, y, imgOrCol); |
| 578 | // round down to get integer numbers |
| 579 | x = Math.floor(x); |
| 580 | y = Math.floor(y); |
| 581 | const pixelsState = this._pixelsState; |
| 582 | if (imgOrCol instanceof Image) { |
| 583 | this.drawingContext.save(); |
| 584 | this.drawingContext.setTransform(1, 0, 0, 1, 0, 0); |
| 585 | this.drawingContext.scale( |
| 586 | this._pixelDensity, |
| 587 | this._pixelDensity |
| 588 | ); |
| 589 | this.drawingContext.clearRect(x, y, imgOrCol.width, imgOrCol.height); |
| 590 | this.drawingContext.drawImage(imgOrCol.canvas, x, y); |
| 591 | this.drawingContext.restore(); |
| 592 | } else { |
| 593 | let r = 0, |
| 594 | g = 0, |
| 595 | b = 0, |
| 596 | a = 0; |
| 597 | let idx = |
| 598 | 4 * |
| 599 | (y * |
| 600 | this._pixelDensity * |
| 601 | (this.width * this._pixelDensity) + |
| 602 | x * this._pixelDensity); |
| 603 | if (!pixelsState.imageData) { |
| 604 | pixelsState.loadPixels(); |
| 605 | } |
| 606 | if (typeof imgOrCol === 'number') { |
| 607 | if (idx < pixelsState.pixels.length) { |
| 608 | r = imgOrCol; |
| 609 | g = imgOrCol; |
| 610 | b = imgOrCol; |
| 611 | a = 255; |
| 612 | //this.updatePixels.call(this); |
| 613 | } |
| 614 | } else if (Array.isArray(imgOrCol)) { |
| 615 | if (imgOrCol.length < 4) { |
| 616 | throw new Error('pixel array must be of the form [R, G, B, A]'); |
| 617 | } |
| 618 | if (idx < pixelsState.pixels.length) { |
| 619 | r = imgOrCol[0]; |
| 620 | g = imgOrCol[1]; |
| 621 | b = imgOrCol[2]; |
| 622 | a = imgOrCol[3]; |
| 623 | //this.updatePixels.call(this); |
| 624 | } |
| 625 | } else if (imgOrCol instanceof p5.Color) { |
| 626 | if (idx < pixelsState.pixels.length) { |
| 627 | [r, g, b, a] = imgOrCol._getRGBA([255, 255, 255, 255]); |
| 628 | //this.updatePixels.call(this); |
| 629 | } |
| 630 | } |
| 631 | // loop over pixelDensity * pixelDensity |
| 632 | for (let i = 0; i < this._pixelDensity; i++) { |
| 633 | for (let j = 0; j < this._pixelDensity; j++) { |
no test coverage detected