Skip to content

Commit e0f0b35

Browse files
committed
Canvas camera scissor
1 parent 2510bee commit e0f0b35

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

v3/src/renderer/canvas/CanvasRenderer.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,27 @@ CanvasRenderer.prototype = {
142142
* by the amount of time that will be simulated the next time update()
143143
* runs. Useful for interpolating frames.
144144
*/
145-
render: function (state, list, interpolationPercentage)
145+
render: function (state, list, interpolationPercentage, camera)
146146
{
147147
var w = state.sys.width;
148148
var h = state.sys.height;
149149
var ctx = state.sys.context;
150150
var settings = state.sys.settings;
151-
151+
var scissor = (camera.x !== 0 || camera.y !== 0 || camera.width !== ctx.canvas.width || camera.height !== ctx.canvas.height);
152152
this.currentContext = ctx;
153153

154154
ctx.setTransform(1, 0, 0, 1, 0, 0);
155155

156156
// If the alpha or blend mode didn't change since the last render, then don't set them again (saves 2 ops)
157157

158+
if (scissor)
159+
{
160+
ctx.beginPath();
161+
ctx.rect(camera.x, camera.y, camera.width, camera.height);
162+
ctx.clip();
163+
ctx.closePath();
164+
}
165+
158166
if (this.currentAlpha !== 1)
159167
{
160168
ctx.globalAlpha = 1;

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,13 @@ WebGLRenderer.prototype = {
261261
{
262262
// Could move to the State Systems or MainLoop
263263
var gl = this.gl;
264+
var scissor = (camera.x !== 0 || camera.y !== 0 || camera.width !== gl.canvas.width || camera.height !== gl.canvas.height);
264265

265-
gl.enable(gl.SCISSOR_TEST);
266-
gl.scissor(camera.x, (gl.drawingBufferWidth - camera.y - camera.height), camera.width, camera.height);
266+
if (scissor)
267+
{
268+
gl.enable(gl.SCISSOR_TEST);
269+
gl.scissor(camera.x, (gl.drawingBufferHeight - camera.y - camera.height), camera.width, camera.height);
270+
}
267271
// We could either clear color or render a quad
268272
gl.clear(gl.COLOR_BUFFER_BIT);
269273

@@ -302,18 +306,19 @@ WebGLRenderer.prototype = {
302306
batch.flush();
303307
}
304308
}
305-
batch.flush();
306-
gl.disable(gl.SCISSOR_TEST);
307-
},
308-
309-
// Called at the end of the render loop (tidy things up, etc)
310-
postRender: function ()
311-
{
312309
if (this.batch)
313310
{
314311
this.batch.flush();
315312
}
313+
if (scissor)
314+
{
315+
gl.disable(gl.SCISSOR_TEST);
316+
}
317+
},
316318

319+
// Called at the end of the render loop (tidy things up, etc)
320+
postRender: function ()
321+
{
317322
// Add Post-render hook
318323

319324
// console.log('%c render end ', 'color: #ffffff; background: #ff0000;');

0 commit comments

Comments
 (0)