Skip to content

Instantly share code, notes, and snippets.

@greypants
Created October 29, 2012 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greypants/3974515 to your computer and use it in GitHub Desktop.
Save greypants/3974515 to your computer and use it in GitHub Desktop.
JS: Time-based animation-pattern 2
var frames = {
init: function() {
$(window).on({
'blur': frames.pause,
'focus': frames.play
});
frames.play();
},
loop: function() {
frames.setDelta();
updateBackgrounds();
frames.animationFrame = window.requestAnimationFrame(frames.loop);
},
setDelta: function() {
frames.now = Date.now();
frames.delta = (frames.now - frames.then) / 1000; // seconds since last frame
frames.then = frames.now;
},
pause: function() {
window.cancelAnimationFrame(frames.animationFrame);
frames.areRunning = false;
},
play: function() {
if(!frames.areRunning) {
frames.then = Date.now();
frames.loop();
frames.areRunning = true;
}
}
};
var timer = 0;
var updateBackgrounds = function() {
this.timer += frames.delta;
if (this.timer > 0.1) {
// Update background positions!
this.timer = 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment