progress.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="progress_box">
  3. <canvas class="progress_bg" canvas-id="cpbg"></canvas>
  4. <canvas class="progress_bar" canvas-id="cpbar"></canvas>
  5. <canvas class="progress_line" canvas-id="cpline"></canvas>
  6. <view class="progress_txt">
  7. <view class="progress_info">{{ progress_txt }}%</view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. props: {
  14. progress_txt: {
  15. type: Number,
  16. default: 80
  17. }
  18. },
  19. name:"progress",
  20. mounted() {
  21. this.drawProgressbg();
  22. this.drawCircle(this.progress_txt); //参数为1-100
  23. this.drawLine();
  24. },
  25. data() {
  26. return {
  27. };
  28. },
  29. methods:{
  30. drawProgressbg(){
  31. // 自定义组件实例 this ,表示在这个自定义组件下查找拥有 canvas-id 的 <canvas/>
  32. var ctx = uni.createCanvasContext('cpbg', this);
  33. ctx.setLineWidth(12); // 设置圆环的宽度
  34. ctx.setStrokeStyle('#E9E9E9'); // 设置圆环的颜色
  35. ctx.setLineCap('round'); // 设置圆环端点的形状
  36. ctx.setLineCap('square'); // 设置圆环端点的形状
  37. ctx.beginPath(); //开始一个新的路径
  38. ctx.arc(110, 110, 70, 0 * Math.PI, 2 * Math.PI, false);
  39. //设置一个原点(110,110),半径为100的圆的路径到当前路径
  40. ctx.stroke(); //对当前路径进行描边
  41. ctx.draw();
  42. },
  43. drawCircle(step){
  44. var ctx = uni.createCanvasContext('cpbar', this);
  45. // 进度条的渐变(中心x坐标-半径-边宽,中心Y坐标,中心x坐标+半径+边宽,中心Y坐标)
  46. var gradient = ctx.createLinearGradient(0, 0, 130, 0);
  47. let increase = 0.05;
  48. let end = (step / 100) * 2 * Math.PI - Math.PI / 2; // 最后的角度
  49. let current = -Math.PI / 2; // 起始角度
  50. let timer = setInterval(() => {
  51. gradient.addColorStop('0', '#00F2FE');
  52. gradient.addColorStop('1.0', '#4FACFE');
  53. ctx.setLineWidth(12);
  54. ctx.setStrokeStyle(gradient);
  55. ctx.setLineCap('square');
  56. ctx.beginPath();
  57. // 参数step 为绘制的百分比
  58. if (current < end) {
  59. current = current + increase;
  60. }
  61. if (current >= end) {
  62. current = end;
  63. clearInterval(timer);
  64. }
  65. ctx.arc(110, 110, 70, -Math.PI / 2, current, false);
  66. ctx.stroke();
  67. ctx.draw();
  68. }, 20);
  69. },
  70. drawLine(){
  71. var context = uni.createCanvasContext("cpline",this);
  72. var r = 70;
  73. var x0 = 110;
  74. var y0 = 110;
  75. var x;
  76. var y;
  77. var lineWidth = 12;
  78. for (let i = 0; i < 60; i++) {
  79. context.beginPath();
  80. context.setLineWidth(lineWidth);
  81. context.setStrokeStyle("#FFFFFF");
  82. x = x0 - r * Math.sin(((6 * (i + 1) - 3) * Math.PI) / 180);
  83. y = y0 - r * Math.cos(((6 * (i + 1) - 3) * Math.PI) / 180);
  84. // console.log('x0:' + x0 + ' y0:' + y0 + ' x:' + x + ' y:' + y);
  85. context.moveTo(x, y);
  86. context.arc(
  87. x0,
  88. y0,
  89. r,
  90. ((270 - 6 * (i + 1) + 3) * Math.PI) / 180,
  91. ((270 - 6 * i) * Math.PI) / 180,
  92. false
  93. );
  94. context.stroke();
  95. context.closePath();
  96. }
  97. context.stroke();
  98. context.draw();
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. .progress_box {
  105. position: relative;
  106. width: 400rpx;
  107. height: 400rpx;
  108. display: flex;
  109. align-items: center;
  110. justify-content: center;
  111. text-align: center;
  112. }
  113. .progress_bg {
  114. position: absolute;
  115. width: 220px;
  116. height: 220px;
  117. }
  118. .progress_bar {
  119. position: absolute;
  120. width: 220px;
  121. height: 220px;
  122. }
  123. .progress_line {
  124. position: absolute;
  125. width: 220px;
  126. height: 220px;
  127. }
  128. .progress_txt {
  129. position: absolute;
  130. font-size: 28upx;
  131. color: #999999;
  132. }
  133. .progress_info {
  134. font-size: 36upx;
  135. padding-left: 16upx;
  136. letter-spacing: 2upx;
  137. font-size: 52upx;
  138. color: #333333;
  139. }
  140. .progress_dot {
  141. width: 16upx;
  142. height: 16upx;
  143. border-radius: 50%;
  144. background-color: #fb9126;
  145. }
  146. </style>