resessh
10/28/2015 - 2:32 PM

A

A

{
    "compilerOptions": {
        "out": "built/test.js",
        "sourceMap": true,
        "target": "es5"
    },
    "files": [
        "test.ts",
        "A.ts"
    ]
}
/// <reference path="A.ts" />

var a = new A();
interface Window {
          requestAnimationFrame(callback: Function, element?: any): void;
    webkitRequestAnimationFrame(callback: Function, element?: any): void;
       mozRequestAnimationFrame(callback: Function, element?: any): void;
         oRequestAnimationFrame(callback: Function, element?: any): void;
}

class A {

    private cont_flag: boolean = true;
    private progress: number = 0;
    private t = function(): number {
        return new Date().getTime();
    };
    private raf = window.requestAnimationFrame
               || window.webkitRequestAnimationFrame
               || window.mozRequestAnimationFrame
               || window.oRequestAnimationFrame
               || window.msRequestAnimationFrame
               || function(callback: Function) { setTimeout(callback, 1000/30); };
    private transition_list = {
                linear: function(p): number { return p }
    ,       sineEaseIn: function(p): number { return 1-Math.cos(p*Math.PI/2); }
    ,      cubicEaseIn: function(p): number { return p*p*p*p }
    ,     cubicEaseOut: function(p): number { p=1-p; return 1-p*p*p*p }
    ,   cubicEaseInOut: function(p): number { return (p<=0.5)?Math.pow(p*2,4)/2:Math.pow((1-p)*2,4)/2+0.5; }
    ,   quinticEaseOut: function(p): number { p=1-p; return 1-Math.pow(p, 5) }
    ,       nthEaseOut: function(p): number { p=1-p; return 1-Math.pow(p, 12) }
    };

    public start = function(func: Function, duration: number, callback: Function, transition: any): void {
        this.cont_flag = true;
        if (typeof transition == "string") {
            transition = this.transition_list[transition];
        } else if (typeof transition == "function") {}
          else {
            transition = this.transition_list["linear"];
        }
        var start_time: number = this.t();

        function step(): void {
            if (duration) {
                this.progress = (this.t() - start_time) / duration;
            } else {
                this.progress = this.t() - start_time;
            }
            if (this.progress < 1 || !duration) {
                func(transition(this.progress) || this.progress);
                if (this.cont_flag == true) {
                    this.raf(step());
                }
            } else {
                callback();
            }
        }
        this.raf(step);
    }

    public stop = function(callback: Function): void {
        this.cont_flag = false;
        callback();
    }
}