詳解JavaScript+Canvas繪制環(huán)形進(jìn)度條
效果圖

思考
移動(dòng)端的場(chǎng)景里經(jīng)常會(huì)出現(xiàn)環(huán)形進(jìn)度條的功能,在實(shí)現(xiàn)這個(gè)功能前,我預(yù)想的解決方案大致有: echarts、antv、canvas、svg
前面兩種第三方提供的解決方案當(dāng)然是簡(jiǎn)單,拿到案例修整一下即可,但是需要下載依賴,而且代碼量不小。有沒有不需要依賴第三方包,采用原生的寫法,獨(dú)立封裝成一個(gè)組件,降低耦合,而且性能優(yōu)越?
當(dāng)然,那就主要介紹canvas的使用
實(shí)現(xiàn)思路
可以展示整個(gè)圓、半圓以及任意角度弧形(左右對(duì)稱)的進(jìn)度條。整體思路如下:
1.先確定展示的形狀,是整個(gè)圓、半圓還是一般的弧形
2.把確定好形狀的圓弧均分100等份,計(jì)算出每一份所占的弧度
3.灰色圓弧占100份,紅色圓弧最終占的份數(shù)由參數(shù)確定
4.設(shè)置setInterval定時(shí)器,重復(fù)執(zhí)行畫圖操作
- 清空畫布
- 先畫灰色的圓弧,占100份
- 再畫紅色的圓?。杭t色圓弧的份數(shù)從0開始,每次加1
- 畫紅色圓弧末端的紅色圓:難點(diǎn)在于根據(jù)角度確定紅色圓的圓心,這里面涉及到三角函數(shù),在草稿紙上畫個(gè)圖就大致明白了
- 當(dāng)紅色圓弧的份數(shù)達(dá)到指定值(傳的參數(shù))的時(shí)候,清除定時(shí)器
具體代碼實(shí)現(xiàn)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>Canvas</title>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
<script>
draw(66);
/**
* [順時(shí)針方向畫圖,起始點(diǎn)在左側(cè)]
* @param {[number]} percent [所占的進(jìn)度百分比,比如66%,則傳66即可,0 <= percent <= 100]
* @param {[number]} sR [圓弧起始角度,可不傳,默認(rèn)是π/2,Math.PI/2 <= sR < 3/2 * Math.PI]
*/
function draw(percent, sR) {
if (percent < 0 || percent > 100) {
return;
}
if (sR < Math.PI/2 || sR >= 3/2 * Math.PI) {
return;
}
var canvas = document.querySelector('#canvas'),
cxt = canvas.getContext('2d'),
cWidth = canvas.width,
cHeight = canvas.height,
baseColor = '#e1e1e1',
coverColor = '#fe4d43',
PI = Math.PI,
sR = sR || 1/2 * PI; // 默認(rèn)圓弧的起始點(diǎn)弧度為π/2
var finalRadian = sR + ((PI + (PI - sR) * 2) * percent / 100); // 紅圈的終點(diǎn)弧度
var step = (PI + (PI - sR) * 2)/100; // 一個(gè)1%對(duì)應(yīng)的弧度大小
var text = 0; // 顯示的數(shù)字
var timer = setInterval(function() {
cxt.clearRect(0, 0, cWidth, cHeight);
var endRadian = sR + text * step;
// 畫灰色圓弧
drawCanvas(cWidth/2, cHeight/2, 80, sR, sR + (PI + (PI - sR) * 2), baseColor, 2);
// 畫紅色圓弧
drawCanvas(cWidth/2, cHeight/2, 80, sR, endRadian, coverColor, 2);
// 畫紅色圓頭
// 紅色圓頭其實(shí)就是一個(gè)圓,關(guān)鍵的是找到其圓心,涉及到三角函數(shù)知識(shí),自己畫個(gè)圖一看就明了
var angle = 2*PI - endRadian; // 轉(zhuǎn)換成逆時(shí)針方向的弧度(三角函數(shù)中的)
xPos = Math.cos(angle) * 80 + cWidth/2; // 紅色圓 圓心的x坐標(biāo)
yPos = -Math.sin(angle) * 80 + cHeight/2; // 紅色圓 圓心的y坐標(biāo)
drawCanvas(xPos, yPos, 2, 0, 2*PI, coverColor, 2);
// 數(shù)字
cxt.fillStyle = coverColor;
cxt.font = '40px PT Sans';
var textWidth = cxt.measureText(text+'%').width;
cxt.fillText(text+'%', cWidth/2 - textWidth/2, cHeight/2 + 15);
text++;
if (endRadian.toFixed(2) >= finalRadian.toFixed(2)) {
clearInterval(timer);
}
}, 30);
function drawCanvas(x,y,r,sRadian,eRadian,color,lineWidth) {
cxt.beginPath();
cxt.lineCap = "round";
cxt.strokeStyle = color;
cxt.lineWidth = lineWidth;
cxt.arc(x, y, r, sRadian, eRadian, false);
cxt.stroke();
}
}
</script>
</body>
</html>關(guān)于動(dòng)畫部分,可以使用requestAnimationFrame做優(yōu)化,函數(shù)改寫如下:
function draw(percent, sR) {
if (percent < 0 || percent > 100) {
return;
}
if (sR < Math.PI/2 || sR >= 3/2 * Math.PI) {
return;
}
var canvas = document.querySelector('#canvas'),
cxt = canvas.getContext('2d'),
cWidth = canvas.width,
cHeight = canvas.height,
baseColor = '#e1e1e1',
coverColor = '#fe4d43',
PI = Math.PI,
sR = sR || 1/2 * PI; // 默認(rèn)圓弧的起始點(diǎn)弧度為π/2
var finalRadian = sR + ((PI + (PI - sR) * 2) * percent / 100); // 紅圈的終點(diǎn)弧度
var step = (PI + (PI - sR) * 2)/100; // 一個(gè)1%對(duì)應(yīng)的弧度大小
var text = 0; // 顯示的數(shù)字
window.requestAnimationFrame(paint);
function paint() {
cxt.clearRect(0, 0, cWidth, cHeight);
var endRadian = sR + text * step;
// 畫灰色圓弧
drawCanvas(cWidth/2, cHeight/2, 80, sR, sR + (PI + (PI - sR) * 2), baseColor, 2);
// 畫紅色圓弧
drawCanvas(cWidth/2, cHeight/2, 80, sR, endRadian, coverColor, 2);
// 畫紅色圓頭
// 紅色圓頭其實(shí)就是一個(gè)圓,關(guān)鍵的是找到其圓心,涉及到三角函數(shù)知識(shí),自己畫個(gè)圖一看就明了
var angle = 2*PI - endRadian; // 轉(zhuǎn)換成逆時(shí)針方向的弧度(三角函數(shù)中的)
xPos = Math.cos(angle) * 80 + cWidth/2; // 紅色圓 圓心的x坐標(biāo)
yPos = -Math.sin(angle) * 80 + cHeight/2; // 紅色圓 圓心的y坐標(biāo)
drawCanvas(xPos, yPos, 2, 0, 2*PI, coverColor, 2);
// 數(shù)字
cxt.fillStyle = coverColor;
cxt.font = '40px PT Sans';
var textWidth = cxt.measureText(text+'%').width;
cxt.fillText(text+'%', cWidth/2 - textWidth/2, cHeight/2 + 15);
text++;
if (endRadian.toFixed(2) < finalRadian.toFixed(2)) {
window.requestAnimationFrame(paint);
}
}
function drawCanvas(x,y,r,sRadian,eRadian,color,lineWidth) {
cxt.beginPath();
cxt.lineCap = "round";
cxt.strokeStyle = color;
cxt.lineWidth = lineWidth;
cxt.arc(x, y, r, sRadian, eRadian, false);
cxt.stroke();
}到此這篇關(guān)于詳解JavaScript+Canvas繪制環(huán)形進(jìn)度條的文章就介紹到這了,更多相關(guān)JavaScript Canvas環(huán)形進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Bootstrap CSS組件之按鈕組(btn-group)
這篇文章主要為大家詳細(xì)介紹了Bootstrap CSS組件之按鈕組(btn-group),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
JavaScript中三個(gè)等號(hào)和兩個(gè)等號(hào)你了解多少
本篇文章主要介紹了js里面的==和===,== 判斷如果兩邊變量的類型不同,而 === 則不做類型轉(zhuǎn)換,有興趣的可以了解一下2017-07-07
js substring從右邊獲取指定長(zhǎng)度字符串(示例代碼)
本篇文章主要是對(duì)js substring從右邊獲取指定長(zhǎng)度字符串的示例代碼進(jìn)行了介紹,需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
JavaScript用20行代碼實(shí)現(xiàn)虎年春節(jié)倒計(jì)時(shí)
春節(jié)將至,相信大家跟小編一樣很激動(dòng)呀。為了迎接虎年春節(jié)到來(lái),小編為大家準(zhǔn)備了一個(gè)虎年春節(jié)倒計(jì)時(shí),僅20行代碼用js就實(shí)現(xiàn)啦。感興趣的可以動(dòng)手試一試2022-01-01
js實(shí)現(xiàn)適配移動(dòng)端的拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)適配移動(dòng)端的拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
使用JavaScript實(shí)現(xiàn)獲取audio時(shí)長(zhǎng)
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)獲取audio時(shí)長(zhǎng),并且轉(zhuǎn)換為分鐘00:00:00格式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04

