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
// @ts-nocheck
// #ifndef UNI-APP-X && APP
import {isBrowser} from '../isBrowser'
class Image {
    currentSrc: string | null = null
    naturalHeight: number = 0
    naturalWidth: number = 0
    width: number = 0
    height: number = 0
    tagName: string = 'IMG'
    path: string = ''
    crossOrigin: string = ''
    referrerPolicy: string = ''
    onload: () => void = () => {}
    onerror: () => void = () => {}
    complete: boolean = false
    constructor() {}
    set src(src: string) {
        console.log('src', src)
        if(!src) {
            return this.onerror()
        }
        src = src.replace(/^@\//,'/')
        this.currentSrc = src
        uni.getImageInfo({
            src,
            success: (res) => {
                const localReg = /^\.|^\/(?=[^\/])/;
                // #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO
                res.path = localReg.test(src) ?  `/${res.path}` : res.path;
                // #endif
                this.complete = true
                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
    }
}
interface UniImage extends WechatMiniprogram.Image {
    complete?: boolean
    naturalHeight?: number
    naturalWidth?: number
}
/** 创建用于 canvas 的 img */
export function createImage(canvas?: any): HTMLImageElement | UniImage {
    if(canvas && canvas.createImage) {
        return (canvas as WechatMiniprogram.Canvas).createImage()
    } else if(this && this['tagName'] == 'canvas' && !('toBlob' in this) || canvas && !('toBlob' in canvas)){
        return new Image()
    } else if(isBrowser) {
        return new window.Image()
    }
    return new Image()
}
// #endif
 
 
// #ifdef UNI-APP-X && APP
export function createImage():Image{
    // console.error('当前环境不支持')
    return new Image()
}
// #endif