Articles in this section
Category / Section

How to Retrieve X,Y Coordinates of Angular Diagram Using Native Mouse?

2 mins read

In a Angular Diagram, obtaining the X and Y coordinates becomes crucial for various interactive functionalities. While the mouseover event is commonly used when hovering over nodes or connectors, it may not trigger when the mouse is over an empty diagram space. To precisely capture the mouse’s position over the entire diagram, the mouseMove event should be employed.

Refer to the following code snippet to understand how to acquire the exact X and Y coordinates of a diagram using the mouseMove event.

The code below utilizes the MouseEvent arguments and diagram objects to accurately determine the X and Y coordinates. Adjustments are made for the diagram’s position, scale, and transforms to provide precise coordinates.

// Initialize diagram and mousemove event
<div (mousemove)="DiagrammouseOver($event)">
     <ejs-diagram #diagram="" id="diagram" width="100%" height="780px" [nodes]="nodes" [connectors]="connectors">
     </ejs-diagram>
</div>


// Function for mousemove event
public DiagrammouseOver(arg: MouseEvent) {
   var offsetX;
   var offsetY;

   offsetX = arg.clientX;
   offsetY = arg.clientY;

   var position: Size = new Size();
   position = this.getRulerSize();
   var boundingRect = this.diagram.element.getBoundingClientRect();
   offsetX =
     offsetX +
     this.diagram.diagramCanvas.scrollLeft -
     boundingRect.left -
     position.width;
   offsetY =
     offsetY +
     this.diagram.diagramCanvas.scrollTop -
     boundingRect.top -
     position.height;
   offsetX /= this.diagram.scroller.transform.scale;
   offsetY /= this.diagram.scroller.transform.scale;
   offsetX -= this.diagram.scroller.transform.tx;
   offsetY -= this.diagram.scroller.transform.ty;
   console.log('offsetX:' + offsetX + ',           offsetY:' + offsetY);
 }
 public getRulerSize(): Size {
   let top: number = 0;
   let left: number = 0;
   if (this.diagram.rulerSettings.showRulers) {
     top = this.diagram.rulerSettings.horizontalRuler.thickness;
     left = this.diagram.rulerSettings.verticalRuler.thickness;
   }
   return new Size(left, top);
 }

For a practical demonstration, refer to the
sample on stackblitz
Conclusion

Hope you enjoyed learning about how to retrieve X, Y coordinates of an Angular Diagram using the native mouse.

You can refer to our Angular Diagram feature tour page to learn about its other groundbreaking feature representations. You can explore our Angular Diagram documentation to understand how to present and manipulate data.

For current customers, you can check out our Angular components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our Angular Diagram and other Angular components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied