Skip to content

Instantly share code, notes, and snippets.

@iangilman
Last active March 27, 2022 03:02
Show Gist options
  • Select an option

  • Save iangilman/5824967 to your computer and use it in GitHub Desktop.

Select an option

Save iangilman/5824967 to your computer and use it in GitHub Desktop.

Revisions

  1. iangilman revised this gist Oct 23, 2015. No changes.
  2. iangilman revised this gist Jun 20, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion requestanimationframe.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // Modified from http://mattsnider.com/cross-browser-and-legacy-supported-requestframeanimation/
    // LICENSE: http://mattsnider.com/projects/license/
    // LICENSE: MIT: http://mattsnider.com/projects/license/

    (function(w) {
    "use strict";
  3. iangilman revised this gist Jun 20, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions requestanimationframe.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    // Modified from http://mattsnider.com/cross-browser-and-legacy-supported-requestframeanimation/
    // LICENSE: http://mattsnider.com/projects/license/

    (function(w) {
    "use strict";
    // most browsers have an implementation
  4. iangilman created this gist Jun 20, 2013.
    70 changes: 70 additions & 0 deletions requestanimationframe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    (function(w) {
    "use strict";
    // most browsers have an implementation
    w.requestAnimationFrame = w.requestAnimationFrame ||
    w.mozRequestAnimationFrame || w.webkitRequestAnimationFrame ||
    w.msRequestAnimationFrame;
    w.cancelAnimationFrame = w.cancelAnimationFrame ||
    w.mozCancelAnimationFrame || w.webkitCancelAnimationFrame ||
    w.msCancelAnimationFrame;

    // polyfill, when necessary
    if (!w.requestAnimationFrame) {
    var aAnimQueue = [],
    aProcessing = [],
    iRequestId = 0,
    iIntervalId;

    // create a mock requestAnimationFrame function
    w.requestAnimationFrame = function(callback) {
    aAnimQueue.push([++iRequestId, callback]);

    if (!iIntervalId) {
    iIntervalId = setInterval(function() {
    if (aAnimQueue.length) {
    var time = +new Date();
    // Process all of the currently outstanding frame
    // requests, but none that get added during the
    // processing.
    // Swap the arrays so we don't have to create a new
    // array every frame.
    var temp = aProcessing;
    aProcessing = aAnimQueue;
    aAnimQueue = temp;
    while (aProcessing.length) {
    aProcessing.shift()[1](time);
    }
    } else {
    // don't continue the interval, if unnecessary
    clearInterval(iIntervalId);
    iIntervalId = undefined;
    }
    }, 1000 / 50); // estimating support for 50 frames per second
    }

    return iRequestId;
    };

    // create a mock cancelAnimationFrame function
    w.cancelAnimationFrame = function(requestId) {
    // find the request ID and remove it
    var i, j;
    for (i = 0, j = aAnimQueue.length; i < j; i += 1) {
    if (aAnimQueue[i][0] === requestId) {
    aAnimQueue.splice(i, 1);
    return;
    }
    }

    // If it's not in the queue, it may be in the set we're currently
    // processing (if cancelAnimationFrame is called from within a
    // requestAnimationFrame callback).
    for (i = 0, j = aProcessing.length; i < j; i += 1) {
    if (aProcessing[i][0] === requestId) {
    aProcessing.splice(i, 1);
    return;
    }
    }
    };
    }
    })(window);