2011-03-07 00:50:44 +00:00
|
|
|
/*
|
2013-01-28 18:03:27 -08:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-03-08 01:41:50 +00:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-01-28 18:03:27 -08:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-03-07 00:50:44 +00:00
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 12:17:45 +02:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-08 01:41:50 +00:00
|
|
|
* All rights reserved.
|
2011-03-07 00:50:44 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-26 15:29:51 +01:00
|
|
|
// 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 16:32:39 +00:00
|
|
|
var CanvasProvider = {
|
2011-02-21 03:17:09 +01:00
|
|
|
canvases: [],
|
2013-02-12 15:53:27 -08:00
|
|
|
|
2013-02-12 16:06:24 -08:00
|
|
|
getCanvas: function(width, height) {
|
2013-02-12 16:23:30 -08:00
|
|
|
var size = height === undefined ? width : Size.create(width, height);
|
2011-02-28 18:30:08 +01:00
|
|
|
if (this.canvases.length) {
|
2011-02-26 15:29:51 +01: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 16:07:44 +00:00
|
|
|
if ((canvas.width != size.width)
|
|
|
|
|| (canvas.height != size.height)) {
|
2011-02-26 15:29:51 +01:00
|
|
|
canvas.width = size.width;
|
|
|
|
canvas.height = size.height;
|
|
|
|
} else {
|
2011-03-02 16:07:44 +00:00
|
|
|
// +1 is needed on some browsers to really clear the borders
|
2011-04-26 15:39:16 +01:00
|
|
|
canvas.getContext('2d').clearRect(0, 0,
|
|
|
|
size.width + 1, size.height + 1);
|
2011-02-26 15:29:51 +01:00
|
|
|
}
|
|
|
|
return canvas;
|
|
|
|
} else {
|
2011-07-26 10:09:31 +01:00
|
|
|
/*#*/ if (options.browser) {
|
2011-02-26 15:29:51 +01:00
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
canvas.width = size.width;
|
|
|
|
canvas.height = size.height;
|
|
|
|
return canvas;
|
2011-07-26 10:09:31 +01:00
|
|
|
/*#*/ } else { // !options.browser
|
2011-07-30 02:04:30 +02:00
|
|
|
return new Canvas(size.width, size.height);
|
2011-07-26 10:09:31 +01:00
|
|
|
/*#*/ } // !options.browser
|
2011-02-26 15:29:51 +01:00
|
|
|
}
|
2011-02-21 03:17:09 +01:00
|
|
|
},
|
|
|
|
|
2013-02-12 16:06:24 -08:00
|
|
|
getContext: function(width, height) {
|
|
|
|
return this.getCanvas(width, height).getContext('2d');
|
|
|
|
},
|
|
|
|
|
|
|
|
// release can receive either a canvas or a context.
|
|
|
|
release: function(obj) {
|
|
|
|
this.canvases.push(obj.canvas ? obj.canvas : obj);
|
|
|
|
},
|
2011-03-03 16:32:39 +00:00
|
|
|
};
|