@junwan666/remotion-animation-utils 4.0.448-zh.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +7 -0
- package/README.md +18 -0
- package/dist/esm/index.mjs +680 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +18 -0
- package/dist/transformation-helpers/interpolate-styles/constants.d.ts +3 -0
- package/dist/transformation-helpers/interpolate-styles/constants.js +7 -0
- package/dist/transformation-helpers/interpolate-styles/index.d.ts +6 -0
- package/dist/transformation-helpers/interpolate-styles/index.js +195 -0
- package/dist/transformation-helpers/interpolate-styles/utils.d.ts +41 -0
- package/dist/transformation-helpers/interpolate-styles/utils.js +178 -0
- package/dist/transformation-helpers/make-transform/index.d.ts +3 -0
- package/dist/transformation-helpers/make-transform/index.js +29 -0
- package/dist/transformation-helpers/make-transform/is-unit-with-string.d.ts +1 -0
- package/dist/transformation-helpers/make-transform/is-unit-with-string.js +18 -0
- package/dist/transformation-helpers/make-transform/transform-functions.d.ts +46 -0
- package/dist/transformation-helpers/make-transform/transform-functions.js +301 -0
- package/dist/type.d.ts +35 -0
- package/dist/type.js +34 -0
- package/package.json +55 -0
|
@@ -0,0 +1,680 @@
|
|
|
1
|
+
// src/transformation-helpers/interpolate-styles/index.tsx
|
|
2
|
+
import { interpolate, interpolateColors } from "remotion";
|
|
3
|
+
|
|
4
|
+
// src/transformation-helpers/interpolate-styles/utils.ts
|
|
5
|
+
import { NoReactInternals } from "remotion/no-react";
|
|
6
|
+
|
|
7
|
+
// src/transformation-helpers/interpolate-styles/constants.ts
|
|
8
|
+
var NUMBER = "[-+]?\\d*\\.?\\d+";
|
|
9
|
+
var PERCENTAGE = NUMBER + "%";
|
|
10
|
+
|
|
11
|
+
// src/transformation-helpers/interpolate-styles/utils.ts
|
|
12
|
+
function call(...args) {
|
|
13
|
+
return "\\(\\s*(" + args.join(")\\s*,\\s*(") + ")\\s*\\)";
|
|
14
|
+
}
|
|
15
|
+
var MODERN_VALUE = "(?:none|[-+]?\\d*\\.?\\d+(?:%|deg|rad|grad|turn)?)";
|
|
16
|
+
function modernColorCall(name) {
|
|
17
|
+
return new RegExp(name + "\\(\\s*(" + MODERN_VALUE + ")\\s+(" + MODERN_VALUE + ")\\s+(" + MODERN_VALUE + ")(?:\\s*\\/\\s*(" + MODERN_VALUE + "))?\\s*\\)");
|
|
18
|
+
}
|
|
19
|
+
function getColorMatchers() {
|
|
20
|
+
const cachedMatchers = {
|
|
21
|
+
rgb: undefined,
|
|
22
|
+
rgba: undefined,
|
|
23
|
+
hsl: undefined,
|
|
24
|
+
hsla: undefined,
|
|
25
|
+
hex3: undefined,
|
|
26
|
+
hex4: undefined,
|
|
27
|
+
hex5: undefined,
|
|
28
|
+
hex6: undefined,
|
|
29
|
+
hex8: undefined,
|
|
30
|
+
oklch: undefined,
|
|
31
|
+
oklab: undefined,
|
|
32
|
+
lab: undefined,
|
|
33
|
+
lch: undefined,
|
|
34
|
+
hwb: undefined
|
|
35
|
+
};
|
|
36
|
+
if (cachedMatchers.rgb === undefined) {
|
|
37
|
+
cachedMatchers.rgb = new RegExp("rgb" + call(NUMBER, NUMBER, NUMBER));
|
|
38
|
+
cachedMatchers.rgba = new RegExp("rgba" + call(NUMBER, NUMBER, NUMBER, NUMBER));
|
|
39
|
+
cachedMatchers.hsl = new RegExp("hsl" + call(NUMBER, PERCENTAGE, PERCENTAGE));
|
|
40
|
+
cachedMatchers.hsla = new RegExp("hsla" + call(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER));
|
|
41
|
+
cachedMatchers.hex3 = /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/;
|
|
42
|
+
cachedMatchers.hex4 = /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/;
|
|
43
|
+
cachedMatchers.hex6 = /^#([0-9a-fA-F]{6})$/;
|
|
44
|
+
cachedMatchers.hex8 = /^#([0-9a-fA-F]{8})$/;
|
|
45
|
+
cachedMatchers.oklch = modernColorCall("oklch");
|
|
46
|
+
cachedMatchers.oklab = modernColorCall("oklab");
|
|
47
|
+
cachedMatchers.lab = modernColorCall("lab");
|
|
48
|
+
cachedMatchers.lch = modernColorCall("lch");
|
|
49
|
+
cachedMatchers.hwb = modernColorCall("hwb");
|
|
50
|
+
}
|
|
51
|
+
return cachedMatchers;
|
|
52
|
+
}
|
|
53
|
+
var extractOrderedPartsOfValue = (value) => {
|
|
54
|
+
const parts = [];
|
|
55
|
+
let remainingValue = value;
|
|
56
|
+
while (remainingValue.length > 0) {
|
|
57
|
+
const functionMatch = remainingValue.match(/([a-zA-Z-]+)\(([^)]+)\)/);
|
|
58
|
+
if (functionMatch) {
|
|
59
|
+
const { index } = functionMatch;
|
|
60
|
+
const matchedFunction = functionMatch[0];
|
|
61
|
+
if ((index || 0) > 0) {
|
|
62
|
+
parts.push(...remainingValue.substring(0, index).trim().split(/\s+/));
|
|
63
|
+
}
|
|
64
|
+
parts.push(matchedFunction);
|
|
65
|
+
remainingValue = remainingValue.substring((index || 0) + matchedFunction.length);
|
|
66
|
+
} else {
|
|
67
|
+
parts.push(...remainingValue.trim().split(/\s+/));
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return parts.filter((part) => part !== "");
|
|
72
|
+
};
|
|
73
|
+
var classifyArgsOfFunction = (value) => {
|
|
74
|
+
let nestedLevel = 0;
|
|
75
|
+
const values = [];
|
|
76
|
+
let currentValue = "";
|
|
77
|
+
for (const char of value) {
|
|
78
|
+
if (char === "(")
|
|
79
|
+
nestedLevel++;
|
|
80
|
+
else if (char === ")")
|
|
81
|
+
nestedLevel--;
|
|
82
|
+
if (char === "," && nestedLevel === 0) {
|
|
83
|
+
values.push(currentValue.trim());
|
|
84
|
+
currentValue = "";
|
|
85
|
+
} else {
|
|
86
|
+
currentValue += char;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (currentValue)
|
|
90
|
+
values.push(currentValue.trim());
|
|
91
|
+
return values.map((val) => {
|
|
92
|
+
const numberUnitMatch = val.match(/^(-?\d+(?:\.\d+)?)([a-zA-Z%]*)$/);
|
|
93
|
+
if (numberUnitMatch) {
|
|
94
|
+
const number = parseFloat(numberUnitMatch[1]);
|
|
95
|
+
const unit = numberUnitMatch[2];
|
|
96
|
+
return unit ? { number, unit } : { number };
|
|
97
|
+
}
|
|
98
|
+
const numberMatch = val.match(/^(\d+(?:\.\d+)?)$/);
|
|
99
|
+
if (numberMatch) {
|
|
100
|
+
const number = parseFloat(numberMatch[1]);
|
|
101
|
+
return { number };
|
|
102
|
+
}
|
|
103
|
+
return { unit: val };
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
var isColorValue = (value) => {
|
|
107
|
+
if (Object.keys(NoReactInternals.colorNames).includes(value)) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
const matchers = getColorMatchers();
|
|
111
|
+
return matchers.rgb?.test(value) || matchers.rgba?.test(value) || matchers.hsl?.test(value) || matchers.hsla?.test(value) || matchers.hex3?.test(value) || matchers.hex4?.test(value) || matchers.hex5?.test(value) || matchers.hex6?.test(value) || matchers.hex8?.test(value) || matchers.oklch?.test(value) || matchers.oklab?.test(value) || matchers.lab?.test(value) || matchers.lch?.test(value) || matchers.hwb?.test(value);
|
|
112
|
+
};
|
|
113
|
+
var classifyParts = (parts) => {
|
|
114
|
+
return parts.map((part) => {
|
|
115
|
+
if (isColorValue(part)) {
|
|
116
|
+
return { color: part };
|
|
117
|
+
}
|
|
118
|
+
const functionMatch = part.match(/([a-zA-Z-]+)\(([^)]+)\)/);
|
|
119
|
+
if (functionMatch) {
|
|
120
|
+
const functionName = functionMatch[1];
|
|
121
|
+
const functionValues = classifyArgsOfFunction(functionMatch[2]);
|
|
122
|
+
return { function: { name: functionName, values: functionValues } };
|
|
123
|
+
}
|
|
124
|
+
const numberUnitMatch = part.match(/^(-?\d+(?:\.\d+)?)([a-zA-Z%]*)$/);
|
|
125
|
+
if (numberUnitMatch) {
|
|
126
|
+
const number = parseFloat(numberUnitMatch[1]);
|
|
127
|
+
const unit = numberUnitMatch[2];
|
|
128
|
+
return unit ? { number, unit } : { number };
|
|
129
|
+
}
|
|
130
|
+
const numberMatch = part.match(/^(-?\d+(?:\.\d+)?)$/);
|
|
131
|
+
if (numberMatch) {
|
|
132
|
+
const number = parseFloat(numberMatch[1]);
|
|
133
|
+
return { number };
|
|
134
|
+
}
|
|
135
|
+
return { unit: part };
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
var breakDownValueIntoUnitNumberAndFunctions = (value) => {
|
|
139
|
+
if (typeof value === "number") {
|
|
140
|
+
return [{ number: value }];
|
|
141
|
+
}
|
|
142
|
+
if (typeof value !== "string") {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
const valueParts = extractOrderedPartsOfValue(value);
|
|
146
|
+
return classifyParts(valueParts);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// src/transformation-helpers/interpolate-styles/index.tsx
|
|
150
|
+
var interpolatedPropertyPart = ({
|
|
151
|
+
inputValue,
|
|
152
|
+
inputRange,
|
|
153
|
+
initialStylePropertyPart,
|
|
154
|
+
finalStylePropertyPart,
|
|
155
|
+
initialStyleProperty,
|
|
156
|
+
finalStyleProperty,
|
|
157
|
+
options
|
|
158
|
+
}) => {
|
|
159
|
+
if (finalStylePropertyPart === undefined) {
|
|
160
|
+
throw new TypeError(`The start and end values must be of the same type. Start value: ${initialStyleProperty}, end value: ${finalStyleProperty}`);
|
|
161
|
+
}
|
|
162
|
+
if (initialStylePropertyPart.color) {
|
|
163
|
+
if (!finalStylePropertyPart.color) {
|
|
164
|
+
throw new TypeError(`The start and end values must be of the same type. Start value: ${initialStyleProperty}, end value: ${finalStyleProperty}`);
|
|
165
|
+
}
|
|
166
|
+
const interpolatedColor = interpolateColors(inputValue, inputRange, [
|
|
167
|
+
initialStylePropertyPart.color,
|
|
168
|
+
finalStylePropertyPart.color
|
|
169
|
+
]);
|
|
170
|
+
return `${interpolatedColor}`;
|
|
171
|
+
}
|
|
172
|
+
if (initialStylePropertyPart.function) {
|
|
173
|
+
if (!finalStylePropertyPart?.function || initialStylePropertyPart.function.name !== finalStylePropertyPart.function?.name) {
|
|
174
|
+
throw new TypeError(`The start and end values must be of the same type. Start value: ${initialStyleProperty}, end value: ${finalStyleProperty}`);
|
|
175
|
+
}
|
|
176
|
+
const endValuePartFunction = finalStylePropertyPart.function;
|
|
177
|
+
const endValuePartFunctionArgs = endValuePartFunction.values || [];
|
|
178
|
+
const interpolatedFunctionArgs = initialStylePropertyPart.function.values.reduce((acc, startValuePartFunctionArg, index) => {
|
|
179
|
+
const endValuePartFunctionArg = endValuePartFunctionArgs[index];
|
|
180
|
+
const interpolatedArg = interpolatedPropertyPart({
|
|
181
|
+
inputValue,
|
|
182
|
+
inputRange,
|
|
183
|
+
initialStylePropertyPart: startValuePartFunctionArg,
|
|
184
|
+
finalStylePropertyPart: endValuePartFunctionArg,
|
|
185
|
+
initialStyleProperty,
|
|
186
|
+
finalStyleProperty,
|
|
187
|
+
options
|
|
188
|
+
});
|
|
189
|
+
return `${acc}, ${interpolatedArg}`;
|
|
190
|
+
}, "");
|
|
191
|
+
return `${initialStylePropertyPart.function.name}(${interpolatedFunctionArgs.slice(2)})`;
|
|
192
|
+
}
|
|
193
|
+
if (typeof initialStylePropertyPart.number === "undefined") {
|
|
194
|
+
if (initialStylePropertyPart.unit !== finalStylePropertyPart.unit) {
|
|
195
|
+
throw new TypeError(`Non-animatable values cannot be interpolated. Start value: ${initialStyleProperty}, end value: ${finalStyleProperty}`);
|
|
196
|
+
}
|
|
197
|
+
return `${initialStylePropertyPart.unit}`;
|
|
198
|
+
}
|
|
199
|
+
if (initialStylePropertyPart.unit !== finalStylePropertyPart.unit && initialStylePropertyPart.number !== 0 && finalStylePropertyPart.number !== 0) {
|
|
200
|
+
throw new TypeError(`The units of the start and end values must match. Start value: ${initialStyleProperty}, end value: ${finalStyleProperty}`);
|
|
201
|
+
}
|
|
202
|
+
const startNumber = initialStylePropertyPart.number;
|
|
203
|
+
const endNumber = finalStylePropertyPart.number || 0;
|
|
204
|
+
const interpolatedNumber = interpolate(inputValue, inputRange, [startNumber, endNumber], options);
|
|
205
|
+
const interpolatedUnit = initialStylePropertyPart.unit || finalStylePropertyPart.unit || "";
|
|
206
|
+
if (!interpolatedUnit) {
|
|
207
|
+
return interpolatedNumber;
|
|
208
|
+
}
|
|
209
|
+
return `${interpolatedNumber}${interpolatedUnit}`;
|
|
210
|
+
};
|
|
211
|
+
var interpolateProperty = ({
|
|
212
|
+
inputValue,
|
|
213
|
+
inputRange,
|
|
214
|
+
initialStyleProperty,
|
|
215
|
+
finalStyleProperty,
|
|
216
|
+
options
|
|
217
|
+
}) => {
|
|
218
|
+
if (typeof initialStyleProperty !== typeof finalStyleProperty && initialStyleProperty !== 0 && finalStyleProperty !== 0) {
|
|
219
|
+
throw new TypeError(`The start and end values must be of the same type. Start value: ${initialStyleProperty}, end value: ${finalStyleProperty}`);
|
|
220
|
+
}
|
|
221
|
+
const initialStylePropertyParts = breakDownValueIntoUnitNumberAndFunctions(initialStyleProperty);
|
|
222
|
+
const finalStylePropertyParts = breakDownValueIntoUnitNumberAndFunctions(finalStyleProperty);
|
|
223
|
+
if (initialStylePropertyParts.length !== finalStylePropertyParts.length) {
|
|
224
|
+
throw new TypeError(`The start and end values must have the same structure. Start value: ${initialStyleProperty}, end value: ${finalStyleProperty}`);
|
|
225
|
+
}
|
|
226
|
+
const interpolatedValue = initialStylePropertyParts.reduce((acc, initialStylePropertyPart, index) => {
|
|
227
|
+
return `${acc} ${interpolatedPropertyPart({
|
|
228
|
+
inputValue,
|
|
229
|
+
inputRange,
|
|
230
|
+
initialStylePropertyPart,
|
|
231
|
+
finalStylePropertyPart: finalStylePropertyParts[index],
|
|
232
|
+
initialStyleProperty,
|
|
233
|
+
finalStyleProperty,
|
|
234
|
+
options
|
|
235
|
+
})}`;
|
|
236
|
+
}, "");
|
|
237
|
+
return interpolatedValue.slice(1);
|
|
238
|
+
};
|
|
239
|
+
var interpolateStylesFunction = ({
|
|
240
|
+
inputValue,
|
|
241
|
+
inputRange,
|
|
242
|
+
initialStyle,
|
|
243
|
+
finalStyle,
|
|
244
|
+
options
|
|
245
|
+
}) => {
|
|
246
|
+
const [startingValue, endingValue] = inputRange;
|
|
247
|
+
return Object.keys(initialStyle).reduce((acc, key) => {
|
|
248
|
+
const value = finalStyle[key];
|
|
249
|
+
if (value === undefined || value === null) {
|
|
250
|
+
return {
|
|
251
|
+
...acc,
|
|
252
|
+
[key]: initialStyle[key]
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
const finalStyleValue = interpolateProperty({
|
|
256
|
+
inputValue,
|
|
257
|
+
inputRange: [startingValue, endingValue],
|
|
258
|
+
initialStyleProperty: initialStyle[key],
|
|
259
|
+
finalStyleProperty: finalStyle[key],
|
|
260
|
+
options
|
|
261
|
+
});
|
|
262
|
+
if (!isNaN(Number(finalStyleValue))) {
|
|
263
|
+
return {
|
|
264
|
+
...acc,
|
|
265
|
+
[key]: Number(finalStyleValue)
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
...acc,
|
|
270
|
+
[key]: finalStyleValue
|
|
271
|
+
};
|
|
272
|
+
}, {});
|
|
273
|
+
};
|
|
274
|
+
function checkInputRange(arr) {
|
|
275
|
+
if (arr.length < 2) {
|
|
276
|
+
throw new Error("inputRange must have at least 2 elements");
|
|
277
|
+
}
|
|
278
|
+
for (let index = 0;index < arr.length; index++) {
|
|
279
|
+
if (typeof arr[index] !== "number") {
|
|
280
|
+
throw new Error(`inputRange must contain only numbers`);
|
|
281
|
+
}
|
|
282
|
+
if (arr[index] === -Infinity || arr[index] === Infinity) {
|
|
283
|
+
throw new Error(`inputRange must contain only finite numbers, but got [${arr.join(",")}]`);
|
|
284
|
+
}
|
|
285
|
+
if (index > 0 && !(arr[index] > arr[index - 1])) {
|
|
286
|
+
throw new Error(`inputRange must be strictly monotonically non-decreasing but got [${arr.join(",")}]`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function checkStylesRange(arr) {
|
|
291
|
+
if (arr.length < 2) {
|
|
292
|
+
throw new Error("outputStyles must have at least 2 elements");
|
|
293
|
+
}
|
|
294
|
+
for (const index in arr) {
|
|
295
|
+
if (typeof arr[index] !== "object") {
|
|
296
|
+
throw new Error("outputStyles must contain only objects");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
var interpolateStyles = (input, inputRange, outputStylesRange, options) => {
|
|
301
|
+
if (typeof input === "undefined") {
|
|
302
|
+
throw new Error("input can not be undefined");
|
|
303
|
+
}
|
|
304
|
+
if (typeof inputRange === "undefined") {
|
|
305
|
+
throw new Error("inputRange can not be undefined");
|
|
306
|
+
}
|
|
307
|
+
if (typeof outputStylesRange === "undefined") {
|
|
308
|
+
throw new Error("outputRange can not be undefined");
|
|
309
|
+
}
|
|
310
|
+
if (inputRange.length !== outputStylesRange.length) {
|
|
311
|
+
throw new Error("inputRange (" + inputRange.length + ") and outputStylesRange (" + outputStylesRange.length + ") must have the same length");
|
|
312
|
+
}
|
|
313
|
+
checkInputRange(inputRange);
|
|
314
|
+
checkStylesRange(outputStylesRange);
|
|
315
|
+
let startIndex = inputRange.findIndex((step) => input < step) - 1;
|
|
316
|
+
if (startIndex === -1) {
|
|
317
|
+
startIndex = 0;
|
|
318
|
+
}
|
|
319
|
+
if (startIndex === -2) {
|
|
320
|
+
startIndex = inputRange.length - 2;
|
|
321
|
+
}
|
|
322
|
+
const endIndex = startIndex + 1;
|
|
323
|
+
const startingValue = inputRange[startIndex];
|
|
324
|
+
const endingValue = inputRange[endIndex];
|
|
325
|
+
const initialStyle = outputStylesRange[startIndex];
|
|
326
|
+
const finalStyle = outputStylesRange[endIndex];
|
|
327
|
+
const easing = options?.easing ?? ((num) => num);
|
|
328
|
+
const extrapolateLeft = options?.extrapolateLeft ?? "extend";
|
|
329
|
+
const extrapolateRight = options?.extrapolateRight ?? "extend";
|
|
330
|
+
return interpolateStylesFunction({
|
|
331
|
+
inputValue: input,
|
|
332
|
+
inputRange: [startingValue, endingValue],
|
|
333
|
+
initialStyle,
|
|
334
|
+
finalStyle,
|
|
335
|
+
options: {
|
|
336
|
+
easing,
|
|
337
|
+
extrapolateLeft,
|
|
338
|
+
extrapolateRight
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
// src/type.ts
|
|
343
|
+
var lengthUnits = [
|
|
344
|
+
"px",
|
|
345
|
+
"em",
|
|
346
|
+
"rem",
|
|
347
|
+
"pt",
|
|
348
|
+
"cap",
|
|
349
|
+
"ch",
|
|
350
|
+
"ex",
|
|
351
|
+
"ic",
|
|
352
|
+
"lh",
|
|
353
|
+
"rlh",
|
|
354
|
+
"vmax",
|
|
355
|
+
"vmin",
|
|
356
|
+
"vb",
|
|
357
|
+
"vi",
|
|
358
|
+
"cqw",
|
|
359
|
+
"vh",
|
|
360
|
+
"vw",
|
|
361
|
+
"cqh",
|
|
362
|
+
"cqi",
|
|
363
|
+
"cqb",
|
|
364
|
+
"cqmin",
|
|
365
|
+
"cqmax",
|
|
366
|
+
"cm",
|
|
367
|
+
"mm",
|
|
368
|
+
"Q",
|
|
369
|
+
"in",
|
|
370
|
+
"pc"
|
|
371
|
+
];
|
|
372
|
+
var angleUnits = ["rad", "deg", "grad", "turn"];
|
|
373
|
+
var lengthPercentageUnits = ["%", ...lengthUnits];
|
|
374
|
+
|
|
375
|
+
// src/transformation-helpers/make-transform/is-unit-with-string.ts
|
|
376
|
+
var isUnitWithString = (input, units) => {
|
|
377
|
+
if (typeof input !== "string") {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
if (!units.find((u) => input.endsWith(u))) {
|
|
381
|
+
throw new Error(`input ${input} does not end with a valid unit. Valid units are: ${units.join(", ")}`);
|
|
382
|
+
}
|
|
383
|
+
const match = input.match(/([0-9.]+)([a-z%]+)/);
|
|
384
|
+
if (!match) {
|
|
385
|
+
throw new Error(`input ${input} is not a valid transform. Must be a number or a string ending in one of the following units: ${lengthUnits.join(", ")}`);
|
|
386
|
+
}
|
|
387
|
+
return true;
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// src/transformation-helpers/make-transform/transform-functions.ts
|
|
391
|
+
var checkNumber = ({
|
|
392
|
+
num,
|
|
393
|
+
param,
|
|
394
|
+
api
|
|
395
|
+
}) => {
|
|
396
|
+
if (typeof num === "undefined") {
|
|
397
|
+
throw new TypeError(`Argument passed to "${api}" for param "${param}" is undefined`);
|
|
398
|
+
}
|
|
399
|
+
if (typeof num !== "number") {
|
|
400
|
+
throw new TypeError(`Argument passed to "${api}" for param "${param}" is ${JSON.stringify(num)}`);
|
|
401
|
+
}
|
|
402
|
+
if (!Number.isFinite(num)) {
|
|
403
|
+
throw new TypeError(`Argument passed to "${api}" for param "${param}" is ${JSON.stringify(num)} (must be finite)`);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
function matrix(a, b, c, d, tx, ty) {
|
|
407
|
+
checkNumber({ num: a, param: "a", api: "matrix" });
|
|
408
|
+
checkNumber({ num: b, param: "b", api: "matrix" });
|
|
409
|
+
checkNumber({ num: c, param: "c", api: "matrix" });
|
|
410
|
+
checkNumber({ num: d, param: "d", api: "matrix" });
|
|
411
|
+
checkNumber({ num: tx, param: "tx", api: "matrix" });
|
|
412
|
+
checkNumber({ num: ty, param: "ty", api: "matrix" });
|
|
413
|
+
return `matrix(${a}, ${b}, ${c}, ${d}, ${tx}, ${ty})`;
|
|
414
|
+
}
|
|
415
|
+
function matrix3d(a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4) {
|
|
416
|
+
checkNumber({ num: a1, param: "a1", api: "matrix3d" });
|
|
417
|
+
checkNumber({ num: b1, param: "b1", api: "matrix3d" });
|
|
418
|
+
checkNumber({ num: c1, param: "c1", api: "matrix3d" });
|
|
419
|
+
checkNumber({ num: d1, param: "d1", api: "matrix3d" });
|
|
420
|
+
checkNumber({ num: a2, param: "a2", api: "matrix3d" });
|
|
421
|
+
checkNumber({ num: b2, param: "b2", api: "matrix3d" });
|
|
422
|
+
checkNumber({ num: c2, param: "c2", api: "matrix3d" });
|
|
423
|
+
checkNumber({ num: d2, param: "d2", api: "matrix3d" });
|
|
424
|
+
checkNumber({ num: a3, param: "a3", api: "matrix3d" });
|
|
425
|
+
checkNumber({ num: b3, param: "b3", api: "matrix3d" });
|
|
426
|
+
checkNumber({ num: c3, param: "c3", api: "matrix3d" });
|
|
427
|
+
checkNumber({ num: d3, param: "d3", api: "matrix3d" });
|
|
428
|
+
checkNumber({ num: a4, param: "a4", api: "matrix3d" });
|
|
429
|
+
checkNumber({ num: b4, param: "b4", api: "matrix3d" });
|
|
430
|
+
checkNumber({ num: c4, param: "c4", api: "matrix3d" });
|
|
431
|
+
checkNumber({ num: d4, param: "d4", api: "matrix3d" });
|
|
432
|
+
return `matrix3d(${a1}, ${b1}, ${c1}, ${d1}, ${a2}, ${b2}, ${c2}, ${d2}, ${a3}, ${b3}, ${c3}, ${d3}, ${a4}, ${b4}, ${c4}, ${d4})`;
|
|
433
|
+
}
|
|
434
|
+
function perspective(length, unit = "px") {
|
|
435
|
+
if (isUnitWithString(length, lengthUnits)) {
|
|
436
|
+
return `perspective(${length})`;
|
|
437
|
+
}
|
|
438
|
+
checkNumber({ num: length, param: "length", api: "perspective" });
|
|
439
|
+
return `perspective(${length}${unit})`;
|
|
440
|
+
}
|
|
441
|
+
function rotate(angle, unit = "deg") {
|
|
442
|
+
if (isUnitWithString(angle, angleUnits)) {
|
|
443
|
+
return `rotate(${angle})`;
|
|
444
|
+
}
|
|
445
|
+
checkNumber({ num: angle, param: "angle", api: "rotate" });
|
|
446
|
+
return `rotate(${angle}${unit})`;
|
|
447
|
+
}
|
|
448
|
+
function rotate3d(x, y, z, angle, unit = "deg") {
|
|
449
|
+
checkNumber({ num: x, param: "x", api: "rotate3d" });
|
|
450
|
+
checkNumber({ num: y, param: "y", api: "rotate3d" });
|
|
451
|
+
checkNumber({ num: z, param: "z", api: "rotate3d" });
|
|
452
|
+
if (isUnitWithString(angle, angleUnits)) {
|
|
453
|
+
return `rotate3d(${x}, ${y}, ${z}, ${angle})`;
|
|
454
|
+
}
|
|
455
|
+
checkNumber({ num: angle, param: "angle", api: "rotate3d" });
|
|
456
|
+
return `rotate3d(${x}, ${y}, ${z}, ${angle}${unit})`;
|
|
457
|
+
}
|
|
458
|
+
function rotateX(angle, unit = "deg") {
|
|
459
|
+
if (isUnitWithString(angle, angleUnits)) {
|
|
460
|
+
return `rotateX(${angle})`;
|
|
461
|
+
}
|
|
462
|
+
checkNumber({ num: angle, param: "angle", api: "rotateX" });
|
|
463
|
+
return `rotateX(${angle}${unit})`;
|
|
464
|
+
}
|
|
465
|
+
function rotateY(angle, unit = "deg") {
|
|
466
|
+
if (isUnitWithString(angle, angleUnits)) {
|
|
467
|
+
return `rotateY(${angle})`;
|
|
468
|
+
}
|
|
469
|
+
checkNumber({ num: angle, param: "angle", api: "rotateY" });
|
|
470
|
+
return `rotateY(${angle}${unit})`;
|
|
471
|
+
}
|
|
472
|
+
function rotateZ(angle, unit = "deg") {
|
|
473
|
+
if (isUnitWithString(angle, angleUnits)) {
|
|
474
|
+
return `rotateZ(${angle})`;
|
|
475
|
+
}
|
|
476
|
+
checkNumber({ num: angle, param: "angle", api: "rotateZ" });
|
|
477
|
+
return `rotateZ(${angle}${unit})`;
|
|
478
|
+
}
|
|
479
|
+
function scale(x, y = x) {
|
|
480
|
+
checkNumber({ num: x, param: "x", api: "scale" });
|
|
481
|
+
return `scale(${x}, ${y})`;
|
|
482
|
+
}
|
|
483
|
+
function scale3d(x, y, z) {
|
|
484
|
+
checkNumber({ num: x, param: "x", api: "scale3d" });
|
|
485
|
+
checkNumber({ num: y, param: "y", api: "scale3d" });
|
|
486
|
+
checkNumber({ num: z, param: "z", api: "scale3d" });
|
|
487
|
+
return `scale3d(${x}, ${y}, ${z})`;
|
|
488
|
+
}
|
|
489
|
+
function scaleX(x) {
|
|
490
|
+
checkNumber({ num: x, param: "x", api: "scaleX" });
|
|
491
|
+
return `scaleX(${x})`;
|
|
492
|
+
}
|
|
493
|
+
function scaleY(y) {
|
|
494
|
+
checkNumber({ num: y, param: "y", api: "scaleY" });
|
|
495
|
+
return `scaleY(${y})`;
|
|
496
|
+
}
|
|
497
|
+
function scaleZ(z) {
|
|
498
|
+
checkNumber({ num: z, param: "z", api: "scaleZ" });
|
|
499
|
+
return `scaleZ(${z})`;
|
|
500
|
+
}
|
|
501
|
+
function skew(...args) {
|
|
502
|
+
const [arg1, arg2, arg3, arg4] = args;
|
|
503
|
+
if (arguments.length === 1) {
|
|
504
|
+
if (isUnitWithString(arg1, angleUnits)) {
|
|
505
|
+
return `skew(${arg1}, ${arg1})`;
|
|
506
|
+
}
|
|
507
|
+
checkNumber({ num: arg1, param: "angle", api: "skew" });
|
|
508
|
+
return `skew(${arg1}deg, ${arg1}deg)`;
|
|
509
|
+
}
|
|
510
|
+
if (arguments.length === 2) {
|
|
511
|
+
if (isUnitWithString(arg1, angleUnits) && isUnitWithString(arg2, angleUnits)) {
|
|
512
|
+
return `skew(${arg1}, ${arg2})`;
|
|
513
|
+
}
|
|
514
|
+
if (typeof arg1 === "number" && typeof arg2 !== "number") {
|
|
515
|
+
checkNumber({ num: arg1, param: "angle", api: "skew" });
|
|
516
|
+
return `skew(${arg1}${arg2}, ${arg1}${arg2})`;
|
|
517
|
+
}
|
|
518
|
+
if (typeof arg1 === "number" && typeof arg2 === "number") {
|
|
519
|
+
checkNumber({ num: arg1, param: "angle", api: "skew" });
|
|
520
|
+
checkNumber({ num: arg2, param: "angle", api: "skew" });
|
|
521
|
+
return `skew(${arg1}deg, ${arg2}deg)`;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
if (arguments.length === 4) {
|
|
525
|
+
if (typeof arg1 === "number" && isUnitWithString(arg2, angleUnits) && typeof arg3 === "number" && isUnitWithString(arg4, angleUnits)) {
|
|
526
|
+
checkNumber({ num: arg1, param: "angle", api: "skew" });
|
|
527
|
+
checkNumber({ num: arg3, param: "angle", api: "skew" });
|
|
528
|
+
return `skew(${arg1}${arg2}, ${arg3}${arg4})`;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
throw new TypeError([
|
|
532
|
+
"skew() supports only the following signatures:",
|
|
533
|
+
"skew(angle: AngleUnitString): string;",
|
|
534
|
+
"skew(angle: AngleUnitString, angle2: AngleUnitString): string;",
|
|
535
|
+
"skew(angle: number, unit: AngleUnit): string;",
|
|
536
|
+
"skew(angleX: number, angleY: number): string;",
|
|
537
|
+
"skew(angleX: number, unitX: AngleUnit, angleY: number, unitY: AngleUnit): string;"
|
|
538
|
+
].join(`
|
|
539
|
+
`));
|
|
540
|
+
}
|
|
541
|
+
function skewX(angle, unit = "deg") {
|
|
542
|
+
if (isUnitWithString(angle, angleUnits)) {
|
|
543
|
+
return `skewX(${angle})`;
|
|
544
|
+
}
|
|
545
|
+
checkNumber({ num: angle, param: "angle", api: "skewX" });
|
|
546
|
+
return `skewX(${angle}${unit})`;
|
|
547
|
+
}
|
|
548
|
+
function skewY(angle, unit = "deg") {
|
|
549
|
+
if (isUnitWithString(angle, angleUnits)) {
|
|
550
|
+
return `skewY(${angle})`;
|
|
551
|
+
}
|
|
552
|
+
checkNumber({ num: angle, param: "angle", api: "skewY" });
|
|
553
|
+
return `skewY(${angle}${unit})`;
|
|
554
|
+
}
|
|
555
|
+
function translate(...args) {
|
|
556
|
+
const [arg1, arg2, arg3, arg4] = args;
|
|
557
|
+
if (arguments.length === 1) {
|
|
558
|
+
if (isUnitWithString(arg1, lengthPercentageUnits)) {
|
|
559
|
+
return `translate(${arg1})`;
|
|
560
|
+
}
|
|
561
|
+
checkNumber({ num: arg1, param: "x", api: "translate" });
|
|
562
|
+
return `translate(${arg1}px)`;
|
|
563
|
+
}
|
|
564
|
+
if (arguments.length === 2) {
|
|
565
|
+
if (typeof arg1 === "number" && typeof arg2 === "number") {
|
|
566
|
+
checkNumber({ num: arg1, param: "x", api: "translate" });
|
|
567
|
+
checkNumber({ num: arg2, param: "y", api: "translate" });
|
|
568
|
+
return `translate(${arg1}px, ${arg2}px)`;
|
|
569
|
+
}
|
|
570
|
+
if (isUnitWithString(arg1, lengthPercentageUnits) && isUnitWithString(arg2, lengthPercentageUnits)) {
|
|
571
|
+
return `translate(${arg1}, ${arg2})`;
|
|
572
|
+
}
|
|
573
|
+
if (typeof arg1 === "number" && typeof arg2 !== "number") {
|
|
574
|
+
checkNumber({ num: arg1, param: "x", api: "translate" });
|
|
575
|
+
return `translate(${arg1}${arg2})`;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
if (arguments.length === 4) {
|
|
579
|
+
if (typeof arg1 === "number" && typeof arg3 === "number") {
|
|
580
|
+
checkNumber({ num: arg1, param: "x", api: "translate" });
|
|
581
|
+
checkNumber({ num: arg3, param: "y", api: "translate" });
|
|
582
|
+
return `translate(${arg1}${arg2}, ${arg3}${arg4})`;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
throw new TypeError([
|
|
586
|
+
`translate() supports only the following signatures:`,
|
|
587
|
+
`translate(x: LengthPercentageUnitString)`,
|
|
588
|
+
`translate(x: number)`,
|
|
589
|
+
`translate(x: number, y: number)`,
|
|
590
|
+
`translate(translation: number, unit: LengthPercentageUnit)`,
|
|
591
|
+
`translate(x: number, unitX: LengthPercentageUnit, y: number, unitY: LengthPercentageUnit): string;`
|
|
592
|
+
].join(`
|
|
593
|
+
`));
|
|
594
|
+
}
|
|
595
|
+
function translate3d(...args) {
|
|
596
|
+
if (arguments.length === 3) {
|
|
597
|
+
const [x, y, z] = args;
|
|
598
|
+
const vars = [x, y, z].map((arg, i) => {
|
|
599
|
+
if (isUnitWithString(arg, lengthPercentageUnits)) {
|
|
600
|
+
return arg;
|
|
601
|
+
}
|
|
602
|
+
checkNumber({
|
|
603
|
+
num: arg,
|
|
604
|
+
param: i === 0 ? "x" : i === 1 ? "y" : "z",
|
|
605
|
+
api: "translate3d"
|
|
606
|
+
});
|
|
607
|
+
if (typeof arg === "number") {
|
|
608
|
+
return `${arg}px`;
|
|
609
|
+
}
|
|
610
|
+
return arg;
|
|
611
|
+
});
|
|
612
|
+
return `translate3d(${vars.join(", ")})`;
|
|
613
|
+
}
|
|
614
|
+
if (arguments.length === 6) {
|
|
615
|
+
const [x, unitX, y, unitY, z, unitZ] = args;
|
|
616
|
+
if (typeof x === "number" && typeof y === "number" && typeof z === "number") {
|
|
617
|
+
checkNumber({ num: x, param: "x", api: "translate3d" });
|
|
618
|
+
checkNumber({ num: y, param: "y", api: "translate3d" });
|
|
619
|
+
checkNumber({ num: z, param: "z", api: "translate3d" });
|
|
620
|
+
return `translate3d(${x}${unitX}, ${y}${unitY}, ${z}${unitZ})`;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
throw new TypeError([
|
|
624
|
+
`translate3d() supports only the following signatures:`,
|
|
625
|
+
`translate3d(x: LengthPercentageUnitString, y: LengthPercentageUnitString, z: LengthPercentageUnitString)`,
|
|
626
|
+
`translate3d(x: number, unitX: LengthPercentageUnit, y: number, unitY: LengthPercentageUnit, z: number, unitZ: LengthUnit)`
|
|
627
|
+
].join(`
|
|
628
|
+
`));
|
|
629
|
+
}
|
|
630
|
+
function translateX(x, unit = "px") {
|
|
631
|
+
if (isUnitWithString(x, lengthPercentageUnits)) {
|
|
632
|
+
return `translateX(${x})`;
|
|
633
|
+
}
|
|
634
|
+
checkNumber({ num: x, param: "x", api: "translateX" });
|
|
635
|
+
return `translateX(${x}${unit})`;
|
|
636
|
+
}
|
|
637
|
+
function translateY(y, unit = "px") {
|
|
638
|
+
if (isUnitWithString(y, lengthPercentageUnits)) {
|
|
639
|
+
return `translateY(${y})`;
|
|
640
|
+
}
|
|
641
|
+
checkNumber({ num: y, param: "y", api: "translateY" });
|
|
642
|
+
return `translateY(${y}${unit})`;
|
|
643
|
+
}
|
|
644
|
+
function translateZ(z, unit = "px") {
|
|
645
|
+
if (isUnitWithString(z, lengthUnits)) {
|
|
646
|
+
return `translateZ(${z})`;
|
|
647
|
+
}
|
|
648
|
+
checkNumber({ num: z, param: "z", api: "translateZ" });
|
|
649
|
+
return `translateZ(${z}${unit})`;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// src/transformation-helpers/make-transform/index.ts
|
|
653
|
+
var makeTransform = (transforms) => {
|
|
654
|
+
return transforms.join(" ");
|
|
655
|
+
};
|
|
656
|
+
export {
|
|
657
|
+
translateZ,
|
|
658
|
+
translateY,
|
|
659
|
+
translateX,
|
|
660
|
+
translate3d,
|
|
661
|
+
translate,
|
|
662
|
+
skewY,
|
|
663
|
+
skewX,
|
|
664
|
+
skew,
|
|
665
|
+
scaleZ,
|
|
666
|
+
scaleY,
|
|
667
|
+
scaleX,
|
|
668
|
+
scale3d,
|
|
669
|
+
scale,
|
|
670
|
+
rotateZ,
|
|
671
|
+
rotateY,
|
|
672
|
+
rotateX,
|
|
673
|
+
rotate3d,
|
|
674
|
+
rotate,
|
|
675
|
+
perspective,
|
|
676
|
+
matrix3d,
|
|
677
|
+
matrix,
|
|
678
|
+
makeTransform,
|
|
679
|
+
interpolateStyles
|
|
680
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./transformation-helpers/interpolate-styles/index.js"), exports);
|
|
18
|
+
__exportStar(require("./transformation-helpers/make-transform"), exports);
|