import { GameObjectBaseType, GameObjectData, GameObjectBase, GameResource, GameObject, } from '@gedit/render-engine'; import { PixiGameScene } from '../../common'; import { setGameObjectBaseData, getVisible } from '../utils'; export interface LottieSchema { loop?: boolean; autoplay?: boolean; speed?: number; direction?: boolean; // 1 | -1 } export interface LottieGameObjectData extends GameObjectData { [GameObjectBaseType.LOTTIE]: LottieSchema; } export class LottieGameObject extends GameObjectBase< LottieGameObjectData, PixiGameScene > { static type = GameObjectBaseType.LOTTIE; static needResource = true; rendererObject?: PIXI.Sprite; lottie?: Lottie.AnimationItem; animationData: LottieData; lottieCanvas: HTMLCanvasElement = document.createElement('canvas'); async create(resource: GameResource, children: GameObject[]): Promise { this.animationData = this.scene.getTexture(resource.resourceKey); this.lottieCanvas.width = this.animationData.w; this.lottieCanvas.height = this.animationData.h; this.rendererObject = new PIXI.Sprite(PIXI.Texture.from(this.lottieCanvas)); this.scene.container.addChild?.(this.rendererObject); this.createLottie(); super.create.apply(this, arguments); } createLottie( data: LottieGameObjectData = this.data, animationData: LottieData = this.animationData ): void { this.lottie = window.bodymovin.loadAnimation<'canvas'>({ animationData, renderer: 'canvas', // container: this.lottieCanvas, loop: data[GameObjectBaseType.LOTTIE].loop, autoplay: data[GameObjectBaseType.LOTTIE].autoplay, rendererSettings: { context: this.lottieCanvas.getContext('2d')!, clearCanvas: true, }, }); if (typeof data[GameObjectBaseType.LOTTIE].speed === 'number') { this.lottie?.setSpeed(data[GameObjectBaseType.LOTTIE].speed); } if (data[GameObjectBaseType.LOTTIE].direction) { this.lottie?.setDirection( data[GameObjectBaseType.LOTTIE].direction ? 1 : -1 ); } this.lottie?.addEventListener('enterFrame', () => { this.rendererObject?.texture?.source?.update(); }); } dispose(): void { this.lottie?.destroy(); this.rendererObject?.destroy(); super.dispose(); } update(data: LottieGameObjectData): void { if (!this.rendererObject || !this.lottie) return; const { loop, autoplay, speed, direction } = data[ GameObjectBaseType.LOTTIE ]; if (this.data[GameObjectBaseType.LOTTIE].loop !== loop) { this.lottie.loop = !!loop; this.lottie.goToAndPlay(0); } if (this.data[GameObjectBaseType.LOTTIE].autoplay !== autoplay) { this.lottie.autoplay = !!autoplay; if (autoplay) { this.lottie.goToAndPlay(0); } else { this.lottie.goToAndStop(0); } } if ( this.data[GameObjectBaseType.LOTTIE].speed !== speed && typeof speed === 'number' ) { this.lottie.setSpeed(speed); } if (this.data[GameObjectBaseType.LOTTIE].direction !== direction) { this.lottie.setDirection(direction ? 1 : -1); } setGameObjectBaseData(this, data, true); this.rendererObject.visible = getVisible(data, this); super.update(data); } getDisplaySize(): { width: number; height: number } { return { width: this.animationData.w || 0, height: this.animationData.h || 0, }; } getRendererObject(): PIXI.Sprite | undefined { return this.rendererObject; } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } }