Skip to content

Commit 8b2b969

Browse files
committed
BitmapText Canvas Rendering
1 parent 1cddea4 commit 8b2b969

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,94 @@
11
var BitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage, camera)
22
{
3+
if (this.renderMask !== this.renderFlags)
4+
{
5+
return;
6+
}
7+
var cameraScrollX = camera.scrollX;
8+
var cameraScrollY = camera.scrollY;
9+
var text = src.text;
10+
var textLength = text.length;
11+
var chars = src.fontData.chars;
12+
var lineHeight = src.fontData.lineHeight;
13+
var srcX = src.x;
14+
var srcY = src.y;
15+
var xAdvance = 0;
16+
var yAdvance = 0;
17+
var indexCount = 0;
18+
var charCode = 0;
19+
var glyph = null;
20+
var glyphX = 0;
21+
var glyphY = 0;
22+
var glyphW = 0;
23+
var glyphH = 0;
24+
var x = 0;
25+
var y = 0;
26+
var lastGlyph = null;
27+
var lastCharCode = 0;
28+
var ctx = renderer.currentContext;
29+
var image = src.frame.source.image;
330

31+
// Blend Mode
32+
if (renderer.currentBlendMode !== src.blendMode)
33+
{
34+
renderer.currentBlendMode = src.blendMode;
35+
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
36+
}
37+
38+
// Alpha
39+
if (renderer.currentAlpha !== src.alpha)
40+
{
41+
renderer.currentAlpha = src.alpha;
42+
ctx.globalAlpha = src.alpha;
43+
}
44+
45+
// Smoothing
46+
if (renderer.currentScaleMode !== src.scaleMode)
47+
{
48+
renderer.currentScaleMode = src.scaleMode;
49+
}
50+
51+
for (var index = 0; index < textLength; ++index)
52+
{
53+
charCode = text.charCodeAt(index);
54+
if (charCode === 10)
55+
{
56+
xAdvance = 0;
57+
indexCount = 0;
58+
yAdvance += lineHeight;
59+
lastGlyph = null;
60+
continue;
61+
}
62+
63+
glyph = chars[charCode];
64+
if (!glyph)
65+
{
66+
continue;
67+
}
68+
69+
glyphX = glyph.x;
70+
glyphY = glyph.y;
71+
glyphW = glyph.width;
72+
glyphH = glyph.height;
73+
x = (srcX + indexCount + glyph.xOffset + xAdvance) - cameraScrollX;
74+
y = (srcY + glyph.yOffset + yAdvance) - cameraScrollY;
75+
76+
if (lastGlyph !== null)
77+
{
78+
var kerningOffset = glyph.kerning[lastCharCode];
79+
x += (kerningOffset !== undefined) ? kerningOffset : 0;
80+
}
81+
82+
ctx.save();
83+
ctx.translate(x, y);
84+
ctx.drawImage(image, glyphX, glyphY, glyphW, glyphH, 0, 0, glyphW, glyphH);
85+
ctx.restore();
86+
87+
xAdvance += glyph.xAdvance;
88+
indexCount += 1;
89+
lastGlyph = glyph;
90+
lastCharCode = charCode;
91+
}
492
};
593

694
module.exports = BitmapTextCanvasRenderer;

v3/src/gameobjects/text/BitmapTextWebGLRenderer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var BitmapTextWebGLRenderer = function (renderer, src, interpolationPercentage,
55
return;
66
}
77

8-
var blitterBatch = renderer.blitterBatch;
98
var cameraMatrix = camera.matrix.matrix;
109
var a = cameraMatrix[0];
1110
var b = cameraMatrix[1];

0 commit comments

Comments
 (0)