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": "a-to-z",
"title": "a to Z",
"description": "An alphabet gag where a big \"Z\" and \"is for zero lock-in.\" land on orange.",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/a-to-z/index.tsx",
"content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Unbounded\";\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 unbounded = loadFont(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\nexport interface AToZProps {\n entries?: string[];\n finale?: string;\n connector?: string;\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst DEFAULT_ENTRIES = [\n \"any aspect ratio.\",\n \"brand fonts.\",\n \"captions.\",\n \"dark mode.\",\n];\n\nconst ALPHABET = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst ELLIPSIS = \"…\";\nconst FONT_PX = 200;\nconst TRACK = -0.03;\nconst LETTER_TRACK = -0.02;\n\nconst EASE_ROLL = Easing.bezier(0.6, 0, 0.1, 1);\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst DWELLS = [28, 19, 17, 17];\nconst ROLL = 7;\nconst STAGGER = 2;\nconst FINAL_ROLL = 13;\nconst HOLD_MIN = 36;\n\ninterface Metrics {\n letters: Record<string, number>;\n maxLetter: number;\n words: Record<string, number>;\n space: number;\n connector: number;\n asc: number;\n desc: number;\n cap: number;\n tall: number;\n deep: number;\n}\n\nfunction measure(texts: string[], connector: string): Metrics {\n const canvas = document.createElement(\"canvas\");\n const ctx = canvas.getContext(\"2d\");\n if (!ctx) throw new Error(\"a-to-z: no 2d context\");\n const em = (s: string, w: number, track: number) => {\n ctx.font = `${w} ${FONT_PX}px \"${unbounded.fontFamily}\"`;\n return (\n ctx.measureText(s).width / FONT_PX +\n track * Math.max(0, Array.from(s).length - 1)\n );\n };\n const letters: Record<string, number> = {};\n let maxLetter = 0;\n for (const ch of ALPHABET) {\n letters[ch] = em(ch, 500, 0);\n maxLetter = Math.max(maxLetter, letters[ch]);\n }\n const words: Record<string, number> = {};\n for (const tx of texts) {\n for (const w of tx.split(/\\s+/)) {\n if (w && words[w] === undefined) words[w] = em(w, 500, TRACK);\n }\n }\n ctx.font = `500 ${FONT_PX}px \"${unbounded.fontFamily}\"`;\n const fm = ctx.measureText(\"Hgy\");\n return {\n letters,\n maxLetter,\n words,\n space: em(\" \", 500, 0),\n connector: em(connector, 400, TRACK),\n asc: fm.fontBoundingBoxAscent / FONT_PX,\n desc: fm.fontBoundingBoxDescent / FONT_PX,\n cap: ctx.measureText(\"H\").actualBoundingBoxAscent / FONT_PX,\n tall: ctx.measureText(\"dlkhfH\").actualBoundingBoxAscent / FONT_PX,\n deep: ctx.measureText(\"gjpqy,\").actualBoundingBoxDescent / FONT_PX,\n };\n}\n\nfunction useMetrics(texts: string[], connector: string) {\n const key = `${connector}\\u0001${texts.join(\"\\u0002\")}`;\n const [state, setState] = useState<{ key: string; m: Metrics } | null>(null);\n const [handle] = useState(() => delayRender(\"a-to-z: measuring\"));\n const released = useRef(false);\n const input = useRef({ texts, connector });\n input.current = { texts, connector };\n useEffect(() => {\n let live = true;\n Promise.all([\n unbounded.waitUntilDone(),\n document.fonts.load(`500 100px \"${unbounded.fontFamily}\"`),\n document.fonts.load(`400 100px \"${unbounded.fontFamily}\"`),\n ])\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n setState({\n key,\n m: measure(input.current.texts, input.current.connector),\n });\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\nfunction wrap(text: string, m: Metrics, max: number) {\n const words = text.trim().split(/\\s+/);\n const widths = words.map((w) => m.words[w] ?? w.length * 0.7);\n const run = (limit: number) => {\n const lines: string[][] = [];\n let line: string[] = [];\n let w = 0;\n words.forEach((word, i) => {\n const add = line.length === 0 ? widths[i] : w + m.space + widths[i];\n if (line.length > 0 && add > limit) {\n lines.push(line);\n line = [word];\n w = widths[i];\n } else {\n line.push(word);\n w = add;\n }\n });\n if (line.length) lines.push(line);\n return lines;\n };\n const first = run(max);\n let best = first;\n for (let k = 1; k <= 20; k++) {\n const next = run(max * (1 - k * 0.025));\n if (next.length !== first.length) break;\n best = next;\n }\n return {\n lines: best.map((l) => l.join(\" \")),\n widest: Math.max(\n ...best.map((l) =>\n l.reduce((s, w, i) => s + (m.words[w] ?? 0) + (i > 0 ? m.space : 0), 0),\n ),\n ),\n };\n}\n\nfunction initialOf(text: string) {\n const ch = text.trim().charAt(0).toUpperCase();\n return ALPHABET.includes(ch) ? ch : \"A\";\n}\n\ninterface Step<T> {\n at: number;\n value: T;\n roll: number;\n ease: (x: number) => number;\n}\n\nfunction stepAt<T>(steps: Step<T>[], t: number) {\n let k = 0;\n for (let i = 0; i < steps.length; i++) if (t >= steps[i].at) k = i;\n return { cur: steps[k], prev: k > 0 ? steps[k - 1] : null };\n}\n\nfunction flickDurations(count: number) {\n return Array.from({ length: count }, (_, i) => {\n const fromStart = i;\n const fromEnd = count - 1 - i;\n if (fromStart === 0) return 3;\n if (fromStart === 1) return 2;\n if (fromEnd === 0) return 4;\n if (fromEnd === 1) return 3;\n if (fromEnd === 2) return 2;\n return 1;\n });\n}\n\nexport function AToZ({\n entries = DEFAULT_ENTRIES,\n finale = \"zero lock-in.\",\n connector = \"is for\",\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: AToZProps) {\n const t = useCurrentFrame() * speed;\n const { width: W, height: H, durationInFrames } = useVideoConfig();\n const dark = theme === \"dark\";\n const field = dark ? (accent ?? \"#FF5A1F\") : \"#FFFFFF\";\n const stage =\n background === \"transparent\" ? \"transparent\" : (background ?? field);\n const ink = \"#000000\";\n const letterInk = dark ? \"#000000\" : (accent ?? \"#E8480F\");\n\n const list = entries.length > 0 ? entries : DEFAULT_ENTRIES;\n const m = useMetrics([...list, finale, ELLIPSIS], connector);\n\n if (!m) {\n return (\n <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n );\n }\n\n const total = durationInFrames * speed;\n const lastLetter = initialOf(list[list.length - 1]);\n const finalLetter = initialOf(finale);\n const between =\n ALPHABET.indexOf(finalLetter) > ALPHABET.indexOf(lastLetter) + 1\n ? ALPHABET.slice(\n ALPHABET.indexOf(lastLetter) + 1,\n ALPHABET.indexOf(finalLetter),\n ).split(\"\")\n : [];\n const flick = flickDurations(between.length);\n const flickSum = flick.reduce((s, d) => s + d, 0);\n const natural = list.map((_, k) => DWELLS[Math.min(k, DWELLS.length - 1)]);\n const natSum = natural.reduce((s, d) => s + d, 0);\n const budget = total - HOLD_MIN - FINAL_ROLL - flickSum - 4;\n const fit = natSum > 0 ? Math.min(1, budget / natSum) : 1;\n const starts: number[] = [];\n let at = 0;\n for (let k = 0; k < list.length; k++) {\n starts.push(at);\n at += Math.max(8, Math.round(natural[k] * fit));\n }\n const ellipsisAt = at;\n const flickAt = between.length > 0 ? ellipsisAt + 2 : ellipsisAt;\n const finaleAt = flickAt + flickSum;\n\n const letterSteps: Step<string>[] = list.map((e, k) => ({\n at: starts[k],\n value: initialOf(e),\n roll: ROLL,\n ease: EASE_ROLL,\n }));\n let fa = flickAt;\n between.forEach((ch, i) => {\n letterSteps.push({ at: fa, value: ch, roll: 0, ease: EASE_ROLL });\n fa += flick[i];\n });\n letterSteps.push({\n at: finaleAt,\n value: finalLetter,\n roll: FINAL_ROLL,\n ease: EASE_OUT,\n });\n\n const portrait = H > W * 1.1;\n const marginX = W * (portrait ? 0.09 : 0.075);\n\n const shown = [...list, finale].map(initialOf);\n const shownMax = Math.max(...shown.map((ch) => m.letters[ch] ?? 0));\n const all = [...list, finale];\n const widestWord = Math.max(\n ...all.flatMap((s) => s.split(/\\s+/).map((w) => m.words[w] ?? 0)),\n );\n const singleWidest = Math.max(...all.map((s) => wrap(s, m, 1e9).widest));\n const lead = Math.max(1.04, m.tall + m.deep + 0.1);\n\n let G: number;\n let F: number;\n let xL: number;\n let textX: number;\n let textMax: number;\n let yB: number;\n let connectorBase: number;\n if (portrait) {\n G = Math.min(H * 0.26, W * 0.62) / m.cap;\n xL = marginX;\n textX = marginX;\n textMax = W - marginX * 2;\n F = Math.min(\n W * 0.1,\n textMax / Math.max(widestWord, 0.1),\n (textMax * 2) / Math.max(singleWidest * 1.1, 0.1),\n );\n yB = H * 0.44;\n connectorBase = yB + F * 1.1;\n } else {\n const gap0 = W * 0.04;\n G = (H * 0.5) / m.cap;\n G = Math.min(G, (marginX * 0.8) / Math.max(m.maxLetter - shownMax, 0.01));\n const colW = shownMax * G;\n const avail = W - marginX * 2 - colW - gap0;\n F = Math.min(\n H * 0.15,\n avail / Math.max(widestWord, 0.1),\n (avail * 2) / Math.max(singleWidest * 1.1, 0.1),\n );\n const textW = Math.max(...all.map((s) => wrap(s, m, avail / F).widest)) * F;\n const groupW = colW + gap0 + textW;\n const left = Math.max(marginX, (W - groupW) / 2);\n xL = left + colW;\n textX = xL + gap0;\n textMax = W - marginX - textX;\n yB = H / 2 + (m.cap * G) / 2;\n connectorBase = yB - m.cap * G + F * 0.5 * m.cap;\n }\n const capPx = m.cap * G;\n const capTop = yB - capPx;\n const lineSets = [...list, ELLIPSIS, finale].map(\n (s) => wrap(s, m, textMax / F).lines,\n );\n const Fc = F * 0.5;\n const lineH = lead * F;\n const itemTop0 = connectorBase + F * (m.deep + 0.25 + m.tall);\n\n const textSteps: Step<string[]>[] = list.map((_, k) => ({\n at: starts[k],\n value: lineSets[k],\n roll: ROLL,\n ease: EASE_ROLL,\n }));\n if (between.length > 0) {\n textSteps.push({\n at: ellipsisAt,\n value: lineSets[list.length],\n roll: ROLL,\n ease: EASE_ROLL,\n });\n }\n textSteps.push({\n at: finaleAt + 2,\n value: lineSets[list.length + 1],\n roll: FINAL_ROLL,\n ease: EASE_OUT,\n });\n\n const rise = (delay: number) =>\n interpolate(t, [delay - 3, delay + 11], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n\n const rollPair = (\n key: string,\n winTop: number,\n winH: number,\n render: (value: string, y: number, k: string) => React.ReactNode,\n outgoing: string | null,\n incoming: string | null,\n p: number,\n winStyle?: React.CSSProperties,\n ) => {\n const travel = winH * 1.02;\n const kids: React.ReactNode[] = [];\n if (outgoing !== null && p < 1)\n kids.push(render(outgoing, -travel * p, \"o\"));\n if (incoming !== null && p > 0)\n kids.push(render(incoming, travel * (1 - p), \"i\"));\n return (\n <div\n key={key}\n style={{\n position: \"absolute\",\n top: winTop,\n height: winH,\n overflow: \"hidden\",\n ...winStyle,\n }}\n >\n {kids}\n </div>\n );\n };\n\n const nodes: React.ReactNode[] = [];\n\n {\n const { cur, prev } = stepAt(letterSteps, t);\n const p = !prev\n ? rise(0)\n : cur.roll === 0\n ? 1\n : interpolate(t, [cur.at, cur.at + cur.roll], [0, 1], {\n ...CLAMP,\n easing: cur.ease,\n });\n const pad = G * 0.06;\n const winTop = capTop - pad;\n const winH = capPx + pad * 2;\n const lineBox = (m.asc + m.desc) * G;\n const textTop = yB - m.asc * G - winTop;\n nodes.push(\n rollPair(\n \"letter\",\n winTop,\n winH,\n (v, y, k) => (\n <div\n key={k}\n style={{\n position: \"absolute\",\n ...(portrait ? { left: 0 } : { right: 0 }),\n top: textTop + y,\n height: lineBox,\n lineHeight: `${lineBox}px`,\n fontFamily: unbounded.fontFamily,\n fontWeight: 500,\n fontSize: G,\n letterSpacing: `${LETTER_TRACK}em`,\n color: letterInk,\n whiteSpace: \"pre\",\n }}\n >\n {v}\n </div>\n ),\n prev ? prev.value : null,\n cur.value,\n p,\n portrait\n ? { left: marginX, width: W - marginX }\n : { left: 0, width: xL },\n ),\n );\n }\n\n const textLine = (size: number, weight: number, color: string) =>\n function TextLine(v: string, y: number, k: string) {\n const lineBox = (m.asc + m.desc) * size;\n return (\n <div\n key={k}\n style={{\n position: \"absolute\",\n left: 0,\n top: (m.tall + 0.05 - m.asc) * size + y,\n height: lineBox,\n lineHeight: `${lineBox}px`,\n fontFamily: unbounded.fontFamily,\n fontWeight: weight,\n fontSize: size,\n letterSpacing: `${TRACK}em`,\n color,\n whiteSpace: \"pre\",\n }}\n >\n {v}\n </div>\n );\n };\n\n {\n const cp = rise(3);\n const winTop = connectorBase - Fc * (m.tall + 0.05);\n nodes.push(\n rollPair(\n \"connector\",\n winTop,\n Fc * (m.tall + m.deep + 0.1),\n textLine(Fc, 400, ink),\n null,\n connector,\n cp,\n { left: textX, right: 0 },\n ),\n );\n }\n\n {\n const { cur, prev } = stepAt(textSteps, t);\n const prevLines = prev ? prev.value : [];\n const curLines = cur.value;\n const slots = Math.max(prevLines.length, curLines.length);\n const slotOf = (lines: string[], s: number) => {\n if (portrait) return lines[s] ?? null;\n const i = lines.length - 1 - s;\n return i >= 0 ? lines[i] : null;\n };\n for (let s = 0; s < slots; s++) {\n const base = portrait ? itemTop0 + s * lineH : yB - s * lineH;\n const order = portrait ? s : slots - 1 - s;\n const delay = order * STAGGER;\n const p = !prev\n ? rise(5 + delay)\n : interpolate(t, [cur.at + delay, cur.at + delay + cur.roll], [0, 1], {\n ...CLAMP,\n easing: cur.ease,\n });\n const winTop = base - F * (m.tall + 0.05);\n nodes.push(\n rollPair(\n `s${s}`,\n winTop,\n F * (m.tall + m.deep + 0.1),\n textLine(F, 500, ink),\n slotOf(prevLines, s),\n slotOf(curLines, s),\n p,\n { left: textX, right: 0 },\n ),\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/a-to-z.tsx"
}
],
"type": "registry:component"
}