這篇文章主要介紹HTML5 Canvas怎么實(shí)現(xiàn)圓形進(jìn)度條并顯示數(shù)字百分比效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

實(shí)現(xiàn)效果

1.首先創(chuàng)建html代碼
<canvas id="canvas" width="500" height="500" style="background:#000;"></canvas>
2.創(chuàng)建canvas環(huán)境
var canvas = document.getElementById('canvas'), //獲取canvas元素
context = canvas.getContext('2d'), //獲取畫圖環(huán)境,指明為2d
centerX = canvas.width/2, //Canvas中心點(diǎn)x軸坐標(biāo)
centerY = canvas.height/2, //Canvas中心點(diǎn)y軸坐標(biāo)
rad = Math.PI*2/100, //將360度分成100份,那么每一份就是rad度
speed = 0.1; //加載的快慢就靠它了3.繪制5像素寬的運(yùn)動(dòng)外圈
//繪制5像素寬的運(yùn)動(dòng)外圈
function blueCircle(n){
context.save();
context.strokeStyle = "#fff"; //設(shè)置描邊樣式
context.lineWidth = 5; //設(shè)置線寬
context.beginPath(); //路徑開始
context.arc(centerX, centerY, 100 , -Math.PI/2, -Math.PI/2 +n*rad, false); //用于繪制圓弧context.arc(x坐標(biāo),y坐標(biāo),半徑,起始角度,終止角度,順時(shí)針/逆時(shí)針)
context.stroke(); //繪制
context.closePath(); //路徑結(jié)束
context.restore();
}4.繪制白色外圈
//繪制白色外圈
function whiteCircle(){
context.save();
context.beginPath();
context.lineWidth = 2; //設(shè)置線寬
context.strokeStyle = "red";
context.arc(centerX, centerY, 100 , 0, Math.PI*2, false);
context.stroke();
context.closePath();
context.restore();
}5.百分比文字繪制
function text(n){
context.save(); //save和restore可以保證樣式屬性只運(yùn)用于該段canvas元素
context.strokeStyle = "#fff"; //設(shè)置描邊樣式
context.font = "40px Arial"; //設(shè)置字體大小和字體
//繪制字體,并且指定位置
context.strokeText(n.toFixed(0)+"%", centerX-25, centerY+10);
context.stroke(); //執(zhí)行繪制
context.restore();
}6.讓它運(yùn)動(dòng)起來
//動(dòng)畫循環(huán)
(function drawFrame(){
window.requestAnimationFrame(drawFrame);
context.clearRect(0, 0, canvas.width, canvas.height);
whiteCircle();
text(speed);
blueCircle(speed);
if(speed > 100) speed = 0;
speed += 0.1;
}());完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5 Canvas 圓形進(jìn)度條并顯示數(shù)字百分比</title>
<style>
*{margin:0;padding:0;}
body{text-align:center;background-color:#000;}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500" style="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中心點(diǎn)x軸坐標(biāo)
centerY = canvas.height/2, //Canvas中心點(diǎn)y軸坐標(biāo)
rad = Math.PI*2/100, //將360度分成100份,那么每一份就是rad度
speed = 0.1; //加載的快慢就靠它了
//繪制5像素寬的運(yùn)動(dòng)外圈
function blueCircle(n){
context.save();
context.strokeStyle = "#fff"; //設(shè)置描邊樣式
context.lineWidth = 5; //設(shè)置線寬
context.beginPath(); //路徑開始
context.arc(centerX, centerY, 100 , -Math.PI/2, -Math.PI/2 +n*rad, false); //用于繪制圓弧context.arc(x坐標(biāo),y坐標(biāo),半徑,起始角度,終止角度,順時(shí)針/逆時(shí)針)
context.stroke(); //繪制
context.closePath(); //路徑結(jié)束
context.restore();
}
//繪制白色外圈
function whiteCircle(){
context.save();
context.beginPath();
context.lineWidth = 2; //設(shè)置線寬
context.strokeStyle = "red";
context.arc(centerX, centerY, 100 , 0, Math.PI*2, false);
context.stroke();
context.closePath();
context.restore();
}
//百分比文字繪制
function text(n){
context.save(); //save和restore可以保證樣式屬性只運(yùn)用于該段canvas元素
context.strokeStyle = "#fff"; //設(shè)置描邊樣式
context.font = "40px Arial"; //設(shè)置字體大小和字體
//繪制字體,并且指定位置
context.strokeText(n.toFixed(0)+"%", centerX-25, centerY+10);
context.stroke(); //執(zhí)行繪制
context.restore();
}
//動(dòng)畫循環(huán)
(function drawFrame(){
window.requestAnimationFrame(drawFrame);
context.clearRect(0, 0, canvas.width, canvas.height);
whiteCircle();
text(speed);
blueCircle(speed);
if(speed > 100) speed = 0;
speed += 0.1;
}());
}
</script>
</body>
</html>以上是“HTML5 Canvas怎么實(shí)現(xiàn)圓形進(jìn)度條并顯示數(shù)字百分比效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:HTML5Canvas怎么實(shí)現(xiàn)圓形進(jìn)度條并顯示數(shù)字百分比效果-創(chuàng)新互聯(lián)
瀏覽地址:http://www.chinadenli.net/article28/deedjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、營(yíng)銷型網(wǎng)站建設(shè)、Google、企業(yè)建站、ChatGPT、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容