web
20 小时以前 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
74
75
76
77
78
 
// @ts-nocheck
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()
 
export async function getCanvas(canvasId: string, options: {context: ComponentInternalInstance}) {
    let { context } = options
    // #ifdef MP || VUE2
    if (context.proxy) context = context.proxy
    // #endif
    return getRect('#' + canvasId, context, isCanvas2d).then(res => {
        if(res.node){
            return res.node
        } else {
            const ctx = uni.createCanvasContext(canvasId, context)
            return {
                getContext(type: string) {
                    if(type == '2d') {
                        return ctx
                    }
                },
                width: res.width,
                height: res.height,
            }
            // #ifdef H5
            // canvas.value = context.proxy.$el.querySelector('#'+ canvasId)
            // #endif
        }
    })
}
 
// #ifndef H5 || APP-NVUE
class Image {
    currentSrc: string | null = null
    naturalHeight: number = 0
    naturalWidth: number = 0
    width: number = 0
    height: number = 0
    tagName: string = 'IMG'
    path: any = ''
    crossOrigin: any = ''
    referrerPolicy: any = ''
    onload: () => void
    onerror: () => void
    constructor() {}
    set src(src) {
        this.currentSrc = src
        uni.getImageInfo({
            src,
            success: (res) => {
                this.path = res.path
                this.naturalWidth = this.width = res.width
                this.naturalHeight = this.height = res.height
                this.onload()
            },
            fail: () => {
                this.onerror()
            }
        })
    }
    get src() {
        return this.currentSrc
    }
}
// #endif
 
export function createImage(canvas: WechatMiniprogram.Canvas) {
    if(canvas && canvas.createImage) {
        return canvas.createImage()
    } else if(typeof window != 'undefined' && window.Image) {
        return new window.Image()
    }
    // #ifndef H5 || APP-NVUE
    return new Image()
    // #endif
}