Last active
March 14, 2017 21:56
-
-
Save cezary/332b9739a94abff7244a33cea5601fba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const animationFrame = () => new Promise(window.requestAnimationFrame); | |
const load = (el=window) => new Promise(resolve => el.onload = resolve); | |
const timeout = (time) => new Promise(resolve => setTimeout(resolve, time)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://coderwall.com/p/iygcpa/gameloop-the-correct-way | |
const animationFrame = () => new Promise(requestAnimationFrame); | |
const loop = async (draw=function(){}) => { | |
let lastTimestamp = 0; | |
let playing = true; | |
setTimeout(() => playing = false, 20); | |
while (playing) { | |
let timestamp = await animationFrame(); | |
let delta = lastTimestamp ? timestamp - lastTimestamp : 0; | |
draw({ delta, timestamp }); | |
lastTimestamp = timestamp; | |
} | |
} | |
let count = 0; | |
loop(({ delta, timestamp }) => { | |
console.log('delta', delta); | |
console.log('ts', timestamp); | |
console.log('count', count++); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment