Production-ready animations, transitions, backgrounds, and scenes for Remotion
TypeScript ai-agentsclaude-codedemo-videomcpmcp-servermodel-context-protocolmotion-designproduct-demoreactremotionshadcnvideo
{
"type": "remotion-element",
"version": 1,
"element": {
"assets": [],
"dependencies": [
{
"name": "@remotion/google-fonts",
"version": null
}
],
"dimensions": null,
"displayName": "Cue Word",
"initialProps": null,
"slug": "cue-word",
"sourceCode": "import { loadFont } from \"@remotion/google-fonts/Syne\";\nimport React, { forwardRef, useEffect, useImperativeHandle, useRef, useState, type ComponentProps } from \"react\";\nimport { AbsoluteFill, Easing, Interactive, Sequence, continueRender, delayRender, interpolate, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\nconst syne = loadFont(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\ntype CueCut = \"slide\" | \"drop\" | \"lift\" | \"cut\";\n\ninterface CueWordScene {\n text: string;\n cue?: string;\n cut?: CueCut;\n}\n\ninterface CueWordProps {\n scenes?: CueWordScene[];\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 accent: \"#FF3EA5\",\n onAccent: \"#0A0A0A\",\n cueOnAccent: \"#FFFFFF\",\n },\n light: {\n bg: \"#FFFFFF\",\n ink: \"#000000\",\n accent: \"#D61F7F\",\n onAccent: \"#FFFFFF\",\n cueOnAccent: \"#000000\",\n },\n} as const;\n\nconst WGHT = 500;\nconst TRACK = -0.035;\nconst FONT_PX = 200;\nconst LH = 1.06;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.7, 0, 0.2, 1);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst RISE = 15;\nconst STAGGER = 3;\nconst CUE_GAP = 7;\nconst CUE_LAND = 12;\nconst MOVE = 20;\nconst FINAL_HOLD = 36;\n\nconst DEFAULT_SCENES: CueWordScene[] = [\n { text: \"This line slides left.\", cue: \"slides left\", cut: \"slide\" },\n { text: \"The next one drops in.\", cue: \"drops in\", cut: \"drop\" },\n { text: \"And this one cuts.\", cue: \"cuts\", cut: \"cut\" },\n { text: \"Every move is one line of code.\", cue: \"one line\" },\n];\n\nfunction useEmWidths(texts: string[]) {\n const key = texts.join(\"\\u0001\");\n const [state, setState] = useState<{\n key: string;\n em: Record<string, number>;\n } | null>(null);\n const [handle] = useState(() => delayRender(\"cue-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 const canvas = document.createElement(\"canvas\");\n const ctx = canvas.getContext(\"2d\");\n const em: Record<string, number> = {};\n if (ctx) {\n ctx.font = `${WGHT} ${FONT_PX}px \"${syne.fontFamily}\"`;\n for (const s of key.split(\"\\u0001\")) {\n em[s] =\n ctx.measureText(s).width / FONT_PX + TRACK * Array.from(s).length;\n }\n }\n setState({ key, em });\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.em : null;\n}\n\nconst bare = (w: string) =>\n w.toLowerCase().replace(/^[“\"'‘(]+|[.,!?;:…”\"'’)—–]+$/g, \"\");\n\nfunction cueMask(words: string[], cue: string | undefined) {\n const flags = words.map(() => false);\n const target = (cue ?? words[words.length - 1] ?? \"\")\n .split(\" \")\n .filter(Boolean)\n .map(bare);\n if (target.length === 0) return flags;\n for (let i = words.length - target.length; i >= 0; i--) {\n if (target.every((tw, j) => bare(words[i + j]) === tw)) {\n for (let j = 0; j < target.length; j++) flags[i + j] = true;\n return flags;\n }\n }\n flags[words.length - 1] = true;\n return flags;\n}\n\nfunction lineCount(\n words: string[],\n em: Record<string, number>,\n F: number,\n maxW: number,\n) {\n let lines = 1;\n let cur = \"\";\n for (const w of words) {\n const next = cur ? `${cur} ${w}` : w;\n if (cur && em[next] * F > maxW) {\n lines++;\n cur = w;\n } else {\n cur = next;\n }\n }\n return lines;\n}\n\nfunction CueWordBase({\n scenes = DEFAULT_SCENES,\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: CueWordProps) {\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 base = transparent ? \"transparent\" : (background ?? c.bg);\n\n const list = scenes.length > 0 ? scenes : [{ text: \"\" }];\n const sceneWords = list.map((s) => s.text.split(\" \").filter(Boolean));\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 + 1; j < ws.length; j++) {\n texts.push(ws.slice(i, j + 1).join(\" \"));\n }\n }\n }\n const em = useEmWidths(texts);\n\n if (!em) {\n return <div style={{ position: \"absolute\", inset: 0, background: base }} />;\n }\n\n const portrait = H > W * 1.1;\n const boxW = W * (portrait ? 0.86 : 0.84);\n const maxLines = portrait ? 5 : 3;\n const capF = portrait ? W * 0.19 : H * 0.215;\n\n const sizes = sceneWords.map((ws) => {\n const widest = Math.max(...ws.map((w) => em[w] ?? 0), 0.01);\n let lo = 4;\n let top = Math.min(capF, boxW / widest);\n for (let i = 0; i < 28; i++) {\n const mid = (lo + top) / 2;\n if (lineCount(ws, em, mid, boxW) <= maxLines) lo = mid;\n else top = mid;\n }\n return lo;\n });\n\n const n = list.length;\n const settleOf = (k: number) =>\n k === 0\n ? RISE +\n Math.max(0, sceneWords[k].length - 1) * STAGGER +\n CUE_GAP +\n CUE_LAND\n : CUE_GAP + CUE_LAND;\n const moveOf = (k: number) => (list[k].cut === \"cut\" ? 0 : MOVE);\n let fixed = FINAL_HOLD;\n for (let k = 0; k < n; k++)\n fixed += settleOf(k) + (k < n - 1 ? moveOf(k) : 0);\n const read = Math.max(12, Math.floor((durationInFrames - fixed) / n));\n\n const arrive: number[] = [];\n const leave: number[] = [];\n let cursor = 0;\n for (let k = 0; k < n; k++) {\n arrive.push(cursor);\n const end = cursor + settleOf(k) + read;\n leave.push(end);\n cursor = end + (k < n - 1 ? moveOf(k) : 0);\n }\n\n const offsetOf = (k: number) => {\n let x = 0;\n let y = 0;\n if (k > 0) {\n const cut = list[k - 1].cut ?? \"slide\";\n const span = moveOf(k - 1);\n const p =\n span === 0\n ? t >= leave[k - 1]\n ? 1\n : 0\n : interpolate(t, [leave[k - 1], leave[k - 1] + span], [0, 1], {\n ...CLAMP,\n easing: EASE_IO,\n });\n if (cut === \"slide\") x += (1 - p) * W;\n if (cut === \"drop\") y -= (1 - p) * H;\n if (cut === \"lift\") y += (1 - p) * H;\n if (cut === \"cut\" && p < 1) y += H * 2;\n }\n if (k < n - 1) {\n const cut = list[k].cut ?? \"slide\";\n const span = moveOf(k);\n const p =\n span === 0\n ? t >= leave[k]\n ? 1\n : 0\n : interpolate(t, [leave[k], leave[k] + span], [0, 1], {\n ...CLAMP,\n easing: EASE_IO,\n });\n if (cut === \"slide\") x -= p * W;\n if (cut === \"drop\") y += p * H;\n if (cut === \"lift\") y -= p * H;\n if (cut === \"cut\" && p >= 1) y += H * 2;\n }\n return { x, y };\n };\n\n const layers: React.ReactNode[] = [];\n for (let k = 0; k < n; k++) {\n const startVis = k === 0 ? 0 : leave[k - 1];\n const endVis = k < n - 1 ? leave[k] + moveOf(k) : Number.POSITIVE_INFINITY;\n if (t < startVis || t > endVis) continue;\n const { x, y } = offsetOf(k);\n if (Math.abs(x) >= W || Math.abs(y) >= H) continue;\n\n const onAccent = k % 2 === 1;\n const field = onAccent ? hi : base;\n const inkCol = onAccent ? c.onAccent : c.ink;\n const cueCol = onAccent ? c.cueOnAccent : hi;\n const F = sizes[k];\n const ws = sceneWords[k];\n const flags = cueMask(ws, list[k].cue);\n const t0 = arrive[k];\n const restTime = (i: number) => (k === 0 ? t0 + i * STAGGER : -1e9);\n const cueAt =\n k === 0\n ? t0 + (ws.length - 1) * STAGGER + RISE + CUE_GAP - 8\n : t0 + CUE_GAP - 5;\n\n const spans = ws.map((w, i) => {\n const isCue = flags[i];\n const startAt = isCue ? cueAt : restTime(i);\n const p = interpolate(\n t,\n [startAt, startAt + (isCue ? CUE_LAND : RISE)],\n [0, 1],\n {\n ...CLAMP,\n easing: EASE_OUT,\n },\n );\n const style: React.CSSProperties = {\n display: \"inline-block\",\n overflow: \"hidden\",\n verticalAlign: \"top\",\n paddingBottom: F * 0.14,\n marginBottom: -F * 0.14,\n };\n return (\n <span key={`w${i}`}>\n <span style={style}>\n <span\n style={{\n display: \"inline-block\",\n color: isCue ? cueCol : inkCol,\n transform: `translateY(${((1 - p) * F * 1.15).toFixed(2)}px)`,\n }}\n >\n {w}\n </span>\n </span>\n {i < ws.length - 1 ? \" \" : \"\"}\n </span>\n );\n });\n\n layers.push(\n <div\n key={`s${k}`}\n style={{\n position: \"absolute\",\n inset: 0,\n background: field,\n transform: `translate(${x.toFixed(2)}px, ${y.toFixed(2)}px)`,\n display: \"flex\",\n alignItems: \"center\",\n justifyContent: \"center\",\n }}\n >\n <div\n style={{\n width: boxW,\n textAlign: \"center\",\n textWrap: \"balance\",\n fontFamily: syne.fontFamily,\n fontWeight: WGHT,\n fontSize: F,\n lineHeight: LH,\n letterSpacing: `${TRACK}em`,\n marginTop: -F * 0.06,\n }}\n >\n {spans}\n </div>\n </div>,\n );\n }\n\n return (\n <AbsoluteFill style={{ background: base, overflow: \"hidden\" }}>\n {layers}\n </AbsoluteFill>\n );\n}\n\nconst elementSchema = {\n ...Interactive.baseSchema,\n theme: {\n type: \"enum\",\n default: \"dark\",\n variants: {\n dark: {},\n light: {},\n },\n description: \"Theme\",\n },\n speed: {\n type: \"number\",\n default: 1,\n min: 0.25,\n max: 4,\n step: 0.25,\n description: \"Speed\",\n hiddenFromList: false,\n },\n ...Interactive.transformSchema,\n} as const satisfies InteractivitySchema;\n\nconst ELEMENT_PROP_KEYS = new Set([\"theme\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n theme: \"dark\",\n speed: 1,\n};\n\ntype CueWordElementProps = InteractiveBaseProps &\n InteractiveTransformProps &\n ComponentProps<typeof CueWordBase>;\n\nconst CueWordInner = forwardRef<\n HTMLDivElement,\n CueWordElementProps & { readonly controls: SequenceControls | undefined }\n>(({ controls, name, style, ...rest }, ref) => {\n const outlineRef = useRef<HTMLDivElement>(null);\n useImperativeHandle(ref, () => outlineRef.current as HTMLDivElement, []);\n const componentProps: Record<string, unknown> = { ...ELEMENT_PROP_DEFAULTS };\n const sequenceProps: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(rest)) {\n if (ELEMENT_PROP_KEYS.has(key)) {\n if (value !== undefined) componentProps[key] = value;\n } else {\n sequenceProps[key] = value;\n }\n }\n return (\n <Sequence\n layout=\"none\"\n {...sequenceProps}\n controls={controls}\n name={name ?? \"<CueWord>\"}\n outlineRef={outlineRef}\n >\n <div\n ref={outlineRef}\n style={{\n position: \"relative\",\n width: \"100%\",\n height: \"100%\",\n display: \"flex\",\n alignItems: \"center\",\n justifyContent: \"center\",\n ...style,\n }}\n >\n <CueWordBase\n {...(componentProps as unknown as ComponentProps<typeof CueWordBase>)}\n />\n </div>\n </Sequence>\n );\n});\n\nexport const CueWord = Interactive.withSchema({\n Component: CueWordInner,\n componentName: \"<CueWord>\",\n componentIdentity: null,\n schema: elementSchema,\n supportsEffects: false,\n});\n",
"durationInFrames": 176,
"installationMode": "component-owned-sequence"
},
"durationInFrames": 176
}