1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-27 17:50:37 +00:00

rewrote AnimationTimer loop, use setTimeout()

This commit is contained in:
Steven Hugg 2021-08-06 21:20:46 -05:00
parent 5eb3e864fb
commit 0bb450944c

View File

@ -233,14 +233,14 @@ export class EmuHalt extends Error {
} }
} }
export var useRequestAnimationFrame : boolean = true; export var useRequestAnimationFrame : boolean = false;
export class AnimationTimer { export class AnimationTimer {
callback; callback;
running : boolean = false; running : boolean = false;
pulsing : boolean = false; pulsing : boolean = false;
lastts = 0; nextts = 0;
nframes; nframes;
startts; // for FPS calc startts; // for FPS calc
frameRate; frameRate;
@ -256,7 +256,7 @@ export class AnimationTimer {
scheduleFrame(msec:number) { scheduleFrame(msec:number) {
var fn = (timestamp) => { var fn = (timestamp) => {
try { try {
this.nextFrame(timestamp); this.nextFrame(this.useReqAnimFrame ? timestamp : Date.now());
} catch (e) { } catch (e) {
this.running = false; this.running = false;
this.pulsing = false; this.pulsing = false;
@ -269,14 +269,8 @@ export class AnimationTimer {
setTimeout(fn, msec); setTimeout(fn, msec);
} }
nextFrame(ts?:number) { nextFrame(ts:number) {
if (!ts) ts = Date.now(); if (ts > this.nextts) {
if (ts - this.lastts < this.intervalMsec*5) {
this.lastts += this.intervalMsec;
} else {
this.lastts = ts + this.intervalMsec; // frames skipped, catch up
}
if (!this.useReqAnimFrame || (this.lastts - ts) > this.intervalMsec/2) {
if (this.running) { if (this.running) {
this.callback(); this.callback();
} }
@ -286,8 +280,14 @@ export class AnimationTimer {
console.log("Avg framerate: " + this.nframes*1000/(ts-this.startts) + " fps"); console.log("Avg framerate: " + this.nframes*1000/(ts-this.startts) + " fps");
} }
} }
this.nextts += this.intervalMsec;
// frames skipped? catch up
if ((ts - this.nextts) > 1000) {
//console.log(ts - this.nextts, 'msec skipped');
this.nextts = ts;
}
if (this.running) { if (this.running) {
this.scheduleFrame(this.lastts - ts); this.scheduleFrame(this.nextts - ts);
} else { } else {
this.pulsing = false; this.pulsing = false;
} }
@ -298,7 +298,7 @@ export class AnimationTimer {
start() { start() {
if (!this.running) { if (!this.running) {
this.running = true; this.running = true;
this.lastts = 0; this.nextts = 0;
this.nframes = 0; this.nframes = 0;
if (!this.pulsing) { if (!this.pulsing) {
this.scheduleFrame(0); this.scheduleFrame(0);