1 | import { __read, __spreadArray } from "tslib";
|
2 | import { GlobalContainer } from 'mana-syringe';
|
3 | import { StyleValueRegistry } from '../css/interfaces';
|
4 | import { parseEasingFunction } from './animation';
|
5 | import { camelCase } from './string';
|
6 | export function convertEffectInput(keyframes, timing, target) {
|
7 | var propertySpecificKeyframeGroups = makePropertySpecificKeyframeGroups(keyframes, timing);
|
8 | var interpolations = makeInterpolations(propertySpecificKeyframeGroups, target);
|
9 | return function (target, fraction) {
|
10 | if (fraction !== null) {
|
11 | interpolations.filter(function (interpolation) {
|
12 | return fraction >= interpolation.applyFrom && fraction < interpolation.applyTo;
|
13 | }).forEach(function (interpolation) {
|
14 | var offsetFraction = fraction - interpolation.startOffset;
|
15 | var localDuration = interpolation.endOffset - interpolation.startOffset;
|
16 | var scaledLocalTime = localDuration === 0 ? 0 : interpolation.easingFunction(offsetFraction / localDuration);
|
17 |
|
18 | target.style[interpolation.property] = interpolation.interpolation(scaledLocalTime);
|
19 | });
|
20 | } else {
|
21 | for (var property in propertySpecificKeyframeGroups) {
|
22 | if (isNotReservedWord(property)) {
|
23 |
|
24 | target.style[property] = null;
|
25 | }
|
26 | }
|
27 | }
|
28 | };
|
29 | }
|
30 |
|
31 | function isNotReservedWord(member) {
|
32 | return member !== 'offset' && member !== 'easing' && member !== 'composite' && member !== 'computedOffset';
|
33 | }
|
34 |
|
35 | function makePropertySpecificKeyframeGroups(keyframes, timing) {
|
36 | var propertySpecificKeyframeGroups = {};
|
37 |
|
38 | for (var i = 0; i < keyframes.length; i++) {
|
39 | for (var member in keyframes[i]) {
|
40 | if (isNotReservedWord(member)) {
|
41 | var propertySpecificKeyframe = {
|
42 | offset: keyframes[i].offset,
|
43 | computedOffset: keyframes[i].computedOffset,
|
44 | easing: keyframes[i].easing,
|
45 | easingFunction: parseEasingFunction(keyframes[i].easing) || timing.easingFunction,
|
46 | value: keyframes[i][member]
|
47 | };
|
48 | propertySpecificKeyframeGroups[member] = propertySpecificKeyframeGroups[member] || [];
|
49 |
|
50 | propertySpecificKeyframeGroups[member].push(propertySpecificKeyframe);
|
51 | }
|
52 | }
|
53 | }
|
54 |
|
55 | return propertySpecificKeyframeGroups;
|
56 | }
|
57 |
|
58 | function makeInterpolations(propertySpecificKeyframeGroups, target) {
|
59 | var interpolations = [];
|
60 |
|
61 | for (var groupName in propertySpecificKeyframeGroups) {
|
62 | var keyframes = propertySpecificKeyframeGroups[groupName];
|
63 |
|
64 | for (var i = 0; i < keyframes.length - 1; i++) {
|
65 | var startIndex = i;
|
66 | var endIndex = i + 1;
|
67 | var startOffset = keyframes[startIndex].computedOffset;
|
68 | var endOffset = keyframes[endIndex].computedOffset;
|
69 | var applyFrom = startOffset;
|
70 | var applyTo = endOffset;
|
71 |
|
72 | if (i === 0) {
|
73 | applyFrom = -Infinity;
|
74 |
|
75 | if (endOffset === 0) {
|
76 | endIndex = startIndex;
|
77 | }
|
78 | }
|
79 |
|
80 | if (i === keyframes.length - 2) {
|
81 | applyTo = Infinity;
|
82 |
|
83 | if (startOffset === 1) {
|
84 | startIndex = endIndex;
|
85 | }
|
86 | }
|
87 |
|
88 | interpolations.push({
|
89 | applyFrom: applyFrom,
|
90 | applyTo: applyTo,
|
91 | startOffset: keyframes[startIndex].computedOffset,
|
92 | endOffset: keyframes[endIndex].computedOffset,
|
93 | easingFunction: keyframes[startIndex].easingFunction,
|
94 | property: groupName,
|
95 | interpolation: propertyInterpolation(groupName, keyframes[startIndex].value, keyframes[endIndex].value, target)
|
96 | });
|
97 | }
|
98 | }
|
99 |
|
100 | interpolations.sort(function (leftInterpolation, rightInterpolation) {
|
101 | return leftInterpolation.startOffset - rightInterpolation.startOffset;
|
102 | });
|
103 | return interpolations;
|
104 | }
|
105 |
|
106 | var InterpolationFactory = function InterpolationFactory(from, to, // eslint-disable-next-line @typescript-eslint/ban-types
|
107 | convertToString) {
|
108 | return function (f) {
|
109 | return convertToString(interpolate(from, to, f));
|
110 | };
|
111 | };
|
112 |
|
113 | function propertyInterpolation(property, left, right, target) {
|
114 | var parsedLeft = left;
|
115 | var parsedRight = right;
|
116 | var registry = GlobalContainer.get(StyleValueRegistry);
|
117 | var metadata = registry.getMetadata(property);
|
118 |
|
119 | if (metadata && metadata.handler && metadata.interpolable) {
|
120 | var propertyHandler = GlobalContainer.get(metadata.handler);
|
121 |
|
122 | if (propertyHandler) {
|
123 | if (propertyHandler.parser) {
|
124 | parsedLeft = propertyHandler.parser(left, target);
|
125 | parsedRight = propertyHandler.parser(right, target);
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | var interpolationArgs = propertyHandler.mixer(parsedLeft, parsedRight, target);
|
134 |
|
135 | if (interpolationArgs) {
|
136 |
|
137 | var interp_1 = InterpolationFactory.apply(void 0, __spreadArray([], __read(interpolationArgs), false));
|
138 | return function (t) {
|
139 | if (t === 0) return left;
|
140 | if (t === 1) return right;
|
141 | return interp_1(t);
|
142 | };
|
143 | }
|
144 | }
|
145 | }
|
146 |
|
147 |
|
148 | return InterpolationFactory(false, true, function (bool) {
|
149 | return bool ? right : left;
|
150 | });
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | function interpolate(from, to, f) {
|
158 | if (typeof from === 'number' && typeof to === 'number') {
|
159 | return from * (1 - f) + to * f;
|
160 | }
|
161 |
|
162 | if (typeof from === 'boolean' && typeof to === 'boolean' || typeof from === 'string' && typeof to === 'string'
|
163 | ) {
|
164 | return f < 0.5 ? from : to;
|
165 | }
|
166 |
|
167 | if (Array.isArray(from) && Array.isArray(to)) {
|
168 |
|
169 | if (from.length === to.length) {
|
170 | var r = [];
|
171 |
|
172 | for (var i = 0; i < from.length; i++) {
|
173 | r.push(interpolate(from[i], to[i], f));
|
174 | }
|
175 |
|
176 | return r;
|
177 | }
|
178 | }
|
179 |
|
180 | throw new Error('Mismatched interpolation arguments ' + from + ':' + to);
|
181 | }
|
182 |
|
183 | var FORMAT_ATTR_MAP = {
|
184 | d: {
|
185 | alias: 'path'
|
186 | },
|
187 | strokeDasharray: {
|
188 | alias: 'lineDash'
|
189 | },
|
190 | strokeWidth: {
|
191 | alias: 'lineWidth'
|
192 | },
|
193 | textAnchor: {
|
194 | alias: 'textAlign',
|
195 | values: {
|
196 | middle: 'center'
|
197 | }
|
198 | },
|
199 | src: {
|
200 | alias: 'img'
|
201 | }
|
202 | };
|
203 | export function formatAttribute(name, value) {
|
204 | var _a;
|
205 |
|
206 | var attributeName = camelCase(name);
|
207 | var map = FORMAT_ATTR_MAP[attributeName];
|
208 | attributeName = (map === null || map === void 0 ? void 0 : map.alias) || attributeName;
|
209 | var attributeValue = ((_a = map === null || map === void 0 ? void 0 : map.values) === null || _a === void 0 ? void 0 : _a[value]) || value;
|
210 | return [attributeName, attributeValue];
|
211 | } |
\ | No newline at end of file |