当前位置:   article > 正文

canvan绘制的双半圆环进度条_绘制双半圆环进度条,其中外层环表示量程,颜色0-10为绿,10-20为橙,20-30为红,内层

绘制双半圆环进度条,其中外层环表示量程,颜色0-10为绿,10-20为橙,20-30为红,内层

效果图

实现原理:

1.使用canvas绘制两个半圆弧,底图灰色半圆弧和颜色进度圆弧。

2.利用setInterval计时器,逐步改变颜色进度条,达到进度条的效果。

 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
  8. <body>
  9. <canvas id="canvas" width="300" height="300">
  10. </canvas>
  11. <script type="text/javascript">
  12. function toCanvas(id, progress, progress2) {
  13. //canvas进度条
  14. var canvas = document.getElementById(id),
  15. ctx = canvas.getContext("2d"),
  16. percent = progress,
  17. percent2 = progress2,
  18. //最终百分比
  19. circleX = canvas.width / 2, // 中心x坐标
  20. circleY = canvas.height / 2, //中心y坐标
  21. radius = 100, //圆环半径
  22. lineWidth = 8, // 圆形线条的宽度
  23. fontSize = 10; //字体大小
  24. /*//两端圆点
  25. function smallcircle1(cx, cy, r) {
  26. ctx.beginPath();
  27. ctx.lineWidth = 1;
  28. ctx.fillStyle = '#06a8f3';
  29. ctx.arc(cx, cy, r, Math.PI, Math.PI * 2);
  30. ctx.fill();
  31. }
  32. function smallcircle2(cx, cy, r) {
  33. ctx.beginPath();
  34. ctx.lineWidth = 1;
  35. ctx.fillStyle = '#00f8bb';
  36. ctx.arc(cx, cy, r, Math.PI, Math.PI * 2);
  37. ctx.fill();
  38. }*/
  39. //画圆
  40. function circle(cx, cy, r) {
  41. ctx.beginPath();
  42. ctx.lineWidth = lineWidth;
  43. ctx.strokeStyle = '#eee';
  44. ctx.arc(cx, cy, r, Math.PI, Math.PI * 2);
  45. //ctx.arc(cx, cy, r, Math.PI * 2 / 3, Math.PI * 1 / 3);
  46. ctx.stroke();
  47. }
  48. //画弧线
  49. function sector(cx, cy, r, startAngle, endAngle, anti) {
  50. ctx.beginPath();
  51. ctx.lineWidth = lineWidth;
  52. // 渐变色 - 可自定义
  53. var linGrad = ctx.createLinearGradient(
  54. circleX - radius - lineWidth, circleY, circleX + radius + lineWidth, circleY
  55. );
  56. linGrad.addColorStop(0.0, '#06a8f3');
  57. linGrad.addColorStop(1.0, '#00f8bb');
  58. ctx.strokeStyle = linGrad;
  59. //圆弧两端的样式
  60. ctx.lineCap = 'round';
  61. ctx.arc(cx, cy, r, Math.PI, Math.PI * (1 + endAngle / 100));
  62. ctx.stroke();
  63. }
  64. //画弧线02
  65. function sector2(cx, cy, r, startAngle, endAngle, anti) {
  66. ctx.beginPath();
  67. ctx.lineWidth = lineWidth;
  68. // 渐变色 - 可自定义
  69. var linGrad = ctx.createLinearGradient(
  70. circleX - radius - lineWidth, circleY, circleX + radius + lineWidth, circleY
  71. );
  72. linGrad.addColorStop(0.0, '#06a8f3');
  73. linGrad.addColorStop(1.0, '#00f8bb');
  74. ctx.strokeStyle = linGrad
  75. //圆弧两端的样式
  76. ctx.lineCap = 'round';
  77. ctx.arc(cx, cy, r * 3 / 4, Math.PI, Math.PI * (1 + endAngle / 100));
  78. ctx.stroke();
  79. }
  80. //刷新
  81. function loading() {
  82. var percent3 = progress;
  83. if(percent < percent2) percent = percent2;
  84. if(process >= percent) clearInterval(circleLoading);
  85. if(process2 >= percent) clearInterval(circleLoading);
  86. //清除canvas内容
  87. ctx.clearRect(0, 0, circleX * 2, circleY * 2);
  88. //中间的字
  89. ctx.font = fontSize + 'px April';
  90. ctx.textAlign = 'center';
  91. ctx.textBaseline = 'middle';
  92. ctx.fillStyle = '#999';
  93. ctx.fillText(parseFloat(process).toFixed(0) + '%', circleX, circleY*4/5);
  94. ctx.fillText("月度完成比",circleX, circleY);
  95. //圆形
  96. circle(circleX, circleY, radius);
  97. //圆弧
  98. sector(circleX, circleY, radius, Math.PI * 2 / 3, process);
  99. sector2(circleX, circleY, radius, Math.PI * 2 / 3, process2);
  100. //两端圆点
  101. //smallcircle1(50 + Math.cos(2 * Math.PI / 360 * 120) * 100, 150 + Math.sin(2 * Math.PI / 360 * 120) * 100, 5);
  102. //smallcircle2(50 + Math.cos(2 * Math.PI / 360 * (120 + process * 3)) * 100, 150 + Math.sin(2 * Math.PI / 360 * (120 + process * 3)) * 100, 5);
  103. //控制结束时动画的速度
  104. if(process < percent3) {
  105. if(process / percent > 0.90) {
  106. process += 0.30;
  107. } else if(process / percent > 0.80) {
  108. process += 0.55;
  109. } else if(process / percent > 0.70) {
  110. process += 0.75;
  111. } else {
  112. process += 1.0;
  113. }
  114. }
  115. if(process2 < percent2) {
  116. if(process2 / percent > 0.90) {
  117. process2 += 0.30;
  118. } else if(process2 / percent > 0.80) {
  119. process2 += 0.55;
  120. } else if(process2 / percent > 0.70) {
  121. process2 += 0.75;
  122. } else {
  123. process2 += 1.0;
  124. }
  125. }
  126. }
  127. var process = 0.0;
  128. var process2 = 0.0;
  129. var circleLoading = window.setInterval(function() {
  130. loading();
  131. }, 20);
  132. }
  133. toCanvas('canvas', 50, 80);
  134. </script>
  135. </body>
  136. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/254289
推荐阅读
相关标签
  

闽ICP备14008679号