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