圆环进度条
绘制背景圆环
function draw(){
ctx.clearRect(0,0,cvs.width, cvs.height);
var x = cvs.width/2;
var y = cvs.height/2;
ctx.lineWidth = 16;
ctx.lineCap = "round";
var r = Math.min(x, y) - ctx.lineWidth/2;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2);
ctx.strokeStyle = "purple";
ctx.stroke();
}
绘制进度条
window.onload = function (){
var cvs = document.getElementById("cvs");
var ctx = cvs.getContext("2d");
var target = 0.4;
function draw(){
ctx.clearRect(0,0,cvs.width, cvs.height);
var x = cvs.width/2;
var y = cvs.height/2;
ctx.lineWidth = 16;
ctx.lineCap = "round";
var r = Math.min(x, y) - ctx.lineWidth/2;
ctx.beginPath();
var start = 0;
var end = Math.PI*2*target;
ctx.arc(x, y, r, start, end);
ctx.strokeStyle = "purple";
ctx.stroke();
}
}
将开始点移动到12点位置
var start = Math.PI*3/2;
var end = Math.PI*2*target + start;
终点延长动效
window.onload = function (){
var cvs = document.getElementById("cvs");
var ctx = cvs.getContext("2d");
var total = 13;
var progress = 0;
var target = 0.4;
function draw(){
if (progress > total)
return;
ctx.clearRect(0,0,cvs.width, cvs.height);
var x = cvs.width/2;
var y = cvs.height/2;
ctx.lineWidth = 16;
ctx.lineCap = "round";
var r = Math.min(x, y) - ctx.lineWidth/2;
ctx.beginPath();
var start = Math.PI*3/2;
var p = target*progress/total;
var end = Math.PI*2*p + start;
ctx.arc(x, y, r, start, end);
ctx.strokeStyle = "purple";
ctx.stroke();
progress++;
}
setInterval(draw, 100);
}
起点旋转动效
var start = Math.PI*3/2*progress/total;
绘制文字
function draw(){
var p = target*progress/total;
var text = (p*100).toFixed(2)+"%";
ctx.font = "25px Arial";
ctx.fillStyle = "orange";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, cvs.width/2, cvs.height/2);
}
完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
window.onload = function (){
var cvs = document.getElementById("cvs");
var ctx = cvs.getContext("2d");
var total = 13;
var progress = 0;
var target = 0.4;
function draw(){
if (progress > total)
return;
ctx.clearRect(0,0,cvs.width, cvs.height);
var x = cvs.width/2;
var y = cvs.height/2;
ctx.lineWidth = 16;
ctx.lineCap = "round";
var r = Math.min(x, y) - ctx.lineWidth/2;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2);
ctx.strokeStyle = "#87CEEB";
ctx.stroke();
ctx.beginPath();
var start = Math.PI*3/2*progress/total;
var p = target*progress/total;
var end = Math.PI*2*p + start;
ctx.arc(x, y, r, start, end);
ctx.strokeStyle = "purple";
ctx.stroke();
var text = (p*100).toFixed(2)+"%";
ctx.font = "25px Arial";
ctx.fillStyle = "orange";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, cvs.width/2, cvs.height/2);
progress++;
}
setInterval(draw, 100);
}
</script>
</head>
<body>
<canvas id="cvs" width="200" height="200"></canvas>
</body>
</html>