Skip to content

Commit 466b021

Browse files
committed
TilemapLayer: Re-added secondary copy-canvas
- This change uses a secondary canvas and rectangle clearing instead of a 'copy' composition on the same canvas. This should hopefully address the flickering issue in Safari and Safari Mobile
1 parent 92a20a5 commit 466b021

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

src/tilemap/TilemapLayer.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
7070
*/
7171
this.context = this.canvas.getContext('2d');
7272

73-
// Required for canvas-shifting to avoid alpha artifacts
74-
this.context.globalCompositeOperation = 'copy';
73+
/**
74+
* @property {?HTMLCanvasElement} _copyCanvas
75+
* @private
76+
*/
77+
this._copyCanvas = Phaser.Canvas.create(width, height, '', true);
7578

7679
/**
7780
* Required Pixi var.
@@ -134,6 +137,7 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
134137
this.renderSettings = {
135138

136139
enableScrollDelta: true,
140+
137141
overdrawRatio: 0.20
138142

139143
};
@@ -643,7 +647,8 @@ Phaser.TilemapLayer.prototype.resetTilesetCache = function ()
643647
};
644648

645649
/**
646-
* Shifts the contents of the canvas - does extra math so that different browsers agree on the result. The specified (x/y) will be shifted to (0,0) after the copy. The newly exposed canvas area will need to be filled in. This method is problematic for transparent tiles.
650+
* Shifts the contents of the canvas - does extra math so that different browsers agree on the result.
651+
* The specified (x/y) will be shifted to (0,0) after the copy and the newly exposed canvas area will need to be filled in.
647652
*
648653
* @method Phaser.TilemapLayer#shiftCanvas
649654
* @private
@@ -676,7 +681,25 @@ Phaser.TilemapLayer.prototype.shiftCanvas = function (context, x, y)
676681
sy = 0;
677682
}
678683

679-
context.drawImage(canvas, dx, dy, copyW, copyH, sx, sy, copyW, copyH);
684+
if (!this._copyCanvas)
685+
{
686+
// Flickers in Safari / Safari Mobile
687+
// Ref. https://github.com/photonstorm/phaser/issues/1439
688+
context.save();
689+
context.globalCompositionOperation = 'copy';
690+
context.drawImage(canvas, dx, dy, copyW, copyH, sx, sy, copyW, copyH);
691+
context.restore();
692+
}
693+
else
694+
{
695+
var cpCanvas = this._copyCanvas;
696+
var cpContext = cpCanvas.getContext('2d');
697+
cpContext.clearRect(0, 0, copyW, copyH);
698+
cpContext.drawImage(canvas, dx, dy, copyW, copyH, 0, 0, copyW, copyH);
699+
// clear allows default 'source-over' semantics
700+
context.clearRect(sx, sy, copyW, copyH);
701+
context.drawImage(cpCanvas, 0, 0, copyW, copyH, sx, sy, copyW, copyH);
702+
}
680703
};
681704

682705
/**

0 commit comments

Comments
 (0)