JSFiddle - React, Tailwind, and code Playground
by Annie Lagang
HTML
<div class="pie">
<!-- clip is used for reversing the colors for angles more than 180 -->
<div class="clip1">
<div class="slice1"></div>
</div>
<div class="clip2">
<div class="slice2"></div>
</div>
<div class="status"></div>
</div>
CSS
* {
margin:0;
padding:0;
}
.pie {
background-color:#ecc0b7;
width:120px;
height:120px;
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%;
position:relative;
}
.clip1 {
position:absolute;
top:0;
left:0;
width:120px;
height:120px;
clip:rect(0px, 120px, 120px, 60px);
}
.slice1 {
position:absolute;
width:120px;
height:120px;
clip:rect(0px, 60px, 120px, 0px);
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%;
background-color:#f7e5e1;
border-color:#f7e5e1;
-moz-transform:rotate(0);
-webkit-transform:rotate(0);
-o-transform:rotate(0);
transform:rotate(0);
}
.clip2 {
position:absolute;
top:0;
left:0;
width:120px;
height:120px;
clip:rect(0, 60px, 120px, 0px);
}
.slice2 {
position:absolute;
width:120px;
height:120px;
clip:rect(0px, 120px, 120px, 60px);
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%;
background-color:#f7e5e1;
border-color:#f7e5e1;
-moz-transform:rotate(0);
-webkit-transform:rotate(0);
-o-transform:rotate(0);
transform:rotate(0);
}
.status {
position:absolute;
height:30px;
width:200px;
line-height:60px;
text-align:center;
top:50%;
margin-top:-35px;
font-size:60px;
}
JavaScript
$(document).ready(function () {
progressBarUpdate(37, 100);
});
function rotate(element, degree) {
element.css({
'-webkit-transform': 'rotate(' + degree + 'deg)',
'-moz-transform': 'rotate(' + degree + 'deg)',
'-ms-transform': 'rotate(' + degree + 'deg)',
'-o-transform': 'rotate(' + degree + 'deg)',
'transform': 'rotate(' + degree + 'deg)',
//'zoom': 1
});
}
function progressBarUpdate(x, outOf) {
var firstHalfAngle = 180;
var secondHalfAngle = 0;
// caluclate the angle
var drawAngle = x / outOf * 360;
// calculate the angle to be displayed if each half
if (drawAngle <= 180) {
firstHalfAngle = drawAngle;
} else {
secondHalfAngle = drawAngle - 180;
}
// set the transition
rotate($(".slice1"), firstHalfAngle);
rotate($(".slice2"), secondHalfAngle);
// set the values on the text
// $(".status").html(x + " of " + outOf);
}