How to find out which graph item is located at particular coordinates
Questions & AnswersSummary
Description
The com.yworks.canvas.CanvasComponent offers a method which returns an Iterable over the
display objects which can be found at a given location: getCanvasObjects(worldX:Number, worldY:Number):Iterable.
If one wants to find out which graph item is hit by given coordinates, the most convenient way is to iterate over these objects. The following method does so and returns a node, if there is one found among the display objects' user objects (otherwise, it returns null):
private function findHitNode( graphCanvas:CanvasComponent, hitX:Number, hitY:Number ):INode {
var it:Iterator = graphCanvas.getCanvasObjects( hitX, hitY ).iterator();
while( it.hasNext() ) {
var hitObj:ICanvasObject = ICanvasObject( it.next() );
var hitNode:INode = hitObj.userObject as INode;
if( null != hitNode ) {
return hitNode;
}
}
return null;
}
Note, that if there is more than one node located at the given coordinates, this method will only find the one which is created first, which usually, but not necessarily, is the topmost node.