paper.js/src/document/Document.js

148 lines
3.8 KiB
JavaScript
Raw Normal View History

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
*/
var Document = this.Document = Base.extend({
beans: true,
2011-02-07 19:28:09 +01:00
initialize: function(canvas) {
if (canvas && canvas instanceof HTMLCanvasElement) {
this.canvas = canvas;
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.context = this.canvas.getContext('2d');
paper.documents.push(this);
this.activate();
this.layers = [];
this.activeLayer = new Layer();
this.setCurrentStyle(null);
this.symbols = [];
this.views = [new DocumentView(this)];
this.activeView = this.views[0];
this._selectedItems = {};
this._selectedItemCount = 0;
2011-02-07 19:28:09 +01:00
},
getCurrentStyle: function() {
return this._currentStyle;
},
setCurrentStyle: function(style) {
this._currentStyle = new PathStyle(this, style);
},
activate: function() {
var index = paper.documents.indexOf(this);
if (index != -1) {
paper.document = this;
return true;
}
return false;
},
2011-04-21 18:10:47 +02:00
getSelectedItems: function() {
// TODO: return groups if their children are all selected,
// and filter out their children from the list.
2011-04-23 14:32:21 +02:00
// TODO: the order of these items should be that of their
// drawing order.
var items = [];
Base.each(this._selectedItems, function(item) {
items.push(item);
});
return items;
2011-04-21 18:10:47 +02:00
},
2011-04-21 18:10:47 +02:00
// TODO: implement setSelectedItems?
_selectItem: function(item, select) {
if (select) {
this._selectedItemCount++;
this._selectedItems[item.getId()] = item;
} else {
this._selectedItemCount--;
delete this._selectedItems[item.getId()];
}
},
draw: function() {
if (this.canvas) {
var ctx = this.context;
ctx.save();
2011-04-26 17:04:15 +01:00
// TODO: Remove dirty rectangle test code once it's actually
// implemented.
var testDirtyRects = false;
if (testDirtyRects) {
var left = this.size.width / 8,
top = this.size.height / 8;
function clear(rect) {
ctx.clearRect(rect.x, rect.y, rect.width, rect.height);
if (true) {
ctx.moveTo(rect.x, rect.y);
ctx.lineTo(rect.x + rect.width, rect.y);
ctx.lineTo(rect.x + rect.width, rect.y + rect.height);
ctx.lineTo(rect.x, rect.y + rect.height);
}
}
ctx.beginPath();
clear(Rectangle.create(left, top, 2 * left, 2 * top));
clear(Rectangle.create(3 * left, 3 * top, 2 * left, 2 * top));
// clear(Rectangle.create(left, top, 4 * left, 4 * top));
ctx.closePath();
ctx.clip();
} else {
// 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
ctx.clearRect(0, 0, this.size.width + 1, this.size.height + 1);
}
var param = { offset: new Point(0, 0) };
2011-04-21 15:48:21 +02:00
for (var i = 0, l = this.layers.length; i < l; i++)
Item.draw(this.layers[i], ctx, param);
ctx.restore();
2011-04-21 15:48:21 +02:00
// Draw the selection of the selected items in the document:
if (this._selectedItemCount > 0) {
ctx.save();
ctx.strokeWidth = 1;
// TODO: use Layer#color
ctx.strokeStyle = ctx.fillStyle = '#4f7aff';
2011-04-21 15:48:21 +02:00
param = { selection: true };
Base.each(this._selectedItems, function(item) {
item.draw(ctx, param);
});
ctx.restore();
}
}
},
2011-02-08 13:15:27 +01:00
redraw: function() {
this.draw();
2011-02-07 19:28:09 +01:00
}
});