How to set the Origin (drag.origin) for drag behavior in d3 JavaScript library

后端 未结 2 469
陌清茗
陌清茗 2020-12-06 06:24

I am trying to implement drag behavior for group consisting from HTML-text and background-rectangle using the d3 framework. I was able to get it working, although when not s

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 06:46

    group element has no x/y attributes - no problem! create ones:

    var node = svg.append("g")
                .attr('x',x_1)
                .attr('y',y_1)
                .attr("class","node")
                .attr("name","Node_A")
                .attr("transform","translate(" + x_1 + ", " + y_1 +")")
                .call(drag);
    

    when, the .origin function from Lars will work

    and dont forget to update x,y attributes, while dragging:

    function dragged() {
    
        d3.select(this).attr("transform","translate(" + d3.event.x + ", " + d3.event.y +")");
        d3.select(this).attr("x",d3.event.x);
        d3.select(this).attr("y",d3.event.y);
    }
    

提交回复
热议问题