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": "run-on",
"title": "Run on",
"description": "A sentence, \"Or change one\", trails off with a joiner and an orange dot.",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/run-on/index.tsx",
"content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/DMSans\";\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 dmSans = loadFont(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\nexport interface RunOnProps {\n text?: string;\n promise?: string;\n highlight?: string;\n joiners?: string[];\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n dark: { bg: \"#2E3440\", ink: \"#FFFFFF\", dim: \"#FFFFFF8C\", accent: \"#FFB020\" },\n light: {\n bg: \"#F1F2F4\",\n ink: \"#2E3440\",\n dim: \"#2E34408C\",\n accent: \"#B86E00\",\n },\n} as const;\n\nconst WGHT = 500;\nconst TRACK = -0.035;\nconst PROBE = 200;\nconst BASE = 200;\nconst LEAD = 1.08;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst START = 6;\nconst FIRST_GAP = 9;\nconst LAST_GAP = 2.6;\nconst REVEAL = 6;\nconst STOP_GAP = 7;\nconst SLAM = 5;\nconst HOLD = 16;\nconst DRAIN = 16;\nconst PROMISE_LAG = 5;\nconst SCROLL = 10;\nconst PROMISE_STAGGER = 4;\nconst PROMISE_IN = 16;\n\ninterface Word {\n text: string;\n start: number;\n x: number;\n w: number;\n}\n\ninterface Metrics {\n words: Word[];\n promise: Word[];\n promiseW: number;\n dotW: number;\n}\n\nfunction layoutWords(s: string, em: (x: string) => number) {\n const out: Word[] = [];\n const re = /\\S+/g;\n for (let m = re.exec(s); m; m = re.exec(s)) {\n out.push({\n text: m[0],\n start: m.index,\n x: em(s.slice(0, m.index)),\n w: em(m[0]),\n });\n }\n return out;\n}\n\nfunction measure(text: string, promise: string): Metrics {\n const canvas = document.createElement(\"canvas\");\n const ctx = canvas.getContext(\"2d\");\n if (!ctx) throw new Error(\"run-on: no 2d context\");\n ctx.font = `${WGHT} ${PROBE}px \"${dmSans.fontFamily}\"`;\n const em = (x: string) =>\n ctx.measureText(x).width / PROBE + TRACK * Array.from(x).length;\n const body = promise.replace(/\\.$/, \"\");\n return {\n words: layoutWords(text, em),\n promise: layoutWords(body, em),\n promiseW: em(body),\n dotW: em(\".\"),\n };\n}\n\nfunction useMetrics(text: string, promise: string) {\n const key = `${text}\\u0001${promise}`;\n const [state, setState] = useState<{ key: string; m: Metrics } | null>(null);\n const [handle] = useState(() => delayRender(\"run-on: measuring\"));\n const released = useRef(false);\n const src = useRef({ text, promise });\n src.current = { text, promise };\n useEffect(() => {\n let live = true;\n Promise.all([\n dmSans.waitUntilDone(),\n document.fonts.load(`${WGHT} 100px \"${dmSans.fontFamily}\"`),\n ])\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n setState({ key, m: measure(src.current.text, src.current.promise) });\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\nexport function RunOn({\n text = \"Open last year’s project and relink the fonts and nudge every keyframe and render and spot a typo and render it all again\",\n promise = \"Or change one line.\",\n highlight = \"one line\",\n joiners = [\"and\", \"then\"],\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: RunOnProps) {\n const frame = useCurrentFrame();\n const { width: W, height: H, durationInFrames } = useVideoConfig();\n const k = Math.min(1, durationInFrames / BASE);\n const t = (frame * speed) / k;\n const c = THEMES[theme];\n const hi = accent ?? c.accent;\n const stage =\n background === \"transparent\" ? \"transparent\" : (background ?? c.bg);\n const body = text.trim().replace(/[.!?…]+$/, \"\");\n const said = promise.trim();\n const m = useMetrics(body, said);\n\n if (!m) {\n return (\n <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n );\n }\n\n const portrait = H > W * 1.1;\n const F = portrait ? W * 0.105 : H * 0.135;\n const pitch = F * LEAD;\n const marginX = W * (portrait ? 0.08 : 0.07);\n const measureW = (W - marginX * 2) / F;\n const top0 = H * (portrait ? 0.2 : 0.14);\n const floor = H * (portrait ? 0.62 : 0.86);\n const n = m.words.length;\n\n const placed: { line: number; x: number }[] = [];\n let line = 0;\n let lineStart = 0;\n m.words.forEach((w, i) => {\n if (i > 0 && w.x + w.w - lineStart > measureW) {\n line += 1;\n lineStart = w.x;\n }\n placed.push({ line, x: w.x - lineStart });\n });\n\n const arrive: number[] = [];\n let acc = START;\n for (let i = 0; i < n; i++) {\n arrive.push(acc);\n const p = n > 1 ? i / (n - 1) : 1;\n acc += FIRST_GAP + (LAST_GAP - FIRST_GAP) * p ** 0.6;\n }\n const stopAt = arrive[n - 1] + STOP_GAP;\n const drainAt = stopAt + SLAM + HOLD;\n const promiseAt = drainAt + DRAIN - PROMISE_LAG;\n\n const need = (ln: number) => Math.max(0, top0 + (ln + 1) * pitch - floor);\n let scroll = 0;\n for (let i = 0; i < n; i++) {\n if (t < arrive[i]) break;\n const from = i === 0 ? 0 : need(placed[i - 1].line);\n const to = need(placed[i].line);\n if (to !== from) {\n scroll =\n from +\n (to - from) *\n interpolate(t, [arrive[i], arrive[i] + SCROLL], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n } else {\n scroll = to;\n }\n }\n const drain = interpolate(t, [drainAt, drainAt + DRAIN], [0, 1], {\n ...CLAMP,\n easing: Easing.bezier(0.55, 0, 1, 0.45),\n });\n const wallY = -scroll - drain * H * 1.05;\n\n const Fp = Math.min(\n portrait ? W * 0.16 : H * 0.22,\n (W - marginX * 2) / (m.promiseW + m.dotW),\n );\n const promiseBase = H * (portrait ? 0.62 : 0.66);\n const joinSet = new Set(joiners.map((j) => j.toLowerCase()));\n\n const hiStart = highlight ? said.lastIndexOf(highlight) : -1;\n const hiEnd = hiStart >= 0 ? hiStart + highlight.length : -1;\n\n const glyph = (\n key: string,\n text: string,\n x: number,\n baseY: number,\n size: number,\n dy: number,\n opacity: number,\n color: string,\n ) => (\n <div\n key={key}\n style={{\n position: \"absolute\",\n left: 0,\n top: 0,\n height: size * 1.2,\n lineHeight: `${size * 1.2}px`,\n whiteSpace: \"pre\",\n fontFamily: dmSans.fontFamily,\n fontWeight: WGHT,\n fontSize: size,\n letterSpacing: `${TRACK}em`,\n fontKerning: \"normal\",\n color,\n opacity,\n transform: `translate(${x.toFixed(2)}px, ${(baseY - size * 0.93 + dy).toFixed(2)}px)`,\n }}\n >\n {text}\n </div>\n );\n\n const baseOf = (ln: number) => top0 + ln * pitch + F * 0.93;\n const nodes: React.ReactNode[] = [];\n if (drain < 1) {\n m.words.forEach((w, i) => {\n const r = interpolate(t, [arrive[i], arrive[i] + REVEAL], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n if (r <= 0) return;\n const y = baseOf(placed[i].line) + wallY;\n if (y < -F || y > H + F * 2) return;\n const join = joinSet.has(w.text.toLowerCase());\n nodes.push(\n glyph(\n `w${i}`,\n w.text,\n marginX + placed[i].x * F,\n y,\n F,\n F * 0.3 * (1 - r),\n r * (1 - drain * 0.5),\n join ? c.dim : c.ink,\n ),\n );\n });\n }\n\n const slam = interpolate(t, [stopAt, stopAt + SLAM], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n const dotTravel = interpolate(t, [drainAt, drainAt + DRAIN + 6], [0, 1], {\n ...CLAMP,\n easing: Easing.bezier(0.65, 0, 0.35, 1),\n });\n if (slam > 0) {\n const last = n - 1;\n const stopX = marginX + (placed[last].x + m.words[last].w) * F;\n const stopY = baseOf(placed[last].line) - scroll;\n const restX = marginX + m.promiseW * Fp;\n const size = F + (Fp - F) * dotTravel;\n nodes.push(\n glyph(\n \"stop\",\n \".\",\n stopX + (restX - stopX) * dotTravel,\n stopY + (promiseBase - stopY) * dotTravel,\n size,\n -F * 0.7 * (1 - slam),\n Math.min(1, slam * 1.6),\n hi,\n ),\n );\n }\n\n m.promise.forEach((w, i) => {\n const at = promiseAt + i * PROMISE_STAGGER;\n const r = interpolate(t, [at, at + PROMISE_IN], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n if (r <= 0) return;\n const lit = interpolate(\n t,\n [promiseAt + PROMISE_IN + 4, promiseAt + PROMISE_IN + 14],\n [0, 1],\n { ...CLAMP, easing: EASE_OUT },\n );\n const inHi =\n hiStart >= 0 && w.start < hiEnd && w.start + w.text.length > hiStart;\n const color = inHi\n ? `color-mix(in oklab, ${hi} ${(lit * 100).toFixed(1)}%, ${c.ink})`\n : c.ink;\n nodes.push(\n glyph(\n `p${i}`,\n w.text,\n marginX + w.x * Fp,\n promiseBase,\n Fp,\n Fp * 0.4 * (1 - r),\n r,\n color,\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/run-on.tsx"
}
],
"type": "registry:component"
}