The render and onFrame functions look exactly the same as in the previous examples:
function onFrame() {
elapsedTime = (new Date).getTime() - initialTime;
if (elapsedTime < animationRate) return;
let steps = Math.floor(elapsedTime / animationRate);
while (steps > 0) {
animate();
steps -= 1;
}
initialTime = (new Date).getTime();
}
function render() {
initialTime = (new Date).getTime();
setInterval(onFrame, animationRate / 1000);
}