Shorten CanvasProvider method names.

This commit is contained in:
Jürg Lehni 2013-02-12 15:53:27 -08:00
parent 24d467e292
commit 357b7dbc54
7 changed files with 18 additions and 20 deletions

View file

@ -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 // Use a canvas to draw to with the given name and then retrieve rgb
// values from. Build a cache for all the used colors. // values from. Build a cache for all the used colors.
if (!colorContext) { if (!colorContext) {
var canvas = CanvasProvider.getCanvas(Size.create(1, 1)); var canvas = CanvasProvider.get(Size.create(1, 1));
colorContext = canvas.getContext('2d'); colorContext = canvas.getContext('2d');
colorContext.globalCompositeOperation = 'copy'; colorContext.globalCompositeOperation = 'copy';
} }

View file

@ -62,12 +62,12 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
if (!this.support) { if (!this.support) {
// Set up paper.support, as an object containing properties that // Set up paper.support, as an object containing properties that
// describe the support of various features. // 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'); ctx = canvas.getContext('2d');
PaperScope.prototype.support = { PaperScope.prototype.support = {
nativeDash: 'setLineDash' in ctx || 'mozDash' in ctx nativeDash: 'setLineDash' in ctx || 'mozDash' in ctx
}; };
CanvasProvider.returnCanvas(canvas); CanvasProvider.release(canvas);
} }
}, },

View file

@ -1101,7 +1101,7 @@ var Item = this.Item = Base.extend(Callback, {
rasterize: function(resolution) { rasterize: function(resolution) {
var bounds = this.getStrokeBounds(), var bounds = this.getStrokeBounds(),
scale = (resolution || 72) / 72, scale = (resolution || 72) / 72,
canvas = CanvasProvider.getCanvas(bounds.getSize().multiply(scale)), canvas = CanvasProvider.get(bounds.getSize().multiply(scale)),
ctx = canvas.getContext('2d'), ctx = canvas.getContext('2d'),
matrix = new Matrix().scale(scale).translate(-bounds.x, -bounds.y); matrix = new Matrix().scale(scale).translate(-bounds.x, -bounds.y);
matrix.applyToContext(ctx); 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 // Floor the offset and ceil the size, so we don't cut off any
// antialiased pixels when drawing onto the temporary canvas. // antialiased pixels when drawing onto the temporary canvas.
itemOffset = param.offset = bounds.getTopLeft().floor(); itemOffset = param.offset = bounds.getTopLeft().floor();
tempCanvas = CanvasProvider.getCanvas( tempCanvas = CanvasProvider.get(
bounds.getSize().ceil().add(Size.create(1, 1))); bounds.getSize().ceil().add(Size.create(1, 1)));
// Set ctx to the context of the temporary canvas, // Set ctx to the context of the temporary canvas,
// so we draw onto it, instead of the parentCtx // so we draw onto it, instead of the parentCtx
@ -2612,7 +2612,7 @@ var Item = this.Item = Base.extend(Callback, {
parentCtx.restore(); parentCtx.restore();
} }
// Return the temporary canvas, so it can be reused // Return the temporary canvas, so it can be reused
CanvasProvider.returnCanvas(tempCanvas); CanvasProvider.release(tempCanvas);
} }
} }
} }

View file

@ -58,7 +58,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
if (!element) { if (!element) {
// If the Raster contains a Canvas object, we need to create // If the Raster contains a Canvas object, we need to create
// a new one and draw this raster's canvas on it. // 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); element.getContext('2d').drawImage(this._canvas, 0, 0);
} }
var copy = new Raster(element); var copy = new Raster(element);
@ -81,7 +81,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
// Get reference to image before changing canvas // Get reference to image before changing canvas
var element = this.getElement(); var element = this.getElement();
// Setting canvas internally sets _size // Setting canvas internally sets _size
this.setCanvas(CanvasProvider.getCanvas(size)); this.setCanvas(CanvasProvider.get(size));
// Draw element back onto new canvas // Draw element back onto new canvas
this.getContext(true).drawImage(element, 0, 0, this.getContext(true).drawImage(element, 0, 0,
size.width, size.height); size.width, size.height);
@ -158,7 +158,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
getCanvas: function() { getCanvas: function() {
if (!this._canvas) { 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 // Since drawimage images into canvases might fail based on security
// policies, wrap the call in try-catch and only set _canvas if we // policies, wrap the call in try-catch and only set _canvas if we
// succeeded. // succeeded.
@ -175,7 +175,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
setCanvas: function(canvas) { setCanvas: function(canvas) {
if (this._canvas) if (this._canvas)
CanvasProvider.returnCanvas(this._canvas); CanvasProvider.release(this._canvas);
this._canvas = canvas; this._canvas = canvas;
this._size = Size.create(canvas.width, canvas.height); this._size = Size.create(canvas.width, canvas.height);
this._image = null; this._image = null;
@ -195,7 +195,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
setImage: function(image) { setImage: function(image) {
if (this._canvas) if (this._canvas)
CanvasProvider.returnCanvas(this._canvas); CanvasProvider.release(this._canvas);
this._image = image; this._image = image;
/*#*/ if (options.browser) { /*#*/ if (options.browser) {
this._size = Size.create(image.naturalWidth, image.naturalHeight); this._size = Size.create(image.naturalWidth, image.naturalHeight);
@ -261,7 +261,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
*/ */
getSubImage: function(rect) { getSubImage: function(rect) {
rect = Rectangle.read(arguments); 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.getContext('2d').drawImage(this.getCanvas(), rect.x, rect.y,
canvas.width, canvas.height, 0, 0, canvas.width, canvas.height); canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
return canvas; return canvas;
@ -323,7 +323,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
// since it's only 32 x 32 pixels. // since it's only 32 x 32 pixels.
var ctx = Raster._sampleContext; var ctx = Raster._sampleContext;
if (!ctx) { if (!ctx) {
ctx = Raster._sampleContext = CanvasProvider.getCanvas( ctx = Raster._sampleContext = CanvasProvider.get(
new Size(sampleSize)).getContext('2d'); new Size(sampleSize)).getContext('2d');
} else { } else {
// Clear the sample canvas: // Clear the sample canvas:

View file

@ -88,7 +88,7 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{
_getBounds: function(getter, matrix) { _getBounds: function(getter, matrix) {
// Create an in-memory canvas on which to do the measuring // Create an in-memory canvas on which to do the measuring
if (!context) if (!context)
context = CanvasProvider.getCanvas( context = CanvasProvider.get(
Size.create(1, 1)).getContext('2d'); Size.create(1, 1)).getContext('2d');
var justification = this.getJustification(), var justification = this.getJustification(),
x = 0; x = 0;

View file

@ -29,7 +29,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
var size = Size.read(arguments, 1); var size = Size.read(arguments, 1);
if (size.isZero()) if (size.isZero())
size = Size.create(1024, 768); size = Size.create(1024, 768);
canvas = CanvasProvider.getCanvas(size); canvas = CanvasProvider.get(size);
} }
this._context = canvas.getContext('2d'); this._context = canvas.getContext('2d');
// Have Item count installed mouse events. // Have Item count installed mouse events.

View file

@ -10,14 +10,12 @@
* All rights reserved. * 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 // TODO: Run through the canvas array to find a canvas with the requested
// width / height, so we don't need to resize it? // width / height, so we don't need to resize it?
var CanvasProvider = { var CanvasProvider = {
canvases: [], canvases: [],
getCanvas: function(size) {
get: function(size) {
if (this.canvases.length) { if (this.canvases.length) {
var canvas = this.canvases.pop(); var canvas = this.canvases.pop();
// If they are not the same size, we don't need to clear them // 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); this.canvases.push(canvas);
} }
}; };