Skip to content

Commit fc0dc13

Browse files
committed
Removed use of currentBlendMode and currentAlpha
1 parent 9f44896 commit fc0dc13

7 files changed

Lines changed: 24 additions & 81 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ The process of managing scissors in the WebGLRenderer has been completely rewrit
7979
* The WebGLRenderer has a new method `setBlankTexture` which forces it to set the blank texture as the current texture. This is used after drawing a Render Texture to ensure no other object tries to draw to itself.
8080
* The StaticTilemapLayer has had the following properties and methods added to it: `skipCull`, `tilesDrawn`, `tilesTotal`, `cullPaddingX`, `cullPaddingY`, `cullCallback`, `setSkipCull` and `setCullPadding` as these are all used by the Canvas Static Layer renderer. Static Layers in 3.11 didn't render in Canvas because the cull values were missing, but now render correctly and can also be rendered to other targets, like a Render Texture.
8181
* The Math.Snap methods `Snap.Floor`, `Snap.Ceil` and `Snap.To` have all gained a new optional boolean argument `divide`. If set the resulting snapped value will be divided by the gap amount before returning. This is handy if you're trying to quickly snap a value into a grid or array location.
82+
* The `currentBlendMode` property has been removed from the Canvas Renderer and is no longer checked by any class. Blend modes are now set directly on the context to avoid state saving invalidation.
83+
* The `currentAlpha` property has been removed from the Canvas Renderer and is no longer checked by any class. Alpha values are now set directly on the context to avoid state saving invalidation.
8284

8385
### Game Config Resolution Specific Bug Fixes
8486

src/gameobjects/bitmaptext/dynamic/DynamicBitmapTextCanvasRenderer.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,25 @@ var DynamicBitmapTextCanvasRenderer = function (renderer, src, interpolationPerc
6666
var rotation = 0;
6767
var scale = (src.fontSize / src.fontData.size);
6868

69-
// Alpha
70-
7169
var alpha = camera.alpha * src.alpha;
7270

7371
if (alpha === 0)
7472
{
7573
// Nothing to see, so abort early
7674
return;
7775
}
78-
else if (renderer.currentAlpha !== alpha)
79-
{
80-
renderer.currentAlpha = alpha;
81-
ctx.globalAlpha = alpha;
82-
}
8376

8477
// Blend Mode
8578
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
8679

8780
// Alpha
88-
ctx.globalAlpha = src.alpha;
81+
ctx.globalAlpha = alpha;
8982

9083
ctx.save();
9184

92-
if (parentMatrix !== undefined)
85+
if (parentMatrix)
9386
{
94-
var matrix = parentMatrix.matrix;
95-
ctx.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
87+
parentMatrix.copyToContext(ctx);
9688
}
9789

9890
ctx.translate(src.x, src.y);

src/gameobjects/blitter/BlitterCanvasRenderer.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ var BlitterCanvasRenderer = function (renderer, src, interpolationPercentage, ca
2828
return;
2929
}
3030

31-
var ctx = renderer.gameContext;
32-
33-
// Alpha
31+
var ctx = renderer.currentContext;
3432

3533
var alpha = camera.alpha * src.alpha;
3634

@@ -39,25 +37,18 @@ var BlitterCanvasRenderer = function (renderer, src, interpolationPercentage, ca
3937
// Nothing to see, so abort early
4038
return;
4139
}
42-
else if (renderer.currentAlpha !== alpha)
43-
{
44-
renderer.currentAlpha = alpha;
45-
ctx.globalAlpha = alpha;
46-
}
4740

4841
// Blend Mode
49-
50-
renderer.setBlendMode(src.blendMode);
42+
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
5143

5244
var cameraScrollX = src.x - camera.scrollX * src.scrollFactorX;
5345
var cameraScrollY = src.y - camera.scrollY * src.scrollFactorY;
5446

5547
ctx.save();
5648

57-
if (parentMatrix !== undefined)
49+
if (parentMatrix)
5850
{
59-
var matrix = parentMatrix.matrix;
60-
ctx.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
51+
parentMatrix.copyToContext(ctx);
6152
}
6253

6354
// Render bobs
@@ -78,11 +69,8 @@ var BlitterCanvasRenderer = function (renderer, src, interpolationPercentage, ca
7869
{
7970
continue;
8071
}
81-
else if (renderer.currentAlpha !== bobAlpha)
82-
{
83-
renderer.currentAlpha = bobAlpha;
84-
ctx.globalAlpha = bobAlpha;
85-
}
72+
73+
ctx.globalAlpha = bobAlpha;
8674

8775
if (!flip)
8876
{

src/gameobjects/container/ContainerCanvasRenderer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ var ContainerCanvasRenderer = function (renderer, container, interpolationPercen
6161
var childScrollFactorX = child.scrollFactorX;
6262
var childScrollFactorY = child.scrollFactorY;
6363

64+
// Set new values
6465
child.setScrollFactor(childScrollFactorX * scrollFactorX, childScrollFactorY * scrollFactorY);
6566
child.setAlpha(childAlpha * alpha);
67+
68+
// Render
6669
child.renderCanvas(renderer, child, interpolationPercentage, camera, transformMatrix);
70+
71+
// Restore old values
6772
child.setAlpha(childAlpha);
6873
child.setScrollFactor(childScrollFactorX, childScrollFactorY);
6974
}

src/gameobjects/graphics/GraphicsCanvasRenderer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
9191
calcMatrix.copyToContext(ctx);
9292

9393
ctx.fillStyle = '#fff';
94-
ctx.globalAlpha = src.alpha;
9594

9695
for (var index = 0; index < commandBufferLength; ++index)
9796
{

src/gameobjects/text/static/TextCanvasRenderer.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ var TextCanvasRenderer = function (renderer, src, interpolationPercentage, camer
2828

2929
var ctx = renderer.currentContext;
3030

31-
// Alpha
32-
3331
var alpha = camera.alpha * src.alpha;
3432

3533
if (alpha === 0)

src/renderer/canvas/CanvasRenderer.js

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,6 @@ var CanvasRenderer = new Class({
157157
*/
158158
this.blendModes = GetBlendModes();
159159

160-
/**
161-
* [description]
162-
*
163-
* @name Phaser.Renderer.Canvas.CanvasRenderer#currentAlpha
164-
* @type {number}
165-
* @default 1
166-
* @since 3.0.0
167-
*/
168-
this.currentAlpha = 1;
169-
170-
/**
171-
* [description]
172-
*
173-
* @name Phaser.Renderer.Canvas.CanvasRenderer#currentBlendMode
174-
* @type {number}
175-
* @default 0
176-
* @since 3.0.0
177-
*/
178-
this.currentBlendMode = 0;
179-
180160
/**
181161
* [description]
182162
*
@@ -346,17 +326,13 @@ var CanvasRenderer = new Class({
346326
*
347327
* @param {number} blendMode - [description]
348328
*
349-
* @return {number} [description]
329+
* @return {this} [description]
350330
*/
351331
setBlendMode: function (blendMode)
352332
{
353-
if (this.currentBlendMode !== blendMode)
354-
{
355-
this.currentContext.globalCompositeOperation = blendMode;
356-
this.currentBlendMode = blendMode;
357-
}
333+
this.currentContext.globalCompositeOperation = blendMode;
358334

359-
return this.currentBlendMode;
335+
return this;
360336
},
361337

362338
/**
@@ -384,17 +360,13 @@ var CanvasRenderer = new Class({
384360
*
385361
* @param {number} alpha - [description]
386362
*
387-
* @return {number} [description]
363+
* @return {this} [description]
388364
*/
389365
setAlpha: function (alpha)
390366
{
391-
if (this.currentAlpha !== alpha)
392-
{
393-
this.currentContext.globalAlpha = alpha;
394-
this.currentAlpha = alpha;
395-
}
367+
this.currentContext.globalAlpha = alpha;
396368

397-
return this.currentAlpha;
369+
return this;
398370
},
399371

400372
/**
@@ -461,15 +433,7 @@ var CanvasRenderer = new Class({
461433

462434
ctx.globalAlpha = camera.alpha;
463435

464-
this.currentAlpha = camera.alpha;
465-
466-
if (this.currentBlendMode !== 0)
467-
{
468-
ctx.globalCompositeOperation = 'source-over';
469-
this.currentBlendMode = 0;
470-
}
471-
472-
this.currentScaleMode = 0;
436+
ctx.globalCompositeOperation = 'source-over';
473437

474438
this.drawCount += list.length;
475439

@@ -481,9 +445,7 @@ var CanvasRenderer = new Class({
481445
ctx.clip();
482446
}
483447

484-
var matrix = camera.matrix.matrix;
485-
486-
ctx.setTransform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
448+
camera.matrix.copyToContext(ctx);
487449

488450
for (var i = 0; i < childCount; i++)
489451
{
@@ -536,9 +498,6 @@ var CanvasRenderer = new Class({
536498
ctx.globalAlpha = 1;
537499
ctx.globalCompositeOperation = 'source-over';
538500

539-
this.currentAlpha = 1;
540-
this.currentBlendMode = 0;
541-
542501
if (this.snapshotCallback)
543502
{
544503
this.snapshotCallback(CanvasSnapshot(this.gameCanvas, this.snapshotType, this.snapshotEncoder));

0 commit comments

Comments
 (0)