From a51745447b613e8f9b25df2a959694b378636bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 17 Jun 2013 15:24:57 -0700 Subject: [PATCH] Restructure CanvasProvider code and call save() and restore() upon retrieval / release. --- src/canvas/CanvasProvider.js | 46 ++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/canvas/CanvasProvider.js b/src/canvas/CanvasProvider.js index bb6d4985..c64368d4 100644 --- a/src/canvas/CanvasProvider.js +++ b/src/canvas/CanvasProvider.js @@ -16,31 +16,34 @@ var CanvasProvider = { canvases: [], getCanvas: function(width, height) { - var size = height === undefined ? width : new Size(width, height); + var size = height === undefined ? width : new Size(width, height), + canvas, + init = true; if (this.canvases.length) { - var canvas = this.canvases.pop(); - // If they are not the same size, we don't need to clear them - // using clearRect and visa versa. - if ((canvas.width != size.width) - || (canvas.height != size.height)) { - canvas.width = size.width; - canvas.height = size.height; - } else { - // +1 is needed on some browsers to really clear the borders - canvas.getContext('2d').clearRect(0, 0, - size.width + 1, size.height + 1); - } - return canvas; + canvas = this.canvases.pop(); } else { /*#*/ if (options.browser) { - var canvas = document.createElement('canvas'); + canvas = document.createElement('canvas'); +/*#*/ } else { // !options.browser + canvas = new Canvas(size.width, size.height); + init = false; // It's already initialized through constructor. +/*#*/ } // !options.browser + + } + var ctx = canvas.getContext('2d'); + // We save on retrieval and restore on release. + ctx.save(); + // If they are not the same size, we don't need to clear them + // using clearRect and visa versa. + if (canvas.width === size.width && canvas.height === size.height) { + // +1 is needed on some browsers to really clear the borders + if (init) + ctx.clearRect(0, 0, size.width + 1, size.height + 1); + } else { canvas.width = size.width; canvas.height = size.height; - return canvas; -/*#*/ } else { // !options.browser - return new Canvas(size.width, size.height); -/*#*/ } // !options.browser } + return canvas; }, getContext: function(width, height) { @@ -49,6 +52,9 @@ var CanvasProvider = { // release can receive either a canvas or a context. release: function(obj) { - this.canvases.push(obj.canvas ? obj.canvas : obj); + var canvas = obj.canvas ? obj.canvas : obj; + // We restore contexts on release(), see getCanvas() + canvas.getContext('2d').restore(); + this.canvases.push(canvas); } };