u-circle-progress.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <view class="u-circle-progress" :style="{
  3. ...progressStyle,
  4. backgroundColor: bgColor
  5. }">
  6. <!-- 支付宝小程序不支持canvas-id属性,必须用id属性 -->
  7. <!-- #ifndef APP-NVUE -->
  8. <canvas class="u-canvas-bg" :canvas-id="elBgId" :id="elBgId" :style="progressStyle"></canvas>
  9. <canvas class="u-canvas" :canvas-id="elId" :id="elId" :style="progressStyle"></canvas>
  10. <!-- #endif -->
  11. <!-- #ifdef APP-NVUE -->
  12. <gcanvas class="u-canvas-bg" :ref="elBgId" :style="progressStyle"></gcanvas>
  13. <gcanvas class="u-canvas" :ref="elId" :style="progressStyle"></gcanvas>
  14. <!-- #endif -->
  15. <slot></slot>
  16. </view>
  17. </template>
  18. <script>
  19. import props from './props.js'
  20. import mixin from '../../libs/mixin/mixin'
  21. import mpMixin from '../../libs/mixin/mpMixin'
  22. // #ifdef APP-NVUE
  23. import { enable, WeexBridge } from '../../libs/gcanvas/index.js';
  24. // #endif
  25. /**
  26. * circleProgress 环形进度条
  27. * @description 展示操作或任务的当前进度,比如上传文件,是一个圆形的进度条。注意:此组件的percent值只能动态增加,不能动态减少。
  28. * @tutorial https://uview.d3u.cn/components/circleProgress.html
  29. * @property {String Number} percent 圆环进度百分比值,为数值类型,0-100
  30. * @property {String} inactive-color 圆环的底色,默认为灰色(该值无法动态变更)(默认#ececec)
  31. * @property {String} active-color 圆环激活部分的颜色(该值无法动态变更)(默认#19be6b)
  32. * @property {String Number} width 整个圆环组件的宽度,高度默认等于宽度值,单位rpx(默认200)
  33. * @property {String Number} border-width 圆环的边框宽度,单位rpx(默认14)
  34. * @property {String Number} duration 整个圆环执行一圈的时间,单位ms(默认呢1500)
  35. * @property {String} type 如设置,active-color值将会失效
  36. * @property {String} bg-color 整个组件背景颜色,默认为白色
  37. * @example <u-circle-progress active-color="#2979ff" :percent="80"></u-circle-progress>
  38. */
  39. export default {
  40. name: 'u-circle-progress',
  41. mixins: [mpMixin, mixin, props],
  42. data() {
  43. return {
  44. // #ifdef MP-WEIXIN
  45. elBgId: 'uCircleProgressBgId', // 微信小程序中不能使用this.$u.guid()形式动态生成id值,否则会报错
  46. elId: 'uCircleProgressElId',
  47. // #endif
  48. // #ifndef MP-WEIXIN
  49. elBgId: uni.$u.guid(), // 非微信端的时候,需用动态的id,否则一个页面多个圆形进度条组件数据会混乱
  50. elId: uni.$u.guid(),
  51. // #endif
  52. widthPx: this.width, // 转成px后的整个组件的背景宽度
  53. borderWidthPx:this.borderWidth, // 转成px后的圆环的宽度
  54. startAngle: -Math.PI / 2, // canvas画圆的起始角度,默认为3点钟方向,定位到12点钟方向
  55. progressContext: null, // 活动圆的canvas上下文
  56. newPercent: 0, // 当动态修改进度值的时候,保存进度值的变化前后值,用于比较用
  57. oldPercent: 0 // 当动态修改进度值的时候,保存进度值的变化前后值,用于比较用
  58. };
  59. },
  60. watch: {
  61. percent(nVal, oVal = 0) {
  62. if (nVal > 100) nVal = 100;
  63. if (nVal < 0) oVal = 0;
  64. // 此值其实等于this.percent,命名一个新
  65. this.newPercent = nVal;
  66. this.oldPercent = oVal;
  67. setTimeout(() => {
  68. // 无论是百分比值增加还是减少,需要操作还是原来的旧的百分比值
  69. // 将此值减少或者新增到新的百分比值
  70. this.drawCircleByProgress(oVal);
  71. }, 50);
  72. }
  73. },
  74. created() {
  75. // 赋值,用于加载后第一个画圆使用
  76. this.newPercent = this.percent;
  77. this.oldPercent = 0;
  78. },
  79. computed: {
  80. // 有type主题时,优先起作用
  81. circleColor() {
  82. if (['success', 'error', 'info', 'primary', 'warning'].indexOf(this.type) >= 0){
  83. return this.$u.theme[this.type];
  84. }else{
  85. return this.activeColor;
  86. }
  87. },
  88. progressStyle() {
  89. return {
  90. width: uni.$u.addUnit(this.widthPx),
  91. height: uni.$u.addUnit(this.widthPx),
  92. }
  93. },
  94. },
  95. mounted() {
  96. this.$nextTick(() => {
  97. setTimeout(() => {
  98. this.drawProgressBg();
  99. this.drawCircleByProgress(this.oldPercent);
  100. }, 100)
  101. })
  102. },
  103. methods: {
  104. drawProgressBg() {
  105. // #ifdef APP-NVUE
  106. let ganvas = this.$refs[this.elBgId];
  107. let canvasObj = enable(ganvas, {bridge: WeexBridge});
  108. let ctx = canvasObj.getContext('2d');
  109. // #endif
  110. // #ifndef APP-NVUE
  111. // #ifdef MP-ALIPAY
  112. let ctx = uni.createCanvasContext(this.elBgId);
  113. // #endif
  114. // #ifndef MP-ALIPAY
  115. let ctx = uni.createCanvasContext(this.elBgId, this);
  116. // #endif
  117. // #endif
  118. ctx.setLineWidth(this.borderWidthPx); // 设置圆环宽度
  119. ctx.setStrokeStyle(this.inactiveColor); // 线条颜色
  120. ctx.beginPath(); // 开始描绘路径
  121. // 设置一个原点(110,110),半径为100的圆的路径到当前路径
  122. let radius = this.widthPx / 2;
  123. ctx.arc(radius, radius, radius - this.borderWidthPx, 0, 2 * Math.PI, false);
  124. ctx.stroke(); // 对路径进行描绘
  125. ctx.draw()
  126. },
  127. drawCircleByProgress(progress) {
  128. // 第一次操作进度环时将上下文保存到了this.data中,直接使用即可
  129. let ctx = this.progressContext;
  130. if (!ctx) {
  131. // #ifdef APP-NVUE
  132. let ganvas = this.$refs[this.elId];
  133. let canvasObj = enable(ganvas, {bridge: WeexBridge});
  134. ctx = canvasObj.getContext('2d');
  135. // #endif
  136. // #ifndef APP-NVUE
  137. // #ifdef MP-ALIPAY
  138. ctx = uni.createCanvasContext(this.elId);
  139. // #endif
  140. // #ifndef MP-ALIPAY
  141. ctx = uni.createCanvasContext(this.elId, this);
  142. // #endif
  143. // #endif
  144. this.progressContext = ctx;
  145. }
  146. // 表示进度的两端为圆形
  147. ctx.setLineCap('round');
  148. // 设置线条的宽度和颜色
  149. ctx.setLineWidth(this.borderWidthPx);
  150. ctx.setStrokeStyle(this.circleColor);
  151. // 将总过渡时间除以100,得出每修改百分之一进度所需的时间
  152. let time = Math.floor(this.duration / 100);
  153. // 结束角的计算依据为:将2π分为100份,乘以当前的进度值,得出终止点的弧度值,加起始角,为整个圆从默认的
  154. // 3点钟方向开始画图,转为更好理解的12点钟方向开始作图,这需要起始角和终止角同时加上this.startAngle值
  155. let endAngle = ((2 * Math.PI) / 100) * progress + this.startAngle;
  156. ctx.beginPath();
  157. // 半径为整个canvas宽度的一半
  158. let radius = this.widthPx / 2;
  159. ctx.arc(radius, radius, radius - this.borderWidthPx, this.startAngle, endAngle, false);
  160. ctx.stroke();
  161. ctx.draw();
  162. // 如果变更后新值大于旧值,意味着增大了百分比
  163. if (this.newPercent > this.oldPercent) {
  164. // 每次递增百分之一
  165. progress++;
  166. // 如果新增后的值,大于需要设置的值百分比值,停止继续增加
  167. if (progress > this.newPercent) return;
  168. } else {
  169. // 同理于上面
  170. progress--;
  171. if (progress < this.newPercent) return;
  172. }
  173. setTimeout(() => {
  174. // 定时器,每次操作间隔为time值,为了让进度条有动画效果
  175. this.drawCircleByProgress(progress);
  176. }, time);
  177. }
  178. }
  179. };
  180. </script>
  181. <style lang="scss" scoped>
  182. @import "../../libs/css/components.scss";
  183. .u-circle-progress {
  184. position: relative;
  185. /* #ifndef APP-NVUE */
  186. display: inline-flex;
  187. /* #endif */
  188. align-items: center;
  189. justify-content: center;
  190. }
  191. .u-canvas-bg {
  192. position: absolute;
  193. }
  194. .u-canvas {
  195. position: absolute;
  196. }
  197. </style>