paper.js/src/item/Group.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

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
*/
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-05-23 07:37:13 -04:00
/**
* Creates a new Group item and places it at the top of the active layer.
*
* @param {Array} [children] An optional array of children that will be
* added to the newly created group.
*
2011-05-23 07:37:13 -04:00
* @example
* // Create an empty group and append a path to the top of its children
* // array:
*
2011-05-23 07:37:13 -04:00
* // Create an empty group:
* var group = new Group();
*
* var path = new Path([new Point(10, 10), new Point(50, 50)]);
* path.strokeColor = 'black';
2011-05-23 07:37:13 -04:00
*
* // Append the path to the group:
* group.appendTop(path);
*
* // Set the stroke color of all items in the group:
* circleGroup.strokeColor = 'black';
*
* @example
* // Create a group containing two paths:
* var circle = new Path.Circle(new Point(30, 50), 10);
* var circle2 = new Path.Circle(new Point(50, 50), 10);
*
* var circleGroup = new Group([circle, circle2]);
* // Set the fill color of all items in the group:
* circleGroup.fillColor = 'black';
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
*/
initialize: function(items) {
this.base();
this._children = [];
this._namedChildren = {};
this._clipped = false;
this.setChildren(!items || !Array.isArray(items)
|| typeof items[0] !== 'object' ? arguments : items);
2011-02-11 12:37:36 -05:00
},
clone: function() {
var copy = this.base();
copy._clipped = this._clipped;
return copy;
},
2011-02-11 12:37:36 -05:00
/**
* Specifies whether the group item is to be clipped.
* When setting to true, the first child in the group is automatically
* defined as the clipping mask.
*
* @type Boolean
2011-05-23 07:37:13 -04:00
* @bean
2011-02-11 12:37:36 -05:00
*/
isClipped: function() {
return this._clipped;
2011-02-11 12:37:36 -05:00
},
2011-02-11 12:37:36 -05:00
setClipped: function(clipped) {
this._clipped = clipped;
2011-05-07 04:56:27 -04:00
var child = this.getFirstChild();
if (child)
2011-02-11 12:37:36 -05:00
child.setClipMask(clipped);
},
draw: function(ctx, param) {
for (var i = 0, l = this._children.length; i < l; i++) {
Item.draw(this._children[i], ctx, param);
if (this._clipped && i == 0)
ctx.clip();
}
2011-02-11 12:37:36 -05:00
}
});