2011-03-06 19:50:44 -05:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2014-01-03 19:47:16 -05:00
|
|
|
* Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://scratchdisk.com/ & 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-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name Group
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class A Group is a collection of items. When you transform a Group, its
|
|
|
|
* children are treated as a single unit without changing their relative
|
|
|
|
* positions.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @extends Item
|
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
var Group = Item.extend(/** @lends Group# */{
|
2013-06-23 23:18:32 -04:00
|
|
|
_class: 'Group',
|
2013-12-09 09:10:09 -05:00
|
|
|
_selectChildren: true,
|
2013-02-28 22:41:13 -05:00
|
|
|
_serializeFields: {
|
|
|
|
children: []
|
|
|
|
},
|
2012-11-04 01:43:18 -05:00
|
|
|
|
2011-06-03 08:25:25 -04:00
|
|
|
// DOCS: document new Group(item, item...);
|
2011-05-23 07:37:13 -04:00
|
|
|
/**
|
|
|
|
* Creates a new Group item and places it at the top of the active layer.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-03-16 08:44:58 -04:00
|
|
|
* @name Group#initialize
|
2011-06-19 16:52:52 -04:00
|
|
|
* @param {Item[]} [children] An array of children that will be added to the
|
|
|
|
* newly created group.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-03-16 08:44:58 -04:00
|
|
|
* @example {@paperscript}
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Create a group containing two paths:
|
2013-03-16 08:44:58 -04:00
|
|
|
* var path = new Path([100, 100], [100, 200]);
|
|
|
|
* var path2 = new Path([50, 150], [150, 150]);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Create a group from the two paths:
|
|
|
|
* var group = new Group([path, path2]);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Set the stroke color of all items in the group:
|
|
|
|
* group.strokeColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Move the group to the center of the view:
|
|
|
|
* group.position = view.center;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-03-16 08:44:58 -04:00
|
|
|
* @example {@paperscript height=320}
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Click in the view to add a path to the group, which in turn is rotated
|
|
|
|
* // every frame:
|
|
|
|
* var group = new Group();
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* function onMouseDown(event) {
|
|
|
|
* // Create a new circle shaped path at the position
|
|
|
|
* // of the mouse:
|
|
|
|
* var path = new Path.Circle(event.point, 5);
|
|
|
|
* path.fillColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-17 10:58:22 -04:00
|
|
|
* // Add the path to the group's children list:
|
|
|
|
* group.addChild(path);
|
2011-06-03 08:25:25 -04:00
|
|
|
* }
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* function onFrame(event) {
|
|
|
|
* // Rotate the group by 1 degree from
|
|
|
|
* // the centerpoint of the view:
|
|
|
|
* group.rotate(1, view.center);
|
|
|
|
* }
|
2011-05-23 07:37:13 -04:00
|
|
|
*/
|
2013-03-16 08:44:58 -04:00
|
|
|
/**
|
|
|
|
* Creates a new Group item and places it at the top of the active layer.
|
|
|
|
*
|
|
|
|
* @name Group#initialize
|
2013-10-16 08:20:13 -04:00
|
|
|
* @param {Object} object an object literal containing the properties to be
|
|
|
|
* set on the group.
|
2013-03-16 08:44:58 -04:00
|
|
|
*
|
|
|
|
* @example {@paperscript}
|
|
|
|
* var path = new Path([100, 100], [100, 200]);
|
|
|
|
* var path2 = new Path([50, 150], [150, 150]);
|
|
|
|
*
|
|
|
|
* // Create a group from the two paths:
|
|
|
|
* var group = new Group({
|
|
|
|
* children: [path, path2],
|
|
|
|
* // Set the stroke color of all items in the group:
|
|
|
|
* strokeColor: 'black',
|
|
|
|
* // Move the group to the center of the view:
|
|
|
|
* position: view.center
|
|
|
|
* });
|
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
initialize: function Group(arg) {
|
2011-06-17 09:11:37 -04:00
|
|
|
// Allow Group to have children and named children
|
2011-05-14 13:07:10 -04:00
|
|
|
this._children = [];
|
2011-05-15 13:12:27 -04:00
|
|
|
this._namedChildren = {};
|
2013-07-21 18:45:22 -04:00
|
|
|
if (!this._initialize(arg))
|
2012-12-25 16:12:25 -05:00
|
|
|
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
2011-02-11 12:37:36 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2013-05-27 13:04:05 -04:00
|
|
|
_changed: function _changed(flags) {
|
|
|
|
_changed.base.call(this, flags);
|
2014-03-17 13:59:10 -04:00
|
|
|
if (flags & (/*#=*/ ChangeFlag.CHILDREN | /*#=*/ ChangeFlag.CLIPPING)) {
|
2011-06-19 17:36:04 -04:00
|
|
|
// Clear cached clip item whenever hierarchy changes
|
2013-12-17 17:27:48 -05:00
|
|
|
this._clipItem = undefined;
|
2011-06-19 17:36:04 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getClipItem: function() {
|
2014-03-17 09:51:47 -04:00
|
|
|
// NOTE: _clipItem is the child that has _clipMask set to true.
|
|
|
|
var clipItem = this._clipItem;
|
|
|
|
// Distinguish null (no clipItem set) and undefined (clipItem was not
|
|
|
|
// looked for yet).
|
|
|
|
if (clipItem === undefined) {
|
|
|
|
clipItem = null;
|
|
|
|
for (var i = 0, l = this._children.length; i < l; i++) {
|
|
|
|
var child = this._children[i];
|
|
|
|
if (child._clipMask) {
|
|
|
|
clipItem = child;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._clipItem = clipItem;
|
2011-06-17 09:05:37 -04:00
|
|
|
}
|
2014-03-17 09:51:47 -04:00
|
|
|
return clipItem;
|
2011-06-17 09:05:37 -04:00
|
|
|
},
|
|
|
|
|
2011-02-11 12:37:36 -05:00
|
|
|
/**
|
|
|
|
* Specifies whether the group item is to be clipped.
|
2011-05-31 08:28:42 -04:00
|
|
|
* When setting to {@code true}, the first child in the group is
|
|
|
|
* automatically defined as the clipping mask.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-30 13:42:17 -04:00
|
|
|
* @type Boolean
|
2011-05-23 07:37:13 -04:00
|
|
|
* @bean
|
2013-03-16 08:44:58 -04:00
|
|
|
*
|
|
|
|
* @example {@paperscript}
|
|
|
|
* var star = new Path.Star({
|
|
|
|
* center: view.center,
|
|
|
|
* points: 6,
|
|
|
|
* radius1: 20,
|
|
|
|
* radius2: 40,
|
|
|
|
* fillColor: 'red'
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* var circle = new Path.Circle({
|
|
|
|
* center: view.center,
|
|
|
|
* radius: 25,
|
|
|
|
* strokeColor: 'black'
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // Create a group of the two items and clip it:
|
|
|
|
* var group = new Group(circle, star);
|
|
|
|
* group.clipped = true;
|
|
|
|
*
|
|
|
|
* // Lets animate the circle:
|
|
|
|
* function onFrame(event) {
|
|
|
|
* var offset = Math.sin(event.count / 30) * 30;
|
|
|
|
* circle.position.x = view.center.x + offset;
|
|
|
|
* }
|
2011-02-11 12:37:36 -05:00
|
|
|
*/
|
|
|
|
isClipped: function() {
|
2011-06-19 17:36:04 -04:00
|
|
|
return !!this._getClipItem();
|
2011-02-11 12:37:36 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-11 12:37:36 -05:00
|
|
|
setClipped: function(clipped) {
|
2011-05-07 04:56:27 -04:00
|
|
|
var child = this.getFirstChild();
|
2011-02-13 11:26:24 -05:00
|
|
|
if (child)
|
2011-02-11 12:37:36 -05:00
|
|
|
child.setClipMask(clipped);
|
2011-03-03 07:19:43 -05:00
|
|
|
},
|
|
|
|
|
2013-04-18 19:58:35 -04:00
|
|
|
_draw: function(ctx, param) {
|
2014-03-17 09:51:47 -04:00
|
|
|
var clip = param.clip,
|
|
|
|
clipItem = !clip && this._getClipItem(),
|
|
|
|
draw = true;
|
|
|
|
param = param.extend({ clipItem: clipItem, clip: false });
|
|
|
|
if (clip) {
|
|
|
|
// If told to clip with a group, we start our own path and draw each
|
|
|
|
// child just like in a compound-path. We also cache the resulting
|
|
|
|
// path in _currentPath.
|
|
|
|
if (this._currentPath) {
|
|
|
|
ctx.currentPath = this._currentPath;
|
|
|
|
draw = false;
|
|
|
|
} else {
|
|
|
|
ctx.beginPath();
|
|
|
|
param.dontStart = param.dontFinish = true;
|
|
|
|
}
|
|
|
|
} else if (clipItem) {
|
2013-06-11 23:40:44 -04:00
|
|
|
clipItem.draw(ctx, param.extend({ clip: true }));
|
2011-03-03 07:19:43 -05:00
|
|
|
}
|
2014-03-17 09:51:47 -04:00
|
|
|
if (draw) {
|
|
|
|
for (var i = 0, l = this._children.length; i < l; i++) {
|
|
|
|
var item = this._children[i];
|
|
|
|
if (item !== clipItem)
|
|
|
|
item.draw(ctx, param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (clip) {
|
|
|
|
this._currentPath = ctx.currentPath;
|
|
|
|
}
|
2011-02-11 12:37:36 -05:00
|
|
|
}
|
2011-02-13 11:26:24 -05:00
|
|
|
});
|