From 69e6643800252ea7c8ddaacdb5c0f9adb59fde74 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 11 Dec 2018 23:22:00 +0000 Subject: [PATCH] Added setPixel method. --- src/textures/CanvasTexture.js | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/textures/CanvasTexture.js b/src/textures/CanvasTexture.js index e548870af..8e531d273 100644 --- a/src/textures/CanvasTexture.js +++ b/src/textures/CanvasTexture.js @@ -271,6 +271,47 @@ var CanvasTexture = new Class({ } }, + /** + * Sets a pixel in the CanvasTexture to the given color and alpha values. + * + * This is an expensive operation to run in large quantities, so use sparingly. + * + * @method Phaser.Textures.CanvasTexture#setPixel + * @since 3.16.0 + * + * @param {integer} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer. + * @param {integer} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer. + * @param {integer} red - The red color value. A number between 0 and 255. + * @param {integer} green - The green color value. A number between 0 and 255. + * @param {integer} blue - The blue color value. A number between 0 and 255. + * @param {integer} [alpha=255] - The alpha value. A number between 0 and 255. + * + * @return {this} This CanvasTexture. + */ + setPixel: function (x, y, red, green, blue, alpha) + { + if (alpha === undefined) { alpha = 255; } + + x = Math.abs(Math.floor(x)); + y = Math.abs(Math.floor(y)); + + var index = this.getIndex(x, y); + + if (index > -1) + { + var imageData = this.context.getImageData(x, y, 1, 1); + + imageData.data[0] = red; + imageData.data[1] = green; + imageData.data[2] = blue; + imageData.data[3] = alpha; + + this.context.putImageData(imageData, x, y); + } + + return this; + }, + /** * Get the color of a specific pixel from this texture and store it in a Color object. *