Forked from greypants/timeBasedAnimationPattern.js
Created
December 18, 2017 15:47
-
-
Save nikitalarionov/717e5c33ec295076f28c607829a0a577 to your computer and use it in GitHub Desktop.
JS: Time-based animation pattern
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
// Full Blog Post: http://viget.com/extend/time-based-animation | |
// requestAnimationFrame() polyfill: https://gist.github.com/1579671 | |
window.APP = window.APP || {}; | |
APP.pause = function() { | |
window.cancelAnimationFrame(APP.core.animationFrame); | |
}; | |
APP.play = function() { | |
APP.core.then = Date.now(); | |
APP.core.frame(); | |
}; | |
APP.core = { | |
frame: function() { | |
APP.core.setDelta(); | |
APP.core.update(); | |
APP.core.render(); | |
APP.core.animationFrame = window.requestAnimationFrame(APP.core.frame); | |
}, | |
setDelta: function() { | |
APP.core.now = Date.now(); | |
APP.core.delta = (APP.core.now - APP.core.then) / 1000; // seconds since last frame | |
APP.core.then = APP.core.now; | |
}, | |
update: function() { | |
// Render updates to browser (draw to canvas, update css, etc.) | |
}, | |
render: function() { | |
// Update values | |
// var distance = 100 * APP._delta; | |
// APP.thing.x += 100 * APP._delta; | |
} | |
}; | |
APP.play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jsfiddle.net/greypants/msuYF/ - See fps counter logic