UNPKG

tav-media

Version:

Cross platform media editing framework

121 lines (120 loc) 4.42 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 { tav } from '../tav'; /** * A surface is used to render the video frames. * @category Engine */ export class TAVSurface { constructor(ref) { this.ref = ref; } /** * Make a surface from a HTML canvas. * @param canvasOrSelector A canvas element or a selector of a canvas element. * @returns A surface. */ static FromHtmlCanvas(canvasOrSelector) { let canvas; if (typeof (canvasOrSelector) === 'string') { canvas = document.querySelector(canvasOrSelector); } else { canvas = canvasOrSelector; } const offscreenCanvas = document.createElement('canvas'); offscreenCanvas.width = canvas.width; offscreenCanvas.height = canvas.height; const gl = offscreenCanvas.getContext('webgl'); const ctx = canvas.getContext('2d'); const surface = this.FromWebGLContext(gl, offscreenCanvas.width, offscreenCanvas.height, true); surface.offscreenCanvas = offscreenCanvas; surface.present = () => { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(offscreenCanvas, 0, 0, canvas.width, canvas.height); gl.clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT); }; return surface; } static FromWebGLContext(glContext, width, height, flipY = true) { const contextId = tav.GL.registerContext(glContext, { majorVersion: 1 }); tav.GL.makeContextCurrent(contextId); const ref = tav.TAVSurface.FromFrameBuffer(0, width, height, flipY); if (!ref) return null; const surface = new TAVSurface(ref); surface.webGLContextId = contextId; return surface; } /** * Returns the width in pixels of the surface. */ width() { var _a; return ((_a = this.ref) === null || _a === void 0 ? void 0 : _a.width()) || 0; } /** * Returns the height in pixels of the surface. */ height() { var _a; return ((_a = this.ref) === null || _a === void 0 ? void 0 : _a.height()) || 0; } /** * Update the size of surface, and reset the internal surface. */ updateSize() { var _a; (_a = this.ref) === null || _a === void 0 ? void 0 : _a.updateSize(); } /** * Erases all pixels of this surface with transparent color. Returns true if the content has * changed. */ clearAll() { var _a; (_a = this.ref) === null || _a === void 0 ? void 0 : _a.clearAll(); } /** * Free the cache created by the surface immediately. Can be called to reduce memory pressure. */ freeCache() { var _a; (_a = this.ref) === null || _a === void 0 ? void 0 : _a.freeCache(); } /** * Copies pixels from current TAVSurface to dstPixels with specified color type, alpha type and * row bytes. */ readPixels(colorType = 2 /* ColorType.RGBA_8888 */, alphaType = 2 /* AlphaType.Premultiplied */) { var _a; let rowBytes = colorType === 1 /* ColorType.ALPHA_8 */ ? 1 : 4; rowBytes = rowBytes * this.width(); const buffer = (_a = this.ref) === null || _a === void 0 ? void 0 : _a.readPixels(colorType, alphaType, rowBytes); return new Uint8Array(buffer); } /** * Build native object * @returns The native TAVSurface object. */ build() { return __awaiter(this, void 0, void 0, function* () { return this.ref; }); } /** * release native object */ release() { var _a; (_a = this.ref) === null || _a === void 0 ? void 0 : _a.delete(); this.ref = null; } }