u-circle-progress.vue 6.8 KB

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