Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jcemer
Created June 7, 2012 14: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 jcemer/2889003 to your computer and use it in GitHub Desktop.
Save jcemer/2889003 to your computer and use it in GitHub Desktop.
Coffeescript #troll
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])) {}
}
})();
})();
@ricardobeat
Copy link

O que eu acabei usando:

window.requestAnimationFrame ?= 
    window.webkitRequestAnimationFrame or
    window.mozRequestAnimationFrame or
    window.msRequestAnimationFrame or
    window.oRequestAnimationFrame or
    (fn, el) -> setTimeout fn, 1000/60

Resultado:

if (window.requestAnimationFrame == null) {
  window.requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame || function(fn, el) {
    return setTimeout(fn, 1000 / 60);
  };
}

:trollface:

@ricardobeat
Copy link

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);
});

@jcemer
Copy link
Author

jcemer commented Jun 12, 2012

Neste teu exemplo o i precisa começar de -1

@ricardobeat
Copy link

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