How to get Children that have same type Kineticjs?

穿精又带淫゛_ 提交于 2019-12-06 13:34:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!