2011-03-06 19:50:44 -05: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-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-03-04 08:34:31 -05:00
|
|
|
var Group = this.Group = Item.extend({
|
2011-05-23 07:37:13 -04:00
|
|
|
/** @lends Group# */
|
|
|
|
|
2011-02-16 16:06:24 -05:00
|
|
|
beans: true,
|
2011-03-06 10:15:21 -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-05-30 13:42:17 -04:00
|
|
|
* @param {Array} [children] An optional array of children that will be
|
|
|
|
* added to the newly created group.
|
|
|
|
*
|
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-05-30 13:42:17 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Create a group from the two paths:
|
|
|
|
* var group = new Group([path, path2]);
|
2011-05-23 07:37:13 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Set the stroke color of all items in the group:
|
|
|
|
* group.strokeColor = 'black';
|
2011-05-23 07:37:13 -04:00
|
|
|
*
|
2011-06-03 08:25:25 -04:00
|
|
|
* // Move the group to the center of the view:
|
|
|
|
* group.position = view.center;
|
2011-05-30 13:42:17 -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-05-30 13:42:17 -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';
|
|
|
|
*
|
|
|
|
* // Move the path to the top of the group's children list:
|
|
|
|
* group.appendTop(path);
|
|
|
|
* }
|
2011-05-30 13:42:17 -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
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @extends Item
|
|
|
|
* @constructs Group
|
|
|
|
*/
|
2011-02-12 13:12:23 -05:00
|
|
|
initialize: function(items) {
|
|
|
|
this.base();
|
2011-05-14 13:07:10 -04:00
|
|
|
this._children = [];
|
2011-05-15 13:12:27 -04:00
|
|
|
this._namedChildren = {};
|
2011-05-16 15:16:27 -04:00
|
|
|
this.setChildren(!items || !Array.isArray(items)
|
|
|
|
|| typeof items[0] !== 'object' ? arguments : items);
|
2011-02-11 12:37:36 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05: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-14 17:59:45 -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-17 08:56:02 -04:00
|
|
|
for (var i = 0, l = this._children.length; i < l; i++) {
|
|
|
|
if (this._children[i]._clipMask)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
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-05-14 13:07:10 -04:00
|
|
|
for (var i = 0, l = this._children.length; i < l; i++) {
|
|
|
|
Item.draw(this._children[i], 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
|
|
|
});
|