-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-player.js
78 lines (68 loc) 路 2.74 KB
/
get-player.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
import {prefix} from './get-vendor-prefix';
import getKeyframes from './get-keyframes';
import getAnimationProperties from './get-animation-properties';
/**
* Converts CSS animation duration string to integer holding duration in milliseconds
* @param CSSAnimationDuration {string} [CSSAnimationDuration='0s'] The CSS animation duration string to convert
* @return {integer} The duration of the css animation string in milliseconds
* @private
*/
function getAnimationDuration(CSSAnimationDuration = '0s') {
const [unit, factor] = CSSAnimationDuration.includes('ms') ? ['ms', 1] : ['s', 1000];
const trimmed = CSSAnimationDuration.replace(unit, '').trim();
const duration = trimmed[0] === '.' ? `0${trimmed}` : trimmed;
return parseFloat(duration, 10) * factor;
}
/**
* Converts CSS animation iteration count string to integer
* @param CSSIterationCount {string} [CSSIterationCount='1'] CSS animation iteration count
* @return {integer}
* @private
*/
function getAnimationIterations(CSSIterationCount = '1') {
return parseInt(CSSIterationCount, 10);
}
/**
* Gets a web animation player based on the currently assigned CSS animation
* @param element {HTMLElement} DOM element to scan for an applied CSS animation
* @param window {Window} [window=global.window] Global context to use
* @param document {Document} [document=global.window] Document context to use
* @return {Object} Configured web animation player instance
* @private
*/
export default function getPlayer(element, window = global.window, document = global.document) {
// Read all animation related styles from element, respect prefixes
const {
name,
duration,
iterationCount,
timingFunction,
fillMode,
playState
} = getAnimationProperties(element, window, document);
// Generate keyframes based on the assigned animationName
const keyframes = getKeyframes(name, window, document);
// TODO: Should bail/stub? here if no keyframes are found
// Construct options for the webanimation player instance
const options = {
duration: getAnimationDuration(duration),
iterations: getAnimationIterations(iterationCount),
fill: fillMode,
easing: timingFunction
};
// TODO: Test get-keyframes for sorting and duplication
// Sort by offset and remove duplicates
keyframes.sort((a, b) => a.offset - b.offset);
// Construct webanimation player instance with keyframes and options
const player = element.animate(keyframes, options);
// Detach the former animation to prevent problems with polyfill
element.style[prefix('animationName', window, document)] = `__jogwheelName-${name}`;
player.__jogwheelName = name;
// Pause or play the webanimation player instance based on CSS animationPlayState
if (playState === 'paused') {
player.pause();
} else {
player.play();
}
return player;
}