How to speed up the force layout animation in d3.js

后端 未结 2 572
情书的邮戳
情书的邮戳 2020-12-09 20:49

I am using D3.js for rendering about 500 nodes and links among them. It usually needs 10 seconds for the layout to settle down (the iteration to converge).

How do I

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 21:26

    Injecting a call / calls within ticked event handler can be better, the requestAnimationFrame method would cause a weird bug on MacBook with touch pad environment.

    function ticked() {
      for (let i = 0; i < 5; i++) {
        force.tick();
      }
    
      link.attr('x1', (d) => d.source.x)
          .attr('y1', (d) => d.source.y)
          .attr('x2', (d) => d.target.x)
          .attr('y2', (d) => d.target.y);
    
      node.attr('transform', (d) => `translate(${d.x}, ${d.y})`);
    }
    

提交回复
热议问题