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": "layout-swap",
"title": "Layout Swap",
"description": "Headlines swap by shifting layout, ending 'Animations, transitions and backgrounds, ready to install.'.",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/layout-swap/index.tsx",
"content": "\"use client\";\n\nimport { loadFont as loadInterTight } from \"@remotion/google-fonts/InterTight\";\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 inter = loadInterTight(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\nexport interface LayoutSwapProps {\n headlines?: string[];\n highlight?: string;\n align?: \"center\" | \"left\";\n hold?: number;\n move?: number;\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst BEAM = [\"#f4f7ff\", \"#8fb4ff\", \"#3d5bff\", \"#1a1660\"] as const;\n\nconst THEMES = {\n dark: {\n bg: \"#0A0A0B\",\n fg: \"#F4F3F0\",\n ground: (a: string) =>\n [\n `radial-gradient(70% 62% at 50% 120%, color-mix(in oklab, ${BEAM[2]} 52%, transparent) 0%, color-mix(in oklab, ${BEAM[3]} 42%, transparent) 46%, transparent 78%)`,\n `radial-gradient(34% 26% at 50% 104%, color-mix(in oklab, ${a} 26%, transparent) 0%, transparent 80%)`,\n ].join(\", \"),\n hl: (a: string) =>\n `linear-gradient(100deg, ${BEAM[0]} 0%, ${BEAM[1]} 34%, color-mix(in oklab, ${a} 70%, ${BEAM[1]}) 64%, ${BEAM[0]} 100%)`,\n },\n light: {\n bg: \"#F5F4F0\",\n fg: \"#111113\",\n ground: (a: string) =>\n [\n `radial-gradient(60% 55% at 50% 118%, color-mix(in oklab, ${BEAM[1]} 40%, transparent) 0%, color-mix(in oklab, ${a} 10%, transparent) 50%, transparent 78%)`,\n ].join(\", \"),\n hl: (a: string) =>\n `linear-gradient(100deg, ${BEAM[3]} 0%, ${a} 36%, #6f8dff 66%, ${BEAM[3]} 100%)`,\n },\n} as const;\n\nconst TRACK = -0.032;\nconst LINE = 1.04;\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.7, 0, 0.2, 1);\nconst EASE_IN = Easing.bezier(0.5, 0, 0.9, 0.4);\n\nconst norm = (w: string) => w.toLowerCase().replace(/[^\\p{L}\\p{N}]+/gu, \"\");\n\nfunction useWordWidths(words: string[]) {\n const key = words.join(\"\\u0001\");\n const estimate = words.map((w) => w.length * 0.52);\n const [widths, setWidths] = useState<{\n key: string;\n w: number[];\n space: number;\n } | null>(null);\n const [handle] = useState(() => delayRender(\"layout-swap: measuring\"));\n const released = useRef(false);\n\n useEffect(() => {\n let live = true;\n const font = `500 100px \"${inter.fontFamily}\"`;\n Promise.all([inter.waitUntilDone(), document.fonts.load(font)])\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n const list = key.split(\"\\u0001\");\n let w = list.map((s) => s.length * 0.52);\n let space = 0.24;\n if (ctx) {\n ctx.font = font;\n w = list.map(\n (s) =>\n ctx.measureText(s).width / 100 +\n TRACK * Math.max(0, s.length - 1),\n );\n space = ctx.measureText(\" \").width / 100 + TRACK;\n }\n setWidths({ key, w, space });\n if (!released.current) {\n released.current = true;\n continueRender(handle);\n }\n });\n return () => {\n live = false;\n };\n }, [key, handle]);\n\n return widths && widths.key === key\n ? widths\n : { key, w: estimate, space: 0.24 };\n}\n\nfunction balance(w: number[], space: number, lines: number) {\n const n = w.length;\n const rowW = (a: number, b: number) => {\n let s = 0;\n for (let i = a; i < b; i++) s += w[i] + (i > a ? space : 0);\n return s;\n };\n const best: number[][] = [];\n const cut: number[][] = [];\n for (let k = 0; k <= lines; k++) {\n best.push(new Array(n + 1).fill(Number.POSITIVE_INFINITY));\n cut.push(new Array(n + 1).fill(0));\n }\n best[0][0] = 0;\n for (let k = 1; k <= lines; k++) {\n for (let i = 1; i <= n; i++) {\n for (let j = k - 1; j < i; j++) {\n const v = Math.max(best[k - 1][j], rowW(j, i));\n if (v < best[k][i]) {\n best[k][i] = v;\n cut[k][i] = j;\n }\n }\n }\n }\n const rows: [number, number][] = [];\n let i = n;\n for (let k = lines; k >= 1; k--) {\n const j = cut[k][i];\n rows.unshift([j, i]);\n i = j;\n }\n return { rows, widest: best[lines][n], rowW };\n}\n\ninterface Placed {\n text: string;\n key: string;\n x: number;\n y: number;\n fs: number;\n w: number;\n order: number;\n}\n\nfunction layoutHeadline(\n words: string[],\n widths: number[],\n space: number,\n box: { w: number; h: number; cx: number; cy: number; left: number },\n maxFs: number,\n align: \"center\" | \"left\",\n) {\n const n = Math.max(1, words.length);\n let chosen = balance(widths, space, 1);\n let fs = Math.min(maxFs, box.w / Math.max(chosen.widest, 0.01));\n for (let lines = 2; lines <= Math.min(4, n); lines++) {\n const next = balance(widths, space, lines);\n const nextFs = Math.min(\n maxFs,\n box.w / Math.max(next.widest, 0.01),\n box.h / (lines * LINE),\n );\n if (nextFs > fs * 1.18) {\n chosen = next;\n fs = nextFs;\n }\n }\n const rows = chosen.rows.filter(([a, b]) => b > a);\n const blockH = rows.length * LINE * fs;\n const top = box.cy - blockH / 2;\n const seen = new Map<string, number>();\n const placed: Placed[] = [];\n rows.forEach(([a, b], r) => {\n const rw = chosen.rowW(a, b) * fs;\n let x = align === \"center\" ? box.cx - rw / 2 : box.left;\n for (let i = a; i < b; i++) {\n const k = norm(words[i]) || words[i];\n const nth = seen.get(k) ?? 0;\n seen.set(k, nth + 1);\n placed.push({\n text: words[i],\n key: `${k}#${nth}`,\n x,\n y: top + r * LINE * fs,\n fs,\n w: widths[i] * fs,\n order: i,\n });\n x += (widths[i] + space) * fs;\n }\n });\n return placed;\n}\n\nexport function LayoutSwap({\n headlines = [\n \"Animations.\",\n \"Animations, transitions, backgrounds.\",\n \"Animations, transitions and backgrounds, ready to install.\",\n ],\n highlight = \"ready to install.\",\n align = \"center\",\n hold = 28,\n move = 28,\n accent = \"#3B5BFD\",\n background,\n speed = 1,\n theme = \"dark\",\n}: LayoutSwapProps) {\n const frame = useCurrentFrame() * speed;\n const { width, height, durationInFrames } = useVideoConfig();\n const t = THEMES[theme];\n const steps = headlines.length > 0 ? headlines : [\"remocn\"];\n\n const split = steps.map((h) => h.split(/\\s+/).filter(Boolean));\n const all = split.flat();\n const m = useWordWidths(all);\n let cursor = 0;\n const widthsPer = split.map((ws) => {\n const w = m.w.slice(cursor, cursor + ws.length);\n cursor += ws.length;\n return w;\n });\n\n const portrait = height > width * 1.1;\n const s = Math.min(width / 1280, height / 720);\n const padX = width * (portrait ? 0.09 : 0.1);\n const box = {\n w: width - padX * 2,\n h: height * (portrait ? 0.5 : 0.56),\n cx: width / 2,\n cy: height * 0.5,\n left: padX,\n };\n const maxFs = (portrait ? 170 : 150) * (portrait ? width / 1080 : s);\n\n const layouts = split.map((ws, k) =>\n layoutHeadline(ws, widthsPer[k], m.space, box, maxFs, align),\n );\n\n const ENTER = 20;\n const stepStart = (k: number) => ENTER + k * (hold + move);\n let k = 0;\n while (k < steps.length - 1 && frame >= stepStart(k) + hold) k++;\n const moving = k > 0 && frame < stepStart(k - 1) + hold + move;\n const from = moving ? k - 1 : k;\n const to = k;\n const moveT = moving\n ? (frame - (stepStart(from) + hold)) / Math.max(1, move)\n : 1;\n\n const hlKeys = new Set(\n (highlight ?? \"\").split(/\\s+/).map(norm).filter(Boolean),\n );\n const last = steps.length - 1;\n const drift = interpolate(\n frame,\n [0, durationInFrames * speed],\n [0, 1],\n CLAMP,\n );\n\n type Draw = {\n id: string;\n text: string;\n x: number;\n y: number;\n fs: number;\n opacity: number;\n blur: number;\n hl: number;\n };\n const draws: Draw[] = [];\n\n const hlOf = (step: number, p: Placed) =>\n step === last && hlKeys.has(norm(p.text)) ? 1 : 0;\n\n if (!moving) {\n const L = layouts[to];\n L.forEach((p, i) => {\n const e =\n to === 0\n ? interpolate(frame, [i * 2.5, i * 2.5 + 16], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n })\n : 1;\n draws.push({\n id: `${to}:${p.key}`,\n text: p.text,\n x: p.x,\n y: p.y + (1 - e) * 0.32 * p.fs,\n fs: p.fs,\n opacity: e,\n blur: (1 - e) * 0.08 * p.fs,\n hl: hlOf(to, p),\n });\n });\n } else {\n const A = layouts[from];\n const B = layouts[to];\n const bByKey = new Map(B.map((p) => [p.key, p]));\n const aKeys = new Set(A.map((p) => p.key));\n const pairs = A.filter((p) => bByKey.has(p.key));\n const lead = (i: number, n: number) => (n <= 1 ? 0 : (i / (n - 1)) * 0.14);\n const sharedSorted = [...pairs].sort(\n (a, b) =>\n (bByKey.get(a.key)?.order ?? 0) - (bByKey.get(b.key)?.order ?? 0),\n );\n sharedSorted.forEach((a, i) => {\n const b = bByKey.get(a.key) as Placed;\n const d = lead(i, sharedSorted.length);\n const u = EASE_IO(Math.min(1, Math.max(0, (moveT - 0.16 - d) / 0.5)));\n const dx = b.x - a.x;\n const dy = b.y - a.y;\n const len = Math.hypot(dx, dy);\n const mx = (a.x + a.w / 2 + b.x + b.w / 2) / 2;\n const my = (a.y + b.y) / 2 + (LINE * (a.fs + b.fs)) / 4;\n let px = len > 0 ? -dy / len : 0;\n let py = len > 0 ? dx / len : 0;\n if (px * (mx - box.cx) + py * (my - box.cy) < 0) {\n px = -px;\n py = -py;\n }\n const fsMid = (a.fs + b.fs) / 2;\n const bow = Math.sin(Math.PI * u) * Math.min(0.55 * fsMid, len * 0.16);\n const x = a.x + dx * u + px * bow;\n const y = a.y + dy * u + py * bow;\n const fs = a.fs + (b.fs - a.fs) * u;\n const travel = len / fsMid;\n const depth = Math.sin(Math.PI * u) * Math.min(1, travel / 3.2);\n const dim = 1 - depth * 0.62;\n const soft = depth * 0.035 * fs;\n const hlA = hlOf(from, a);\n const hlB = hlOf(to, b);\n if (a.text === b.text) {\n draws.push({\n id: `s:${a.key}`,\n text: a.text,\n x,\n y,\n fs,\n opacity: dim,\n blur: soft,\n hl: hlA + (hlB - hlA) * u,\n });\n } else {\n const c = interpolate(u, [0.3, 0.7], [0, 1], CLAMP);\n draws.push({\n id: `sa:${a.key}`,\n text: a.text,\n x,\n y,\n fs,\n opacity: (1 - c) * dim,\n blur: soft,\n hl: hlA,\n });\n draws.push({\n id: `sb:${a.key}`,\n text: b.text,\n x,\n y,\n fs,\n opacity: c * dim,\n blur: soft,\n hl: hlB,\n });\n }\n });\n A.filter((p) => !bByKey.has(p.key)).forEach((a, i) => {\n const o = EASE_IN(Math.min(1, Math.max(0, (moveT - i * 0.03) / 0.34)));\n draws.push({\n id: `o:${a.key}`,\n text: a.text,\n x: a.x,\n y: a.y + o * 0.22 * a.fs,\n fs: a.fs * (1 - o * 0.06),\n opacity: 1 - o,\n blur: o * 0.07 * a.fs,\n hl: hlOf(from, a),\n });\n });\n const fresh = B.filter((p) => !aKeys.has(p.key));\n const gap = Math.min(0.05, 0.2 / Math.max(1, fresh.length));\n fresh.forEach((b, i) => {\n const e = EASE_OUT(\n Math.min(1, Math.max(0, (moveT - 0.56 - i * gap) / 0.38)),\n );\n draws.push({\n id: `i:${b.key}`,\n text: b.text,\n x: b.x,\n y: b.y + (1 - e) * 0.3 * b.fs,\n fs: b.fs,\n opacity: e,\n blur: (1 - e) * 0.08 * b.fs,\n hl: hlOf(to, b),\n });\n });\n }\n\n const transparent = background === \"transparent\";\n const hlImage = t.hl(accent);\n\n return (\n <AbsoluteFill\n style={{\n background: transparent ? \"transparent\" : (background ?? t.bg),\n overflow: \"hidden\",\n }}\n >\n {!transparent && background === undefined ? (\n <div\n style={{\n position: \"absolute\",\n inset: \"-6%\",\n background: t.ground(accent),\n transform: `translateY(${(-drift * 3).toFixed(3)}%)`,\n }}\n />\n ) : null}\n {draws.map((d) => {\n const base: React.CSSProperties = {\n position: \"absolute\",\n left: d.x,\n top: d.y,\n fontFamily: inter.fontFamily,\n fontWeight: 500,\n fontSize: d.fs,\n lineHeight: LINE,\n letterSpacing: `${TRACK}em`,\n whiteSpace: \"pre\",\n opacity: d.opacity,\n filter: d.blur > 0.05 ? `blur(${d.blur.toFixed(2)}px)` : undefined,\n };\n return (\n <div key={d.id} style={base}>\n <span style={{ color: t.fg, opacity: 1 - d.hl }}>{d.text}</span>\n {d.hl > 0 ? (\n <span\n style={{\n position: \"absolute\",\n left: 0,\n top: 0,\n opacity: d.hl,\n backgroundImage: hlImage,\n backgroundSize: \"180% 100%\",\n backgroundPosition: `${(drift * 60).toFixed(2)}% 0%`,\n WebkitBackgroundClip: \"text\",\n backgroundClip: \"text\",\n color: \"transparent\",\n paddingRight: \"0.06em\",\n }}\n >\n {d.text}\n </span>\n ) : null}\n </div>\n );\n })}\n </AbsoluteFill>\n );\n}\n",
"type": "registry:component",
"target": "components/remocn/layout-swap.tsx"
}
],
"type": "registry:component"
}