AnimationFrame.ts 1.42 KB
Newer Older
patcharaporn's avatar
patcharaporn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import {  root  } from './root';

export class RequestAnimationFrameDefinition {
  cancelAnimationFrame: (handle: number) => void;
  requestAnimationFrame: (cb: () => void) => number;
  constructor(root: any) {
    if (root.requestAnimationFrame) {
      this.cancelAnimationFrame = root.cancelAnimationFrame.bind(root);
      this.requestAnimationFrame = root.requestAnimationFrame.bind(root);
    } else if (root.mozRequestAnimationFrame) {
      this.cancelAnimationFrame = root.mozCancelAnimationFrame.bind(root);
      this.requestAnimationFrame = root.mozRequestAnimationFrame.bind(root);
    } else if (root.webkitRequestAnimationFrame) {
      this.cancelAnimationFrame = root.webkitCancelAnimationFrame.bind(root);
      this.requestAnimationFrame = root.webkitRequestAnimationFrame.bind(root);
    } else if (root.msRequestAnimationFrame) {
      this.cancelAnimationFrame = root.msCancelAnimationFrame.bind(root);
      this.requestAnimationFrame = root.msRequestAnimationFrame.bind(root);
    } else if (root.oRequestAnimationFrame) {
      this.cancelAnimationFrame = root.oCancelAnimationFrame.bind(root);
      this.requestAnimationFrame = root.oRequestAnimationFrame.bind(root);
    } else {
      this.cancelAnimationFrame = root.clearTimeout.bind(root);
      this.requestAnimationFrame = function(cb) { return root.setTimeout(cb, 1000 / 60); };
    }
  }
}

export const AnimationFrame = new RequestAnimationFrameDefinition(root);