# Animation 类

# 概述

监听浏览器的每次渲染请求,发起更新,根据 __dirty 等属性来实现刷新。

# 构造函数

Amination 基础自 Dispatcher ,

# 关系

# 继承

# Dispatcher

其实就是 Eventful 改了下名字,类可以监听派发事件

# 方法

# addClip

添加动画剪辑,动画剪辑会被遍历调用,用来更新动画的数据

addClip: function (clip) {
    this._clips.push(clip);
}

# addAnimator

添加动画对象,获取动画对象中的所有剪辑并添加

ddAnimator: function (animator) {
    animator.animation = this;
    var clips = animator.getClips();
    for (var i = 0; i < clips.length; i++) {
        this.addClip(clips[i]);
    }
}

# removeClip

移除动画剪辑

removeClip: function (clip) {
    var idx = util.indexOf(this._clips, clip);
    if (idx >= 0) {
        this._clips.splice(idx, 1);
    }
}

# removeAnimator

移除动画对象中包含的所有动画剪辑

removeAnimator: function (animator) {
    var clips = animator.getClips();
    for (var i = 0; i < clips.length; i++) {
        this.removeClip(clips[i]);
    }
    animator.animation = null;
}

# _update

每一帧都需要调用(!self._paused

_update: function () {
    var time = new Date().getTime() - this._pausedTime;
    var delta = time - this._time;
    var clips = this._clips;
    var len = clips.length;

    var deferredEvents = [];
    var deferredClips = [];
    for (var i = 0; i < len; i++) {
        var clip = clips[i];
        var e = clip.step(time, delta);
        if (e) {
            deferredEvents.push(e);
            deferredClips.push(clip);
        }
    }

    // Remove the finished clip
    for (var i = 0; i < len;) {
        if (clips[i]._needsRemove) {
            clips[i] = clips[len - 1];
            clips.pop();
            len--;
        }
        else {
            i++;
        }
    }

    len = deferredEvents.length;
    for (var i = 0; i < len; i++) {
        deferredClips[i].fire(deferredEvents[i]);
    }

    this._time = time;

    this.onframe(delta);
    this.trigger('frame', delta);

    if (this.stage.update) {
        this.stage.update();
    }
}

# _startLoop

开始事件循环

_startLoop: function () {
    var self = this;
    this._running = true;
    function step() {
        if (self._running) {
            requestAnimationFrame(step);
            !self._paused && self._update();
        }
    }
    requestAnimationFrame(step);
}

# start

开始动画

start: function () {
    this._time = new Date().getTime();
    this._pausedTime = 0;
    this._startLoop();
}

# stop

停止动画

stop: function () {
    this._running = false;
}

# pause

暂停动画

pause: function () {
    if (!this._paused) {
        this._pauseStart = new Date().getTime();
        this._paused = true;
    }
}

# resume

恢复暂停

resume: function () {
    if (this._paused) {
        this._pausedTime += (new Date().getTime()) - this._pauseStart;
        this._paused = false;
    }
}

# clear

清除动画

clear: function () {
    this._clips = [];
}

# isFinished

动画是否结束

isFinished: function () {
    return !this._clips.length;
}

# animate

为目标创建动画,使其可以执行动画

animate: function (target, options) {
    options = options || {};
    var animator = new Animator(
        target,
        options.loop,
        options.getter,
        options.setter
    );
    this.addAnimator(animator);
    return animator;
}