How to draw a path smoothly from start point to end point in D3.js

前端 未结 1 1857
心在旅途
心在旅途 2020-11-28 08:51

I have the following code which plots a line path based on sine function:

var data = d3.range(40).map(function(i) {
  return {x: i / 39, y: (Math.sin(i / 3)          


        
相关标签:
1条回答
  • 2020-11-28 09:19

    You can animate paths quite easily with stroke-dashoffset and and path.getTotalLength()

    var totalLength = path.node().getTotalLength();
    
    path
      .attr("stroke-dasharray", totalLength + " " + totalLength)
      .attr("stroke-dashoffset", totalLength)
      .transition()
        .duration(2000)
        .ease("linear")
        .attr("stroke-dashoffset", 0);
    

    http://bl.ocks.org/4063326

    0 讨论(0)
提交回复
热议问题