Stroke animation, how to attach another path to the appearing stroke?

后端 未结 3 608
广开言路
广开言路 2020-12-19 08:01

I have the following animation:

3条回答
  •  清歌不尽
    2020-12-19 09:05

    Yes, it's possible, however in this case you will need JavaScript. Please reed the comments in my code.

    let chart = document.querySelector("#currency_chart_path");
    // the length of the chart path
    let length = currency_chart_path.getTotalLength();
    // the request animation id
    let rid = null;
    // setting the stroke-dasharray and the stroke-dashoffset for the chart
    chart.style.strokeDasharray = length;
    chart.style.strokeDashoffset = length;
    // the animation frames
    let frames = length;
    // two points on the path: the actual point and an other point very near used to calculate the angle of rotation for the arrow
    let point1, point2;
    // the animation:
    function Frame() {
      rid = requestAnimationFrame(Frame);
      chart.style.strokeDashoffset = frames;
      //two points on the path: the actual point and an other point very near
      point1 = chart.getPointAtLength(length - frames);
      point2 = chart.getPointAtLength((length - frames + 2) % length);
      //the angle of rotation for the arrow
      angle = Math.atan2(point2.y - point1.y, point2.x - point1.x);
      // set the transformation for the arrow
      arrow.setAttribute(
        "transform",
        "translate(" +
          [point1.x, point1.y] +
          ")" +
          "rotate(" +
          angle * 180 / Math.PI +
          ")"
      );
    
      frames--;
      // stop the animation
      if (frames <= 2) {
        cancelAnimationFrame(rid);
        rid = null;
      }
    }
    
    Frame();
    svg{border:1px solid}
    
    
    
    
    
    

    This is inspired by a demo in Using SVG with CSS3 and HTML5: Vector Graphics for Web Design

提交回复
热议问题