Production-ready animations, transitions, backgrounds, and scenes for Remotion
TypeScript ai-agentsclaude-codedemo-videomcpmcp-servermodel-context-protocolmotion-designproduct-demoreactremotionshadcnvideo
{
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
"name": "baton-word",
"title": "Baton Word",
"description": "A word is handed like a baton between scenes: \"Film it in code.\" in pink and white.",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/baton-word/index.tsx",
"content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Syne\";\nimport type React from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n AbsoluteFill,\n continueRender,\n delayRender,\n Easing,\n interpolate,\n interpolateColors,\n useCurrentFrame,\n useVideoConfig,\n} from \"remotion\";\n\nconst syne = loadFont(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\nexport interface BatonWordProps {\n scenes?: string[];\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n dark: { bg: \"#0A0A0A\", ink: \"#FFFFFF\", accent: \"#FF3EA5\" },\n light: { bg: \"#FFFFFF\", ink: \"#000000\", accent: \"#D61F7F\" },\n} as const;\n\nconst WGHT = 500;\nconst TRACK = -0.035;\nconst FONT_PX = 200;\nconst LH = 1.04;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IN = Easing.bezier(0.55, 0, 0.75, 0.2);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst RISE = 16;\nconst STAGGER = 3;\nconst DROP = 9;\nconst DROP_STAGGER = 1.5;\nconst TRAVEL = 24;\nconst FINAL_HOLD = 34;\nconst PUNCT = /[.,!?;:…—–]+$/;\n\ninterface Metrics {\n em: Record<string, number>;\n asc: number;\n desc: number;\n}\n\nfunction measureAll(texts: string[]): Metrics {\n const canvas = document.createElement(\"canvas\");\n const ctx = canvas.getContext(\"2d\");\n if (!ctx) throw new Error(\"baton-word: no 2d context\");\n ctx.font = `${WGHT} ${FONT_PX}px \"${syne.fontFamily}\"`;\n const em: Record<string, number> = {};\n for (const s of texts) {\n em[s] = ctx.measureText(s).width / FONT_PX + TRACK * Array.from(s).length;\n }\n const fm = ctx.measureText(\"Hxg\");\n return {\n em,\n asc: fm.fontBoundingBoxAscent / FONT_PX,\n desc: fm.fontBoundingBoxDescent / FONT_PX,\n };\n}\n\nfunction useMetrics(texts: string[]) {\n const key = texts.join(\"\\u0001\");\n const [state, setState] = useState<{ key: string; m: Metrics } | null>(null);\n const [handle] = useState(() => delayRender(\"baton-word: measuring\"));\n const released = useRef(false);\n useEffect(() => {\n let live = true;\n Promise.all([\n syne.waitUntilDone(),\n document.fonts.load(`${WGHT} 100px \"${syne.fontFamily}\"`),\n ])\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n setState({ key, m: measureAll(key.split(\"\\u0001\")) });\n if (!released.current) {\n released.current = true;\n continueRender(handle);\n }\n });\n return () => {\n live = false;\n };\n }, [key, handle]);\n return state && state.key === key ? state.m : null;\n}\n\ninterface Placed {\n word: string;\n line: number;\n x: number;\n}\n\ninterface SceneLayout {\n words: Placed[];\n lines: number;\n F: number;\n left: number;\n top: number;\n}\n\nfunction wrap(\n words: string[],\n em: Record<string, number>,\n F: number,\n maxW: number,\n) {\n const lines: string[][] = [[]];\n for (const w of words) {\n const cur = lines[lines.length - 1];\n const next = [...cur, w].join(\" \");\n if (cur.length > 0 && em[next] * F > maxW) lines.push([w]);\n else cur.push(w);\n }\n return lines;\n}\n\nfunction layoutScene(\n words: string[],\n m: Metrics,\n boxW: number,\n boxH: number,\n maxF: number,\n): { lines: string[][]; F: number } {\n let lo = 4;\n let hi = maxF;\n const widest = Math.max(...words.map((w) => m.em[w]), 0.01);\n hi = Math.min(hi, boxW / widest);\n for (let i = 0; i < 30; i++) {\n const mid = (lo + hi) / 2;\n const lines = wrap(words, m.em, mid, boxW);\n if (lines.length * LH * mid <= boxH) lo = mid;\n else hi = mid;\n }\n return { lines: wrap(words, m.em, lo, boxW), F: lo };\n}\n\nfunction splitPunct(word: string) {\n const match = word.match(PUNCT);\n const punct = match ? match[0] : \"\";\n return { core: word.slice(0, word.length - punct.length), punct };\n}\n\nfunction prefixes(s: string) {\n const chars = Array.from(s);\n return chars.map((_, i) => chars.slice(0, i).join(\"\"));\n}\n\nexport function BatonWord({\n scenes = [\n \"The app is ready for launch.\",\n \"Launch day needs a film.\",\n \"Film it in code.\",\n ],\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: BatonWordProps) {\n const t = useCurrentFrame() * speed;\n const { width: W, height: H, durationInFrames } = useVideoConfig();\n const c = THEMES[theme];\n const hi = accent ?? c.accent;\n const stage =\n background === \"transparent\" ? \"transparent\" : (background ?? c.bg);\n\n const list = scenes.length > 0 ? scenes : [\"\"];\n const sceneWords = list.map((s) => s.split(\" \").filter(Boolean));\n const batons = sceneWords.slice(0, -1).map((ws, i) => {\n const a = splitPunct(ws[ws.length - 1] ?? \"\");\n const b = splitPunct(sceneWords[i + 1][0] ?? \"\");\n return { a, b };\n });\n\n const texts: string[] = [];\n for (const ws of sceneWords) {\n for (let i = 0; i < ws.length; i++) {\n texts.push(ws[i]);\n for (let j = i; j < ws.length; j++) {\n texts.push(ws.slice(i, j + 1).join(\" \"));\n texts.push(`${ws.slice(i, j + 1).join(\" \")} `);\n }\n }\n }\n for (const bt of batons) {\n texts.push(\n bt.a.core,\n bt.b.core,\n ...prefixes(bt.a.core),\n ...prefixes(bt.b.core),\n );\n }\n const m = useMetrics(texts);\n\n if (!m) {\n return (\n <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n );\n }\n\n const portrait = H > W * 1.1;\n const marginX = W * (portrait ? 0.08 : 0.065);\n const boxW = W - marginX * 2;\n const boxH = H * (portrait ? 0.66 : 0.84);\n const maxF = portrait ? W * 0.26 : H * 0.36;\n\n const layouts: SceneLayout[] = sceneWords.map((ws) => {\n const { lines, F } = layoutScene(ws, m, boxW, boxH, maxF);\n const placed: Placed[] = [];\n lines.forEach((ln, li) => {\n ln.forEach((w, wi) => {\n const before = ln.slice(0, wi).join(\" \");\n placed.push({\n word: w,\n line: li,\n x: wi === 0 ? 0 : m.em[`${before} `] * F,\n });\n });\n });\n const blockH = lines.length * LH * F;\n return {\n words: placed,\n lines: lines.length,\n F,\n left: marginX,\n top: (H - blockH) / 2,\n };\n });\n\n const n = list.length;\n const fixedIn = (k: number) =>\n RISE + Math.max(0, sceneWords[k].length - 1) * STAGGER;\n let fixed = FINAL_HOLD;\n for (let k = 0; k < n; k++) fixed += fixedIn(k);\n const clearIn = (k: number) =>\n Math.max(0, sceneWords[k].length - 2) * DROP_STAGGER + DROP - 3;\n for (let k = 0; k < n - 1; k++) fixed += clearIn(k) + TRAVEL - 3;\n const read = Math.max(14, Math.floor((durationInFrames - fixed) / n));\n\n const inAt: number[] = [];\n const outAt: number[] = [];\n const travelAt: number[] = [];\n let cursor = 0;\n for (let k = 0; k < n; k++) {\n inAt.push(cursor);\n const settled = cursor + fixedIn(k);\n outAt.push(settled + read);\n const go = settled + read + (k < n - 1 ? clearIn(k) : 0);\n travelAt.push(go);\n cursor = go + TRAVEL - 3;\n }\n\n const lineTop = (L: SceneLayout, line: number) => L.top + line * LH * L.F;\n const textStyle = (size: number): React.CSSProperties => ({\n position: \"absolute\",\n height: size * LH,\n lineHeight: `${size * LH}px`,\n whiteSpace: \"pre\",\n fontFamily: syne.fontFamily,\n fontWeight: WGHT,\n fontSize: size,\n letterSpacing: `${TRACK}em`,\n });\n\n const nodes: React.ReactNode[] = [];\n\n for (let k = 0; k < n; k++) {\n const L = layouts[k];\n const ws = L.words;\n const first = k > 0 ? 1 : 0;\n const last = k < n - 1 ? ws.length - 1 : ws.length;\n if (t < inAt[k] - 2) continue;\n\n ws.forEach((pw, i) => {\n if (i < first || i >= last) return;\n const order = i - first;\n const p = interpolate(\n t,\n [inAt[k] + order * STAGGER, inAt[k] + order * STAGGER + RISE],\n [0, 1],\n { ...CLAMP, easing: EASE_OUT },\n );\n const q =\n k < n - 1\n ? interpolate(\n t,\n [outAt[k] + i * DROP_STAGGER, outAt[k] + i * DROP_STAGGER + DROP],\n [0, 1],\n { ...CLAMP, easing: EASE_IN },\n )\n : 0;\n if (p <= 0 || q >= 1) return;\n const leadColor = k > 0 && i === 0 ? hi : c.ink;\n const y = (1 - p) * L.F * 1.2 + q * L.F * 0.14;\n nodes.push(\n <div\n key={`s${k}w${i}`}\n style={{\n position: \"absolute\",\n left: L.left + pw.x,\n top: lineTop(L, pw.line) - L.F * 0.12,\n height: L.F * (LH + 0.3),\n overflow: \"hidden\",\n }}\n >\n <div\n style={{\n ...textStyle(L.F),\n position: \"relative\",\n top: L.F * 0.12,\n color: leadColor,\n opacity: 1 - q,\n transform: `translateY(${y.toFixed(2)}px)`,\n }}\n >\n {pw.word}\n </div>\n </div>,\n );\n });\n }\n\n for (let k = 0; k < n - 1; k++) {\n const A = layouts[k];\n const B = layouts[k + 1];\n const { a, b } = batons[k];\n const aw = A.words[A.words.length - 1];\n const bw = B.words[0];\n const solo = k > 0 && A.words.length === 1;\n const handsOn = k + 1 < n - 1 && B.words.length === 1;\n const startIn = inAt[k] + (A.words.length - 1 - (k > 0 ? 1 : 0)) * STAGGER;\n if (solo && t < outAt[k]) continue;\n if (!solo && t < startIn - 1) continue;\n if (handsOn && t >= outAt[k + 1]) continue;\n\n const pIn = solo\n ? 1\n : interpolate(t, [startIn, startIn + RISE], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n const go = interpolate(t, [travelAt[k], travelAt[k] + TRAVEL], [0, 1], {\n ...CLAMP,\n easing: EASE_IO,\n });\n const lit = solo\n ? 1\n : interpolate(t, [outAt[k], outAt[k] + 8], [0, 1], CLAMP);\n const swap = interpolate(go, [0.35, 0.7], [0, 1], CLAMP);\n const punctOut = interpolate(t, [outAt[k], outAt[k] + 8], [1, 0], CLAMP);\n\n const F = A.F + (B.F - A.F) * go;\n const x0 = A.left + aw.x;\n const y0 = lineTop(A, aw.line);\n const x1 = B.left + bw.x;\n const y1 = lineTop(B, bw.line);\n const arc = Math.sin(go * Math.PI) * Math.min(W, H) * 0.04;\n const x = x0 + (x1 - x0) * go;\n const y = y0 + (y1 - y0) * go - arc;\n\n const qOut =\n k + 1 < n - 1 && !handsOn\n ? interpolate(t, [outAt[k + 1], outAt[k + 1] + DROP], [0, 1], {\n ...CLAMP,\n easing: EASE_IN,\n })\n : 0;\n if (qOut >= 1) continue;\n const color = interpolateColors(lit, [0, 1], [c.ink, hi]);\n\n const aChars = Array.from(a.core);\n const bChars = Array.from(b.core);\n const same = aChars.length === bChars.length;\n const pa = prefixes(a.core).map((s) => (s ? m.em[s] : 0));\n const pb = prefixes(b.core).map((s) => (s ? m.em[s] : 0));\n const glyphs: React.ReactNode[] = [];\n if (same) {\n aChars.forEach((ch, i) => {\n const gx = (pa[i] + (pb[i] - pa[i]) * swap) * F;\n if (ch === bChars[i]) {\n glyphs.push(\n <span key={`g${i}`} style={{ ...textStyle(F), left: gx, top: 0 }}>\n {ch}\n </span>,\n );\n } else {\n glyphs.push(\n <span\n key={`ga${i}`}\n style={{ ...textStyle(F), left: gx, top: 0, opacity: 1 - swap }}\n >\n {ch}\n </span>,\n <span\n key={`gb${i}`}\n style={{ ...textStyle(F), left: gx, top: 0, opacity: swap }}\n >\n {bChars[i]}\n </span>,\n );\n }\n });\n } else {\n glyphs.push(\n <span\n key=\"wa\"\n style={{ ...textStyle(F), left: 0, top: 0, opacity: 1 - swap }}\n >\n {a.core}\n </span>,\n <span\n key=\"wb\"\n style={{ ...textStyle(F), left: 0, top: 0, opacity: swap }}\n >\n {b.core}\n </span>,\n );\n }\n const coreW = (m.em[a.core] + (m.em[b.core] - m.em[a.core]) * swap) * F;\n if (a.punct && punctOut > 0) {\n glyphs.push(\n <span\n key=\"pa\"\n style={{\n ...textStyle(F),\n left: coreW,\n top: 0,\n opacity: punctOut,\n color: c.ink,\n }}\n >\n {a.punct}\n </span>,\n );\n }\n if (b.punct) {\n const pIn2 = interpolate(go, [0.8, 1], [0, 1], CLAMP);\n glyphs.push(\n <span\n key=\"pb\"\n style={{\n ...textStyle(F),\n left: coreW,\n top: 0,\n opacity: pIn2,\n color: c.ink,\n }}\n >\n {b.punct}\n </span>,\n );\n }\n\n const dropY = qOut * B.F * 0.14;\n const riseY = (1 - pIn) * A.F * 1.2;\n const masked = go <= 0 || qOut > 0;\n const maskL = go <= 0 ? A : B;\n const inner = (\n <div\n style={{\n position: \"absolute\",\n left: masked ? 0 : x,\n top: masked ? maskL.F * 0.12 : y,\n width: 1,\n height: F * LH,\n color,\n opacity: 1 - qOut,\n transform: `translateY(${(riseY + dropY).toFixed(2)}px)`,\n }}\n >\n {glyphs}\n </div>\n );\n nodes.push(\n masked ? (\n <div\n key={`b${k}`}\n style={{\n position: \"absolute\",\n left: x,\n top: y - maskL.F * 0.12,\n width: W,\n height: maskL.F * (LH + 0.3),\n overflow: \"hidden\",\n }}\n >\n {inner}\n </div>\n ) : (\n <div key={`b${k}`}>{inner}</div>\n ),\n );\n }\n\n return (\n <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n {nodes}\n </AbsoluteFill>\n );\n}\n",
"type": "registry:component",
"target": "components/remocn/baton-word.tsx"
}
],
"type": "registry:component"
}