phaser/src/gameobjects/BitmapData.js

272 lines
8 KiB
JavaScript
Raw Normal View History

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
*
* @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.
* @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
*/
Phaser.BitmapData = function (game, key, width, height) {
2013-11-13 20:57:09 +00:00
if (typeof width === 'undefined') { width = 100; }
if (typeof height === 'undefined') { height = 100; }
2013-11-13 20:57:09 +00:00
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
2013-11-13 20:57:09 +00:00
/**
* @property {string} key - The key of the BitmapData in the Cache, if stored there.
*/
this.key = key;
2013-11-13 20:57:09 +00:00
/**
* @property {number} width - The width of the BitmapData in pixels.
2013-11-13 20:57:09 +00:00
*/
this.width = width;
/**
* @property {number} height - The height of the BitmapData in pixels.
2013-11-13 20:57:09 +00:00
*/
this.height = height;
2013-11-13 20:57:09 +00:00
/**
* @property {HTMLCanvasElement} canvas - The canvas to which this BitmapData draws.
* @default
*/
2013-11-13 20:57:09 +00:00
this.canvas = Phaser.Canvas.create(width, height);
/**
* @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
/**
* @property {CanvasRenderingContext2D} ctx - A reference to BitmapData.context.
*/
this.ctx = this.context;
/**
* @property {array} imageData - The canvas image data.
*/
this.imageData = this.context.getImageData(0, 0, width, height);
/**
* @property {UInt8Clamped} pixels - A reference to the context imageData buffer.
*/
if (this.imageData.data.buffer)
{
this.pixels = this.imageData.data.buffer;
}
else
{
this.pixels = this.imageData.data;
}
/**
* @property {PIXI.BaseTexture} baseTexture - The PIXI.BaseTexture.
* @default
*/
2013-11-13 20:57:09 +00:00
this.baseTexture = new PIXI.BaseTexture(this.canvas);
/**
* @property {PIXI.Texture} texture - The PIXI.Texture.
* @default
*/
2013-11-13 20:57:09 +00:00
this.texture = new PIXI.Texture(this.baseTexture);
/**
* @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());
/**
* @property {number} type - The const type of this object.
2013-11-13 20:57:09 +00:00
* @default
*/
2013-11-13 20:57:09 +00:00
this.type = Phaser.BITMAPDATA;
this._dirty = false;
2013-11-17 00:55:28 +00:00
2013-11-13 20:57:09 +00:00
}
Phaser.BitmapData.prototype = {
/**
* Updates the given objects so that they use this BitmapData as their texture.
*
2013-11-28 15:57:09 +00:00
* @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.
*/
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);
}
},
/**
* Clears the BitmapData.
2013-11-28 15:57:09 +00:00
* @method Phaser.BitmapData#clear
*/
clear: function () {
this.context.clearRect(0, 0, this.width, this.height);
this._dirty = true;
},
refreshBuffer: function () {
this.imageData = this.context.getImageData(0, 0, this.width, this.height);
this.pixels = new Int32Array(this.imageData.data.buffer);
// this.data8 = new Uint8ClampedArray(this.imageData.buffer);
// this.data32 = new Uint32Array(this.imageData.buffer);
},
/**
* 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
* @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) {
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
{
this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
2013-11-17 12:31:57 +00:00
/*
if (this.isLittleEndian)
{
this.data32[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
}
else
{
this.data32[y * this.width + x] = (red << 24) | (green << 16) | (blue << 8) | alpha;
}
*/
2013-11-15 20:40:39 +00:00
// this.imageData.data.set(this.data8);
2013-11-15 20:40:39 +00:00
this.context.putImageData(this.imageData, 0, 0);
2013-11-17 00:55:28 +00:00
this._dirty = true;
}
2013-11-15 20:40:39 +00:00
},
/**
* Sets the color of the given pixel to the specified red, green and blue values.
2013-11-28 15:57:09 +00:00
* @method Phaser.BitmapData#setPixel
* @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) {
this.setPixel32(x, y, red, green, blue, 255);
2013-11-15 20:40:39 +00:00
},
/**
* Get a color of a specific pixel.
* @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)
*/
2013-11-15 20:40:39 +00:00
getPixel: function (x, y) {
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
{
return this.data32[y * this.width + x];
}
2013-11-15 20:40:39 +00:00
},
/**
* Get a color of a specific pixel (including alpha value).
* @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) {
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
{
return this.data32[y * this.width + x];
}
2013-11-15 20:40:39 +00:00
},
/**
* Get pixels in array in a specific Rectangle.
* @param rect {Rectangle} The specific Rectangle.
* @return {array} CanvasPixelArray.
*/
2013-11-15 20:40:39 +00:00
getPixels: function (rect) {
return this.context.getImageData(rect.x, rect.y, rect.width, rect.height);
},
/**
* 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.
2013-11-28 15:57:09 +00:00
* @method Phaser.BitmapData#render
*/
render: function () {
if (this._dirty)
{
// Only needed if running in WebGL, otherwise this array will never get cleared down
if (this.game.renderType == Phaser.WEBGL)
{
PIXI.texturesToUpdate.push(this.baseTexture);
}
this._dirty = false;
}
}
2013-11-13 20:57:09 +00:00
};
Phaser.BitmapData.prototype.constructor = Phaser.BitmapData;