From ee4760afc4fe8a424e0ef466c9b4bc22204afb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 27 Jan 2016 09:38:57 +0100 Subject: [PATCH] Fix issue with zoomed retina canvases when repeatetly initializing same view canvas. --- src/view/CanvasView.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/view/CanvasView.js b/src/view/CanvasView.js index d4a29936..f7cf40ae 100644 --- a/src/view/CanvasView.js +++ b/src/view/CanvasView.js @@ -42,25 +42,34 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ + [].slice.call(arguments, 1)); canvas = CanvasProvider.getCanvas(size); } - this._context = canvas.getContext('2d'); + var context = this._context = canvas.getContext('2d'); + // Save context right away, and restore in #remove(). Also restore() and + // save() again in _setViewSize(), to prevent accumulation of scaling. + context.save(); this._pixelRatio = 1; if (!/^off|false$/.test(PaperScope.getAttribute(canvas, 'hidpi'))) { // Hi-DPI Canvas support based on: // http://www.html5rocks.com/en/tutorials/canvas/hidpi/ var deviceRatio = window.devicePixelRatio || 1, - backingStoreRatio = DomElement.getPrefixed(this._context, + backingStoreRatio = DomElement.getPrefixed(context, 'backingStorePixelRatio') || 1; this._pixelRatio = deviceRatio / backingStoreRatio; } View.call(this, project, canvas); }, + remove: function remove() { + this._context.restore(); + return remove.base.call(this); + }, + _setViewSize: function _setViewSize(width, height) { var pixelRatio = this._pixelRatio; // Upscale the canvas if the pixel ratio is more than 1. _setViewSize.base.call(this, width * pixelRatio, height * pixelRatio); if (pixelRatio !== 1) { - var element = this._element; + var element = this._element, + context = this._context; // We need to set the correct size on non-resizable canvases through // their style when HiDPI is active, as otherwise they would appear // too big. @@ -71,7 +80,9 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ } // Scale the context to counter the fact that we've manually scaled // our canvas element. - this._context.scale(pixelRatio, pixelRatio); + context.restore(); + context.save(); + context.scale(pixelRatio, pixelRatio); } },