forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasProvider.js
More file actions
60 lines (55 loc) · 1.81 KB
/
Copy pathCanvasProvider.js
File metadata and controls
60 lines (55 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
// 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(width, height) {
var size = height === undefined ? width : new Size(width, height),
canvas,
init = true;
if (this.canvases.length) {
canvas = this.canvases.pop();
} else {
/*#*/ if (options.environment == 'browser') {
canvas = document.createElement('canvas');
/*#*/ } else { // !options.environment == 'browser'
canvas = new Canvas(size.width, size.height);
init = false; // It's already initialized through constructor.
/*#*/ } // !options.environment == 'browser'
}
var ctx = canvas.getContext('2d');
// We save on retrieval and restore on release.
ctx.save();
// If they are not the same size, we don't need to clear them
// using clearRect and visa versa.
if (canvas.width === size.width && canvas.height === size.height) {
// +1 is needed on some browsers to really clear the borders
if (init)
ctx.clearRect(0, 0, size.width + 1, size.height + 1);
} else {
canvas.width = size.width;
canvas.height = size.height;
}
return canvas;
},
getContext: function(width, height) {
return this.getCanvas(width, height).getContext('2d');
},
// release can receive either a canvas or a context.
release: function(obj) {
var canvas = obj.canvas ? obj.canvas : obj;
// We restore contexts on release(), see getCanvas()
canvas.getContext('2d').restore();
this.canvases.push(canvas);
}
};