Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 version 4 workaround for drag origin

Tags:

d3.js

In v3 I used drag origin to prevent a noticeable jump due to the mouse-position/element-coordinates offset. Is there an alternative in v4 as the origin function has been removed?

var drag1 = d3.behavior.drag()
            .origin(function () {
                var t = d3.select(this);
                return {
                    x: t.attr("x") + d3.transform(t.attr("transform")).translate[0],
                    y: t.attr("y") + d3.transform(t.attr("transform")).translate[1]
                };
            })
            .on("drag", function (d, i) {
                d3.select(this).attr("transform", function (d, i) {
                    return "translate(" + [d3.event.x, d3.event.y] + ")"
                })
            });

    var drag2 = d3.behavior.drag()
            .origin(function () {
                var t = d3.select(this);
                return { x: t.attr("x"), y: t.attr("y") };
            })
            .on("drag", function (d, i) {
                d3.select(this)
                .attr("x", d3.event.x)
                .attr("y", d3.event.y);
            });
like image 350
user3359706 Avatar asked Jul 26 '16 21:07

user3359706


1 Answers

This worked:

var drag = d3.drag()
     .subject(subject)
     .on("start", function () {
         d3.event.sourceEvent.stopPropagation(); // silence other listeners
         if (d3.event.sourceEvent.which == 1)
             dragInitiated = true;
     })


function subject(d) {return { x: 0, y: d3.event.y }};
like image 189
user3359706 Avatar answered Oct 09 '22 12:10

user3359706