Skip to content

Instantly share code, notes, and snippets.

@jeongsd
Forked from addyosmani/limitLoop.js
Created March 5, 2017 13:59
Show Gist options
  • Save jeongsd/64f68e9779f231ec32b04d24f080d24b to your computer and use it in GitHub Desktop.
Save jeongsd/64f68e9779f231ec32b04d24f080d24b to your computer and use it in GitHub Desktop.

Revisions

  1. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -59,8 +59,8 @@ var limitLoop = function (fn, fps) {
    // using then = now, which can end up lowering overall fps
    then = now - (delta % interval);

    // call the fn, passing current fps to it
    fn(frames);
    // call the fn
    fn();
    }
    }(0));
    };
  2. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    Feel free to play with this over at http://jsfiddle.net/addyo/Y8P6S/.
    Feel free to play with this over at http://jsfiddle.net/addyo/Y8P6S/1/.
    You can either use the Chrome DevTools Timeline or FPS counter to confirm
    if you're hitting a consistent fps.
    */
  3. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 7 additions and 12 deletions.
    19 changes: 7 additions & 12 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -41,29 +41,24 @@ var limitLoop = function (fn, fps) {
    // Use var then = Date.now(); if you
    // don't care about targetting < IE9
    var then = new Date().getTime();
    var oldtime = 0;


    // custom fps, otherwise fallback to 60
    fps = fps || 60;

    return (function loop(time) {
    var interval = 1000 / fps;

    return (function loop(time){
    requestAnimationFrame(loop);

    var interval = 1000 / (this.fps || fps);

    // again, Date.now() if it's available
    var now = new Date().getTime();
    var delta = now - then;

    if (delta > interval) {
    // Update time
    // now - (delta % interval) is an improvement over just
    // using then = now, which can end up lowering overall fps
    then = now - (delta % interval);

    // calculate fps
    var frames = 1000 / (time - oldtime)
    oldtime = time;


    // call the fn, passing current fps to it
    fn(frames);
    }
  4. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,9 @@ http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/
    */

    var limitLoop = function (fn, fps) {


    // Use var then = Date.now(); if you
    // don't care about targetting < IE9
    var then = new Date().getTime();
    var oldtime = 0;

    @@ -48,6 +50,7 @@ var limitLoop = function (fn, fps) {
    requestAnimationFrame(loop);

    var interval = 1000 / (this.fps || fps);
    // again, Date.now() if it's available
    var now = new Date().getTime();
    var delta = now - then;

  5. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,12 @@ occasionally hitting 60fps. Use this trick to target a specific frame-
    rate (e.g 30fps, 48fps) until browsers better tackle this problem
    natively.
    Please ensure that if you're using this workaround, you've done your best
    to find and optimize the performance bottlenecks in your application first.
    60fps should be an attainable goal. If however you've tried your best and
    are still not getting the desired frame-rate, see if you can get some mileage
    with it.
    This type of trick works better when you know you have a fixed amount
    of work to be done and it will always take longer than 16.6ms. It doesn't
    work as well when your workload is somewhat variable.
  6. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    limitLoop.js - limit the frame-rate when using requestAnimation frame
    Released under an MIT license.
    Why use it?
    When to use it?
    ----------------
    A consistent frame-rate can be better than a janky experience only
    occasionally hitting 60fps. Use this trick to target a specific frame-
  7. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,8 @@ Why use it?
    ----------------
    A consistent frame-rate can be better than a janky experience only
    occasionally hitting 60fps. Use this trick to target a specific frame-
    rate (e.g 30fps, 48fps).
    rate (e.g 30fps, 48fps) until browsers better tackle this problem
    natively.
    This type of trick works better when you know you have a fixed amount
    of work to be done and it will always take longer than 16.6ms. It doesn't
  8. @addyosmani addyosmani revised this gist Apr 22, 2013. 2 changed files with 12 additions and 12 deletions.
    12 changes: 0 additions & 12 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -29,18 +29,6 @@ http://cssdeck.com/labs/embed/gvxnxdrh/0/output
    http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/
    */

    // rAF
    window.requestAnimationFrame = function() {
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    function(f) {
    window.setTimeout(f,1e3/60);
    }
    }();

    var limitLoop = function (fn, fps) {

    var then = new Date().getTime();
    12 changes: 12 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,18 @@ You can either use the Chrome DevTools Timeline or FPS counter to confirm
    if you're hitting a consistent fps.
    */

    // rAF normalization
    window.requestAnimationFrame = function() {
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    function(f) {
    window.setTimeout(f,1e3/60);
    }
    }();

    // define a reference to the canvas's 2D context
    var context = television.getContext('2d');

  9. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,6 @@ Prior art / inspiration
    ------------------------
    http://cssdeck.com/labs/embed/gvxnxdrh/0/output
    http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/
    and the comment by Yannick Albert
    */

    // rAF
    @@ -44,10 +43,11 @@ window.requestAnimationFrame = function() {

    var limitLoop = function (fn, fps) {

    var defaultFps = 60;
    var then = new Date().getTime();
    var oldtime = 0;
    fps = fps || defaultFps;

    // custom fps, otherwise fallback to 60
    fps = fps || 60;

    return (function loop(time) {
    requestAnimationFrame(loop);
    @@ -57,14 +57,16 @@ var limitLoop = function (fn, fps) {
    var delta = now - then;

    if (delta > interval) {
    // update time
    // Update time
    // now - (delta % interval) is an improvement over just
    // using then = now, which can end up lowering overall fps
    then = now - (delta % interval);

    // calculate fps
    var frames = 1000 / (time - oldtime)
    oldtime = time;

    // call the fn, pass current fps to it
    // call the fn, passing current fps to it
    fn(frames);
    }
    }(0));
  10. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    /*
    limitLoop.js - limit the frame-rate when using requestAnimation frame
    Released under an MIT license.
    Why use it?
    ----------------
  11. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,11 @@ A consistent frame-rate can be better than a janky experience only
    occasionally hitting 60fps. Use this trick to target a specific frame-
    rate (e.g 30fps, 48fps).
    This type of trick works better when you know you have a fixed amount
    of work to be done and it will always take longer than 16.6ms. It doesn't
    work as well when your workload is somewhat variable.
    Solution
    ----------------
  12. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 0 additions and 40 deletions.
    40 changes: 0 additions & 40 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -33,44 +33,4 @@ function drawStatic() {
    };


    // rAF
    window.requestAnimationFrame = function() {
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    function(f) {
    window.setTimeout(f,1e3/60);
    }
    }();

    var limitLoop = function (fn, fps) {

    var defaultFps = 60;
    var then = new Date().getTime();
    var oldtime = 0;
    fps = fps || defaultFps;

    return (function loop(time) {
    requestAnimationFrame(loop);

    var interval = 1000 / (this.fps || fps);
    var now = new Date().getTime();
    var delta = now - then;

    if (delta > interval) {
    // update time
    then = now - (delta % interval);

    // calculate fps
    var frames = 1000 / (time - oldtime)
    oldtime = time;

    // call the fn, pass current fps to it
    fn(frames);
    }
    }(0));
    };

    limitLoop(drawStatic, 30);
  13. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 73 additions and 10 deletions.
    83 changes: 73 additions & 10 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,76 @@
    var set;
    document.onclick = function () {
    set = true;
    /*
    Feel free to play with this over at http://jsfiddle.net/addyo/Y8P6S/.
    You can either use the Chrome DevTools Timeline or FPS counter to confirm
    if you're hitting a consistent fps.
    */

    // define a reference to the canvas's 2D context
    var context = television.getContext('2d');

    // create a buffer to hold the pixel data
    var pixelBuffer = context.createImageData(television.width, television.height);

    function drawStatic() {
    var color,
    data = pixelBuffer.data,
    index = 0,
    len = data.length;

    while (index < len) {
    // choose a random grayscale color
    color = Math.floor(Math.random() * 0xff);

    // red, green and blue are set to the same color
    // to result in a random gray pixel
    data[index++] = data[index++] = data[index++] = color;
    // the fourth multiple is always completely opaque
    data[index++] = 0xff; // alpha
    }

    // flush our pixel buffer to the canvas
    context.putImageData(pixelBuffer, 0, 0);

    };

    function test(fps) {
    if (set) {
    this.fps = 15;
    }
    console.log(fps);
    }

    limitLoop(test, 30);
    // rAF
    window.requestAnimationFrame = function() {
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    function(f) {
    window.setTimeout(f,1e3/60);
    }
    }();

    var limitLoop = function (fn, fps) {

    var defaultFps = 60;
    var then = new Date().getTime();
    var oldtime = 0;
    fps = fps || defaultFps;

    return (function loop(time) {
    requestAnimationFrame(loop);

    var interval = 1000 / (this.fps || fps);
    var now = new Date().getTime();
    var delta = now - then;

    if (delta > interval) {
    // update time
    then = now - (delta % interval);

    // calculate fps
    var frames = 1000 / (time - oldtime)
    oldtime = time;

    // call the fn, pass current fps to it
    fn(frames);
    }
    }(0));
    };

    limitLoop(drawStatic, 30);
  14. @addyosmani addyosmani revised this gist Apr 22, 2013. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,18 @@ http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/
    and the comment by Yannick Albert
    */

    // rAF
    window.requestAnimationFrame = function() {
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    function(f) {
    window.setTimeout(f,1e3/60);
    }
    }();

    var limitLoop = function (fn, fps) {

    var defaultFps = 60;
  15. @addyosmani addyosmani created this gist Apr 22, 2013.
    53 changes: 53 additions & 0 deletions limitLoop.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    /*
    limitLoop.js - limit the frame-rate when using requestAnimation frame
    Why use it?
    ----------------
    A consistent frame-rate can be better than a janky experience only
    occasionally hitting 60fps. Use this trick to target a specific frame-
    rate (e.g 30fps, 48fps).
    Solution
    ----------------
    When we draw, deduct the last frame's execution time from the current
    time to see if the time elapsed since the last frame is more than the
    fps-based interval or not. Should the condition evaluate to true, set
    the time for the current frame which will be the last frame execution
    time in the next drawing call.
    Prior art / inspiration
    ------------------------
    http://cssdeck.com/labs/embed/gvxnxdrh/0/output
    http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/
    and the comment by Yannick Albert
    */

    var limitLoop = function (fn, fps) {

    var defaultFps = 60;
    var then = new Date().getTime();
    var oldtime = 0;
    fps = fps || defaultFps;

    return (function loop(time) {
    requestAnimationFrame(loop);

    var interval = 1000 / (this.fps || fps);
    var now = new Date().getTime();
    var delta = now - then;

    if (delta > interval) {
    // update time
    then = now - (delta % interval);

    // calculate fps
    var frames = 1000 / (time - oldtime)
    oldtime = time;

    // call the fn, pass current fps to it
    fn(frames);
    }
    }(0));
    };
    13 changes: 13 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    var set;
    document.onclick = function () {
    set = true;
    };

    function test(fps) {
    if (set) {
    this.fps = 15;
    }
    console.log(fps);
    }

    limitLoop(test, 30);