UNPKG

tav-media

Version:

Cross platform media editing framework

69 lines (68 loc) 2.77 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { releaseCanvas2D } from '../utils/canvas'; export class NativeImage { constructor(source, reuse = false) { this.source = source; this.reuse = reuse; } static createFromBytes(bytes) { return __awaiter(this, void 0, void 0, function* () { const blob = new Blob([bytes], { type: 'image/*' }); return new Promise((resolve) => { const image = new Image(); image.onload = function () { resolve(new NativeImage(image)); }; image.onerror = function () { console.error('image create from bytes error.'); resolve(null); }; image.src = URL.createObjectURL(blob); }); }); } static createFromPath(path) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => { const image = new Image(); image.onload = function () { resolve(new NativeImage(image)); }; image.onerror = function () { console.error(`image create from path error: ${path}`); resolve(null); }; image.src = path; }); }); } width() { return window.HTMLVideoElement && this.source instanceof HTMLVideoElement ? this.source.videoWidth : this.source.width; } height() { return window.HTMLVideoElement && this.source instanceof HTMLVideoElement ? this.source.videoHeight : this.source.height; } upload(GL) { var _a; const gl = (_a = GL.currentContext) === null || _a === void 0 ? void 0 : _a.GLctx; gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.source); } onDestroy() { if (this.reuse) { releaseCanvas2D(this.source); } } }