web
9 小时以前 49fa0d82a40345342966e810b44429aec0480ef3
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
 
// @ts-nocheck
// #ifndef UNI-APP-X && APP
import type { ComponentInternalInstance } from '@/uni_modules/lime-shared/vue'
import { getRect } from '@/uni_modules/lime-shared/getRect'
import { canIUseCanvas2d } from '@/uni_modules/lime-shared/canIUseCanvas2d'
export const isCanvas2d = canIUseCanvas2d()
// #endif
 
 
export function createCanvas(canvasId : string, component : ComponentInternalInstance) {
    // #ifdef UNI-APP-X
    uni.createCanvasContextAsync({
        canvasId,
        component,
        success(context : CanvasContext) {
 
        },
        fail(error : UniError) {
 
        }
    })
    // #endif
    // #ifndef UNI-APP-X
    const isCanvas2d = canIUseCanvas2d()
    getRect('#' + canvasId, context, isCanvas2d).then(res => {
        if (res.node) {
            res.node.width = res.width
            res.node.height = res.height
            return res.node
        } else {
            const ctx = uni.createCanvasContext(canvasId, context)
            if (!ctx._drawImage) {
                ctx._drawImage = ctx.drawImage
                ctx.drawImage = function (...args) {
                    const { path } = args.shift()
                    ctx._drawImage(path, ...args)
                }
            }
            if (!ctx.getImageData) {
                ctx.getImageData = function () {
                    return new Promise((resolve, reject) => {
                        uni.canvasGetImageData({
                            canvasId,
                            x: parseInt(arguments[0]),
                            y: parseInt(arguments[1]),
                            width: parseInt(arguments[2]),
                            height: parseInt(arguments[3]),
                            success(res) {
                                resolve(res)
                            },
                            fail(err) {
                                reject(err)
                            }
                        }, context)
                    })
 
                }
                return {
                    getContext(type: string) {
                        if(type == '2d') {
                            return ctx
                        }
                    },
                    width: res.width,
                    height: res.height,
                    createImage
                }
            }
        }
    })
    // #endif
}