2012-07-09 12:56:52 +08:00
|
|
|
/**
|
|
|
|
* Container constructor. Containers are used to contain nodes or other containers
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Node
|
|
|
|
* @param {Object} config
|
2012-07-24 13:39:55 +08:00
|
|
|
* @param {Number} [config.x]
|
|
|
|
* @param {Number} [config.y]
|
|
|
|
* @param {Boolean} [config.visible]
|
|
|
|
* @param {Boolean} [config.listening] whether or not the node is listening for events
|
|
|
|
* @param {String} [config.id] unique id
|
|
|
|
* @param {String} [config.name] non-unique name
|
|
|
|
* @param {Number} [config.alpha] determines node opacity. Can be any number between 0 and 1
|
|
|
|
* @param {Object} [config.scale]
|
|
|
|
* @param {Number} [config.scale.x]
|
|
|
|
* @param {Number} [config.scale.y]
|
|
|
|
* @param {Number} [config.rotation] rotation in radians
|
|
|
|
* @param {Number} [config.rotationDeg] rotation in degrees
|
|
|
|
* @param {Object} [config.offset] offsets default position point and rotation point
|
|
|
|
* @param {Number} [config.offset.x]
|
|
|
|
* @param {Number} [config.offset.y]
|
|
|
|
* @param {Boolean} [config.draggable]
|
|
|
|
* @param {String} [config.dragConstraint] can be vertical, horizontal, or none. The default
|
|
|
|
* is none
|
|
|
|
* @param {Object} [config.dragBounds]
|
|
|
|
* @param {Number} [config.dragBounds.top]
|
|
|
|
* @param {Number} [config.dragBounds.right]
|
|
|
|
* @param {Number} [config.dragBounds.bottom]
|
|
|
|
* @param {Number} [config.dragBounds.left]
|
2012-07-09 12:56:52 +08:00
|
|
|
*/
|
2012-08-23 14:13:09 +08:00
|
|
|
Kinetic.Container = function(config) {
|
|
|
|
this._containerInit(config);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kinetic.Container.prototype = {
|
|
|
|
_containerInit: function(config) {
|
2012-07-04 03:07:27 +08:00
|
|
|
this.children = [];
|
2012-08-23 14:13:09 +08:00
|
|
|
Kinetic.Node.call(this, config);
|
2012-07-04 03:07:27 +08:00
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
|
|
|
* get children
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name getChildren
|
|
|
|
* @methodOf Kinetic.Container.prototype
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
getChildren: function() {
|
|
|
|
return this.children;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* remove all children
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name removeChildren
|
|
|
|
* @methodOf Kinetic.Container.prototype
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
removeChildren: function() {
|
|
|
|
while(this.children.length > 0) {
|
2012-09-27 03:50:08 +08:00
|
|
|
this.children[0].remove();
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
},
|
2012-05-27 07:49:58 +08:00
|
|
|
/**
|
|
|
|
* add node to container
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name add
|
|
|
|
* @methodOf Kinetic.Container.prototype
|
2012-05-27 07:49:58 +08:00
|
|
|
* @param {Node} child
|
|
|
|
*/
|
|
|
|
add: function(child) {
|
2012-07-04 13:08:59 +08:00
|
|
|
child._id = Kinetic.Global.idCounter++;
|
2012-05-27 07:49:58 +08:00
|
|
|
child.index = this.children.length;
|
|
|
|
child.parent = this;
|
2012-08-14 14:06:29 +08:00
|
|
|
|
2012-05-27 07:49:58 +08:00
|
|
|
this.children.push(child);
|
|
|
|
var stage = child.getStage();
|
2012-07-27 13:58:38 +08:00
|
|
|
|
|
|
|
if(!stage) {
|
2012-08-01 12:35:04 +08:00
|
|
|
Kinetic.Global._addTempNode(child);
|
2012-05-27 07:49:58 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
stage._addId(child);
|
|
|
|
stage._addName(child);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pull in other nodes that are now linked
|
|
|
|
* to a stage
|
|
|
|
*/
|
2012-07-04 13:08:59 +08:00
|
|
|
var go = Kinetic.Global;
|
2012-05-27 07:49:58 +08:00
|
|
|
go._pullNodes(stage);
|
|
|
|
}
|
2012-10-04 10:38:12 +08:00
|
|
|
|
2012-05-27 07:49:58 +08:00
|
|
|
// chainable
|
|
|
|
return this;
|
|
|
|
},
|
2012-04-09 12:26:13 +08:00
|
|
|
/**
|
2012-04-09 12:37:10 +08:00
|
|
|
* return an array of nodes that match the selector. Use '#' for id selections
|
|
|
|
* and '.' for name selections
|
2012-04-09 12:26:13 +08:00
|
|
|
* ex:
|
|
|
|
* var node = stage.get('#foo'); // selects node with id foo
|
|
|
|
* var nodes = layer.get('.bar'); // selects nodes with name bar inside layer
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name get
|
|
|
|
* @methodOf Kinetic.Container.prototype
|
2012-04-09 12:26:13 +08:00
|
|
|
* @param {String} selector
|
|
|
|
*/
|
|
|
|
get: function(selector) {
|
2012-10-07 05:35:46 +08:00
|
|
|
var collection = new Kinetic.Collection();
|
2012-09-26 03:06:02 +08:00
|
|
|
// ID selector
|
2012-10-07 05:35:46 +08:00
|
|
|
if(selector.charAt(0) === '#') {
|
2012-10-08 10:14:14 +08:00
|
|
|
var node = this._getNodeById(selector.slice(1));
|
|
|
|
if(node)
|
|
|
|
collection.push(node);
|
2012-04-09 12:26:13 +08:00
|
|
|
}
|
2012-09-26 03:06:02 +08:00
|
|
|
// name selector
|
|
|
|
else if(selector.charAt(0) === '.') {
|
2012-10-08 10:14:14 +08:00
|
|
|
var nodeList = this._getNodesByName(selector.slice(1));
|
|
|
|
Kinetic.Collection.apply(collection, nodeList);
|
2012-04-28 13:54:39 +08:00
|
|
|
}
|
2012-10-07 05:35:46 +08:00
|
|
|
// unrecognized selector, pass to children
|
2012-04-09 12:26:13 +08:00
|
|
|
else {
|
2012-10-07 05:35:46 +08:00
|
|
|
var retArr = [];
|
|
|
|
var children = this.getChildren();
|
|
|
|
for(var n = 0; n < children.length; n++) {
|
|
|
|
retArr = retArr.concat(children[n]._get(selector));
|
|
|
|
}
|
|
|
|
Kinetic.Collection.apply(collection, retArr);
|
2012-04-09 12:26:13 +08:00
|
|
|
}
|
2012-10-07 05:35:46 +08:00
|
|
|
return collection;
|
|
|
|
},
|
|
|
|
_getNodeById: function(key) {
|
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage.ids[key] !== undefined && this.isAncestorOf(stage.ids[key])) {
|
|
|
|
return stage.ids[key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
_getNodesByName: function(key) {
|
|
|
|
var arr = this.getStage().names[key] || [];
|
2012-10-08 10:14:14 +08:00
|
|
|
return this._getDescendants(arr);
|
2012-10-07 05:35:46 +08:00
|
|
|
},
|
|
|
|
_get: function(selector) {
|
|
|
|
var retArr = Kinetic.Node.prototype._get.call(this, selector);
|
|
|
|
var children = this.getChildren();
|
|
|
|
for(var n = 0; n < children.length; n++) {
|
|
|
|
retArr = retArr.concat(children[n]._get(selector));
|
|
|
|
}
|
|
|
|
return retArr;
|
2012-09-26 03:06:02 +08:00
|
|
|
},
|
2012-10-04 10:38:12 +08:00
|
|
|
// extenders
|
|
|
|
toObject: function() {
|
|
|
|
var obj = Kinetic.Node.prototype.toObject.call(this);
|
|
|
|
|
|
|
|
obj.children = [];
|
|
|
|
|
|
|
|
var children = this.getChildren();
|
|
|
|
for(var n = 0; n < children.length; n++) {
|
|
|
|
var child = children[n];
|
|
|
|
obj.children.push(child.toObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
2012-09-26 03:06:02 +08:00
|
|
|
_getDescendants: function(arr) {
|
2012-10-08 10:14:14 +08:00
|
|
|
var retArr = [];
|
2012-04-09 12:26:13 +08:00
|
|
|
for(var n = 0; n < arr.length; n++) {
|
|
|
|
var node = arr[n];
|
|
|
|
if(this.isAncestorOf(node)) {
|
|
|
|
retArr.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-04 10:38:12 +08:00
|
|
|
return retArr;
|
2012-04-09 12:26:13 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* determine if node is an ancestor
|
|
|
|
* of descendant
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name isAncestorOf
|
|
|
|
* @methodOf Kinetic.Container.prototype
|
2012-04-09 12:26:13 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
2012-09-03 05:44:00 +08:00
|
|
|
/**
|
|
|
|
* clone node
|
|
|
|
* @name clone
|
2012-09-18 14:20:23 +08:00
|
|
|
* @methodOf Kinetic.Container.prototype
|
2012-09-03 05:44:00 +08:00
|
|
|
* @param {Object} attrs override attrs
|
|
|
|
*/
|
|
|
|
clone: function(obj) {
|
|
|
|
// call super method
|
|
|
|
var node = Kinetic.Node.prototype.clone.call(this, obj)
|
2012-09-26 03:06:02 +08:00
|
|
|
|
2012-09-03 05:44:00 +08:00
|
|
|
// perform deep clone on containers
|
|
|
|
for(var key in this.children) {
|
|
|
|
node.add(this.children[key].clone());
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
},
|
2012-07-07 12:45:18 +08:00
|
|
|
/**
|
|
|
|
* get shapes that intersect a point
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name getIntersections
|
|
|
|
* @methodOf Kinetic.Container.prototype
|
2012-07-07 12:45:18 +08:00
|
|
|
* @param {Object} point
|
|
|
|
*/
|
|
|
|
getIntersections: function() {
|
|
|
|
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments));
|
|
|
|
var arr = [];
|
|
|
|
var shapes = this.get('Shape');
|
|
|
|
|
|
|
|
for(var n = 0; n < shapes.length; n++) {
|
|
|
|
var shape = shapes[n];
|
|
|
|
if(shape.isVisible() && shape.intersects(pos)) {
|
|
|
|
arr.push(shape);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
|
|
|
* set children indices
|
|
|
|
*/
|
|
|
|
_setChildrenIndices: function() {
|
|
|
|
for(var n = 0; n < this.children.length; n++) {
|
|
|
|
this.children[n].index = n;
|
|
|
|
}
|
2012-10-08 10:14:14 +08:00
|
|
|
},
|
|
|
|
draw: function(canvas) {
|
|
|
|
if(Kinetic.Node.prototype._shouldDraw.call(this, canvas)) {
|
|
|
|
var children = this.children;
|
|
|
|
var length = children.length;
|
|
|
|
for(var n = 0; n < length; n++) {
|
|
|
|
children[n].draw(canvas);
|
|
|
|
}
|
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
2012-08-23 14:13:09 +08:00
|
|
|
};
|
|
|
|
Kinetic.Global.extend(Kinetic.Container, Kinetic.Node);
|