Many, apparently because of the examples of Chris Koyera on css-tricks, put stroke-dasharray="1000" When it works, and when it doesn't. The fact is that it is necessary to accurately calculate the length of the animated line. For example, for a circle with a radius of 100px the circumference will be - 2*3.14*100= 628px This is the calculated value and should be substituted into the stroke-dasharray and the stroke-dashoffset .
SVG solution
Animation starts when you hover
<svg id="svg2" width="400" height="400" viewBox="0 0 400 400"> <circle id="back" cx="120" cy="120" r="100" fill="none" stroke="#d5d5d5" stroke-width="5"/> <circle transform="rotate(-90 120 120)" id="shape" cx="120" cy="120" r="100" stroke-width="5" stroke-dasharray='628' stroke-dashoffset='628' stroke="red" fill="none"> <animate attributeName="stroke-dashoffset" from="628" to="0" dur="4s" begin="svg2.mouseover" restart="whenNotActive" /> </circle> </svg>
CSS solution
Everything is carried away in styles, only circles remain in svg.
#back{ stroke:red; stroke-width:5; fill:none; stroke:#d5d5d5; } #shape { stroke-dashoffset:628; stroke-dasharray:628; stroke:red; stroke-width:5; fill:none; animation: circle_stroke 4s ease-in forwards; } @keyframes circle_stroke { 0% { stroke-dashoffset: 628; } 100% { stroke-dashoffset: 0; } }
<svg id="svg2" width="400" height="400" viewBox="0 0 400 400"> <circle id="back" cx="120" cy="120" r="100" /> <circle id="shape" transform="rotate(-90 120 120)" cx="120" cy="120" r="100" /> </svg>
Half circle
I will repeat the principle of drawing a line from zero to full length: when reducing the stroke-dashoffset from the maximum value (628) to zero, the line will be drawn from zero to full length. But we need to paint only half the circle, so we will reduce the stroke-dashoffset from 628px to 314px
<style> #shape { stroke-dashoffset:314; stroke-dasharray:628; stroke:red; stroke-width:5; fill:none; animation: circle_stroke 4s ease-in forwards; } @keyframes circle_stroke { 0% { stroke-dashoffset: 628; } 100% { stroke-dashoffset: 314; } } </style> <svg id="svg2" width="400" height="400" viewBox="0 0 400 400"> <circle id="shape" transform="rotate(-90 120 120)" cx="120" cy="120" r="100" /> </svg>