diff --git a/src/color/Color.js b/src/color/Color.js index f3979b7e..d9cecfac 100644 --- a/src/color/Color.js +++ b/src/color/Color.js @@ -62,7 +62,7 @@ var Color = this.Color = Base.extend(new function() { // Use a canvas to draw to with the given name and then retrieve rgb // values from. Build a cache for all the used colors. if (!colorContext) { - var canvas = CanvasProvider.getCanvas(Size.create(1, 1)); + var canvas = CanvasProvider.get(Size.create(1, 1)); colorContext = canvas.getContext('2d'); colorContext.globalCompositeOperation = 'copy'; } diff --git a/src/core/PaperScope.js b/src/core/PaperScope.js index 2775441c..4bad25b9 100644 --- a/src/core/PaperScope.js +++ b/src/core/PaperScope.js @@ -62,12 +62,12 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{ if (!this.support) { // Set up paper.support, as an object containing properties that // describe the support of various features. - var canvas = CanvasProvider.getCanvas(Size.create(1, 1)), + var canvas = CanvasProvider.get(Size.create(1, 1)), ctx = canvas.getContext('2d'); PaperScope.prototype.support = { nativeDash: 'setLineDash' in ctx || 'mozDash' in ctx }; - CanvasProvider.returnCanvas(canvas); + CanvasProvider.release(canvas); } }, diff --git a/src/item/Item.js b/src/item/Item.js index 12315596..ae8376da 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1101,7 +1101,7 @@ var Item = this.Item = Base.extend(Callback, { rasterize: function(resolution) { var bounds = this.getStrokeBounds(), scale = (resolution || 72) / 72, - canvas = CanvasProvider.getCanvas(bounds.getSize().multiply(scale)), + canvas = CanvasProvider.get(bounds.getSize().multiply(scale)), ctx = canvas.getContext('2d'), matrix = new Matrix().scale(scale).translate(-bounds.x, -bounds.y); matrix.applyToContext(ctx); @@ -2575,7 +2575,7 @@ var Item = this.Item = Base.extend(Callback, { // Floor the offset and ceil the size, so we don't cut off any // antialiased pixels when drawing onto the temporary canvas. itemOffset = param.offset = bounds.getTopLeft().floor(); - tempCanvas = CanvasProvider.getCanvas( + tempCanvas = CanvasProvider.get( bounds.getSize().ceil().add(Size.create(1, 1))); // Set ctx to the context of the temporary canvas, // so we draw onto it, instead of the parentCtx @@ -2612,7 +2612,7 @@ var Item = this.Item = Base.extend(Callback, { parentCtx.restore(); } // Return the temporary canvas, so it can be reused - CanvasProvider.returnCanvas(tempCanvas); + CanvasProvider.release(tempCanvas); } } } diff --git a/src/item/Raster.js b/src/item/Raster.js index b24d2c12..b44efa54 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -58,7 +58,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ if (!element) { // If the Raster contains a Canvas object, we need to create // a new one and draw this raster's canvas on it. - element = CanvasProvider.getCanvas(this._size); + element = CanvasProvider.get(this._size); element.getContext('2d').drawImage(this._canvas, 0, 0); } var copy = new Raster(element); @@ -81,7 +81,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ // Get reference to image before changing canvas var element = this.getElement(); // Setting canvas internally sets _size - this.setCanvas(CanvasProvider.getCanvas(size)); + this.setCanvas(CanvasProvider.get(size)); // Draw element back onto new canvas this.getContext(true).drawImage(element, 0, 0, size.width, size.height); @@ -158,7 +158,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ getCanvas: function() { if (!this._canvas) { - var canvas = CanvasProvider.getCanvas(this._size); + var canvas = CanvasProvider.get(this._size); // Since drawimage images into canvases might fail based on security // policies, wrap the call in try-catch and only set _canvas if we // succeeded. @@ -175,7 +175,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ setCanvas: function(canvas) { if (this._canvas) - CanvasProvider.returnCanvas(this._canvas); + CanvasProvider.release(this._canvas); this._canvas = canvas; this._size = Size.create(canvas.width, canvas.height); this._image = null; @@ -195,7 +195,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ setImage: function(image) { if (this._canvas) - CanvasProvider.returnCanvas(this._canvas); + CanvasProvider.release(this._canvas); this._image = image; /*#*/ if (options.browser) { this._size = Size.create(image.naturalWidth, image.naturalHeight); @@ -261,7 +261,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ */ getSubImage: function(rect) { rect = Rectangle.read(arguments); - var canvas = CanvasProvider.getCanvas(rect.getSize()); + var canvas = CanvasProvider.get(rect.getSize()); canvas.getContext('2d').drawImage(this.getCanvas(), rect.x, rect.y, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height); return canvas; @@ -323,7 +323,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ // since it's only 32 x 32 pixels. var ctx = Raster._sampleContext; if (!ctx) { - ctx = Raster._sampleContext = CanvasProvider.getCanvas( + ctx = Raster._sampleContext = CanvasProvider.get( new Size(sampleSize)).getContext('2d'); } else { // Clear the sample canvas: diff --git a/src/text/PointText.js b/src/text/PointText.js index 7904ec16..20349287 100644 --- a/src/text/PointText.js +++ b/src/text/PointText.js @@ -88,7 +88,7 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{ _getBounds: function(getter, matrix) { // Create an in-memory canvas on which to do the measuring if (!context) - context = CanvasProvider.getCanvas( + context = CanvasProvider.get( Size.create(1, 1)).getContext('2d'); var justification = this.getJustification(), x = 0; diff --git a/src/ui/CanvasView.js b/src/ui/CanvasView.js index 7ff93108..deeaa46a 100644 --- a/src/ui/CanvasView.js +++ b/src/ui/CanvasView.js @@ -29,7 +29,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ var size = Size.read(arguments, 1); if (size.isZero()) size = Size.create(1024, 768); - canvas = CanvasProvider.getCanvas(size); + canvas = CanvasProvider.get(size); } this._context = canvas.getContext('2d'); // Have Item count installed mouse events. diff --git a/src/util/CanvasProvider.js b/src/util/CanvasProvider.js index 5bb92663..90d96bdd 100644 --- a/src/util/CanvasProvider.js +++ b/src/util/CanvasProvider.js @@ -10,14 +10,12 @@ * All rights reserved. */ -// TODO: It might be better to make a ContextProvider class, since you -// can always find the canvas through context.canvas. This saves code and -// speed by not having to do canvas.getContext('2d') // TODO: Run through the canvas array to find a canvas with the requested // width / height, so we don't need to resize it? var CanvasProvider = { canvases: [], - getCanvas: function(size) { + + get: function(size) { if (this.canvases.length) { var canvas = this.canvases.pop(); // If they are not the same size, we don't need to clear them @@ -44,7 +42,7 @@ var CanvasProvider = { } }, - returnCanvas: function(canvas) { + release: function(canvas) { this.canvases.push(canvas); } };