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": "Prompt Restyle",
"initialProps": null,
"slug": "prompt-restyle",
"sourceCode": "import { loadFont as loadGeist } from \"@remotion/google-fonts/Geist\";\nimport { loadFont as loadGeistMono } from \"@remotion/google-fonts/GeistMono\";\nimport { loadFont as loadSerif } from \"@remotion/google-fonts/InstrumentSerif\";\nimport React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState, type ComponentProps } from \"react\";\nimport { AbsoluteFill, Easing, Interactive, Sequence, continueRender, delayRender, interpolate, interpolateColors, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\nconst geist = loadGeist(\"normal\", {\n weights: [\"300\", \"400\", \"500\"],\n subsets: [\"latin\"],\n});\nconst mono = loadGeistMono(\"normal\", { weights: [\"400\"], subsets: [\"latin\"] });\nconst serif = loadSerif(\"italic\", { weights: [\"400\"], subsets: [\"latin\"] });\n\ninterface PromptRestyleProps {\n headline?: string;\n highlight?: string;\n instructions?: string[];\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst HALO = [\"#fff4ec\", \"#ffb36b\", \"#e2457a\", \"#3d4dff\"] as const;\nconst RIBBON = [\"#ff3d9a\", \"#ff8a3d\", \"#ffc83d\", \"#7a5cff\"] as const;\n\nconst THEMES = {\n dark: {\n bg: \"#0A0A0B\",\n ink: \"#F4F3F0\",\n draft: \"#6E6D73\",\n muted: \"#8E8D94\",\n hero: (a: string) =>\n `linear-gradient(100deg, ${HALO[1]} 0%, ${HALO[2]} 48%, ${a} 100%)`,\n },\n light: {\n bg: \"#F5F4F0\",\n ink: \"#111113\",\n draft: \"#A3A19B\",\n muted: \"#6E6C67\",\n hero: (a: string) =>\n `linear-gradient(100deg, ${RIBBON[1]} 0%, ${RIBBON[0]} 46%, ${a} 100%)`,\n },\n} as const;\n\nconst SERIF_SCALE = 1.22;\nconst SERIF_TRACK = -0.02;\n\ninterface Style {\n size: number;\n wght: number;\n track: number;\n lead: number;\n}\nconst S0: Style = { size: 0.42, wght: 300, track: 0.01, lead: 1.3 };\nconst S1: Style = { size: 0.8, wght: 500, track: 0.01, lead: 1.3 };\nconst S2: Style = { size: 1, wght: 500, track: -0.04, lead: 1.02 };\n\nconst TYPE_RATE = 1.6;\nconst ENTER_PAUSE = 6;\nconst APPLY = 24;\nconst NEXT_GAP = 20;\nconst FIRST_AT = 16;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\ninterface Token {\n text: string;\n hero: boolean;\n}\n\ninterface Layout {\n lines: Token[][];\n F: number;\n heroSans: number;\n heroSerif: number;\n sansAsc: number;\n sansDesc: number;\n serifAsc: number;\n serifDesc: number;\n}\n\nfunction tokenize(headline: string, highlight: string): Token[] {\n const words = headline.split(/\\s+/).filter(Boolean);\n const hw = highlight.split(/\\s+/).filter(Boolean);\n if (hw.length > 0) {\n for (let i = 0; i + hw.length <= words.length; i++) {\n const slice = words.slice(i, i + hw.length);\n const ok = slice.every((w, k) =>\n k === hw.length - 1\n ? w.replace(/[.,!?;:]+$/, \"\") === hw[k]\n : w === hw[k],\n );\n if (!ok) continue;\n const last = slice[slice.length - 1];\n const tail = last.slice(hw[hw.length - 1].length);\n const out: Token[] = words\n .slice(0, i)\n .map((text) => ({ text, hero: false }));\n out.push({ text: hw.join(\" \"), hero: true });\n if (tail) out.push({ text: tail, hero: false });\n for (const text of words.slice(i + hw.length))\n out.push({ text, hero: false });\n return out;\n }\n }\n return words.map((text) => ({ text, hero: false }));\n}\n\nfunction measureLayout(\n tokens: Token[],\n W: number,\n H: number,\n heroOn: boolean,\n): Layout {\n const canvas = document.createElement(\"canvas\");\n const ctx = canvas.getContext(\"2d\");\n if (!ctx) throw new Error(\"prompt-restyle: no 2d context\");\n const PX = 200;\n const sansW = (s: string) => {\n ctx.font = `500 ${PX}px \"${geist.fontFamily}\"`;\n return ctx.measureText(s).width / PX + S2.track * Array.from(s).length;\n };\n const serifW = (s: string) => {\n ctx.font = `400 ${PX}px \"${serif.fontFamily}\"`;\n return (\n (ctx.measureText(s).width / PX + SERIF_TRACK * Array.from(s).length) *\n SERIF_SCALE\n );\n };\n const space = sansW(\" \");\n const tokW = tokens.map((t) =>\n t.hero && heroOn ? serifW(t.text) : sansW(t.text),\n );\n const glue = tokens.map((t) => /^[.,!?;:]+$/.test(t.text));\n\n const lineWidth = (a: number, b: number) => {\n let w = 0;\n for (let i = a; i < b; i++) w += tokW[i] + (i > a && !glue[i] ? space : 0);\n return w;\n };\n const breaks = tokens.map((_, i) => i > 0 && !glue[i]);\n\n let best: { lines: number[]; F: number } | null = null;\n const n = tokens.length;\n for (let L = 1; L <= Math.min(4, n); L++) {\n let bestCut: number[] | null = null;\n let bestMax = Number.POSITIVE_INFINITY;\n const cut = (start: number, left: number, acc: number[], max: number) => {\n if (max >= bestMax) return;\n if (left === 1) {\n const m = Math.max(max, lineWidth(start, n));\n if (m < bestMax) {\n bestMax = m;\n bestCut = [...acc];\n }\n return;\n }\n for (let i = start + 1; i <= n - left + 1; i++) {\n if (!breaks[i]) continue;\n cut(i, left - 1, [...acc, i], Math.max(max, lineWidth(start, i)));\n }\n };\n cut(0, L, [], 0);\n if (!bestCut) continue;\n const F = Math.min(\n (W * 0.84) / bestMax,\n (H * 0.52) / (L * S2.lead),\n H * 0.2,\n );\n if (!best || F > best.F * 1.12) best = { lines: bestCut, F };\n }\n const cuts = best ? best.lines : [];\n const lines: Token[][] = [];\n let s = 0;\n for (const c of [...cuts, n]) {\n lines.push(tokens.slice(s, c));\n s = c;\n }\n const hero = tokens.find((t) => t.hero);\n ctx.font = `500 ${PX}px \"${geist.fontFamily}\"`;\n const ms = ctx.measureText(\"Hg\");\n ctx.font = `italic 400 ${PX}px \"${serif.fontFamily}\"`;\n const mr = ctx.measureText(\"Hg\");\n return {\n sansAsc: ms.fontBoundingBoxAscent / PX,\n sansDesc: ms.fontBoundingBoxDescent / PX,\n serifAsc: mr.fontBoundingBoxAscent / PX,\n serifDesc: mr.fontBoundingBoxDescent / PX,\n lines,\n F: best ? best.F : H * 0.1,\n heroSans: hero ? sansW(hero.text) : 0,\n heroSerif: hero ? serifW(hero.text) : 0,\n };\n}\n\nfunction useLayout(tokens: Token[], W: number, H: number, heroOn: boolean) {\n const [layout, setLayout] = useState<Layout | null>(null);\n const [handle] = useState(() => delayRender(\"prompt-restyle: measuring\"));\n const released = useRef(false);\n useEffect(() => {\n let live = true;\n Promise.all([\n geist.waitUntilDone(),\n serif.waitUntilDone(),\n mono.waitUntilDone(),\n document.fonts.load(`500 100px \"${geist.fontFamily}\"`),\n document.fonts.load(`italic 400 100px \"${serif.fontFamily}\"`),\n ])\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n setLayout(measureLayout(tokens, W, H, heroOn));\n if (!released.current) {\n released.current = true;\n continueRender(handle);\n }\n });\n return () => {\n live = false;\n };\n }, [tokens, W, H, heroOn, handle]);\n return layout;\n}\n\nconst lerp = (a: number, b: number, k: number) => a + (b - a) * k;\n\nfunction PromptRestyleBase({\n headline = \"Launch videos from parts you own.\",\n highlight = \"you own\",\n instructions,\n accent = \"#3B5BFD\",\n background,\n speed = 1,\n theme = \"dark\",\n}: PromptRestyleProps) {\n const t = useCurrentFrame() * speed;\n const { width: W, height: H } = useVideoConfig();\n const c = THEMES[theme];\n const bg = background ?? c.bg;\n\n const tokens = useMemo(\n () => tokenize(headline, highlight),\n [headline, highlight],\n );\n const heroOn = tokens.some((tk) => tk.hero);\n const layout = useLayout(tokens, W, H, heroOn);\n\n const steps = useMemo(() => {\n const d = [\n \"make it bigger\",\n \"tighten it up\",\n `set “${highlight}” in serif`,\n ];\n const list = (instructions ?? d).slice(0, heroOn ? 3 : 2);\n const out: { text: string; start: number; enter: number }[] = [];\n let at = FIRST_AT;\n for (const text of list) {\n const enter = at + Math.ceil(text.length / TYPE_RATE) + ENTER_PAUSE;\n out.push({ text, start: at, enter });\n at = enter + NEXT_GAP;\n }\n return out;\n }, [instructions, highlight, heroOn]);\n\n if (!layout) {\n return <div style={{ position: \"absolute\", inset: 0, background: bg }} />;\n }\n\n const apply = (i: number) =>\n steps[i]\n ? interpolate(t, [steps[i].enter, steps[i].enter + APPLY], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n })\n : 0;\n const k1 = apply(0);\n const k2 = apply(1);\n const k3 = apply(2);\n\n const st: Style = {\n size: lerp(lerp(S0.size, S1.size, k1), S2.size, k2),\n wght: lerp(S0.wght, S1.wght, k1),\n track: lerp(S0.track, S2.track, k2),\n lead: lerp(S0.lead, S2.lead, k2),\n };\n const F = layout.F;\n const size = st.size * F;\n const ink = interpolateColors(k1, [0, 1], [c.draft, c.ink]);\n const enterIn = interpolate(t, [0, 12], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n\n const lineH = size * st.lead;\n const blockH = lineH * layout.lines.length;\n const centerY = H * 0.46;\n\n const sansStyle: React.CSSProperties = {\n fontFamily: geist.fontFamily,\n fontWeight: 400,\n fontVariationSettings: `\"wght\" ${st.wght.toFixed(1)}`,\n letterSpacing: `${st.track}em`,\n color: ink,\n };\n\n const heroNode = (text: string) => {\n if (k3 <= 0) return <span style={sansStyle}>{text}</span>;\n const wSans = layout.heroSans * size;\n const wSerif = layout.heroSerif * size;\n const w = lerp(wSans, wSerif + size * 0.17, k3);\n const roll = lineH * 1.1;\n const sz = size * SERIF_SCALE;\n const baseSans =\n (lineH - (layout.sansAsc + layout.sansDesc) * size) / 2 +\n layout.sansAsc * size;\n const baseSerif =\n (lineH - (layout.serifAsc + layout.serifDesc) * sz) / 2 +\n layout.serifAsc * sz;\n const dy = baseSans - baseSerif;\n return (\n <span\n style={{\n position: \"relative\",\n display: \"inline-block\",\n width: w,\n height: lineH,\n verticalAlign: \"top\",\n clipPath: `inset(0 ${-size * 0.4}px)`,\n }}\n >\n <span\n style={{\n ...sansStyle,\n position: \"absolute\",\n left: (w - wSans) / 2,\n top: 0,\n lineHeight: `${lineH}px`,\n whiteSpace: \"pre\",\n opacity: 1 - k3,\n transform: `translateY(${(-k3 * roll).toFixed(2)}px)`,\n }}\n >\n {text}\n </span>\n <span\n style={{\n position: \"absolute\",\n left: (w - wSerif - size * 0.17) / 2,\n top: dy,\n width: wSerif + size * 0.3,\n paddingRight: size * 0.1,\n lineHeight: `${lineH}px`,\n whiteSpace: \"pre\",\n fontFamily: serif.fontFamily,\n fontStyle: \"italic\",\n fontWeight: 400,\n fontSize: sz,\n letterSpacing: `${SERIF_TRACK}em`,\n backgroundImage: c.hero(accent),\n backgroundSize: \"100% 100%\",\n WebkitBackgroundClip: \"text\",\n backgroundClip: \"text\",\n color: \"transparent\",\n transform: `translateY(${((1 - k3) * roll).toFixed(2)}px)`,\n }}\n >\n {text}\n </span>\n </span>\n );\n };\n\n let active: (typeof steps)[number] | undefined;\n for (const s of steps) if (t >= s.start) active = s;\n let instruction: React.ReactNode = null;\n if (active) {\n const typed = Math.min(\n active.text.length,\n Math.floor((t - active.start) * TYPE_RATE),\n );\n const gone = interpolate(t, [active.enter, active.enter + 10], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n const show = interpolate(\n t,\n [active.start, active.start + 5],\n [0, 1],\n CLAMP,\n );\n const monoSize = Math.max(16, Math.min(W, H) * 0.04);\n const blink = t < active.enter && Math.floor((t - active.start) / 8) % 2;\n instruction = (\n <div\n style={{\n position: \"absolute\",\n left: 0,\n width: W,\n top: H * 0.84 - monoSize / 2,\n display: \"flex\",\n justifyContent: \"center\",\n alignItems: \"center\",\n gap: monoSize * 0.6,\n fontFamily: mono.fontFamily,\n fontWeight: 400,\n fontSize: monoSize,\n lineHeight: 1,\n color: c.muted,\n whiteSpace: \"pre\",\n opacity: show * (1 - gone),\n transform: `translateY(${(-gone * monoSize * 1.2).toFixed(2)}px)`,\n }}\n >\n <span style={{ color: accent }}>›</span>\n <span style={{ position: \"relative\" }}>\n <span style={{ visibility: \"hidden\" }}>{active.text}</span>\n <span style={{ position: \"absolute\", left: 0, top: 0 }}>\n {active.text.slice(0, typed)}\n <span\n style={{\n display: \"inline-block\",\n width: Math.max(2, monoSize * 0.08),\n height: monoSize * 1.1,\n marginLeft: 2,\n verticalAlign: \"-0.2em\",\n background: accent,\n opacity: blink ? 0 : 1,\n }}\n />\n </span>\n </span>\n </div>\n );\n }\n\n return (\n <AbsoluteFill style={{ background: bg, overflow: \"hidden\" }}>\n <div\n style={{\n position: \"absolute\",\n left: 0,\n width: W,\n top: centerY - blockH / 2,\n opacity: enterIn,\n transform: `translateY(${((1 - enterIn) * F * 0.15).toFixed(2)}px)`,\n fontSize: size,\n lineHeight: `${lineH}px`,\n }}\n >\n {layout.lines.map((line, li) => (\n <div\n key={`line-${li}-${line.map((tk) => tk.text).join(\"_\")}`}\n style={{\n display: \"flex\",\n justifyContent: \"center\",\n height: lineH,\n whiteSpace: \"pre\",\n }}\n >\n {line.map((tk, ti) => {\n const glued = /^[.,!?;:]+$/.test(tk.text);\n const lead = ti > 0 && !glued ? \" \" : \"\";\n return (\n <span key={`${tk.text}-${ti}`} style={{ display: \"flex\" }}>\n {lead ? <span style={sansStyle}>{lead}</span> : null}\n {tk.hero && heroOn ? (\n heroNode(tk.text)\n ) : (\n <span style={sansStyle}>{tk.text}</span>\n )}\n </span>\n );\n })}\n </div>\n ))}\n </div>\n {instruction}\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 accent: {\n type: \"color\",\n default: \"#3B5BFD\",\n description: \"Accent\",\n },\n headline: {\n type: \"text-content\",\n default: \"Launch videos from parts you own.\",\n description: \"Headline\",\n },\n highlight: {\n type: \"text-content\",\n default: \"you own\",\n description: \"Highlight\",\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\",\"accent\",\"headline\",\"highlight\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n theme: \"dark\",\n accent: \"#3B5BFD\",\n headline: \"Launch videos from parts you own.\",\n highlight: \"you own\",\n speed: 1,\n};\n\ntype PromptRestyleElementProps = InteractiveBaseProps &\n InteractiveTransformProps &\n ComponentProps<typeof PromptRestyleBase>;\n\nconst PromptRestyleInner = forwardRef<\n HTMLDivElement,\n PromptRestyleElementProps & { 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 ?? \"<PromptRestyle>\"}\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 <PromptRestyleBase\n {...(componentProps as unknown as ComponentProps<typeof PromptRestyleBase>)}\n />\n </div>\n </Sequence>\n );\n});\n\nexport const PromptRestyle = Interactive.withSchema({\n Component: PromptRestyleInner,\n componentName: \"<PromptRestyle>\",\n componentIdentity: null,\n schema: elementSchema,\n supportsEffects: false,\n});\n",
"durationInFrames": 130,
"installationMode": "component-owned-sequence"
},
"durationInFrames": 130
}