Skip to content

Instantly share code, notes, and snippets.

@premasagar
Forked from louisremi/animLoopX.js
Created September 24, 2011 00:47

Revisions

  1. Louis-Rémi Babé revised this gist Aug 2, 2011. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ window.animLoop = function( render, element ) {
    // - setTimeout returns the actual timeout
    now = now && now > 1E4 ? now : +new Date;
    var deltaT = now - lastFrame;
    // skip a frame when deltaT is too high
    // do not render frame when deltaT is too high
    if ( deltaT < 160 ) {
    running = render( deltaT, lastFrame = now );
    }
    2 changes: 1 addition & 1 deletion animLoop_fixed.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ function animLoop( render, element ) {
    if ( running !== false ) {
    requestAnimationFrame( loop, element );
    var deltaT = now - lastFrame;
    // skip a frame when deltaT is too high
    // do not render frame when deltaT is too high
    if ( deltaT < 160 ) {
    running = render( deltaT );
    }
  2. Louis-Rémi Babé revised this gist Aug 2, 2011. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions intervalLoop_constant.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    var elem = document.getElementById("animatedElem"),
    left = 0,
    lastFrame = +new Date,
    timer;
    // Move the element on the right at ~600px/s
    timer = setInterval(function() {
    var now = +new Date,
    deltaT = now - lastFrame;
    elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
    lastFrame = now;
    // clear the timer at 400px to stop the animation
    if ( left > 400 ) {
    clearInterval( timer );
    }
    }, 16);
  3. Louis-Rémi Babé revised this gist Aug 2, 2011. No changes.
  4. Louis-Rémi Babé revised this gist Aug 2, 2011. 3 changed files with 21 additions and 35 deletions.
    20 changes: 0 additions & 20 deletions animLoop.js
    Original file line number Diff line number Diff line change
    @@ -1,20 +0,0 @@
    function animLoop( render, element ) {
    var running;
    function loop() {
    // stop if previous render returned false
    if ( running !== false ) {
    requestAnimationFrame( loop, element );
    running = render();
    }
    }
    loop();
    }

    // Usage
    animLoop(function() {
    elem.style.left = ( left += 10 ) + "px";
    if ( left > 400 ) {
    return false;
    }
    // optional 2nd arg: elem containing the animation
    }, animWrapper );
    15 changes: 0 additions & 15 deletions animLoop_constant.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +0,0 @@
    var elem = document.getElementById("animatedElem"),
    left = 0,
    lastFrame = +new Date,
    timer;
    // Move the element on the right at ~600px/s
    timer = setInterval(function() {
    var now = +new Date,
    deltaT = now - lastFrame;
    elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
    lastFrame = now;
    // clear the timer at 400px to stop the animation
    if ( left > 400 ) {
    clearInterval( timer );
    }
    }, 16);
    21 changes: 21 additions & 0 deletions animLoop_raf.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function animLoop( render, element ) {
    var running, lastFrame = +new Date;
    function loop( now ) {
    // stop the loop if render returned false
    if ( running !== false ) {
    requestAnimationFrame( loop, element );
    running = render( now - lastFrame );
    lastFrame = now;
    }
    }
    loop( lastFrame );
    }

    // Usage
    animLoop(function( deltaT ) {
    elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
    if ( left > 400 ) {
    return false;
    }
    // optional 2nd arg: elem containing the animation
    }, animWrapper );
  5. Louis-Rémi Babé revised this gist Aug 2, 2011. 1 changed file with 11 additions and 16 deletions.
    27 changes: 11 additions & 16 deletions animLoop_constant.js
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,15 @@
    function animLoop( render, element ) {
    var running, lastFrame = +new Date;
    function loop( now ) {
    // stop the loop if render returned false
    if ( running !== false ) {
    requestAnimationFrame( loop, element );
    running = render( now - lastFrame );
    lastFrame = now;
    }
    }
    loop( lastFrame );
    }

    var elem = document.getElementById("animatedElem"),
    left = 0,
    lastFrame = +new Date,
    timer;
    // Move the element on the right at ~600px/s
    animLoop(function( deltaT ) {
    timer = setInterval(function() {
    var now = +new Date,
    deltaT = now - lastFrame;
    elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
    lastFrame = now;
    // clear the timer at 400px to stop the animation
    if ( left > 400 ) {
    return false;
    clearInterval( timer );
    }
    });
    }, 16);
  6. Louis-Rémi Babé revised this gist Aug 2, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // Cross browser, backward compatible solution
    (function() {
    (function( window, Date ) {
    // feature testing
    var raf = window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    @@ -27,7 +27,7 @@ window.animLoop = function( render, element ) {
    }
    loop();
    };
    })();
    })( window, Date );

    // Usage
    animLoop(function( deltaT, now ) {
  7. Louis-Rémi Babé revised this gist Aug 2, 2011. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ window.animLoop = function( render, element ) {
    now = now && now > 1E4 ? now : +new Date;
    var deltaT = now - lastFrame;
    // skip a frame when deltaT is too high
    if ( deltaT > 160 ) {
    if ( deltaT < 160 ) {
    running = render( deltaT, lastFrame = now );
    }
    }
    2 changes: 1 addition & 1 deletion animLoop_fixed.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ function animLoop( render, element ) {
    requestAnimationFrame( loop, element );
    var deltaT = now - lastFrame;
    // skip a frame when deltaT is too high
    if ( deltaT > 160 ) {
    if ( deltaT < 160 ) {
    running = render( deltaT );
    }
    lastFrame = now;
  8. Louis-Rémi Babé revised this gist Aug 2, 2011. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,11 @@ window.animLoop = function( render, element ) {
    // - Chrome 10 doesn't return it at all
    // - setTimeout returns the actual timeout
    now = now && now > 1E4 ? now : +new Date;
    running = render( now - lastFrame, lastFrame = now );
    var deltaT = now - lastFrame;
    // skip a frame when deltaT is too high
    if ( deltaT > 160 ) {
    running = render( deltaT, lastFrame = now );
    }
    }
    }
    loop();
  9. Louis-Rémi Babé revised this gist Aug 1, 2011. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions animLoop_fixed.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    function animLoop( render, element ) {
    var running, lastFrame = +new Date;
    function loop( now ) {
    // stop the loop if render returned false
    if ( running !== false ) {
    requestAnimationFrame( loop, element );
    var deltaT = now - lastFrame;
    // skip a frame when deltaT is too high
    if ( deltaT > 160 ) {
    running = render( deltaT );
    }
    lastFrame = now;
    }
    }
    loop( lastFrame );
    }
  10. Louis-Rémi Babé revised this gist Aug 1, 2011. 2 changed files with 14 additions and 11 deletions.
    20 changes: 13 additions & 7 deletions animLoop.js
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,20 @@
    function animLoop( render, element ) {
    function loop( now ) {
    requestAnimationFrame( loop, element );
    render( now );
    var running;
    function loop() {
    // stop if previous render returned false
    if ( running !== false ) {
    requestAnimationFrame( loop, element );
    running = render();
    }
    }
    loop( +new Date );
    loop();
    }

    // Usage
    animLoop(function( now ) {
    // rendering code goes here
    ...
    animLoop(function() {
    elem.style.left = ( left += 10 ) + "px";
    if ( left > 400 ) {
    return false;
    }
    // optional 2nd arg: elem containing the animation
    }, animWrapper );
    5 changes: 1 addition & 4 deletions animLoopConstant.js → animLoop_constant.js
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,9 @@ function animLoop( render, element ) {
    lastFrame = now;
    }
    }
    loop( +new Date );
    loop( lastFrame );
    }

    // Updated example
    var elem = document.getElementById("animatedElem"),
    left = 0;
    // Move the element on the right at ~600px/s
    animLoop(function( deltaT ) {
    elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
  11. Louis-Rémi Babé revised this gist Aug 1, 2011. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions animLoopConstant.js
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,9 @@ function animLoop( render, element ) {
    loop( +new Date );
    }

    // Example
    // Updated example
    var elem = document.getElementById("animatedElem"),
    left = 0,
    timer;
    left = 0;
    // Move the element on the right at ~600px/s
    animLoop(function( deltaT ) {
    elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
  12. Louis-Rémi Babé revised this gist Aug 1, 2011. 2 changed files with 19 additions and 8 deletions.
    2 changes: 1 addition & 1 deletion animLoop.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function animLoop( render, element ){
    function animLoop( render, element ) {
    function loop( now ) {
    requestAnimationFrame( loop, element );
    render( now );
    25 changes: 18 additions & 7 deletions animLoopConstant.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,24 @@
    function animLoop( render, element ){
    var lastFrame = +new Date;
    function animLoop( render, element ) {
    var running, lastFrame = +new Date;
    function loop( now ) {
    requestAnimationFrame( loop, element );
    render( now - lastFrame );
    // stop the loop if render returned false
    if ( running !== false ) {
    requestAnimationFrame( loop, element );
    running = render( now - lastFrame );
    lastFrame = now;
    }
    }
    loop( +new Date );
    }

    // Usage
    // Example
    var elem = document.getElementById("animatedElem"),
    left = 0,
    timer;
    // Move the element on the right at ~600px/s
    animLoop(function( deltaT ) {
    ...
    }, animWrapper );
    elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
    if ( left > 400 ) {
    return false;
    }
    });
  13. Louis-Rémi Babé revised this gist Aug 1, 2011. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -14,18 +14,19 @@ window.animLoop = function( render, element ) {
    raf( loop, element ) :
    // fallback to setTimeout
    setTimeout( loop, 16 );
    // Make sure to always return a valid time, since:
    // Make sure to use a valid time, since:
    // - Chrome 10 doesn't return it at all
    // - setTimeout returns the actual timeout
    running = render( now = ( now && now > 1E4 ? now : +new Date ), now - lastFrame );
    now = now && now > 1E4 ? now : +new Date;
    running = render( now - lastFrame, lastFrame = now );
    }
    }
    loop();
    };
    })();

    // Usage
    animLoop(function( now ) {
    animLoop(function( deltaT, now ) {
    // rendering code goes here
    // return false; will stop the loop
    ...
  14. Louis-Rémi Babé revised this gist Aug 1, 2011. 4 changed files with 18 additions and 7 deletions.
    6 changes: 3 additions & 3 deletions animLoop.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    function animLoop( render, element ){
    function loop( now ) {
    requestAnimationFrame( loop, element );
    render( now || +new Date );
    render( now );
    }
    loop();
    loop( +new Date );
    }

    // Usage
    animLoop(function() {
    animLoop(function( now ) {
    // rendering code goes here
    ...
    // optional 2nd arg: elem containing the animation
    13 changes: 13 additions & 0 deletions animLoopConstant.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    function animLoop( render, element ){
    var lastFrame = +new Date;
    function loop( now ) {
    requestAnimationFrame( loop, element );
    render( now - lastFrame );
    }
    loop( +new Date );
    }

    // Usage
    animLoop(function( deltaT ) {
    ...
    }, animWrapper );
    4 changes: 2 additions & 2 deletions animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ var raf = window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame;

    window.animLoop = function( render, element ) {
    var running;
    var running, lastFrame = +new Date;
    function loop( now ) {
    if ( running !== false ) {
    raf ?
    @@ -17,7 +17,7 @@ window.animLoop = function( render, element ) {
    // Make sure to always return a valid time, since:
    // - Chrome 10 doesn't return it at all
    // - setTimeout returns the actual timeout
    running = render( now && now > 1E4 ? now : +new Date );
    running = render( now = ( now && now > 1E4 ? now : +new Date ), now - lastFrame );
    }
    }
    loop();
    2 changes: 0 additions & 2 deletions animLoopX.min.js
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    // minified to 208B, to see if it was tweetable, but :-(
    (function(b,a){a="RequestAnimationFrame";a=b["moz"+a]||b["webkit"+a]||b["ms"+a]||b["o"+a];b.animLoop=function(b,f,e){function c(d){e!==!1&&(a?a(c,f):setTimeout(c,16),e=b(d&&d>1E4?d:+new Date))}c()}})(window);
  15. Louis-Rémi Babé revised this gist Aug 1, 2011. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -14,13 +14,13 @@ window.animLoop = function( render, element ) {
    raf( loop, element ) :
    // fallback to setTimeout
    setTimeout( loop, 16 );
    // Make sure to always return a valid time, since:
    // - Chrome 10 doesn't return it at all
    // - setTimeout returns the actual timeout
    running = render( now && now > 1E4 ? now : +new Date );
    }
    // Make sure to always return a valid time, since:
    // - Chrome 10 doesn't return it at all
    // - setTimeout returns the actual timeout
    running = render( now && now > 1E4 ? now : +new Date );
    }
    loop();
    }
    loop();
    };
    })();

  16. Louis-Rémi Babé revised this gist Aug 1, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions animLoopX.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    // minified to 208B, to see if it was tweetable, but :-(
    (function(b,a){a="RequestAnimationFrame";a=b["moz"+a]||b["webkit"+a]||b["ms"+a]||b["o"+a];b.animLoop=function(b,f,e){function c(d){e!==!1&&(a?a(c,f):setTimeout(c,16),e=b(d&&d>1E4?d:+new Date))}c()}})(window);
  17. Louis-Rémi Babé revised this gist Aug 1, 2011. 1 changed file with 33 additions and 0 deletions.
    33 changes: 33 additions & 0 deletions animLoopX.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // Cross browser, backward compatible solution
    (function() {
    // feature testing
    var raf = window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame;

    window.animLoop = function( render, element ) {
    var running;
    function loop( now ) {
    if ( running !== false ) {
    raf ?
    raf( loop, element ) :
    // fallback to setTimeout
    setTimeout( loop, 16 );
    // Make sure to always return a valid time, since:
    // - Chrome 10 doesn't return it at all
    // - setTimeout returns the actual timeout
    running = render( now && now > 1E4 ? now : +new Date );
    }
    }
    loop();
    };
    })();

    // Usage
    animLoop(function( now ) {
    // rendering code goes here
    // return false; will stop the loop
    ...
    // optional 2nd arg: elem containing the animation
    }, animWrapper );
  18. Louis-Rémi Babé revised this gist Jul 29, 2011. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion animLoop.js
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,6 @@ function animLoop( render, element ){
    // Usage
    animLoop(function() {
    // rendering code goes here
    }, document.getElementById("animationWrapper") );
    ...
    // optional 2nd arg: elem containing the animation
    }, animWrapper );
  19. Louis-Rémi Babé revised this gist Jul 29, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion animLoop.js
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,4 @@ function animLoop( render, element ){
    // Usage
    animLoop(function() {
    // rendering code goes here
    });
    }, document.getElementById("animationWrapper") );
  20. Louis-Rémi Babé created this gist Jul 29, 2011.
    12 changes: 12 additions & 0 deletions animLoop.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    function animLoop( render, element ){
    function loop( now ) {
    requestAnimationFrame( loop, element );
    render( now || +new Date );
    }
    loop();
    }

    // Usage
    animLoop(function() {
    // rendering code goes here
    });