Skip to content

Commit 6ff2cc6

Browse files
committed
The new CanvasTexture class now working and linked to examples.
1 parent 152c68e commit 6ff2cc6

1 file changed

Lines changed: 123 additions & 12 deletions

File tree

src/textures/CanvasTexture.js

Lines changed: 123 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,28 @@
55
*/
66

77
var Class = require('../utils/Class');
8+
var IsSizePowerOfTwo = require('../math/pow2/IsSizePowerOfTwo');
89
var Texture = require('./Texture');
910

1011
/**
1112
* @classdesc
12-
* [description]
13+
* A Canvas Texture is a special kind of Texture that is backed by an HTML Canvas Element as its source.
14+
*
15+
* You can use the properties of this texture to draw to the canvas element directly, using all of the standard
16+
* canvas operations available in the browser. Any Game Object can be given this texture and will render with it.
17+
*
18+
* Note: When running under WebGL the Canvas Texture needs to re-generate its base WebGLTexture and reupload it to
19+
* the GPU every time you modify it, otherwise the changes you make to this texture will not be visible. To do this
20+
* you should call `CanvasTexture.refresh()` once you are finished with your changes to the canvas. Try and keep
21+
* this to a minimum, especially on large canvas sizes, or you may inadvertently thrash the GPU by constantly uploading
22+
* texture data to it. This restriction does not apply if using the Canvas Renderer.
23+
*
24+
* It starts with only one frame that covers the whole of the canvas. You can add further frames, that specify
25+
* sections of the canvas using the `add` method.
26+
*
27+
* Should you need to resize the canvas use the `setSize` method so that it accurately updates all of the underlying
28+
* texture data as well. Forgetting to do this (i.e. by changing the canvas size directly from your code) could cause
29+
* graphical errors.
1330
*
1431
* @class CanvasTexture
1532
* @extends Phaser.Textures.Texture
@@ -19,9 +36,9 @@ var Texture = require('./Texture');
1936
*
2037
* @param {Phaser.Textures.TextureManager} manager - A reference to the Texture Manager this Texture belongs to.
2138
* @param {string} key - The unique string-based key of this Texture.
22-
* @param {HTMLCanvasElement} source - The source that is used to create the texture. Usually an Image, but can also be a Canvas.
23-
* @param {number} [width] - The width of the Texture. This is optional and automatically derived from the source images.
24-
* @param {number} [height] - The height of the Texture. This is optional and automatically derived from the source images.
39+
* @param {HTMLCanvasElement} source - The canvas element that is used as the base of this texture.
40+
* @param {integer} width - The width of the canvas.
41+
* @param {integer} height - The height of the canvas.
2542
*/
2643
var CanvasTexture = new Class({
2744

@@ -33,14 +50,58 @@ var CanvasTexture = new Class({
3350
{
3451
Texture.call(this, manager, key, source, width, height);
3552

36-
this._texture = this.frames['__BASE'].source;
37-
38-
this.canvas = this._texture.image;
39-
53+
this.add('__BASE', 0, 0, 0, width, height);
54+
55+
/**
56+
* A reference to the Texture Source of this Canvas.
57+
*
58+
* @name Phaser.Textures.TextureManager#_source
59+
* @type {Phaser.Textures.TextureSource}
60+
* @private
61+
* @since 3.6.1
62+
*/
63+
this._source = this.frames['__BASE'].source;
64+
65+
/**
66+
* The source Canvas Element.
67+
*
68+
* @name Phaser.Textures.TextureManager#canvas
69+
* @readOnly
70+
* @type {HTMLCanvasElement}
71+
* @since 3.6.1
72+
*/
73+
this.canvas = this._source.image;
74+
75+
/**
76+
* The 2D Canvas Rendering Context.
77+
*
78+
* @name Phaser.Textures.TextureManager#canvas
79+
* @readOnly
80+
* @type {CanvasRenderingContext2D}
81+
* @since 3.6.1
82+
*/
4083
this.context = this.canvas.getContext('2d');
4184

85+
/**
86+
* The width of the Canvas.
87+
* This property is read-only, if you wish to change use `setSize`.
88+
*
89+
* @name Phaser.Textures.TextureManager#width
90+
* @readOnly
91+
* @type {integer}
92+
* @since 3.6.1
93+
*/
4294
this.width = width;
4395

96+
/**
97+
* The height of the Canvas.
98+
* This property is read-only, if you wish to change use `setSize`.
99+
*
100+
* @name Phaser.Textures.TextureManager#height
101+
* @readOnly
102+
* @type {integer}
103+
* @since 3.6.1
104+
*/
44105
this.height = height;
45106
},
46107

@@ -56,13 +117,13 @@ var CanvasTexture = new Class({
56117
*/
57118
refresh: function ()
58119
{
59-
this._texture.update();
120+
this._source.update();
60121

61122
return this;
62123
},
63124

64125
/**
65-
* [description]
126+
* Gets the Canvas Element.
66127
*
67128
* @method Phaser.Textures.CanvasTexture#getCanvas
68129
* @since 3.6.1
@@ -75,18 +136,68 @@ var CanvasTexture = new Class({
75136
},
76137

77138
/**
78-
* [description]
139+
* Gets the 2D Canvas Rendering Context.
79140
*
80141
* @method Phaser.Textures.CanvasTexture#getContext
81142
* @since 3.6.1
82143
*
83-
* @return {HTMLCanvasElement} The Canvas DOM element this texture is using.
144+
* @return {CanvasRenderingContext2D} The Canvas Rendering Context this texture is using.
84145
*/
85146
getContext: function ()
86147
{
87148
return this.context;
88149
},
89150

151+
/**
152+
* Clears this Canvas Texture, resetting it back to transparent.
153+
*
154+
* @method Phaser.Textures.CanvasTexture#clear
155+
* @since 3.6.1
156+
*
157+
* @return {Phaser.Textures.CanvasTexture} The Canvas Texture.
158+
*/
159+
clear: function ()
160+
{
161+
this.context.clearRect(0, 0, this.width, this.height);
162+
163+
return this;
164+
},
165+
166+
/**
167+
* Changes the size of this Canvas Texture.
168+
*
169+
* @method Phaser.Textures.CanvasTexture#setSize
170+
* @since 3.6.1
171+
*
172+
* @param {integer} width - The new width of the Canvas.
173+
* @param {integer} [height] - The new height of the Canvas. If not given it will use the width as the height.
174+
*
175+
* @return {Phaser.Textures.CanvasTexture} The Canvas Texture.
176+
*/
177+
setSize: function (width, height)
178+
{
179+
if (height === undefined) { height = width; }
180+
181+
if (width !== this.width || height !== this.height)
182+
{
183+
// Update the Canvas
184+
this.canvas.width = width;
185+
this.canvas.height = height;
186+
187+
// Update the Texture Source
188+
this._source.width = width;
189+
this._source.height = height;
190+
this._source.isPowerOf2 = IsSizePowerOfTwo(width, height);
191+
192+
// Update the Frame
193+
this.frames['__BASE'].setSize(width, height, 0, 0);
194+
195+
this.refresh();
196+
}
197+
198+
return this;
199+
}
200+
90201
});
91202

92203
module.exports = CanvasTexture;

0 commit comments

Comments
 (0)