cuiqian2004
2025-07-10 2db331628bbf94deee446d6e172e98f4db474a33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<template>
    <view class="circle-progress-bar" 
    :style="{
        width: sunit(size),
        height: sunit(size),
    }">
        <view class="circle" :change:prop="animateModule.pro" :prop="cpro"
        :data-animate="animate"
        :style="{
            transform: `rotate(${start * 360 + 45}deg)`,
            border: `${sunit(border_width)} solid ${border_color}`,
        }">
        </view>
        <view class="bg" v-if="background"
        :style="{
            background: background,
        }"></view>
        <view class="border-back" v-if="border_back_color"
        :style="{
            border: `calc(${sunit(border_width)} - 1px) solid ${border_back_color}`
        }"></view>
        <view class="center">
            <slot :pro="cpro"></slot>
        </view>
    </view>
</template>
 
<script module="animateModule" lang="wxs">
    var Timing = {
        easeIn: function easeIn(pos) {
            return Math.pow(pos, 3);
        },
        easeOut: function easeOut(pos) {
            return Math.pow(pos - 1, 3) + 1;
        },
        easeInOut: function easeInOut(pos) {
            if ((pos /= 0.5) < 1) {
                return 0.5 * Math.pow(pos, 3);
            } else {
                return 0.5 * (Math.pow(pos - 2, 3) + 2);
            }
        },
        linear: function linear(pos) {
            return pos;
        }
    };
 
    //#ifdef MP
    function setTimeout(t, cb, d) {
        if (d > 0) {
            var s = getDate().getTime();
            var fn = function () {
                if (getDate().getTime() - s > d) {
                    cb && cb();
                } else
                    t.requestAnimationFrame(fn);
            }
            fn();
        }
        else
            cb && cb();
    }
    //#endif
 
    function Animation(opts) {
        opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
        opts.timing = opts.timing || 'linear';
        var delay = 17;
 
        function createAnimationFrame() {
            if (typeof setTimeout !== 'undefined') {
                return function(step, delay) {
                    //#ifndef MP
                    setTimeout(function() {
                        var timeStamp = +new Date();
                        step(timeStamp);
                    }, delay);
                    //#endif
                    //#ifdef MP
                    setTimeout(opts.instance, function () {
                        var timeStamp = getDate()
                        step(timeStamp);
                    }, delay)
                    //#endif
                };
            } else if (typeof requestAnimationFrame !== 'undefined') {
                return requestAnimationFrame;
            } else {
                return function(step) {
                    step(null);
                };
            }
        };
        var animationFrame = createAnimationFrame();
        var startTimeStamp = null;
        var _step = function step(timestamp) {
            if (timestamp === null) {
                opts.onProcess && opts.onProcess(1);
                opts.onAnimationFinish && opts.onAnimationFinish();
                return;
            }
            if (startTimeStamp === null) {
                startTimeStamp = timestamp;
            }
            if (timestamp - startTimeStamp < opts.duration) {
                var process = (timestamp - startTimeStamp) / opts.duration;
                var timingFunction = Timing[opts.timing];
                process = timingFunction(process);
 
                opts.onProcess && opts.onProcess(process);
                animationFrame(_step, delay);
            } else {
                opts.onProcess && opts.onProcess(1);
                opts.onAnimationFinish && opts.onAnimationFinish();
            }
        };
        animationFrame(_step, delay);
    }
 
    function getPath(deg) {
        var path = '50% 50%'
        //各个锚点
        var ps = ['0% 0%', '100% 0%', '100% 100%', '0% 100%']
        var ps1 = path + ',' + ps[0]
        var ps2 = ps1 + ',' + ps[1]
        var ps3 = ps2 + ',' + ps[2]
        var ps4 = ps3 + ',' + ps[3]
        var ops = [
            function(per) { return ps1 + ',' + (per + '% 0%') },
            function(per) { return ps2 + ',' + ('100% ' + per + '%') },
            function(per) { return ps3 + ',' + (100 - per) + '% 100%' },
            function(per) { return ps4 + ',' + '0% ' + (100 - per) + '%' },
        ]
        if (deg == 0) {
            return 'polygon(50% 50%, 50% 0%)'
        }
        else if (deg % 360 == 0) {
            return ''
        }
        var idx = parseInt(deg / 90) % 4
        var pdeg = deg % 90
        var per = pdeg / 90 * 100
        if(ops[idx]) {
            return 'polygon(' + ops[idx](per) + ')'
        }
        else {
            return ''
        }
    }
 
    function setDeg(newValue, oldValue, ownerInstance, instance) {
        var odeg = oldValue * 360
        var deg = newValue * 360
        var offset = deg - odeg
        
        var ds = instance.getDataset()
        if(!ds.animate) {
            var path = getPath(deg)
            instance.setStyle({
                'clip-path': path,
            })
            return
        }
        Animation({
            instance: ownerInstance,
            timing: 'easeInOut',
            duration: 300,
            onProcess: function onProcess(process) {
                var pdeg = odeg + process * offset
                var path = getPath(pdeg)
                var com = ownerInstance.selectComponent('.circle');
                com.setStyle({
                    'clip-path': path,
                })
            },
            onAnimationFinish: function onAnimationFinish() {}
        });
    }
    module.exports = {
        pro: setDeg,
    }
</script>
 
<script>
    export default {
        props: {
            pro: {
                type: Number,
                default: 0
            },
            //起始位置 0-1
            start: {
                type: Number,
                default: 0,
            },
            //圆形大小
            size: {
                type: Number,
                default: 200
            },
            //线宽度
            border_width: {
                type: Number,
                default: 20
            },
            //线颜色
            border_color: {
                type: String,
                default: '#07C160',
            },
            //线背景色
            border_back_color: {
                type: String,
            },
            //中心内容背景色
            background: {
                type: String,
            },
            //单位
            unit: {
                type: String,
                default: 'rpx',
            },
            //是否启用动画
            animate:{
                type: Boolean,
                default: true,
            }
        },
        data() {
            return {
                cpro: 0,
            }
        },
        watch: {
            pro(val) {
                this.cpro = val
            }
        },
        mounted() {
            this.cpro = this.pro
        },
        methods: {
            sunit(num) {
                if(typeof num === 'number') {
                    return num + this.unit
                }
            }
        }
    }
</script>
 
<style scoped lang="scss">
    .circle-progress-bar{
        position: relative;
    }
    .circle, .bg, .border-back {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        position: absolute;
        box-sizing: border-box;
    }
    .circle{
        z-index: 1;
    }
    .border-back{
        height: calc(100% - 1px);
        width: calc(100% - 1px);
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
    .point {
        position: absolute;
        border-radius: 50%;
        z-index: 1;
    }
    .center{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        z-index: 2;
    }
</style>