简体   繁体   English

d3.drag在几秒钟后停止

[英]d3.drag stops after a few seconds

Today's question is about the d3.drag() function. 今天的问题是关于d3.drag()函数。 I don't really understand the simulation.alpha value. 我真的不明白simulation.alpha值。

I face the following problem: I want to drag my nodes around. 我面临以下问题:我想拖动我的节点。 Here is a little part of the code: 这是代码的一小部分:

var node = d3.select("svg").selectAll("circle")
             .data(nodes).enter().append("circle")
             .call(d3.drag().on("start", dragstart)
                            .on("drag", dragged)
                            .on("end", dragend));

function dragstart(d){
    //If i delete the following line, my simulation stops after some time.
    if (!d3.event.active) simulation.alphaTarget(0.3).restart();
    d.fx = d.x;
    d.fy = d.y;
};

Like I said in the comment, if I don't restart the alphaTarget , the dragging stops after a few seconds. 就像我在评论中说的那样,如果我不重新启动alphaTarget ,拖动会在几秒钟后停止。 Is someone able to explain this phenomenon? 有人能够解释这种现象吗?

Here is my complete JSFiddle: https://jsfiddle.net/FFoDWindow/zwtzq8rj/2/ 这是我完整的JSFiddle: https ://jsfiddle.net/FFoDWindow/zwtzq8rj/2/

Got same problem an hour ago. 一小时前遇到同样的问题。

Probably you have your render logic in tick function. 你可能在tick功能中有渲染逻辑。 If you want to be able to drag elements without restarting simulation you should implement render function (which tells elements to set their position from data's x and y). 如果你想在不重新启动模拟的情况下拖动元素,你应该实现渲染函数(它告诉元素从数据的x和y设置它们的位置)。 Add then the render function call to your "dragged" function 然后将渲染函数调用添加到“拖动”功能

function dragged(d) {
    d.x = d3.event.x;
    d.y = d3.event.y;
    render();
}

You can change also your tick() function to something like 您也可以将tick()函数更改为类似的函数

function tick(){
    render();
}

Not the most efficent way but it works for me. 不是最有效的方式,但它适用于我。

If you want to have simulation started after drag as well then your implementation is correct. 如果你想在拖动后开始模拟,那么你的实现是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM