Skip to content

Instantly share code, notes, and snippets.

@Akiyamka
Created July 23, 2018 09:13
Show Gist options
  • Select an option

  • Save Akiyamka/de1d0428481822d9eb9e5353b37eef3f to your computer and use it in GitHub Desktop.

Select an option

Save Akiyamka/de1d0428481822d9eb9e5353b37eef3f to your computer and use it in GitHub Desktop.
<div id="cont" data-precent="100">
<svg id="svg" width="200" height="200" viewPort="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
<circle id="bar" r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
</svg>
</div>
<label for="percent">Type a percent!</label>
<input id="percent" name="percent">
const userInput = document.querySelector('#percent');
userInput.addEventListener('change', function() {
const inputVal = parseInt(this.value);
const progress = Number.isNaN(inputVal) ? 0 : Math.max(0, Math.min(100, inputVal));
const circle = document.querySelector('#svg #bar');
const r = circle.attributes.r.value;
const c = Math.PI*(r*2);
const precent = ((100-progress)/100)*c;
circle.style = `stroke-dashoffset: ${precent};`
const parent = document.querySelector('#cont');
console.log(parent.dataset)
parent.dataset.precent = progress;
#svg circle {
stroke-dashoffset: 0;
transition: stroke-dashoffset 1s ease;
stroke: #666;
stroke-width: 1em;
}
#svg #bar {
stroke: #FF9F1E;
}
#cont {
display: block;
height: 200px;
width: 200px;
margin: 2em auto;
position: relative;
}
#cont:after {
position: absolute;
display: block;
height: 160px;
width: 160px;
left: 50%;
top: 50%;
content: attr(data-precent)"%";
margin-top: -80px;
margin-left: -80px;
border-radius: 100%;
line-height: 160px;
font-size: 2em;
}
input {
color: #000;
}
/* Make things perty */
body {
height: 100%;
text-align: center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment