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": "riddle-clues",
"title": "Riddle Clues",
"description": "A tiny teaser line, \"Give up? Answer on the 14th.\", sits low in an empty dark frame.",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/riddle-clues/index.tsx",
"content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/FunnelDisplay\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n AbsoluteFill,\n continueRender,\n delayRender,\n Easing,\n interpolate,\n useCurrentFrame,\n useVideoConfig,\n} from \"remotion\";\n\nconst funnel = loadFont(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\nexport interface RiddleClue {\n line: string;\n turn: string;\n}\n\nexport interface RiddleCluesProps {\n clues?: RiddleClue[];\n closer?: RiddleClue;\n label?: string;\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst DEFAULT_CLUES: RiddleClue[] = [\n { line: \"It has scenes,\", turn: \"but no camera.\" },\n { line: \"It has a timeline,\", turn: \"but no editor.\" },\n { line: \"It has a release date,\", turn: \"but no name yet.\" },\n];\n\nconst DEFAULT_CLOSER: RiddleClue = {\n line: \"Give up?\",\n turn: \"Answer on the 14th.\",\n};\n\nconst THEMES = {\n dark: {\n bg: \"#0B0B0F\",\n ink: \"#FFFFFF\",\n dim: \"rgba(255,255,255,0.42)\",\n accent: \"#B79CFF\",\n },\n light: {\n bg: \"#FAFAFA\",\n ink: \"#121216\",\n dim: \"rgba(18,18,22,0.45)\",\n accent: \"#6B4FD8\",\n },\n} as const;\n\nconst WGHT = 400;\nconst TRACK = -0.012;\nconst MEASURE_PX = 100;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_MOVE = Easing.bezier(0.65, 0, 0.25, 1);\nconst EASE_IN = Easing.bezier(0.55, 0, 0.8, 0.2);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst STEP = 56;\nconst IN_LEN = 13;\nconst STAGGER = 3;\nconst TURN_AT = 20;\nconst SHIFT_LEN = 14;\nconst OUT_AT = 50;\nconst OUT_LEN = 9;\nconst MIN_HOLD = 30;\n\nlet measureCtx: CanvasRenderingContext2D | null = null;\n\nfunction em(s: string) {\n if (!measureCtx) {\n measureCtx = document.createElement(\"canvas\").getContext(\"2d\");\n }\n if (!measureCtx) throw new Error(\"riddle-clues: no 2d context\");\n measureCtx.font = `${WGHT} ${MEASURE_PX}px \"${funnel.fontFamily}\"`;\n return (\n measureCtx.measureText(s).width / MEASURE_PX + TRACK * Array.from(s).length\n );\n}\n\nfunction useFontsReady() {\n const [ready, setReady] = useState(false);\n const [handle] = useState(() => delayRender(\"riddle-clues: fonts\"));\n const released = useRef(false);\n useEffect(() => {\n let live = true;\n Promise.all([\n funnel.waitUntilDone(),\n document.fonts.load(`${WGHT} 100px \"${funnel.fontFamily}\"`),\n ])\n .catch(() => undefined)\n .then(() => {\n if (live) setReady(true);\n if (!released.current) {\n released.current = true;\n continueRender(handle);\n }\n });\n return () => {\n live = false;\n };\n }, [handle]);\n return ready;\n}\n\ninterface Word {\n text: string;\n x: number;\n}\n\ninterface Part {\n words: Word[];\n w: number;\n}\n\ninterface Item {\n line: Part[];\n turn: Part[];\n inline: boolean;\n}\n\nfunction split(s: string) {\n return s.split(/\\s+/).filter(Boolean);\n}\n\nfunction toPart(words: string[]): Part {\n const space = em(\" \");\n let x = 0;\n const out = words.map((text) => {\n const at = x;\n x += em(text) + space;\n return { text, x: at };\n });\n return { words: out, w: em(words.join(\" \")) };\n}\n\nfunction greedy(words: string[], maxEm: number): string[][] {\n const rows: string[][] = [];\n let cur: string[] = [];\n for (const w of words) {\n const next = [...cur, w];\n if (cur.length > 0 && em(next.join(\" \")) > maxEm) {\n rows.push(cur);\n cur = [w];\n } else {\n cur = next;\n }\n }\n if (cur.length) rows.push(cur);\n return rows;\n}\n\nfunction wrap(s: string, maxEm: number): Part[] {\n const words = split(s);\n let rows = greedy(words, maxEm);\n let m = maxEm;\n for (let guard = 0; guard < 30 && rows.length > 1; guard++) {\n m *= 0.96;\n const next = greedy(words, m);\n if (next.length > rows.length) break;\n rows = next;\n }\n return rows.map(toPart);\n}\n\nfunction buildLayout(items: RiddleClue[], W: number, H: number) {\n const portrait = H > W * 1.1;\n const maxW = W * (portrait ? 0.8 : 0.58);\n const F = Math.min(W, H) * (portrait ? 0.052 : 0.05);\n const gap = em(\" \");\n const out: Item[] = items.map((it) => {\n const line = wrap(it.line, maxW / F);\n const turn = wrap(it.turn, maxW / F);\n const inline =\n line.length === 1 &&\n turn.length === 1 &&\n (line[0].w + gap + turn[0].w) * F <= maxW;\n return { line, turn, inline };\n });\n return { items: out, F, gap, portrait };\n}\n\nexport function RiddleClues({\n clues = DEFAULT_CLUES,\n closer = DEFAULT_CLOSER,\n label = \"Clue\",\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: RiddleCluesProps) {\n const frame = useCurrentFrame();\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 const ready = useFontsReady();\n\n const all = useMemo(() => [...clues, closer], [clues, closer]);\n const layout = useMemo(\n () => (ready ? buildLayout(all, W, H) : null),\n [ready, all, W, H],\n );\n\n if (!layout) {\n return (\n <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n );\n }\n\n const { F, gap } = layout;\n const n = layout.items.length;\n const lastTurnWords = layout.items[n - 1].turn.reduce(\n (acc, r) => acc + r.words.length,\n 0,\n );\n const motionEnd =\n (n - 1) * STEP + TURN_AT + 4 + (lastTurnWords - 1) * STAGGER + IN_LEN;\n const room = durationInFrames * speed - MIN_HOLD;\n const k = Math.max(0.6, Math.min(1, room / motionEnd));\n const t = (frame * speed) / k;\n\n const rowH = F * 1.34;\n const pitch = F * 1.36;\n const cy = H / 2;\n const LF = F * 0.38;\n\n const nodes = layout.items.map((item, i) => {\n const s = i * STEP;\n const local = t - s;\n const isLast = i === n - 1;\n if (local < -2) return null;\n if (!isLast && local > OUT_AT + OUT_LEN + 2) return null;\n\n const shift = interpolate(local, [TURN_AT, TURN_AT + SHIFT_LEN], [0, 1], {\n ...CLAMP,\n easing: EASE_MOVE,\n });\n const out = isLast\n ? 0\n : interpolate(local, [OUT_AT, OUT_AT + OUT_LEN], [0, 1], {\n ...CLAMP,\n easing: EASE_IN,\n });\n\n const placed: {\n part: Part;\n x: number;\n y: number;\n turn: boolean;\n first: number;\n }[] = [];\n let topY: number;\n if (item.inline) {\n const lineW = item.line[0].w * F;\n const turnW = item.turn[0].w * F;\n const totalW = lineW + gap * F + turnW;\n const x0 = W / 2 - lineW / 2;\n const x1 = W / 2 - totalW / 2;\n placed.push({\n part: item.line[0],\n x: x0 + (x1 - x0) * shift,\n y: cy,\n turn: false,\n first: 0,\n });\n placed.push({\n part: item.turn[0],\n x: x0 + (x1 - x0) * shift + lineW + gap * F,\n y: cy,\n turn: true,\n first: 0,\n });\n topY = cy;\n } else {\n const nl = item.line.length;\n const nt = item.turn.length;\n const lineTop0 = cy - ((nl - 1) * pitch) / 2;\n const lineTop1 = cy - ((nl + nt - 1) * pitch) / 2;\n const lineTop = lineTop0 + (lineTop1 - lineTop0) * shift;\n let first = 0;\n item.line.forEach((part, li) => {\n placed.push({\n part,\n x: W / 2 - (part.w * F) / 2,\n y: lineTop + li * pitch,\n turn: false,\n first,\n });\n first += part.words.length;\n });\n first = 0;\n item.turn.forEach((part, ti) => {\n placed.push({\n part,\n x: W / 2 - (part.w * F) / 2,\n y: lineTop1 + (nl + ti) * pitch,\n turn: true,\n first,\n });\n first += part.words.length;\n });\n topY = lineTop;\n }\n\n const rise = (at: number) =>\n interpolate(local, [at, at + IN_LEN], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n\n const row = (\n part: Part,\n x: number,\n y: number,\n color: string,\n at: number,\n first: number,\n key: string,\n ) => (\n <div\n key={key}\n style={{\n position: \"absolute\",\n left: x - F * 0.2,\n top: y - rowH / 2,\n width: part.w * F + F * 0.5,\n height: rowH,\n overflow: \"hidden\",\n }}\n >\n {part.words.map((w, wi) => {\n const q = rise(at + (first + wi) * STAGGER);\n const dy = (1 - q) * rowH - out * rowH;\n return (\n <div\n key={`${key}-${wi}`}\n style={{\n position: \"absolute\",\n left: w.x * F + F * 0.2,\n top: 0,\n height: rowH,\n fontFamily: funnel.fontFamily,\n fontWeight: WGHT,\n fontSize: F,\n lineHeight: `${rowH}px`,\n letterSpacing: `${TRACK}em`,\n whiteSpace: \"pre\",\n color,\n transform: `translateY(${dy.toFixed(2)}px)`,\n }}\n >\n {w.text}\n </div>\n );\n })}\n </div>\n );\n\n const labelY = topY - rowH / 2 - LF * 1.9;\n const labelQ = rise(0);\n const labelText = isLast ? \"\" : `${label} ${i + 1} of ${n - 1}`;\n\n return (\n <div key={`item-${i}`} style={{ position: \"absolute\", inset: 0 }}>\n {labelText ? (\n <div\n style={{\n position: \"absolute\",\n left: 0,\n width: W,\n top: labelY,\n height: LF * 1.6,\n overflow: \"hidden\",\n textAlign: \"center\",\n }}\n >\n <div\n style={{\n fontFamily: funnel.fontFamily,\n fontWeight: WGHT,\n fontSize: LF,\n lineHeight: `${LF * 1.6}px`,\n letterSpacing: \"0.14em\",\n textTransform: \"uppercase\",\n color: c.dim,\n transform: `translateY(${((1 - labelQ) * LF * 1.6 - out * LF * 1.6).toFixed(2)}px)`,\n }}\n >\n {labelText}\n </div>\n </div>\n ) : null}\n {placed.map((p, pi) =>\n row(\n p.part,\n p.x,\n p.y,\n p.turn ? hi : c.ink,\n p.turn ? TURN_AT + 4 : 0,\n p.first,\n `r-${i}-${pi}`,\n ),\n )}\n </div>\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/riddle-clues.tsx"
}
],
"type": "registry:component"
}