Articles in this section
Category / Section

How to Position a Node Based on Mouse Position in JavaScript Diagram?

2 mins read

This article explains how to position a node based on the mouse position when dropping from the Tree View in the JavaScript Diagram. In the JavaScript Diagram component, when a node is dragged from the Tree View and dropped onto the diagram surface, the drop event is triggered. You can use this event to position the dropped node based on the current mouse location.

Within the drop event, you can access the dropped node using args.element. You can retrieve the current mouse position using:

diagram.eventHandler.currentPosition.x
diagram.eventHandler.currentPosition.y 

You can then assign these coordinates to the node’s offsetX and offsetY properties to position the node exactly where the mouse pointer is located at the time of the drop.
Here is the implementation:

drop: function (args) {
 let node = args.element;
 node.offsetX = diagram.eventHandler.currentPosition.x;
 node.offsetY = diagram.eventHandler.currentPosition.y;
 diagram.dataBind();
} 

This approach ensures that the node appears precisely at the mouse position when it is dropped from the tree view.

Refer to the working sample for additional details and implementation: Sample

Conclusion

We hope you enjoyed learning on how to position a node based on mouse position when dropping from the palette in JavaScript 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!

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