JS代码如下:
function draw(ctx, rate) {
ctx.clearRect(0,0,300,300);
ctx.beginPath();
ctx.strokeStyle = "gray";
ctx.arc(150, 150, 100, (1/3)*Math.PI, (2/3)*Math.PI, true);
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle = "green";
ctx.arc(150, 150, 100, (2/3)*Math.PI, (rate*5 + 2)/3*Math.PI, false);
ctx.stroke();
ctx.font = "30px Verdana";
var prevStr = rate >= 0.1 ? '' : '0';
// console.log("rate", rate);
ctx.textAlign = "center";
ctx.fillText(prevStr + Math.round(rate * 100) + '%',150,160);
}
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.lineCap = "round";
ctx.lineWidth = 15;
var rate = 0;
setInterval(function() {
if (rate >= 1) {
rate = 0;
}
draw(ctx, rate);
rate = Math.round((rate + 0.01) * 100) / 100;
}, 300);
HTML代码:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="300" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
</body>
</html>
效果如图: