🎥 Make videos programmatically with React
template-helloworld: Make title and logo editable in Studio
JonnyBurger25c475a1dd13
diff --git a/packages/template-helloworld/src/HelloWorld.tsx b/packages/template-helloworld/src/HelloWorld.tsxindex 803c33b010..964ad6f8cb 100644--- a/packages/template-helloworld/src/HelloWorld.tsx+++ b/packages/template-helloworld/src/HelloWorld.tsx@@ -1,13 +1,12 @@import {AbsoluteFill,+ Easing,+ Interactive,interpolate,- Sequence,- spring,useCurrentFrame,useVideoConfig,} from "remotion";import { Logo } from "./HelloWorld/Logo";-import { Subtitle } from "./HelloWorld/Subtitle";import { Title } from "./HelloWorld/Title";export type HelloWorldProps = {@@ -18,57 +17,70 @@ export type HelloWorldProps = {};export const HelloWorld: React.FC<HelloWorldProps> = ({- titleText: propOne,- titleColor: propTwo,+ titleText,+ titleColor,logoColor1,logoColor2,}) => {const frame = useCurrentFrame();const { durationInFrames, fps } = useVideoConfig();- // Animate from 0 to 1 after 25 frames- const logoTranslationProgress = spring({- frame: frame - 25,- fps,- config: {- damping: 100,- },- });-- // Move the logo up by 150 pixels once the transition starts- const logoTranslation = interpolate(- logoTranslationProgress,- [0, 1],- [0, -150],- );-- // Fade out the animation at the end- const opacity = interpolate(- frame,- [durationInFrames - 25, durationInFrames - 15],- [1, 0],- {- extrapolateLeft: "clamp",- extrapolateRight: "clamp",- },- );-- // A <AbsoluteFill> is just a absolutely positioned <div>!return (<AbsoluteFill style={{ backgroundColor: "white" }}>- <AbsoluteFill style={{ opacity }}>- <AbsoluteFill style={{ transform: `translateY(${logoTranslation}px)` }}>- <Logo logoColor1={logoColor1} logoColor2={logoColor2} />- </AbsoluteFill>- {/* Sequences can shift the time for its children! */}- <Sequence from={35}>- <Title titleText={propOne} titleColor={propTwo} />- </Sequence>- {/* The subtitle will only enter on the 75th frame. */}- <Sequence from={75}>- <Subtitle />- </Sequence>- </AbsoluteFill>+ <Interactive.Div+ name="Content fade"+ premountFor={fps}+ style={{+ position: "absolute",+ inset: 0,+ opacity: interpolate(+ frame,+ [durationInFrames - 25, durationInFrames - 15],+ [1, 0],+ { extrapolateLeft: "clamp", extrapolateRight: "clamp" },+ ),+ }}+ >+ <Logo+ name="Remotion logo"+ premountFor={fps}+ logoColor1={logoColor1}+ logoColor2={logoColor2}+ style={{+ translate: interpolate(frame, [25, 55], ["0px 0px", "0px -150px"], {+ easing: Easing.spring({ damping: 100 }),+ extrapolateLeft: "clamp",+ extrapolateRight: "clamp",+ }),+ }}+ />+ <Title+ name="Welcome title"+ from={35}+ premountFor={fps}+ titleText={titleText}+ titleColor={titleColor}+ />+ <Interactive.Div+ name="Editing hint"+ from={75}+ premountFor={fps}+ style={{+ fontFamily: "SF Pro Text, Helvetica, Arial, sans-serif",+ fontSize: 40,+ textAlign: "center",+ position: "absolute",+ bottom: 140,+ width: "100%",+ opacity: interpolate(frame, [75, 105], [0, 1], {+ extrapolateLeft: "clamp",+ extrapolateRight: "clamp",+ }),+ }}+ >+ Edit <code style={{ color: "#86A8E7" }}>src/Root.tsx</code> and save to reload.+ </Interactive.Div>+ </Interactive.Div></AbsoluteFill>);};diff --git a/packages/template-helloworld/src/HelloWorld/Arc.tsx b/packages/template-helloworld/src/HelloWorld/Arc.tsxindex e580b47d09..309ce30dce 100644--- a/packages/template-helloworld/src/HelloWorld/Arc.tsx+++ b/packages/template-helloworld/src/HelloWorld/Arc.tsx@@ -1,5 +1,5 @@import { useState } from "react";-import { random, useVideoConfig } from "remotion";+import { Easing, interpolate, random, useCurrentFrame, useVideoConfig } from "remotion";const getCircumferenceOfArc = (rx: number, ry: number) => {return Math.PI * 2 * Math.sqrt((rx * rx + ry * ry) / 2);@@ -13,13 +13,17 @@ const arcLength = getCircumferenceOfArc(rx, ry);const strokeWidth = 30;export const Arc: React.FC<{- progress: number;rotation: number;- rotateProgress: number;color1: string;color2: string;-}> = ({ progress, rotation, rotateProgress, color1, color2 }) => {- const { width, height } = useVideoConfig();+}> = ({ rotation, color1, color2 }) => {+ const { width, height, fps } = useVideoConfig();+ const frame = useCurrentFrame();+ const progress = interpolate(frame, [0, fps], [0, 1], {+ easing: Easing.spring({ damping: 100, mass: 0.5 }),+ extrapolateLeft: "clamp",+ extrapolateRight: "clamp",+ });// Each svg Id must be unique to not conflict with each otherconst [gradientId] = useState(() => String(random(null)));@@ -29,7 +33,7 @@ export const Arc: React.FC<{viewBox={`0 0 ${width} ${height}`}style={{position: "absolute",- transform: `rotate(${rotation * rotateProgress}deg)`,+ rotate: `${rotation * progress}deg`,}}><defs>diff --git a/packages/template-helloworld/src/HelloWorld/Atom.tsx b/packages/template-helloworld/src/HelloWorld/Atom.tsxindex 5fdb507f70..22f02c5b04 100644--- a/packages/template-helloworld/src/HelloWorld/Atom.tsx+++ b/packages/template-helloworld/src/HelloWorld/Atom.tsx@@ -1,22 +1,27 @@import { useState } from "react";-import { random, useVideoConfig } from "remotion";+import { Easing, interpolate, random, useCurrentFrame, useVideoConfig } from "remotion";export const Atom: React.FC<{- scale: number;color1: string;color2: string;-}> = ({ scale, color1, color2 }) => {- const config = useVideoConfig();+}> = ({ color1, color2 }) => {+ const { width, height, fps } = useVideoConfig();+ const frame = useCurrentFrame();// Each SVG ID must be unique to not conflict with each otherconst [gradientId] = useState(() => String(random(null)));return (<svg- viewBox={`0 0 ${config.width} ${config.height}`}+ viewBox={`0 0 ${width} ${height}`}style={{position: "absolute",- transform: `scale(${scale})`,+ scale: interpolate(frame, [0, fps], [0, 1], {+ easing: Easing.spring({ damping: 100, mass: 0.5 }),+ output: "perceptual-scale",+ extrapolateLeft: "clamp",+ extrapolateRight: "clamp",+ }),}}><defs>@@ -27,8 +32,8 @@ export const Atom: React.FC<{</defs><circler={70}- cx={config.width / 2}- cy={config.height / 2}+ cx={width / 2}+ cy={height / 2}fill={`url(#${gradientId})`}/></svg>diff --git a/packages/template-helloworld/src/HelloWorld/Logo.tsx b/packages/template-helloworld/src/HelloWorld/Logo.tsxindex 4afabf2b8b..3756d9b0c5 100644--- a/packages/template-helloworld/src/HelloWorld/Logo.tsx+++ b/packages/template-helloworld/src/HelloWorld/Logo.tsx@@ -1,85 +1,71 @@import {- AbsoluteFill,+ Easing,+ Interactive,interpolate,- spring,useCurrentFrame,useVideoConfig,+ type InteractivitySchema,} from "remotion";+import type React from "react";import { Arc } from "./Arc";import { Atom } from "./Atom";export type LogoProps = {readonly logoColor1: string;readonly logoColor2: string;+ readonly style?: React.CSSProperties;};-export const Logo: React.FC<LogoProps> = ({- logoColor1: color1,- logoColor2: color2,-}) => {- const videoConfig = useVideoConfig();+const LogoInner: React.FC<LogoProps> = ({ logoColor1, logoColor2, style }) => {+ const { durationInFrames, fps } = useVideoConfig();const frame = useCurrentFrame();- const development = spring({- config: {- damping: 100,- mass: 0.5,- },- fps: videoConfig.fps,- frame,- });-- const rotationDevelopment = spring({- config: {- damping: 100,- mass: 0.5,- },- fps: videoConfig.fps,- frame,- });-- const scale = spring({- frame,- config: {- mass: 0.5,- },- fps: videoConfig.fps,- });-- const logoRotation = interpolate(- frame,- [0, videoConfig.durationInFrames],- [0, 360],- );-return (- <AbsoluteFill+ <Interactive.Divstyle={{- transform: `scale(${scale}) rotate(${logoRotation}deg)`,+ position: "absolute",+ inset: 0,+ scale: interpolate(frame, [0, fps], [0, 1], {+ easing: Easing.spring({ mass: 0.5 }),+ output: "perceptual-scale",+ extrapolateLeft: "clamp",+ extrapolateRight: "clamp",+ }),+ rotate: interpolate(frame, [0, durationInFrames], ["0deg", "360deg"], {+ extrapolateLeft: "clamp",+ extrapolateRight: "clamp",+ }),+ ...style,}}><Arc- rotateProgress={rotationDevelopment}- progress={development}rotation={30}- color1={color1}- color2={color2}+ color1={logoColor1}+ color2={logoColor2}/><Arc- rotateProgress={rotationDevelopment}rotation={90}- progress={development}- color1={color1}- color2={color2}+ color1={logoColor1}+ color2={logoColor2}/><Arc- rotateProgress={rotationDevelopment}rotation={-30}- progress={development}- color1={color1}- color2={color2}+ color1={logoColor1}+ color2={logoColor2}/>- <Atom scale={rotationDevelopment} color1={color1} color2={color2} />- </AbsoluteFill>+ <Atom color1={logoColor1} color2={logoColor2} />+ </Interactive.Div>);};++const logoSchema = {+ logoColor1: { type: "color", default: "#91EAE4", description: "First logo color" },+ logoColor2: { type: "color", default: "#86A8E7", description: "Second logo color" },+} as const satisfies InteractivitySchema;++export const Logo = Interactive.withSchema({+ Component: LogoInner,+ componentName: "<Logo>",+ schema: logoSchema,+ wrapInSequence: true,+});diff --git a/packages/template-helloworld/src/HelloWorld/Subtitle.tsx b/packages/template-helloworld/src/HelloWorld/Subtitle.tsxdeleted file mode 100644index 9ccbe5da78..0000000000--- a/packages/template-helloworld/src/HelloWorld/Subtitle.tsx+++ /dev/null@@ -1,26 +0,0 @@-import React from "react";-import { interpolate, useCurrentFrame } from "remotion";-import { COLOR_1, FONT_FAMILY } from "./constants";--const subtitle: React.CSSProperties = {- fontFamily: FONT_FAMILY,- fontSize: 40,- textAlign: "center",- position: "absolute",- bottom: 140,- width: "100%",-};--const codeStyle: React.CSSProperties = {- color: COLOR_1,-};--export const Subtitle: React.FC = () => {- const frame = useCurrentFrame();- const opacity = interpolate(frame, [0, 30], [0, 1]);- return (- <div style={{ ...subtitle, opacity }}>- Edit <code style={codeStyle}>src/Root.tsx</code> and save to reload.- </div>- );-};diff --git a/packages/template-helloworld/src/HelloWorld/Title.tsx b/packages/template-helloworld/src/HelloWorld/Title.tsxindex d691214320..2ef71b5c59 100644--- a/packages/template-helloworld/src/HelloWorld/Title.tsx+++ b/packages/template-helloworld/src/HelloWorld/Title.tsx@@ -1,58 +1,62 @@-import React from "react";-import { spring, useCurrentFrame, useVideoConfig } from "remotion";-import { FONT_FAMILY } from "./constants";+import type React from "react";+import { Easing, Interactive, interpolate, useCurrentFrame, useVideoConfig, type InteractivitySchema } from "remotion";-const title: React.CSSProperties = {- fontFamily: FONT_FAMILY,- fontWeight: "bold",- fontSize: 100,- textAlign: "center",- position: "absolute",- bottom: 160,- width: "100%",-};--const word: React.CSSProperties = {- marginLeft: 10,- marginRight: 10,- display: "inline-block",-};--export const Title: React.FC<{+type TitleProps = {readonly titleText: string;readonly titleColor: string;-}> = ({ titleText, titleColor }) => {- const videoConfig = useVideoConfig();- const frame = useCurrentFrame();+ readonly style?: React.CSSProperties;+};- const words = titleText.split(" ");+const TitleInner: React.FC<TitleProps> = ({ titleText, titleColor, style }) => {+ const { fps } = useVideoConfig();+ const frame = useCurrentFrame();return (- <h1 style={title}>- {words.map((t, i) => {- const delay = i * 5;-- const scale = spring({- fps: videoConfig.fps,- frame: frame - delay,- config: {- damping: 200,- },- });-+ <Interactive.H1+ style={{+ fontFamily: "SF Pro Text, Helvetica, Arial, sans-serif",+ fontWeight: "bold",+ fontSize: 100,+ textAlign: "center",+ position: "absolute",+ bottom: 160,+ width: "100%",+ ...style,+ }}+ >+ {titleText.split(" ").map((word, index) => {return (<span- key={t}+ key={`${word}-${index}`}style={{- ...word,+ marginLeft: 10,+ marginRight: 10,+ display: "inline-block",color: titleColor,- transform: `scale(${scale})`,+ scale: interpolate(frame, [index * 5, index * 5 + fps], [0, 1], {+ easing: Easing.spring({ damping: 200 }),+ output: "perceptual-scale",+ extrapolateLeft: "clamp",+ extrapolateRight: "clamp",+ }),}}>- {t}+ {word}</span>);})}- </h1>+ </Interactive.H1>);};++const titleSchema = {+ titleText: { type: "text-content", default: "Welcome to Remotion", description: "Title" },+ titleColor: { type: "color", default: "#000000", description: "Title color" },+} as const satisfies InteractivitySchema;++export const Title = Interactive.withSchema({+ Component: TitleInner,+ componentName: "<Title>",+ schema: titleSchema,+ wrapInSequence: true,+});diff --git a/packages/template-helloworld/src/HelloWorld/constants.ts b/packages/template-helloworld/src/HelloWorld/constants.tsdeleted file mode 100644index e20c050a54..0000000000--- a/packages/template-helloworld/src/HelloWorld/constants.ts+++ /dev/null@@ -1,5 +0,0 @@-// Change any of these to update your video live.--export const COLOR_1 = "#86A8E7";--export const FONT_FAMILY = "SF Pro Text, Helvetica, Arial, sans-serif";diff --git a/packages/template-helloworld/src/Root.tsx b/packages/template-helloworld/src/Root.tsxindex f7ac9e5082..3672ed0710 100644--- a/packages/template-helloworld/src/Root.tsx+++ b/packages/template-helloworld/src/Root.tsx@@ -1,6 +1,7 @@import { Composition } from "remotion";import { HelloWorld } from "./HelloWorld";import { Logo } from "./HelloWorld/Logo";+import { Title } from "./HelloWorld/Title";// Each <Composition> is an entry in the sidebar!@@ -9,7 +10,7 @@ export const RemotionRoot: React.FC = () => {<><Composition// You can take the "id" to render a video:- // npx remotion render HelloWorld+ // bunx remotion render HelloWorldid="HelloWorld"component={HelloWorld}durationInFrames={150}@@ -35,10 +36,22 @@ export const RemotionRoot: React.FC = () => {width={1920}height={1080}defaultProps={{- logoColor1: "#91dAE2",+ logoColor1: "#91EAE4",logoColor2: "#86A8E7",}}/>+ <Composition+ id="Title"+ component={Title}+ durationInFrames={115}+ fps={30}+ width={1920}+ height={1080}+ defaultProps={{+ titleText: "Welcome to Remotion",+ titleColor: "#000000",+ }}+ /></>);};