2011-03-06 19:50:44 -05:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: It might be better to make a ContextProvider class, since you
|
|
|
|
// can always find the canvas through context.canvas. This saves code and
|
2011-02-26 09:29:51 -05:00
|
|
|
// 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?
|
2011-03-03 11:32:39 -05:00
|
|
|
var CanvasProvider = {
|
2011-02-20 21:17:09 -05:00
|
|
|
canvases: [],
|
2011-02-23 19:36:38 -05:00
|
|
|
getCanvas: function(size) {
|
2011-02-28 12:30:08 -05:00
|
|
|
if (this.canvases.length) {
|
2011-02-26 09:29:51 -05:00
|
|
|
var canvas = this.canvases.pop();
|
|
|
|
// If they are not the same size, we don't need to clear them
|
|
|
|
// using clearRect and visa versa.
|
2011-03-02 11:07:44 -05:00
|
|
|
if ((canvas.width != size.width)
|
|
|
|
|| (canvas.height != size.height)) {
|
2011-02-26 09:29:51 -05:00
|
|
|
canvas.width = size.width;
|
|
|
|
canvas.height = size.height;
|
|
|
|
} else {
|
2011-03-02 11:07:44 -05:00
|
|
|
// +1 is needed on some browsers to really clear the borders
|
2011-04-26 10:39:16 -04:00
|
|
|
canvas.getContext('2d').clearRect(0, 0,
|
|
|
|
size.width + 1, size.height + 1);
|
2011-02-26 09:29:51 -05:00
|
|
|
}
|
|
|
|
return canvas;
|
|
|
|
} else {
|
2011-07-26 05:09:31 -04:00
|
|
|
/*#*/ if (options.browser) {
|
2011-02-26 09:29:51 -05:00
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
canvas.width = size.width;
|
|
|
|
canvas.height = size.height;
|
|
|
|
return canvas;
|
2011-07-26 05:09:31 -04:00
|
|
|
/*#*/ } else { // !options.browser
|
2011-07-29 20:04:30 -04:00
|
|
|
return new Canvas(size.width, size.height);
|
2011-07-26 05:09:31 -04:00
|
|
|
/*#*/ } // !options.browser
|
2011-02-26 09:29:51 -05:00
|
|
|
}
|
2011-02-20 21:17:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
returnCanvas: function(canvas) {
|
|
|
|
this.canvases.push(canvas);
|
|
|
|
}
|
2011-03-03 11:32:39 -05:00
|
|
|
};
|