Skip to content

Commit da9a6de

Browse files
committed
Canvas TileSprite
1 parent 9ee4160 commit da9a6de

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

v3/src/gameobjects/tilesprite/TileSprite.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var TileSprite = new Class({
4747

4848
this.potWidth = this.frame.width;
4949
this.potHeight = this.frame.height;
50+
this.canvasPattern = null;
5051

5152
if (resourceManager)
5253
{
@@ -71,7 +72,7 @@ var TileSprite = new Class({
7172

7273
this.tileTexture = resourceManager.createTexture(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.canvasBuffer, this.potWidth, this.potHeight);
7374

74-
}
75+
}
7576

7677
this.canvasBuffer = CanvasPool.create2D(null, this.potWidth, this.potHeight);
7778
this.canvasBufferCtx = this.canvasBuffer.getContext('2d');
@@ -84,6 +85,7 @@ var TileSprite = new Class({
8485
if (!this.dirty)
8586
return;
8687

88+
this.canvasBuffer.width = this.canvasBuffer.width;
8789
this.canvasBufferCtx.drawImage(
8890
this.frame.source.image,
8991
this.frame.cutX, this.frame.cutY,
@@ -96,6 +98,10 @@ var TileSprite = new Class({
9698
{
9799
this.renderer.uploadCanvasToGPU(this.canvasBuffer, this.tileTexture, true);
98100
}
101+
else
102+
{
103+
this.canvasPattern = this.canvasBufferCtx.createPattern(this.canvasBuffer, 'repeat');
104+
}
99105
this.dirty = false;
100106
}
101107

v3/src/gameobjects/tilesprite/TileSpriteCanvasRenderer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@ var TileSpriteCanvasRenderer = function (renderer, src, interpolationPercentage,
55
{
66
return;
77
}
8+
9+
var ctx = renderer.currentContext;
10+
var frame = src.frame;
11+
12+
// Blend Mode
13+
14+
if (renderer.currentBlendMode !== src.blendMode)
15+
{
16+
renderer.currentBlendMode = src.blendMode;
17+
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
18+
}
19+
20+
// Alpha
21+
22+
if (renderer.currentAlpha !== src.alpha)
23+
{
24+
renderer.currentAlpha = src.alpha;
25+
ctx.globalAlpha = src.alpha;
26+
}
27+
28+
// Smoothing
29+
30+
if (renderer.currentScaleMode !== src.scaleMode)
31+
{
32+
renderer.currentScaleMode = src.scaleMode;
33+
}
34+
35+
var dx = frame.x - (src.originX * src.width);
36+
var dy = frame.y - (src.originY * src.height);
37+
38+
ctx.save();
39+
ctx.translate(dx, dy);
40+
ctx.translate(src.x - camera.scrollX, src.y - camera.scrollY);
41+
ctx.fillStyle = src.canvasPattern;
42+
ctx.translate(-this.tilePositionX, -this.tilePositionY);
43+
ctx.fillRect(this.tilePositionX, this.tilePositionY, src.width, src.height);
44+
ctx.restore();
845
};
946

1047
module.exports = TileSpriteCanvasRenderer;

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ WebGLRenderer.prototype = {
427427
/* only call this once */
428428
dstTexture.texture = gl.createTexture();
429429
}
430-
431430
if (shouldUpdateResource)
432431
{
433432
/* Update resource */

v3/src/renderer/webgl/renderers/tilebatch/TileBatch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ TileBatch.prototype = {
202202
var mva, mvb, mvc, mvd, mve, mvf, tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3;
203203
var sra, srb, src, srd, sre, srf, cma, cmb, cmc, cmd, cme, cmf;
204204
var alpha = gameObject.alpha;
205-
var tilePositionX = gameObject.tilePositionX / width;
206-
var tilePositionY = gameObject.tilePositionY / height;
205+
var tilePositionX = gameObject.tilePositionX / gameObject.frame.width;
206+
var tilePositionY = gameObject.tilePositionY / gameObject.frame.height;
207207
var texture = gameObject.tileTexture;
208208

209209
tempMatrix.applyITRS(translateX, translateY, rotation, scaleX, scaleY);

0 commit comments

Comments
 (0)