Skip to content

Instantly share code, notes, and snippets.

@floz
Created August 8, 2013 11:07
Show Gist options
  • Save floz/6183741 to your computer and use it in GitHub Desktop.
Save floz/6183741 to your computer and use it in GitHub Desktop.
CoffeeScript - RequestAnimationFrame
do ->
w = window
for vendor in ['ms', 'moz', 'webkit', 'o']
break if w.requestAnimationFrame
w.requestAnimationFrame = w["#{vendor}RequestAnimationFrame"]
w.cancelAnimationFrame = (w["#{vendor}CancelAnimationFrame"] or
w["#{vendor}CancelRequestAnimationFrame"])
# deal with the case where rAF is built in but cAF is not.
if w.requestAnimationFrame
return if w.cancelAnimationFrame
browserRaf = w.requestAnimationFrame
canceled = {}
w.requestAnimationFrame = (callback) ->
id = browserRaf (time) ->
if id of canceled then delete canceled[id]
else callback time
w.cancelAnimationFrame = (id) -> canceled[id] = true
# handle legacy browsers which don’t implement rAF
else
targetTime = 0
w.requestAnimationFrame = (callback) ->
targetTime = Math.max targetTime + 16, currentTime = +new Date
w.setTimeout (-> callback +new Date), targetTime - currentTime
w.cancelAnimationFrame = (id) -> clearTimeout id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment