<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{margin:0;padding:0;}
body{text-align:center;}
</style>
</head>
<body>
<canvas id="canvas" width="200" height="200" style="position: absolute;left: 0px;background: #000;"></canvas>
<script>
window.onload = function(){
var canvas = document.getElementById('canvas'), //獲取canvas 元素
context = canvas.getContext('2d'), //獲取畫圖環(huán)境,指明為2d
centerX = canvas.width/2, //Canvas中心點x軸坐標
centerY = canvas.height/2, //Canvas中心點y軸坐標
rad = Math.PI*2/100, //將360度分成100份,那么每一份就是rad度
speed = 0.1; //加載的快慢就靠它了
//繪制5像素寬的運動外圈
function blueCircle1(n){
context.save();
context.strokeStyle = "red"; //設置描邊樣式
context.lineWidth = 5; //設置線寬
context.beginPath(); //路徑開始
context.arc(centerX, centerY, 50 , -Math.PI/2, -Math.PI/2 +n*rad, false); //用于繪制圓弧context.arc(x坐標,y坐標,半徑,起始角度,終止角度,順時針/逆時針)
context.stroke(); //繪制
context.closePath(); //路徑結(jié)束
context.restore();
}
//繪制白色外圈
function whiteCircle1(){
context.save();
context.beginPath();
context.lineWidth = 5; //設置線寬
context.strokeStyle = "#444444";
context.arc(centerX, centerY, 50 , 0, Math.PI*2, false);
context.stroke();
context.closePath();
context.restore();
}
//百分比文字繪制
function text(n){
context.save(); //save和restore可以保證樣式屬性只運用于該段canvas元素
context.strokeStyle = "#fff"; //設置描邊樣式
context.font = "20px Arial"; //設置字體大小和字體
//繪制字體,并且指定位置
context.strokeText(n.toFixed(0)+"%", centerX-15, centerY+10);//字的位置
context.stroke(); //執(zhí)行繪制
context.restore();
}
//動畫循環(huán)
(function drawFrame(){
window.requestAnimationFrame(drawFrame);
context.clearRect(0, 0, canvas.width, canvas.height);
whiteCircle1();
text(speed);
blueCircle1(speed);
if(speed > 100) speed = 0;
speed += 0.1;
}());
}
</script>
</body>
</html>
canvas圓形進度條注釋超全
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。