2015-04-07 20:50:066 ♡
Works quite well here.
window.requestAnimationFrame() can play more nicely with others (e.g. maybe stopping when the window doesn't have focus, delaying when the CPU is overloaded and maybe other niceties that vary by browser) than setTimeout on modern browsers, at the cost of having to track time yourself.
function init() {
...
for( ...) {
var frameState = {idx: i, start: null, nextFrame: null}
window.requestAnimationFrame(processOverlay.bind(frameState))
}
}
function processOverlay(timestamp) {
var idx = this.idx;
if (!this.start) { this.start = timestamp; this.nextFrame = timestamp }
if (this.nextFrame > timestamp)
{
// Nothing to do this time.
window.requestAnimationFrame(processOverlay.bind(this));
return
}
...
if (done)
{
this.nextFrame = tempFrameTime + timestamp
window.requestAnimationFrame(processOverlay.bind(this));
return
}
...
}
window.requestAnimationFrame() can play more nicely with others (e.g. maybe stopping when the window doesn't have focus, delaying when the CPU is overloaded and maybe other niceties that vary by browser) than setTimeout on modern browsers, at the cost of having to track time yourself.
function init() {
...
for( ...) {
var frameState = {idx: i, start: null, nextFrame: null}
window.requestAnimationFrame(processOverlay.bind(frameState))
}
}
function processOverlay(timestamp) {
var idx = this.idx;
if (!this.start) { this.start = timestamp; this.nextFrame = timestamp }
if (this.nextFrame > timestamp)
{
// Nothing to do this time.
window.requestAnimationFrame(processOverlay.bind(this));
return
}
...
if (done)
{
this.nextFrame = tempFrameTime + timestamp
window.requestAnimationFrame(processOverlay.bind(this));
return
}
...
}
