UNPKG

tav-media

Version:

Cross platform media editing framework

43 lines (42 loc) 1.38 kB
import { WEBGL_CONTEXT_ATTRIBUTES } from '../constant'; import { BackendContext } from './backend-context'; const renderCanvasList = []; export class RenderCanvas { constructor(canvas, contextAttributes) { this._canvas = null; this._glContext = null; this.retainCount = 0; this._canvas = canvas; const gl = canvas.getContext('webgl', Object.assign(Object.assign({}, WEBGL_CONTEXT_ATTRIBUTES), contextAttributes)); if (!gl) throw new Error('Canvas context is not WebGL!'); this._glContext = BackendContext.from(gl); } static from(canvas, contextAttributes) { let renderCanvas = renderCanvasList.find((targetCanvas) => targetCanvas.canvas === canvas); if (renderCanvas) return renderCanvas; renderCanvas = new RenderCanvas(canvas, contextAttributes); renderCanvasList.push(renderCanvas); return renderCanvas; } retain() { this.retainCount += 1; } release() { this.retainCount -= 1; if (this.retainCount === 0) { if (!this._glContext) return; this._glContext.destroy(); this._glContext = null; this._canvas = null; } } get canvas() { return this._canvas; } get glContext() { return this._glContext; } }