zengh
2022-09-14 8ea19a6bb34daab1410f6186992a4c1e90299c92
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
const cacheChart = {}
class Image {
    constructor() {
        // __zr.dom
        this.currentSrc = null
        this.naturalHeight = 0
        this.naturalWidth = 0
        this.width = 0
        this.height = 0
        this.tagName = 'IMG'
    }
    set src(src) {
        this.currentSrc = src
        uni.getImageInfo({
            src,
            success: (res) => {
                this.naturalWidth = this.width = res.width
                this.naturalHeight = this.height = res.height
                this.onload()
            },
            fail: () => {
                this.onerror()
            }
        })
    }
    get src() {
        return this.currentSrc
    }
}
export default class Canvas {
    constructor(ctx, canvasId, isNew, canvasNode) {
        cacheChart[canvasId] = {ctx}
        this.canvasId = canvasId;
        this.chart = null;
        this.isNew = isNew
        if (isNew) {
            this.canvasNode = canvasNode;
        } else {
            this.canvasNode = {
                createImage: () => {
                    return new Image()
                }
            };
            this._initStyle(ctx);
        }
        this._initEvent();
    }
 
    getContext(contextType) {
        if (contextType === '2d') {
            return this.ctx;
        }
    }
    setChart(chart) {
        this.chart = chart;
    }
    attachEvent(e) {}
    detachEvent() {}
    addEventListener() {}
    removeEventListener() {}
    _initCanvas(zrender, ctx) {
        zrender.util.getContext = function() {
            return ctx;
        };
        zrender.util.$override('measureText', function(text, font) {
            ctx.font = font || '12px sans-serif';
            return ctx.measureText(text, font);
        });
    }
    strLen(str) {
        let len = 0;
        for (let i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 128) {
                len++;
            } else {
                len += 2;
            }
        }
        return len;
    }
    _initStyle(ctx) {
        var styles = [
            'fillStyle',
            'strokeStyle',
            'fontSize',
            'globalAlpha',
            'opacity',
            'textAlign',
            'textBaseline',
            'shadow',
            'lineWidth',
            'lineCap',
            'lineJoin',
            'lineDash',
            'miterLimit',
            // #ifdef MP-TOUTIAO
            'font'
            // #endif
        ];
        const fontSizeReg = /([\d\.]+)px/;
        const colorReg = /#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])\b/g;
        styles.forEach(style => {
            Object.defineProperty(ctx, style, {
                set: value => {
                    if (style === 'font' && fontSizeReg.test(value)) {
                        const match = fontSizeReg.exec(value);
                        ctx.setFontSize(match[1]);
                        return;
                    }
                    if (style === 'opacity') {
                        ctx.setGlobalAlpha(value)
                        return;
                    }
                    if (style !== 'fillStyle' && style !== 'strokeStyle' || value !== 'none' && value !== null) {
                        // #ifdef H5 || APP-PLUS || MP-BAIDU
                        if(typeof value == 'object') {
                            if (value.hasOwnProperty('colorStop') || value.hasOwnProperty('colors')) {
                                ctx['set' + style.charAt(0).toUpperCase() + style.slice(1)](value);
                            }
                            return
                        } 
                        // #endif
                        // #ifdef MP-TOUTIAO
                        if(colorReg.test(value)) {
                            value = value.replace(colorReg, '#$1$1$2$2$3$3')
                        }
                        // #endif
                        ctx['set' + style.charAt(0).toUpperCase() + style.slice(1)](value);
                    }
                }
            });
        });
        if(!this.isNew) {
            ctx.uniDrawImage = ctx.drawImage
            ctx.drawImage = (...a) => {
                a[0] = a[0].src
                ctx.uniDrawImage(...a)
            }
        }
        if(!ctx.createRadialGradient) {
            ctx.createRadialGradient = function() {
                return ctx.createCircularGradient(...[...arguments].slice(-3))
            };
        }
        // 字节不支持
        if (!ctx.strokeText) {
            ctx.strokeText = (...a) => {
                ctx.fillText(...a)
            }
        }
        // 钉钉不支持 
        if (!ctx.measureText) {
            ctx.measureText = (text, font) => {
                let fontSize = 12;
                if (font) {
                    fontSize = parseInt(font.match(/([\d\.]+)px/)[1])
                }
                fontSize /= 2;
                return {
                    width: this.strLen(text) * fontSize
                };
            }
        }
    }
 
    _initEvent() {
        this.event = {};
        const eventNames = [{
            wxName: 'touchStart',
            ecName: 'mousedown'
        }, {
            wxName: 'touchMove',
            ecName: 'mousemove'
        }, {
            wxName: 'touchEnd',
            ecName: 'mouseup'
        }, {
            wxName: 'touchEnd',
            ecName: 'click'
        }];
 
        eventNames.forEach(name => {
            this.event[name.wxName] = e => {
                const touch = e.touches[0];
                this.chart.getZr().handler.dispatch(name.ecName, {
                    zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
                    zrY: name.wxName === 'tap' ? touch.clientY : touch.y
                });
            };
        });
    }
 
    set width(w) {
        if (this.canvasNode) this.canvasNode.width = w
    }
    set height(h) {
        if (this.canvasNode) this.canvasNode.height = h
    }
 
    get width() {
        if (this.canvasNode)
            return this.canvasNode.width
        return 0
    }
    get height() {
        if (this.canvasNode)
            return this.canvasNode.height
        return 0
    }
    get ctx() {
        return cacheChart[this.canvasId]['ctx'] || null
    }
    set chart(chart) {
        cacheChart[this.canvasId]['chart'] = chart
    }
    get chart() {
        return cacheChart[this.canvasId]['chart'] || null
    }
}