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