d3 draggable svg path line segment

coc*_*coa 5 javascript svg d3.js

我的任务是使用d3在mousedown上绘制svg形状.我一直试图找出如何使它们可拖动.我有svg线路(见这里)但我实际上需要使用路径.我已经非常接近,但我很难过.有人可以帮帮我吗?这是一些代码,是我的小提琴.

var drag = d3.behavior.drag().on('drag', dragmove);
function dragmove() {
    isDown = false;
    m3 = d3.mouse(this);
    var newArray = [ {x: (m1[0] + d3.event.dx), y: (m1[1] + d3.event.dy)},
                     {x: (m2[0] + d3.event.dx), y: (m2[1] + d3.event.dy)} ]; 
    line.attr('d', lineFunction(newArray));    
}
Run Code Online (Sandbox Code Playgroud)

鼠标按下

pathArray = [ {'x': m1[0], 'y': m1[1]}, {'x': m1[0], 'y': m1[1]} ];
    line = svg.append('path')
        .attr('d', lineFunction(pathArray))
        .attr({'stroke': 'purple', 'stroke-width': 5, 'fill': 'none'})
        .call(drag);
Run Code Online (Sandbox Code Playgroud)

鼠标移动

m2 = d3.mouse(this);
    pathArray[1] = {'x': m2[0], 'y': m2[1]};
    line.attr('d', lineFunction(pathArray));
Run Code Online (Sandbox Code Playgroud)

coc*_*coa 4

嗯,我非常接近。这是我在dragmove中改变的。现在效果很好。

var newArray = [ {x: (m1[0] += d3.event.dx), y: (m1[1] += d3.event.dy)},
                 {x: (m2[0] += d3.event.dx), y: (m2[1] += d3.event.dy)} ]; 
Run Code Online (Sandbox Code Playgroud)