gpt4 book ai didi

javascript - 如何进行转换 :translate-drag function of d3 more smooth?

转载 作者:行者123 更新时间:2023-11-29 16:06:51 24 4
gpt4 key购买 nike

this JSFiddle我已经在 svg 中实现了元素。我希望这组元素是可拖动的,我已经尝试使用 d3.dragtransform:translate。拖动不流畅。它在这里和那里闪烁和跳跃。

背后的原因是什么,如何克服?

拖动功能如下:

var dragcontainer = d3.drag()
.on("start", function() {})
.on("drag", function(d, i) {
var x = d3.event.x;
var y = d3.event.y;
d3.select(this.parentNode.parentNode).attr("transform", "translate(" + x + "," + y + ")");
})
.on("end", function() {});

最佳答案

而不是将拖动监听器附加到外部对象 div:

d3.select("#input-container-div").call(dragcontainer);

像这样将它添加到 svg 组:

d3.select(d3.select("#input-container-div").node().parentNode.parentNode).call(dragcontainer);

其次而不是使用 d3.event.x/d3.event.y

var x = d3.event.x;
var y = d3.event.y;

使用 dx 和 dy 获取鼠标移动差异并存储它以备将来拖动。

像这样:

  this.x = this.x || 0;//reset if not there
this.y = this.y || 0;

this.x += d3.event.dx;
this.y += d3.event.dy;
d3.select(this).attr("transform", "translate(" + this.x + "," + this.y + ")");

工作代码here

关于javascript - 如何进行转换 :translate-drag function of d3 more smooth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com