-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
184 lines (146 loc) · 3.92 KB
/
index.ts
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
import { clamp } from '@/utils/math';
import { BaseTimeline } from '../BaseTimeline';
import { NTimeline } from './types';
export type { NTimeline };
/**
* Animation timeline
*/
export class Timeline<
StaticProps extends NTimeline.IStaticProps = NTimeline.IStaticProps,
ChangeableProps extends
NTimeline.IChangeableProps = NTimeline.IChangeableProps,
CallbacksTypes extends NTimeline.ICallbacksTypes = NTimeline.ICallbacksTypes,
> extends BaseTimeline<StaticProps, ChangeableProps, CallbacksTypes> {
protected _getDefaultProps() {
return {
...super._getDefaultProps(),
duration: 1000,
shouldDestroyOnEnd: false,
};
}
/** The animation frame */
protected _animationFrame?: number;
/** Last time when animationFrame callback has been called */
protected _animationFrameLastTime: number;
/** Timeline is playing */
get isPlaying() {
return typeof this._animationFrame !== 'undefined';
}
/** Timeline is reversed */
protected _isReversed: boolean;
/** Timeline is reversed */
get isReversed() {
return this._isReversed;
}
/** Timeline is paused */
protected _isPaused: boolean;
/** Timeline is paused */
get isPaused() {
return this._isPaused;
}
get duration() {
return Math.max(this.props.duration, 1);
}
constructor(initialProps?: StaticProps & ChangeableProps, canInit = true) {
super(initialProps, false);
// set default variables
this._animationFrame = undefined;
this._animationFrameLastTime = 0;
this._isReversed = false;
this._isPaused = false;
if (canInit) {
this.init();
}
}
/** Play the timeline */
public play() {
if (this.isDestroyed || this.progress === 1) {
return;
}
this._isReversed = false;
this._isPaused = false;
if (!this.isPlaying) {
this._animationFrameLastTime = +new Date();
this._animate();
}
}
/** Reverse timeline */
public reverse() {
if (this.isDestroyed || this.progress === 0) {
return;
}
this._isReversed = true;
this._isPaused = false;
if (!this.isPlaying) {
this._animationFrameLastTime = +new Date();
this._animate();
}
}
/** Pause animation */
public pause() {
if (this.isDestroyed) {
return;
}
this._isPaused = true;
if (this._animationFrame) {
window.cancelAnimationFrame(this._animationFrame);
}
this._animationFrame = undefined;
}
/** Reset timeline */
public reset() {
if (this.isDestroyed) {
return;
}
this.pause();
this.progress = 0;
}
/** Start animation */
protected _animate() {
if (this.isPaused) {
return;
}
const { isReversed } = this;
// calculate difference between frames
const currentTime = +new Date();
const frameDiff = Math.abs(this._animationFrameLastTime - currentTime);
this._animationFrameLastTime = currentTime;
// calculate current progress
const progressIterator = frameDiff / this.duration / (isReversed ? -1 : 1);
const progressTarget = clamp(this.progress + progressIterator, [0, 1]);
this.progress = progressTarget;
// end animation
if (
(progressTarget === 1 && !isReversed) ||
(progressTarget === 0 && isReversed)
) {
this._isReversed = false;
this._isPaused = false;
this._animationFrame = undefined;
return;
}
// continue animation
this._animationFrame = window.requestAnimationFrame(
this._animate.bind(this),
);
}
/** Events on progress */
protected _handleProgressUpdate() {
super._handleProgressUpdate();
if (this.progress === 0) {
this.callbacks.tbt('start', undefined);
return;
}
if (this.progress === 1) {
this.callbacks.tbt('end', undefined);
if (this.props.shouldDestroyOnEnd) {
this.destroy();
}
}
}
/** Destroy the timeline */
protected _destroy() {
this.pause();
super._destroy();
}
}