Created
October 29, 2012 16:09
-
-
Save greypants/3974515 to your computer and use it in GitHub Desktop.
JS: Time-based animation-pattern 2
This file contains 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
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