phaser/src/textures/CanvasTexture.js

630 lines
22 KiB
JavaScript
Raw Normal View History

2018-04-23 17:37:44 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-04-23 17:37:44 +00:00
*/
var Class = require('../utils/Class');
2018-12-11 02:37:00 +00:00
var Clamp = require('../math/Clamp');
var Color = require('../display/color/Color');
var CONST = require('../const');
var IsSizePowerOfTwo = require('../math/pow2/IsSizePowerOfTwo');
2018-04-23 17:37:44 +00:00
var Texture = require('./Texture');
/**
* @classdesc
* A Canvas Texture is a special kind of Texture that is backed by an HTML Canvas Element as its source.
*
* You can use the properties of this texture to draw to the canvas element directly, using all of the standard
* canvas operations available in the browser. Any Game Object can be given this texture and will render with it.
*
* Note: When running under WebGL the Canvas Texture needs to re-generate its base WebGLTexture and reupload it to
* the GPU every time you modify it, otherwise the changes you make to this texture will not be visible. To do this
* you should call `CanvasTexture.refresh()` once you are finished with your changes to the canvas. Try and keep
* this to a minimum, especially on large canvas sizes, or you may inadvertently thrash the GPU by constantly uploading
* texture data to it. This restriction does not apply if using the Canvas Renderer.
2020-11-23 10:32:00 +00:00
*
* It starts with only one frame that covers the whole of the canvas. You can add further frames, that specify
* sections of the canvas using the `add` method.
2020-11-23 10:32:00 +00:00
*
* Should you need to resize the canvas use the `setSize` method so that it accurately updates all of the underlying
* texture data as well. Forgetting to do this (i.e. by changing the canvas size directly from your code) could cause
* graphical errors.
2018-04-23 17:37:44 +00:00
*
* @class CanvasTexture
* @extends Phaser.Textures.Texture
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Textures
2018-04-23 17:37:44 +00:00
* @constructor
2018-05-04 17:51:02 +00:00
* @since 3.7.0
2018-04-23 17:37:44 +00:00
*
2019-07-13 09:50:06 +00:00
* @param {Phaser.Textures.TextureManager} manager - A reference to the Texture Manager this Texture belongs to.
2018-04-23 17:37:44 +00:00
* @param {string} key - The unique string-based key of this Texture.
* @param {HTMLCanvasElement} source - The canvas element that is used as the base of this texture.
2020-11-23 10:22:13 +00:00
* @param {number} width - The width of the canvas.
* @param {number} height - The height of the canvas.
2018-04-23 17:37:44 +00:00
*/
var CanvasTexture = new Class({
Extends: Texture,
initialize:
function CanvasTexture (manager, key, source, width, height)
{
Texture.call(this, manager, key, source, width, height);
this.add('__BASE', 0, 0, 0, width, height);
/**
* A reference to the Texture Source of this Canvas.
*
2018-09-27 13:16:09 +00:00
* @name Phaser.Textures.CanvasTexture#_source
* @type {Phaser.Textures.TextureSource}
* @private
2018-05-04 17:51:02 +00:00
* @since 3.7.0
*/
this._source = this.frames['__BASE'].source;
/**
* The source Canvas Element.
*
* @name Phaser.Textures.CanvasTexture#canvas
2018-10-09 12:40:00 +00:00
* @readonly
* @type {HTMLCanvasElement}
2018-05-04 17:51:02 +00:00
* @since 3.7.0
*/
this.canvas = this._source.image;
/**
* The 2D Canvas Rendering Context.
*
2018-09-27 13:16:09 +00:00
* @name Phaser.Textures.CanvasTexture#context
2018-10-09 12:40:00 +00:00
* @readonly
* @type {CanvasRenderingContext2D}
2018-05-04 17:51:02 +00:00
* @since 3.7.0
*/
2018-04-23 17:37:44 +00:00
this.context = this.canvas.getContext('2d');
/**
* The width of the Canvas.
2018-09-10 19:44:19 +00:00
* This property is read-only, if you wish to change it use the `setSize` method.
*
* @name Phaser.Textures.CanvasTexture#width
2018-10-09 12:40:00 +00:00
* @readonly
2020-11-23 10:22:13 +00:00
* @type {number}
2018-05-04 17:51:02 +00:00
* @since 3.7.0
*/
2018-04-23 17:37:44 +00:00
this.width = width;
/**
* The height of the Canvas.
2018-09-10 19:44:19 +00:00
* This property is read-only, if you wish to change it use the `setSize` method.
*
* @name Phaser.Textures.CanvasTexture#height
2018-10-09 12:40:00 +00:00
* @readonly
2020-11-23 10:22:13 +00:00
* @type {number}
2018-05-04 17:51:02 +00:00
* @since 3.7.0
*/
2018-04-23 17:37:44 +00:00
this.height = height;
/**
* The context image data.
* Use the `update` method to populate this when the canvas changes.
*
* @name Phaser.Textures.CanvasTexture#imageData
* @type {ImageData}
* @since 3.13.0
*/
this.imageData = this.context.getImageData(0, 0, width, height);
/**
* A Uint8ClampedArray view into the `buffer`.
* Use the `update` method to populate this when the canvas changes.
* Note that this is unavailable in some browsers, such as Epic Browser, due to their security restrictions.
*
2018-09-10 19:44:19 +00:00
* @name Phaser.Textures.CanvasTexture#data
* @type {Uint8ClampedArray}
* @since 3.13.0
*/
this.data = null;
if (this.imageData)
{
this.data = this.imageData.data;
}
/**
* An Uint32Array view into the `buffer`.
*
* @name Phaser.Textures.CanvasTexture#pixels
* @type {Uint32Array}
* @since 3.13.0
*/
this.pixels = null;
/**
* An ArrayBuffer the same size as the context ImageData.
*
* @name Phaser.Textures.CanvasTexture#buffer
* @type {ArrayBuffer}
* @since 3.13.0
*/
this.buffer;
if (this.data)
{
if (this.imageData.data.buffer)
{
this.buffer = this.imageData.data.buffer;
this.pixels = new Uint32Array(this.buffer);
}
2018-09-10 19:44:19 +00:00
else if (window.ArrayBuffer)
{
this.buffer = new ArrayBuffer(this.imageData.data.length);
this.pixels = new Uint32Array(this.buffer);
}
else
{
this.pixels = this.imageData.data;
}
}
},
/**
* This re-creates the `imageData` from the current context.
* It then re-builds the ArrayBuffer, the `data` Uint8ClampedArray reference and the `pixels` Int32Array.
*
* Warning: This is a very expensive operation, so use it sparingly.
*
* @method Phaser.Textures.CanvasTexture#update
* @since 3.13.0
*
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
2018-09-10 19:44:19 +00:00
*/
update: function ()
{
2018-09-08 00:19:25 +00:00
this.imageData = this.context.getImageData(0, 0, this.width, this.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;
}
if (this.manager.game.config.renderType === CONST.WEBGL)
{
this.refresh();
}
return this;
},
2018-09-10 19:44:19 +00:00
/**
* Draws the given Image or Canvas element to this CanvasTexture, then updates the internal
* ImageData buffer and arrays.
*
* @method Phaser.Textures.CanvasTexture#draw
* @since 3.13.0
2020-11-23 10:32:00 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate to draw the source at.
* @param {number} y - The y coordinate to draw the source at.
2018-09-10 19:44:19 +00:00
* @param {(HTMLImageElement|HTMLCanvasElement)} source - The element to draw to this canvas.
2020-11-23 10:32:00 +00:00
*
2018-09-10 19:44:19 +00:00
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
*/
2018-09-08 00:19:25 +00:00
draw: function (x, y, source)
{
this.context.drawImage(source, x, y);
return this.update();
},
2018-12-08 13:10:55 +00:00
/**
* Draws the given texture frame to this CanvasTexture, then updates the internal
* ImageData buffer and arrays.
*
* @method Phaser.Textures.CanvasTexture#drawFrame
* @since 3.16.0
2020-11-23 10:32:00 +00:00
*
2018-12-08 13:10:55 +00:00
* @param {string} key - The unique string-based key of the Texture.
2020-11-23 10:32:00 +00:00
* @param {(string|number)} [frame] - The string-based name, or integer based index, of the Frame to get from the Texture.
2020-11-23 10:22:13 +00:00
* @param {number} [x=0] - The x coordinate to draw the source at.
* @param {number} [y=0] - The y coordinate to draw the source at.
2020-11-23 10:32:00 +00:00
*
2018-12-08 13:10:55 +00:00
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
*/
drawFrame: function (key, frame, x, y)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
2018-12-08 14:59:07 +00:00
var textureFrame = this.manager.getFrame(key, frame);
2018-12-08 13:10:55 +00:00
2018-12-08 14:59:07 +00:00
if (textureFrame)
2018-12-08 13:10:55 +00:00
{
2018-12-08 14:59:07 +00:00
var cd = textureFrame.canvasData;
2018-12-08 13:10:55 +00:00
2018-12-08 14:59:07 +00:00
var width = textureFrame.cutWidth;
var height = textureFrame.cutHeight;
var res = textureFrame.source.resolution;
2018-12-08 13:10:55 +00:00
2018-12-08 14:59:07 +00:00
this.context.drawImage(
textureFrame.source.image,
cd.x, cd.y,
width,
height,
x, y,
width / res,
height / res
);
2018-12-08 13:10:55 +00:00
return this.update();
}
else
{
return this;
}
},
2018-12-11 23:22:00 +00:00
/**
* Sets a pixel in the CanvasTexture to the given color and alpha values.
*
* This is an expensive operation to run in large quantities, so use sparingly.
*
* @method Phaser.Textures.CanvasTexture#setPixel
* @since 3.16.0
2020-11-23 10:32:00 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} red - The red color value. A number between 0 and 255.
* @param {number} green - The green color value. A number between 0 and 255.
* @param {number} blue - The blue color value. A number between 0 and 255.
* @param {number} [alpha=255] - The alpha value. A number between 0 and 255.
2020-11-23 10:32:00 +00:00
*
2018-12-11 23:22:00 +00:00
* @return {this} This CanvasTexture.
*/
setPixel: function (x, y, red, green, blue, alpha)
{
if (alpha === undefined) { alpha = 255; }
x = Math.abs(Math.floor(x));
y = Math.abs(Math.floor(y));
var index = this.getIndex(x, y);
if (index > -1)
{
var imageData = this.context.getImageData(x, y, 1, 1);
imageData.data[0] = red;
imageData.data[1] = green;
imageData.data[2] = blue;
imageData.data[3] = alpha;
this.context.putImageData(imageData, x, y);
}
return this;
},
/**
2018-12-13 00:30:13 +00:00
* Puts the ImageData into the context of this CanvasTexture at the given coordinates.
*
* @method Phaser.Textures.CanvasTexture#putData
* @since 3.16.0
2020-11-23 10:32:00 +00:00
*
2018-12-13 00:30:13 +00:00
* @param {ImageData} imageData - The ImageData to put at the given location.
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} [dirtyX=0] - Horizontal position (x coordinate) of the top-left corner from which the image data will be extracted.
* @param {number} [dirtyY=0] - Vertical position (x coordinate) of the top-left corner from which the image data will be extracted.
* @param {number} [dirtyWidth] - Width of the rectangle to be painted. Defaults to the width of the image data.
* @param {number} [dirtyHeight] - Height of the rectangle to be painted. Defaults to the height of the image data.
2020-11-23 10:32:00 +00:00
*
2018-12-13 00:30:13 +00:00
* @return {this} This CanvasTexture.
*/
putData: function (imageData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight)
2018-12-13 00:30:13 +00:00
{
if (dirtyX === undefined) { dirtyX = 0; }
if (dirtyY === undefined) { dirtyY = 0; }
if (dirtyWidth === undefined) { dirtyWidth = imageData.width; }
if (dirtyHeight === undefined) { dirtyHeight = imageData.height; }
2018-12-13 00:30:13 +00:00
this.context.putImageData(imageData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight);
2018-12-13 00:30:13 +00:00
return this;
},
/**
* Gets an ImageData region from this CanvasTexture from the position and size specified.
* You can write this back using `CanvasTexture.putData`, or manipulate it.
*
* @method Phaser.Textures.CanvasTexture#getData
* @since 3.16.0
2020-11-23 10:32:00 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate of the top-left of the area to get the ImageData from. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the top-left of the area to get the ImageData from. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} width - The width of the rectangle from which the ImageData will be extracted. Positive values are to the right, and negative to the left.
* @param {number} height - The height of the rectangle from which the ImageData will be extracted. Positive values are down, and negative are up.
2020-11-23 10:32:00 +00:00
*
2018-12-13 00:30:13 +00:00
* @return {ImageData} The ImageData extracted from this CanvasTexture.
*/
getData: function (x, y, width, height)
{
x = Clamp(Math.floor(x), 0, this.width - 1);
y = Clamp(Math.floor(y), 0, this.height - 1);
width = Clamp(width, 1, this.width - x);
height = Clamp(height, 1, this.height - y);
var imageData = this.context.getImageData(x, y, width, height);
return imageData;
},
/**
2018-09-10 19:44:19 +00:00
* Get the color of a specific pixel from this texture and store it in a Color object.
2020-11-23 10:32:00 +00:00
*
2018-09-10 19:44:19 +00:00
* If you have drawn anything to this CanvasTexture since it was created you must call `CanvasTexture.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.Textures.CanvasTexture#getPixel
* @since 3.13.0
2020-11-23 10:32:00 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
2018-12-11 02:37:00 +00:00
* @param {Phaser.Display.Color} [out] - A Color object to store the pixel values in. If not provided a new Color object will be created.
2020-11-23 10:32:00 +00:00
*
2018-09-08 00:19:25 +00:00
* @return {Phaser.Display.Color} 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 = new Color();
}
2018-12-11 02:37:00 +00:00
var index = this.getIndex(x, y);
2018-12-11 02:37:00 +00:00
if (index > -1)
{
var data = this.data;
var r = data[index + 0];
var g = data[index + 1];
var b = data[index + 2];
var a = data[index + 3];
out.setTo(r, g, b, a);
}
return out;
},
/**
* Returns an array containing all of the pixels in the given region.
*
* If the requested region extends outside the bounds of this CanvasTexture,
* the region is truncated to fit.
2020-11-23 10:32:00 +00:00
*
2018-12-11 02:37:00 +00:00
* If you have drawn anything to this CanvasTexture since it was created you must call `CanvasTexture.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.Textures.CanvasTexture#getPixels
* @since 3.16.0
2020-11-23 10:32:00 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} [x=0] - The x coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} [y=0] - The y coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} [width] - The width of the region to get. Must be an integer. Defaults to the canvas width if not given.
* @param {number} [height] - The height of the region to get. Must be an integer. If not given will be set to the `width`.
2020-11-23 10:32:00 +00:00
*
* @return {Phaser.Types.Textures.PixelConfig[][]} A 2d array of Pixel objects.
2018-12-11 02:37:00 +00:00
*/
getPixels: function (x, y, width, height)
{
2019-07-21 15:18:21 +00:00
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.width; }
2018-12-11 02:37:00 +00:00
if (height === undefined) { height = width; }
x = Math.abs(Math.round(x));
y = Math.abs(Math.round(y));
var left = Clamp(x, 0, this.width);
var right = Clamp(x + width, 0, this.width);
var top = Clamp(y, 0, this.height);
var bottom = Clamp(y + height, 0, this.height);
var pixel = new Color();
var out = [];
for (var py = top; py < bottom; py++)
{
var row = [];
for (var px = left; px < right; px++)
{
2018-12-11 09:31:56 +00:00
pixel = this.getPixel(px, py, pixel);
2018-12-11 02:37:00 +00:00
2018-12-11 09:31:56 +00:00
row.push({ x: px, y: py, color: pixel.color, alpha: pixel.alphaGL });
2018-12-11 02:37:00 +00:00
}
out.push(row);
}
2018-12-11 02:37:00 +00:00
return out;
},
2018-12-11 02:37:00 +00:00
/**
* Returns the Image Data index for the given pixel in this CanvasTexture.
*
* The index can be used to read directly from the `this.data` array.
*
* The index points to the red value in the array. The subsequent 3 indexes
* point to green, blue and alpha respectively.
*
* @method Phaser.Textures.CanvasTexture#getIndex
* @since 3.16.0
2020-11-23 10:32:00 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
2020-11-23 10:32:00 +00:00
*
* @return {number}
2018-12-11 02:37:00 +00:00
*/
getIndex: function (x, y)
{
x = Math.abs(Math.round(x));
y = Math.abs(Math.round(y));
if (x < this.width && y < this.height)
{
return (x + y * this.width) * 4;
}
else
{
return -1;
}
2018-04-23 17:37:44 +00:00
},
/**
* This should be called manually if you are running under WebGL.
* It will refresh the WebGLTexture from the Canvas source. Only call this if you know that the
* canvas has changed, as there is a significant GPU texture allocation cost involved in doing so.
*
* @method Phaser.Textures.CanvasTexture#refresh
2018-05-04 17:51:02 +00:00
* @since 3.7.0
2018-04-23 17:37:44 +00:00
*
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
*/
refresh: function ()
{
this._source.update();
2018-04-23 17:37:44 +00:00
return this;
},
/**
* Gets the Canvas Element.
2018-04-23 17:37:44 +00:00
*
* @method Phaser.Textures.CanvasTexture#getCanvas
2018-05-04 17:51:02 +00:00
* @since 3.7.0
2018-04-23 17:37:44 +00:00
*
* @return {HTMLCanvasElement} The Canvas DOM element this texture is using.
*/
getCanvas: function ()
{
return this.canvas;
},
/**
* Gets the 2D Canvas Rendering Context.
2018-04-23 17:37:44 +00:00
*
* @method Phaser.Textures.CanvasTexture#getContext
2018-05-04 17:51:02 +00:00
* @since 3.7.0
2018-04-23 17:37:44 +00:00
*
* @return {CanvasRenderingContext2D} The Canvas Rendering Context this texture is using.
2018-04-23 17:37:44 +00:00
*/
getContext: function ()
{
return this.context;
},
/**
2018-12-07 19:28:38 +00:00
* Clears the given region of this Canvas Texture, resetting it back to transparent.
* If no region is given, the whole Canvas Texture is cleared.
*
* @method Phaser.Textures.CanvasTexture#clear
2018-05-04 17:51:02 +00:00
* @since 3.7.0
2020-11-23 10:32:00 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} [x=0] - The x coordinate of the top-left of the region to clear.
* @param {number} [y=0] - The y coordinate of the top-left of the region to clear.
* @param {number} [width] - The width of the region.
* @param {number} [height] - The height of the region.
*
* @return {Phaser.Textures.CanvasTexture} The Canvas Texture.
*/
2018-12-07 19:28:38 +00:00
clear: function (x, y, width, height)
{
2018-12-07 19:28:38 +00:00
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);
2018-09-10 19:44:19 +00:00
return this.update();
},
/**
* Changes the size of this Canvas Texture.
*
* @method Phaser.Textures.CanvasTexture#setSize
2018-05-04 17:51:02 +00:00
* @since 3.7.0
*
2020-11-23 10:22:13 +00:00
* @param {number} width - The new width of the Canvas.
* @param {number} [height] - The new height of the Canvas. If not given it will use the width as the height.
*
* @return {Phaser.Textures.CanvasTexture} The Canvas Texture.
*/
setSize: function (width, height)
{
if (height === undefined) { height = width; }
if (width !== this.width || height !== this.height)
{
// Update the Canvas
this.canvas.width = width;
this.canvas.height = height;
// Update the Texture Source
this._source.width = width;
this._source.height = height;
this._source.isPowerOf2 = IsSizePowerOfTwo(width, height);
// Update the Frame
this.frames['__BASE'].setSize(width, height, 0, 0);
// Update this
this.width = width;
this.height = height;
this.refresh();
}
return this;
},
/**
* Destroys this Texture and releases references to its sources and frames.
*
* @method Phaser.Textures.CanvasTexture#destroy
* @since 3.16.0
*/
destroy: function ()
{
Texture.prototype.destroy.call(this);
this._source = null;
this.canvas = null;
this.context = null;
this.imageData = null;
this.data = null;
this.pixels = null;
this.buffer = null;
}
2018-04-23 17:37:44 +00:00
});
module.exports = CanvasTexture;