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
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-03-06 19:50:44 -05: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-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
|
|
|
|
*/
|
|
|
|
var Group = this.Group = Item.extend(/** @lends Group# */{
|
2012-11-04 01:43:18 -05:00
|
|
|
_type: 'group',
|
|
|
|
|
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
|
|
|
*
|
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
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* @example {@paperscript split=true height=200}
|
|
|
|
* // Create a group containing two paths:
|
|
|
|
* var path = new Path(new Point(100, 100), new Point(100, 200));
|
|
|
|
* var path2 = new Path(new Point(50, 150), new Point(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
|
|
|
*
|
2011-06-03 12:44:22 -04:00
|
|
|
* @example {@paperscript split=true 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
|
|
|
*/
|
2012-12-25 16:12:25 -05:00
|
|
|
initialize: function(arg) {
|
2011-02-12 13:12:23 -05:00
|
|
|
this.base();
|
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-02-15 21:28:49 -05:00
|
|
|
if (!this._set(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
|
|
|
|
2011-06-19 17:36:04 -04:00
|
|
|
_changed: function(flags) {
|
2012-11-05 11:54:10 -05:00
|
|
|
// Don't use this.base() for reasons of performance.
|
2011-06-19 17:40:49 -04:00
|
|
|
Item.prototype._changed.call(this, flags);
|
2012-11-05 21:11:44 -05:00
|
|
|
if (flags & (/*#=*/ ChangeFlag.HIERARCHY | /*#=*/ ChangeFlag.CLIPPING)) {
|
2011-06-19 17:36:04 -04:00
|
|
|
// Clear cached clip item whenever hierarchy changes
|
|
|
|
delete this._clipItem;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getClipItem: function() {
|
|
|
|
// Allow us to set _clipItem to null when none is found and still return
|
|
|
|
// it as a defined value without searching again
|
|
|
|
if (this._clipItem !== undefined)
|
|
|
|
return this._clipItem;
|
2011-06-17 09:05:37 -04:00
|
|
|
for (var i = 0, l = this._children.length; i < l; i++) {
|
|
|
|
var child = this._children[i];
|
|
|
|
if (child._clipMask)
|
2011-06-19 17:36:04 -04:00
|
|
|
return this._clipItem = child;
|
2011-06-17 09:05:37 -04:00
|
|
|
}
|
2011-06-19 17:36:41 -04:00
|
|
|
// Make sure we're setting _clipItem to null so it won't be searched for
|
|
|
|
// nex time.
|
2011-06-19 17:36:04 -04:00
|
|
|
return this._clipItem = null;
|
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
|
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-06-17 08:56:02 -04:00
|
|
|
return this;
|
2011-03-03 07:19:43 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
draw: function(ctx, param) {
|
2011-06-19 17:36:04 -04:00
|
|
|
var clipItem = this._getClipItem();
|
2012-03-02 05:54:11 -05:00
|
|
|
if (clipItem) {
|
|
|
|
param.clipping = true;
|
2011-06-19 17:36:04 -04:00
|
|
|
Item.draw(clipItem, ctx, param);
|
2012-03-02 05:54:11 -05:00
|
|
|
delete param.clipping;
|
|
|
|
}
|
2011-05-14 13:07:10 -04:00
|
|
|
for (var i = 0, l = this._children.length; i < l; i++) {
|
2011-06-17 09:05:37 -04:00
|
|
|
var item = this._children[i];
|
2011-06-19 17:36:04 -04:00
|
|
|
if (item != clipItem)
|
2011-06-17 09:05:37 -04:00
|
|
|
Item.draw(item, ctx, param);
|
2011-03-03 07:19:43 -05:00
|
|
|
}
|
2011-02-11 12:37:36 -05:00
|
|
|
}
|
2011-02-13 11:26:24 -05:00
|
|
|
});
|