Skip to content

Instantly share code, notes, and snippets.

@ArtemSites
Created September 7, 2019 10:18
Show Gist options
  • Save ArtemSites/95b04e0be1fc48014e96e2ab961ffd1b to your computer and use it in GitHub Desktop.
Save ArtemSites/95b04e0be1fc48014e96e2ab961ffd1b to your computer and use it in GitHub Desktop.
SVG_circle_progress_bar
<svg class="progress-bar" width="120" height="120">
<circle class="progress-bar__circle" stroke="#ff6e00" stroke-width="4" cx="60" cy="60" r="52" fill="transparent"/>
</svg>
<input type="number" id="inputPercent" value="0">
const circle = document.querySelector('.progress-bar__circle');
const radius = circle.r.baseVal.value;
// console.log(circle.r);
const circumference = 2*Math.PI * radius;
let inputPercent = document.getElementById('inputPercent');
console.log(inputPercent);
inputPercent.addEventListener('change', function() {
setProgressBar(inputPercent.value);
});
circle.style.strokeDasharray = `${circumference} ${circumference}`;
circle.style.strokeDashoffset = circumference;
function setProgressBar(percent) {
let offset = circumference - percent / 100 * circumference;
circle.style.strokeDashoffset = offset;
}
html,
body {
display: flex;
justify-content: center;
height: 100vh;
align-items: center;
background-color: #43a047;
}
.progress-bar__circle {
transform-origin: center;
transform: rotate(-90deg);
transition: stroke-dashoffset 500ms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment