tui-circular-progress.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view class="tui-circular-container" :style="{ width: diam + 'px', height: (height || diam) + 'px' }">
  3. <canvas class="tui-circular-default" :canvas-id="defaultCanvasId" :id="defaultCanvasId" :style="{ width: diam + 'px', height: (height || diam) + 'px' }"
  4. v-if="defaultShow"></canvas>
  5. <canvas class="tui-circular-progress" :canvas-id="progressCanvasId" :id="progressCanvasId" :style="{ width: diam + 'px', height: (height || diam) + 'px' }"></canvas>
  6. <slot />
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'tuiCircularProgress',
  12. props: {
  13. /*
  14. 传值需使用rpx进行转换保证各终端兼容
  15. px = rpx / 750 * wx.getSystemInfoSync().windowWidth
  16. 圆形进度条(画布)宽度,直径 [px]
  17. */
  18. diam: {
  19. type: Number,
  20. default: 60
  21. },
  22. //圆形进度条(画布)高度,默认取diam值[当画半弧时传值,height有值时则取height]
  23. height: {
  24. type: Number,
  25. default: 0
  26. },
  27. //进度条线条宽度[px]
  28. lineWidth: {
  29. type: Number,
  30. default: 4
  31. },
  32. /*
  33. 线条的端点样式
  34. butt:向线条的每个末端添加平直的边缘
  35. round 向线条的每个末端添加圆形线帽
  36. square 向线条的每个末端添加正方形线帽
  37. */
  38. lineCap: {
  39. type: String,
  40. default: 'round'
  41. },
  42. //圆环进度字体大小 [px]
  43. fontSize: {
  44. type: Number,
  45. default: 12
  46. },
  47. //圆环进度字体颜色
  48. fontColor: {
  49. type: String,
  50. default: '#5677fc'
  51. },
  52. //是否显示进度文字
  53. fontShow: {
  54. type: Boolean,
  55. default: true
  56. },
  57. /*
  58. 自定义显示文字[默认为空,显示百分比,fontShow=true时生效]
  59. 可以使用 slot自定义显示内容
  60. */
  61. percentText: {
  62. type: String,
  63. default: ''
  64. },
  65. //是否显示默认(背景)进度条
  66. defaultShow: {
  67. type: Boolean,
  68. default: true
  69. },
  70. //默认进度条颜色
  71. defaultColor: {
  72. type: String,
  73. default: '#CCC'
  74. },
  75. //进度条颜色
  76. progressColor: {
  77. type: String,
  78. default: '#5677fc'
  79. },
  80. //进度条渐变颜色[结合progressColor使用,默认为空]
  81. gradualColor: {
  82. type: String,
  83. default: ''
  84. },
  85. //起始弧度,单位弧度
  86. sAngle: {
  87. type: Number,
  88. default: -Math.PI / 2
  89. },
  90. //指定弧度的方向是逆时针还是顺时针。默认是false,即顺时针
  91. counterclockwise: {
  92. type: Boolean,
  93. default: false
  94. },
  95. //进度百分比 [10% 传值 10]
  96. percentage: {
  97. type: Number,
  98. default: 0
  99. },
  100. //进度百分比缩放倍数[使用半弧为100%时,则可传2]
  101. multiple: {
  102. type: Number,
  103. default: 1
  104. },
  105. //动画执行时间[单位毫秒,低于50无动画]
  106. duration: {
  107. type: Number,
  108. default: 800
  109. },
  110. //backwards: 动画从头播;forwards:动画从上次结束点接着播
  111. activeMode: {
  112. type: String,
  113. default: 'backwards'
  114. }
  115. },
  116. watch: {
  117. percentage(val) {
  118. this.initDraw()
  119. }
  120. },
  121. data() {
  122. return {
  123. // #ifdef MP-WEIXIN
  124. progressCanvasId:"progressCanvasId",
  125. defaultCanvasId: "defaultCanvasId",
  126. // #endif
  127. // #ifndef MP-WEIXIN
  128. progressCanvasId:this.getCanvasId(),
  129. defaultCanvasId: this.getCanvasId(),
  130. // #endif
  131. progressContext: null,
  132. linearGradient: null,
  133. //起始百分比
  134. startPercentage: 0
  135. // dpi
  136. //pixelRatio: uni.getSystemInfoSync().pixelRatio
  137. };
  138. },
  139. mounted() {
  140. this.initDraw(true)
  141. },
  142. methods: {
  143. //初始化绘制
  144. initDraw(init) {
  145. let start = this.activeMode === 'backwards' ? 0 : this.startPercentage;
  146. if (this.defaultShow && init) {
  147. this.drawDefaultCircular();
  148. }
  149. this.drawProgressCircular(start);
  150. },
  151. //默认(背景)圆环
  152. drawDefaultCircular() {
  153. let ctx = uni.createCanvasContext(this.defaultCanvasId, this);
  154. ctx.setLineWidth(this.lineWidth);
  155. ctx.setStrokeStyle(this.defaultColor);
  156. //终止弧度
  157. let eAngle = Math.PI * (this.height ? 1 : 2) + this.sAngle;
  158. this.drawArc(ctx, eAngle);
  159. },
  160. //进度圆环
  161. drawProgressCircular(startPercentage) {
  162. let ctx = this.progressContext;
  163. let gradient = this.linearGradient;
  164. if (!ctx) {
  165. ctx = uni.createCanvasContext(this.progressCanvasId, this);
  166. //创建一个线性的渐变颜色 CanvasGradient对象
  167. gradient = ctx.createLinearGradient(0, 0, this.diam, 0);
  168. gradient.addColorStop('0', this.progressColor);
  169. if (this.gradualColor) {
  170. gradient.addColorStop('1', this.gradualColor);
  171. }
  172. // #ifdef APP-PLUS
  173. const res = uni.getSystemInfoSync();
  174. if (!this.gradualColor && res.platform.toLocaleLowerCase() == "android") {
  175. gradient.addColorStop('1', this.progressColor);
  176. }
  177. // #endif
  178. this.progressContext = ctx;
  179. this.linearGradient = gradient;
  180. }
  181. ctx.setLineWidth(this.lineWidth);
  182. ctx.setStrokeStyle(gradient);
  183. let time = this.duration / this.percentage;
  184. if (this.percentage > 0 || !this.fontShow) {
  185. startPercentage = this.duration < 50 ? this.percentage - 1 : startPercentage;
  186. startPercentage++;
  187. if (startPercentage > this.percentage) {
  188. this.$emit('end', {
  189. canvasId: this.progressCanvasId
  190. });
  191. return;
  192. }
  193. }
  194. if (this.fontShow) {
  195. ctx.setFontSize(this.fontSize);
  196. ctx.setFillStyle(this.fontColor);
  197. ctx.setTextAlign('center');
  198. ctx.setTextBaseline('middle');
  199. let percentage = this.percentText;
  200. if (!percentage) {
  201. percentage = this.counterclockwise ? 100 - startPercentage * this.multiple : startPercentage * this.multiple;
  202. percentage = `${percentage}%`;
  203. }
  204. let radius = this.diam / 2;
  205. ctx.fillText(percentage, radius, radius);
  206. if (this.percentage === 0 || (this.counterclockwise && startPercentage === 100)) {
  207. ctx.draw();
  208. return;
  209. }
  210. }
  211. let eAngle = ((2 * Math.PI) / 100) * startPercentage + this.sAngle;
  212. this.drawArc(ctx, eAngle);
  213. setTimeout(() => {
  214. this.startPercentage = startPercentage;
  215. this.drawProgressCircular(startPercentage);
  216. this.$emit('change', {
  217. percentage: startPercentage
  218. });
  219. }, time);
  220. // #ifdef H5
  221. // requestAnimationFrame(()=>{})
  222. // #endif
  223. },
  224. //创建弧线
  225. drawArc(ctx, eAngle) {
  226. ctx.setLineCap(this.lineCap);
  227. ctx.beginPath();
  228. let radius = this.diam / 2; //x=y
  229. ctx.arc(radius, radius, radius - this.lineWidth, this.sAngle, eAngle, this.counterclockwise);
  230. ctx.stroke();
  231. ctx.draw();
  232. },
  233. //生成canvasId
  234. getCanvasId() {
  235. let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  236. return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16);
  237. });
  238. return uuid;
  239. }
  240. }
  241. };
  242. </script>
  243. <style scoped>
  244. .tui-circular-container,
  245. .tui-circular-default {
  246. position: relative;
  247. }
  248. .tui-circular-progress {
  249. position: absolute;
  250. left: 0;
  251. top: 0;
  252. z-index: 10;
  253. }
  254. </style>