我已经让它工作得非常粗略:http: //jsfiddle.net/UT69H/18/
这真的很紧张,但它会随着路径旋转。请注意,我将 mousemove 处理程序移动到了 body 元素,这样如果您的鼠标移出框本身,它就不会突然停止。
bod.on "mousemove", () ->
if mousedown
pos = getPosHandle { x: d3.mouse(h[0][0])[0], y: d3.mouse(h[0][0])[1] }
pos1 = getPosHandle { x: d3.mouse(h[0][0])[0], y: d3.mouse(h[0][0])[1] - 1 }
pos2 = getPosHandle { x: d3.mouse(h[0][0])[0], y: d3.mouse(h[0][0])[1] + 1 }
ang = Math.round(Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x) * 180 / Math.PI) - 90
h.attr "transform", "rotate(#{ang} #{pos.x} #{pos.y})"
h.attr "x", (pos.x - h.attr("width") / 2)
h.attr "y", (pos.y - h.attr("height") / 2)
这更好一点:http: //jsfiddle.net/UT69H/19/
bod.on "mousemove", () ->
if mousedown
pos = getPosHandle { x: d3.mouse(bod[0][0])[0], y: d3.mouse(bod[0][0])[1] }
pos1 = getPosHandle { x: d3.mouse(bod[0][0])[0], y: d3.mouse(bod[0][0])[1] - 1 }
pos2 = getPosHandle { x: d3.mouse(bod[0][0])[0], y: d3.mouse(bod[0][0])[1] + 1 }
ang = Math.round(Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x) * 180 / Math.PI) - 90
h.attr "transform", "rotate(#{ang} #{pos.x} #{pos.y})"
h.attr "x", (pos.x - h.attr("width") / 2)
h.attr "y", (pos.y - h.attr("height") / 2)
您可以通过进一步间隔测试点来减少抖动:http: //jsfiddle.net/UT69H/21/
bod.on "mousemove", () ->
if mousedown
pos = getPosHandle { x: d3.mouse(bod[0][0])[0], y: d3.mouse(bod[0][0])[1] }
pos1 = getPosHandle { x: d3.mouse(bod[0][0])[0], y: d3.mouse(bod[0][0])[1] - 4 }
pos2 = getPosHandle { x: d3.mouse(bod[0][0])[0], y: d3.mouse(bod[0][0])[1] + 4 }
ang = Math.round(Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x) * 180 / Math.PI) - 90
h.attr "transform", "rotate(#{ang} #{pos.x} #{pos.y})"
h.attr "x", (pos.x - h.attr("width") / 2)
h.attr "y", (pos.y - h.attr("height") / 2)