Created
June 7, 2012 14:09
-
-
Save jcemer/2889003 to your computer and use it in GitHub Desktop.
Coffeescript #troll
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
raf = 'RequestAnimationFrame' | |
window['r' + raf[1..]] ?= do -> | |
return R for pre in ['webkit', 'moz', 'ms', 'o'] when R = window[pre + raf] | |
# =.=.=.=.=.=.=.=.=.=.=.=.=.= | |
raf = 'RequestAnimationFrame'; | |
if (window[_name = 'r' + raf.slice(1)] == null) { | |
window[_name] = (function() { | |
var R, pre, _i, _len, _ref; | |
_ref = ['webkit', 'moz', 'ms', 'o']; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
pre = _ref[_i]; | |
if (R = window[pre + raf]) return R; | |
} | |
})(); | |
} | |
do -> | |
raf = 'RequestAnimationFrame' | |
window['r' + raf[1..]] ?= do -> | |
return R for pre in ['webkit', 'moz', 'ms', 'o'] when R = window[pre + raf] | |
# =.=.=.=.=.=.=.=.=.=.=.=.=.= | |
(function() { | |
var raf, _name, _ref; | |
raf = 'RequestAnimationFrame'; | |
return (_ref = window[_name = 'r' + raf.slice(1)]) != null ? _ref : window[_name] = (function() { | |
var R, pre, _i, _len, _ref2; | |
_ref2 = ['webkit', 'moz', 'ms', 'o']; | |
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | |
pre = _ref2[_i]; | |
if (!(R = window[pre + raf])) {} | |
} | |
})(); | |
})(); |
O negócio é evitar for
pra ter código mais limpo:
vendors = ['webkit', 'moz', 'ms', 'o']
i = 0
while !window.requestAnimationFrame && i < vendors.length
window.requestAnimationFrame = window[vendors[i++]+'RequestAnimationFrame']
window.requestAnimationFrame or= (cb) -> setTimeout cb, 1000/60
Compila lindamente:
var i, vendors;
vendors = ['webkit', 'moz', 'ms', 'o'];
i = 0;
while (!window.requestAnimationFrame && i++ < vendors.length) {
window.requestAnimationFrame = window[vendors[i] + 'RequestAnimationFrame'];
}
window.requestAnimationFrame || (window.requestAnimationFrame = function(cb) {
return setTimeout(cb, 1000 / 60);
});
Neste teu exemplo o i
precisa começar de -1
Ooops. Já ta na grifo a essa hora?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
O que eu acabei usando:
Resultado: