UNPKG

tav-media

Version:

Cross platform media editing framework

144 lines (143 loc) 6.26 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; 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()); }); }; var PAGView_1; import { PAGModule } from '../pag-module'; import { PAGView as NativePAGView } from '../pag-view'; import { RenderCanvas } from '../core/render-canvas'; import { BackendContext } from '../core/backend-context'; import { destroyVerify, wasmAwaitRewind } from '../utils/decorators'; let PAGView = PAGView_1 = class PAGView extends NativePAGView { constructor() { super(...arguments); this.pagViewOptions = { firstFrame: true, }; } /** * Create pag view. * @param file pag file. * @param canvas target render canvas. * @param initOptions pag view options * @returns */ static init(file, canvas, initOptions = {}) { return __awaiter(this, void 0, void 0, function* () { const pagPlayer = PAGModule.PAGPlayer.create(); const pagView = new PAGView_1(pagPlayer, canvas, file); pagView.pagViewOptions = Object.assign(Object.assign({}, pagView.pagViewOptions), initOptions); pagView.resetSize(); pagView.renderCanvas = RenderCanvas.from(canvas, { alpha: true }); pagView.renderCanvas.retain(); pagView.pagGlContext = BackendContext.from(pagView.renderCanvas.glContext); pagView.frameRate = file.frameRate(); pagView.pagSurface = this.makePAGSurface(pagView.pagGlContext, canvas.width, canvas.height); pagView.player.setSurface(pagView.pagSurface); pagView.player.setComposition(file); pagView.setProgress(0); if (pagView.pagViewOptions.firstFrame) { yield pagView.flush(); pagView.currentFrame = 0; } return pagView; }); } /** * Update size when changed canvas size. */ updateSize() { var _a; if (!this.pagGlContext) return; this.resetSize(); const pagSurface = PAGView_1.makePAGSurface(this.pagGlContext, this.canvasElement.width, this.canvasElement.height); this.player.setSurface(pagSurface); (_a = this.pagSurface) === null || _a === void 0 ? void 0 : _a.destroy(); this.pagSurface = pagSurface; } flushLoop() { return __awaiter(this, void 0, void 0, function* () { const loop = () => { if (!this.isPlaying) return; this.timer = this.canvasElement.requestAnimationFrame(loop); if (this.flushingNextFrame) return; const now = this.getNowTime(); const duration = this.duration(); this.playTime = now * 1000 - this.startTime; const currentFrame = Math.floor((this.playTime / 1000000) * this.frameRate); const count = Math.floor(this.playTime / duration); if (this.repeatedTimes === count && this.currentFrame === currentFrame) { return; } this.flushNextFrame(); }; loop(); }); } flushNextFrame() { return __awaiter(this, void 0, void 0, function* () { this.flushingNextFrame = true; const duration = this.duration(); const currentFrame = Math.floor((this.playTime / 1000000) * this.frameRate); const count = Math.floor(this.playTime / duration); if (this.repeatCount >= 0 && count > this.repeatCount) { this.clearTimer(); this.player.setProgress(1); yield this.flush(); this.playTime = 0; this.isPlaying = false; this.eventManager.emit("onAnimationEnd" /* PAGViewListenerEvent.onAnimationEnd */, this); this.repeatedTimes = 0; this.flushingNextFrame = false; return true; } if (this.repeatedTimes < count) { this.eventManager.emit("onAnimationRepeat" /* PAGViewListenerEvent.onAnimationRepeat */, this); } this.player.setProgress((this.playTime % duration) / duration); const res = yield this.flush(); this.currentFrame = currentFrame; this.repeatedTimes = count; this.flushingNextFrame = false; return res; }); } getNowTime() { try { return wx.getPerformance().now(); } catch (e) { return Date.now(); } } clearTimer() { if (this.timer) { this.canvasElement.cancelAnimationFrame(this.timer); this.timer = null; } } resetSize() { const dpr = wx.getSystemInfoSync().pixelRatio; this.canvasElement.width = this.canvasElement.width * dpr; this.canvasElement.height = this.canvasElement.height * dpr; } }; PAGView = PAGView_1 = __decorate([ destroyVerify, wasmAwaitRewind ], PAGView); export { PAGView };