2011-05-16 08:33:15 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-05-16 08:33:15 -04:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-05-16 08:33:15 -04:00
|
|
|
* 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
|
|
|
*/
|
2011-08-02 05:08:08 -04:00
|
|
|
var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
|
|
|
|
_list: 'projects',
|
|
|
|
_reference: '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-11-12 10:56:23 -05:00
|
|
|
*
|
|
|
|
* @param {View|HTMLCanvasElement} view Either a view object or an HTML
|
|
|
|
* Canvas element that should be wrapped in a newly created view.
|
2011-05-23 12:13:03 -04:00
|
|
|
*/
|
2011-11-12 10:56:23 -05:00
|
|
|
initialize: function(view) {
|
|
|
|
// Activate straight away by passing true to base(), so paper.project is
|
|
|
|
// set, as required by Layer and DoumentView constructors.
|
2011-08-02 05:08:08 -04:00
|
|
|
this.base(true);
|
2011-05-16 08:33:15 -04:00
|
|
|
this.layers = [];
|
|
|
|
this.symbols = [];
|
|
|
|
this.activeLayer = new Layer();
|
2011-11-12 10:56:23 -05:00
|
|
|
if (view)
|
|
|
|
this.view = view instanceof View ? view : View.create(view);
|
2012-12-03 12:53:47 -05:00
|
|
|
this._currentStyle = new PathStyle();
|
|
|
|
this._selectedItems = {};
|
|
|
|
this._selectedItemCount = 0;
|
2013-01-20 17:01:27 -05:00
|
|
|
// See Item.draw() for an explanation of _drawCount
|
|
|
|
this._drawCount = 0;
|
2011-09-22 04:32:17 -04:00
|
|
|
// Change tracking, not in use for now. Activate once required:
|
|
|
|
// this._changes = [];
|
|
|
|
// this._changesById = {};
|
2013-03-01 21:28:22 -05:00
|
|
|
this.options = {};
|
2011-05-16 08:33:15 -04:00
|
|
|
},
|
|
|
|
|
2013-02-12 17:57:54 -05:00
|
|
|
_serialize: function(options, dictionary) {
|
2013-02-11 19:43:31 -05:00
|
|
|
// Just serialize layers to an array for now, they will be unserialized
|
|
|
|
// into the active project automatically. We might want to add proper
|
|
|
|
// project serialization later, but deserialization of a layers array
|
|
|
|
// will always work.
|
2013-02-12 17:57:54 -05:00
|
|
|
return Base.serialize(this.layers, options, false, dictionary);
|
2013-02-11 19:43:31 -05:00
|
|
|
},
|
|
|
|
|
2011-06-19 18:03:18 -04:00
|
|
|
_needsRedraw: function() {
|
2011-11-12 10:56:23 -05:00
|
|
|
if (this.view)
|
|
|
|
this.view._redrawNeeded = true;
|
2011-06-19 16:49:26 -04:00
|
|
|
},
|
|
|
|
|
2011-08-02 05:08:08 -04:00
|
|
|
/**
|
|
|
|
* Activates this project, so all newly created items will be placed
|
|
|
|
* in it.
|
|
|
|
*
|
|
|
|
* @name Project#activate
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-11-12 10:56:23 -05:00
|
|
|
* Removes this project from the {@link PaperScope#projects} list, and also
|
|
|
|
* removes its view, if one was defined.
|
|
|
|
*/
|
|
|
|
remove: function() {
|
|
|
|
if (!this.base())
|
|
|
|
return false;
|
|
|
|
if (this.view)
|
|
|
|
this.view.remove();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The reference to the project's view.
|
|
|
|
* @name Project#view
|
|
|
|
* @type View
|
2011-08-02 05:08:08 -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
|
|
|
/**
|
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 = [];
|
2013-01-20 17:01:27 -05:00
|
|
|
for (var id in this._selectedItems) {
|
|
|
|
var item = this._selectedItems[id];
|
|
|
|
if (item._drawCount === this._drawCount)
|
|
|
|
items.push(item);
|
|
|
|
}
|
2011-05-16 08:33:15 -04:00
|
|
|
return items;
|
|
|
|
},
|
|
|
|
|
2013-03-01 23:24:26 -05:00
|
|
|
// DOCS: Project#options
|
|
|
|
/**
|
|
|
|
* <b>options.handleSize:</b>
|
|
|
|
* <b>options.hitTolerance:</b>
|
|
|
|
*
|
|
|
|
* @name Project#options
|
|
|
|
* @type Object
|
|
|
|
*/
|
|
|
|
|
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++;
|
2011-11-28 17:14:02 -05:00
|
|
|
this._selectedItems[item._id] = item;
|
2013-01-20 18:56:58 -05:00
|
|
|
// Make sure the item is considered selected right away if it is
|
|
|
|
// part of the DOM, even before it's getting drawn for the first
|
|
|
|
// time.
|
|
|
|
if (item.isInserted())
|
|
|
|
item._drawCount = this._drawCount;
|
2011-05-16 08:33:15 -04:00
|
|
|
} else {
|
|
|
|
this._selectedItemCount--;
|
2011-11-28 17:14:02 -05:00
|
|
|
delete this._selectedItems[item._id];
|
2011-05-16 08:33:15 -04:00
|
|
|
}
|
|
|
|
},
|
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)
|
2013-02-15 21:21:06 -05:00
|
|
|
this._selectedItems[i].setSelected(false);
|
2011-05-16 08:33:15 -04:00
|
|
|
},
|
2011-05-25 18:54:25 -04:00
|
|
|
|
2011-07-31 16:58:51 -04:00
|
|
|
/**
|
|
|
|
* Perform a hit test on the items contained within the project at the
|
|
|
|
* location of the specified point.
|
|
|
|
*
|
|
|
|
* The optional options object allows you to control the specifics of the
|
|
|
|
* hit test and may contain a combination of the following values:
|
2011-08-01 09:25:46 -04:00
|
|
|
* <b>options.tolerance:</b> {@code Number} - The tolerance of the hit test
|
|
|
|
* in points.
|
|
|
|
* <b>options.type:</b> Only hit test again a certain item
|
2011-07-31 16:58:51 -04:00
|
|
|
* type: {@link PathItem}, {@link Raster}, {@link TextItem}, etc.
|
2011-08-01 09:25:46 -04:00
|
|
|
* <b>options.fill:</b> {@code Boolean} - Hit test the fill of items.
|
|
|
|
* <b>options.stroke:</b> {@code Boolean} - Hit test the curves of path
|
|
|
|
* items, taking into account stroke width.
|
2011-11-10 09:14:49 -05:00
|
|
|
* <b>options.segments:</b> {@code Boolean} - Hit test for
|
2011-08-01 09:25:46 -04:00
|
|
|
* {@link Segment#point} of {@link Path} items.
|
|
|
|
* <b>options.handles:</b> {@code Boolean} - Hit test for the handles
|
2011-07-31 16:58:51 -04:00
|
|
|
* ({@link Segment#handleIn} / {@link Segment#handleOut}) of path segments.
|
2011-08-01 09:25:46 -04:00
|
|
|
* <b>options.ends:</b> {@code Boolean} - Only hit test for the first or
|
|
|
|
* last segment points of open path items.
|
|
|
|
* <b>options.bounds:</b> {@code Boolean} - Hit test the corners and
|
|
|
|
* side-centers of the bounding rectangle of items ({@link Item#bounds}).
|
|
|
|
* <b>options.center:</b> {@code Boolean} - Hit test the
|
|
|
|
* {@link Rectangle#center} of the bounding rectangle of items
|
|
|
|
* ({@link Item#bounds}).
|
2013-03-16 09:16:02 -04:00
|
|
|
* <b>options.guides:</b> {@code Boolean} - Hit test items that have
|
2011-07-31 16:58:51 -04:00
|
|
|
* {@link Item#guide} set to {@code true}.
|
2011-08-01 09:25:46 -04:00
|
|
|
* <b>options.selected:</b> {@code Boolean} - Only hit selected items.
|
2011-07-31 16:58:51 -04:00
|
|
|
*
|
|
|
|
* @param {Point} point The point where the hit test should be performed
|
|
|
|
* @param {Object} [options={ fill: true, stroke: true, segments: true,
|
|
|
|
* tolerance: true }]
|
2011-08-01 06:48:07 -04:00
|
|
|
* @return {HitResult} A hit result object that contains more
|
2011-07-31 16:58:51 -04:00
|
|
|
* information about what exactly was hit or {@code null} if nothing was
|
|
|
|
* hit.
|
|
|
|
*/
|
2011-07-07 16:14:58 -04:00
|
|
|
hitTest: function(point, options) {
|
2012-11-23 15:41:00 -05:00
|
|
|
// We don't need to do this here, but it speeds up things since we won't
|
|
|
|
// repeatetly convert in Item#hitTest() then.
|
|
|
|
point = Point.read(arguments);
|
2012-12-30 10:07:20 -05:00
|
|
|
options = HitResult.getOptions(Base.read(arguments));
|
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;
|
|
|
|
},
|
|
|
|
|
2013-02-25 19:17:33 -05:00
|
|
|
// DOCS: document exportJson (documented in @private Base)
|
|
|
|
// DOCS: document importJson
|
|
|
|
// DOCS: Figure out a way to group these together with importSvg / exportSvg
|
|
|
|
|
|
|
|
importJson: function(json) {
|
|
|
|
return Base.importJson(json);
|
|
|
|
},
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2011-12-20 05:41:23 -05:00
|
|
|
draw: function(ctx, matrix) {
|
2013-02-08 21:23:33 -05:00
|
|
|
// Create a local lookup table for hierarchically concatenated matrices
|
|
|
|
// by item id, to speed up drawing by eliminating repeated concatenation
|
|
|
|
// of parent's matrices through caching.
|
|
|
|
var matrices = {};
|
|
|
|
// Description of the paramters to getGlobalMatrix():
|
|
|
|
// mx is the container for the final concatenated matrix, passed to
|
|
|
|
// getGlobalMatrix() on the initial call.
|
|
|
|
// cached defines wether the result of the concatenation should be
|
|
|
|
// cached, only used for parents of items that this is called for.
|
|
|
|
function getGlobalMatrix(item, mx, cached) {
|
|
|
|
var cache = cached && matrices[item._id];
|
|
|
|
// Found a cached version? Return a clone of it.
|
|
|
|
if (cache)
|
|
|
|
return cache.clone();
|
|
|
|
// Get concatenated matrix from all the parents, using
|
|
|
|
// local caching (passing true for cached):
|
|
|
|
if (item._parent)
|
|
|
|
mx = getGlobalMatrix(item._parent, mx, true);
|
|
|
|
// No need to concatenate if it's the identity matrix
|
|
|
|
if (!item._matrix.isIdentity())
|
|
|
|
mx.concatenate(item._matrix);
|
|
|
|
// If the result needs to be cached, create a copy since matrix
|
|
|
|
// might be further modified through recursive calls
|
|
|
|
if (cached)
|
|
|
|
matrices[item._id] = mx.clone();
|
|
|
|
return mx;
|
|
|
|
}
|
|
|
|
|
2013-01-20 17:01:27 -05:00
|
|
|
this._drawCount++;
|
2011-05-16 08:33:15 -04:00
|
|
|
ctx.save();
|
2011-12-27 10:40:49 -05:00
|
|
|
if (!matrix.isIdentity())
|
2011-12-20 05:41:23 -05:00
|
|
|
matrix.applyToContext(ctx);
|
2011-05-16 08:33:15 -04:00
|
|
|
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';
|
2011-12-20 05:42:00 -05:00
|
|
|
for (var id in this._selectedItems) {
|
2011-12-20 17:09:49 -05:00
|
|
|
var item = this._selectedItems[id];
|
2013-02-24 18:53:37 -05:00
|
|
|
if (item._drawCount === this._drawCount
|
|
|
|
&& (item.drawSelected || item._boundsSelected)) {
|
2013-02-24 18:41:31 -05:00
|
|
|
var mx = getGlobalMatrix(item, matrix.clone());
|
2013-02-24 18:53:37 -05:00
|
|
|
if (item.drawSelected)
|
|
|
|
item.drawSelected(ctx, mx);
|
2013-02-24 18:41:31 -05:00
|
|
|
if (item._boundsSelected)
|
2013-02-24 19:26:46 -05:00
|
|
|
// We need to call the internal _getBounds, to get non-
|
|
|
|
// transformed bounds.
|
|
|
|
Item.drawSelectedBounds(item._getBounds('getBounds'),
|
|
|
|
ctx, mx);
|
2013-02-24 18:41:31 -05:00
|
|
|
}
|
2011-12-20 05:42:00 -05:00
|
|
|
}
|
2011-05-16 08:33:15 -04:00
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|