Skip to content

Commit 708a857

Browse files
committed
SetTransform is a new Canvas Renderer function that consolidates the process of preparing a Game Object for rendering, without actually rendering it. This is used internally by the Graphics and Bitmap Text classes.
1 parent 8ed749b commit 708a857

6 files changed

Lines changed: 112 additions & 83 deletions

File tree

src/gameobjects/bitmaptext/dynamic/DynamicBitmapTextCanvasRenderer.js

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var SetTransform = require('../../../renderer/canvas/utils/SetTransform');
8+
79
/**
810
* Renders this Game Object with the Canvas Renderer to the given Camera.
911
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
@@ -24,7 +26,9 @@ var DynamicBitmapTextCanvasRenderer = function (renderer, src, interpolationPerc
2426
var text = src.text;
2527
var textLength = text.length;
2628

27-
if (textLength === 0)
29+
var ctx = renderer.currentContext;
30+
31+
if (textLength === 0 || !SetTransform(renderer, ctx, src, camera, parentMatrix))
2832
{
2933
return;
3034
}
@@ -57,7 +61,7 @@ var DynamicBitmapTextCanvasRenderer = function (renderer, src, interpolationPerc
5761
var lastGlyph = null;
5862
var lastCharCode = 0;
5963

60-
var ctx = renderer.currentContext;
64+
// var ctx = renderer.currentContext;
6165
var image = src.frame.source.image;
6266

6367
var textureX = textureFrame.cutX;
@@ -66,35 +70,6 @@ var DynamicBitmapTextCanvasRenderer = function (renderer, src, interpolationPerc
6670
var rotation = 0;
6771
var scale = (src.fontSize / src.fontData.size);
6872

69-
var alpha = camera.alpha * src.alpha;
70-
71-
if (alpha === 0)
72-
{
73-
// Nothing to see, so abort early
74-
return;
75-
}
76-
77-
// Blend Mode
78-
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
79-
80-
// Alpha
81-
ctx.globalAlpha = alpha;
82-
83-
ctx.save();
84-
85-
if (parentMatrix)
86-
{
87-
parentMatrix.copyToContext(ctx);
88-
}
89-
90-
ctx.translate(src.x, src.y);
91-
92-
ctx.rotate(src.rotation);
93-
94-
ctx.translate(-src.displayOriginX, -src.displayOriginY);
95-
96-
ctx.scale(src.scaleX, src.scaleY);
97-
9873
if (src.cropWidth > 0 && src.cropHeight > 0)
9974
{
10075
ctx.save();

src/gameobjects/bitmaptext/static/BitmapTextCanvasRenderer.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var SetTransform = require('../../../renderer/canvas/utils/SetTransform');
8+
79
/**
810
* Renders this Game Object with the Canvas Renderer to the given Camera.
911
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
@@ -24,7 +26,9 @@ var BitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage,
2426
var text = src._text;
2527
var textLength = text.length;
2628

27-
if (textLength === 0)
29+
var ctx = renderer.currentContext;
30+
31+
if (textLength === 0 || !SetTransform(renderer, ctx, src, camera, parentMatrix))
2832
{
2933
return;
3034
}
@@ -52,7 +56,6 @@ var BitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage,
5256
var lastGlyph = null;
5357
var lastCharCode = 0;
5458

55-
var ctx = renderer.currentContext;
5659
var image = src.frame.source.image;
5760

5861
var textureX = textureFrame.cutX;
@@ -78,6 +81,7 @@ var BitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage,
7881
lineOffsetX = (lineData.longest - lineData.lengths[0]);
7982
}
8083

84+
/*
8185
// Alpha
8286
8387
var alpha = camera.alpha * src.alpha;
@@ -119,6 +123,11 @@ var BitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage,
119123
ctx.translate(-src.displayOriginX, -src.displayOriginY);
120124
121125
ctx.scale(src.scaleX, src.scaleY);
126+
*/
127+
128+
ctx.translate(-src.displayOriginX, -src.displayOriginY);
129+
130+
var roundPixels = camera.roundPixels;
122131

123132
for (var i = 0; i < textLength; i++)
124133
{

src/gameobjects/graphics/GraphicsCanvasRenderer.js

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var Commands = require('./Commands');
8+
var SetTransform = require('../../renderer/canvas/utils/SetTransform');
89

910
/**
1011
* Renders this Game Object with the Canvas Renderer to the given Camera.
@@ -28,12 +29,13 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
2829
var commandBuffer = src.commandBuffer;
2930
var commandBufferLength = commandBuffer.length;
3031

31-
if (commandBufferLength === 0)
32+
var ctx = renderTargetCtx || renderer.currentContext;
33+
34+
if (commandBufferLength === 0 || !SetTransform(renderer, ctx, src, camera, parentMatrix))
3235
{
3336
return;
3437
}
3538

36-
var ctx = renderTargetCtx || renderer.currentContext;
3739
var lineAlpha = 1;
3840
var fillAlpha = 1;
3941
var lineColor = 0;
@@ -43,53 +45,6 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
4345
var green = 0;
4446
var blue = 0;
4547

46-
var alpha = camera.alpha * src.alpha;
47-
48-
if (alpha === 0)
49-
{
50-
// Nothing to see, so abort early
51-
return;
52-
}
53-
54-
// Blend Mode
55-
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
56-
57-
// Alpha
58-
ctx.globalAlpha = alpha;
59-
60-
ctx.save();
61-
62-
var camMatrix = renderer._tempMatrix1;
63-
var graphicsMatrix = renderer._tempMatrix2;
64-
var calcMatrix = renderer._tempMatrix3;
65-
66-
graphicsMatrix.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY);
67-
68-
camMatrix.copyFrom(camera.matrix);
69-
70-
if (parentMatrix)
71-
{
72-
// Multiply the camera by the parent matrix
73-
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
74-
75-
// Undo the camera scroll
76-
graphicsMatrix.e = src.x;
77-
graphicsMatrix.f = src.y;
78-
79-
// Multiply by the Sprite matrix, store result in calcMatrix
80-
camMatrix.multiply(graphicsMatrix, calcMatrix);
81-
}
82-
else
83-
{
84-
graphicsMatrix.e -= camera.scrollX * src.scrollFactorX;
85-
graphicsMatrix.f -= camera.scrollY * src.scrollFactorY;
86-
87-
// Multiply by the Sprite matrix, store result in calcMatrix
88-
camMatrix.multiply(graphicsMatrix, calcMatrix);
89-
}
90-
91-
calcMatrix.copyToContext(ctx);
92-
9348
ctx.fillStyle = '#fff';
9449

9550
for (var index = 0; index < commandBufferLength; ++index)

src/renderer/canvas/CanvasRenderer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,17 @@ var CanvasRenderer = new Class({
522522
this.snapshotEncoder = encoderOptions;
523523
},
524524

525+
/**
526+
* Takes a Sprite Game Object, or any object that extends it, and draws it to the current context.
527+
*
528+
* @method Phaser.Renderer.Canvas.CanvasRenderer#batchSprite
529+
* @since 3.12.0
530+
*
531+
* @param {Phaser.GameObjects.GameObject} sprite - The texture based Game Object to draw.
532+
* @param {Phaser.Textures.Frame} frame - The frame to draw, doesn't have to be that owned by the Game Object.
533+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to use for the rendering transform.
534+
* @param {Phaser.GameObjects.Components.TransformMatrix} [parentTransformMatrix] - The transform matrix of the parent container, if set.
535+
*/
525536
batchSprite: function (sprite, frame, camera, parentTransformMatrix)
526537
{
527538
var alpha = camera.alpha * sprite.alpha;

src/renderer/canvas/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
BlitImage: require('./utils/BlitImage'),
1414
CanvasRenderer: require('./CanvasRenderer'),
1515
DrawImage: require('./utils/DrawImage'),
16-
GetBlendModes: require('./utils/GetBlendModes')
16+
GetBlendModes: require('./utils/GetBlendModes'),
17+
SetTransform: require('./utils/SetTransform')
1718

1819
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
/**
8+
* Takes a reference to the Canvas Renderer, a Canvas Rendering Context, a Game Object, a Camera and a parent matrix
9+
* and then performs the following steps:
10+
*
11+
* 1) Checks the alpha of the source combined with the Camera alpha. If 0 or less it aborts.
12+
* 2) Takes the Camera and Game Object matrix and multiplies them, combined with the parent matrix if given.
13+
* 3) Sets the blend mode of the context to be that used by the Game Object.
14+
* 4) Sets the alpha value of the context to be that used by the Game Object combined with the Camera.
15+
* 5) Saves the context state.
16+
* 6) Sets the final matrix values into the context via setTransform.
17+
*
18+
* This function is only meant to be used internally. Most of the Canvas Renderer classes use it.
19+
*
20+
* @function Phaser.Renderer.Canvas.SetTransform
21+
* @since 3.12.0
22+
*
23+
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
24+
* @param {Phaser.GameObjects.GameObject} src - The Game Object being rendered. Can be any type that extends the base class.
25+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
26+
* @param {Phaser.GameObjects.Components.TransformMatrix} [parentMatrix] - A parent transform matrix to apply to the Game Object before rendering.
27+
*
28+
* @return {boolean} `true` if the Game Object context was set, otherwise `false`.
29+
*/
30+
var SetTransform = function (renderer, ctx, src, camera, parentMatrix)
31+
{
32+
var alpha = camera.alpha * src.alpha;
33+
34+
if (alpha <= 0)
35+
{
36+
// Nothing to see, so don't waste time calculating stuff
37+
return false;
38+
}
39+
40+
var camMatrix = renderer._tempMatrix1.copyFromArray(camera.matrix.matrix);
41+
var gameObjectMatrix = renderer._tempMatrix2.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY);
42+
var calcMatrix = renderer._tempMatrix3;
43+
44+
if (parentMatrix)
45+
{
46+
// Multiply the camera by the parent matrix
47+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
48+
49+
// Undo the camera scroll
50+
gameObjectMatrix.e = src.x;
51+
gameObjectMatrix.f = src.y;
52+
53+
// Multiply by the Sprite matrix, store result in calcMatrix
54+
camMatrix.multiply(gameObjectMatrix, calcMatrix);
55+
}
56+
else
57+
{
58+
gameObjectMatrix.e -= camera.scrollX * src.scrollFactorX;
59+
gameObjectMatrix.f -= camera.scrollY * src.scrollFactorY;
60+
61+
// Multiply by the Sprite matrix, store result in calcMatrix
62+
camMatrix.multiply(gameObjectMatrix, calcMatrix);
63+
}
64+
65+
// Blend Mode
66+
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
67+
68+
// Alpha
69+
ctx.globalAlpha = alpha;
70+
71+
ctx.save();
72+
73+
calcMatrix.setToContext(ctx);
74+
75+
return true;
76+
};
77+
78+
module.exports = SetTransform;

0 commit comments

Comments
 (0)