From fe9853852aae348b08717cae5ad6d01226b5c32d Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Mon, 28 Feb 2011 18:34:39 +0100 Subject: [PATCH] Implement new Doc(size) constructor, Doc#views / Doc#activeView and support DocumentView in Doc#redraw. --- src/document/Doc.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/document/Doc.js b/src/document/Doc.js index c62667a0..df2a5b58 100644 --- a/src/document/Doc.js +++ b/src/document/Doc.js @@ -2,17 +2,25 @@ Doc = Base.extend({ beans: true, initialize: function(canvas) { - if (canvas) { + if (canvas && canvas instanceof HTMLCanvasElement) { this.canvas = canvas; - this.ctx = this.canvas.getContext('2d'); 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); this.activate(); this.layers = []; this.activeLayer = new Layer(); this.currentStyle = null; this.symbols = []; + this.views = [new DocumentView(this)]; + this.activeView = this.views[0]; }, getCurrentStyle: function() { @@ -29,13 +37,18 @@ Doc = Base.extend({ redraw: function() { if (this.canvas) { - // Initial tests conclude that clearing the canvas is always - // faster than using clearRect: - // http://jsperf.com/clearrect-vs-setting-width/7 + // Initial tests conclude that clearing the canvas using clearRect + // is always faster than setting canvas.width = canvas.width + // 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.save(); + view.matrix.applyToContext(this.ctx, true); for (var i = 0, l = this.layers.length; i < l; i++) { this.layers[i].draw(this.ctx, {}); } + this.ctx.restore(); } } });