Skip to content

Commit 4a58ed3

Browse files
committed
The start of a new CanvasTexture class
1 parent d96d677 commit 4a58ed3

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

src/textures/CanvasTexture.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Class = require('../utils/Class');
8+
var Texture = require('./Texture');
9+
10+
/**
11+
* @classdesc
12+
* [description]
13+
*
14+
* @class CanvasTexture
15+
* @extends Phaser.Textures.Texture
16+
* @memberOf Phaser.Textures
17+
* @constructor
18+
* @since 3.6.1
19+
*
20+
* @param {Phaser.Textures.TextureManager} manager - A reference to the Texture Manager this Texture belongs to.
21+
* @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.
25+
*/
26+
var CanvasTexture = new Class({
27+
28+
Extends: Texture,
29+
30+
initialize:
31+
32+
function CanvasTexture (manager, key, source, width, height)
33+
{
34+
Texture.call(this, manager, key, source, width, height);
35+
36+
this._texture = this.frames['__BASE'].source;
37+
38+
this.canvas = this._texture.image;
39+
40+
this.context = this.canvas.getContext('2d');
41+
42+
this.width = width;
43+
44+
this.height = height;
45+
},
46+
47+
/**
48+
* This should be called manually if you are running under WebGL.
49+
* It will refresh the WebGLTexture from the Canvas source. Only call this if you know that the
50+
* canvas has changed, as there is a significant GPU texture allocation cost involved in doing so.
51+
*
52+
* @method Phaser.Textures.CanvasTexture#refresh
53+
* @since 3.6.1
54+
*
55+
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
56+
*/
57+
refresh: function ()
58+
{
59+
this._texture.update();
60+
61+
return this;
62+
},
63+
64+
/**
65+
* [description]
66+
*
67+
* @method Phaser.Textures.CanvasTexture#getCanvas
68+
* @since 3.6.1
69+
*
70+
* @return {HTMLCanvasElement} The Canvas DOM element this texture is using.
71+
*/
72+
getCanvas: function ()
73+
{
74+
return this.canvas;
75+
},
76+
77+
/**
78+
* [description]
79+
*
80+
* @method Phaser.Textures.CanvasTexture#getContext
81+
* @since 3.6.1
82+
*
83+
* @return {HTMLCanvasElement} The Canvas DOM element this texture is using.
84+
*/
85+
getContext: function ()
86+
{
87+
return this.context;
88+
},
89+
90+
});
91+
92+
module.exports = CanvasTexture;

0 commit comments

Comments
 (0)