问题
I have a trouble in Kineticjs, in my code:
var G1=new Kinetic.Group()
var sq=new Kinetic.Rect({
x:0,
y:0,
name:"sq"
})
var line1=new Kinetic.Line({
Points:[0,0,10,10],
name:"line1"
})
var line2=new Kinetic.Line({
Points:[0,0,50,50],
name:"line1"
})
G1.add(sq).add(line1).add(line2)
I know that to get the children from G1 just type "G1.getChildren()". But to get the children that has same type for example type Kinetic.Line I don't have any idea. Please help me, tahnks
回答1:
sorry for not having a tutorial on this, but you can select children by type like this:
var shapes = layer.get('Line');
in KineticJS, shape types are similar to DOM tags. You can select them by name.
回答2:
So the @Grant Timmerman example will work but I'm not sure that for children you can have only shapes so here what I suggest you to do:
var lines = G1.getChildren().filter(function(element) {
return element instanceof Kinetic.Line;
});
I don't know kinetic very well that's why I'm making that assumption.
回答3:
Simply use the Shape's shapeType property.
Here's an example that gets an array of KineticJS Lines
var lines = G1.getChildren().filter(function(element) {
return element.shapeType === 'Line';
});
来源:https://stackoverflow.com/questions/16552329/how-to-get-children-that-have-same-type-kineticjs