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,9 +116,13 @@ 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);
private draw() {
if (this.disposed || this._renderPaused) {
return;
}
if (!this.animationPaused) { if (!this.animationPaused) {
this.animationTime++; this.animationTime++;
if (this.animation) { if (this.animation) {
@ -124,8 +130,7 @@ export class SkinViewer {
} }
} }
this.renderer.render(this.scene, this.camera); this.renderer.render(this.scene, this.camera);
}; window.requestAnimationFrame(() => this.draw());
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;
} }