55 */
66
77var Class = require ( '../utils/Class' ) ;
8+ var Color = require ( '../display/color/Color' ) ;
89var IsSizePowerOfTwo = require ( '../math/pow2/IsSizePowerOfTwo' ) ;
910var Texture = require ( './Texture' ) ;
1011
@@ -34,7 +35,7 @@ var Texture = require('./Texture');
3435 * @constructor
3536 * @since 3.7.0
3637 *
37- * @param {Phaser.Textures.TextureManager } manager - A reference to the Texture Manager this Texture belongs to.
38+ * @param {Phaser.Textures.CanvasTexture } manager - A reference to the Texture Manager this Texture belongs to.
3839 * @param {string } key - The unique string-based key of this Texture.
3940 * @param {HTMLCanvasElement } source - The canvas element that is used as the base of this texture.
4041 * @param {integer } width - The width of the canvas.
@@ -55,7 +56,7 @@ var CanvasTexture = new Class({
5556 /**
5657 * A reference to the Texture Source of this Canvas.
5758 *
58- * @name Phaser.Textures.TextureManager #_source
59+ * @name Phaser.Textures.CanvasTexturer #_source
5960 * @type {Phaser.Textures.TextureSource }
6061 * @private
6162 * @since 3.7.0
@@ -65,7 +66,7 @@ var CanvasTexture = new Class({
6566 /**
6667 * The source Canvas Element.
6768 *
68- * @name Phaser.Textures.TextureManager #canvas
69+ * @name Phaser.Textures.CanvasTexture #canvas
6970 * @readOnly
7071 * @type {HTMLCanvasElement }
7172 * @since 3.7.0
@@ -75,7 +76,7 @@ var CanvasTexture = new Class({
7576 /**
7677 * The 2D Canvas Rendering Context.
7778 *
78- * @name Phaser.Textures.TextureManager #canvas
79+ * @name Phaser.Textures.CanvasTexture #canvas
7980 * @readOnly
8081 * @type {CanvasRenderingContext2D }
8182 * @since 3.7.0
@@ -86,7 +87,7 @@ var CanvasTexture = new Class({
8687 * The width of the Canvas.
8788 * This property is read-only, if you wish to change use `setSize`.
8889 *
89- * @name Phaser.Textures.TextureManager #width
90+ * @name Phaser.Textures.CanvasTexture #width
9091 * @readOnly
9192 * @type {integer }
9293 * @since 3.7.0
@@ -97,12 +98,144 @@ var CanvasTexture = new Class({
9798 * The height of the Canvas.
9899 * This property is read-only, if you wish to change use `setSize`.
99100 *
100- * @name Phaser.Textures.TextureManager #height
101+ * @name Phaser.Textures.CanvasTexture #height
101102 * @readOnly
102103 * @type {integer }
103104 * @since 3.7.0
104105 */
105106 this . height = height ;
107+
108+ /**
109+ * The context image data.
110+ * Use the `update` method to populate this when the canvas changes.
111+ *
112+ * @name Phaser.Textures.CanvasTexture#imageData
113+ * @type {ImageData }
114+ * @since 3.13.0
115+ */
116+ this . imageData = this . context . getImageData ( 0 , 0 , width , height ) ;
117+
118+ /**
119+ * A Uint8ClampedArray view into the `buffer`.
120+ * Use the `update` method to populate this when the canvas changes.
121+ * Note that this is unavailable in some browsers, such as Epic Browser, due to their security restrictions.
122+ *
123+ * @name Phaser.Textures.CanvasTexture#imageData
124+ * @type {Uint8ClampedArray }
125+ * @since 3.13.0
126+ */
127+ this . data = null ;
128+
129+ if ( this . imageData )
130+ {
131+ this . data = this . imageData . data ;
132+ }
133+
134+ /**
135+ * An Uint32Array view into the `buffer`.
136+ *
137+ * @name Phaser.Textures.CanvasTexture#pixels
138+ * @type {Uint32Array }
139+ * @since 3.13.0
140+ */
141+ this . pixels = null ;
142+
143+ /**
144+ * An ArrayBuffer the same size as the context ImageData.
145+ *
146+ * @name Phaser.Textures.CanvasTexture#buffer
147+ * @type {ArrayBuffer }
148+ * @since 3.13.0
149+ */
150+ this . buffer ;
151+
152+ if ( this . data )
153+ {
154+ if ( this . imageData . data . buffer )
155+ {
156+ this . buffer = this . imageData . data . buffer ;
157+ this . pixels = new Uint32Array ( this . buffer ) ;
158+ }
159+ else
160+ if ( window . ArrayBuffer )
161+ {
162+ this . buffer = new ArrayBuffer ( this . imageData . data . length ) ;
163+ this . pixels = new Uint32Array ( this . buffer ) ;
164+ }
165+ else
166+ {
167+ this . pixels = this . imageData . data ;
168+ }
169+ }
170+ } ,
171+
172+ /**
173+ * This re-creates the `imageData` from the current context.
174+ * It then re-builds the ArrayBuffer, the `data` Uint8ClampedArray reference and the `pixels` Int32Array.
175+ *
176+ * Warning: This is a very expensive operation, so use it sparingly.
177+ *
178+ * @method Phaser.Textures.CanvasTexture#update
179+ * @since 3.13.0
180+ *
181+ * @return {Phaser.Textures.CanvasTexture } This CanvasTexture.
182+ */
183+ update : function ( )
184+ {
185+ this . imageData = this . context . getImageData ( 0 , 0 , this . context . width , this . context . height ) ;
186+
187+ this . data = this . imageData . data ;
188+
189+ if ( this . imageData . data . buffer )
190+ {
191+ this . buffer = this . imageData . data . buffer ;
192+ this . pixels = new Uint32Array ( this . buffer ) ;
193+ }
194+ else if ( window . ArrayBuffer )
195+ {
196+ this . buffer = new ArrayBuffer ( this . imageData . data . length ) ;
197+ this . pixels = new Uint32Array ( this . buffer ) ;
198+ }
199+ else
200+ {
201+ this . pixels = this . imageData . data ;
202+ }
203+
204+ return this ;
205+ } ,
206+
207+ /**
208+ * Get the color of a specific pixel in the context into a color object.
209+ *
210+ * If you have drawn anything to the CanvasTexture since it was created you must call CanvasTexture.update to refresh the array buffer,
211+ * 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.
212+ *
213+ * @method Phaser.Textures.CanvasTexture#getPixel
214+ * @since 3.13.0
215+ *
216+ * @param {integer } x - The x coordinate of the pixel to be set. Must lay within the dimensions of this CanvasTexture and be an integer, not a float.
217+ * @param {integer } y - The y coordinate of the pixel to be set. Must lay within the dimensions of this CanvasTexture and be an integer, not a float.
218+ * @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.
219+ *
220+ * @return {object } An object with the red, green, blue and alpha values set in the r, g, b and a properties.
221+ */
222+ getPixel : function ( x , y , out )
223+ {
224+ if ( ! out )
225+ {
226+ out = new Color ( ) ;
227+ }
228+
229+ var index = ~ ~ ( x + ( y * this . width ) ) ;
230+
231+ index *= 4 ;
232+
233+ out . r = this . data [ index ] ;
234+ out . g = this . data [ ++ index ] ;
235+ out . b = this . data [ ++ index ] ;
236+ out . a = this . data [ ++ index ] ;
237+
238+ return out ;
106239 } ,
107240
108241 /**
0 commit comments