1 | import { CoreTypes } from '../../core-types';
|
2 | const STYLE_CURVE_MAP = Object.freeze({
|
3 | ease: CoreTypes.AnimationCurve.ease,
|
4 | linear: CoreTypes.AnimationCurve.linear,
|
5 | 'ease-in': CoreTypes.AnimationCurve.easeIn,
|
6 | 'ease-out': CoreTypes.AnimationCurve.easeOut,
|
7 | 'ease-in-out': CoreTypes.AnimationCurve.easeInOut,
|
8 | spring: CoreTypes.AnimationCurve.spring,
|
9 | });
|
10 | export function timeConverter(value) {
|
11 | let result = parseFloat(value);
|
12 | if (value.indexOf('ms') === -1) {
|
13 | result = result * 1000;
|
14 | }
|
15 | return Math.max(0.0, result);
|
16 | }
|
17 | export function animationTimingFunctionConverter(value) {
|
18 | return value ? STYLE_CURVE_MAP[value] || parseCubicBezierCurve(value) : CoreTypes.AnimationCurve.ease;
|
19 | }
|
20 | function parseCubicBezierCurve(value) {
|
21 | const coordsString = /\((.*?)\)/.exec(value);
|
22 | const coords = coordsString && coordsString[1].split(',').map(stringToBezieCoords);
|
23 | if (value.startsWith('cubic-bezier') && coordsString && coords.length === 4) {
|
24 | const [x1, x2, y1, y2] = [...coords];
|
25 | return CoreTypes.AnimationCurve.cubicBezier(x1, x2, y1, y2);
|
26 | }
|
27 | else {
|
28 | throw new Error(`Invalid value for animation: ${value}`);
|
29 | }
|
30 | }
|
31 | function stringToBezieCoords(value) {
|
32 | const result = parseFloat(value);
|
33 | if (result < 0) {
|
34 | return 0;
|
35 | }
|
36 | else if (result > 1) {
|
37 | return 1;
|
38 | }
|
39 | return result;
|
40 | }
|
41 |
|
\ | No newline at end of file |