Stop moving other nodes while dragging one node in D3 forceSimulation

本秂侑毒 提交于 2021-02-08 03:41:18

问题


I made a D3 forced-directed graph by d3.forceSimulation() and attached the drag function. While clicking one node, I don't want other nodes to move accordingly. Now I can freeze the node being dragged by setting the d.fx and d.fy as the following:

function dragended(d) {
    if (!d3.event.active) simulation.alphaTarget(0);
    d.fx = d.x;
    d.fy = d.y;
}

Is it possible to freeze all the other nodes while dragging one node?


回答1:


Thanks @rioV8 for the hint! I tried to fix the other nodes while dragging one node.

node.call(d3.drag()
    .on('start', dragstarted)
    .on('drag', dragged)
    .on('end', dragended))

function dragged(d) {
    d.fx = d3.event.x;
    d.fy = d3.event.y;
    fix_nodes(d);
}

// Preventing other nodes from moving while dragging one node
function fix_nodes(this_node) {
    node.each(function(d){
        if (this_node != d){
            d.fx = d.x;
            d.fy = d.y;
        }
    });
}

Demo is here: https://jsfiddle.net/casbie/21dvjcgn/



来源:https://stackoverflow.com/questions/54460838/stop-moving-other-nodes-while-dragging-one-node-in-d3-forcesimulation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!