index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view
  3. class="circle-progress"
  4. :style="{
  5. width: widthPx + 'px',
  6. height: widthPx + 'px',
  7. backgroundColor: bgColor
  8. }"
  9. >
  10. <!-- 有的不支持canvas-id属性,必须用id属性 -->
  11. <canvas
  12. v-if="canvasId"
  13. class="canvas-bg"
  14. :canvas-id="canvasId"
  15. :id="canvasId"
  16. :style="{
  17. width: widthPx + 'px',
  18. height: widthPx + 'px'
  19. }"
  20. ></canvas>
  21. <canvas
  22. class="canvas"
  23. v-if="elId"
  24. :canvas-id="elId"
  25. :id="elId"
  26. :style="{
  27. width: widthPx + 'px',
  28. height: widthPx + 'px'
  29. }"
  30. ></canvas>
  31. <slot></slot>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'circle-progress',
  37. props: {
  38. // 圆环进度百分比值
  39. percent: {
  40. type: Number,
  41. default: 0,
  42. // 值在0到100之间
  43. validator: val => {
  44. return val >= 0 && val <= 100;
  45. }
  46. },
  47. // 圆环底色(灰色的圆环)
  48. inactiveColor: {
  49. type: String,
  50. default: '#ececec'
  51. },
  52. // 圆环激活部分的颜色
  53. activeColor: {
  54. type: String,
  55. default: '#009dff'
  56. },
  57. // 圆环线条的宽度,单位rpx
  58. borderWidth: {
  59. type: [Number, String],
  60. default: 14
  61. },
  62. // 整个圆形的宽度,单位rpx
  63. width: {
  64. type: [Number, String],
  65. default: 200
  66. },
  67. // 整个圆环执行一圈的时间,单位ms
  68. duration: {
  69. type: [Number, String],
  70. default: 1500
  71. },
  72. // 圆环进度区域的背景色
  73. bgColor: {
  74. type: String,
  75. default: '#ffffff'
  76. }
  77. },
  78. data() {
  79. return {
  80. canvasId: this.randomId(), //一个页面多个圆形进度
  81. elId: this.randomId(),
  82. widthPx: uni.upx2px(this.width), // 转成px后的整个组件的背景宽度
  83. borderWidthPx: uni.upx2px(this.borderWidth), // 转成px后的圆环的宽度
  84. startAngle: -Math.PI / 2, // canvas画圆的起始角度,默认为3点钟方向,定位到12点钟方向
  85. progressContext: null, // 活动圆的canvas上下文
  86. newPercent: 0, // 当动态修改进度值的时候,保存进度值的变化前后值,用于比较用
  87. oldPercent: 0 // 当动态修改进度值的时候,保存进度值的变化前后值,用于比较用
  88. };
  89. },
  90. watch: {
  91. percent(nVal, oVal = 0) {
  92. if (nVal > 100) nVal = 100;
  93. if (nVal < 0) oVal = 0;
  94. this.newPercent = nVal;
  95. this.oldPercent = oVal;
  96. setTimeout(() => {
  97. this.drawCircleByProgress(oVal);
  98. }, 50);
  99. }
  100. },
  101. created() {
  102. // 赋值,用于加载后第一个画圆使用
  103. this.newPercent = this.percent;
  104. this.oldPercent = 0;
  105. },
  106. mounted() {
  107. this.drawProgressBg();
  108. this.drawCircleByProgress(this.oldPercent);
  109. },
  110. methods: {
  111. //一个页面多个progress时ID需不同
  112. randomId(){
  113. return 'progressId'+parseInt(Math.random()*1000000)
  114. },
  115. drawProgressBg() {
  116. let ctx = uni.createCanvasContext(this.canvasId, this);
  117. ctx.setLineWidth(this.borderWidthPx); // 设置圆环宽度
  118. ctx.setStrokeStyle(this.inactiveColor); // 线条颜色
  119. ctx.beginPath(); // 开始描绘路径
  120. let radius = this.widthPx / 2;
  121. ctx.arc(radius, radius, radius - this.borderWidthPx, 0, 2 * Math.PI, false);
  122. ctx.stroke(); // 对路径进行描绘
  123. ctx.draw();
  124. },
  125. drawCircleByProgress(progress) {
  126. if (this.oldPercent === 0 && this.newPercent === 0) { return; }
  127. let ctx = this.progressContext;
  128. if (!ctx) {
  129. ctx = uni.createCanvasContext(this.elId, this);
  130. this.progressContext = ctx;
  131. }
  132. // 表示进度的两端为圆形
  133. ctx.setLineCap('round');
  134. // 设置线条的宽度和颜色
  135. ctx.setLineWidth(this.borderWidthPx);
  136. ctx.setStrokeStyle(this.activeColor);
  137. // 计算过渡时间
  138. let time = Math.floor(this.duration / 200);
  139. let endAngle = ((2 * Math.PI) / 100) * progress + this.startAngle;
  140. ctx.beginPath();
  141. // 半径为整个canvas宽度的一半
  142. let radius = this.widthPx / 2;
  143. ctx.arc(radius, radius, radius - this.borderWidthPx, this.startAngle, endAngle, false);
  144. ctx.stroke();
  145. ctx.draw();
  146. // 增大了百分比
  147. if (this.newPercent > this.oldPercent) {
  148. progress++;
  149. if (progress > this.newPercent) return;
  150. } else {
  151. // 减少百分比
  152. progress--;
  153. if (progress < this.newPercent) return;
  154. }
  155. setTimeout(() => {
  156. // 定时器,为了让进度条有动画效果
  157. this.drawCircleByProgress(progress);
  158. }, time);
  159. }
  160. }
  161. };
  162. </script>
  163. <style scoped>
  164. .circle-progress {
  165. position: relative;
  166. display: flex;
  167. align-items: center;
  168. justify-content: center;
  169. }
  170. .canvas-bg {
  171. position: absolute;
  172. }
  173. .canvas {
  174. position: absolute;
  175. }
  176. </style>