当前位置:   article > 正文

canvas实现进度条_canvas 进度条

canvas 进度条
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>canvas_task_xiapeimin</title>
  7. </head>
  8. <body>
  9. <canvas id="canvas" height="600" width="600" style="background: lightgray;"></canvas>
  10. </body>
  11. <script>
  12. var canvas = document.querySelector('#canvas');
  13. var context = canvas.getContext("2d");
  14. setDraw(30);
  15. function setDraw(speed){
  16. var i = 0;
  17. var timer = setInterval(function(){
  18. context.clearRect(0,0,canvas.width,canvas.height);
  19. draw(i);
  20. if(i==100) clearInterval(timer);
  21. i++;
  22. },speed);
  23. }
  24. function draw(i){
  25. // 外层圆
  26. context.beginPath();
  27. context.arc(300,300,110,0,Math.PI*2);
  28. context.lineWidth = 1;
  29. context.strokeStyle = "grey";
  30. context.stroke();
  31. // 中间圆
  32. context.beginPath();
  33. context.arc(300,300,100,Math.PI*1.5,Math.PI*(1.5+i*0.02));
  34. context.lineWidth = 10;
  35. context.strokeStyle = "blue";
  36. context.stroke();
  37. // 内层圆
  38. context.beginPath();
  39. context.arc(300,300,90,0,Math.PI*2);
  40. context.lineWidth = 1;
  41. context.strokeStyle = "grey";
  42. context.stroke();
  43. context.fillStyle = "white";
  44. context.fill();
  45. // 进度显示
  46. context.beginPath();
  47. context.textBaseline = "middle";
  48. context.textAlign = "center";
  49. context.font = "15px Arial";
  50. context.fillStyle = "black";
  51. context.fillText("进度:"+i+"%",300,300);
  52. }
  53. </script>
  54. </html>

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/254305
推荐阅读
相关标签
  

闽ICP备14008679号