2013-11-13 20:57:09 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-11-13 20:57:09 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-11-28 15:57:09 +00:00
|
|
|
* Creates a new BitmapData object.
|
2013-11-13 20:57:09 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.BitmapData
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
2014-02-13 14:19:41 +00:00
|
|
|
* @classdesc A BitmapData object contains a Canvas element to which you can draw anything you like via normal Canvas context operations.
|
|
|
|
* A single BitmapData can be used as the texture one or many Images/Sprites. So if you need to dynamically create a Sprite texture then they are a good choice.
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
2013-11-13 20:57:09 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2014-02-07 06:25:28 +00:00
|
|
|
* @param {string} key - Internal Phaser reference key for the render texture.
|
|
|
|
* @param {number} [width=100] - The width of the BitmapData in pixels.
|
|
|
|
* @param {number} [height=100] - The height of the BitmapData in pixels.
|
2013-11-13 20:57:09 +00:00
|
|
|
*/
|
2014-02-07 06:25:28 +00:00
|
|
|
Phaser.BitmapData = function (game, key, width, height) {
|
2013-11-13 20:57:09 +00:00
|
|
|
|
2014-02-07 06:25:28 +00:00
|
|
|
if (typeof width === 'undefined') { width = 100; }
|
|
|
|
if (typeof height === 'undefined') { height = 100; }
|
2013-11-13 20:57:09 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-03-23 07:59:28 +00:00
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.game = game;
|
2013-11-13 20:57:09 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-07 06:25:28 +00:00
|
|
|
* @property {string} key - The key of the BitmapData in the Cache, if stored there.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-02-07 06:25:28 +00:00
|
|
|
this.key = key;
|
2013-11-13 20:57:09 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {number} width - The width of the BitmapData in pixels.
|
2013-11-13 20:57:09 +00:00
|
|
|
*/
|
2013-11-25 03:13:04 +00:00
|
|
|
this.width = width;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {number} height - The height of the BitmapData in pixels.
|
2013-11-13 20:57:09 +00:00
|
|
|
*/
|
2013-11-25 03:13:04 +00:00
|
|
|
this.height = height;
|
2013-11-13 20:57:09 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {HTMLCanvasElement} canvas - The canvas to which this BitmapData draws.
|
|
|
|
* @default
|
|
|
|
*/
|
2014-02-28 03:55:06 +00:00
|
|
|
this.canvas = Phaser.Canvas.create(width, height, '', true);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {CanvasRenderingContext2D} context - The 2d context of the canvas.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
this.context = this.canvas.getContext('2d');
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2014-02-13 14:19:41 +00:00
|
|
|
/**
|
|
|
|
* @property {CanvasRenderingContext2D} ctx - A reference to BitmapData.context.
|
|
|
|
*/
|
|
|
|
this.ctx = this.context;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-04-24 02:49:49 +00:00
|
|
|
* @property {ImageData} imageData - The context image data.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.imageData = this.context.getImageData(0, 0, width, height);
|
|
|
|
|
2014-04-29 14:38:33 +00:00
|
|
|
/**
|
|
|
|
* @property {Uint8ClampedArray} data - A Uint8ClampedArray view into BitmapData.buffer.
|
|
|
|
*/
|
|
|
|
this.data = this.imageData.data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Uint32Array} pixels - An Uint32Array view into BitmapData.buffer.
|
|
|
|
*/
|
|
|
|
this.pixels = null;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-04-24 02:49:49 +00:00
|
|
|
* @property {ArrayBuffer} buffer - An ArrayBuffer the same size as the context ImageData.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
if (this.imageData.data.buffer)
|
|
|
|
{
|
2014-04-24 02:49:49 +00:00
|
|
|
this.buffer = this.imageData.data.buffer;
|
2014-04-29 14:38:33 +00:00
|
|
|
this.pixels = new Uint32Array(this.buffer);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-29 14:38:33 +00:00
|
|
|
if (window['ArrayBuffer'])
|
|
|
|
{
|
|
|
|
this.buffer = new ArrayBuffer(this.imageData.data.length);
|
|
|
|
this.pixels = new Uint32Array(this.buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.pixels = this.imageData.data;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {PIXI.BaseTexture} baseTexture - The PIXI.BaseTexture.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
this.baseTexture = new PIXI.BaseTexture(this.canvas);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {PIXI.Texture} texture - The PIXI.Texture.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
this.texture = new PIXI.Texture(this.baseTexture);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Frame} textureFrame - The Frame this BitmapData uses for rendering.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
this.textureFrame = new Phaser.Frame(0, 0, 0, width, height, 'bitmapData', game.rnd.uuid());
|
|
|
|
|
2014-05-24 01:02:49 +00:00
|
|
|
this.texture.frame = this.textureFrame;
|
|
|
|
|
2013-11-13 20:57:09 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {number} type - The const type of this object.
|
2013-11-13 20:57:09 +00:00
|
|
|
* @default
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
this.type = Phaser.BITMAPDATA;
|
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} disableTextureUpload - If disableTextureUpload is true this BitmapData will never send its image data to the GPU when its dirty flag is true.
|
|
|
|
*/
|
|
|
|
this.disableTextureUpload = false;
|
|
|
|
|
2014-03-21 18:04:24 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} dirty - If dirty this BitmapData will be re-rendered.
|
|
|
|
*/
|
|
|
|
this.dirty = false;
|
2013-11-17 00:55:28 +00:00
|
|
|
|
2014-09-02 22:16:10 +00:00
|
|
|
this.copyCallback = null;
|
|
|
|
this.copyCallbackContext = null;
|
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
// Aliases
|
2014-04-23 22:35:36 +00:00
|
|
|
this.cls = this.clear;
|
|
|
|
|
2014-04-25 01:45:27 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _tempR - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tempR = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tempG - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tempG = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tempB - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tempB = 0;
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-11-13 20:57:09 +00:00
|
|
|
|
|
|
|
Phaser.BitmapData.prototype = {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-21 14:50:18 +00:00
|
|
|
* Updates the given objects so that they use this BitmapData as their texture. This will replace any texture they will currently have set.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-11-28 15:57:09 +00:00
|
|
|
* @method Phaser.BitmapData#add
|
2014-02-13 14:19:41 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Sprite[]|Phaser.Image|Phaser.Image[]} object - Either a single Sprite/Image or an Array of Sprites/Images.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-02-13 14:19:41 +00:00
|
|
|
add: function (object) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-13 14:19:41 +00:00
|
|
|
if (Array.isArray(object))
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-13 14:19:41 +00:00
|
|
|
for (var i = 0; i < object.length; i++)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-13 14:19:41 +00:00
|
|
|
if (object[i]['loadTexture'])
|
|
|
|
{
|
|
|
|
object[i].loadTexture(this);
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 14:19:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
object.loadTexture(this);
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
},
|
|
|
|
|
2014-05-24 00:29:47 +00:00
|
|
|
/**
|
|
|
|
* Takes the given Game Object, resizes this BitmapData to match it and then draws it into this BitmapDatas canvas, ready for further processing.
|
|
|
|
* The source game object is not modified by this operation.
|
|
|
|
* If the source object uses a texture as part of a Texture Atlas or Sprite Sheet, only the current frame will be used for sizing and draw.
|
|
|
|
* If a string is given it will assume it's a cache key and look in Phaser.Cache for an image key matching the string.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#load
|
|
|
|
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|string} source - The object that will be used to populate this BitmapData.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-05-24 00:29:47 +00:00
|
|
|
*/
|
|
|
|
load: function (source) {
|
|
|
|
|
2014-05-24 01:02:49 +00:00
|
|
|
if (typeof source === 'string')
|
|
|
|
{
|
|
|
|
source = this.game.cache.getImage(source);
|
|
|
|
}
|
|
|
|
|
2014-05-24 01:43:59 +00:00
|
|
|
this.resize(source.width, source.height);
|
2014-05-24 00:29:47 +00:00
|
|
|
|
|
|
|
this.cls();
|
|
|
|
|
2014-05-24 01:02:49 +00:00
|
|
|
if (source instanceof Phaser.Image || source instanceof Phaser.Sprite)
|
2014-05-24 00:29:47 +00:00
|
|
|
{
|
2014-05-24 01:02:49 +00:00
|
|
|
this.drawSprite(source, 0, 0);
|
2014-05-24 00:29:47 +00:00
|
|
|
}
|
2014-05-24 01:02:49 +00:00
|
|
|
else
|
2014-05-24 00:29:47 +00:00
|
|
|
{
|
2014-05-24 01:02:49 +00:00
|
|
|
this.draw(source, 0, 0);
|
2014-05-24 00:29:47 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 01:43:59 +00:00
|
|
|
this.update();
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-05-24 00:29:47 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-04-24 02:49:49 +00:00
|
|
|
* Clears the BitmapData context using a clearRect.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#cls
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the BitmapData context using a clearRect.
|
|
|
|
*
|
2013-11-28 15:57:09 +00:00
|
|
|
* @method Phaser.BitmapData#clear
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
clear: function () {
|
|
|
|
|
|
|
|
this.context.clearRect(0, 0, this.width, this.height);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-21 18:04:24 +00:00
|
|
|
this.dirty = true;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
},
|
|
|
|
|
2014-02-14 06:04:29 +00:00
|
|
|
/**
|
2014-04-24 02:49:49 +00:00
|
|
|
* Fills the BitmapData with the given color.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#fill
|
|
|
|
* @param {number} r - The red color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} g - The green color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} b - The blue color value, between 0 and 0xFF (255).
|
2014-04-25 14:11:54 +00:00
|
|
|
* @param {number} [a=1] - The alpha color value, between 0 and 1.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-04-24 02:49:49 +00:00
|
|
|
*/
|
|
|
|
fill: function (r, g, b, a) {
|
|
|
|
|
2014-04-25 14:11:54 +00:00
|
|
|
if (typeof a === 'undefined') { a = 1; }
|
2014-04-24 02:49:49 +00:00
|
|
|
|
|
|
|
this.context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
|
|
|
|
this.context.fillRect(0, 0, this.width, this.height);
|
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes the BitmapData. This changes the size of the underlying canvas and refreshes the buffer.
|
|
|
|
*
|
2014-02-14 06:04:29 +00:00
|
|
|
* @method Phaser.BitmapData#resize
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-02-14 06:04:29 +00:00
|
|
|
*/
|
|
|
|
resize: function (width, height) {
|
|
|
|
|
|
|
|
if (width !== this.width || height !== this.height)
|
|
|
|
{
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2014-05-24 01:43:59 +00:00
|
|
|
|
2014-02-14 06:04:29 +00:00
|
|
|
this.canvas.width = width;
|
|
|
|
this.canvas.height = height;
|
2014-05-24 01:43:59 +00:00
|
|
|
|
2014-05-24 01:02:49 +00:00
|
|
|
this.baseTexture.width = width;
|
|
|
|
this.baseTexture.height = height;
|
2014-05-24 01:43:59 +00:00
|
|
|
|
2014-02-14 06:04:29 +00:00
|
|
|
this.textureFrame.width = width;
|
|
|
|
this.textureFrame.height = height;
|
2014-05-24 01:43:59 +00:00
|
|
|
|
2014-05-24 01:02:49 +00:00
|
|
|
this.texture.width = width;
|
|
|
|
this.texture.height = height;
|
2014-05-24 01:43:59 +00:00
|
|
|
|
2014-07-18 02:33:17 +00:00
|
|
|
this.texture.crop.width = width;
|
|
|
|
this.texture.crop.height = height;
|
|
|
|
|
2014-08-28 02:40:59 +00:00
|
|
|
this.update();
|
2014-05-24 01:43:59 +00:00
|
|
|
this.dirty = true;
|
2014-02-14 06:04:29 +00:00
|
|
|
}
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-02-14 06:04:29 +00:00
|
|
|
},
|
|
|
|
|
2014-02-14 12:07:04 +00:00
|
|
|
/**
|
2014-04-24 02:49:49 +00:00
|
|
|
* This re-creates the BitmapData.imageData from the current context.
|
2014-04-24 04:19:45 +00:00
|
|
|
* It then re-builds the ArrayBuffer, the data Uint8ClampedArray reference and the pixels Int32Array.
|
2014-04-24 02:49:49 +00:00
|
|
|
* If not given the dimensions defaults to the full size of the context.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#update
|
|
|
|
* @param {number} [x=0] - The x coordinate of the top-left of the image data area to grab from.
|
|
|
|
* @param {number} [y=0] - The y coordinate of the top-left of the image data area to grab from.
|
|
|
|
* @param {number} [width] - The width of the image data area.
|
|
|
|
* @param {number} [height] - The height of the image data area.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-04-24 02:49:49 +00:00
|
|
|
*/
|
2014-08-28 02:40:59 +00:00
|
|
|
update: function (x, y, width, height) {
|
2014-04-24 02:49:49 +00:00
|
|
|
|
|
|
|
if (typeof x === 'undefined') { x = 0; }
|
|
|
|
if (typeof y === 'undefined') { y = 0; }
|
|
|
|
if (typeof width === 'undefined') { width = this.width; }
|
|
|
|
if (typeof height === 'undefined') { height = this.height; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
this.imageData = this.context.getImageData(x, y, width, height);
|
2014-04-29 14:38:33 +00:00
|
|
|
this.data = this.imageData.data;
|
2014-04-24 02:49:49 +00:00
|
|
|
|
|
|
|
if (this.imageData.data.buffer)
|
|
|
|
{
|
|
|
|
this.buffer = this.imageData.data.buffer;
|
2014-04-29 14:38:33 +00:00
|
|
|
this.pixels = new Uint32Array(this.buffer);
|
2014-04-24 02:49:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-29 14:38:33 +00:00
|
|
|
if (window['ArrayBuffer'])
|
|
|
|
{
|
|
|
|
this.buffer = new ArrayBuffer(this.imageData.data.length);
|
|
|
|
this.pixels = new Uint32Array(this.buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.pixels = this.imageData.data;
|
|
|
|
}
|
2014-04-24 02:49:49 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-24 04:19:45 +00:00
|
|
|
* Scans through the area specified in this BitmapData and sends a color object for every pixel to the given callback.
|
2014-04-28 04:33:52 +00:00
|
|
|
* The callback will be sent a color object with 6 properties: `{ r: number, g: number, b: number, a: number, color: number, rgba: string }`.
|
2014-04-24 04:19:45 +00:00
|
|
|
* Where r, g, b and a are integers between 0 and 255 representing the color component values for red, green, blue and alpha.
|
|
|
|
* The `color` property is an Int32 of the full color. Note the endianess of this will change per system.
|
|
|
|
* The `rgba` property is a CSS style rgba() string which can be used with context.fillStyle calls, among others.
|
2014-04-28 04:33:52 +00:00
|
|
|
* The callback will also be sent the pixels x and y coordinates respectively.
|
2014-04-24 04:19:45 +00:00
|
|
|
* The callback must return either `false`, in which case no change will be made to the pixel, or a new color object.
|
|
|
|
* If a new color object is returned the pixel will be set to the r, g, b and a color values given within it.
|
2014-04-23 22:35:36 +00:00
|
|
|
*
|
2014-04-24 04:19:45 +00:00
|
|
|
* @method Phaser.BitmapData#processPixelRGB
|
|
|
|
* @param {function} callback - The callback that will be sent each pixel color object to be processed.
|
|
|
|
* @param {object} callbackContext - The context under which the callback will be called.
|
|
|
|
* @param {number} [x=0] - The x coordinate of the top-left of the region to process from.
|
|
|
|
* @param {number} [y=0] - The y coordinate of the top-left of the region to process from.
|
|
|
|
* @param {number} [width] - The width of the region to process.
|
|
|
|
* @param {number} [height] - The height of the region to process.
|
2014-04-23 22:35:36 +00:00
|
|
|
*/
|
2014-04-24 04:19:45 +00:00
|
|
|
processPixelRGB: function (callback, callbackContext, x, y, width, height) {
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
if (typeof x === 'undefined') { x = 0; }
|
|
|
|
if (typeof y === 'undefined') { y = 0; }
|
|
|
|
if (typeof width === 'undefined') { width = this.width; }
|
|
|
|
if (typeof height === 'undefined') { height = this.height; }
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
var w = x + width;
|
|
|
|
var h = y + height;
|
2014-04-25 01:45:27 +00:00
|
|
|
var pixel = Phaser.Color.createColor();
|
2014-04-24 04:19:45 +00:00
|
|
|
var result = { r: 0, g: 0, b: 0, a: 0 };
|
|
|
|
var dirty = false;
|
|
|
|
|
|
|
|
for (var ty = y; ty < h; ty++)
|
2014-04-23 22:35:36 +00:00
|
|
|
{
|
2014-04-24 04:19:45 +00:00
|
|
|
for (var tx = x; tx < w; tx++)
|
|
|
|
{
|
2014-04-25 01:45:27 +00:00
|
|
|
Phaser.Color.unpackPixel(this.getPixel32(tx, ty), pixel);
|
|
|
|
|
2014-04-28 04:33:52 +00:00
|
|
|
result = callback.call(callbackContext, pixel, tx, ty);
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-05-07 01:43:35 +00:00
|
|
|
if (result !== false && result !== null && result !== undefined)
|
2014-04-24 04:19:45 +00:00
|
|
|
{
|
|
|
|
this.setPixel32(tx, ty, result.r, result.g, result.b, result.a, false);
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
if (dirty)
|
2014-04-23 22:35:36 +00:00
|
|
|
{
|
2014-04-24 04:19:45 +00:00
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
},
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
/**
|
2014-04-28 04:33:52 +00:00
|
|
|
* 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.
|
2014-04-25 01:45:27 +00:00
|
|
|
* 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.
|
|
|
|
* Note that the format of the color received will be different depending on if the system is big or little endian.
|
|
|
|
* It is expected that your callback will deal with endianess. If you'd rather Phaser did it then use processPixelRGB instead.
|
2014-04-28 04:33:52 +00:00
|
|
|
* The callback will also be sent the pixels x and y coordinates respectively.
|
2014-04-25 01:45:27 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#processPixel
|
|
|
|
* @param {function} callback - The callback that will be sent each pixel color to be processed.
|
|
|
|
* @param {object} callbackContext - The context under which the callback will be called.
|
|
|
|
* @param {number} [x=0] - The x coordinate of the top-left of the region to process from.
|
|
|
|
* @param {number} [y=0] - The y coordinate of the top-left of the region to process from.
|
|
|
|
* @param {number} [width] - The width of the region to process.
|
|
|
|
* @param {number} [height] - The height of the region to process.
|
|
|
|
*/
|
|
|
|
processPixel: function (callback, callbackContext, x, y, width, height) {
|
|
|
|
|
|
|
|
if (typeof x === 'undefined') { x = 0; }
|
|
|
|
if (typeof y === 'undefined') { y = 0; }
|
|
|
|
if (typeof width === 'undefined') { width = this.width; }
|
|
|
|
if (typeof height === 'undefined') { height = this.height; }
|
|
|
|
|
|
|
|
var w = x + width;
|
|
|
|
var h = y + height;
|
|
|
|
var pixel = 0;
|
|
|
|
var result = 0;
|
|
|
|
var dirty = false;
|
|
|
|
|
|
|
|
for (var ty = y; ty < h; ty++)
|
|
|
|
{
|
|
|
|
for (var tx = x; tx < w; tx++)
|
|
|
|
{
|
|
|
|
pixel = this.getPixel32(tx, ty);
|
2014-04-28 04:33:52 +00:00
|
|
|
result = callback.call(callbackContext, pixel, tx, ty);
|
2014-04-25 01:45:27 +00:00
|
|
|
|
|
|
|
if (result !== pixel)
|
|
|
|
{
|
|
|
|
this.pixels[ty * this.width + tx] = result;
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirty)
|
|
|
|
{
|
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces all pixels matching one color with another. The color values are given as two sets of RGBA values.
|
|
|
|
* An optional region parameter controls if the replacement happens in just a specific area of the BitmapData or the entire thing.
|
2014-04-24 04:19:45 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#replaceRGB
|
|
|
|
* @param {number} r1 - The red color value to be replaced. Between 0 and 255.
|
|
|
|
* @param {number} g1 - The green color value to be replaced. Between 0 and 255.
|
|
|
|
* @param {number} b1 - The blue color value to be replaced. Between 0 and 255.
|
|
|
|
* @param {number} a1 - The alpha color value to be replaced. Between 0 and 255.
|
|
|
|
* @param {number} r2 - The red color value that is the replacement color. Between 0 and 255.
|
|
|
|
* @param {number} g2 - The green color value that is the replacement color. Between 0 and 255.
|
|
|
|
* @param {number} b2 - The blue color value that is the replacement color. Between 0 and 255.
|
|
|
|
* @param {number} a2 - The alpha color value that is the replacement color. Between 0 and 255.
|
|
|
|
* @param {Phaser.Rectangle} [region] - The area to perform the search over. If not given it will replace over the whole BitmapData.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-04-24 04:19:45 +00:00
|
|
|
*/
|
|
|
|
replaceRGB: function (r1, g1, b1, a1, r2, g2, b2, a2, region) {
|
|
|
|
|
|
|
|
var sx = 0;
|
|
|
|
var sy = 0;
|
|
|
|
var w = this.width;
|
|
|
|
var h = this.height;
|
2014-04-25 01:45:27 +00:00
|
|
|
var source = Phaser.Color.packPixel(r1, g1, b1, a1);
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
if (region !== undefined && region instanceof Phaser.Rectangle)
|
|
|
|
{
|
|
|
|
sx = region.x;
|
|
|
|
sy = region.y;
|
|
|
|
w = region.width;
|
|
|
|
h = region.height;
|
|
|
|
}
|
2014-04-23 22:35:36 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
for (var y = 0; y < h; y++)
|
|
|
|
{
|
|
|
|
for (var x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
if (this.getPixel32(sx + x, sy + y) === source)
|
|
|
|
{
|
|
|
|
this.setPixel32(sx + x, sy + y, r2, g2, b2, a2, false);
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 22:35:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-04-23 22:35:36 +00:00
|
|
|
},
|
|
|
|
|
2014-04-25 01:45:27 +00:00
|
|
|
/**
|
2014-04-29 01:34:16 +00:00
|
|
|
* Sets the hue, saturation and lightness values on every pixel in the given region, or the whole BitmapData if no region was specified.
|
2014-04-25 01:45:27 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#setHSL
|
2014-04-25 03:13:59 +00:00
|
|
|
* @param {number} [h=null] - The hue, in the range 0 - 1.
|
|
|
|
* @param {number} [s=null] - The saturation, in the range 0 - 1.
|
|
|
|
* @param {number} [l=null] - The lightness, in the range 0 - 1.
|
|
|
|
* @param {Phaser.Rectangle} [region] - The area to perform the operation on. If not given it will run over the whole BitmapData.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-04-25 01:45:27 +00:00
|
|
|
*/
|
|
|
|
setHSL: function (h, s, l, region) {
|
|
|
|
|
|
|
|
if (typeof h === 'undefined' || h === null) { h = false; }
|
|
|
|
if (typeof s === 'undefined' || s === null) { s = false; }
|
|
|
|
if (typeof l === 'undefined' || l === null) { l = false; }
|
|
|
|
|
|
|
|
if (!h && !s && !l)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
if (typeof region === 'undefined')
|
2014-04-25 01:45:27 +00:00
|
|
|
{
|
2014-04-25 03:13:59 +00:00
|
|
|
region = new Phaser.Rectangle(0, 0, this.width, this.height);
|
2014-04-25 01:45:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
var pixel = Phaser.Color.createColor();
|
|
|
|
|
|
|
|
for (var y = region.y; y < region.bottom; y++)
|
2014-04-25 01:45:27 +00:00
|
|
|
{
|
2014-04-25 03:13:59 +00:00
|
|
|
for (var x = region.x; x < region.right; x++)
|
2014-04-25 01:45:27 +00:00
|
|
|
{
|
2014-04-25 03:13:59 +00:00
|
|
|
Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel, true);
|
2014-04-25 01:45:27 +00:00
|
|
|
|
|
|
|
if (h)
|
|
|
|
{
|
|
|
|
pixel.h = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
pixel.s = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l)
|
|
|
|
{
|
|
|
|
pixel.l = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel);
|
2014-04-25 03:13:59 +00:00
|
|
|
this.setPixel32(x, y, pixel.r, pixel.g, pixel.b, pixel.a, false);
|
2014-04-25 01:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-04-25 01:45:27 +00:00
|
|
|
},
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
/**
|
|
|
|
* Shifts any or all of the hue, saturation and lightness values on every pixel in the given region, or the whole BitmapData if no region was specified.
|
|
|
|
* Shifting will add the given value onto the current h, s and l values, not replace them.
|
|
|
|
* The hue is wrapped to keep it within the range 0 to 1. Saturation and lightness are clamped to not exceed 1.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#shiftHSL
|
|
|
|
* @param {number} [h=null] - The amount to shift the hue by.
|
|
|
|
* @param {number} [s=null] - The amount to shift the saturation by.
|
|
|
|
* @param {number} [l=null] - The amount to shift the lightness by.
|
|
|
|
* @param {Phaser.Rectangle} [region] - The area to perform the operation on. If not given it will run over the whole BitmapData.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-04-25 03:13:59 +00:00
|
|
|
*/
|
|
|
|
shiftHSL: function (h, s, l, region) {
|
2014-04-25 01:45:27 +00:00
|
|
|
|
|
|
|
if (typeof h === 'undefined' || h === null) { h = false; }
|
|
|
|
if (typeof s === 'undefined' || s === null) { s = false; }
|
|
|
|
if (typeof l === 'undefined' || l === null) { l = false; }
|
|
|
|
|
|
|
|
if (!h && !s && !l)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
if (typeof region === 'undefined')
|
2014-04-25 01:45:27 +00:00
|
|
|
{
|
2014-04-25 03:13:59 +00:00
|
|
|
region = new Phaser.Rectangle(0, 0, this.width, this.height);
|
2014-04-25 01:45:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
var pixel = Phaser.Color.createColor();
|
|
|
|
|
|
|
|
for (var y = region.y; y < region.bottom; y++)
|
2014-04-25 01:45:27 +00:00
|
|
|
{
|
2014-04-25 03:13:59 +00:00
|
|
|
for (var x = region.x; x < region.right; x++)
|
2014-04-25 01:45:27 +00:00
|
|
|
{
|
2014-04-25 03:13:59 +00:00
|
|
|
Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel, true);
|
2014-04-25 01:45:27 +00:00
|
|
|
|
|
|
|
if (h)
|
|
|
|
{
|
|
|
|
pixel.h = this.game.math.wrap(pixel.h + h, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
pixel.s = this.game.math.limitValue(pixel.s + s, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l)
|
|
|
|
{
|
|
|
|
pixel.l = this.game.math.limitValue(pixel.l + l, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel);
|
2014-04-25 03:13:59 +00:00
|
|
|
this.setPixel32(x, y, pixel.r, pixel.g, pixel.b, pixel.a, false);
|
2014-04-25 01:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-04-25 01:45:27 +00:00
|
|
|
},
|
|
|
|
|
2014-04-23 22:35:36 +00:00
|
|
|
/**
|
|
|
|
* Sets the color of the given pixel to the specified red, green, blue and alpha values.
|
|
|
|
*
|
2013-11-28 15:57:09 +00:00
|
|
|
* @method Phaser.BitmapData#setPixel32
|
2014-04-24 04:19:45 +00:00
|
|
|
* @param {number} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {number} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @param {number} red - The red color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} green - The green color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} blue - The blue color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} alpha - The alpha color value, between 0 and 0xFF (255).
|
2014-04-24 04:19:45 +00:00
|
|
|
* @param {boolean} [immediate=true] - If `true` the context.putImageData will be called and the dirty flag set.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-04-24 04:19:45 +00:00
|
|
|
setPixel32: function (x, y, red, green, blue, alpha, immediate) {
|
|
|
|
|
|
|
|
if (typeof immediate === 'undefined') { immediate = true; }
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
|
|
|
|
{
|
2014-04-25 01:45:27 +00:00
|
|
|
if (Phaser.Device.LITTLE_ENDIAN)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-04-24 02:49:49 +00:00
|
|
|
this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-24 02:49:49 +00:00
|
|
|
this.pixels[y * this.width + x] = (red << 24) | (green << 16) | (blue << 8) | alpha;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2014-04-24 04:19:45 +00:00
|
|
|
if (immediate)
|
|
|
|
{
|
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2013-11-15 20:40:39 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Sets the color of the given pixel to the specified red, green and blue values.
|
2014-02-21 14:50:18 +00:00
|
|
|
*
|
2013-11-28 15:57:09 +00:00
|
|
|
* @method Phaser.BitmapData#setPixel
|
2014-04-24 04:19:45 +00:00
|
|
|
* @param {number} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {number} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {number} red - The red color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} green - The green color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} blue - The blue color value, between 0 and 0xFF (255).
|
|
|
|
* @param {number} alpha - The alpha color value, between 0 and 0xFF (255).
|
|
|
|
* @param {boolean} [immediate=true] - If `true` the context.putImageData will be called and the dirty flag set.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-04-24 04:19:45 +00:00
|
|
|
setPixel: function (x, y, red, green, blue, immediate) {
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this.setPixel32(x, y, red, green, blue, 255, immediate);
|
2013-11-15 20:40:39 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-04-25 03:13:59 +00:00
|
|
|
* Get the color of a specific pixel in the context into a color object.
|
2014-05-01 01:38:12 +00:00
|
|
|
* If you have drawn anything to the BitmapData since it was created you must call BitmapData.update to refresh the array buffer,
|
|
|
|
* otherwise this may return out of date color values, or worse - throw a run-time error as it tries to access an array element that doesn't exist.
|
2014-02-21 14:50:18 +00:00
|
|
|
*
|
2014-04-25 03:13:59 +00:00
|
|
|
* @method Phaser.BitmapData#getPixel
|
|
|
|
* @param {number} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {number} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {object} [out] - An object into which 4 properties will be created: r, g, b and a. If not provided a new object will be created.
|
|
|
|
* @return {object} An object with the red, green, blue and alpha values set in the r, g, b and a properties.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-04-24 02:49:49 +00:00
|
|
|
getPixel: function (x, y, out) {
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
if (!out)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-04-25 01:45:27 +00:00
|
|
|
out = Phaser.Color.createColor();
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2014-04-25 01:45:27 +00:00
|
|
|
var index = ~~(x + (y * this.width));
|
|
|
|
|
|
|
|
index *= 4;
|
|
|
|
|
2014-06-09 21:34:06 +00:00
|
|
|
out.r = this.data[index];
|
|
|
|
out.g = this.data[++index];
|
|
|
|
out.b = this.data[++index];
|
|
|
|
out.a = this.data[++index];
|
2014-04-24 02:49:49 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
|
2013-11-15 20:40:39 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-21 14:50:18 +00:00
|
|
|
* Get the color of a specific pixel including its alpha value.
|
2014-05-01 01:38:12 +00:00
|
|
|
* If you have drawn anything to the BitmapData since it was created you must call BitmapData.update to refresh the array buffer,
|
|
|
|
* otherwise this may return out of date color values, or worse - throw a run-time error as it tries to access an array element that doesn't exist.
|
2014-04-24 02:49:49 +00:00
|
|
|
* Note that on little-endian systems the format is 0xAABBGGRR and on big-endian the format is 0xRRGGBBAA.
|
2014-02-21 14:50:18 +00:00
|
|
|
*
|
2014-04-23 22:35:36 +00:00
|
|
|
* @method Phaser.BitmapData#getPixel32
|
2014-04-25 03:13:59 +00:00
|
|
|
* @param {number} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {number} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @return {number} A native color value integer (format: 0xAARRGGBB)
|
|
|
|
*/
|
2013-11-15 20:40:39 +00:00
|
|
|
getPixel32: function (x, y) {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
|
|
|
|
{
|
2014-04-24 02:49:49 +00:00
|
|
|
return this.pixels[y * this.width + x];
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the color of a specific pixel including its alpha value as a color object containing r,g,b,a and rgba properties.
|
2014-05-01 01:38:12 +00:00
|
|
|
* If you have drawn anything to the BitmapData since it was created you must call BitmapData.update to refresh the array buffer,
|
|
|
|
* otherwise this may return out of date color values, or worse - throw a run-time error as it tries to access an array element that doesn't exist.
|
2014-04-24 02:49:49 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#getPixelRGB
|
2014-04-25 03:13:59 +00:00
|
|
|
* @param {number} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {number} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData.
|
|
|
|
* @param {object} [out] - An object into which 3 properties will be created: r, g and b. If not provided a new object will be created.
|
|
|
|
* @param {boolean} [hsl=false] - Also convert the rgb values into hsl?
|
|
|
|
* @param {boolean} [hsv=false] - Also convert the rgb values into hsv?
|
|
|
|
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
|
2014-04-24 02:49:49 +00:00
|
|
|
*/
|
2014-04-25 03:13:59 +00:00
|
|
|
getPixelRGB: function (x, y, out, hsl, hsv) {
|
2014-04-24 02:49:49 +00:00
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
return Phaser.Color.unpackPixel(this.getPixel32(x, y), out, hsl, hsv);
|
2013-11-15 20:40:39 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-21 14:50:18 +00:00
|
|
|
* Gets all the pixels from the region specified by the given Rectangle object.
|
|
|
|
*
|
2014-04-23 22:35:36 +00:00
|
|
|
* @method Phaser.BitmapData#getPixels
|
2014-02-21 14:50:18 +00:00
|
|
|
* @param {Phaser.Rectangle} rect - The Rectangle region to get.
|
2014-04-24 02:49:49 +00:00
|
|
|
* @return {ImageData} Returns a ImageData object containing a Uint8ClampedArray data property.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-11-15 20:40:39 +00:00
|
|
|
getPixels: function (rect) {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
return this.context.getImageData(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-05-29 02:30:03 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Phaser.Image object, assigns this BitmapData to be its texture, adds it to the world then returns it.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#addToWorld
|
|
|
|
* @param {number} [x=0] - The x coordinate to place the image at.
|
|
|
|
* @param {number} [y=0] - The y coordinate to place the image at.
|
|
|
|
* @return {Phaser.Image} The newly added Image object.
|
|
|
|
*/
|
|
|
|
addToWorld: function (x, y) {
|
|
|
|
|
|
|
|
return this.game.add.image(x, y, this);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-09-03 00:28:58 +00:00
|
|
|
/**
|
2014-09-03 10:12:06 +00:00
|
|
|
* Copies a rectangular region from the source image to this BitmapData.
|
|
|
|
* You can optionally resize, translate, rotate, scale, alpha or blend the block as it's drawn.
|
2014-09-03 00:28:58 +00:00
|
|
|
* All rotation, scaling and drawing takes place around the blocks center point by default, but can be changed with the anchor parameters.
|
2014-09-03 10:12:06 +00:00
|
|
|
* Note that the source image can also be this BitmapData, which can create some interesting effects.
|
|
|
|
*
|
|
|
|
* This method has a lot of parameters for maximum control. You can use the more friendly methods like `draw` to avoid having to remember them all.
|
2014-09-03 00:28:58 +00:00
|
|
|
*
|
2014-09-03 10:12:06 +00:00
|
|
|
* @method Phaser.BitmapData#copy
|
|
|
|
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} source - The source to copy from. If you give a string it will try and find the Image in the Game.Cache first. This is quite expensie so try to provide the image itself.
|
|
|
|
* @param {number} [x=0] - The x coordinate representing the top-left of the region to copy from the source image.
|
|
|
|
* @param {number} [y=0] - The y coordinate representing the top-left of the region to copy from the source image.
|
|
|
|
* @param {number} [width] - The width of the region to copy from the source image. If not specified it will use the full source image width.
|
|
|
|
* @param {number} [height] - The height of the region to copy from the source image. If not specified it will use the full source image height.
|
|
|
|
* @param {number} [tx] - The x coordinate to translate to before drawing. If not specified it will default to the `x` parameter.
|
|
|
|
* @param {number} [ty] - The y coordinate to translate to before drawing. If not specified it will default to the `y` parameter.
|
|
|
|
* @param {number} [newWidth] - The new width of the block being copied. If not specified it will default to the `width` parameter.
|
|
|
|
* @param {number} [newHeight] - The new height of the block being copied. If not specified it will default to the `height` parameter.
|
2014-09-03 00:28:58 +00:00
|
|
|
* @param {number} [rotate=0] - The angle in degrees to rotate the block to before drawing. Rotation takes place around the center by default, but can be changed with the `anchor` parameters.
|
|
|
|
* @param {number} [anchorX=0.5] - The anchor point around with the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
|
|
|
|
* @param {number} [anchorY=0.5] - The anchor point around with the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
|
|
|
|
* @param {number} [scaleX=1] - The horizontal scale factor of the block. A value of 1 means no scaling. 2 would be twice the size, and so on.
|
|
|
|
* @param {number} [scaleY=1] - The vertical scale factor of the block. A value of 1 means no scaling. 2 would be twice the size, and so on.
|
|
|
|
* @param {number} [alpha=1] - The alpha that will be set on the context before the block is drawn. A value between 0 (fully transparent) and 1, opaque.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @param {number} [blendMode=null] - The composite blend mode that will be used when drawing the block. The default is no blend mode at all.
|
2014-09-03 00:28:58 +00:00
|
|
|
* @param {boolean} [roundPx=false] - Should the x and y values be rounded to integers before drawing? This prevents anti-aliasing in some instances.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-09-03 00:28:58 +00:00
|
|
|
*/
|
2014-09-03 10:12:06 +00:00
|
|
|
copy: function (source, x, y, width, height, tx, ty, newWidth, newHeight, rotate, anchorX, anchorY, scaleX, scaleY, alpha, blendMode, roundPx) {
|
2014-09-02 22:16:10 +00:00
|
|
|
|
|
|
|
var sx = 0;
|
|
|
|
var sy = 0;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
if (source instanceof Phaser.Sprite || source instanceof Phaser.Image)
|
2014-09-02 22:16:10 +00:00
|
|
|
{
|
|
|
|
sx = source.texture.frame.x;
|
|
|
|
sy = source.texture.frame.y;
|
|
|
|
source = source.texture.baseTexture.image;
|
|
|
|
}
|
|
|
|
else if (source instanceof Phaser.BitmapData)
|
|
|
|
{
|
|
|
|
source = source.canvas;
|
|
|
|
}
|
2014-09-03 10:12:06 +00:00
|
|
|
else if (typeof source === 'string')
|
2014-09-03 00:28:58 +00:00
|
|
|
{
|
2014-09-03 10:12:06 +00:00
|
|
|
source = this.game.cache.getImage(source);
|
2014-09-02 22:16:10 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
if (source === null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-09-02 22:16:10 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
// The source region to copy from
|
|
|
|
if (typeof x === 'undefined' || x === null) { x = 0; }
|
|
|
|
if (typeof y === 'undefined' || y === null) { y = 0; }
|
|
|
|
if (typeof width === 'undefined' || width === null) { width = source.width; }
|
|
|
|
if (typeof height === 'undefined' || height === null) { height = source.height; }
|
2014-09-02 22:16:10 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
// The destination region to copy to
|
|
|
|
if (typeof tx === 'undefined' || tx === null) { tx = x; }
|
|
|
|
if (typeof ty === 'undefined' || ty === null) { ty = y; }
|
|
|
|
if (typeof newWidth === 'undefined' || newWidth === null) { newWidth = width; }
|
|
|
|
if (typeof newHeight === 'undefined' || newHeight === null) { newHeight = width; }
|
2014-09-02 22:16:10 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
// Rotation
|
|
|
|
if (typeof rotate === 'undefined' || rotate === null) { rotate = 0; }
|
|
|
|
if (typeof anchorX === 'undefined' || anchorX === null) { anchorX = 0.5; }
|
|
|
|
if (typeof anchorY === 'undefined' || anchorY === null) { anchorY = 0.5; }
|
2014-09-02 22:16:10 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
// Scaling
|
|
|
|
if (typeof scaleX === 'undefined' || scaleX === null) { scaleX = 1; }
|
|
|
|
if (typeof scaleY === 'undefined' || scaleY === null) { scaleY = 1; }
|
2014-09-01 19:22:47 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
// Effects
|
|
|
|
if (typeof alpha === 'undefined' || alpha === null) { alpha = 1; }
|
|
|
|
if (typeof blendMode === 'undefined') { blendMode = null; }
|
|
|
|
if (typeof roundPx === 'undefined') { roundPx = false; }
|
2014-09-02 20:03:16 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
if (alpha <= 0 || scaleX === 0 || scaleY === 0)
|
2014-09-01 19:22:47 +00:00
|
|
|
{
|
2014-09-03 10:12:06 +00:00
|
|
|
// Because why bother wasting CPU cycles drawing something you can't see?
|
|
|
|
return;
|
2014-09-01 19:22:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
var prevAlpha = this.context.globalAlpha;
|
2014-09-01 19:22:47 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.save();
|
2014-09-02 20:03:16 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.globalAlpha = alpha;
|
2014-09-01 19:22:47 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
if (blendMode)
|
2014-09-01 19:22:47 +00:00
|
|
|
{
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.globalCompositeOperation = blendMode;
|
2014-09-01 19:22:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
if (roundPx)
|
2014-09-01 19:22:47 +00:00
|
|
|
{
|
2014-09-03 10:12:06 +00:00
|
|
|
x |= 0;
|
|
|
|
y |= 0;
|
2014-09-01 19:22:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.translate(tx, ty);
|
2014-09-01 19:22:47 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.scale(scaleX, scaleY);
|
2014-09-01 19:22:47 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.rotate(rotate * Math.PI / 180);
|
2014-09-01 19:22:47 +00:00
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.drawImage(source, sx + x, sy + y, width, height, -newWidth * anchorX, -newHeight * anchorY, newWidth, newHeight);
|
2014-09-01 19:22:47 +00:00
|
|
|
|
2014-09-02 20:03:16 +00:00
|
|
|
this.context.restore();
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
this.context.globalAlpha = prevAlpha;
|
2014-09-02 20:03:16 +00:00
|
|
|
|
2014-09-01 19:22:47 +00:00
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-09-01 19:22:47 +00:00
|
|
|
},
|
|
|
|
|
2014-02-21 14:50:18 +00:00
|
|
|
/**
|
|
|
|
* Copies the pixels from the source image to this BitmapData based on the given area and destination.
|
|
|
|
*
|
2014-04-23 22:35:36 +00:00
|
|
|
* @method Phaser.BitmapData#copyPixels
|
2014-05-29 02:30:03 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} source - The Image to copy from. If you give a string it will try and find the Image in the Game.Cache.
|
2014-02-21 14:50:18 +00:00
|
|
|
* @param {Phaser.Rectangle} area - The Rectangle region to copy from the source image.
|
2014-05-29 02:30:03 +00:00
|
|
|
* @param {number} x - The destination x coordinate to copy the image to.
|
|
|
|
* @param {number} y - The destination y coordinate to copy the image to.
|
2014-08-27 20:24:53 +00:00
|
|
|
* @param {number} [alpha] - An optional alpha value. If given the BitmapData.context.globalAlpha will be set to this before drawing and reset after.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-02-21 14:50:18 +00:00
|
|
|
*/
|
2014-08-27 20:24:53 +00:00
|
|
|
copyPixels: function (source, area, x, y, alpha) {
|
2014-02-14 06:04:29 +00:00
|
|
|
|
2014-02-21 15:09:04 +00:00
|
|
|
if (typeof source === 'string')
|
|
|
|
{
|
|
|
|
source = this.game.cache.getImage(source);
|
|
|
|
}
|
|
|
|
|
2014-08-27 20:24:53 +00:00
|
|
|
if (typeof alpha === 'undefined')
|
|
|
|
{
|
|
|
|
alpha = null;
|
|
|
|
}
|
|
|
|
else if (alpha <= 0)
|
|
|
|
{
|
|
|
|
// No point doing anything if alpha is zero
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (alpha > 1)
|
|
|
|
{
|
|
|
|
// Sanity cap
|
|
|
|
alpha = 1;
|
|
|
|
}
|
|
|
|
|
2014-05-29 02:30:03 +00:00
|
|
|
var src = source;
|
|
|
|
var sx = 0;
|
|
|
|
var sy = 0;
|
|
|
|
|
|
|
|
if (source instanceof Phaser.Image || source instanceof Phaser.Sprite)
|
|
|
|
{
|
|
|
|
src = source.texture.baseTexture.source;
|
|
|
|
var frame = source.texture.frame;
|
|
|
|
sx = frame.x;
|
|
|
|
sy = frame.y;
|
|
|
|
}
|
|
|
|
else
|
2014-02-21 15:09:04 +00:00
|
|
|
{
|
2014-05-29 02:30:03 +00:00
|
|
|
if (source instanceof Phaser.BitmapData)
|
|
|
|
{
|
|
|
|
src = source.canvas;
|
|
|
|
}
|
2014-02-21 15:09:04 +00:00
|
|
|
}
|
2014-02-14 06:04:29 +00:00
|
|
|
|
2014-08-27 20:24:53 +00:00
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
var prevAlpha = this.context.globalAlpha;
|
|
|
|
this.context.globalAlpha = alpha;
|
|
|
|
}
|
|
|
|
|
2014-05-29 02:30:03 +00:00
|
|
|
this.context.drawImage(src, sx + area.x, sy + area.y, area.width, area.height, x, y, area.width, area.height);
|
|
|
|
|
2014-08-27 20:24:53 +00:00
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
this.context.globalAlpha = prevAlpha;
|
|
|
|
}
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-02-14 06:04:29 +00:00
|
|
|
},
|
|
|
|
|
2014-02-21 14:50:18 +00:00
|
|
|
/**
|
2014-05-24 02:15:13 +00:00
|
|
|
* Draws the given image or Game Object to this BitmapData at the coordinates specified.
|
2014-05-24 02:58:00 +00:00
|
|
|
* You can use the optional width and height values to 'stretch' the image as it's drawn.
|
2014-02-21 14:50:18 +00:00
|
|
|
*
|
2014-04-23 22:35:36 +00:00
|
|
|
* @method Phaser.BitmapData#draw
|
2014-05-24 02:15:13 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} source - The Image to draw. If you give a string it will try and find the Image in the Game.Cache.
|
2014-04-23 22:35:36 +00:00
|
|
|
* @param {number} [x=0] - The x coordinate to draw the image to.
|
|
|
|
* @param {number} [y=0] - The y coordinate to draw the image to.
|
2014-05-24 02:58:00 +00:00
|
|
|
* @param {number} [width] - The width when drawing the image. You can use this to optionally stretch the drawn image horizontally.
|
|
|
|
* @param {number} [height] - The height when drawing the image. You can use this to optionally stretch the drawn image vertically.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-02-21 15:09:04 +00:00
|
|
|
*/
|
2014-05-24 02:58:00 +00:00
|
|
|
draw: function (source, x, y, width, height) {
|
2014-04-23 22:35:36 +00:00
|
|
|
|
|
|
|
if (typeof x === 'undefined') { x = 0; }
|
|
|
|
if (typeof y === 'undefined') { y = 0; }
|
2014-02-21 15:09:04 +00:00
|
|
|
|
|
|
|
if (typeof source === 'string')
|
|
|
|
{
|
|
|
|
source = this.game.cache.getImage(source);
|
|
|
|
}
|
|
|
|
|
2014-05-24 02:58:00 +00:00
|
|
|
var src = source;
|
|
|
|
var sx = 0;
|
|
|
|
var sy = 0;
|
|
|
|
var sw = 0;
|
|
|
|
var sh = 0;
|
|
|
|
|
|
|
|
if (source instanceof Phaser.Image || source instanceof Phaser.Sprite)
|
2014-05-24 02:15:13 +00:00
|
|
|
{
|
2014-05-26 19:15:11 +00:00
|
|
|
src = source.texture.baseTexture.source;
|
|
|
|
var frame = source.texture.frame;
|
2014-05-24 02:58:00 +00:00
|
|
|
sx = frame.x;
|
|
|
|
sy = frame.y;
|
|
|
|
sw = frame.width;
|
|
|
|
sh = frame.height;
|
|
|
|
// this.context.drawImage(sprite.texture.baseTexture.source, frame.x, frame.y, frame.width, frame.height, x, y, frame.width, frame.height);
|
2014-05-24 02:15:13 +00:00
|
|
|
}
|
2014-05-24 01:43:59 +00:00
|
|
|
else
|
2014-02-21 15:09:04 +00:00
|
|
|
{
|
2014-05-24 02:58:00 +00:00
|
|
|
if (source instanceof Phaser.BitmapData)
|
|
|
|
{
|
|
|
|
src = source.canvas;
|
|
|
|
// this.context.drawImage(source.canvas, 0, 0, source.width, source.height, x, y, source.width, source.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
sw = source.width;
|
|
|
|
sh = source.height;
|
|
|
|
// this.context.drawImage(source, 0, 0, source.width, source.height, x, y, source.width, source.height);
|
2014-02-21 15:09:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 02:58:00 +00:00
|
|
|
if (typeof width === 'undefined') { width = sw; }
|
|
|
|
if (typeof height === 'undefined') { height = sh; }
|
|
|
|
|
|
|
|
this.context.drawImage(src, sx, sy, sw, sh, x, y, width, height);
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-02-21 15:09:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the given image onto this BitmapData using an image as an alpha mask.
|
|
|
|
*
|
2014-04-20 20:17:01 +00:00
|
|
|
* @method Phaser.BitmapData#alphaMask
|
2014-05-24 02:15:13 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
|
2014-05-24 02:58:00 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string|null} [mask] - The Image to use as the alpha mask. If you give a key it will try and find the Image in the Game.Cache. If you pass nothing or null it will use itself.
|
|
|
|
* @param {Phaser.Rectangle} [sourceRect] - A Rectangle where x/y define the coordinates to draw the Source image to and width/height define the size.
|
|
|
|
* @param {Phaser.Rectangle} [maskRect] - A Rectangle where x/y define the coordinates to draw the Mask image to and width/height define the size.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-02-21 14:50:18 +00:00
|
|
|
*/
|
2014-05-24 02:58:00 +00:00
|
|
|
alphaMask: function (source, mask, sourceRect, maskRect) {
|
2014-02-21 14:50:18 +00:00
|
|
|
|
2014-05-24 02:58:00 +00:00
|
|
|
if (typeof mask === 'undefined' || mask === null) { mask = this; }
|
2014-02-21 14:50:18 +00:00
|
|
|
|
2014-05-24 02:15:13 +00:00
|
|
|
var temp = this.context.globalCompositeOperation;
|
2014-02-21 15:09:04 +00:00
|
|
|
|
2014-05-24 02:58:00 +00:00
|
|
|
if (typeof maskRect === 'undefined' || maskRect === null)
|
|
|
|
{
|
|
|
|
this.draw(mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.draw(mask, maskRect.x, maskRect.y, maskRect.width, maskRect.height);
|
|
|
|
}
|
2014-02-21 15:09:04 +00:00
|
|
|
|
2014-02-21 14:50:18 +00:00
|
|
|
this.context.globalCompositeOperation = 'source-atop';
|
2014-02-21 15:09:04 +00:00
|
|
|
|
2014-05-24 02:58:00 +00:00
|
|
|
if (typeof sourceRect === 'undefined' || sourceRect === null)
|
|
|
|
{
|
|
|
|
this.draw(source);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.draw(source, sourceRect.x, sourceRect.y, sourceRect.width, sourceRect.height);
|
|
|
|
}
|
2014-02-21 14:50:18 +00:00
|
|
|
|
|
|
|
this.context.globalCompositeOperation = temp;
|
|
|
|
|
2014-05-24 02:17:58 +00:00
|
|
|
this.update();
|
|
|
|
|
2014-04-25 03:13:59 +00:00
|
|
|
this.dirty = true;
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-04-20 20:17:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-28 04:33:52 +00:00
|
|
|
* Scans this BitmapData for all pixels matching the given r,g,b values and then draws them into the given destination BitmapData.
|
2014-05-24 00:29:47 +00:00
|
|
|
* The original BitmapData remains unchanged.
|
2014-05-24 01:43:59 +00:00
|
|
|
* The destination BitmapData must be large enough to receive all of the pixels that are scanned unless the 'resize' parameter is true.
|
2014-04-28 04:33:52 +00:00
|
|
|
* Although the destination BitmapData is returned from this method, it's actually modified directly in place, meaning this call is perfectly valid:
|
|
|
|
* `picture.extract(mask, r, g, b)`
|
2014-05-24 00:29:47 +00:00
|
|
|
* You can specify optional r2, g2, b2 color values. If given the pixel written to the destination bitmap will be of the r2, g2, b2 color.
|
|
|
|
* If not given it will be written as the same color it was extracted. You can provide one or more alternative colors, allowing you to tint
|
|
|
|
* the color during extraction.
|
2014-04-20 20:17:01 +00:00
|
|
|
*
|
2014-04-28 04:33:52 +00:00
|
|
|
* @method Phaser.BitmapData#extract
|
|
|
|
* @param {Phaser.BitmapData} destination - The BitmapData that the extracts pixels will be drawn to.
|
|
|
|
* @param {number} r - The red color component, in the range 0 - 255.
|
|
|
|
* @param {number} g - The green color component, in the range 0 - 255.
|
|
|
|
* @param {number} b - The blue color component, in the range 0 - 255.
|
2014-05-24 01:43:59 +00:00
|
|
|
* @param {number} [a=255] - The alpha color component, in the range 0 - 255 that the new pixel will be drawn at.
|
|
|
|
* @param {boolean} [resize=false] - Should the destination BitmapData be resized to match this one before the pixels are copied?
|
2014-05-24 00:29:47 +00:00
|
|
|
* @param {number} [r2] - An alternative red color component to be written to the destination, in the range 0 - 255.
|
|
|
|
* @param {number} [g2] - An alternative green color component to be written to the destination, in the range 0 - 255.
|
|
|
|
* @param {number} [b2] - An alternative blue color component to be written to the destination, in the range 0 - 255.
|
2014-04-28 04:33:52 +00:00
|
|
|
* @returns {Phaser.BitmapData} The BitmapData that the extract pixels were drawn on.
|
2014-04-20 20:17:01 +00:00
|
|
|
*/
|
2014-05-24 01:53:48 +00:00
|
|
|
extract: function (destination, r, g, b, a, resize, r2, g2, b2) {
|
2014-04-28 04:33:52 +00:00
|
|
|
|
|
|
|
if (typeof a === 'undefined') { a = 255; }
|
2014-05-24 01:43:59 +00:00
|
|
|
if (typeof resize === 'undefined') { resize = false; }
|
2014-05-24 00:29:47 +00:00
|
|
|
if (typeof r2 === 'undefined') { r2 = r; }
|
|
|
|
if (typeof g2 === 'undefined') { g2 = g; }
|
|
|
|
if (typeof b2 === 'undefined') { b2 = b; }
|
2014-05-24 01:43:59 +00:00
|
|
|
|
|
|
|
if (resize)
|
|
|
|
{
|
|
|
|
destination.resize(this.width, this.height);
|
|
|
|
}
|
2014-04-20 20:17:01 +00:00
|
|
|
|
2014-04-28 04:33:52 +00:00
|
|
|
this.processPixelRGB(
|
2014-05-24 01:43:59 +00:00
|
|
|
function (pixel, x, y)
|
|
|
|
{
|
2014-04-28 04:33:52 +00:00
|
|
|
if (pixel.r === r && pixel.g === g && pixel.b === b)
|
|
|
|
{
|
2014-05-24 01:43:59 +00:00
|
|
|
destination.setPixel32(x, y, r2, g2, b2, a, false);
|
2014-04-28 04:33:52 +00:00
|
|
|
}
|
|
|
|
return false;
|
2014-04-28 12:48:23 +00:00
|
|
|
},
|
|
|
|
this);
|
2014-04-20 20:17:01 +00:00
|
|
|
|
2014-04-28 04:33:52 +00:00
|
|
|
destination.context.putImageData(destination.imageData, 0, 0);
|
|
|
|
destination.dirty = true;
|
2014-04-20 20:17:01 +00:00
|
|
|
|
2014-04-28 04:33:52 +00:00
|
|
|
return destination;
|
2014-04-20 20:17:01 +00:00
|
|
|
|
2014-04-28 03:19:26 +00:00
|
|
|
},
|
|
|
|
|
2014-05-09 15:37:40 +00:00
|
|
|
/**
|
|
|
|
* Draws a filled Rectangle to the BitmapData at the given x, y coordinates and width / height in size.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#rect
|
|
|
|
* @param {number} x - The x coordinate of the top-left of the Rectangle.
|
|
|
|
* @param {number} y - The y coordinate of the top-left of the Rectangle.
|
|
|
|
* @param {number} width - The width of the Rectangle.
|
|
|
|
* @param {number} height - The height of the Rectangle.
|
|
|
|
* @param {string} [fillStyle] - If set the context fillStyle will be set to this value before the rect is drawn.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-05-09 15:37:40 +00:00
|
|
|
*/
|
|
|
|
rect: function (x, y, width, height, fillStyle) {
|
|
|
|
|
|
|
|
if (typeof fillStyle !== 'undefined')
|
|
|
|
{
|
|
|
|
this.context.fillStyle = fillStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.context.fillRect(x, y, width, height);
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-05-09 15:37:40 +00:00
|
|
|
},
|
|
|
|
|
2014-04-28 03:19:26 +00:00
|
|
|
/**
|
|
|
|
* Draws a filled Circle to the BitmapData at the given x, y coordinates and radius in size.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#circle
|
2014-05-07 17:10:13 +00:00
|
|
|
* @param {number} x - The x coordinate to draw the Circle at. This is the center of the circle.
|
|
|
|
* @param {number} y - The y coordinate to draw the Circle at. This is the center of the circle.
|
|
|
|
* @param {number} radius - The radius of the Circle in pixels. The radius is half the diameter.
|
2014-04-28 03:19:26 +00:00
|
|
|
* @param {string} [fillStyle] - If set the context fillStyle will be set to this value before the circle is drawn.
|
2014-09-03 10:12:06 +00:00
|
|
|
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
|
2014-04-28 03:19:26 +00:00
|
|
|
*/
|
|
|
|
circle: function (x, y, radius, fillStyle) {
|
|
|
|
|
|
|
|
if (typeof fillStyle !== 'undefined')
|
|
|
|
{
|
|
|
|
this.context.fillStyle = fillStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.context.beginPath();
|
|
|
|
this.context.arc(x, y, radius, 0, Math.PI * 2, false);
|
|
|
|
this.context.closePath();
|
|
|
|
|
|
|
|
this.context.fill();
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
return this;
|
|
|
|
|
2014-02-21 14:50:18 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* If the game is running in WebGL this will push the texture up to the GPU if it's dirty.
|
|
|
|
* This is called automatically if the BitmapData is being used by a Sprite, otherwise you need to remember to call it in your render function.
|
2014-04-24 02:49:49 +00:00
|
|
|
* If you wish to suppress this functionality set BitmapData.disableTextureUpload to `true`.
|
2014-02-21 14:50:18 +00:00
|
|
|
*
|
2013-11-28 15:57:09 +00:00
|
|
|
* @method Phaser.BitmapData#render
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
render: function () {
|
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
if (!this.disableTextureUpload && this.game.renderType === Phaser.WEBGL && this.dirty)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
// Only needed if running in WebGL, otherwise this array will never get cleared down
|
2014-03-21 18:04:24 +00:00
|
|
|
// should use the rendersession
|
|
|
|
PIXI.updateWebGLTexture(this.baseTexture, this.game.renderer.gl);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-21 18:04:24 +00:00
|
|
|
this.dirty = false;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:12:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
blendReset: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'source-over';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendSourceOver: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'source-over';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendAdd: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'lighter';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendMultiply: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'multiply';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendScreen: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'screen';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendOverlay: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'overlay';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendDarken: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'darken';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendLighten: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'lighten';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendColorDodge: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'color-dodge';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendColorBurn: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'color-burn';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendHardLight: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'hard-light';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendSoftLight: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'soft-light';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendDifference: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'difference';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendExclusion: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'exclusion';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendHue: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'hue';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendSaturation: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'saturation';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendColor: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'color';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
blendLuminosity: function () {
|
|
|
|
|
|
|
|
this.context.globalCompositeOperation = 'luminosity';
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
2013-11-13 20:57:09 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
|
|
|
|
2014-09-02 20:03:16 +00:00
|
|
|
/**
|
|
|
|
* Gets a JavaScript object that has 6 properties set that are used by BitmapData in a transform.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData.getTransform
|
|
|
|
* @param {number} translateX - The x translate value.
|
|
|
|
* @param {number} translateY - The y translate value.
|
|
|
|
* @param {number} scaleX - The scale x value.
|
|
|
|
* @param {number} scaleY - The scale y value.
|
|
|
|
* @param {number} skewX - The skew x value.
|
|
|
|
* @param {number} skewY - The skew y value.
|
|
|
|
* @return {Object} A JavaScript object containing all of the properties BitmapData needs for transforms.
|
|
|
|
*/
|
|
|
|
Phaser.BitmapData.getTransform = function (translateX, translateY, scaleX, scaleY, skewX, skewY) {
|
|
|
|
|
|
|
|
if (typeof translateX !== 'number') { translateX = 0; }
|
|
|
|
if (typeof translateY !== 'number') { translateY = 0; }
|
|
|
|
if (typeof scaleX !== 'number') { scaleX = 1; }
|
|
|
|
if (typeof scaleY !== 'number') { scaleY = 1; }
|
|
|
|
if (typeof skewX !== 'number') { skewX = 0; }
|
|
|
|
if (typeof skewY !== 'number') { skewY = 0; }
|
|
|
|
|
|
|
|
return { sx: scaleX, sy: scaleY, scaleX: scaleX, scaleY: scaleY, skewX: skewX, skewY: skewY, translateX: translateX, translateY: translateY, tx: translateX, ty: translateY };
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
Phaser.BitmapData.prototype.constructor = Phaser.BitmapData;
|