I am new on D3 and I am trying to detect when a shape overlap a path using D3 library. Actually the functionality of my code is that you can drag everywhere in the canvas a black rectangle and I need now to detect when the rectangle touch the circular path and attach to it like a magnet. I can't find nothing similar on the web so I wanna ask you if you can give me some hints. I think I have to detect rectangle's position and path coordinates.
Here you can see my code: https://jsfiddle.net/hsspve49/
var drag = d3.drag().on("drag", function () {
var rect = d3.select(this);
var theta = Math.atan2(d3.event.y - height/2, d3.event.x - width/2) * 180 / Math.PI
rect
.attr("x", d3.event.x)
.attr("y", d3.event.y)
.attr('transform', `rotate(${theta + 90}, ${d3.event.x}, ${d3.event.y})`)
Thank you in advance.