(Adjust values to suit your needs)
- Determine the initial stroke values for your
.donut-chart-1
. The required value is ceil((radius of your circle)*2*Pi)
.
.donut-chart-1 {
stroke-dasharray: 440; /* circumference of your circle */
stroke-dashoffset: 440; /* specify the offset for a starting gap, not the dash*/
}
- Name the animation as
donut-chart-1
and set the value for stroke-dashoffset
to initiate the animation. The targeted value is (your initial value) - (your initial value) * %
. For instance, if you want to animate 90%, it would be 44
and 75% 110
.
@keyframes donut-chart-1 {
to {
stroke-dashoffset: 110; /* animates 75% */
}
}
- Affect the
.circle
element within .donut-chart-1
with the previously defined animation:
.donut-chart-1 .circle {
animation: donut-chart-1 1s ease-out forwards;
}
You may not want to manually adjust these values, so utilize the following JavaScript code to calculate the path length of your SVG element and set the values accordingly:
var path = document.querySelector('circle');
var length = path.getTotalLength();
Complete 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>