@@ -2,45 +2,43 @@ var CONST = require('../../const');
22var DrawImage = require ( './utils/DrawImage' ) ;
33var BlitImage = require ( './utils/BlitImage' ) ;
44var GetBlendModes = require ( './utils/GetBlendModes' ) ;
5+ var GetContext = require ( '../../canvas/GetContext' ) ;
56
67var CanvasRenderer = function ( game )
78{
89 /**
910 * @property {Phaser.Game } game - A reference to the currently running Game.
1011 */
11- // Needed?
1212 this . game = game ;
1313
1414 // Needed?
1515 this . type = CONST . CANVAS ;
1616
17- // Read all the following from game config (or State config?)
18- this . clearBeforeRender = true ;
19-
20- this . transparent = false ;
21-
22- this . autoResize = false ;
23-
2417 this . drawCount = 0 ;
2518
19+ // Read all the following from game config (or State config?)
20+ // this.clearBeforeRender = true;
21+ // this.transparent = false;
22+ // this.autoResize = false;
2623 // this.smoothProperty = Phaser.Canvas.getSmoothingPrefix(this.context);
27-
28- this . roundPixels = false ;
24+ // this.roundPixels = false;
2925
3026 this . width = game . config . width * game . config . resolution ;
31-
3227 this . height = game . config . height * game . config . resolution ;
33-
3428 this . resolution = game . config . resolution ;
3529
36- this . view = game . canvas ;
30+ this . gameCanvas = game . canvas ;
3731
3832 /**
3933 * The canvas 2d context that everything is drawn with
4034 * @property context
4135 * @type CanvasRenderingContext2D
4236 */
43- this . context = this . view . getContext ( '2d' , { alpha : true } ) ;
37+ this . gameContext = GetContext ( this . gameCanvas ) ;
38+
39+ this . gameConfig = game . config ;
40+
41+ this . currentContext = this . gameContext ;
4442
4543 // Map to the required function
4644 this . drawImage = DrawImage ;
@@ -73,70 +71,31 @@ CanvasRenderer.prototype = {
7371 this . width = width * res ;
7472 this . height = height * res ;
7573
76- this . view . width = this . width ;
77- this . view . height = this . height ;
74+ this . gameCanvas . width = this . width ;
75+ this . gameCanvas . height = this . height ;
7876
7977 if ( this . autoResize )
8078 {
81- this . view . style . width = ( this . width / res ) + 'px' ;
82- this . view . style . height = ( this . height / res ) + 'px' ;
79+ this . gameCanvas . style . width = ( this . width / res ) + 'px' ;
80+ this . gameCanvas . style . height = ( this . height / res ) + 'px' ;
8381 }
8482
8583 // if (this.smoothProperty)
8684 // {
87- // this.context [this.smoothProperty] = (this.scaleMode === ScaleModes.LINEAR);
85+ // this.gameContext [this.smoothProperty] = (this.scaleMode === ScaleModes.LINEAR);
8886 // }
8987 } ,
9088
91- // Call at the start of the render loop
92- preRender : function ( )
93- {
94- // console.log('%c render start ', 'color: #ffffff; background: #00ff00;');
95-
96- // Add Pre-render hook
97-
98- this . drawCount = 0 ;
99-
100- var ctx = this . context ;
101-
102- ctx . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
103-
104- // If the alpha or blend mode didn't change since the last render, then don't set them again (saves 2 ops)
105-
106- if ( this . currentAlpha !== 1 )
107- {
108- ctx . globalAlpha = 1 ;
109- this . currentAlpha = 1 ;
110- }
111-
112- if ( this . currentBlendMode !== 0 )
113- {
114- ctx . globalCompositeOperation = 'source-over' ;
115- this . currentBlendMode = 0 ;
116- }
117-
118- this . currentScaleMode = 0 ;
119-
120- if ( this . clearBeforeRender )
121- {
122- ctx . clearRect ( 0 , 0 , this . width , this . height ) ;
123- }
124-
125- // TEMP
126- ctx . fillStyle = '#000000' ;
127- ctx . fillRect ( 0 , 0 , this . width , this . height ) ;
128- } ,
129-
13089 resetTransform : function ( )
13190 {
132- this . context . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
91+ this . currentContext . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
13392 } ,
13493
13594 setBlendMode : function ( blendMode )
13695 {
13796 if ( this . currentBlendMode !== blendMode )
13897 {
139- this . context . globalCompositeOperation = blendMode ;
98+ this . currentContext . globalCompositeOperation = blendMode ;
14099 this . currentBlendMode = blendMode ;
141100 }
142101 } ,
@@ -145,11 +104,35 @@ CanvasRenderer.prototype = {
145104 {
146105 if ( this . currentAlpha !== alpha )
147106 {
148- this . context . globalAlpha = alpha ;
107+ this . currentContext . globalAlpha = alpha ;
149108 this . currentAlpha = alpha ;
150109 }
151110 } ,
152111
112+ // Call at the start of the render loop
113+ preRender : function ( )
114+ {
115+ // console.log('%c render start ', 'color: #ffffff; background: #00ff00;');
116+
117+ var ctx = this . gameContext ;
118+ var config = this . gameConfig ;
119+
120+ if ( config . clearBeforeRender )
121+ {
122+ ctx . clearRect ( 0 , 0 , this . width , this . height ) ;
123+ }
124+
125+ if ( ! config . transparent )
126+ {
127+ ctx . fillStyle = config . backgroundColor ;
128+ ctx . fillRect ( 0 , 0 , this . width , this . height ) ;
129+ }
130+
131+ // Add Pre-render hook
132+
133+ this . drawCount = 0 ;
134+ } ,
135+
153136 /**
154137 * Renders the State.
155138 *
@@ -161,9 +144,46 @@ CanvasRenderer.prototype = {
161144 */
162145 render : function ( state , list , interpolationPercentage )
163146 {
164- this . drawCount = list . length ;
147+ var w = state . sys . width ;
148+ var h = state . sys . height ;
149+ var ctx = state . sys . context ;
150+ var settings = state . sys . settings ;
165151
166- // console.log(this.drawCount);
152+ this . currentContext = ctx ;
153+
154+ ctx . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
155+
156+ // If the alpha or blend mode didn't change since the last render, then don't set them again (saves 2 ops)
157+
158+ if ( this . currentAlpha !== 1 )
159+ {
160+ ctx . globalAlpha = 1 ;
161+ this . currentAlpha = 1 ;
162+ }
163+
164+ if ( this . currentBlendMode !== 0 )
165+ {
166+ ctx . globalCompositeOperation = 'source-over' ;
167+ this . currentBlendMode = 0 ;
168+ }
169+
170+ this . currentScaleMode = 0 ;
171+
172+ if ( settings . renderToTexture )
173+ {
174+ if ( settings . clearBeforeRender )
175+ {
176+ ctx . clearRect ( 0 , 0 , w , h ) ;
177+ }
178+
179+ if ( settings . backgroundColor )
180+ {
181+ ctx . fillStyle = settings . backgroundColor ;
182+ ctx . fillRect ( 0 , 0 , w , h ) ;
183+ }
184+ }
185+
186+ this . drawCount += list . length ;
167187
168188 for ( var c = 0 ; c < list . length ; c ++ )
169189 {
@@ -173,7 +193,16 @@ CanvasRenderer.prototype = {
173193 }
174194
175195 // Reset the transform so going into the devs render function the context is ready for use
176- this . context . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
196+ ctx . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
197+
198+ // Call the State.render function
199+ state . render ( ctx , interpolationPercentage ) ;
200+
201+ // Blast it to the Game Canvas (if needed)
202+ if ( settings . renderToTexture )
203+ {
204+ this . gameContext . drawImage ( state . sys . canvas , 0 , 0 , w , h , settings . x , settings . y , w , h ) ;
205+ }
177206 } ,
178207
179208 postRender : function ( )
@@ -187,14 +216,14 @@ CanvasRenderer.prototype = {
187216 * Removes everything from the renderer and optionally removes the Canvas DOM element.
188217 *
189218 * @method destroy
190- * @param [removeView =true] {boolean} Removes the Canvas element from the DOM.
219+ * @param [removegameCanvas =true] {boolean} Removes the Canvas element from the DOM.
191220 */
192221 destroy : function ( )
193222 {
194223 // CanvasPool
195224
196- this . view = null ;
197- this . context = null ;
225+ this . gameCanvas = null ;
226+ this . gameContext = null ;
198227 }
199228
200229} ;
0 commit comments