zhangjian
2023-06-05 0976d2d0f90cff460cedfdc8bd74e98c2c31a58c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
 * Animation main class, dispatch and manage all animation controllers
 *
 */
// TODO Additive animation
// http://iosoteric.com/additive-animations-animatewithduration-in-ios-8/
// https://developer.apple.com/videos/wwdc2014/#236
 
import Eventful from '../core/Eventful';
import requestAnimationFrame from './requestAnimationFrame';
import Animator from './Animator';
import Clip from './Clip';
 
export function getTime() {
    return new Date().getTime();
}
 
interface Stage {
    update?: () => void
}
 
interface AnimationOption {
    stage?: Stage
}
/**
 * @example
 *     const animation = new Animation();
 *     const obj = {
 *         x: 100,
 *         y: 100
 *     };
 *     animation.animate(node.position)
 *         .when(1000, {
 *             x: 500,
 *             y: 500
 *         })
 *         .when(2000, {
 *             x: 100,
 *             y: 100
 *         })
 *         .start();
 */
 
export default class Animation extends Eventful {
 
    stage: Stage
 
    // Use linked list to store clip
    private _head: Clip
    private _tail: Clip
 
    private _running = false
 
    private _time = 0
    private _pausedTime = 0
    private _pauseStart = 0
 
    private _paused = false;
 
    constructor(opts?: AnimationOption) {
        super();
 
        opts = opts || {};
 
        this.stage = opts.stage || {};
    }
 
    /**
     * Add clip
     */
    addClip(clip: Clip) {
        if (clip.animation) {
            // Clip has been added
            this.removeClip(clip);
        }
 
        if (!this._head) {
            this._head = this._tail = clip;
        }
        else {
            this._tail.next = clip;
            clip.prev = this._tail;
            clip.next = null;
            this._tail = clip;
        }
        clip.animation = this;
    }
    /**
     * Add animator
     */
    addAnimator(animator: Animator<any>) {
        animator.animation = this;
        const clip = animator.getClip();
        if (clip) {
            this.addClip(clip);
        }
    }
    /**
     * Delete animation clip
     */
    removeClip(clip: Clip) {
        if (!clip.animation) {
            return;
        }
        const prev = clip.prev;
        const next = clip.next;
        if (prev) {
            prev.next = next;
        }
        else {
            // Is head
            this._head = next;
        }
        if (next) {
            next.prev = prev;
        }
        else {
            // Is tail
            this._tail = prev;
        }
        clip.next = clip.prev = clip.animation = null;
    }
 
    /**
     * Delete animation clip
     */
    removeAnimator(animator: Animator<any>) {
        const clip = animator.getClip();
        if (clip) {
            this.removeClip(clip);
        }
        animator.animation = null;
    }
 
    update(notTriggerFrameAndStageUpdate?: boolean) {
        const time = getTime() - this._pausedTime;
        const delta = time - this._time;
        let clip = this._head;
 
        while (clip) {
            // Save the nextClip before step.
            // So the loop will not been affected if the clip is removed in the callback
            const nextClip = clip.next;
            let finished = clip.step(time, delta);
            if (finished) {
                clip.ondestroy();
                this.removeClip(clip);
                clip = nextClip;
            }
            else {
                clip = nextClip;
            }
        }
 
        this._time = time;
 
        if (!notTriggerFrameAndStageUpdate) {
 
            // 'frame' should be triggered before stage, because upper application
            // depends on the sequence (e.g., echarts-stream and finish
            // event judge)
            this.trigger('frame', delta);
 
            this.stage.update && this.stage.update();
        }
    }
 
    _startLoop() {
        const self = this;
 
        this._running = true;
 
        function step() {
            if (self._running) {
                requestAnimationFrame(step);
                !self._paused && self.update();
            }
        }
 
        requestAnimationFrame(step);
    }
 
    /**
     * Start animation.
     */
    start() {
        if (this._running) {
            return;
        }
 
        this._time = getTime();
        this._pausedTime = 0;
 
        this._startLoop();
    }
 
    /**
     * Stop animation.
     */
    stop() {
        this._running = false;
    }
 
    /**
     * Pause animation.
     */
    pause() {
        if (!this._paused) {
            this._pauseStart = getTime();
            this._paused = true;
        }
    }
 
    /**
     * Resume animation.
     */
    resume() {
        if (this._paused) {
            this._pausedTime += getTime() - this._pauseStart;
            this._paused = false;
        }
    }
 
    /**
     * Clear animation.
     */
    clear() {
        let clip = this._head;
 
        while (clip) {
            let nextClip = clip.next;
            clip.prev = clip.next = clip.animation = null;
            clip = nextClip;
        }
 
        this._head = this._tail = null;
    }
 
    /**
     * Whether animation finished.
     */
    isFinished() {
        return this._head == null;
    }
 
    /**
     * Creat animator for a target, whose props can be animated.
     */
    // TODO Gap
    animate<T>(target: T, options: {
        loop?: boolean  // Whether loop animation
    }) {
        options = options || {};
 
        // Start animation loop
        this.start();
 
        const animator = new Animator(
            target,
            options.loop
        );
 
        this.addAnimator(animator);
 
        return animator;
    }
}