-
Notifications
You must be signed in to change notification settings - Fork 1
/
anim-sequence.esm.js
276 lines (241 loc) · 8.13 KB
/
anim-sequence.esm.js
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
268
269
270
271
272
273
274
275
276
/* ================================================
DON'T MANUALLY EDIT THIS FILE
================================================ */
/*
* AnimSequence
* https://anseki.github.io/anim-sequence/
*
* Copyright (c) 2018 anseki
* Licensed under the MIT license.
*/
// *** Currently, this code except `export` is not ES2015. ***
var
FUNC_KEYS = {
ease: [0.25, 0.1, 0.25, 1],
linear: [0, 0, 1, 1],
'ease-in': [0.42, 0, 1, 1],
'ease-out': [0, 0, 0.58, 1],
'ease-in-out': [0.42, 0, 0.58, 1]
},
MSPF = 1000 / 60 / 2, // precision ms/frame (FPS: 60)
requestAnim = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) { setTimeout(callback, MSPF); },
cancelAnim = window.cancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.msCancelAnimationFrame ||
function(requestID) { clearTimeout(requestID); },
isFinite = Number.isFinite || function(value) { return typeof value === 'number' && window.isFinite(value); },
/**
* @callback frameCallback
* @param {} value - A value that was made by `valueCallback`.
* @param {boolean} finish
* @param {number} timeRatio - Progress [0, 1].
* @param {number} outputRatio - Progress [0, 1].
* @returns {} `false` to stop.
*/
/**
* @typedef {Object} task
* @property {number} animId
* @property {frameCallback} frameCallback - Callback that is called each frame.
* @property {number} duration
* @property {number} count - `0` as infinite.
* @property {{value, timeRatio: number, outputRatio: number}[]} frames
* @property {(number|null)} framesStart - The time when first frame ran, or `null` if it is not playing.
* @property {number} loopsLeft - A counter for loop.
* @property {number} lastFrame - index of last frame that ran.
* @property {boolean} reverse - Play backwards.
*/
/** @type {task[]} */
tasks = [],
newAnimId = 0,
requestID;
function step() {
var now = Date.now(),
next = false;
if (requestID) {
cancelAnim.call(window, requestID);
requestID = null;
}
tasks.forEach(function(task) {
var timeLen, loops, frame;
if (!task.framesStart) { return; }
timeLen = now - task.framesStart;
if (timeLen >= task.duration && task.count && task.loopsLeft <= 1) {
frame = task.frames[(task.lastFrame = task.reverse ? 0 : task.frames.length - 1)];
task.frameCallback(frame.value, true, frame.timeRatio, frame.outputRatio);
task.framesStart = null;
return;
}
if (timeLen > task.duration) {
loops = Math.floor(timeLen / task.duration);
if (task.count) {
if (loops >= task.loopsLeft) { // Here `task.loopsLeft > 1`
frame = task.frames[(task.lastFrame = task.reverse ? 0 : task.frames.length - 1)];
task.frameCallback(frame.value, true, frame.timeRatio, frame.outputRatio);
task.framesStart = null;
return;
}
task.loopsLeft -= loops;
}
task.framesStart += task.duration * loops;
timeLen = now - task.framesStart;
}
if (task.reverse) { timeLen = task.duration - timeLen; }
frame = task.frames[(task.lastFrame = Math.round(timeLen / MSPF))];
if (task.frameCallback(frame.value, false, frame.timeRatio, frame.outputRatio) !== false) {
next = true;
} else {
task.framesStart = null;
}
});
if (next) { requestID = requestAnim.call(window, step); }
}
function startTask(task, timeRatio) {
task.framesStart = Date.now();
if (timeRatio != null) {
task.framesStart -= task.duration * (task.reverse ? 1 - timeRatio : timeRatio);
}
task.loopsLeft = task.count;
task.lastFrame = null;
step();
}
var AnimSequence = {
/**
* Callback that makes value that is required by each frame.
* @callback valueCallback
* @param {number} outputRatio - Progress [0, 1].
* @returns {}
*/
/**
* @param {(valueCallback|null)} valueCallback - valueCallback
* @param {frameCallback} frameCallback - task property
* @param {number} duration - task property
* @param {number} count - task property
* @param {(string|number[])} timing - FUNC_KEYS or [x1, y1, x2, y2]
* @param {(boolean|null)} reverse - playing property
* @param {number|boolean} [timeRatio] - Play from the midst. [0, 1], or `false` that prevents it starting.
* @returns {number} animId to control the task.
*/
add: function(valueCallback, frameCallback, duration, count, timing, reverse, timeRatio) {
var animId = ++newAnimId,
task, frames, stepX, stepT, nextX, t, point;
function getPoint(t) {
var t2 = t * t,
t3 = t2 * t,
t1 = 1 - t,
t12 = t1 * t1,
p1f = 3 * t12 * t,
p2f = 3 * t1 * t2;
return {
x: p1f * timing[0] + p2f * timing[2] + t3,
y: p1f * timing[1] + p2f * timing[3] + t3
};
}
function newFrame(timeRatio, outputRatio) {
return {value: valueCallback(outputRatio),
timeRatio: timeRatio, outputRatio: outputRatio};
}
if (typeof timing === 'string') { timing = FUNC_KEYS[timing]; }
valueCallback = valueCallback || function() {};
// Generate `frames` list
if (duration < MSPF) {
frames = [newFrame(0, 0), newFrame(1, 1)];
} else {
stepX = MSPF / duration;
frames = [newFrame(0, 0)];
if (timing[0] === 0 && timing[1] === 0 && timing[2] === 1 && timing[3] === 1) { // linear
for (nextX = stepX; nextX <= 1; nextX += stepX) {
frames.push(newFrame(nextX, nextX)); // x === y
}
} else {
stepT = stepX / 10; // precision for `t`
nextX = stepX;
for (t = stepT; t <= 1; t += stepT) {
point = getPoint(t);
if (point.x >= nextX) {
frames.push(newFrame(point.x, point.y));
nextX += stepX;
}
}
}
frames.push(newFrame(1, 1)); // for tolerance
}
task = {
animId: animId,
frameCallback: frameCallback, duration: duration, count: count, // task properties
frames: frames,
reverse: !!reverse
};
tasks.push(task);
if (timeRatio !== false) { startTask(task, timeRatio); }
return animId;
},
remove: function(animId) {
var iRemove;
if (tasks.some(function(task, i) {
if (task.animId === animId) {
iRemove = i;
task.framesStart = null; // for `tasks.forEach` that is playing now.
return true;
}
return false;
})) {
tasks.splice(iRemove, 1);
}
},
/**
* @param {number} animId - Target task.
* @param {boolean} reverse - Play backwards.
* @param {number} [timeRatio] - Play from the midst. [0, 1]
* @returns {void}
*/
start: function(animId, reverse, timeRatio) {
tasks.some(function(task) {
if (task.animId === animId) {
task.reverse = !!reverse;
startTask(task, timeRatio);
return true;
}
return false;
});
},
/**
* @param {number} animId - Target task.
* @param {boolean} [getTimeRatioByFrame] - Return timeRatio of last frame that ran. [0, 1]
* @returns {(number|undefined)} timeRatio [0, 1]
*/
stop: function(animId, getTimeRatioByFrame) {
var timeRatio;
tasks.some(function(task) {
if (task.animId === animId) {
if (!getTimeRatioByFrame) {
timeRatio = (Date.now() - task.framesStart) / task.duration;
if (task.reverse) { timeRatio = 1 - timeRatio; }
if (timeRatio < 0) {
timeRatio = 0;
} else if (timeRatio > 1) {
timeRatio = 1;
}
} else if (task.lastFrame != null) {
timeRatio = task.frames[task.lastFrame].timeRatio;
}
task.framesStart = null;
return true;
}
return false;
});
return timeRatio;
},
validTiming: function(timing) {
return typeof timing === 'string' ? FUNC_KEYS[timing] :
Array.isArray(timing) && [0, 1, 2, 3].every(function(i) {
return isFinite(timing[i]) && timing[i] >= 0 && timing[i] <= 1;
}) ? [timing[0], timing[1], timing[2], timing[3]] :
null;
}
};
export default AnimSequence;