Last active
December 17, 2015 20:39
-
-
Save gurasa/5668908 to your computer and use it in GitHub Desktop.
色んなものに使えるループ
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
mainLoop = | |
initialize: -> | |
@currentTime = @getTime | |
@lastTime = @currentTime | |
vendors = ["ms", "moz", "webkit", "o"] | |
x = 0 | |
while x < vendors.length and not window.requestAnimationFrame | |
window.requestAnimationFrame = window[vendors[x] + "RequestAnimationFrame"] | |
window.cancelAnimationFrame = window[vendors[x] + "CancelAnimationFrame"] or window[vendors[x] + "CancelRequestAnimationFrame"] | |
++x | |
lastTime = 0 | |
unless window.requestAnimationFrame | |
window.requestAnimationFrame = (callback, element) -> | |
currTime = @getTime() | |
timeToCall = Math.max(0, 16 - (currTime - lastTime)) | |
id = window.setTimeout(-> | |
callback currTime + timeToCall | |
, timeToCall) | |
lastTime = currTime + timeToCall | |
id | |
unless window.cancelAnimationFrame | |
window.cancelAnimationFrame = (id) -> | |
clearTimeout id | |
getTime: -> | |
new Date().getTime() | |
tick: -> | |
@currentTime = @getTime | |
@elapseTime = @currentTime - @lastTime | |
@update @elapseTime | |
@lastTime = @currentTime | |
requestAnimationFrame mainLoop.tick | |
update: (@elapseTime)-> | |
# ループさせる処理 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment