Reverse SVG line drawing animation

前端 未结 1 545
太阳男子
太阳男子 2021-02-06 01:47

I\'m trying to reverse how this SVG line animates. I need the line to draw from left to right but can only get it to work right to left. I tried changing the end point in illust

1条回答
  •  深忆病人
    2021-02-06 02:46

    As you would be aware, the stroke-dasharray property creates a dashed line pattern. When the value is assigned as 1400 it means that the length of the dash and the space between the dashes is 1400. That is for 0 to 1400 the line will be present and from 1400 to 2800 a dash would be present.

    Now when you change the stroke-dashoffset from 1400 to 0 it brings the line into view from one direction. Initially the offset is at 1400 and thus only the dash is visible (no line). When the offset is animated to 0, the dash moves out towards the left and the line (that is present from 0 to 1400) slowly comes into view.

    A simple method to do it from the other direction would be to animate it from 1400 to 2800. When this is done, the dash moves out towards the right as the line (that is present from 2800 to 4200) slowly comes into view.

    .phone-line {
      stroke-dasharray: 1400;
      animation: draw 4s ease-in;
    }
    @keyframes draw {
      from {
        stroke-dashoffset: 1400;
      }
      to {
        stroke-dashoffset: 2800;
      }
    }
    
    
      
    


    Another approach (as mentioned by Paul LeBeau in comments) would be to animate it from -1400 to 0. This also produces the same output as the above snippet.

    .phone-line {
      stroke-dasharray: 1400;
      animation: draw 4s ease-in;
    }
    @keyframes draw {
      from {
        stroke-dashoffset: -1400;
      }
      to {
        stroke-dashoffset: 0;
      }
    }
    
    
      
    

    Note: Ideal approach in my view would be to change the direction of your path itself to start from the left and move to the right instead of go from right to left. This is only a simple workaround to reverse the direction without altering the path.

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