import { ImageConfig } from '../../../components/image/export'; import { getGlobalData } from '../../../utils/globalData'; import Element, { Position } from './base'; class Image extends Element { constructor(canvas: HTMLCanvasElement, scale: number) { super(canvas, 'image', scale); } private _image: WechatMiniprogram.Image | undefined; /** 资源本身的宽度 */ private resWidth = 0; /** 资源本身的高度 */ private resHeight = 0; /** 上一次load的图片地址 */ private prevSrc = ''; private _src = ''; public get src() { return this._src; } protected async setSrc() { return new Promise((reslove) => { this._image = (this.canvas as any as WechatMiniprogram.Canvas).createImage(); this._image.src = this._src; this._image.onload = (res: { path: ImageData[] }) => { reslove(null); }; this._image.onerror = (err) => { console.error(err); reslove(null); }; }); } /** * 从网络中加载图片 * @param src 图片全地址 * @returns 图片在微信中的本地地址 */ public async load(src: string): Promise { return new Promise(async (reslove, reject) => { // 防止reload时重复加载图片 if (this.prevSrc === src && this.resWidth && this.resHeight) { this.setSize(); await this.setSrc(); reslove(); return; } await this.getImageInfo(src); reslove(); }); } private getImageInfo(src: string) { return new Promise((reslove) => { wx.getImageInfo({ src: src, success: async (res) => { this._src = src.indexOf('://') > 0 ? res.path : '/' + res.path; this.resWidth = res.width; this.resHeight = res.height; this.prevSrc = src; this.setSize(); await this.setSrc(); reslove(null); }, fail: async (err) => { const defaultImage = getGlobalData().__image__.defaultImage; if (defaultImage && src !== defaultImage) { await this.getImageInfo(defaultImage); } console.error(err); reslove(null); } }); }); } public setSize() { const [autoWidth, autoHeight] = [this.resWidth, this.resHeight]; let [swidth, sheight] = [0, 0]; if (this.style.width === 'auto' && this.style.height === 'auto') { [swidth, sheight] = [autoWidth, autoHeight]; } else if (this.style.width === 'auto' && this.style.height !== 'auto') { sheight = this.style.getComputedStyle('height')!; swidth = autoWidth / autoHeight * sheight; } else if (this.style.width !== 'auto' && this.style.height === 'auto') { swidth = this.style.getComputedStyle('width')!; sheight = autoHeight / autoWidth * swidth; } else { swidth = typeof this.style.width === 'number' ? this.style.width : this.style.getComputedStyle('width'); sheight = typeof this.style.height === 'number' ? this.style.height : this.style.getComputedStyle('height'); } this.style.width = swidth; this.style.height = sheight; } public render(ctx: CanvasRenderingContext2D) { const x = this.style.x; const y = this.style.getComputedStyle('y'); const width = this.style.width as number; const height = this.style.height as number; ctx.save(); if (this.style.borderRadius) { this.style.drawBorderRadius(ctx); } if (this._src) { try { ctx.drawImage( this._image as any, x, y, // 经过前面setSize,这里宽度一定为number width, height, ); } catch (err) { console.error(err); } } ctx.restore(); } } export default Image;