2011-03-07 00:50:44 +00:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
2011-03-08 01:41:50 +00:00
|
|
|
* http://paperjs.org/
|
2011-03-07 00:50:44 +00:00
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
2011-03-08 01:41:50 +00:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-07 00:50:44 +00:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
2011-03-08 01:41:50 +00:00
|
|
|
* All rights reserved.
|
2011-03-07 00:50:44 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-04 13:34:31 +00:00
|
|
|
var Document = this.Document = Base.extend({
|
2011-02-16 22:09:51 +01:00
|
|
|
beans: true,
|
|
|
|
|
2011-02-07 19:28:09 +01:00
|
|
|
initialize: function(canvas) {
|
2011-02-28 18:34:39 +01:00
|
|
|
if (canvas && canvas instanceof HTMLCanvasElement) {
|
2011-02-11 18:38:44 +01:00
|
|
|
this.canvas = canvas;
|
|
|
|
this.size = new Size(canvas.offsetWidth, canvas.offsetHeight);
|
2011-02-28 18:34:39 +01:00
|
|
|
} 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;
|
2011-02-11 18:38:44 +01:00
|
|
|
}
|
2011-02-28 18:34:39 +01:00
|
|
|
this.bounds = new Rectangle(new Point(0, 0), this.size);
|
2011-03-05 01:26:12 +00:00
|
|
|
this.context = this.canvas.getContext('2d');
|
2011-03-03 16:21:17 +00:00
|
|
|
paper.documents.push(this);
|
2011-02-12 16:20:10 +01:00
|
|
|
this.activate();
|
|
|
|
this.layers = [];
|
2011-02-11 18:38:44 +01:00
|
|
|
this.activeLayer = new Layer();
|
2011-03-05 00:03:28 +00:00
|
|
|
this.setCurrentStyle(null);
|
2011-02-20 18:34:38 +01:00
|
|
|
this.symbols = [];
|
2011-02-28 18:34:39 +01:00
|
|
|
this.views = [new DocumentView(this)];
|
|
|
|
this.activeView = this.views[0];
|
2011-02-07 19:28:09 +01:00
|
|
|
},
|
2011-02-16 22:09:51 +01:00
|
|
|
|
|
|
|
getCurrentStyle: function() {
|
|
|
|
return this._currentStyle;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCurrentStyle: function(style) {
|
|
|
|
this._currentStyle = new PathStyle(this, style);
|
|
|
|
},
|
|
|
|
|
2011-02-12 16:20:10 +01:00
|
|
|
activate: function() {
|
2011-03-03 16:21:17 +00:00
|
|
|
var index = paper.documents.indexOf(this);
|
|
|
|
if (index != -1) {
|
|
|
|
paper.document = this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-02-12 16:20:10 +01:00
|
|
|
},
|
2011-02-16 22:09:51 +01:00
|
|
|
|
2011-03-03 12:19:43 +00:00
|
|
|
draw: function() {
|
|
|
|
if (this.canvas) {
|
|
|
|
// 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/7
|
2011-03-08 12:55:52 +00:00
|
|
|
this.context.clearRect(0, 0,
|
|
|
|
this.size.width + 1, this.size.height + 1);
|
2011-03-05 01:26:12 +00:00
|
|
|
this.context.save();
|
2011-03-08 12:55:52 +00:00
|
|
|
var param = { offset: new Point(0, 0) };
|
2011-03-03 12:19:43 +00:00
|
|
|
for (var i = 0, l = this.layers.length; i < l; i++) {
|
2011-03-08 12:55:52 +00:00
|
|
|
Item.draw(this.layers[i], this.context, param);
|
2011-03-03 12:19:43 +00:00
|
|
|
}
|
2011-03-05 01:26:12 +00:00
|
|
|
this.context.restore();
|
2011-03-03 12:19:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-02-08 13:15:27 +01:00
|
|
|
redraw: function() {
|
2011-03-03 12:19:43 +00:00
|
|
|
this.draw();
|
2011-02-07 19:28:09 +01:00
|
|
|
}
|
2011-02-13 16:26:24 +00:00
|
|
|
});
|