44* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License }
55*/
66
7+ var CONST = require ( '../const' ) ;
8+
79/**
810 * The pool into which the canvas elements are placed.
911 *
@@ -35,18 +37,23 @@ var CanvasPool = function ()
3537 * @param {number } height - The height of the canvas element.
3638 * @return {HTMLCanvasElement } The canvas element.
3739 */
38- var create = function ( parent , width , height )
40+ var create = function ( parent , width , height , type )
3941 {
40- var idx = first ( ) ;
41- var canvas ;
42+ if ( width === undefined ) { width = 1 ; }
43+ if ( height === undefined ) { height = 1 ; }
44+ if ( type === undefined ) { type = CONST . CANVAS ; }
4245
43- console . log ( 'CanvasPool.create' , idx ) ;
46+ var canvas ;
47+ var container = first ( type ) ;
4448
45- if ( idx === - 1 )
49+ if ( container === null )
4650 {
47- var container = {
51+ console . log ( 'CanvasPool.create new' ) ;
52+
53+ container = {
4854 parent : parent ,
49- canvas : document . createElement ( 'canvas' )
55+ canvas : document . createElement ( 'canvas' ) ,
56+ type : type
5057 } ;
5158
5259 pool . push ( container ) ;
@@ -55,9 +62,11 @@ var CanvasPool = function ()
5562 }
5663 else
5764 {
58- pool [ idx ] . parent = parent ;
65+ console . log ( 'CanvasPool.create existing' ) ;
66+
67+ container . parent = parent ;
5968
60- canvas = pool [ idx ] . canvas ;
69+ canvas = container . canvas ;
6170 }
6271
6372 if ( width !== undefined )
@@ -69,24 +78,36 @@ var CanvasPool = function ()
6978 return canvas ;
7079 } ;
7180
81+ var create2D = function ( parent , width , height )
82+ {
83+ return create ( parent , width , height , CONST . CANVAS ) ;
84+ } ;
85+
86+ var createWebGL = function ( parent , width , height )
87+ {
88+ return create ( parent , width , height , CONST . WEBGL ) ;
89+ } ;
90+
7291 /**
7392 * Gets the first free canvas index from the pool.
7493 *
7594 * @static
7695 * @method Phaser.CanvasPool.getFirst
7796 * @return {number }
7897 */
79- var first = function ( )
98+ var first = function ( type )
8099 {
81- for ( var i = 0 ; i < pool . length ; i ++ )
100+ if ( type === undefined ) { type = CONST . CANVAS ; }
101+
102+ pool . forEach ( function ( container )
82103 {
83- if ( ! pool [ i ] . parent )
104+ if ( ! container . parent && container . type === type )
84105 {
85- return i ;
106+ return container ;
86107 }
87- }
108+ } ) ;
88109
89- return - 1 ;
110+ return null ;
90111 } ;
91112
92113 /**
@@ -95,49 +116,23 @@ var CanvasPool = function ()
95116 *
96117 * @static
97118 * @method Phaser.CanvasPool.remove
98- * @param {any } parent - The parent of the canvas element.
119+ * @param {any|HTMLCanvasElement } parent - The parent of the canvas element.
99120 */
100121 var remove = function ( parent )
101122 {
102- // Check to see if the parent is a canvas object, then do removeByCanvas stuff instead
103- // CanvasRenderingContext2D
104-
105- for ( var i = 0 ; i < pool . length ; i ++ )
106- {
107- if ( pool [ i ] . parent === parent )
108- {
109- pool [ i ] . parent = null ;
110- pool [ i ] . canvas . width = 1 ;
111- pool [ i ] . canvas . height = 1 ;
112- }
113- }
114-
115- } ;
116-
117- /**
118- * Looks up a canvas based on its type, and if found puts it back in the pool, freeing it up for re-use.
119- * The canvas has its width and height set to 1, and its parent attribute nulled.
120- *
121- * @static
122- * @method Phaser.CanvasPool.removeByCanvas
123- * @param {HTMLCanvasElement } canvas - The canvas element to remove.
124- */
125- var removeByCanvas = function ( canvas )
126- {
127- console . log ( 'removeByCanvas' ) ;
123+ // Check to see if the parent is a canvas object
124+ var isCanvas = parent instanceof HTMLCanvasElement ;
128125
129- for ( var i = 0 ; i < pool . length ; i ++ )
126+ pool . forEach ( function ( container )
130127 {
131- if ( pool [ i ] . canvas === canvas )
128+ if ( ( isCanvas && container . canvas === parent ) || ( ! isCanvas && container . parent === parent ) )
132129 {
133- console . log ( 'found and removed' ) ;
134-
135- pool [ i ] . parent = null ;
136- pool [ i ] . canvas . width = 1 ;
137- pool [ i ] . canvas . height = 1 ;
130+ console . log ( 'CanvasPool.remove found and removed' ) ;
131+ container . parent = null ;
132+ container . canvas . width = 1 ;
133+ container . canvas . height = 1 ;
138134 }
139- }
140-
135+ } ) ;
141136 } ;
142137
143138 /**
@@ -147,17 +142,17 @@ var CanvasPool = function ()
147142 * @method Phaser.CanvasPool.getTotal
148143 * @return {number } The number of in-use (parented) canvas elements in the pool.
149144 */
150- var getTotal = function ( )
145+ var total = function ( )
151146 {
152147 var c = 0 ;
153148
154- for ( var i = 0 ; i < pool . length ; i ++ )
149+ pool . forEach ( function ( container )
155150 {
156- if ( pool [ i ] . parent )
151+ if ( container . parent )
157152 {
158153 c ++ ;
159154 }
160- }
155+ } ) ;
161156
162157 return c ;
163158 } ;
@@ -169,32 +164,22 @@ var CanvasPool = function ()
169164 * @method Phaser.CanvasPool.getFree
170165 * @return {number } The number of free (un-parented) canvas elements in the pool.
171166 */
172- var getFree = function ( )
167+ var free = function ( )
173168 {
174- var c = 0 ;
175-
176- for ( var i = 0 ; i < pool . length ; i ++ )
177- {
178- if ( ! pool [ i ] . parent )
179- {
180- c ++ ;
181- }
182- }
183-
184- return c ;
169+ return pool . length - total ( ) ;
185170 } ;
186171
187172 return {
188173 create : create ,
174+ create2D : create2D ,
175+ createWebGL : createWebGL ,
189176 first : first ,
190177 remove : remove ,
191- removeByCanvas : removeByCanvas ,
192- getTotal : getTotal ,
193- getFree : getFree ,
178+ total : total ,
179+ free : free ,
194180 pool : pool
195181 } ;
196182} ;
197183
198184// If we export the called function here, it'll only be invoked once (not every time it's required).
199- // This function must return something though
200185module . exports = CanvasPool ( ) ;
0 commit comments