简体   繁体   English

Kinetic.js使用字符串搜索ID

[英]Kinetic.js Use a String to Search for ID

EDIT: I want to use a string rectString because I will be using a loop to get all the rectangles and checking to see if any of them have a specific property. 编辑:我想使用一个字符串rectString,因为我将使用一个循环来获取所有的矩形,并检查它们是否具有特定的属性。

I have a set of rectangles, say their names are 'rect1' , 'rect2' , and 'rect3'. 我有一组矩形,说它们的名称是'rect1','rect2'和'rect3'。 I have been trying to search different ways to search my stage such as: 我一直在尝试寻找不同的方式来搜索自己的舞台,例如:

var rectString="rect1";
var method1= stage.get(rectString)[0];
var method2= stage.get(rectString);
var method3 =stage.find(rectString);
var method4=node.getAttr(rectString);

and unfortunately none of these work. 不幸的是,这些都不起作用。 I am trying to get the stroke color of the shape and then change it using the ID of the shape. 我正在尝试获取形状的笔触颜色,然后使用形状的ID对其进行更改。 Thanks for your help 谢谢你的帮助

This will get all the rectangles on the stage into a collection: 这会将舞台上的所有矩形放入集合中:

var allRectangles=stage.find("Rect");

Then you can apply a function to each rectangle like this: 然后,您可以将函数应用于每个矩形,如下所示:

// run a function for each element in the allRectangles collection

allRectangles.each(function(rect){

    // check if this rect has a red stroke

    if(rect.stroke()=="red"){

        // if the stroke is red, change the stroke to blue

        rect.stroke("blue");

    }

});

layer.draw();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM