Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

d3.js - d3 version 4 workaround for drag origin

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);
            });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 }};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...