phaser-ce
Version:
Phaser CE (Community Edition) is a fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.
1,292 lines (1,113 loc) • 94.3 kB
JavaScript
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* 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 for one or many Images / Sprites.
* So if you need to dynamically create a Sprite texture then they are a good choice.
*
* Important note: Every BitmapData creates its own Canvas element. Because BitmapData's are now Game Objects themselves, and don't
* live on the display list, they are NOT automatically cleared when you change State. Therefore you _must_ call BitmapData.destroy
* in your State's shutdown method if you wish to free-up the resources the BitmapData used, it will not happen for you.
*
* @class Phaser.BitmapData
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {string} key - Internal Phaser reference key for the BitmapData.
* @param {number} [width=256] - The width of the BitmapData in pixels. If undefined or zero it's set to a default value.
* @param {number} [height=256] - The height of the BitmapData in pixels. If undefined or zero it's set to a default value.
* @param {boolean} [skipPool=false] - When this BitmapData generates its internal canvas to use for rendering, it will get the canvas from the CanvasPool if false, or create its own if true.
*/
Phaser.BitmapData = function (game, key, width, height, skipPool)
{
if (width === undefined || width === 0) { width = 256; }
if (height === undefined || height === 0) { height = 256; }
if (skipPool === undefined) { skipPool = false; }
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
/**
* @property {string} key - The key of the BitmapData in the Cache, if stored there.
*/
this.key = key;
/**
* @property {number} width - The width of the BitmapData in pixels.
*/
this.width = width;
/**
* @property {number} height - The height of the BitmapData in pixels.
*/
this.height = height;
/**
* @property {HTMLCanvasElement} canvas - The canvas to which this BitmapData draws.
* @default
*/
this.canvas = Phaser.Canvas.create(this, width, height, null, skipPool);
/**
* @property {CanvasRenderingContext2D} context - The 2d context of the canvas.
* @default
*/
this.context = this.canvas.getContext('2d', { alpha: true });
/**
* @property {CanvasRenderingContext2D} ctx - A reference to BitmapData.context.
*/
this.ctx = this.context;
/**
* @property {string} smoothProperty - The context property needed for smoothing this Canvas.
*/
this.smoothProperty = (game.renderType === Phaser.CANVAS) ? game.renderer.renderSession.smoothProperty : Phaser.Canvas.getSmoothingPrefix(this.context);
/**
* @property {ImageData} imageData - The context image data.
* Please note that a call to BitmapData.draw() or BitmapData.copy() does not update immediately this property for performance reason. Use BitmapData.update() to do so.
* This property is updated automatically after the first game loop, according to the dirty flag property.
*/
this.imageData = this.context.getImageData(0, 0, width, height);
/**
* A Uint8ClampedArray view into BitmapData.buffer.
* Note that this is unavailable in some browsers (such as Epic Browser due to its security restrictions)
* @property {Uint8ClampedArray} data
*/
this.data = null;
if (this.imageData)
{
this.data = this.imageData.data;
}
/**
* @property {Uint32Array} pixels - An Uint32Array view into BitmapData.buffer.
*/
this.pixels = null;
/**
* @property {ArrayBuffer} buffer - An ArrayBuffer the same size as the context ImageData.
*/
if (this.data)
{
if (this.imageData.data.buffer)
{
this.buffer = this.imageData.data.buffer;
this.pixels = new Uint32Array(this.buffer);
}
else
if (window.ArrayBuffer)
{
this.buffer = new ArrayBuffer(this.imageData.data.length);
this.pixels = new Uint32Array(this.buffer);
}
else
{
this.pixels = this.imageData.data;
}
}
/**
* @property {PIXI.BaseTexture} baseTexture - The PIXI.BaseTexture.
* @default
*/
this.baseTexture = new PIXI.BaseTexture(this.canvas, null, this.game.resolution);
/**
* @property {PIXI.Texture} texture - The PIXI.Texture.
* @default
*/
this.texture = new PIXI.Texture(this.baseTexture);
/**
* @property {Phaser.FrameData} frameData - The FrameData container this BitmapData uses for rendering.
*/
this.frameData = new Phaser.FrameData();
/**
* @property {Phaser.Frame} textureFrame - The Frame this BitmapData uses for rendering.
* @default
*/
this.textureFrame = this.frameData.addFrame(new Phaser.Frame(0, 0, 0, width, height, 'bitmapData'));
this.texture.frame = this.textureFrame;
/**
* @property {number} type - The const type of this object.
* @default
*/
this.type = Phaser.BITMAPDATA;
/**
* @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;
/**
* @property {boolean} dirty - If dirty this BitmapData will be re-rendered.
*/
this.dirty = false;
// Aliases
this.cls = this.clear;
/**
* @property {number} _image - Internal cache var.
* @private
*/
this._image = null;
/**
* @property {Phaser.Point} _pos - Internal cache var.
* @private
*/
this._pos = new Phaser.Point();
/**
* @property {Phaser.Point} _size - Internal cache var.
* @private
*/
this._size = new Phaser.Point();
/**
* @property {Phaser.Point} _scale - Internal cache var.
* @private
*/
this._scale = new Phaser.Point();
/**
* @property {number} _rotate - Internal cache var.
* @private
*/
this._rotate = 0;
/**
* @property {object} _alpha - Internal cache var.
* @private
*/
this._alpha = { prev: 1, current: 1 };
/**
* @property {Phaser.Point} _anchor - Internal cache var.
* @private
*/
this._anchor = new Phaser.Point();
/**
* @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;
/**
* @property {Phaser.Circle} _circle - Internal cache var.
* @private
*/
this._circle = new Phaser.Circle();
/**
* @property {HTMLCanvasElement} _swapCanvas - A swap canvas. Used by moveH and moveV, created in those methods.
* @private
*/
this._swapCanvas = undefined;
};
Phaser.BitmapData.prototype = {
/**
* Shifts the contents of this BitmapData by the distances given.
*
* The image will wrap-around the edges on all sides if the wrap argument is true (the default).
*
* @method Phaser.BitmapData#move
* @param {integer} x - The amount of pixels to horizontally shift the canvas by. Use a negative value to shift to the left, positive to the right.
* @param {integer} y - The amount of pixels to vertically shift the canvas by. Use a negative value to shift up, positive to shift down.
* @param {boolean} [wrap=true] - Wrap the content of the BitmapData.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
move: function (x, y, wrap)
{
if (x !== 0)
{
this.moveH(x, wrap);
}
if (y !== 0)
{
this.moveV(y, wrap);
}
return this;
},
/**
* Shifts the contents of this BitmapData horizontally.
*
* The image will wrap-around the sides if the wrap argument is true (the default).
*
* @method Phaser.BitmapData#moveH
* @param {integer} distance - The amount of pixels to horizontally shift the canvas by. Use a negative value to shift to the left, positive to the right.
* @param {boolean} [wrap=true] - Wrap the content of the BitmapData.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
moveH: function (distance, wrap)
{
if (wrap === undefined) { wrap = true; }
if (this._swapCanvas === undefined)
{
this._swapCanvas = Phaser.CanvasPool.create(this, this.width, this.height);
}
var c = this._swapCanvas;
var ctx = c.getContext('2d');
var h = this.height;
var src = this.canvas;
ctx.clearRect(0, 0, this.width, this.height);
if (distance < 0)
{
distance = Math.abs(distance);
// Moving to the left
var w = this.width - distance;
// Left-hand chunk
if (wrap)
{
ctx.drawImage(src, 0, 0, distance, h, w, 0, distance, h);
}
// Rest of the image
ctx.drawImage(src, distance, 0, w, h, 0, 0, w, h);
}
else
{
// Moving to the right
var w = this.width - distance;
// Right-hand chunk
if (wrap)
{
ctx.drawImage(src, w, 0, distance, h, 0, 0, distance, h);
}
// Rest of the image
ctx.drawImage(src, 0, 0, w, h, distance, 0, w, h);
}
this.clear();
return this.copy(this._swapCanvas);
},
/**
* Shifts the contents of this BitmapData vertically.
*
* The image will wrap-around the sides if the wrap argument is true (the default).
*
* @method Phaser.BitmapData#moveV
* @param {integer} distance - The amount of pixels to vertically shift the canvas by. Use a negative value to shift up, positive to shift down.
* @param {boolean} [wrap=true] - Wrap the content of the BitmapData.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
moveV: function (distance, wrap)
{
if (wrap === undefined) { wrap = true; }
if (this._swapCanvas === undefined)
{
this._swapCanvas = Phaser.CanvasPool.create(this, this.width, this.height);
}
var c = this._swapCanvas;
var ctx = c.getContext('2d');
var w = this.width;
var src = this.canvas;
ctx.clearRect(0, 0, this.width, this.height);
if (distance < 0)
{
distance = Math.abs(distance);
// Moving up
var h = this.height - distance;
// Top chunk
if (wrap)
{
ctx.drawImage(src, 0, 0, w, distance, 0, h, w, distance);
}
// Rest of the image
ctx.drawImage(src, 0, distance, w, h, 0, 0, w, h);
}
else
{
// Moving down
var h = this.height - distance;
// Bottom chunk
if (wrap)
{
ctx.drawImage(src, 0, h, w, distance, 0, 0, w, distance);
}
// Rest of the image
ctx.drawImage(src, 0, 0, w, h, 0, distance, w, h);
}
this.clear();
return this.copy(this._swapCanvas);
},
/**
* Updates the given objects so that they use this BitmapData as their texture.
* This will replace any texture they will currently have set.
*
* @method Phaser.BitmapData#add
* @param {Phaser.Sprite|Phaser.Sprite[]|Phaser.Image|Phaser.Image[]} object - Either a single Sprite/Image or an Array of Sprites/Images.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
add: function (object)
{
if (Array.isArray(object))
{
for (var i = 0; i < object.length; i++)
{
if (object[i].loadTexture)
{
object[i].loadTexture(this);
}
}
}
else
{
object.loadTexture(this);
}
return this;
},
/**
* 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.
* 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.Text|Phaser.BitmapData|Image|HTMLCanvasElement|string} source - The object that will be used to populate this BitmapData. If you give a string it will try and find the Image in the Game.Cache first.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
load: function (source)
{
if (typeof source === 'string')
{
source = this.game.cache.getImage(source);
}
if (source)
{
this.resize(source.width, source.height);
this.cls();
}
else
{
return;
}
this.draw(source);
this.update();
return this;
},
/**
* Clears the BitmapData context using a clearRect.
*
* @method Phaser.BitmapData#cls
*/
/**
* Clears the BitmapData context using a clearRect.
*
* You can optionally define the area to clear.
* If the arguments are left empty it will clear the entire canvas.
*
* You may need to call BitmapData.update after this in order to clear out the pixel data,
* but Phaser will not do this automatically for you.
*
* @method Phaser.BitmapData#clear
* @param {number} [x=0] - The x coordinate of the top-left of the area to clear.
* @param {number} [y=0] - The y coordinate of the top-left of the area to clear.
* @param {number} [width] - The width of the area to clear. If undefined it will use BitmapData.width.
* @param {number} [height] - The height of the area to clear. If undefined it will use BitmapData.height.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
clear: function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.width; }
if (height === undefined) { height = this.height; }
this.context.clearRect(x, y, width, height);
this.dirty = true;
return this;
},
/**
* 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=1] - The alpha color value, between 0 and 1.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
fill: function (r, g, b, a)
{
if (a === undefined) { a = 1; }
this.context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
this.context.fillRect(0, 0, this.width, this.height);
this.dirty = true;
return this;
},
/**
* Returns a data URI representing the bitmap image.
*
* @method Phaser.BitmapData#getBase64
* @param {string} [type] - Image format.
* @param {number} [encoderOptions] - Image quality, for lossy formats.
* @return {string} - The data URI.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
*/
getBase64: function (type, encoderOptions)
{
return this.canvas.toDataURL(type, encoderOptions);
},
/**
* Returns an HTML image of the bitmap.
*
* The image is loaded asynchronously, not right away.
* Use the callbacks if you need to wait for the loaded image.
*
* @method Phaser.BitmapData#getImage
* @param {string} [type] - Image format.
* @param {number} [encoderOptions] - Image quality, for lossy formats.
* @param {function} [onLoadCallback] - A function to call when the image loads.
* @param {function} [onErrorCallback] - A function to call when the image fails to load.
* @return {HTMLImageElement} - The image.
*
* @see Phaser.BitmapData#getBase64
*/
getImage: function (type, encoderOptions, onLoadCallback, onErrorCallback)
{
var image = new Image();
if (onLoadCallback) { image.onload = onLoadCallback; }
if (onErrorCallback) { image.onerror = onErrorCallback; }
image.src = this.getBase64(type, encoderOptions);
return image;
},
/**
* Creates a new {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image Image} element by converting this BitmapDatas canvas into a dataURL.
*
* The image is then stored in the {@link Phaser.Cache image Cache} using the key given.
*
* Finally a {@link PIXI.Texture} is created based on the image and returned.
*
* You can apply the texture to a sprite or any other supporting object by using either the
* key or the texture. First call `generateTexture`:
*
* ```javascript
* var texture = bitmapdata.generateTexture('ball');
* ```
*
* Then you can either apply the texture to a sprite:
*
* ```javascript
* game.add.sprite(0, 0, texture);
* ```
*
* or by using the string based key:
*
* ```javascript
* game.add.sprite(0, 0, 'ball');
* ```
*
* Most browsers now load the image data asynchronously, so you should use a callback:
*
* ```javascript
* bitmapdata.generateTexture('ball', function (texture) {
* game.add.sprite(0, 0, texture);
* // or
* game.add.sprite(0, 0, 'ball');
* });
* ```
*
* If this BitmapData is available during preload, you can use {@link Phaser.Loader#imageFromBitmapData} instead.
*
* @method Phaser.BitmapData#generateTexture
* @param {string} key - The key which will be used to store the image in the Cache.
* @param {function} [callback] - A function to execute once the texture is generated. It will be passed the newly generated texture.
* @param {any} [callbackContext] - The context in which to invoke the callback.
* @return {PIXI.Texture|null} The newly generated texture, or `null` if a callback was passed and the texture isn't available yet.
*/
generateTexture: function (key, callback, callbackContext)
{
var cache = this.game.cache;
var image = new Image();
if (callback)
{
image.onload = function ()
{
var obj = cache.addImage(key, '', image);
var texture = new PIXI.Texture(obj.base);
callback.call(callbackContext || null, texture);
image.onload = null;
};
}
image.src = this.getBase64();
if (!callback)
{
var obj = cache.addImage(key, '', image);
return new PIXI.Texture(obj.base);
}
return null;
},
/**
* Resizes the BitmapData. This changes the size of the underlying canvas and refreshes the buffer.
*
* @method Phaser.BitmapData#resize
* @param {integer} width - The new width of the BitmapData.
* @param {integer} height - The new height of the BitmapData.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
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;
if (this._swapCanvas !== undefined)
{
this._swapCanvas.width = width;
this._swapCanvas.height = height;
}
this.baseTexture.width = width;
this.baseTexture.height = height;
this.textureFrame.width = width;
this.textureFrame.height = height;
this.texture.width = width;
this.texture.height = height;
this.texture.crop.width = width;
this.texture.crop.height = height;
this.update();
this.dirty = true;
}
return this;
},
/**
* This re-creates the BitmapData.imageData from the current context.
* It then re-builds the ArrayBuffer, the data Uint8ClampedArray reference and the pixels Int32Array.
* If not given the dimensions defaults to the full size of the context.
*
* Warning: This is a very expensive operation, so use it sparingly.
*
* @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=1] - The width of the image data area.
* @param {number} [height=1] - The height of the image data area.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
update: function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = Math.max(1, this.width); }
if (height === undefined) { height = Math.max(1, this.height); }
this.imageData = this.context.getImageData(x, y, width, height);
this.data = this.imageData.data;
if (this.imageData.data.buffer)
{
this.buffer = this.imageData.data.buffer;
this.pixels = new Uint32Array(this.buffer);
}
else
if (window.ArrayBuffer)
{
this.buffer = new ArrayBuffer(this.imageData.data.length);
this.pixels = new Uint32Array(this.buffer);
}
else
{
this.pixels = this.imageData.data;
}
return this;
},
/**
* Scans through the area specified in this BitmapData and sends a color object for every pixel to the given callback.
* The callback will be sent a color object with 6 properties: `{ r: number, g: number, b: number, a: number, color: number, rgba: string }`.
* 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.
* The callback will also be sent the pixels x and y coordinates respectively.
* 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.
*
* @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.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
processPixelRGB: function (callback, callbackContext, x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.width; }
if (height === undefined) { height = this.height; }
var w = x + width;
var h = y + height;
var pixel = Phaser.Color.createColor();
var result = { r: 0, g: 0, b: 0, a: 0 };
var dirty = false;
for (var ty = y; ty < h; ty++)
{
for (var tx = x; tx < w; tx++)
{
Phaser.Color.unpackPixel(this.getPixel32(tx, ty), pixel);
result = callback.call(callbackContext, pixel, tx, ty);
if (result !== false && result !== null && result !== undefined)
{
this.setPixel32(tx, ty, result.r, result.g, result.b, result.a, false);
dirty = true;
}
}
}
if (dirty)
{
this.context.putImageData(this.imageData, 0, 0);
this.dirty = true;
}
return this;
},
/**
* 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.
* 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.
* The callback will also be sent the pixels x and y coordinates respectively.
*
* @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.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
processPixel: function (callback, callbackContext, x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.width; }
if (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);
result = callback.call(callbackContext, pixel, tx, ty);
if (result !== pixel)
{
this.pixels[ty * this.width + tx] = result;
dirty = true;
}
}
}
if (dirty)
{
this.context.putImageData(this.imageData, 0, 0);
this.dirty = true;
}
return this;
},
/**
* 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.
*
* @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.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
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;
var source = Phaser.Color.packPixel(r1, g1, b1, a1);
if (region !== undefined && region instanceof Phaser.Rectangle)
{
sx = region.x;
sy = region.y;
w = region.width;
h = region.height;
}
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);
}
}
}
this.context.putImageData(this.imageData, 0, 0);
this.dirty = true;
return this;
},
/**
* Sets the hue, saturation and lightness values on every pixel in the given region, or the whole BitmapData if no region was specified.
*
* @method Phaser.BitmapData#setHSL
* @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.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
setHSL: function (h, s, l, region)
{
var bHaveH = h || h === 0;
var bHaveS = s || s === 0;
var bHaveL = l || l === 0;
if (!bHaveH && !bHaveS && !bHaveL)
{
return;
}
if (region === undefined)
{
region = new Phaser.Rectangle(0, 0, this.width, this.height);
}
var pixel = Phaser.Color.createColor();
for (var y = region.y; y < region.bottom; y++)
{
for (var x = region.x; x < region.right; x++)
{
Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel, true);
if (bHaveH)
{
pixel.h = h;
}
if (bHaveS)
{
pixel.s = s;
}
if (bHaveL)
{
pixel.l = l;
}
Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel);
this.setPixel32(x, y, pixel.r, pixel.g, pixel.b, pixel.a, false);
}
}
this.context.putImageData(this.imageData, 0, 0);
this.dirty = true;
return this;
},
/**
* 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. Within [-1, 1].
* @param {number} [s=null] - The amount to shift the saturation by. Within [-1, 1].
* @param {number} [l=null] - The amount to shift the lightness by. Within [-1, 1].
* @param {Phaser.Rectangle} [region] - The area to perform the operation on. If not given it will run over the whole BitmapData.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
shiftHSL: function (h, s, l, region)
{
if (h === undefined || h === null) { h = false; }
if (s === undefined || s === null) { s = false; }
if (l === undefined || l === null) { l = false; }
if (!h && !s && !l)
{
return;
}
if (region === undefined)
{
region = new Phaser.Rectangle(0, 0, this.width, this.height);
}
var pixel = Phaser.Color.createColor();
for (var y = region.y; y < region.bottom; y++)
{
for (var x = region.x; x < region.right; x++)
{
Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel, true);
if (h)
{
pixel.h = this.game.math.wrap(pixel.h + h, 0, 1);
}
if (s)
{
pixel.s = this.game.math.clamp(pixel.s + s, 0, 1);
}
if (l)
{
pixel.l = this.game.math.clamp(pixel.l + l, 0, 1);
}
Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel);
this.setPixel32(x, y, pixel.r, pixel.g, pixel.b, pixel.a, false);
}
}
this.context.putImageData(this.imageData, 0, 0);
this.dirty = true;
return this;
},
/**
* Sets the color of the given pixel to the specified red, green, blue and alpha values.
*
* @method Phaser.BitmapData#setPixel32
* @param {integer} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @param {integer} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @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.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
setPixel32: function (x, y, red, green, blue, alpha, immediate)
{
if (immediate === undefined) { immediate = true; }
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
{
if (Phaser.Device.LITTLE_ENDIAN)
{
this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
}
else
{
this.pixels[y * this.width + x] = (red << 24) | (green << 16) | (blue << 8) | alpha;
}
if (immediate)
{
this.context.putImageData(this.imageData, 0, 0);
this.dirty = true;
}
}
return this;
},
/**
* Sets the color of the given pixel to the specified red, green and blue values.
*
* @method Phaser.BitmapData#setPixel
* @param {integer} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @param {integer} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @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 {boolean} [immediate=true] - If `true` the context.putImageData will be called and the dirty flag set.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
setPixel: function (x, y, red, green, blue, immediate)
{
return this.setPixel32(x, y, red, green, blue, 255, immediate);
},
/**
* Get the color of a specific pixel in the context into a color object.
* 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.
*
* @method Phaser.BitmapData#getPixel
* @param {integer} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @param {integer} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @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.
*/
getPixel: function (x, y, out)
{
if (!out)
{
out = Phaser.Color.createColor();
}
var index = ~~(x + (y * this.width));
index *= 4;
out.r = this.data[index];
out.g = this.data[++index];
out.b = this.data[++index];
out.a = this.data[++index];
return out;
},
/**
* Get the color of a specific pixel including its alpha value.
* 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.
* Note that on little-endian systems the format is 0xAABBGGRR and on big-endian the format is 0xRRGGBBAA.
*
* @method Phaser.BitmapData#getPixel32
* @param {integer} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @param {integer} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @return {number} A native color value integer (format: 0xAARRGGBB)
*/
getPixel32: function (x, y)
{
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
{
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.
* 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.
*
* @method Phaser.BitmapData#getPixelRGB
* @param {integer} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @param {integer} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this BitmapData and be an integer, not a float.
* @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.
*/
getPixelRGB: function (x, y, out, hsl, hsv)
{
return Phaser.Color.unpackPixel(this.getPixel32(x, y), out, hsl, hsv);
},
/**
* Gets all the pixels from the region specified by the given Rectangle object.
*
* @method Phaser.BitmapData#getPixels
* @param {Phaser.Rectangle} rect - The Rectangle region to get.
* @return {ImageData} Returns a ImageData object containing a Uint8ClampedArray data property.
*/
getPixels: function (rect)
{
return this.context.getImageData(rect.x, rect.y, rect.width, rect.height);
},
/**
* Scans the BitmapData, pixel by pixel, until it encounters a pixel that isn't transparent (i.e. has an alpha value > 0).
* It then stops scanning and returns an object containing the color of the pixel in r, g and b properties and the location in the x and y properties.
*
* The direction parameter controls from which direction it should start the scan:
*
* 0 = top to bottom
* 1 = bottom to top
* 2 = left to right
* 3 = right to left
*
* @method Phaser.BitmapData#getFirstPixel
* @param {number} [direction=0] - The direction in which to scan for the first pixel. 0 = top to bottom, 1 = bottom to top, 2 = left to right and 3 = right to left.
* @return {object} Returns an object containing the color of the pixel in the `r`, `g` and `b` properties and the location in the `x` and `y` properties.
*/
getFirstPixel: function (direction)
{
if (direction === undefined) { direction = 0; }
var pixel = Phaser.Color.createColor();
var x = 0;
var y = 0;
var v = 1;
var scan = false;
if (direction === 1)
{
v = -1;
y = this.height;
}
else if (direction === 3)
{
v = -1;
x = this.width;
}
do
{
Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel);
if (direction === 0 || direction === 1)
{
// Top to Bottom / Bottom to Top
x++;
if (x === this.width)
{
x = 0;
y += v;
if (y >= this.height || y <= 0)
{
scan = true;
}
}
}
else if (direction === 2 || direction === 3)
{
// Left to Right / Right to Left
y++;
if (y === this.height)
{
y = 0;
x += v;
if (x >= this.width || x <= 0)
{
scan = true;
}
}
}
}
while (pixel.a === 0 && !scan);
pixel.x = x;
pixel.y = y;
return pixel;
},
/**
* Scans the BitmapData and calculates the bounds. This is a rectangle that defines the extent of all non-transparent pixels.
* The rectangle returned will extend from the top-left of the image to the bottom-right, excluding transparent pixels.
*
* @method Phaser.BitmapData#getBounds
* @param {Phaser.Rectangle} [rect] - If provided this Rectangle object will be populated with the bounds, otherwise a new object will be created.
* @return {Phaser.Rectangle} A Rectangle whose dimensions encompass the full extent of non-transparent pixels in this BitmapData.
*/
getBounds: function (rect)
{
if (rect === undefined) { rect = new Phaser.Rectangle(); }
rect.x = this.getFirstPixel(2).x;
// If we hit this, there's no point scanning any more, the image is empty
if (rect.x === this.width)
{
return rect.setTo(0, 0, 0, 0);
}
rect.y = this.getFirstPixel(0).y;
rect.width = (this.getFirstPixel(3).x - rect.x) + 1;
rect.height = (this.getFirstPixel(1).y - rect.y) + 1;
return rect;
},
/**
* 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.
* @param {number} [anchorX=0] - Set the x anchor point of the Image. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
* @param {number} [anchorY=0] - Set the y anchor point of the Image. 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 Image. 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 Image. A value of 1 means no scaling. 2 would be twice the size, and so on.
* @return {Phaser.Image} The newly added Image object.
*/
addToWorld: function (x, y, anchorX, anchorY, scaleX, scaleY)
{
scaleX = scaleX || 1;
scaleY = scaleY || 1;
var image = this.game.add.image(x, y, this);
image.anchor.set(anchorX, anchorY);
image.scale.set(scaleX, scaleY);
return image;
},
/**
* Copies a rectangular area from the source object to this BitmapData. If you give `null` as the source it will copy from itself.
*
* You can optionally resize, translate, rotate, scale, alpha or blend as it's drawn.
*
* All rotation, scaling and drawing takes place around the regions center point by default, but can be changed with the anchor parameters.
*
* 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 `copyRect` and `draw` to avoid having to remember them all.
*
* You may prefer to use `copyTransform` if you're simply trying to draw a Sprite to this BitmapData,
* and don't wish to translate, scale or rotate it from its original values.
*
* @method Phaser.BitmapData#copy
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapData|Phaser.RenderTexture|Image|HTMLCanvasElement|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 expensive 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. If `null` and `source` is a Display Object, it will default to `source.x`.
* @param {number} [ty] - The y coordinate to translate to before drawing. If not specified it will default to the `y` parameter. If `null` and `source` is a Display Object, it will default to `source.y`.
* @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.
* @param {number} [rotate=0] - The angle in radians 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] - The anchor point around which 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] - The anchor point around which 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 drawing. A value between 0 (fully transparent) and 1, opaque.
* @param {string} [blendMode=null] - The composite blend mode that will be used when drawing. The default is no blend mode at all. This is a Canvas globalCompositeOperation value such as 'lighter' or 'xor'.
* @param {boolean} [roundPx=false] - Should the x and y values be rounded to integers before drawing? This prevents anti-aliasing in some instances.
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
*/
copy: function (s