Skip to content

Commit a49e770

Browse files
committed
Added getPixelAlpha method
1 parent ed97d15 commit a49e770

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

src/textures/TextureManager.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,48 @@ var TextureManager = new Class({
860860
return null;
861861
},
862862

863+
/**
864+
* Given a Texture and an `x` and `y` coordinate this method will return a value between 0 and 255
865+
* corresponding to the alpha value of the pixel at that location in the Texture. If the coordinate
866+
* is out of bounds it will return null.
867+
*
868+
* @method Phaser.Textures.TextureManager#getPixelAlpha
869+
* @since 3.10.0
870+
*
871+
* @param {integer} x - The x coordinate of the pixel within the Texture.
872+
* @param {integer} y - The y coordinate of the pixel within the Texture.
873+
* @param {string} key - The unique string-based key of the Texture.
874+
* @param {(string|integer)} frame - The string or index of the Frame.
875+
*
876+
* @return {integer} A value between 0 and 255, or `null` if the coordinates were out of bounds.
877+
*/
878+
getPixelAlpha: function (x, y, key, frame)
879+
{
880+
var textureFrame = this.getFrame(key, frame);
881+
882+
if (textureFrame)
883+
{
884+
var source = textureFrame.source.image;
885+
886+
if (x >= 0 && x <= source.width && y >= 0 && y <= source.height)
887+
{
888+
x += textureFrame.cutX;
889+
y += textureFrame.cutY;
890+
891+
var context = this._tempContext;
892+
893+
context.clearRect(0, 0, 1, 1);
894+
context.drawImage(source, x, y, 1, 1, 0, 0, 1, 1);
895+
896+
var rgb = context.getImageData(0, 0, 1, 1);
897+
898+
return rgb.data[3];
899+
}
900+
}
901+
902+
return null;
903+
},
904+
863905
/**
864906
* Sets the given Game Objects `texture` and `frame` properties so that it uses
865907
* the Texture and Frame specified in the `key` and `frame` arguments to this method.

0 commit comments

Comments
 (0)