Skip to content

🎥 Make videos programmatically with React

remotion: Normalize rotate interpolation values

Jonny Burgerea10860a9cec
diff --git a/packages/core/src/interpolate-rotate.ts b/packages/core/src/interpolate-rotate.ts
index 11eb66ba23..ac6376b7e9 100644
--- a/packages/core/src/interpolate-rotate.ts
+++ b/packages/core/src/interpolate-rotate.ts
@@ -1,4 +1,5 @@
import {interpolate, type InterpolateOptions} from './interpolate.js';
+import {normalizeNumber} from './normalize-number.js';
export type InterpolateRotateOptions = InterpolateOptions;
@@ -67,5 +68,5 @@ export const interpolateRotate = (
options,
);
- return `${interpolatedValue}deg`;
+ return `${normalizeNumber(interpolatedValue)}deg`;
};
diff --git a/packages/core/src/interpolate-translate.ts b/packages/core/src/interpolate-translate.ts
index a5a2009dfd..3c0f280af0 100644
--- a/packages/core/src/interpolate-translate.ts
+++ b/packages/core/src/interpolate-translate.ts
@@ -1,4 +1,5 @@
import {interpolate, type InterpolateOptions} from './interpolate.js';
+import {normalizeNumber} from './normalize-number.js';
export type InterpolateTranslateOptions = InterpolateOptions;
@@ -102,7 +103,7 @@ export const interpolateTranslate = (
options,
);
- return `${interpolatedValue}px`;
+ return `${normalizeNumber(interpolatedValue)}px`;
})
.join(' ');
};
diff --git a/packages/core/src/normalize-number.ts b/packages/core/src/normalize-number.ts
new file mode 100644
index 0000000000..a0073a3bfd
--- /dev/null
+++ b/packages/core/src/normalize-number.ts
@@ -0,0 +1,3 @@
+export const normalizeNumber = (value: number): number => {
+ return Math.round(value * 1000000) / 1000000;
+};
diff --git a/packages/core/src/scale-value.ts b/packages/core/src/scale-value.ts
index 6c339c6a3e..0e28e3c746 100644
--- a/packages/core/src/scale-value.ts
+++ b/packages/core/src/scale-value.ts
@@ -1,11 +1,9 @@
+import {normalizeNumber} from './normalize-number.js';
+
export type ScaleValue = readonly [number, number, number];
const defaultScaleValue: ScaleValue = [1, 1, 1];
-const normalizeScaleNumber = (value: number): number => {
- return Math.round(value * 1000000) / 1000000;
-};
-
const parseScaleString = (value: string): ScaleValue | null => {
const parts = value.trim().split(/\s+/);
if (parts.length < 1 || parts.length > 3 || parts[0] === '') {
@@ -41,9 +39,9 @@ export const parseScaleValue = (value: unknown): ScaleValue => {
};
export const serializeScaleValue = ([x, y, z]: ScaleValue): number | string => {
- const normalizedX = normalizeScaleNumber(x);
- const normalizedY = normalizeScaleNumber(y);
- const normalizedZ = normalizeScaleNumber(z);
+ const normalizedX = normalizeNumber(x);
+ const normalizedY = normalizeNumber(y);
+ const normalizedZ = normalizeNumber(z);
if (normalizedX === normalizedY && normalizedZ === 1) {
return normalizedX;
diff --git a/packages/core/src/test/interpolateRotate.test.ts b/packages/core/src/test/interpolateRotate.test.ts
index b87ac42339..343b625ed2 100644
--- a/packages/core/src/test/interpolateRotate.test.ts
+++ b/packages/core/src/test/interpolateRotate.test.ts
@@ -44,6 +44,12 @@ describe('interpolateRotate()', () => {
).toBe('30deg');
});
+ test('rounds floating point noise', () => {
+ expect(interpolateRotate(0.1, [0, 1], ['0deg', '100.2deg'])).toBe(
+ '10.02deg',
+ );
+ });
+
test('returns the only output value for a single keyframe', () => {
expect(interpolateRotate(15, [0], ['10deg'])).toBe('10deg');
});
diff --git a/packages/core/src/test/interpolateTranslate.test.ts b/packages/core/src/test/interpolateTranslate.test.ts
index ebc4d01779..8a6ce872a2 100644
--- a/packages/core/src/test/interpolateTranslate.test.ts
+++ b/packages/core/src/test/interpolateTranslate.test.ts
@@ -54,6 +54,12 @@ describe('interpolateTranslate()', () => {
).toBe('30px 10px');
});
+ test('rounds floating point noise', () => {
+ expect(
+ interpolateTranslate(0.1, [0, 1], ['0px 0px', '100.2px 302.4px']),
+ ).toBe('10.02px 30.24px');
+ });
+
test('returns the only output value for a single keyframe', () => {
expect(interpolateTranslate(15, [0], ['10px 20px'])).toBe('10px 20px');
});
diff --git a/packages/core/src/test/scale-value.test.ts b/packages/core/src/test/scale-value.test.ts
index def1388123..8fa69c8459 100644
--- a/packages/core/src/test/scale-value.test.ts
+++ b/packages/core/src/test/scale-value.test.ts
@@ -27,3 +27,7 @@ test('serializes non-uniform scale as CSS scale syntax', () => {
expect(serializeScaleValue([2, 3, 1])).toBe('2 3');
expect(serializeScaleValue([2, 3, 4])).toBe('2 3 4');
});
+
+test('serializes scale values without floating point noise', () => {
+ expect(serializeScaleValue([0.1 + 0.2, 0.1 + 0.2, 1])).toBe(0.3);
+});
diff --git a/packages/studio/src/components/Timeline/TimelineRotationField.tsx b/packages/studio/src/components/Timeline/TimelineRotationField.tsx
index ab59274a48..8eac542e84 100644
--- a/packages/studio/src/components/Timeline/TimelineRotationField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineRotationField.tsx
@@ -6,7 +6,11 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
-import {draggerStyle, getDecimalPlaces} from './timeline-field-utils';
+import {
+ draggerStyle,
+ getDecimalPlaces,
+ normalizeTimelineNumber,
+} from './timeline-field-utils';
const unitPattern = /^([+-]?(?:\d+\.?\d*|\.\d+))(deg|rad|turn|grad)$/;
@@ -20,12 +24,12 @@ const unitToDegrees: Record<string, number> = {
const parseCssRotationToDegrees = (value: string): number => {
const match = value.trim().match(unitPattern);
if (match) {
- return Number(match[1]) * unitToDegrees[match[2]];
+ return normalizeTimelineNumber(Number(match[1]) * unitToDegrees[match[2]]);
}
try {
const m = new DOMMatrix(`rotate(${value})`);
- return Math.round(Math.atan2(m.b, m.a) * (180 / Math.PI) * 1e6) / 1e6;
+ return normalizeTimelineNumber(Math.atan2(m.b, m.a) * (180 / Math.PI));
} catch {
return 0;
}
@@ -58,7 +62,10 @@ export const TimelineRotationField: React.FC<{
}, [effectiveValue, isCssRotation]);
const serializeValue = useCallback(
- (value: number) => (isCssRotation ? `${value}deg` : value),
+ (value: number) => {
+ const normalized = normalizeTimelineNumber(value);
+ return isCssRotation ? `${normalized}deg` : normalized;
+ },
[isCssRotation],
);
@@ -120,7 +127,7 @@ export const TimelineRotationField: React.FC<{
const formatter = useCallback(
(v: number | string) => {
- const num = Number(v);
+ const num = normalizeTimelineNumber(Number(v));
const digits = Math.max(stepDecimals, getDecimalPlaces(num));
const formatted = digits === 0 ? String(num) : num.toFixed(digits);
return `${formatted}\u00B0`;
diff --git a/packages/studio/src/components/Timeline/TimelineScaleField.tsx b/packages/studio/src/components/Timeline/TimelineScaleField.tsx
index 0f14e62caf..4ec59988ca 100644
--- a/packages/studio/src/components/Timeline/TimelineScaleField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineScaleField.tsx
@@ -8,7 +8,10 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
-import {getDecimalPlaces} from './timeline-field-utils';
+import {
+ getDecimalPlaces,
+ normalizeTimelineNumber,
+} from './timeline-field-utils';
import {timelineLayerIconContainer} from './TimelineLayerEye';
const leftDraggerStyle: React.CSSProperties = {
@@ -49,10 +52,6 @@ const clamp = (value: number, min: number, max: number): number => {
return Math.min(max, Math.max(min, value));
};
-const normalizeScaleNumber = (value: number): number => {
- return Math.round(value * 1000000) / 1000000;
-};
-
const getLinkedScale = ({
axis,
newValue,
@@ -72,7 +71,7 @@ const getLinkedScale = ({
const linkedBase = axis === 'x' ? baseY : baseX;
if (drivingBase === 0 || linkedBase === 0) {
- const clamped = normalizeScaleNumber(clamp(newValue, min, max));
+ const clamped = normalizeTimelineNumber(clamp(newValue, min, max));
return [clamped, clamped];
}
@@ -88,8 +87,8 @@ const getLinkedScale = ({
}
const clampedDriving = clamp(driving, min, max);
- const normalizedDriving = normalizeScaleNumber(clampedDriving);
- const normalizedLinked = normalizeScaleNumber(linked);
+ const normalizedDriving = normalizeTimelineNumber(clampedDriving);
+ const normalizedLinked = normalizeTimelineNumber(linked);
return axis === 'x'
? [normalizedDriving, normalizedLinked]
diff --git a/packages/studio/src/components/Timeline/TimelineTranslateField.tsx b/packages/studio/src/components/Timeline/TimelineTranslateField.tsx
index 2973ffb088..5f6f5d6da6 100644
--- a/packages/studio/src/components/Timeline/TimelineTranslateField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTranslateField.tsx
@@ -6,7 +6,10 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
-import {getDecimalPlaces} from './timeline-field-utils';
+import {
+ getDecimalPlaces,
+ normalizeTimelineNumber,
+} from './timeline-field-utils';
import {parseTranslate, serializeTranslate} from './timeline-translate-utils';
const leftDraggerStyle: React.CSSProperties = {
@@ -52,7 +55,7 @@ export const TimelineTranslateField: React.FC<{
const formatter = useCallback(
(v: number | string) => {
- const num = Number(v);
+ const num = normalizeTimelineNumber(Number(v));
const digits = Math.max(stepDecimals, getDecimalPlaces(num));
const formatted = digits === 0 ? String(num) : num.toFixed(digits);
return `${formatted}px`;
diff --git a/packages/studio/src/components/Timeline/timeline-field-utils.ts b/packages/studio/src/components/Timeline/timeline-field-utils.ts
index 9d8460b4be..2d33b9c7f4 100644
--- a/packages/studio/src/components/Timeline/timeline-field-utils.ts
+++ b/packages/studio/src/components/Timeline/timeline-field-utils.ts
@@ -4,6 +4,10 @@ export const getDecimalPlaces = (num: number): number => {
return decimalIndex === -1 ? 0 : str.length - decimalIndex - 1;
};
+export const normalizeTimelineNumber = (value: number): number => {
+ return Math.round(value * 1000000) / 1000000;
+};
+
export const draggerStyle: React.CSSProperties = {
width: 80,
};
diff --git a/packages/studio/src/components/Timeline/timeline-translate-utils.ts b/packages/studio/src/components/Timeline/timeline-translate-utils.ts
index 63e394baec..3756c215a9 100644
--- a/packages/studio/src/components/Timeline/timeline-translate-utils.ts
+++ b/packages/studio/src/components/Timeline/timeline-translate-utils.ts
@@ -1,3 +1,5 @@
+import {normalizeTimelineNumber} from './timeline-field-utils';
+
const PIXEL_PATTERN = /^(-?\d+(?:\.\d+)?)px(?:\s+(-?\d+(?:\.\d+)?)px)?$/;
export const parseTranslate = (value: string): [number, number] => {
@@ -6,11 +8,14 @@ export const parseTranslate = (value: string): [number, number] => {
return [0, 0];
}
- return [Number(m[1]), m[2] !== undefined ? Number(m[2]) : 0];
+ return [
+ normalizeTimelineNumber(Number(m[1])),
+ m[2] !== undefined ? normalizeTimelineNumber(Number(m[2])) : 0,
+ ];
};
const formatTranslateCoordinate = (value: number): string => {
- const rounded = Math.round(value * 1000) / 1000;
+ const rounded = normalizeTimelineNumber(value);
return String(Object.is(rounded, -0) ? 0 : rounded);
};
diff --git a/packages/studio/src/test/timeline-translate-utils.test.ts b/packages/studio/src/test/timeline-translate-utils.test.ts
new file mode 100644
index 0000000000..e28caea56e
--- /dev/null
+++ b/packages/studio/src/test/timeline-translate-utils.test.ts
@@ -0,0 +1,17 @@
+import {expect, test} from 'bun:test';
+import {
+ parseTranslate,
+ serializeTranslate,
+} from '../components/Timeline/timeline-translate-utils';
+
+test('parseTranslate normalizes floating point noise', () => {
+ expect(parseTranslate('10.020000000000001px 30.240000000000002px')).toEqual([
+ 10.02, 30.24,
+ ]);
+});
+
+test('serializeTranslate normalizes floating point noise', () => {
+ expect(serializeTranslate(0.1 + 0.2, 10.020000000000001)).toBe(
+ '0.3px 10.02px',
+ );
+});