Add renderPaused property, closing #42

This commit is contained in:
yushijinhun 2019-12-27 19:04:32 +08:00
parent 1121ee4c09
commit b5593d1943
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
1 changed files with 28 additions and 11 deletions

View File

@ -40,6 +40,8 @@ export class SkinViewer {
public readonly playerObject: PlayerObject; public readonly playerObject: PlayerObject;
private _renderPaused: boolean = false;
constructor(options: SkinViewerOptions) { constructor(options: SkinViewerOptions) {
this.domElement = options.domElement; this.domElement = options.domElement;
this.animation = options.animation || null; this.animation = options.animation || null;
@ -114,18 +116,21 @@ export class SkinViewer {
if (options.width) this.width = options.width; if (options.width) this.width = options.width;
if (options.height) this.height = options.height; if (options.height) this.height = options.height;
const draw = () => { window.requestAnimationFrame(() => this.draw());
if (this.disposed) return; }
window.requestAnimationFrame(draw);
if (!this.animationPaused) { private draw() {
this.animationTime++; if (this.disposed || this._renderPaused) {
if (this.animation) { return;
invokeAnimation(this.animation, this.playerObject, this.animationTime / 100.0); }
} if (!this.animationPaused) {
this.animationTime++;
if (this.animation) {
invokeAnimation(this.animation, this.playerObject, this.animationTime / 100.0);
} }
this.renderer.render(this.scene, this.camera); }
}; this.renderer.render(this.scene, this.camera);
draw(); window.requestAnimationFrame(() => this.draw());
} }
setSize(width, height) { setSize(width, height) {
@ -142,6 +147,18 @@ export class SkinViewer {
this.capeTexture.dispose(); 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() { get skinUrl() {
return this.skinImg.src; return this.skinImg.src;
} }