2012-12-02 04:04:10 +08:00
|
|
|
(function() {
|
2013-05-08 14:51:02 +08:00
|
|
|
Kinetic.Util.addMethods(Kinetic.Container, {
|
2012-12-02 04:04:10 +08:00
|
|
|
_containerInit: function(config) {
|
2013-05-20 12:07:43 +08:00
|
|
|
this.children = new Kinetic.Collection();
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Node.call(this, config);
|
|
|
|
},
|
|
|
|
/**
|
2013-05-20 12:07:43 +08:00
|
|
|
* returns a {@link Kinetic.Collection} of direct descendant nodes
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
getChildren: function() {
|
|
|
|
return this.children;
|
|
|
|
},
|
2013-06-07 13:45:31 +08:00
|
|
|
/**
|
|
|
|
* determine if node has children
|
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
|
|
|
*/
|
|
|
|
hasChildren: function() {
|
|
|
|
return this.getChildren().length > 0;
|
|
|
|
},
|
2012-12-02 04:04:10 +08:00
|
|
|
/**
|
|
|
|
* remove all children
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
removeChildren: function() {
|
2013-06-07 13:45:31 +08:00
|
|
|
var children = this.children,
|
|
|
|
child;
|
|
|
|
|
|
|
|
while(children.length > 0) {
|
|
|
|
var child = children[0];
|
|
|
|
if (child.hasChildren()) {
|
|
|
|
child.removeChildren();
|
|
|
|
}
|
|
|
|
child.remove();
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
2013-06-07 13:45:31 +08:00
|
|
|
|
|
|
|
return this;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
2013-06-07 14:03:00 +08:00
|
|
|
/**
|
|
|
|
* destroy all children
|
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
|
|
|
*/
|
|
|
|
destroyChildren: function() {
|
2013-06-09 06:57:36 +08:00
|
|
|
var children = this.children;
|
2013-06-07 14:03:00 +08:00
|
|
|
while(children.length > 0) {
|
2013-06-09 06:57:36 +08:00
|
|
|
children[0].destroy();
|
2013-06-07 14:03:00 +08:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2012-12-02 04:04:10 +08:00
|
|
|
/**
|
|
|
|
* add node to container
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
* @param {Node} child
|
|
|
|
*/
|
|
|
|
add: function(child) {
|
|
|
|
var go = Kinetic.Global, children = this.children;
|
|
|
|
child.index = children.length;
|
|
|
|
child.parent = this;
|
|
|
|
children.push(child);
|
2013-05-21 13:12:43 +08:00
|
|
|
this._fire('add', {
|
2013-05-21 12:58:57 +08:00
|
|
|
child: child
|
2013-05-21 13:12:43 +08:00
|
|
|
});
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
// chainable
|
|
|
|
return this;
|
|
|
|
},
|
2013-06-09 06:57:36 +08:00
|
|
|
destroy: function() {
|
|
|
|
// destroy children
|
|
|
|
if (this.hasChildren()) {
|
|
|
|
this.destroyChildren();
|
|
|
|
}
|
|
|
|
// then destroy self
|
|
|
|
Kinetic.Node.prototype.destroy.call(this);
|
|
|
|
},
|
2012-12-02 04:04:10 +08:00
|
|
|
/**
|
2013-05-19 01:40:05 +08:00
|
|
|
* return a {@link Kinetic.Collection} of nodes that match the selector. Use '#' for id selections
|
|
|
|
* and '.' for name selections. You can also select by type or class name
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
* @param {String} selector
|
2013-05-19 01:40:05 +08:00
|
|
|
* @example
|
|
|
|
* // select node with id foo<br>
|
|
|
|
* var node = stage.get('#foo');<br><br>
|
|
|
|
*
|
|
|
|
* // select nodes with name bar inside layer<br>
|
|
|
|
* var nodes = layer.get('.bar');<br><br>
|
|
|
|
*
|
|
|
|
* // select all groups inside layer<br>
|
|
|
|
* var nodes = layer.get('Group');<br><br>
|
|
|
|
*
|
|
|
|
* // select all rectangles inside layer<br>
|
|
|
|
* var nodes = layer.get('Rect');
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
get: function(selector) {
|
|
|
|
var collection = new Kinetic.Collection();
|
|
|
|
// ID selector
|
|
|
|
if(selector.charAt(0) === '#') {
|
|
|
|
var node = this._getNodeById(selector.slice(1));
|
|
|
|
if(node) {
|
|
|
|
collection.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// name selector
|
|
|
|
else if(selector.charAt(0) === '.') {
|
|
|
|
var nodeList = this._getNodesByName(selector.slice(1));
|
|
|
|
Kinetic.Collection.apply(collection, nodeList);
|
|
|
|
}
|
|
|
|
// unrecognized selector, pass to children
|
|
|
|
else {
|
|
|
|
var retArr = [];
|
|
|
|
var children = this.getChildren();
|
|
|
|
var len = children.length;
|
|
|
|
for(var n = 0; n < len; n++) {
|
|
|
|
retArr = retArr.concat(children[n]._get(selector));
|
|
|
|
}
|
|
|
|
Kinetic.Collection.apply(collection, retArr);
|
|
|
|
}
|
|
|
|
return collection;
|
|
|
|
},
|
|
|
|
_getNodeById: function(key) {
|
2013-01-14 03:10:49 +08:00
|
|
|
var stage = this.getStage(), go = Kinetic.Global, node = go.ids[key];
|
|
|
|
if(node !== undefined && this.isAncestorOf(node)) {
|
|
|
|
return node;
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
_getNodesByName: function(key) {
|
2013-01-14 03:10:49 +08:00
|
|
|
var go = Kinetic.Global, arr = go.names[key] || [];
|
2012-12-02 04:04:10 +08:00
|
|
|
return this._getDescendants(arr);
|
|
|
|
},
|
|
|
|
_get: function(selector) {
|
|
|
|
var retArr = Kinetic.Node.prototype._get.call(this, selector);
|
2012-10-07 05:35:46 +08:00
|
|
|
var children = this.getChildren();
|
2012-11-14 16:18:49 +08:00
|
|
|
var len = children.length;
|
|
|
|
for(var n = 0; n < len; n++) {
|
2012-10-07 05:35:46 +08:00
|
|
|
retArr = retArr.concat(children[n]._get(selector));
|
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
return retArr;
|
|
|
|
},
|
|
|
|
// extenders
|
|
|
|
toObject: function() {
|
|
|
|
var obj = Kinetic.Node.prototype.toObject.call(this);
|
2012-10-04 10:38:12 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
obj.children = [];
|
2012-10-04 10:38:12 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
var children = this.getChildren();
|
|
|
|
var len = children.length;
|
|
|
|
for(var n = 0; n < len; n++) {
|
|
|
|
var child = children[n];
|
|
|
|
obj.children.push(child.toObject());
|
2012-04-09 12:26:13 +08:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
_getDescendants: function(arr) {
|
|
|
|
var retArr = [];
|
|
|
|
var len = arr.length;
|
|
|
|
for(var n = 0; n < len; n++) {
|
|
|
|
var node = arr[n];
|
|
|
|
if(this.isAncestorOf(node)) {
|
|
|
|
retArr.push(node);
|
|
|
|
}
|
2012-04-09 12:26:13 +08:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
return retArr;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* determine if node is an ancestor
|
|
|
|
* of descendant
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
* @param {Kinetic.Node} node
|
|
|
|
*/
|
|
|
|
isAncestorOf: function(node) {
|
|
|
|
var parent = node.getParent();
|
|
|
|
while(parent) {
|
|
|
|
if(parent._id === this._id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
parent = parent.getParent();
|
|
|
|
}
|
2012-09-26 03:06:02 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
clone: function(obj) {
|
|
|
|
// call super method
|
2013-05-20 12:07:43 +08:00
|
|
|
var node = Kinetic.Node.prototype.clone.call(this, obj);
|
2012-07-07 12:45:18 +08:00
|
|
|
|
2013-05-20 12:07:43 +08:00
|
|
|
this.getChildren().each(function(no) {
|
|
|
|
node.add(no.clone());
|
|
|
|
});
|
2012-12-02 04:04:10 +08:00
|
|
|
return node;
|
|
|
|
},
|
|
|
|
/**
|
2013-05-18 07:20:37 +08:00
|
|
|
* get all shapes that intersect a point. Note: because this method must clear a temporary
|
|
|
|
* canvas and redraw every shape inside the container, it should only be used for special sitations
|
|
|
|
* because it performs very poorly. Please use the {@link Kinetic.Stage#getIntersection} method if at all possible
|
|
|
|
* because it performs much better
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2013-05-18 06:50:53 +08:00
|
|
|
* @param {Object} pos
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
2013-05-18 06:50:53 +08:00
|
|
|
getAllIntersections: function() {
|
2013-05-08 14:51:02 +08:00
|
|
|
var pos = Kinetic.Util._getXY(Array.prototype.slice.call(arguments));
|
2012-12-02 04:04:10 +08:00
|
|
|
var arr = [];
|
|
|
|
var shapes = this.get('Shape');
|
2012-07-07 12:45:18 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
var len = shapes.length;
|
2012-11-19 11:50:50 +08:00
|
|
|
for(var n = 0; n < len; n++) {
|
2012-12-02 04:04:10 +08:00
|
|
|
var shape = shapes[n];
|
|
|
|
if(shape.isVisible() && shape.intersects(pos)) {
|
|
|
|
arr.push(shape);
|
|
|
|
}
|
2012-11-19 11:50:50 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
return arr;
|
|
|
|
},
|
|
|
|
_setChildrenIndices: function() {
|
2012-11-19 11:50:50 +08:00
|
|
|
var children = this.children, len = children.length;
|
|
|
|
for(var n = 0; n < len; n++) {
|
2012-12-02 04:04:10 +08:00
|
|
|
children[n].index = n;
|
2012-11-19 11:50:50 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
2012-12-11 16:08:59 +08:00
|
|
|
drawScene: function(canvas) {
|
2013-03-22 15:46:41 +08:00
|
|
|
var layer = this.getLayer(),
|
|
|
|
clip = !!this.getClipFunc(),
|
2013-04-22 13:42:25 +08:00
|
|
|
children, n, len;
|
2013-03-22 15:46:41 +08:00
|
|
|
|
|
|
|
if (!canvas && layer) {
|
|
|
|
canvas = layer.getCanvas();
|
2013-03-24 11:02:11 +08:00
|
|
|
}
|
2013-03-22 15:46:41 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
if(this.isVisible()) {
|
2013-03-22 15:46:41 +08:00
|
|
|
if (clip) {
|
|
|
|
canvas._clip(this);
|
|
|
|
}
|
|
|
|
|
2013-04-07 17:23:33 +08:00
|
|
|
children = this.children;
|
2013-03-22 15:46:41 +08:00
|
|
|
len = children.length;
|
|
|
|
|
2013-04-22 13:42:25 +08:00
|
|
|
for(n = 0; n < len; n++) {
|
2012-12-11 16:08:59 +08:00
|
|
|
children[n].drawScene(canvas);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
2013-03-22 15:46:41 +08:00
|
|
|
|
|
|
|
if (clip) {
|
|
|
|
canvas.getContext().restore();
|
|
|
|
}
|
2013-02-16 10:20:34 +08:00
|
|
|
}
|
2013-06-07 13:45:31 +08:00
|
|
|
|
|
|
|
return this;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
drawHit: function() {
|
2013-02-16 10:20:34 +08:00
|
|
|
var clip = !!this.getClipFunc() && this.nodeType !== 'Stage',
|
2013-04-08 01:00:55 +08:00
|
|
|
n = 0,
|
|
|
|
len = 0,
|
|
|
|
children = [],
|
2013-02-16 10:20:34 +08:00
|
|
|
hitCanvas;
|
|
|
|
|
2013-03-24 11:02:11 +08:00
|
|
|
if(this.shouldDrawHit()) {
|
|
|
|
if (clip) {
|
|
|
|
hitCanvas = this.getLayer().hitCanvas;
|
|
|
|
hitCanvas._clip(this);
|
|
|
|
}
|
2013-04-08 01:00:55 +08:00
|
|
|
|
|
|
|
children = this.children;
|
|
|
|
len = children.length;
|
|
|
|
|
|
|
|
for(n = 0; n < len; n++) {
|
2012-12-02 04:04:10 +08:00
|
|
|
children[n].drawHit();
|
|
|
|
}
|
2013-03-24 11:02:11 +08:00
|
|
|
if (clip) {
|
|
|
|
hitCanvas.getContext().restore();
|
|
|
|
}
|
2013-02-16 10:20:34 +08:00
|
|
|
}
|
2013-06-07 13:45:31 +08:00
|
|
|
|
|
|
|
return this;
|
2012-10-08 10:14:14 +08:00
|
|
|
}
|
2013-05-08 14:17:57 +08:00
|
|
|
});
|
2013-02-16 10:20:34 +08:00
|
|
|
|
2013-05-08 14:51:02 +08:00
|
|
|
Kinetic.Util.extend(Kinetic.Container, Kinetic.Node);
|
2013-02-16 10:20:34 +08:00
|
|
|
|
|
|
|
// add getters setters
|
2013-03-15 23:33:05 +08:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Container, 'clipFunc');
|
2013-02-16 10:20:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set clipping function
|
|
|
|
* @name setClipFunc
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2013-02-16 10:20:34 +08:00
|
|
|
* @param {Number} deg
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get clipping function
|
|
|
|
* @name getClipFunc
|
2013-05-16 00:27:22 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Container.prototype
|
2013-02-16 10:20:34 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
})();
|