mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Speed up CanvasProvider.
This commit is contained in:
parent
1a6c5c8c6f
commit
9e6fd5a7e7
1 changed files with 24 additions and 9 deletions
|
@ -1,17 +1,32 @@
|
||||||
|
// 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?
|
||||||
CanvasProvider = {
|
CanvasProvider = {
|
||||||
canvases: [],
|
canvases: [],
|
||||||
getCanvas: function(size) {
|
getCanvas: function(size) {
|
||||||
var canvas = this.canvases.length
|
if(this.canvases.length) {
|
||||||
? this.canvases.pop()
|
var canvas = this.canvases.pop();
|
||||||
: document.createElement('canvas');
|
// If they are not the same size, we don't need to clear them
|
||||||
canvas.width = size.width;
|
// using clearRect and visa versa.
|
||||||
canvas.height = size.height;
|
if((canvas.width != size.width) || (canvas.height != size.height)) {
|
||||||
return canvas;
|
canvas.width = size.width;
|
||||||
|
canvas.height = size.height;
|
||||||
|
} else {
|
||||||
|
var context = canvas.getContext('2d');
|
||||||
|
context.clearRect(0, 0, size.width + 1, size.height + 1);
|
||||||
|
}
|
||||||
|
return canvas;
|
||||||
|
} else {
|
||||||
|
var canvas = document.createElement('canvas');
|
||||||
|
canvas.width = size.width;
|
||||||
|
canvas.height = size.height;
|
||||||
|
return canvas;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
returnCanvas: function(canvas) {
|
returnCanvas: function(canvas) {
|
||||||
// reset canvas:
|
|
||||||
canvas.width = canvas.width;
|
|
||||||
this.canvases.push(canvas);
|
this.canvases.push(canvas);
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Reference in a new issue