1
我想要实现的是取mousemove事件的当前坐标,并将它们与圆上最近位置的坐标进行匹配。我已经成功地得到这个使用for循环,其在中圈每个可能的迭代点和坐标,以便进行比较找到的最近点部分工作:D3js找到圆上最近的点
for (var i = Math.PI; i > -Math.PI; i -= 0.01) {
// coords of current point in circle:
var curX = Math.sin(i+Math.PI/2)*radius + 200,
curY = Math.sin(i)*radius + 200;
// conditional which attempts to find the coords of the point closest to the cursor...
if (Math.abs((x - curX)) < distanceX && Math.abs((y - curY)) < distanceY) {
distanceX = Math.abs((x - curX));
distanceY = Math.abs((y - curY));
// and then assigns the values to the newX/newY variables
newX = curX;
newY = curY;
}
}
麻烦的是,这种解决方案会经常回错误的坐标,我不知道为什么。
的jsfiddle,看看我的意思是:
https://jsfiddle.net/eLn4jsue/