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": "cut-off",
"title": "Cut Off",
"description": "A line is cut off by a white reply card: \"Or let the code cut it.\".",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/cut-off/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 useCurrentFrame,\n useVideoConfig,\n} from \"remotion\";\n\nconst syne = loadFont(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\nexport interface CutOffProps {\n line?: string;\n reply?: string;\n highlight?: string;\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n dark: {\n bg: \"#0A0A0A\",\n ink: \"#FFFFFF\",\n dim: \"rgba(255,255,255,0.94)\",\n replyBg: \"#FFFFFF\",\n replyInk: \"#0A0A0A\",\n accent: \"#FF3EA5\",\n },\n light: {\n bg: \"#FFFFFF\",\n ink: \"#000000\",\n dim: \"rgba(0,0,0,0.92)\",\n replyBg: \"#000000\",\n replyInk: \"#FFFFFF\",\n accent: \"#D61F7F\",\n },\n} as const;\n\nconst TRACK_A = -0.02;\nconst TRACK_B = -0.04;\nconst FONT_PX = 200;\nconst LH_A = 1.18;\nconst LH_B = 1.0;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_HIT = Easing.bezier(0.2, 1.35, 0.4, 1);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst LEAD_IN = 6;\nconst WORD_IN = 9;\nconst LETTER = 2;\nconst DASH_HOLD = 4;\nconst HIT = 8;\nconst HIT_GAP = 9;\nconst MIN_HOLD = 40;\n\nfunction useEm(textsA: string[], textsB: string[]) {\n const key = `${textsA.join(\"\\u0001\")}\\u0002${textsB.join(\"\\u0001\")}`;\n const [state, setState] = useState<{\n key: string;\n a: Record<string, number>;\n b: Record<string, number>;\n } | null>(null);\n const [handle] = useState(() => delayRender(\"cut-off: measuring\"));\n const released = useRef(false);\n useEffect(() => {\n let live = true;\n Promise.all([\n syne.waitUntilDone(),\n document.fonts.load(`400 100px \"${syne.fontFamily}\"`),\n document.fonts.load(`500 100px \"${syne.fontFamily}\"`),\n ])\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n const [ka, kb] = key.split(\"\\u0002\");\n const canvas = document.createElement(\"canvas\");\n const ctx = canvas.getContext(\"2d\");\n const a: Record<string, number> = {};\n const b: Record<string, number> = {};\n if (ctx) {\n ctx.font = `400 ${FONT_PX}px \"${syne.fontFamily}\"`;\n for (const s of ka.split(\"\\u0001\")) {\n a[s] =\n ctx.measureText(s).width / FONT_PX +\n TRACK_A * Array.from(s).length;\n }\n ctx.font = `500 ${FONT_PX}px \"${syne.fontFamily}\"`;\n for (const s of kb.split(\"\\u0001\")) {\n b[s] =\n ctx.measureText(s).width / FONT_PX +\n TRACK_B * Array.from(s).length;\n }\n }\n setState({ key, a, b });\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 : null;\n}\n\nfunction runs(words: string[]) {\n const out: string[] = [];\n for (let i = 0; i < words.length; i++) {\n for (let j = i; j < words.length; j++) {\n out.push(words.slice(i, j + 1).join(\" \"));\n }\n }\n return out;\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 if (cur.length > 0 && em[[...cur, w].join(\" \")] * F > maxW) lines.push([w]);\n else cur.push(w);\n }\n return lines;\n}\n\nconst bare = (w: string) =>\n w.toLowerCase().replace(/^[“\"'‘(]+|[.,!?;:…”\"'’)—–]+$/g, \"\");\n\nexport function CutOff({\n line = \"So first I export the clips, redo all the captions, then line up the mus\",\n reply = \"Or let the code cut it.\",\n highlight = \"the code\",\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: CutOffProps) {\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 transparent = background === \"transparent\";\n const stage = transparent ? \"transparent\" : (background ?? c.bg);\n\n const cleanLine = line.replace(/[\\s—–-]+$/, \"\");\n const aWords = cleanLine.split(\" \").filter(Boolean);\n const bWords = reply.split(\" \").filter(Boolean);\n const fragment = aWords[aWords.length - 1] ?? \"\";\n const em = useEm(\n runs([...aWords.slice(0, -1), `${fragment}—`]),\n runs(bWords),\n );\n\n if (!em) {\n return (\n <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n );\n }\n\n const portrait = H > W * 1.1;\n const shown = [...aWords.slice(0, -1), `${fragment}—`];\n\n const aMaxW = W * (portrait ? 0.84 : 0.8);\n const fA = portrait ? W * 0.09 : H * 0.122;\n const aLines = wrap(shown, em.a, fA, aMaxW);\n const aBlockH = aLines.length * LH_A * fA;\n const aLeft = W * (portrait ? 0.09 : 0.08);\n const aTop = (H - aBlockH) / 2;\n\n const bBoxW = W * (portrait ? 0.84 : 0.86);\n const bBoxH = H * (portrait ? 0.5 : 0.62);\n const widestB = Math.max(...bWords.map((w) => em.b[w] ?? 0), 0.01);\n let lo = 4;\n let top = Math.min(portrait ? W * 0.26 : H * 0.34, bBoxW / widestB);\n for (let i = 0; i < 28; i++) {\n const mid = (lo + top) / 2;\n if (wrap(bWords, em.b, mid, bBoxW).length * LH_B * mid <= bBoxH) lo = mid;\n else top = mid;\n }\n const fB = lo;\n const bLines = wrap(bWords, em.b, fB, bBoxW);\n const bLeft = W * (portrait ? 0.08 : 0.07);\n const bBottom = H * (portrait ? 0.9 : 0.86);\n const bTop = bBottom - bLines.length * LH_B * fB;\n\n const fragLetters = Array.from(fragment).length;\n const fixedTail = fragLetters * LETTER + DASH_HOLD + HIT_GAP + HIT + MIN_HOLD;\n const gap = Math.max(\n 3,\n Math.min(\n 7,\n (durationInFrames - LEAD_IN - fixedTail) / Math.max(1, aWords.length - 1),\n ),\n );\n const fragAt = LEAD_IN + (aWords.length - 1) * gap;\n const dashAt = fragAt + fragLetters * LETTER;\n const cutAt = dashAt + DASH_HOLD;\n\n const hlTokens = highlight.split(\" \").filter(Boolean).map(bare);\n const hlFlags = bWords.map(() => false);\n for (\n let i = 0;\n i + hlTokens.length <= bWords.length && hlTokens.length > 0;\n i++\n ) {\n if (hlTokens.every((h, j) => bare(bWords[i + j]) === h)) {\n for (let j = 0; j < hlTokens.length; j++) hlFlags[i + j] = true;\n break;\n }\n }\n let splitAt = bWords.findIndex((w) => /[,;:—]$/.test(w)) + 1;\n if (splitAt <= 0 || splitAt >= bWords.length) {\n const firstHl = hlFlags.indexOf(true);\n splitAt = firstHl > 0 ? firstHl : Math.ceil(bWords.length / 2);\n }\n\n const textBase = (\n size: number,\n wght: number,\n track: number,\n lh: number,\n ): React.CSSProperties => ({\n position: \"absolute\",\n whiteSpace: \"pre\",\n fontFamily: syne.fontFamily,\n fontWeight: wght,\n fontSize: size,\n lineHeight: `${size * lh}px`,\n height: size * lh,\n letterSpacing: `${track}em`,\n });\n\n const before = t < cutAt;\n const nodes: React.ReactNode[] = [];\n if (before) {\n let idx = 0;\n aLines.forEach((ln, li) => {\n ln.forEach((w, wi) => {\n const i = idx++;\n const before = ln.slice(0, wi).join(\" \");\n const x = wi === 0 ? 0 : (em.a[`${before} ${w}`] ?? 0) - (em.a[w] ?? 0);\n const isFrag = i === shown.length - 1;\n const at = LEAD_IN + i * gap;\n let content = w;\n let p = interpolate(t, [at, at + WORD_IN], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n if (isFrag) {\n const letters = Array.from(fragment);\n const typed = Math.max(\n 0,\n Math.min(letters.length, Math.floor((t - fragAt) / LETTER) + 1),\n );\n content = letters.slice(0, typed).join(\"\");\n p = t >= fragAt ? 1 : 0;\n }\n if (p <= 0 || content === \"\") return;\n nodes.push(\n <div\n key={`a${i}`}\n style={{\n ...textBase(fA, 400, TRACK_A, LH_A),\n left: aLeft + x * fA,\n top: aTop + li * LH_A * fA,\n color: c.dim,\n opacity: p,\n transform: `translateY(${((1 - p) * fA * 0.35).toFixed(2)}px)`,\n }}\n >\n {content}\n {isFrag && t >= dashAt ? (\n <span style={{ color: hi }}>—</span>\n ) : null}\n </div>,\n );\n });\n });\n }\n\n let bi = 0;\n if (!before)\n bLines.forEach((ln, li) => {\n ln.forEach((w, wi) => {\n const i = bi++;\n const before = ln.slice(0, wi).join(\" \");\n const x = wi === 0 ? 0 : (em.b[`${before} ${w}`] ?? 0) - (em.b[w] ?? 0);\n const at = i < splitAt ? cutAt - 3 : cutAt + HIT_GAP;\n if (t < Math.max(cutAt, at)) return;\n const p = interpolate(t, [at, at + HIT], [0, 1], {\n ...CLAMP,\n easing: EASE_HIT,\n });\n nodes.push(\n <div\n key={`b${i}`}\n style={{\n position: \"absolute\",\n left: bLeft + x * fB,\n top: bTop + li * LH_B * fB - fB * 0.1,\n height: fB * (LH_B + 0.32),\n }}\n >\n <div\n style={{\n ...textBase(fB, 500, TRACK_B, LH_B),\n position: \"relative\",\n top: fB * 0.1,\n color: hlFlags[i] ? hi : c.replyInk,\n transform: `translateY(${((1 - p) * fB * 0.22).toFixed(2)}px) scale(${(1 + (1 - p) * 0.05).toFixed(4)})`,\n transformOrigin: \"0% 100%\",\n }}\n >\n {w}\n </div>\n </div>,\n );\n });\n });\n\n const field = before ? stage : transparent ? \"transparent\" : c.replyBg;\n return (\n <AbsoluteFill style={{ background: field, overflow: \"hidden\" }}>\n {nodes}\n </AbsoluteFill>\n );\n}\n",
"type": "registry:component",
"target": "components/remocn/cut-off.tsx"
}
],
"type": "registry:component"
}