mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
Shorten CanvasProvider method names.
This commit is contained in:
parent
24d467e292
commit
357b7dbc54
7 changed files with 18 additions and 20 deletions
|
@ -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';
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue