mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-04 03:45:58 -05:00
Implement new Doc(size) constructor, Doc#views / Doc#activeView and support DocumentView in Doc#redraw.
This commit is contained in:
parent
285f81f363
commit
fe9853852a
1 changed files with 18 additions and 5 deletions
|
@ -2,17 +2,25 @@ Doc = Base.extend({
|
||||||
beans: true,
|
beans: true,
|
||||||
|
|
||||||
initialize: function(canvas) {
|
initialize: function(canvas) {
|
||||||
if (canvas) {
|
if (canvas && canvas instanceof HTMLCanvasElement) {
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.ctx = this.canvas.getContext('2d');
|
|
||||||
this.size = new Size(canvas.offsetWidth, canvas.offsetHeight);
|
this.size = new Size(canvas.offsetWidth, canvas.offsetHeight);
|
||||||
|
} else {
|
||||||
|
this.size = Size.read(arguments) || new Size(1024, 768);
|
||||||
|
this.canvas = document.createElement('canvas');
|
||||||
|
this.canvas.width = this.size.width;
|
||||||
|
this.canvas.height = this.size.height;
|
||||||
}
|
}
|
||||||
|
this.bounds = new Rectangle(new Point(0, 0), this.size);
|
||||||
|
this.ctx = this.canvas.getContext('2d');
|
||||||
Paper.documents.push(this);
|
Paper.documents.push(this);
|
||||||
this.activate();
|
this.activate();
|
||||||
this.layers = [];
|
this.layers = [];
|
||||||
this.activeLayer = new Layer();
|
this.activeLayer = new Layer();
|
||||||
this.currentStyle = null;
|
this.currentStyle = null;
|
||||||
this.symbols = [];
|
this.symbols = [];
|
||||||
|
this.views = [new DocumentView(this)];
|
||||||
|
this.activeView = this.views[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
getCurrentStyle: function() {
|
getCurrentStyle: function() {
|
||||||
|
@ -29,13 +37,18 @@ Doc = Base.extend({
|
||||||
|
|
||||||
redraw: function() {
|
redraw: function() {
|
||||||
if (this.canvas) {
|
if (this.canvas) {
|
||||||
// Initial tests conclude that clearing the canvas is always
|
// Initial tests conclude that clearing the canvas using clearRect
|
||||||
// faster than using clearRect:
|
// is always faster than setting canvas.width = canvas.width
|
||||||
// http://jsperf.com/clearrect-vs-setting-width/7
|
// http://jsperf.com/clearrect-vs-setting-width/
|
||||||
|
var view = this.activeView;
|
||||||
|
var bounds = view.bounds;
|
||||||
this.ctx.clearRect(0, 0, this.size.width + 1, this.size.height + 1);
|
this.ctx.clearRect(0, 0, this.size.width + 1, this.size.height + 1);
|
||||||
|
this.ctx.save();
|
||||||
|
view.matrix.applyToContext(this.ctx, true);
|
||||||
for (var i = 0, l = this.layers.length; i < l; i++) {
|
for (var i = 0, l = this.layers.length; i < l; i++) {
|
||||||
this.layers[i].draw(this.ctx, {});
|
this.layers[i].draw(this.ctx, {});
|
||||||
}
|
}
|
||||||
|
this.ctx.restore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue