Skip to content

Commit 95fe57e

Browse files
committed
BitmapData.extract working :)
1 parent de9fc08 commit 95fe57e

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

src/gameobjects/BitmapData.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,11 @@ Phaser.BitmapData.prototype = {
282282

283283
/**
284284
* Scans through the area specified in this BitmapData and sends a color object for every pixel to the given callback.
285-
* The callback will be sent a single object with 6 properties: `{ r: number, g: number, b: number, a: number, color: number, rgba: string }`.
285+
* The callback will be sent a color object with 6 properties: `{ r: number, g: number, b: number, a: number, color: number, rgba: string }`.
286286
* Where r, g, b and a are integers between 0 and 255 representing the color component values for red, green, blue and alpha.
287287
* The `color` property is an Int32 of the full color. Note the endianess of this will change per system.
288288
* The `rgba` property is a CSS style rgba() string which can be used with context.fillStyle calls, among others.
289+
* The callback will also be sent the pixels x and y coordinates respectively.
289290
* The callback must return either `false`, in which case no change will be made to the pixel, or a new color object.
290291
* If a new color object is returned the pixel will be set to the r, g, b and a color values given within it.
291292
*
@@ -316,7 +317,7 @@ Phaser.BitmapData.prototype = {
316317
{
317318
Phaser.Color.unpackPixel(this.getPixel32(tx, ty), pixel);
318319

319-
result = callback.call(callbackContext, pixel);
320+
result = callback.call(callbackContext, pixel, tx, ty);
320321

321322
if (result !== false && result !== null)
322323
{
@@ -335,10 +336,11 @@ Phaser.BitmapData.prototype = {
335336
},
336337

337338
/**
338-
* Scans through the area specified in this BitmapData and sends the color for every pixel to the given callback.
339+
* Scans through the area specified in this BitmapData and sends the color for every pixel to the given callback along with its x and y coordinates.
339340
* Whatever value the callback returns is set as the new color for that pixel, unless it returns the same color, in which case it's skipped.
340341
* Note that the format of the color received will be different depending on if the system is big or little endian.
341342
* It is expected that your callback will deal with endianess. If you'd rather Phaser did it then use processPixelRGB instead.
343+
* The callback will also be sent the pixels x and y coordinates respectively.
342344
*
343345
* @method Phaser.BitmapData#processPixel
344346
* @param {function} callback - The callback that will be sent each pixel color to be processed.
@@ -366,7 +368,7 @@ Phaser.BitmapData.prototype = {
366368
for (var tx = x; tx < w; tx++)
367369
{
368370
pixel = this.getPixel32(tx, ty);
369-
result = callback.call(callbackContext, pixel);
371+
result = callback.call(callbackContext, pixel, tx, ty);
370372

371373
if (result !== pixel)
372374
{
@@ -796,18 +798,37 @@ Phaser.BitmapData.prototype = {
796798
},
797799

798800
/**
799-
*
801+
* Scans this BitmapData for all pixels matching the given r,g,b values and then draws them into the given destination BitmapData.
802+
* The destination BitmapData must be large enough to receive all of the pixels that are scanned.
803+
* Although the destination BitmapData is returned from this method, it's actually modified directly in place, meaning this call is perfectly valid:
804+
* `picture.extract(mask, r, g, b)`
800805
*
801-
* @method Phaser.BitmapData#extractMask
802-
* @param {HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
803-
* @param {string} key - The Image to use as the alpha mask. If you give a key it will try and find the Image in the Game.Cache.
804-
* @return {HTMLImage}
806+
* @method Phaser.BitmapData#extract
807+
* @param {Phaser.BitmapData} destination - The BitmapData that the extracts pixels will be drawn to.
808+
* @param {number} r - The red color component, in the range 0 - 255.
809+
* @param {number} g - The green color component, in the range 0 - 255.
810+
* @param {number} b - The blue color component, in the range 0 - 255.
811+
* @param {number} [a=255] - The alpha color component, in the range 0 - 255.
812+
* @returns {Phaser.BitmapData} The BitmapData that the extract pixels were drawn on.
805813
*/
806-
extractMask: function (source, color, alpha) {
814+
extract: function (destination, r, g, b, a) {
815+
816+
if (typeof a === 'undefined') { a = 255; }
807817

808-
if (typeof alpha === 'undefined') { alpha = 255; }
818+
this.processPixelRGB(
819+
function(pixel, x, y){
820+
if (pixel.r === r && pixel.g === g && pixel.b === b)
821+
{
822+
destination.setPixel32(x, y, r, g, b, a, false);
823+
}
824+
return false;
825+
}
826+
, this);
809827

828+
destination.context.putImageData(destination.imageData, 0, 0);
829+
destination.dirty = true;
810830

831+
return destination;
811832

812833
},
813834

0 commit comments

Comments
 (0)