(Tweak values to your benefit)
- Assign your
.donut-chart-1 stroke start off values. The value you need is ceil((radius of your circle)*2*Pi).
.donut-chart-1 {
stroke-dasharray: 440; /* circumference of your circle */
stroke-dashoffset: 440; /* set the offset, so you start with a gap, not the dash*/
}
- Define the animation name, in this case
donut-chart-1 and the value for stroke-dashoffset to trigger the animation. The target value is (your initial value) - (your initial value) * %. Taking the above example, if you want to animate 90%, it would be 44 and 75% 110.
@keyframes donut-chart-1 {
to {
stroke-dashoffset: 110; /* animates 75% */
}
}
- Impact the
.circle element inside .donut-chart-1 with the previously defined animation:
.donut-chart-1 .circle {
animation: donut-chart-1 1s ease-out forwards;
}
You probably don't wanna mess with these values by hand, so you could use the following JS, to calculate the path lenght of your svg element, and set the values accordingly:
var path = document.querySelector('circle');
var length = path.getTotalLength();
Full snippet:
.item {
position: relative;
float: left;
}
.item h2 {
text-align: center;
position: absolute;
line-height: 125px;
width: 100%;
}
svg {
transform: rotate(-90deg);
}
.donut-chart-1 {
stroke-dasharray: 440;
stroke-dashoffset: 440;
}
.donut-chart-1 .circle {
-webkit-animation: donut-chart-1 1s ease-out forwards;
animation: donut-chart-1 1s ease-out forwards;
}
@keyframes donut-chart-1 {
to {
stroke-dashoffset: 110;
}
}
<div class="item donut-chart-1">
<h2>HTML</h2>
<svg width="160" height="160" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<circle id="circle" class="circle" r="70.14149" cy="81" cx="81" stroke-width="8" stroke="#6fdb6f" fill="none"/>
</g>
</svg>
</div>