js canvas實現(xiàn)適用于移動端的百分比儀表盤dashboard
本文為大家分享了canvas實現(xiàn)適用于移動端的百分比儀表盤,供大家參考,具體內(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標簽。 </canvas> <span style="display:block;position:absolute;top:.94rem;left:.3rem;width:2rem;text-align:center;font-size:.5rem;font-family:microsoft Yahei" 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>
截圖如下:
建議:不要因為簡單而不去動手,多動手多思考,你會進步的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
用Javascript實現(xiàn)Sleep暫停功能代碼
ie和firefox都可以使用,有興趣可以試試2010-09-09JavaScript根據(jù)CSS的Media Queries來判斷瀏覽設備的方法
這篇文章主要介紹了JavaScript根據(jù)CSS的Media Queries來判斷瀏覽設備的方法,主要思路是通過CSS Media Queries改變一個類的某個屬性值(例如 z-index),然后用JavaScript讀取判斷,需要的朋友可以參考下2016-05-05js結(jié)合css實現(xiàn)登錄后才能復制的效果實例
很多網(wǎng)站都有登錄后才能復制的限制,什么原理呢?css屬性user-select:none,通常會采用這種方式來禁止復制文本。但瀏覽開發(fā)者工具-審查元素,取消此樣式后,就可以選中文本了。想要完整地禁止復制,還需要通過js控制選擇的內(nèi)容。2023-07-07分享9個最好用的JavaScript開發(fā)工具和代碼編輯器
這篇文章主要介紹了9個最好用的JavaScript開發(fā)工具和代碼編輯器,需要的朋友可以參考下2015-03-03layui radio點擊事件實現(xiàn)input顯示和隱藏的例子
今天小編就為大家分享一篇layui radio點擊事件實現(xiàn)input顯示和隱藏的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09