D3 tween - pause and resume controls

后端 未结 1 1541
独厮守ぢ
独厮守ぢ 2021-01-07 13:05

I am trying to edit this d3 example.

More specifically, I will try to apply the pause-resume controls of a pause resume guide in addition with a control bar like thi

相关标签:
1条回答
  • 2021-01-07 13:44

    Here's a quick implementation. The pause essentially cancels the current transition and the play resumes it based on time/position it was paused:

    var pauseValues = {
      lastT: 0,
      currentT: 0
    };
    function transition() {
      circle.transition()
          .duration(duration - (duration * pauseValues.lastT)) // take into account any pause
          .attrTween("transform", translateAlong(path.node()))
          .each("end", function(){
            pauseValues = {
              lastT: 0,
              currentT: 0
            };
            transition()
          });
    }
    function translateAlong(path) {
      var l = path.getTotalLength();
      return function(d, i, a) {
        return function(t) {
          t += pauseValues.lastT; // was it previously paused?
          var p = path.getPointAtLength(t * l);
          pauseValues.currentT = t; // just in case they pause it
          return "translate(" + p.x + "," + p.y + ")";
        };
      };
    }
    d3.select('button').on('click',function(d,i){
      var self = d3.select(this);
      if (self.text() == "Pause"){
        self.text('Play');
        circle.transition()
          .duration(0);
        setTimeout(function(){
          pauseValues.lastT = pauseValues.currentT; // give it a bit to stop the transition
        }, 100);
      }else{
        self.text('Pause');
        transition();
      }
    });
    transition();
    

    Example here.

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