Kineticjs Tutorials (9)Author: ysm 9. Selector
Kinetic provides a Get method on stage, layer, and group objects to return the objects contained in the three.
9.1. According toID Get Object
To get an object with an ID, first assign an ID to the object , such as the ID of the rect created by the following code is id1:
<script>
var rect = new kinetic.rect ({
ID: "id1"
});
</script>
Note that theID is unique, and different objects cannot use the same ID, otherwise get can only return one of them.
Then, with the get method, theID selector "#" gets the object for the parameter:
<script>
var obj = stage.get (' #id1) [0];
var obj = layer.get (' #id1) [0];
var obj = group.get (' #id1) [0];
</script>
This place to note that this get returns an array, even if the ID is unique, so to get the object you want, you must bring the index of the array.
9.2. Get objects by name
To get an object by name, first give the object a name , such as the name of the rect created by the following code is name1:
<script>
var rect = new kinetic.rect ({
Name: "name1"
});
</script>
Unlike IDs,name is not unique, and different objects can share the same name.
Then use the get method, theID selector "." To get an object for a parameter:
<script>
var Objs = Stage.get ('. name1′);
var Objs = Layer.get ('. name1 ');
var Objs = Group.get ('. name1 ');
</script>
This place to note that this get returns an array that contains all the objects called this name.