zhangwei
2024-08-14 9d8994790fe403935ed46ad478f83ae110bb7a01
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
// 本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID: 16 3,营业执照号:9 1  5 1 0 1 31 33  2  0 0   61 93K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。
// #ifdef APP-NVUE
import {
    enable,
    WeexBridge
} from './gcanvas/index.js';
export default {
    data() {
        return {
            start: 0
        }
    },
    watch: {
        percent(val) {
            setTimeout(() => {
                this.init();
            }, 50)
        },
        w(val) {
            setTimeout(() => {
                this.init();
            }, 50)
        },
        strokeWidth(val) {
            setTimeout(() => {
                this.init();
            }, 50)
        }
    },
    created() {
        let sys = uni.getSystemInfoSync()
        this.isAndroid = sys.platform.toLocaleLowerCase() == "android"
        this.fontSize = this.isAndroid ? Number(this.size) : Number(this.size) * sys.pixelRatio
    },
    mounted() {
        setTimeout(() => {
            this.init(true);
        }, 50);
    },
    // #ifndef VUE3
    beforeDestroy() {
        this.context = null;
    },
    // #endif
    // #ifdef VUE3
    beforeUnmount() {
        this.context = null;
    },
    // #endif
    methods: {
        //初始化绘制
        init(init) {
            let start = this.activeMode === 'backwards' ? 0 : this.start;
            this.drawCircle(start);
        },
        //默认背景
        drawDefaultCircle(ctx) {
            ctx.setLineWidth(Number(this.strokeWidth));
            ctx.setStrokeStyle(this.background);
            let eAngle = Math.PI * 2 + Number(this.sAngle) * Math.PI;
            this.drawArc(ctx, eAngle, true);
        },
        //进度圆环
        drawCircle(start) {
            let ctx = this.context;
            if (!ctx) {
                let ganvas = this.$refs[this.circleId];
                let canvas = enable(ganvas, {
                    bridge: WeexBridge
                });
                ctx = canvas.getContext('2d');
                //android赋值后,canvas大小可能不受控制
                this.context = ctx;
            }
            //绘制默认背景
            if (!this.isAndroid) {
                ctx.setFillStyle(this.fillStyle)
                ctx.fillRect(0, 0, this.w, this.w)
            }
            // ctx.clearRect(0, 0, this.w, this.w)
            this.defaultShow && this.drawDefaultCircle(ctx)
            ctx.setLineWidth(Number(this.strokeWidth));
            ctx.beginPath()
            let gradient = this.foreground || this.primaryColor;
            // if (this.gradient) {
            //     gradient = ctx.createLinearGradient(0, 0, this.w, 0);
            //     gradient.addColorStop(0, this.gradient);
            //     gradient.addColorStop(1, this.foreground);
            // }
            ctx.setStrokeStyle(gradient);
            let percentage = Number(this.percent)
            if (percentage > 0) {
                start += Number(this.speed);
                start = start > percentage ? percentage : start;
            }
            if (this.show) {
                ctx.setFontSize(this.fontSize);
                ctx.setFillStyle(this.color || this.primaryColor);
                ctx.setTextAlign('center');
                ctx.setTextBaseline('middle');
                let percent = `${this.counterclockwise ? 100 - start : start}%`;
                let radius = this.w / 2;
                ctx.fillText(percent, radius, radius);
            }
            if (percentage == 0 || (this.counterclockwise && start == 100)) {
                ctx.draw();
            } else {
                let eAngle = ((2 * Math.PI) / 100) * start + Number(this.sAngle) * Math.PI;
                this.drawArc(ctx, eAngle);
            }
            setTimeout(() => {
                this.start = start;
                if (start == percentage) {
                    this.$emit('end', {
                        canvasId: this.circleId,
                        percent: start
                    });
                } else {
                    this.drawCircle(start);
                }
                this.$emit('change', {
                    percent: start
                });
            }, 1000 / 60);
        },
        drawArc(ctx, eAngle, def) {
            ctx.setLineCap(this.lineCap);
            ctx.beginPath();
            let radius = this.w / 2;
            ctx.arc(radius, radius, radius - Number(this.strokeWidth), Number(this.sAngle) * Math.PI, eAngle, this
                .counterclockwise);
            ctx.stroke();
            !def && ctx.draw();
        }
    }
}
 
// #endif
 
// #ifndef APP-NVUE
export default {}
// #endif