canvas畫圓形進度條(動態+漸變)


1、canvas畫進度條時,由於內部部分文字是不動,漸變色可以由動態一筆筆畫出

     html代碼:

1             <div>
2                 <canvas id="gameCanvas">
3                     您的瀏覽器不支持Canvas標簽!
4                 </canvas>
5                 <canvas id="earthCanvas">
6                     您的瀏覽器不支持Canvas標簽!
7                 </canvas>
8             </div>

  css代碼:在布局中注意以下兩點

    1、在同一個div中,需要把動態隨定時器改變的部分放在一個canvas中;

    2、把靜態的文字等可以放在一個canvas中,或者一個靜態div中,利用position: absolute;定位讓兩個元素重疊,視圖中即可顯示

1 #earthCanvas,#gameCanvas {
2                 width: 100%;
3                 height: 15em;
4                 position: absolute;
5             }

  js代碼:miletargetTarmileage為進度條為100%時數值,progress為項目當前的進度

  1         function canvasProgrss(miletargetTarmileage, progress) {
  2             var canvasWidth = $("#gameCanvas").width();
  3             var canvasHeight = $("#gameCanvas").height();
  4             var canvas = document.getElementById('gameCanvas'); //畫布對象
  5             canvas.width = canvasWidth;
  6             canvas.height = canvasHeight;
  7             var ctx = canvas.getContext('2d');
  8             //var progress = 50; 當前已加載的進度
  9             var progressPercent=progress*100/miletargetTarmileage;
 10             if(miletargetTarmileage<=0){
 11                 progressPercent=100;
 12             };
 13             if(progress==0){
 14                 progressPercent=0;
 15             }
 16             function canvastxt(progress,progressPercent) {
 17                 ctx.lineWidth = 10;
 18                 ctx.strokeStyle = '#fda2a2';
 19                 ctx.clearRect(0, 0, canvasWidth, canvasHeight); //清除已繪制內容
 20                 ctx.beginPath();
 21                 ctx.arc(canvasWidth / 2, canvasHeight / 2, 80, 0, 180);
 22                 ctx.stroke(); //對路徑進行描邊 
 23                 ctx.closePath();
 24                 var txtday = progress;
 25                 ctx.font = '60px 微軟雅黑';
 26                 ctx.fillStyle = '#fff';
 27                 var txtdayWidth = ctx.measureText(txtday).width;
 28                 ctx.fillText(txtday, canvasWidth / 2 - txtdayWidth / 2, canvasHeight / 2 + 20); //繪制進度提示文字
 29                 var txtkm = "目標:" + miletargetTarmileage + "km";
 30                 ctx.font = '1em 微軟雅黑';
 31                 ctx.lineWidth = 1;
 32                 var txtkmWidth = ctx.measureText(txtkm).width;
 33                 ctx.fillText(txtkm, canvasWidth / 2 - txtkmWidth / 2, canvasHeight / 3);
 34                 //寫“km”
 35                 var txtkm = "km";
 36                 ctx.font = '1em 微軟雅黑';
 37                 ctx.lineWidth = 1;
 38                 var txtkmWidth = ctx.measureText(txtkm).width;
 39                 ctx.fillText(txtkm, canvasWidth / 2 - txtkmWidth / 2, 5 * canvasHeight / 7);
 40                 //划外圓
 41                 ctx.closePath();
 42                 ctx.beginPath();
 43                 ctx.arc(canvasWidth / 2, canvasHeight / 2, 100, 0, 180);
 44                 ctx.stroke(); //對路徑進行描邊 
 45                 ctx.closePath();
 46                 var d=0;
 47                 ctx.lineWidth = 10;
 48                 if(progressPercent>=100){
 49                     ctx.beginPath();
 50                     ctx.strokeStyle = 'rgb(0,255,0)';
 51                     ctx.lineWidth = 10;
 52                     var startAngle = -90 * Math.PI / 180;
 53                     var endAngle = (-90 + 360 * progressPercent / 100) * Math.PI / 180;
 54                     ctx.arc(canvasWidth / 2, canvasHeight / 2, 80, startAngle, endAngle);
 55                     ctx.stroke();
 56                 }else{
 57                     var timer = setInterval(function(){
 58                     //ctx.clearRect(0,0,canvasWidth,canvasHeight);
 59                     ctx.beginPath();
 60                     var startAngle = (-90 + 360 * (d++) / 500) * Math.PI / 180;
 61                     var endAngle = (-90 + 360 * d / 500) * Math.PI / 180;
 62                     ctx.arc(canvasWidth / 2, canvasHeight / 2,80,startAngle, endAngle);
 63                     ctx.strokeStyle = "rgb("+(255-d*0.5)+",255,"+(255-d*0.5)+")";
 64                     ctx.stroke();
 65                     console.log(progressPercent);
 66                     console.log(d);
 67                     if (d > progressPercent*5) {
 68                         clearInterval(timer);
 69                     }
 70                 },10);
 71                 }
 72                 /*ctx.beginPath();
 73 
 74                 ctx.strokeStyle = '#d7fa82';
 75                 ctx.lineWidth = 10;
 76                 var startAngle = -90 * Math.PI / 180;
 77                 console.log(progressPercent);
 78                 var endAngle = (-90 + 360 * progressPercent / 100) * Math.PI / 180;
 79                 ctx.arc(canvasWidth / 2, canvasHeight / 2, 80, startAngle, endAngle);
 80                 ctx.stroke(); //對路徑進行描邊 */
 81             }
 82             //划旋轉小球
 83             var canvas1 = document.getElementById('earthCanvas'); //畫布對象
 84             canvas1.width = canvasWidth;
 85             canvas1.height = canvasHeight;
 86             var ctx1 = canvas1.getContext('2d');
 87             var imgearth = new Image();
 88             imgearth.src = "img/images/card_07.png";
 89             imgearth.onload = function() {
 90                 var n = 0;
 91                 var i = 0;
 92                 ctx1.translate(canvasWidth / 2, canvasHeight / 2);
 93                 var timer = setInterval(function() {
 94                     ctx1.clearRect(0, 0, canvasWidth, canvasHeight);
 95                     n = n * Math.PI / 100;
 96                     ctx1.rotate(n++);
 97                     ctx1.drawImage(imgearth, 95, 0, 10, 10);
 98                 }, 100);
 99                 
100             }
101             canvastxt(progress,progressPercent);
102         }

最終顯示效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM