用鼠标点击事件连接两个圆圈

时间:2017-01-01 23:46:37

标签: javascript d3.js svg geometry

我想用鼠标事件连接两个或更多个圈子。我有以下代码

      var jsonCircles = [
          { "x_axis": 30, "y_axis": 30, "radius": 20, "color" : "green" , "class":"circleFirst"},
          { "x_axis": 70, "y_axis": 70, "radius": 20, "color" : "purple" , "class": "circleSecond"},
          { "x_axis": 110, "y_axis": 100, "radius": 20, "color" : "red", "class":"circleThird"}];

    var spaceHeight = 500;
    var spaceWidth = 500;
    var keep = false,
        mouseStart = null, path = null;

var dataObj = {};
var locationsObj = "locations";
dataObj[locationsObj] = {};
dataObj[locationsObj].source = [];
dataObj[locationsObj].target = [];


    var drag = d3.behavior.drag()
        .on("dragstart", function() {
            d3.event.sourceEvent.stopPropagation();
        })
        .on("drag", dragmove);

    function dragmove(d)
    {
        //boundary of svg area
        var x = Math.max(0, Math.min(spaceWidth - 100, d3.event.x));
        var y = Math.max(0, Math.min(spaceHeight - 50, d3.event.y));
        d3.select(this).attr("transform", "translate(" + x + "," + y + ")"); //main objects
    }


    var svgContainer = d3.select("body").append("svg")
                                       .attr("width", spaceWidth)
                                        .attr("height", spaceHeight)
                                        .on("mousedown", mousedown)
                                        .on("mousemove", mousemove)
                                        .on("mouseup", mouseup);


    var circles = svgContainer.selectAll("circle")
                              .data(jsonCircles)
                              .enter()
                              .append("circle");

    var circleAttributes = circles
                           .attr("cx", function (d) { return d.x_axis; })
                           .attr("cy", function (d) { return d.y_axis; })
                           .attr("r", function (d) { return d.radius; })
                           .attr("class", function(d) { return d.class;})
                           .style("fill", function(d) { return d.color; })
                           .call(drag);




    function mousedown()
    {
        path = svgContainer.append("path")
            .style("stroke", "gray")
            .style("stroke-width", "2px")
            .style("fill", "none");
        keep = true;
        mouseStart = d3.mouse(this);

    }

    function mouseup()
    {
        keep= false;
    }

    function mousemove()
    {
        if(keep)
        {
            var mouseEnd = d3.mouse(this);
            var dx = mouseStart[0] - mouseEnd[0],
                dy = mouseStart[1] - mouseEnd[1],
                dr = Math.sqrt(dx * dx + dy*dy);
            path.attr("d", "M" +
                mouseStart[0] + "," +
                mouseStart[1] + "A" +
                dr + "," + dr + " 0 0,1 " +
                mouseEnd[0] + "," +
                mouseEnd[1]);
        }

    }

1)我如何按照课程连接它们?我将把类信息推送到dataObj中,如代码所示。

enter image description here

2)我只需要在两个不在svg区域内的对象之间绘制。当我在svg空间中绘制一条线时,需要将其转换为背景颜色。对此有什么建议吗?(*)

  • 我使用mousemove函数绘制曲线或线条。当我将鼠标从一个圆圈拖到另一个圆圈时,如果它在另一个圆的半径内,则需要绘制一条线。如果不是,就没有线。您可以在下图中看到。附加信息:圆圈将是可拖动的圆圈。因此,当我选择并移动圆圈时,需要刷新路径。

enter image description here

提前致谢,

1 个答案:

答案 0 :(得分:1)

这是我对它的看法。尝试点击每个圈子。

https://jsfiddle.net/guanzo/f5c22h08/2/

不确定你的意思"当我在svg空间画一条线时,它需要转换成背景颜色",所以我把它当作被点击的圆圈的背景颜色。

另外,我不知道如何创建像你图像中那样的线条。

sudo apt-get install build-essential