Skip to content

Commit 500da5d

Browse files
committed
Canvas GeometryMask rendering
1 parent 658e8c0 commit 500da5d

4 files changed

Lines changed: 71 additions & 11 deletions

File tree

v3/src/display/mask/BitmapMask.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ var BitmapMask = new Class({
144144

145145
// Apply alpha masking using mask renderer
146146
maskRenderer.draw(null, null, this.mainTexture, this.maskTexture);
147+
},
148+
149+
preRenderCanvas: function (renderer, mask, camera)
150+
{
151+
// NOOP
152+
},
153+
154+
postRenderCanvas: function (renderer)
155+
{
156+
// NOOP
147157
}
148158

149159
});

v3/src/display/mask/GeometryMask.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ var GeometryMask = new Class({
4646
// Force flush before disabling stencil test
4747
renderer.currentRenderer.flush();
4848
gl.disable(gl.STENCIL_TEST);
49+
},
50+
51+
preRenderCanvas: function (renderer, mask, camera)
52+
{
53+
var geometryMask = this.geometryMask;
54+
renderer.currentContext.save();
55+
//renderer.currentContext.beginPath();
56+
geometryMask.renderCanvas(renderer, geometryMask, 0.0, camera, null, true);
57+
renderer.currentContext.clip();
58+
},
59+
60+
postRenderCanvas: function (renderer)
61+
{
62+
//renderer.currentContext.closePath();
63+
renderer.currentContext.restore();
4964
}
5065

5166
});

v3/src/gameobjects/graphics/GraphicsCanvasRenderer.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var Commands = require('./Commands');
22
var GameObject = require('../GameObject');
33

4-
var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, camera, renderTargetCtx)
4+
var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, camera, renderTargetCtx, allowClip)
55
{
66
if (GameObject.RENDER_MASK !== src.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id)))
77
{
@@ -102,20 +102,38 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
102102
break;
103103

104104
case Commands.FILL_PATH:
105-
ctx.fill();
105+
if (!allowClip)
106+
{
107+
ctx.fill();
108+
}
106109
break;
107110

108111
case Commands.STROKE_PATH:
109-
ctx.stroke();
112+
if (!allowClip)
113+
{
114+
ctx.stroke();
115+
}
110116
break;
111117

112118
case Commands.FILL_RECT:
113-
ctx.fillRect(
114-
commandBuffer[index + 1],
115-
commandBuffer[index + 2],
116-
commandBuffer[index + 3],
117-
commandBuffer[index + 4]
118-
);
119+
if (!allowClip)
120+
{
121+
ctx.fillRect(
122+
commandBuffer[index + 1],
123+
commandBuffer[index + 2],
124+
commandBuffer[index + 3],
125+
commandBuffer[index + 4]
126+
);
127+
}
128+
else
129+
{
130+
ctx.rect(
131+
commandBuffer[index + 1],
132+
commandBuffer[index + 2],
133+
commandBuffer[index + 3],
134+
commandBuffer[index + 4]
135+
);
136+
}
119137
index += 4;
120138
break;
121139

@@ -125,7 +143,10 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
125143
ctx.lineTo(commandBuffer[index + 3], commandBuffer[index + 4]);
126144
ctx.lineTo(commandBuffer[index + 5], commandBuffer[index + 6]);
127145
ctx.closePath();
128-
ctx.fill();
146+
if (!allowClip)
147+
{
148+
ctx.fill();
149+
}
129150
index += 6;
130151
break;
131152

@@ -135,7 +156,10 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
135156
ctx.lineTo(commandBuffer[index + 3], commandBuffer[index + 4]);
136157
ctx.lineTo(commandBuffer[index + 5], commandBuffer[index + 6]);
137158
ctx.closePath();
138-
ctx.stroke();
159+
if (!allowClip)
160+
{
161+
ctx.stroke();
162+
}
139163
index += 6;
140164
break;
141165

v3/src/renderer/canvas/CanvasRenderer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,18 @@ var CanvasRenderer = new Class({
215215
{
216216
var child = list[c];
217217

218+
if (child.mask)
219+
{
220+
child.mask.preRenderCanvas(this, child, camera);
221+
}
222+
218223
child.renderCanvas(this, child, interpolationPercentage, camera);
224+
225+
if (child.mask)
226+
{
227+
child.mask.postRenderCanvas(this, child, camera);
228+
}
229+
219230
}
220231

221232
// Call the Scene.render function

0 commit comments

Comments
 (0)