u-circle-progress.vue 7.5 KB

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