From b5593d19437c4c83fb6a60d68e302cd6115332c6 Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Fri, 27 Dec 2019 19:04:32 +0800 Subject: [PATCH] Add renderPaused property, closing #42 --- src/viewer.ts | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/viewer.ts b/src/viewer.ts index 3da650c..12575f2 100644 --- a/src/viewer.ts +++ b/src/viewer.ts @@ -40,6 +40,8 @@ export class SkinViewer { public readonly playerObject: PlayerObject; + private _renderPaused: boolean = false; + constructor(options: SkinViewerOptions) { this.domElement = options.domElement; this.animation = options.animation || null; @@ -114,18 +116,21 @@ export class SkinViewer { if (options.width) this.width = options.width; if (options.height) this.height = options.height; - const draw = () => { - if (this.disposed) return; - window.requestAnimationFrame(draw); - if (!this.animationPaused) { - this.animationTime++; - if (this.animation) { - invokeAnimation(this.animation, this.playerObject, this.animationTime / 100.0); - } + window.requestAnimationFrame(() => this.draw()); + } + + private draw() { + if (this.disposed || this._renderPaused) { + return; + } + if (!this.animationPaused) { + this.animationTime++; + if (this.animation) { + invokeAnimation(this.animation, this.playerObject, this.animationTime / 100.0); } - this.renderer.render(this.scene, this.camera); - }; - draw(); + } + this.renderer.render(this.scene, this.camera); + window.requestAnimationFrame(() => this.draw()); } setSize(width, height) { @@ -142,6 +147,18 @@ export class SkinViewer { this.capeTexture.dispose(); } + get renderPaused() { + return this._renderPaused; + } + + set renderPaused(value: boolean) { + const toResume = !this.disposed && !value && this._renderPaused; + this._renderPaused = value; + if (toResume) { + window.requestAnimationFrame(() => this.draw()); + } + } + get skinUrl() { return this.skinImg.src; }