zhangwei
2025-03-07 d3e6dd6b3032c119676efa1e9fcee3a8a8043fc5
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
// 本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:  163,营业执照号:91    51 01 3 13      320 061  93 K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。
// #ifdef MP-WEIXIN
export default {
    data() {
        return {
            start: 0,
            isReady: false
        }
    },
    created() {
        this.context = null;
        this.canvas = null;
    },
    watch: {
        percent(val) {
            setTimeout(() => {
                this.isReady && this.initDraw()
            }, 20)
        },
        w(val) {
            setTimeout(() => {
                this.isReady && this.initDraw()
            }, 20)
        },
        strokeWidth(val) {
            setTimeout(() => {
                this.isReady && this.initDraw()
            }, 20)
        }
    },
    mounted() {
        this.$nextTick(() => {
            setTimeout(() => {
                this.init();
            }, 50)
        })
    },
    methods: {
        //初始化绘制
        init() {
            uni.createSelectorQuery().in(this)
                .select(`#${this.circleId}`)
                .fields({
                    node: true,
                    size: true,
                })
                .exec(this.initDraw.bind(this))
        },
        initDraw(res) {
            this.isReady = true;
            let start = this.activeMode === 'backwards' ? 0 : this.start;
            start = start > this.percent ? 0 : start;
            if (res) {
                const width = res[0].width
                const height = res[0].height
                const canvas = res[0].node
                const ctx = canvas.getContext('2d')
                const dpr = uni.getSystemInfoSync().pixelRatio
                canvas.width = width * dpr
                canvas.height = height * dpr
                ctx.scale(dpr, dpr)
                this.context = ctx;
                this.canvas = canvas
                this.drawCircle(start, ctx, canvas);
            } else {
                this.drawCircle(start, this.context, this.canvas);
            }
        },
        drawDefaultCircle(ctx, canvas) {
            //终止弧度
            let eAngle = Math.PI * 2 + Number(this.sAngle) * Math.PI;
            this.drawArc(ctx, eAngle, this.background);
        },
        drawpercent(ctx, percent) {
            ctx.save();
            ctx.beginPath();
            ctx.fillStyle = this.color || this.primaryColor;
            ctx.font = this.size + "px Arial";
            ctx.textAlign = "center";
            ctx.textBaseline = "middle";
            let radius = this.w / 2;
            percent = this.counterclockwise ? 100 - percent : percent;
            percent = percent.toFixed(0) + "%"
            ctx.fillText(percent, radius, radius);
            ctx.stroke();
            ctx.restore();
        },
        drawCircle(start, ctx, canvas) {
            if (!ctx || !canvas) return;
            let that = this
            let percent = that.percent;
            let requestId = null
            let renderLoop = () => {
                drawFrame((res) => {
                    if (res) {
                        requestId = canvas.requestAnimationFrame(renderLoop)
                    } else {
                        canvas.cancelAnimationFrame(requestId)
                        requestId = null;
                        renderLoop = null;
                    }
                })
            }
            requestId = canvas.requestAnimationFrame(renderLoop)
 
            function drawFrame(callback) {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                if (that.defaultShow) {
                    that.drawDefaultCircle(ctx, canvas)
                }
                if (that.show) {
                    that.drawpercent(ctx, start);
                }
                let isEnd = (percent == 0 || (that.counterclockwise && start == 100));
                if (!isEnd) {
                    let eAngle = ((2 * Math.PI) / 100) * start + Number(that.sAngle) * Math.PI;
                    let gradient = that.foreground || that.primaryColor;
                    if (that.gradient) {
                        gradient = ctx.createLinearGradient(0, 0, Number(that.w), 0);
                        gradient.addColorStop(0, that.gradient);
                        gradient.addColorStop(1, (that.foreground || that.primaryColor));
                    }
                    that.drawArc(ctx, eAngle, gradient);
                }
                that.$emit('change', {
                    percent: start
                });
                if (start >= percent) {
                    that.start = start
                    that.$emit('end', {
                        canvasId: that.circleId,
                        percent: percent
                    });
                    canvas.cancelAnimationFrame(requestId)
                    callback && callback(false)
                    return;
                }
                let num = start + that.speed
                start = num > percent ? percent : num;
                callback && callback(true)
            }
 
        },
        drawArc(ctx, eAngle, strokeStyle) {
            ctx.save();
            ctx.beginPath();
            let sw = Number(this.strokeWidth);
            ctx.lineCap = this.lineCap;
            ctx.lineWidth = sw;
            ctx.strokeStyle = strokeStyle;
            let radius = this.w / 2;
            ctx.arc(radius, radius, radius - sw, Number(this.sAngle) * Math.PI, eAngle, this
                .counterclockwise);
            ctx.stroke();
            ctx.closePath();
            ctx.restore();
        }
    }
}
// #endif
 
 
// #ifndef MP-WEIXIN
export default {}
// #endif