zhangback
2026-01-13 487ef37ec34860b255986d8716d193369024e000
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
const uniPlatform = uni.getSystemInfoSync().uniPlatform
 
export const uniContext = (canvasId, context) => {
    let ctx = uni.createCanvasContext(canvasId, context)
    if (!ctx.uniDrawImage) {
        ctx.uniDrawImage = ctx.drawImage
        ctx.drawImage = (image, ...agrs) => {
            ctx.uniDrawImage(image.src, ...agrs)
        }
    }
 
    if (!ctx.getImageData) {
        ctx.getImageData = (x, y, width, height) => {
            return new Promise((resolve, reject) => {
                // #ifdef MP || VUE2
                if (context.proxy) context = context.proxy
                // #endif
                uni.canvasGetImageData({
                    canvasId,
                    x,
                    y,
                    width:parseInt(width),
                    height:parseInt(height),
                    success(res) {
                        resolve(res)
                    },
                    fail(error) {
                        reject(error)
                    }
                }, context)
            })
        }
    } else {
        ctx._getImageData = ctx.getImageData
        ctx.getImageData = (x, y, width, height) => {
            return new Promise((resolve, reject) => {
                ctx._getImageData({
                    x,
                    y,
                    width: parseInt(width) ,
                    height:parseInt(height),
                    success(res) {
                        resolve(res)
                    },
                    fail(error) {
                        reject(error)
                    }
                })
            })
        }
    }
 
    return ctx
}
 
class Image {
    constructor() {
        this.currentSrc = null
        this.naturalHeight = 0
        this.naturalWidth = 0
        this.width = 0
        this.height = 0
        this.tagName = 'IMG'
    }
    onerror() {}
    onload() {}
    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 const createImage = () => {
    return new Image()
}
export function useCurrentPage() {
    const pages = getCurrentPages();
    return pages[pages.length - 1];
}
export const toDataURL = (canvasId, context, options = {}) => {
    // #ifdef MP-QQ
    // context = context.$scope
    // #endif
    // #ifdef MP-ALIPAY
    context = ''
    // #endif
 
    return new Promise((resolve, reject) => {
        let {
            canvas,
            width,
            height,
            destWidth = 0,
            destHeight = 0,
            x = 0,
            y = 0,
            preferToDataURL
        } = options
        const {
            pixelRatio
        } = uni.getSystemInfoSync()
        
        // #ifdef MP-ALIPAY
        const isDD = typeof dd != 'undefined'
        if (!isDD && (!destWidth || !destHeight)) {
            destWidth = width * pixelRatio;
            destHeight = height * pixelRatio;
            width = destWidth;
            height = destHeight;
            x = x * pixelRatio
            y = y * pixelRatio
        }
        // #endif
        const params = {
            ...options,
            canvasId,
            id: canvasId,
            // #ifdef MP-ALIPAY
            x,
            y,
            width,
            height,
            destWidth,
            destHeight,
            // #endif
            canvas,
            success: (res) => {
                resolve(res.tempFilePath)
            },
            fail: (err) => {
                reject(err)
            }
        }
        // 抖音小程序canvas 2d不支持canvasToTempFilePath
        if (canvas && canvas.toDataURL && preferToDataURL) {
            let next = true
            const devtools = uni.getSystemInfoSync().platform == 'devtools'
            // #ifdef MP-TOUTIAO
            next = uni.getSystemInfoSync().platform != 'devtools'
            if (!next) {
                console.warn('[lime-signature] 抖音开发工具不支持bbox')
            }
            // #endif
            if ((x || y) && next) {
                const offCanvas = uni.createOffscreenCanvas({
                    type: '2d'
                });
                const ctx = offCanvas.getContext("2d");
                const destWidth = Math.floor(width * pixelRatio)
                const destHeight = Math.floor(height * pixelRatio)
                offCanvas.width = destWidth // canvas.width;
                offCanvas.height = destHeight // canvas.height;
                // ctx.scale(pixelRatio, pixelRatio)
                // ctx.drawImage(canvas, Math.floor(x*pixelRatio), Math.floor(y*pixelRatio), destWidth, destHeight, 0,0, destWidth, destHeight);
                // 抖音不能在drawImage使用canvas
                const image = canvas.createImage()
                image.onload = () => {
                    ctx.drawImage(image, Math.floor(x * pixelRatio), Math.floor(y * pixelRatio),
                        destWidth, destHeight, 0, 0, destWidth, destHeight)
                    const tempFilePath = offCanvas.toDataURL();
                    resolve(tempFilePath)
                    if (params.success) {
                        params.success({
                            tempFilePath
                        })
                    }
                }
                image.src = canvas.toDataURL()
 
            } else {
                const tempFilePath = canvas.toDataURL()
                resolve(tempFilePath)
                if (params.success) {
                    params.success({
                        tempFilePath
                    })
                }
            }
        } else if (canvas && canvas.toTempFilePath) {
            canvas.toTempFilePath(params)
        } else {
            uni.canvasToTempFilePath(params, context)
        }
    })
 
}