🎥 Make videos programmatically with React
remotion: Support rotate angle units
Jonny Burgeree88597da21a
diff --git a/packages/core/src/interpolate-rotate.ts b/packages/core/src/interpolate-rotate.tsindex ac6376b7e9..2334ab5bd3 100644--- a/packages/core/src/interpolate-rotate.ts+++ b/packages/core/src/interpolate-rotate.ts@@ -3,7 +3,14 @@ import {normalizeNumber} from './normalize-number.js';export type InterpolateRotateOptions = InterpolateOptions;-const degreesValueRegex = /^([+-]?(?:\d+\.?\d*|\.\d+))deg$/;+const angleValueRegex = /^([+-]?(?:\d+\.?\d*|\.\d+))(deg|rad|grad|turn)$/;++const angleUnitToDegrees: Record<string, number> = {+ deg: 1,+ rad: 180 / Math.PI,+ grad: 360 / 400,+ turn: 360,+};const parseRotate = (value: string): number => {if (typeof value !== 'string') {@@ -12,18 +19,18 @@ const parseRotate = (value: string): number => {);}- const match = degreesValueRegex.exec(value.trim());+ const match = angleValueRegex.exec(value.trim());if (match === null) {throw new TypeError(- `interpolateRotate() only supports deg values, but got "${value}"`,+ `interpolateRotate() only supports deg, rad, grad and turn values, but got "${value}"`,);}- return Number(match[1]);+ return Number(match[1]) * angleUnitToDegrees[match[2]];};/*- * @description Allows you to map a range of values to CSS rotate values using degree units.+ * @description Allows you to map a range of values to CSS rotate values using angle units.* @see [Documentation](https://remotion.dev/docs/interpolate-rotate)*/export const interpolateRotate = (diff --git a/packages/core/src/test/interpolateRotate.test.ts b/packages/core/src/test/interpolateRotate.test.tsindex 343b625ed2..692b8d9f88 100644--- a/packages/core/src/test/interpolateRotate.test.ts+++ b/packages/core/src/test/interpolateRotate.test.ts@@ -14,6 +14,26 @@ describe('interpolateRotate()', () => {);});+ test('interpolates radian values', () => {+ expect(interpolateRotate(15, [0, 30], ['0rad', `${Math.PI}rad`])).toBe(+ '90deg',+ );+ });++ test('interpolates gradian values', () => {+ expect(interpolateRotate(15, [0, 30], ['0grad', '200grad'])).toBe('90deg');+ });++ test('interpolates turn values', () => {+ expect(interpolateRotate(15, [0, 30], ['0turn', '0.5turn'])).toBe('90deg');+ });++ test('interpolates mixed angle units', () => {+ expect(interpolateRotate(15, [0, 30], ['0.25turn', '200grad'])).toBe(+ '135deg',+ );+ });+test('supports multiple keyframes', () => {expect(interpolateRotate(45, [0, 30, 60], ['0deg', '60deg', '0deg'])).toBe('30deg',@@ -54,18 +74,14 @@ describe('interpolateRotate()', () => {expect(interpolateRotate(15, [0], ['10deg'])).toBe('10deg');});- test('throws if output range contains non-degree units', () => {- expectToThrow(- () => interpolateRotate(15, [0, 30], ['0deg', '1turn']),- /only supports deg values/,- );+ test('throws if output range contains unsupported units', () => {expectToThrow(- () => interpolateRotate(15, [0, 30], ['0deg', '1rad']),- /only supports deg values/,+ () => interpolateRotate(15, [0, 30], ['0deg', '100px']),+ /only supports deg, rad, grad and turn values/,);expectToThrow(() => interpolateRotate(15, [0, 30], ['0deg', '10']),- /only supports deg values/,+ /only supports deg, rad, grad and turn values/,);});});diff --git a/packages/docs/docs/interpolate-rotate.mdx b/packages/docs/docs/interpolate-rotate.mdxindex e38b24c434..52bf9c5acc 100644--- a/packages/docs/docs/interpolate-rotate.mdx+++ b/packages/docs/docs/interpolate-rotate.mdx@@ -7,7 +7,7 @@ crumb: 'API'# interpolateRotate()<AvailableFrom v="4.0.472" />-Allows you to map a range of values to CSS rotate values using degree units.+Allows you to map a range of values to CSS rotate values using angle units.## API@@ -21,7 +21,8 @@ Takes three to four arguments:`inputRange` and `outputRange` must have the same length and at least one rotate value.With one rotate value, `interpolateRotate()` always returns the only output value.-Rotate values must be `deg` values, for example `0deg`, `-45deg`, or `180.5deg`.+Rotate values may use CSS angle units: `deg`, `rad`, `grad`, or `turn`.+Mixed units are supported, for example `0deg`, `3.14rad`, `100grad`, and `0.5turn`.## Returns@@ -54,8 +55,10 @@ export const MyComposition: React.FC = () => {## Supported units-Only `deg` values are supported.-Other CSS angle units such as `rad`, `grad`, `turn`, or unitless values will throw an error.+The `outputRange` may contain `deg`, `rad`, `grad`, and `turn` values.+Values are converted to degrees before interpolation, and `interpolateRotate()` returns a `deg` value.++Other CSS units such as `px`, `%`, or unitless values will throw an error.## Options