2011-05-16 08:33:15 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-16 08:33:15 -04:00
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-16 08:33:15 -04:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-05-16 08:33:15 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-06-30 06:01:51 -04:00
|
|
|
/**
|
2011-06-22 18:56:05 -04:00
|
|
|
* @name Project
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-27 17:02:29 -04:00
|
|
|
* @class A Project object in Paper.js is what usually is referred to as the
|
2011-06-27 08:43:39 -04:00
|
|
|
* document: The top level object that holds all the items contained in the
|
|
|
|
* scene graph. As the term document is already taken in the browser context,
|
|
|
|
* it is called Project.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-27 08:43:39 -04:00
|
|
|
* Projects allow the manipluation of the styles that are applied to all newly
|
|
|
|
* created items, give access to the selected items, and will in future versions
|
|
|
|
* offer ways to query for items in the scene graph defining specific
|
|
|
|
* requirements, and means to persist and load from different formats, such as
|
|
|
|
* SVG and PDF.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-27 17:02:29 -04:00
|
|
|
* The currently active project can be accessed through the
|
|
|
|
* {@link PaperScope#project} variable.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-27 17:02:29 -04:00
|
|
|
* An array of all open projects is accessible through the
|
|
|
|
* {@link PaperScope#projects} variable.
|
2011-06-22 18:56:05 -04:00
|
|
|
*/
|
|
|
|
var Project = this.Project = Base.extend(/** @lends Project# */{
|
2011-05-17 08:25:46 -04:00
|
|
|
// TODO: Add arguments to define pages
|
2011-05-23 12:13:03 -04:00
|
|
|
/**
|
2011-06-27 08:43:39 -04:00
|
|
|
* Creates a Paper.js project.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-27 08:43:39 -04:00
|
|
|
* When working with PaperScript, a project is automatically created for us
|
2011-07-27 17:02:29 -04:00
|
|
|
* and the {@link PaperScope#project} variable points to it.
|
2011-05-23 12:13:03 -04:00
|
|
|
*/
|
2011-05-17 08:25:46 -04:00
|
|
|
initialize: function() {
|
2011-05-16 08:33:15 -04:00
|
|
|
// Store reference to the currently active global paper scope:
|
|
|
|
this._scope = paper;
|
|
|
|
// Push it onto this._scope.projects and set index:
|
|
|
|
this._index = this._scope.projects.push(this) - 1;
|
2011-06-20 09:58:48 -04:00
|
|
|
this._currentStyle = new PathStyle();
|
2011-05-20 20:05:22 -04:00
|
|
|
this._selectedItems = {};
|
|
|
|
this._selectedItemCount = 0;
|
2011-05-16 08:33:15 -04:00
|
|
|
// Activate straight away so paper.project is set, as required by
|
|
|
|
// Layer and DoumentView constructors.
|
|
|
|
this.activate();
|
|
|
|
this.layers = [];
|
|
|
|
this.symbols = [];
|
|
|
|
this.activeLayer = new Layer();
|
|
|
|
},
|
|
|
|
|
2011-06-19 18:03:18 -04:00
|
|
|
_needsRedraw: function() {
|
|
|
|
if (this._scope)
|
|
|
|
this._scope._needsRedraw();
|
2011-06-19 16:49:26 -04:00
|
|
|
},
|
|
|
|
|
2011-05-23 12:13:03 -04:00
|
|
|
/**
|
|
|
|
* The currently active path style. All selected items and newly
|
|
|
|
* created items will be styled with this style.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-23 12:13:03 -04:00
|
|
|
* @type PathStyle
|
|
|
|
* @bean
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-10 07:44:30 -04:00
|
|
|
* @example {@paperscript}
|
2011-05-30 13:42:17 -04:00
|
|
|
* project.currentStyle = {
|
|
|
|
* fillColor: 'red',
|
|
|
|
* strokeColor: 'black',
|
|
|
|
* strokeWidth: 5
|
|
|
|
* }
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-10 07:44:30 -04:00
|
|
|
* // The following paths will take over all style properties of
|
2011-05-30 13:42:17 -04:00
|
|
|
* // the current style:
|
2011-06-10 07:44:30 -04:00
|
|
|
* var path = new Path.Circle(new Point(75, 50), 30);
|
|
|
|
* var path2 = new Path.Circle(new Point(175, 50), 20);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-10 07:44:30 -04:00
|
|
|
* @example {@paperscript}
|
2011-05-30 13:42:17 -04:00
|
|
|
* project.currentStyle.fillColor = 'red';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-30 13:42:17 -04:00
|
|
|
* // The following path will take over the fill color we just set:
|
2011-06-10 07:44:30 -04:00
|
|
|
* var path = new Path.Circle(new Point(75, 50), 30);
|
|
|
|
* var path2 = new Path.Circle(new Point(175, 50), 20);
|
2011-05-23 12:13:03 -04:00
|
|
|
*/
|
2011-05-16 08:33:15 -04:00
|
|
|
getCurrentStyle: function() {
|
|
|
|
return this._currentStyle;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCurrentStyle: function(style) {
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: Style selected items with the style:
|
2011-05-16 14:21:36 -04:00
|
|
|
this._currentStyle.initialize(style);
|
2011-05-16 08:33:15 -04:00
|
|
|
},
|
|
|
|
|
2011-05-23 12:13:03 -04:00
|
|
|
/**
|
|
|
|
* Activates this project, so all newly created items will be placed
|
|
|
|
* in it.
|
|
|
|
*/
|
2011-05-16 08:33:15 -04:00
|
|
|
activate: function() {
|
2011-06-19 11:08:51 -04:00
|
|
|
if (this._scope) {
|
2011-05-16 08:33:15 -04:00
|
|
|
this._scope.project = this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2011-06-27 08:43:28 -04:00
|
|
|
/**
|
2011-07-27 17:02:29 -04:00
|
|
|
* Removes this project from the {@link PaperScope#projects} list.
|
2011-06-27 08:43:28 -04:00
|
|
|
*/
|
2011-05-16 08:33:15 -04:00
|
|
|
remove: function() {
|
2011-06-19 11:08:51 -04:00
|
|
|
if (this._scope) {
|
|
|
|
Base.splice(this._scope.projects, null, this._index, 1);
|
2011-06-27 08:43:28 -04:00
|
|
|
// Clear the active project reference if it was pointint to this.
|
|
|
|
if (this._scope.project == this)
|
|
|
|
this._scope.project = null;
|
2011-06-19 11:08:51 -04:00
|
|
|
this._scope = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-05-16 08:33:15 -04:00
|
|
|
},
|
|
|
|
|
2011-05-23 12:13:03 -04:00
|
|
|
/**
|
2011-07-27 17:02:29 -04:00
|
|
|
* The index of the project in the {@link PaperScope#projects} list.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-27 14:15:15 -04:00
|
|
|
* @type Number
|
2011-05-23 12:13:03 -04:00
|
|
|
* @bean
|
|
|
|
*/
|
2011-05-16 08:33:15 -04:00
|
|
|
getIndex: function() {
|
|
|
|
return this._index;
|
|
|
|
},
|
|
|
|
|
2011-05-23 12:13:03 -04:00
|
|
|
/**
|
|
|
|
* The selected items contained within the project.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-26 10:49:19 -04:00
|
|
|
* @type Item[]
|
2011-05-23 12:13:03 -04:00
|
|
|
* @bean
|
|
|
|
*/
|
2011-05-16 08:33:15 -04:00
|
|
|
getSelectedItems: function() {
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: Return groups if their children are all selected,
|
2011-05-16 08:33:15 -04:00
|
|
|
// and filter out their children from the list.
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: The order of these items should be that of their
|
2011-05-16 08:33:15 -04:00
|
|
|
// drawing order.
|
|
|
|
var items = [];
|
|
|
|
Base.each(this._selectedItems, function(item) {
|
|
|
|
items.push(item);
|
|
|
|
});
|
|
|
|
return items;
|
|
|
|
},
|
|
|
|
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: Implement setSelectedItems?
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-06-14 10:37:25 -04:00
|
|
|
_updateSelection: function(item) {
|
|
|
|
if (item._selected) {
|
2011-05-16 08:33:15 -04:00
|
|
|
this._selectedItemCount++;
|
|
|
|
this._selectedItems[item.getId()] = item;
|
|
|
|
} else {
|
|
|
|
this._selectedItemCount--;
|
|
|
|
delete this._selectedItems[item.getId()];
|
|
|
|
}
|
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-05-16 08:33:15 -04:00
|
|
|
/**
|
|
|
|
* Selects all items in the project.
|
|
|
|
*/
|
|
|
|
selectAll: function() {
|
|
|
|
for (var i = 0, l = this.layers.length; i < l; i++)
|
|
|
|
this.layers[i].setSelected(true);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deselects all selected items in the project.
|
|
|
|
*/
|
|
|
|
deselectAll: function() {
|
|
|
|
for (var i in this._selectedItems)
|
|
|
|
this._selectedItems[i].setSelected(false);
|
|
|
|
},
|
2011-05-25 18:54:25 -04:00
|
|
|
|
2011-07-07 16:14:58 -04:00
|
|
|
hitTest: function(point, options) {
|
2011-07-08 17:25:27 -04:00
|
|
|
options = HitResult.getOptions(point, options);
|
|
|
|
point = options.point;
|
2011-07-08 15:15:16 -04:00
|
|
|
// Loop backwards, so layers that get drawn last are tested first
|
|
|
|
for (var i = this.layers.length - 1; i >= 0; i--) {
|
2011-07-07 16:14:58 -04:00
|
|
|
var res = this.layers[i].hitTest(point, options);
|
2011-07-08 17:26:50 -04:00
|
|
|
if (res) return res;
|
2011-07-07 16:14:58 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2011-05-25 18:54:25 -04:00
|
|
|
/**
|
|
|
|
* {@grouptitle Project Hierarchy}
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:57:58 -04:00
|
|
|
* The layers contained within the project.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:54:25 -04:00
|
|
|
* @name Project#layers
|
2011-05-26 10:49:19 -04:00
|
|
|
* @type Layer[]
|
2011-05-25 18:54:25 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-05-25 18:57:58 -04:00
|
|
|
* The layer which is currently active. New items will be created on this
|
2011-05-25 18:54:25 -04:00
|
|
|
* layer by default.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:54:25 -04:00
|
|
|
* @name Project#activeLayer
|
|
|
|
* @type Layer
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-05-25 18:57:58 -04:00
|
|
|
* The symbols contained within the project.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:54:25 -04:00
|
|
|
* @name Project#symbols
|
2011-05-26 10:49:19 -04:00
|
|
|
* @type Symbol[]
|
2011-05-25 18:54:25 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The views contained within the project.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:54:25 -04:00
|
|
|
* @name Project#views
|
2011-05-26 10:49:19 -04:00
|
|
|
* @type View[]
|
2011-05-25 18:54:25 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The view which is currently active.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:54:25 -04:00
|
|
|
* @name Project#activeView
|
|
|
|
* @type View
|
|
|
|
*/
|
|
|
|
|
2011-05-16 08:33:15 -04:00
|
|
|
draw: function(ctx) {
|
|
|
|
ctx.save();
|
|
|
|
var param = { offset: new Point(0, 0) };
|
|
|
|
for (var i = 0, l = this.layers.length; i < l; i++)
|
|
|
|
Item.draw(this.layers[i], ctx, param);
|
|
|
|
ctx.restore();
|
|
|
|
|
|
|
|
// Draw the selection of the selected items in the project:
|
|
|
|
if (this._selectedItemCount > 0) {
|
|
|
|
ctx.save();
|
|
|
|
ctx.strokeWidth = 1;
|
|
|
|
// TODO: use Layer#color
|
|
|
|
ctx.strokeStyle = ctx.fillStyle = '#009dec';
|
|
|
|
param = { selection: true };
|
|
|
|
Base.each(this._selectedItems, function(item) {
|
|
|
|
item.draw(ctx, param);
|
|
|
|
});
|
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|