remotion
Version:
Make videos programmatically
71 lines (70 loc) • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.interpolateTranslate = void 0;
const interpolate_js_1 = require("./interpolate.js");
const pixelValueRegex = /^([+-]?(?:\d+\.?\d*|\.\d+))px$/;
const parseTranslate = (value) => {
if (typeof value !== 'string') {
throw new TypeError(`outputRange must contain only strings, but got ${typeof value}`);
}
const parts = value.trim().split(/\s+/);
if (parts.length < 1 || parts.length > 3 || parts[0] === '') {
throw new TypeError(`translate values must contain 1 to 3 pixel values, but got "${value}"`);
}
return parts.map((part) => {
const match = pixelValueRegex.exec(part);
if (match === null) {
throw new TypeError(`interpolateTranslate() only supports px values, but got "${part}" in "${value}"`);
}
return Number(match[1]);
});
};
/*
* @description Allows you to map a range of values to CSS translate values using pixel units.
* @see [Documentation](https://remotion.dev/docs/interpolate-translate)
*/
const interpolateTranslate = (input, inputRange, outputRange, options) => {
var _a;
if (typeof input === 'undefined') {
throw new TypeError('input can not be undefined');
}
if (typeof inputRange === 'undefined') {
throw new TypeError('inputRange can not be undefined');
}
if (typeof outputRange === 'undefined') {
throw new TypeError('outputRange can not be undefined');
}
if (inputRange.length !== outputRange.length) {
throw new TypeError('inputRange (' +
inputRange.length +
' values provided) and outputRange (' +
outputRange.length +
' values provided) must have the same length');
}
const parsedOutputRange = outputRange.map((translateValue) => parseTranslate(translateValue));
const firstValueLength = (_a = parsedOutputRange[0]) === null || _a === void 0 ? void 0 : _a.length;
if (firstValueLength === undefined) {
throw new TypeError('outputRange must have at least 1 element');
}
for (const parsedTranslate of parsedOutputRange) {
if (parsedTranslate.length !== firstValueLength) {
throw new TypeError(`All translate values must have the same number of pixel values, but got ${firstValueLength} and ${parsedTranslate.length}`);
}
}
return new Array(firstValueLength)
.fill(true)
.map((_, index) => {
const outputValues = [];
for (const translateValue of parsedOutputRange) {
const value = translateValue[index];
if (value === undefined) {
throw new TypeError(`All translate values must have the same number of pixel values, but got ${firstValueLength} and ${translateValue.length}`);
}
outputValues.push(value);
}
const interpolatedValue = (0, interpolate_js_1.interpolate)(input, inputRange, outputValues, options);
return `${interpolatedValue}px`;
})
.join(' ');
};
exports.interpolateTranslate = interpolateTranslate;