How to Retrieve X, Y Coordinates of JavaScript Diagram?
This article how to Retrieve X, Y Coordinates of JavaScript Diagram. In a JavaScript 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.
diagram = new Diagram({
width: '100%', height: '645px', nodes: nodes,
});
diagram.appendTo('#diagram');
function created(): void {
diagram.fitToPage({ mode: 'Width' });
}
function DiagrammouseOver(arg:MouseEvent) {
var offsetX;
var offsetY;
offsetX = arg.clientX;
offsetY = arg.clientY;
var position:Size = new Size();
position = getRulerSize(diagram);
var boundingRect = diagram.element.getBoundingClientRect();
offsetX = offsetX + diagram.diagramCanvas.scrollLeft - boundingRect.left - position.width;
offsetY = offsetY + diagram.diagramCanvas.scrollTop - boundingRect.top - position.height;
offsetX /= diagram.scroller.transform.scale;
offsetY /= diagram.scroller.transform.scale;
offsetX -= diagram.scroller.transform.tx;
offsetY -= diagram.scroller.transform.ty;
console.log('offsetX:' + offsetX + ', offsetY:' + offsetY);
}
function getRulerSize(diagram: Diagram): Size {
let top: number = 0;
let left: number = 0;
if (diagram.rulerSettings.showRulers) {
top = diagram.rulerSettings.horizontalRuler.thickness;
left = diagram.rulerSettings.verticalRuler.thickness;
}
return new Size(left, top);
}
//Enable or disable the AspectRatio for Node.
document.addEventListener('mousemove',DiagrammouseOver)
Refer to the working sample for additional details and implementation: sample on StackBlitz.
Conclusion
We hope you enjoyed learning about how to Retrieve X, Y Coordinates of a
Diagram.
You can refer to our JavaScript Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Diagram example to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
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, BoldDesk Support, or feedback
portal. We are always happy to assist you!