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-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;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-24 02:49:49 +00:00
|
|
|
this.buffer = new ArrayBuffer(this.imageData.data.length);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
/**
|
|
|
|
* @property {Uint8ClampedArray} data - A Uint8ClampedArray view into BitmapData.buffer.
|
|
|
|
*/
|
|
|
|
this.data = this.imageData.data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Uint32Array} pixels - A Uint32Array view into BitmapData.buffer.
|
|
|
|
*/
|
|
|
|
this.pixels = new Uint32Array(this.buffer);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
/**
|
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-04-23 22:35:36 +00:00
|
|
|
/**
|
2014-04-24 02:49:49 +00:00
|
|
|
* @property {boolean} littleEndian - True if Little Endian, false if Big Endian.
|
2014-04-23 22:35:36 +00:00
|
|
|
*/
|
2014-04-24 02:49:49 +00:00
|
|
|
this.littleEndian = this.game.device.littleEndian;
|
|
|
|
|
|
|
|
// Aliases
|
2014-04-23 22:35:36 +00:00
|
|
|
this.cls = this.clear;
|
2014-04-24 02:49:49 +00:00
|
|
|
this.update = this.refreshBuffer;
|
2014-04-23 22:35:36 +00:00
|
|
|
|
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.
|
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-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
|
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-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).
|
|
|
|
* @param {number} [a=255] - The alpha color value, between 0 and 0xFF (255).
|
|
|
|
*/
|
|
|
|
fill: function (r, g, b, a) {
|
|
|
|
|
|
|
|
if (typeof a === 'undefined') { a = 255; }
|
|
|
|
|
|
|
|
this.context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
|
|
|
|
this.context.fillRect(0, 0, this.width, this.height);
|
|
|
|
this.dirty = true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
resize: function (width, height) {
|
|
|
|
|
|
|
|
if (width !== this.width || height !== this.height)
|
|
|
|
{
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
this.canvas.width = width;
|
|
|
|
this.canvas.height = height;
|
|
|
|
this.textureFrame.width = width;
|
|
|
|
this.textureFrame.height = height;
|
2014-04-24 02:49:49 +00:00
|
|
|
this.refreshBuffer();
|
2014-02-14 06:04:29 +00:00
|
|
|
}
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-21 18:04:24 +00:00
|
|
|
this.dirty = true;
|
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.
|
|
|
|
* It then re-builds the ArrayBuffer, the data Uint8ClampedArray reference and the pixels Uint32Array.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This re-creates the BitmapData.imageData from the current context.
|
|
|
|
* It then re-builds the ArrayBuffer, the data Uint8ClampedArray reference and the pixels Uint32Array.
|
|
|
|
* If not given the dimensions defaults to the full size of the context.
|
|
|
|
*
|
2014-02-14 12:07:04 +00:00
|
|
|
* @method Phaser.BitmapData#refreshBuffer
|
2014-04-24 02:49:49 +00:00
|
|
|
* @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-02-14 12:07:04 +00:00
|
|
|
*/
|
2014-04-24 02:49:49 +00:00
|
|
|
refreshBuffer: function (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; }
|
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);
|
|
|
|
|
|
|
|
if (this.imageData.data.buffer)
|
|
|
|
{
|
|
|
|
this.buffer = this.imageData.data.buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.buffer = new ArrayBuffer(this.imageData.data.length);
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
this.data = this.imageData.data;
|
|
|
|
this.pixels = new Uint32Array(this.buffer);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the color of the given pixel to the specified red, green, blue and alpha values.
|
2014-04-23 22:35:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#replaceRGB
|
|
|
|
* @param {number} x - The X coordinate of the pixel to be set.
|
|
|
|
* @param {number} y - The Y coordinate of the pixel to be set.
|
|
|
|
* @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).
|
|
|
|
*/
|
|
|
|
replaceRGB: function (sourceR, sourceG, sourceB, sourceA, destR, destG, destB, destA, region) {
|
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
var tx = 0;
|
|
|
|
var ty = 0;
|
2014-04-23 22:35:36 +00:00
|
|
|
var w = this.width;
|
|
|
|
var h = this.height;
|
|
|
|
|
|
|
|
if (region instanceof Phaser.Rectangle)
|
|
|
|
{
|
2014-04-24 02:49:49 +00:00
|
|
|
tx = region.x;
|
|
|
|
ty = region.y;
|
2014-04-23 22:35:36 +00:00
|
|
|
w = region.width;
|
|
|
|
h = region.height;
|
|
|
|
}
|
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
// for (var x = tx; x < w)
|
2014-04-23 22:35:36 +00:00
|
|
|
|
|
|
|
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
|
|
|
|
{
|
|
|
|
this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
|
|
|
|
|
|
|
|
|
|
|
|
// this.imageData.data.set(this.data8);
|
|
|
|
|
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
|
|
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2013-11-25 03:13:04 +00:00
|
|
|
* @param {number} x - The X coordinate of the pixel to be set.
|
|
|
|
* @param {number} y - The Y coordinate of the pixel to be set.
|
|
|
|
* @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).
|
|
|
|
*/
|
2013-11-15 20:40:39 +00:00
|
|
|
setPixel32: function (x, y, red, green, blue, alpha) {
|
|
|
|
|
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
|
|
|
if (this.littleEndian)
|
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
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
this.context.putImageData(this.imageData, 0, 0);
|
2013-11-17 00:55:28 +00:00
|
|
|
|
2014-03-21 18:04:24 +00:00
|
|
|
this.dirty = true;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
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
|
2013-11-25 03:13:04 +00:00
|
|
|
* @param {number} x - The X coordinate of the pixel to be set.
|
|
|
|
* @param {number} y - The Y coordinate of the pixel to be set.
|
|
|
|
* @param {number} red - The red color value (between 0 and 255)
|
|
|
|
* @param {number} green - The green color value (between 0 and 255)
|
|
|
|
* @param {number} blue - The blue color value (between 0 and 255)
|
|
|
|
*/
|
2013-11-15 20:40:39 +00:00
|
|
|
setPixel: function (x, y, red, green, blue) {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
this.setPixel32(x, y, red, green, blue, 255);
|
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.
|
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
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @param {number} x - The X coordinate of the pixel to get.
|
|
|
|
* @param {number} y - The Y coordinate of the pixel to get.
|
|
|
|
* @return {number} A native color value integer (format: 0xRRGGBB)
|
|
|
|
*/
|
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
|
|
|
var index = ~~(x + (y * this.width));
|
|
|
|
|
|
|
|
index *= 4;
|
|
|
|
|
|
|
|
if (!out)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-04-24 02:49:49 +00:00
|
|
|
out = { r:0, g:0, b:0, a:0 };
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-11-15 20:40:39 +00:00
|
|
|
|
2014-04-24 02:49:49 +00:00
|
|
|
out.r = this.data[index];
|
|
|
|
out.g = this.data[++index];
|
|
|
|
out.b = this.data[++index];
|
|
|
|
out.a = this.data[++index];
|
|
|
|
|
|
|
|
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-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
|
2013-11-25 03:13:04 +00:00
|
|
|
* @param {number} x - The X coordinate of the pixel to get.
|
|
|
|
* @param {number} y - The Y coordinate of the pixel to get.
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#getPixelRGB
|
|
|
|
* @param {number} x - The X coordinate of the pixel to get.
|
|
|
|
* @param {number} y - The Y coordinate of the pixel to get.
|
|
|
|
* @return {object} The color object containing r, g, b, a and rgba properties.
|
|
|
|
*/
|
|
|
|
getPixelRGB: function (x, y) {
|
|
|
|
|
|
|
|
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
|
|
|
|
{
|
|
|
|
return this.unpackPixel(this.pixels[y * this.width + x]);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
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-04-24 02:49:49 +00:00
|
|
|
/**
|
|
|
|
* Packs the r, g, b, a components into a single integer, for use with Int32Array.
|
|
|
|
* If device is little endian then ABGR order is used. Otherwise RGBA order is used.
|
|
|
|
*
|
|
|
|
* @author Matt DesLauriers (@mattdesl)
|
|
|
|
* @method Phaser.BitmapData#packPixel
|
|
|
|
* @param {number} r - The red byte, 0-255
|
|
|
|
* @param {number} g - The green byte, 0-255
|
|
|
|
* @param {number} b - The blue byte, 0-255
|
|
|
|
* @param {number} a - The alpha byte, 0-255
|
|
|
|
* @return {number} The packed color
|
|
|
|
*/
|
|
|
|
packPixel: function (r, g, b, a) {
|
|
|
|
|
|
|
|
if (this.littleEndian)
|
|
|
|
{
|
|
|
|
return (a << 24) | (b << 16) | (g << 8) | r;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (r << 24) | (g << 16) | (b << 8) | a;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpacks the r, g, b, a components into the specified color object, or a new
|
|
|
|
* object, for use with Int32Array. If little endian, then ABGR order is used when
|
|
|
|
* unpacking, otherwise, RGBA order is used. The resulting color object has the
|
|
|
|
* `r, g, b, a` properties which are unrelated to endianness.
|
|
|
|
*
|
|
|
|
* Note that the integer is assumed to be packed in the correct endianness. On little-endian
|
|
|
|
* the format is 0xAABBGGRR and on big-endian the format is 0xRRGGBBAA. If you want a
|
|
|
|
* endian-independent method, use fromRGBA(rgba) and toRGBA(r, g, b, a).
|
|
|
|
*
|
|
|
|
* @author Matt DesLauriers (@mattdesl)
|
|
|
|
* @method Phaser.BitmapData#unpackPixel
|
|
|
|
* @param {number} rgba - The integer, packed in endian order by packPixel.
|
|
|
|
* @param {object} out - The color object with `r, g, b, a` properties, or null.
|
|
|
|
* @return {object} A color representing the pixel at that location.
|
|
|
|
*/
|
|
|
|
unpackPixel: function (rgba, out) {
|
|
|
|
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
out = { r: 0, g: 0, b: 0, a: 0, rgba: '' };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.littleEndian)
|
|
|
|
{
|
|
|
|
out.a = ((rgba & 0xff000000) >>> 24);
|
|
|
|
out.b = ((rgba & 0x00ff0000) >>> 16);
|
|
|
|
out.g = ((rgba & 0x0000ff00) >>> 8);
|
|
|
|
out.r = ((rgba & 0x000000ff));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.r = ((rgba & 0xff000000) >>> 24);
|
|
|
|
out.g = ((rgba & 0x00ff0000) >>> 16);
|
|
|
|
out.b = ((rgba & 0x0000ff00) >>> 8);
|
|
|
|
out.a = ((rgba & 0x000000ff));
|
|
|
|
}
|
|
|
|
|
|
|
|
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility to convert an integer in 0xRRGGBBAA format to a color object.
|
|
|
|
* This does not rely on endianness.
|
|
|
|
*
|
|
|
|
* @author Matt DesLauriers (@mattdesl)
|
|
|
|
* @method Phaser.BitmapData#fromRGBA
|
|
|
|
* @param {number} rgba - An RGBA hex
|
|
|
|
* @param {object} [out] - The object to use, optional.
|
|
|
|
* @return {object} A color object.
|
|
|
|
*/
|
|
|
|
fromRGBA: function (rgba, out) {
|
|
|
|
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
out = { r: 0, g: 0, b: 0, a: 0, rgba: '' };
|
|
|
|
}
|
|
|
|
|
|
|
|
out.r = ((rgba & 0xff000000) >>> 24);
|
|
|
|
out.g = ((rgba & 0x00ff0000) >>> 16);
|
|
|
|
out.b = ((rgba & 0x0000ff00) >>> 8);
|
|
|
|
out.a = ((rgba & 0x000000ff));
|
|
|
|
|
|
|
|
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility to convert RGBA components to a 32 bit integer in RRGGBBAA format.
|
|
|
|
*
|
|
|
|
* @author Matt DesLauriers (@mattdesl)
|
|
|
|
* @method Phaser.BitmapData#toRGBA
|
|
|
|
* @param {number} r - The r color component (0 - 255)
|
|
|
|
* @param {number} g - The g color component (0 - 255)
|
|
|
|
* @param {number} b - The b color component (0 - 255)
|
|
|
|
* @param {number} a - The a color component (0 - 255)
|
|
|
|
* @return {number} A RGBA-packed 32 bit integer
|
|
|
|
*/
|
|
|
|
toRGBA: function (r, g, b, a) {
|
|
|
|
|
|
|
|
return (r << 24) | (g << 16) | (b << 8) | a;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility function to create a lightweight 'color' object with the default components.
|
|
|
|
* Any components that are not specified will default to zero.
|
|
|
|
*
|
|
|
|
* This is useful when you want to use a shared color object for the getPixel and getPixelAt methods.
|
|
|
|
*
|
|
|
|
* @author Matt DesLauriers (@mattdesl)
|
|
|
|
* @method Phaser.BitmapData#createColor
|
|
|
|
* @param {number} [r=0] - The r color component (0 - 255)
|
|
|
|
* @param {number} [g=0] - The g color component (0 - 255)
|
|
|
|
* @param {number} [b=0] - The b color component (0 - 255)
|
|
|
|
* @param {number} [a=0] - The a color component (0 - 255)
|
|
|
|
* @return {object} The resulting color object, with r, g, b, a properties
|
|
|
|
*/
|
|
|
|
createColor: function (r, g, b, a) {
|
|
|
|
|
|
|
|
var out = { r: r||0, g: g||0, b: b||0, a: a||0 };
|
|
|
|
|
|
|
|
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
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-02-21 15:09:04 +00:00
|
|
|
* @param {HTMLImage|string} source - The Image to draw. If you give a key 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.
|
|
|
|
* @param {number} destX - The destination x coordinate to copy the image to.
|
|
|
|
* @param {number} destY - The destination y coordinate to copy the image to.
|
|
|
|
*/
|
2014-02-14 06:04:29 +00:00
|
|
|
copyPixels: function (source, area, destX, destY) {
|
|
|
|
|
2014-02-21 15:09:04 +00:00
|
|
|
if (typeof source === 'string')
|
|
|
|
{
|
|
|
|
source = this.game.cache.getImage(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source)
|
|
|
|
{
|
|
|
|
this.context.drawImage(source, area.x, area.y, area.width, area.height, destX, destY, area.width, area.height);
|
|
|
|
}
|
2014-02-14 06:04:29 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-21 14:50:18 +00:00
|
|
|
/**
|
2014-02-21 15:09:04 +00:00
|
|
|
* Draws the given image to this BitmapData at the coordinates specified. If you need to only draw a part of the image use BitmapData.copyPixels instead.
|
2014-02-21 14:50:18 +00:00
|
|
|
*
|
2014-04-23 22:35:36 +00:00
|
|
|
* @method Phaser.BitmapData#draw
|
|
|
|
* @param {HTMLImage|string} source - The Image to draw. If you give a string it will try and find the Image in the Game.Cache.
|
|
|
|
* @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-02-21 15:09:04 +00:00
|
|
|
*/
|
2014-04-23 22:35:36 +00:00
|
|
|
draw: function (source, x, y) {
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source)
|
|
|
|
{
|
2014-04-23 22:35:36 +00:00
|
|
|
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-04-23 22:35:36 +00:00
|
|
|
/**
|
|
|
|
* Draws the given image to this BitmapData at the coordinates specified. If you need to only draw a part of the image use BitmapData.copyPixels instead.
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#drawSprite
|
|
|
|
* @param {Phaser.Sprite|Phaser.Image} sprite - The Sprite to draw. Must have a loaded texture and frame.
|
|
|
|
* @param {number} [x=0] - The x coordinate to draw the Sprite to.
|
|
|
|
* @param {number} [y=0] - The y coordinate to draw the Sprite to.
|
|
|
|
*/
|
|
|
|
drawSprite: function (sprite, x, y) {
|
|
|
|
|
|
|
|
if (typeof x === 'undefined') { x = 0; }
|
|
|
|
if (typeof y === 'undefined') { y = 0; }
|
|
|
|
|
|
|
|
var frame = sprite.texture.frame;
|
|
|
|
|
|
|
|
this.context.drawImage(sprite.texture.baseTexture.source, frame.x, frame.y, frame.width, frame.height, x, y, frame.width, frame.height);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
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-02-21 15:09:04 +00:00
|
|
|
* @param {HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
|
|
|
|
* @param {HTMLImage|string} 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.
|
2014-02-21 14:50:18 +00:00
|
|
|
*/
|
2014-02-21 15:09:04 +00:00
|
|
|
alphaMask: function (source, mask) {
|
2014-02-21 14:50:18 +00:00
|
|
|
|
|
|
|
var temp = this.context.globalCompositeOperation;
|
|
|
|
|
2014-02-21 15:09:04 +00:00
|
|
|
if (typeof mask === 'string')
|
|
|
|
{
|
|
|
|
mask = this.game.cache.getImage(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask)
|
|
|
|
{
|
|
|
|
this.context.drawImage(mask, 0, 0);
|
|
|
|
}
|
|
|
|
|
2014-02-21 14:50:18 +00:00
|
|
|
this.context.globalCompositeOperation = 'source-atop';
|
2014-02-21 15:09:04 +00:00
|
|
|
|
|
|
|
if (typeof source === 'string')
|
|
|
|
{
|
|
|
|
source = this.game.cache.getImage(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source)
|
|
|
|
{
|
|
|
|
this.context.drawImage(source, 0, 0);
|
|
|
|
}
|
2014-02-21 14:50:18 +00:00
|
|
|
|
|
|
|
this.context.globalCompositeOperation = temp;
|
|
|
|
|
2014-04-20 20:17:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @method Phaser.BitmapData#extractMask
|
|
|
|
* @param {HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
|
|
|
|
* @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.
|
|
|
|
* @return {HTMLImage}
|
|
|
|
*/
|
|
|
|
extractMask: function (source, color, alpha) {
|
|
|
|
|
|
|
|
if (typeof alpha === 'undefined') { alpha = 255; }
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-11-13 20:57:09 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.BitmapData.prototype.constructor = Phaser.BitmapData;
|