mirror of
https://github.com/konvajs/konva.git
synced 2025-04-24 19:03:56 +08:00
238 lines
7.3 KiB
JavaScript
238 lines
7.3 KiB
JavaScript
(function() {
|
|
/**
|
|
* Container constructor. Containers are used to contain nodes or other containers
|
|
* @constructor
|
|
* @augments Kinetic.Node
|
|
* @param {Object} config
|
|
*/
|
|
Kinetic.Container = function(config) {
|
|
this._containerInit(config);
|
|
};
|
|
|
|
Kinetic.Container.prototype = {
|
|
_containerInit: function(config) {
|
|
this.children = [];
|
|
Kinetic.Node.call(this, config);
|
|
},
|
|
/**
|
|
* get children
|
|
* @name getChildren
|
|
* @methodOf Kinetic.Container.prototype
|
|
*/
|
|
getChildren: function() {
|
|
return this.children;
|
|
},
|
|
/**
|
|
* remove all children
|
|
* @name removeChildren
|
|
* @methodOf Kinetic.Container.prototype
|
|
*/
|
|
removeChildren: function() {
|
|
while(this.children.length > 0) {
|
|
this.children[0].remove();
|
|
}
|
|
},
|
|
/**
|
|
* add node to container
|
|
* @name add
|
|
* @methodOf Kinetic.Container.prototype
|
|
* @param {Node} child
|
|
*/
|
|
add: function(child) {
|
|
var go = Kinetic.Global, children = this.children;
|
|
child.index = children.length;
|
|
child.parent = this;
|
|
children.push(child);
|
|
var stage = child.getStage();
|
|
|
|
if(!stage) {
|
|
go._addTempNode(child);
|
|
}
|
|
else {
|
|
stage._addId(child);
|
|
stage._addName(child);
|
|
|
|
/*
|
|
* pull in other nodes that are now linked
|
|
* to a stage
|
|
*/
|
|
go._pullNodes(stage);
|
|
}
|
|
|
|
// chainable
|
|
return this;
|
|
},
|
|
/**
|
|
* return an array of nodes that match the selector. Use '#' for id selections
|
|
* and '.' for name selections
|
|
* ex:
|
|
* var node = stage.get('#foo'); // selects node with id foo
|
|
* var nodes = layer.get('.bar'); // selects nodes with name bar inside layer
|
|
* @name get
|
|
* @methodOf Kinetic.Container.prototype
|
|
* @param {String} selector
|
|
*/
|
|
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) {
|
|
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] || [];
|
|
return this._getDescendants(arr);
|
|
},
|
|
_get: function(selector) {
|
|
var retArr = Kinetic.Node.prototype._get.call(this, selector);
|
|
var children = this.getChildren();
|
|
var len = children.length;
|
|
for(var n = 0; n < len; n++) {
|
|
retArr = retArr.concat(children[n]._get(selector));
|
|
}
|
|
return retArr;
|
|
},
|
|
// extenders
|
|
toObject: function() {
|
|
var obj = Kinetic.Node.prototype.toObject.call(this);
|
|
|
|
obj.children = [];
|
|
|
|
var children = this.getChildren();
|
|
var len = children.length;
|
|
for(var n = 0; n < len; n++) {
|
|
var child = children[n];
|
|
obj.children.push(child.toObject());
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
return retArr;
|
|
},
|
|
/**
|
|
* determine if node is an ancestor
|
|
* of descendant
|
|
* @name isAncestorOf
|
|
* @methodOf Kinetic.Container.prototype
|
|
* @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;
|
|
},
|
|
/**
|
|
* clone node
|
|
* @name clone
|
|
* @methodOf Kinetic.Container.prototype
|
|
* @param {Object} attrs override attrs
|
|
*/
|
|
clone: function(obj) {
|
|
// call super method
|
|
var node = Kinetic.Node.prototype.clone.call(this, obj)
|
|
|
|
// perform deep clone on containers
|
|
for(var key in this.children) {
|
|
node.add(this.children[key].clone());
|
|
}
|
|
return node;
|
|
},
|
|
/**
|
|
* get shapes that intersect a point
|
|
* @name getIntersections
|
|
* @methodOf Kinetic.Container.prototype
|
|
* @param {Object} point
|
|
*/
|
|
getIntersections: function() {
|
|
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments));
|
|
var arr = [];
|
|
var shapes = this.get('Shape');
|
|
|
|
var len = shapes.length;
|
|
for(var n = 0; n < len; n++) {
|
|
var shape = shapes[n];
|
|
if(shape.isVisible() && shape.intersects(pos)) {
|
|
arr.push(shape);
|
|
}
|
|
}
|
|
|
|
return arr;
|
|
},
|
|
/**
|
|
* set children indices
|
|
*/
|
|
_setChildrenIndices: function() {
|
|
var children = this.children, len = children.length;
|
|
for(var n = 0; n < len; n++) {
|
|
children[n].index = n;
|
|
}
|
|
},
|
|
/*
|
|
* draw both scene and hit graphs
|
|
*/
|
|
draw: function() {
|
|
this.drawScene();
|
|
this.drawHit();
|
|
},
|
|
drawScene: function(canvas) {
|
|
if(this.isVisible()) {
|
|
var children = this.children, len = children.length;
|
|
for(var n = 0; n < len; n++) {
|
|
children[n].drawScene(canvas);
|
|
}
|
|
}
|
|
},
|
|
drawHit: function() {
|
|
if(this.isVisible() && this.isListening()) {
|
|
var children = this.children, len = children.length;
|
|
for(var n = 0; n < len; n++) {
|
|
children[n].drawHit();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Kinetic.Global.extend(Kinetic.Container, Kinetic.Node);
|
|
})();
|