Search code examples
csssvgcss-animationssvg-animate

how can i animate svg svg stroke line animation from left to right?


svg stroke animate start from right, but i want to stroke that animate form left to right. After animating stroke remain stands. can add any animate css class like fadeIn during the animation

.cls-1 {
  fill: none;
  stroke: #00a139;
  stroke-miterlimit: 10;
}

svg {
  width: 100%;
  height: 100vh;
  margin-left: auto;
  margin-right: auto;
  display: block;
  background: #1e1e1e;
}

#Path_70 {
  stroke-dasharray: 1800;
  stroke-dashoffset: 1800;
  animation: dash 5s linear forwards;
}

@keyframes dash {
  to {
    stroke-dashoffset: 0;
  }
}
<svg id="ex6" xmlns="http://www.w3.org/2000/svg" viewBox="2080.831 303.745 1673.195 406.547">
  <defs>
  </defs>
  <g id="Group_191" data-name="Group 191" transform="translate(2393 93)">
    <path id="Path_70" data-name="Path 70" class="cls-1" d="M1700.935,169.155s-227.809,11.434-421.654,140.759c-174.322,116.314-275.519,110.6-373.713,41.794C786.235,268.022,551.495-57.262,63.3,9.47" transform="translate(-361.422 210.687)"/>
  </g>
</svg>


Solution

  • You can make the stroke-dashoffset a negative value:

    .cls-1 {
      fill: none;
      stroke: #00a139;
      stroke-miterlimit: 10;
    }
    
    svg {
      width: 100%;
      height: 100vh;
      margin-left: auto;
      margin-right: auto;
      display: block;
      background: #1e1e1e;
    }
    
    #Path_70 {
      stroke-dasharray: 1800;
      stroke-dashoffset: -1800;
      animation: dash 5s linear forwards;
    }
    
    @keyframes dash {
      to {
        stroke-dashoffset: 0;
      }
    }
    <svg id="ex6" xmlns="http://www.w3.org/2000/svg" viewBox="2080.831 303.745 1673.195 406.547">
      <defs>
      </defs>
      <g id="Group_191" data-name="Group 191" transform="translate(2393 93)">
        <path id="Path_70" data-name="Path 70" class="cls-1" d="M1700.935,169.155s-227.809,11.434-421.654,140.759c-174.322,116.314-275.519,110.6-373.713,41.794C786.235,268.022,551.495-57.262,63.3,9.47" transform="translate(-361.422 210.687)"/>
      </g>
    </svg>