-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.js
50 lines (44 loc) · 1.26 KB
/
animate.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
import { getDeferred } from './promises.js';
/**
* Helper methods so there's less working with strings
*/
export const rotate = n => `rotate(${n})`;
export const scale = n => `scale(${n})`;
export const translate = (x, y) => `translate(${x}, ${y})`;
export async function animate(target, keyframes, {
composite = 'replace',
delay = 0,
direction = 'normal',
duration = 400,
easing = 'linear',
endDelay = 0,
fill = 'none',
iterationComposite = 'replace',
iterationStart = 0,
iterations = 1,
pseudoElement,
signal,
} = {}) {
const { resolve, reject, promise } = getDeferred();
if (signal instanceof AbortSignal && signal.aborted) {
reject(signal.reason);
} else {
const controller = new AbortController();
const anim = target.animate(keyframes, {
composite, delay, direction, duration, easing, endDelay, fill,
iterationComposite, iterationStart, iterations, pseudoElement,
});
anim.addEventListener('finish', ({ target }) => {
resolve(target);
controller.abort();
}, { signal: controller.signal });
if (signal instanceof AbortSignal) {
signal.addEventListener('abort', ({ target }) => {
reject(target.reason);
anim.cancel();
controller.abort(target.reason);
}, { signal: controller.signal });
}
}
return await promise;
}