Skip to content

πŸŽ₯ Make videos programmatically with React

@remotion/studio: Add font weight keyframes

Jonny Burgerb08e79b6b39a
diff --git a/packages/core/src/interactivity-schema.ts b/packages/core/src/interactivity-schema.ts
index c3348c9030..67d958937f 100644
--- a/packages/core/src/interactivity-schema.ts
+++ b/packages/core/src/interactivity-schema.ts
@@ -116,7 +116,7 @@ export type FontWeightFieldSchema = {
type: 'font-weight';
default: number | string | undefined;
description?: string;
- keyframable?: false;
+ keyframable?: boolean;
};
export type AssetFieldSchema = {
@@ -313,7 +313,6 @@ export const textSchema = {
type: 'font-weight',
default: 400,
description: 'Font weight',
- keyframable: false,
},
'style.fontStyle': {
type: 'enum',
diff --git a/packages/core/src/test/with-interactivity-schema-helpers.test.ts b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
index a73539e028..df6b2e191e 100644
--- a/packages/core/src/test/with-interactivity-schema-helpers.test.ts
+++ b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
@@ -21,8 +21,8 @@ import {
cropSchema,
extendSchemaWithSequenceName,
premountSchema,
- sequencePremountSchema,
sequenceCropSchema,
+ sequencePremountSchema,
sequenceSchema,
sequenceSchemaWithoutFrom,
sequenceStyleSchema,
@@ -362,7 +362,6 @@ test('textSchema exposes common text style fields', () => {
type: 'font-weight',
default: 400,
description: 'Font weight',
- keyframable: false,
});
expect(Object.keys(textSchema['style.fontStyle'].variants)).toEqual([
'normal',
diff --git a/packages/example/src/VariableGoogleFont/VariableGoogleFont.tsx b/packages/example/src/VariableGoogleFont/VariableGoogleFont.tsx
index e7f355b69e..f722087a70 100644
--- a/packages/example/src/VariableGoogleFont/VariableGoogleFont.tsx
+++ b/packages/example/src/VariableGoogleFont/VariableGoogleFont.tsx
@@ -1,31 +1,12 @@
import {loadVariableFont} from '@remotion/google-fonts/NotoSans';
import React from 'react';
-import {
- AbsoluteFill,
- Easing,
- interpolate,
- useCurrentFrame,
- useVideoConfig,
-} from 'remotion';
+import {AbsoluteFill, Interactive} from 'remotion';
const {axes, fontFamily} = loadVariableFont('normal', {
subsets: ['latin'],
});
export const VariableGoogleFont: React.FC = () => {
- const frame = useCurrentFrame();
- const {durationInFrames} = useVideoConfig();
- const weight = Math.round(
- interpolate(
- frame,
- [0, (durationInFrames - 1) / 2, durationInFrames - 1],
- [axes.wght.min, axes.wght.max, axes.wght.min],
- {
- easing: Easing.inOut(Easing.ease),
- },
- ),
- );
-
return (
<AbsoluteFill
style={{
@@ -35,17 +16,6 @@ export const VariableGoogleFont: React.FC = () => {
justifyContent: 'center',
}}
>
- <div
- style={{
- fontFamily,
- fontSize: 150,
- fontWeight: weight,
- letterSpacing: -7,
- lineHeight: 1,
- }}
- >
- Variable
- </div>
<div
style={{
fontFamily,
@@ -55,8 +25,20 @@ export const VariableGoogleFont: React.FC = () => {
marginTop: 56,
}}
>
- FONT WEIGHT {weight}
+ FONT WEIGHT {axes.wght.min}–{axes.wght.max}
</div>
+ <Interactive.Div
+ name="Variable font weight"
+ style={{
+ fontFamily,
+ fontSize: 150,
+ fontWeight: 100,
+ letterSpacing: -7,
+ lineHeight: 1,
+ }}
+ >
+ Variable
+ </Interactive.Div>
</AbsoluteFill>
);
};
diff --git a/packages/studio-shared/src/keyframe-interpolation-function.ts b/packages/studio-shared/src/keyframe-interpolation-function.ts
index 7d73a6139e..5a574528d7 100644
--- a/packages/studio-shared/src/keyframe-interpolation-function.ts
+++ b/packages/studio-shared/src/keyframe-interpolation-function.ts
@@ -1,4 +1,4 @@
-import type {InteractivitySchemaField, InteractivitySchema} from 'remotion';
+import type {InteractivitySchema, InteractivitySchemaField} from 'remotion';
export const keyframeInterpolationFunctions = [
'interpolate',
@@ -18,7 +18,7 @@ const KEYFRAME_FIELD_TYPE_SUPPORT = {
color: true,
enum: true,
'font-family': false,
- 'font-weight': false,
+ 'font-weight': true,
hidden: true,
number: true,
'rotation-css': true,
@@ -43,7 +43,7 @@ const KEYFRAME_FIELD_TYPE_INTERPOLATION = {
color: 'interpolateColors',
enum: 'interpolate',
'font-family': 'unsupported',
- 'font-weight': 'unsupported',
+ 'font-weight': 'interpolate',
hidden: 'infer',
number: 'infer',
'rotation-css': 'interpolate',
diff --git a/packages/studio-shared/src/test/keyframe-interpolation-function.test.ts b/packages/studio-shared/src/test/keyframe-interpolation-function.test.ts
index d17b4aa032..48ec766007 100644
--- a/packages/studio-shared/src/test/keyframe-interpolation-function.test.ts
+++ b/packages/studio-shared/src/test/keyframe-interpolation-function.test.ts
@@ -131,6 +131,25 @@ test('isSchemaFieldKeyframable rejects font-family fields', () => {
);
});
+test('font-weight fields use interpolate keyframes', () => {
+ const schema = {
+ 'style.fontWeight': {
+ type: 'font-weight',
+ default: 400,
+ },
+ } satisfies InteractivitySchema;
+
+ expect(isSchemaFieldKeyframable({schema, key: 'style.fontWeight'})).toBe(
+ true,
+ );
+ expect(
+ getKeyframeInterpolationFunctionForSchemaField({
+ schema,
+ key: 'style.fontWeight',
+ }),
+ ).toBe('interpolate');
+});
+
test('isSchemaFieldKeyframable rejects asset fields', () => {
const schema = {
src: {
diff --git a/packages/studio/src/components/Timeline/TimelineKeyframeControls.tsx b/packages/studio/src/components/Timeline/TimelineKeyframeControls.tsx
index 1dfa2105c4..865e580f85 100644
--- a/packages/studio/src/components/Timeline/TimelineKeyframeControls.tsx
+++ b/packages/studio/src/components/Timeline/TimelineKeyframeControls.tsx
@@ -43,6 +43,7 @@ import {
getKeyframeDisplayOffset,
getTimelineKeyframes,
} from './get-timeline-keyframes';
+import {normalizeFontWeightForKeyframe} from './normalize-font-weight-for-keyframe';
import {ensureFrameIsInViewport} from './timeline-scroll-logic';
import {TimelineKeyframeDiamondIcon} from './TimelineKeyframeDiamondIcon';
import {useTimelineKeyframeTracks} from './TimelineKeyframeTracksContext';
@@ -368,12 +369,21 @@ const getAddChange = (
return null;
}
+ const fieldSchema = target.schema[target.fieldKey];
+ const normalizedValue =
+ fieldSchema?.type === 'font-weight'
+ ? normalizeFontWeightForKeyframe(value)
+ : value;
+ if (normalizedValue === null) {
+ return null;
+ }
+
const change = {
fileName: target.fileName,
nodePath: target.nodePath,
fieldKey: target.fieldKey,
sourceFrame: target.sourceFrame,
- value,
+ value: normalizedValue,
schema: target.schema,
};
diff --git a/packages/studio/src/components/Timeline/normalize-font-weight-for-keyframe.ts b/packages/studio/src/components/Timeline/normalize-font-weight-for-keyframe.ts
new file mode 100644
index 0000000000..bad05e30c9
--- /dev/null
+++ b/packages/studio/src/components/Timeline/normalize-font-weight-for-keyframe.ts
@@ -0,0 +1,21 @@
+export const normalizeFontWeightForKeyframe = (
+ value: unknown,
+): number | null => {
+ if (value === 'normal') {
+ return 400;
+ }
+
+ if (value === 'bold') {
+ return 700;
+ }
+
+ if (typeof value === 'number') {
+ return Number.isFinite(value) ? value : null;
+ }
+
+ if (typeof value === 'string' && /^\d+(?:\.\d+)?$/.test(value)) {
+ return Number(value);
+ }
+
+ return null;
+};
diff --git a/packages/studio/src/test/normalize-font-weight-for-keyframe.test.ts b/packages/studio/src/test/normalize-font-weight-for-keyframe.test.ts
new file mode 100644
index 0000000000..65d84caec1
--- /dev/null
+++ b/packages/studio/src/test/normalize-font-weight-for-keyframe.test.ts
@@ -0,0 +1,16 @@
+import {expect, test} from 'bun:test';
+import {normalizeFontWeightForKeyframe} from '../components/Timeline/normalize-font-weight-for-keyframe';
+
+test('normalizes font weights before keyframing', () => {
+ expect(normalizeFontWeightForKeyframe(650)).toBe(650);
+ expect(normalizeFontWeightForKeyframe('650')).toBe(650);
+ expect(normalizeFontWeightForKeyframe('normal')).toBe(400);
+ expect(normalizeFontWeightForKeyframe('bold')).toBe(700);
+});
+
+test('rejects font weights that cannot be interpolated', () => {
+ expect(normalizeFontWeightForKeyframe('bolder')).toBe(null);
+ expect(normalizeFontWeightForKeyframe('lighter')).toBe(null);
+ expect(normalizeFontWeightForKeyframe('invalid')).toBe(null);
+ expect(normalizeFontWeightForKeyframe(Number.NaN)).toBe(null);
+});