UNPKG

tav-media

Version:

Cross platform media editing framework

54 lines (53 loc) 1.82 kB
/* eslint-disable no-param-reassign */ import { Http } from '../io/http'; export class ImageReader { constructor(path, rect) { this.textureId = 0; this.released = false; /** * Browser, it will be 1 in WeChat reader */ this.type = 0; this.width = rect.right - rect.left; this.height = rect.bottom - rect.top; this.path = path; ImageReader.preload(path); this.imageEl = ImageReader.images[path]; this.width = this.imageEl.width || this.width; this.height = this.imageEl.height || this.height; } static preload(path) { if (this.images[path]) { return Promise.resolve(this.images[path]); } const imageEl = new Image(); imageEl.src = Http.getUrl(path); imageEl.crossOrigin = 'anonymous'; this.images[path] = imageEl; return new Promise((res, rej) => { imageEl.onload = _e => res(imageEl); imageEl.onerror = _e => rej(imageEl); }); } renderToTexture(GL) { if (!this.textureId || !GL.textures[this.textureId]) { const gl = GL.currentContext.GLctx; this.texture = gl.createTexture(); this.textureId = GL.getNewId(GL.textures); this.texture.name = this.textureId; GL.textures[this.textureId] = this.texture; gl.bindTexture(gl.TEXTURE_2D, this.texture); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.imageEl); gl.bindTexture(gl.TEXTURE_2D, null); } return { textureId: this.textureId, width: this.width, height: this.height, }; } release() { this.textureId = 0; } } ImageReader.images = {};