小編給大家分享一下js和canvas如何實現(xiàn)適用于移動端的百分比儀表盤dashboard,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內(nèi)容如下
由于最近工作中,經(jīng)常會遇到一些動態(tài)百分比的儀表盤,一開始都是用圖片樣式方式實現(xiàn);
但是隨著越來越多的項目,決定用canvas繪制一個簡易的儀表盤,便于以后項目中直接使用;
現(xiàn)版本只是書寫為方法形式,也許之后會有時間對其優(yōu)化為插件形式。
簡簡單單而已,以下直接給出代碼和執(zhí)行過程中的三張截圖:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Pragma" content="no-cache">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<title>canvas繪制簡易百分比儀表盤dashboard(建議最好用于移動端)</title>
<style type="text/css">
div{margin:1rem;background:#eee;padding:.3rem; position:relative }
div canvas{background:#cacaca;
-webkit-transform: rotateZ(-270deg);
transform:rotateZ(-270deg);
-webkit-animation:ani01 1s ease 0s both;
animation:ani01 1s ease 0s both;
}
@-webkit-keyframes ani01 {
0%{
-webkit-transform:scale(.5,.5) rotateZ(-270deg);
transform:scale(.5,.5) rotateZ(-270deg);
}
100%{
-webkit-transform:scale(1,1) rotateZ(-90deg);
transform:scale(1,1) rotateZ(-90deg);
}
}
@keyframes ani01 {
0%{
-webkit-transform:scale(.5,.5) rotateZ(-270deg);
transform:scale(.5,.5) rotateZ(-270deg);
}
100%{
-webkit-transform:scale(1,1) rotateZ(-90deg);
transform:scale(1,1) rotateZ(-90deg);
}
}
</style>
</head>
<body>
<div>
<canvas id="myCanvas1" data-percent="80">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
<span id="dushu" >0</span>
</div>
<script type="text/javascript" src="../js/flexible.js"></script>
<script type="text/javascript">
var pper=0;
var pper_interal;
var dushu=document.getElementById('dushu');
var aaa=drawCanvanPercent('myCanvas1','rem',2,'#0e9cfa',0.2,'#fff');
function drawCanvanPercent(ele_id,dw,cir_r,cir_color,line_w,fill_color){
if(dw=="rem"){
cir_r=cir_r*(window.screen.width/10);
line_w=line_w*(window.screen.width/10);
}
var canvas = document.getElementById(ele_id);
var circle = {
r : cir_r/2, //圓的半徑
per : canvas.getAttribute('data-percent'), //百分比分子
color : cir_color, //圓的顏色
lineWidth : line_w //圓的顏色
};
canvas.width=canvas.height=circle.r*2;
canvas.style.borderRadius="50%";
if(canvas.getContext){
var ctx2 = canvas.getContext("2d");
ctx2.fillStyle = fill_color;
ctx2.arc(circle.r, circle.r, circle.r-circle.lineWidth/2, 0, Math.PI*2, false);
ctx2.fill();
var ctx = canvas.getContext("2d");
pper_interal= setInterval(function () {
drawMove(ctx,circle);
}, 10);
}
}
function drawMove(ctx,circle){
if(pper>=circle.per){
pper=circle.per;
clearTimeout(pper_interal);
}else{
pper++;
}
dushu.innerText=pper+'%';
ctx.beginPath();
ctx.strokeStyle = circle.color;
ctx.lineWidth=circle.lineWidth;
ctx.arc(circle.r, circle.r, circle.r, 0, Math.PI*(pper/100)*360/180, false);
ctx.stroke();
}
</script>
</body>
</html>截圖如下:



建議:不要因為簡單而不去動手,多動手多思考,你會進步的。
1.JavaScript主要用來向HTML頁面添加交互行為。 2.JavaScript可以直接嵌入到HTML頁面,但寫成單獨的js文件有利于結(jié)構(gòu)和行為的分離。 3.JavaScript具有跨平臺特性,在絕大多數(shù)瀏覽器的支持下,可以在多種平臺下運行。
以上是“js和canvas如何實現(xiàn)適用于移動端的百分比儀表盤dashboard”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前標(biāo)題:js和canvas如何實現(xiàn)適用于移動端的百分比儀表盤dashboard-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article34/deiepe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、定制開發(fā)、網(wǎng)站建設(shè)、外貿(mào)建站、定制網(wǎng)站、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容