Skip to content

🎥 Make videos programmatically with React

simplify typewriter example

Jonny Burger7691743a7559
diff --git a/packages/skills/skills/remotion/rules/assets/charts-bar-chart.tsx b/packages/skills/skills/remotion/rules/assets/charts-bar-chart.tsx
index 91383d1e71..0094d1b546 100644
--- a/packages/skills/skills/remotion/rules/assets/charts-bar-chart.tsx
+++ b/packages/skills/skills/remotion/rules/assets/charts-bar-chart.tsx
@@ -8,13 +8,10 @@ const COLOR_AXIS = '#333333';
// Ideal composition size: 1280x720
-const Title: React.FC<{title: string; opacity: number}> = ({
- title,
- opacity,
-}) => (
- <div style={{textAlign: 'center', opacity, marginBottom: 40}}>
+const Title: React.FC<{children: React.ReactNode}> = ({children}) => (
+ <div style={{textAlign: 'center', marginBottom: 40}}>
<div style={{color: COLOR_TEXT, fontSize: 48, fontWeight: 600}}>
- {title}
+ {children}
</div>
</div>
);
@@ -137,12 +134,6 @@ export const MyAnimation = () => {
const chartHeight = height - 280;
const yAxisSteps = [2000, 2400, 2800];
- const headerOpacity = spring({
- frame,
- fps,
- config: {damping: 20, stiffness: 100},
- });
-
return (
<AbsoluteFill
style={{
@@ -153,7 +144,7 @@ export const MyAnimation = () => {
fontFamily: 'Inter, sans-serif',
}}
>
- <Title title="Gold Price 2024" opacity={headerOpacity} />
+ <Title>Gold Price 2024</Title>
<div style={{display: 'flex', flex: 1}}>
<YAxis steps={yAxisSteps} height={chartHeight} />
diff --git a/packages/skills/skills/remotion/rules/assets/text-animations-text-rotation.tsx b/packages/skills/skills/remotion/rules/assets/text-animations-text-rotation.tsx
index 1e7c23e531..5381360e55 100644
--- a/packages/skills/skills/remotion/rules/assets/text-animations-text-rotation.tsx
+++ b/packages/skills/skills/remotion/rules/assets/text-animations-text-rotation.tsx
@@ -17,7 +17,7 @@ export const MyAnimation = () => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
- const WORD_DURATION = 0 * fps;
+ const WORD_DURATION = 2 * fps;
const FADE_IN_DURATION = 0.5 * fps;
const FADE_OUT_START = 1.5 * fps;
diff --git a/packages/skills/skills/remotion/rules/assets/text-animations-typewriter.tsx b/packages/skills/skills/remotion/rules/assets/text-animations-typewriter.tsx
index 396537598f..9d2dbd3e74 100644
--- a/packages/skills/skills/remotion/rules/assets/text-animations-typewriter.tsx
+++ b/packages/skills/skills/remotion/rules/assets/text-animations-typewriter.tsx
@@ -5,56 +5,81 @@ import {
useVideoConfig,
} from 'remotion';
-export const MyAnimation = () => {
- const frame = useCurrentFrame();
- const {fps} = useVideoConfig();
-
- const COLOR_BG = '#ffffff';
- const COLOR_TEXT = '#000000';
+const COLOR_BG = '#ffffff';
+const COLOR_TEXT = '#000000';
+const FULL_TEXT = 'From prompt to motion graphics. This is Remotion.';
+const PAUSE_AFTER = 'From prompt to motion graphics.';
+const FONT_SIZE = 72;
+const FONT_WEIGHT = 700;
+const CHAR_FRAMES = 2;
+const CURSOR_BLINK_FRAMES = 16;
+const PAUSE_SECONDS = 1;
- const FULL_TEXT = 'From prompt to motion graphics. This is Remotion.';
- const CARET_SYMBOL = '\u258C';
- const PAUSE_AFTER = 'From prompt to motion graphics.';
-
- const FONT_SIZE = 72;
- const FONT_WEIGHT = 700;
- const CHAR_FRAMES = 2;
- const CURSOR_BLINK_FRAMES = 16;
- const PAUSE_SECONDS = 1;
-
- const PAUSE_FRAMES = Math.round(fps * PAUSE_SECONDS);
-
- const pauseIndex = FULL_TEXT.indexOf(PAUSE_AFTER);
+const getTypedText = ({
+ frame,
+ fullText,
+ pauseAfter,
+ charFrames,
+ pauseFrames,
+}: {
+ frame: number;
+ fullText: string;
+ pauseAfter: string;
+ charFrames: number;
+ pauseFrames: number;
+}): string => {
+ const pauseIndex = fullText.indexOf(pauseAfter);
const preLen =
- pauseIndex >= 0 ? pauseIndex + PAUSE_AFTER.length : FULL_TEXT.length;
+ pauseIndex >= 0 ? pauseIndex + pauseAfter.length : fullText.length;
let typedChars = 0;
- if (frame < preLen * CHAR_FRAMES) {
- typedChars = Math.floor(frame / CHAR_FRAMES);
- } else if (frame < preLen * CHAR_FRAMES + PAUSE_FRAMES) {
+ if (frame < preLen * charFrames) {
+ typedChars = Math.floor(frame / charFrames);
+ } else if (frame < preLen * charFrames + pauseFrames) {
typedChars = preLen;
} else {
- const postPhase = frame - preLen * CHAR_FRAMES - PAUSE_FRAMES;
+ const postPhase = frame - preLen * charFrames - pauseFrames;
typedChars = Math.min(
- FULL_TEXT.length,
- preLen + Math.floor(postPhase / CHAR_FRAMES),
+ fullText.length,
+ preLen + Math.floor(postPhase / charFrames),
);
}
- const typedText = FULL_TEXT.slice(0, typedChars);
+ return fullText.slice(0, typedChars);
+};
- const caretOpacity = interpolate(
- frame % CURSOR_BLINK_FRAMES,
- [0, CURSOR_BLINK_FRAMES / 2, CURSOR_BLINK_FRAMES],
+const Cursor: React.FC<{
+ frame: number;
+ blinkFrames: number;
+ symbol?: string;
+}> = ({frame, blinkFrames, symbol = '\u258C'}) => {
+ const opacity = interpolate(
+ frame % blinkFrames,
+ [0, blinkFrames / 2, blinkFrames],
[1, 0, 1],
{extrapolateLeft: 'clamp', extrapolateRight: 'clamp'},
);
+ return <span style={{opacity}}>{symbol}</span>;
+};
+
+export const MyAnimation = () => {
+ const frame = useCurrentFrame();
+ const {fps} = useVideoConfig();
+
+ const pauseFrames = Math.round(fps * PAUSE_SECONDS);
+
+ const typedText = getTypedText({
+ frame,
+ fullText: FULL_TEXT,
+ pauseAfter: PAUSE_AFTER,
+ charFrames: CHAR_FRAMES,
+ pauseFrames,
+ });
+
return (
<AbsoluteFill
style={{
backgroundColor: COLOR_BG,
- display: 'flex',
- fontFamily: 'Inter, sans-serif',
}}
>
<div
@@ -62,12 +87,11 @@ export const MyAnimation = () => {
color: COLOR_TEXT,
fontSize: FONT_SIZE,
fontWeight: FONT_WEIGHT,
- lineHeight: 1.15,
- whiteSpace: 'pre-wrap',
+ fontFamily: 'sans-serif',
}}
>
<span>{typedText}</span>
- <span style={{opacity: caretOpacity}}>{CARET_SYMBOL}</span>
+ <Cursor frame={frame} blinkFrames={CURSOR_BLINK_FRAMES} />
</div>
</AbsoluteFill>
);
diff --git a/packages/skills/src/Root.tsx b/packages/skills/src/Root.tsx
index 7d9cd8ccdb..ca4c410351 100644
--- a/packages/skills/src/Root.tsx
+++ b/packages/skills/src/Root.tsx
@@ -1,35 +1,13 @@
import {Composition} from 'remotion';
-import {
- COMPOSITION_HEIGHT,
- COMPOSITION_WIDTH,
- MyAnimation,
-} from '../skills/remotion/charts/assets/animated-shapes';
-import {MyAnimation as BarChartAnimation} from '../skills/remotion/charts/assets/bar-chart';
-import {MyAnimation as LottieAnimation} from '../skills/remotion/lottie/assets/example';
-import {MyAnimation as TextRotationAnimation} from '../skills/remotion/text-animations/assets/text-rotation';
-import {MyAnimation as TypewriterAnimation} from '../skills/remotion/text-animations/assets/typewriter';
-import {MyAnimation as WordCarouselAnimation} from '../skills/remotion/text-animations/assets/word-carousel';
-import {MyAnimation as WordHighlightAnimation} from '../skills/remotion/text-animations/assets/word-highlight';
+import {MyAnimation as BarChartAnimation} from '../skills/remotion/rules/assets/charts-bar-chart';
+import {MyAnimation as TextRotationAnimation} from '../skills/remotion/rules/assets/text-animations-text-rotation';
+import {MyAnimation as TypewriterAnimation} from '../skills/remotion/rules/assets/text-animations-typewriter';
+import {MyAnimation as WordCarouselAnimation} from '../skills/remotion/rules/assets/text-animations-word-carousel';
+import {MyAnimation as WordHighlightAnimation} from '../skills/remotion/rules/assets/text-animations-word-highlight';
export const RemotionRoot = () => {
return (
<>
- <Composition
- id="AnimatedShapes"
- component={MyAnimation}
- durationInFrames={100}
- fps={30}
- width={COMPOSITION_WIDTH}
- height={COMPOSITION_HEIGHT}
- />
- <Composition
- id="LottieAnimation"
- component={LottieAnimation}
- durationInFrames={150}
- fps={30}
- width={1080}
- height={1080}
- />
<Composition
id="TextRotation"
component={TextRotationAnimation}
@@ -53,6 +31,10 @@ export const RemotionRoot = () => {
fps={30}
width={1920}
height={1080}
+ defaultProps={{
+ fullText: 'From prompt to motion graphics. This is Remotion.',
+ pauseAfter: 'From prompt to motion graphics.',
+ }}
/>
<Composition
id="WordHighlight"