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": "Keep Words",
"initialProps": null,
"slug": "keep-words",
"sourceCode": "import { loadFont } from \"@remotion/google-fonts/Gloock\";\nimport React, { forwardRef, useEffect, useImperativeHandle, 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 gloock = loadFont(\"normal\", { weights: [\"400\"], subsets: [\"latin\"] });\n\ninterface KeepBelief {\n text: string;\n keep: string;\n}\n\ninterface KeepWordsProps {\n beliefs?: KeepBelief[];\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst DEFAULT_BELIEFS: KeepBelief[] = [\n {\n text: \"A launch video should be written, the same way the product was.\",\n keep: \"written\",\n },\n {\n text: \"Every frame you install should stay yours, down to the last easing curve.\",\n keep: \"yours\",\n },\n {\n text: \"And the film should land on time, in the same release as the code.\",\n keep: \"on time\",\n },\n];\n\nconst THEMES = {\n dark: { bg: \"#0A1230\", ink: \"#FFFFFF\", accent: \"#FF4436\" },\n light: { bg: \"#F7F3EC\", ink: \"#14161F\", accent: \"#D92B1E\" },\n} as const;\n\nconst TRACK = -0.02;\nconst LH = 1.08;\nconst START = 6;\nconst STACK_MOVE = 24;\nconst FINAL_HOLD = 34;\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_IN = Easing.bezier(0.55, 0, 0.75, 0.2);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nlet measureCtx: CanvasRenderingContext2D | null = null;\n\nfunction em(s: string): number {\n if (!measureCtx) {\n measureCtx = document.createElement(\"canvas\").getContext(\"2d\");\n }\n if (!measureCtx) return s.length * 0.5;\n measureCtx.font = `400 100px \"${gloock.fontFamily}\"`;\n return measureCtx.measureText(s).width / 100 + TRACK * Array.from(s).length;\n}\n\nfunction useFontReady() {\n const [ready, setReady] = useState(false);\n const [handle] = useState(() => delayRender(\"keep-words: font\"));\n useEffect(() => {\n let live = true;\n gloock\n .waitUntilDone()\n .then(() => document.fonts.load(`400 100px \"${gloock.fontFamily}\"`))\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n setReady(true);\n continueRender(handle);\n });\n return () => {\n live = false;\n };\n }, [handle]);\n return ready;\n}\n\ninterface Seg {\n text: string;\n dx: number;\n key: number;\n}\n\ninterface Word {\n segs: Seg[];\n w: number;\n}\n\ninterface Laid {\n words: { x: number; line: number }[];\n lines: number;\n}\n\nfunction wrapAt(words: Word[], maxEm: number): Laid {\n const space = em(\" \");\n const out: { x: number; line: number }[] = [];\n let line = 0;\n let x = 0;\n for (const w of words) {\n if (x > 0 && x + w.w > maxEm) {\n line += 1;\n x = 0;\n }\n out.push({ x, line });\n x += w.w + space;\n }\n return { words: out, lines: line + 1 };\n}\n\nfunction balanced(words: Word[], maxEm: number): Laid {\n const base = wrapAt(words, maxEm);\n let lo = Math.max(...words.map((w) => w.w), 0.1);\n let hi = maxEm;\n for (let i = 0; i < 14; i++) {\n const mid = (lo + hi) / 2;\n if (wrapAt(words, mid).lines <= base.lines) hi = mid;\n else lo = mid;\n }\n return wrapAt(words, hi);\n}\n\nfunction fitSize(words: Word[], boxW: number, boxH: number, maxF: number) {\n let lo = 8;\n let hi = maxF;\n for (let i = 0; i < 18; i++) {\n const mid = (lo + hi) / 2;\n const lay = wrapAt(words, boxW / mid);\n const widest = Math.max(...words.map((w) => w.w)) * mid;\n if (lay.lines * LH * mid <= boxH && widest <= boxW) lo = mid;\n else hi = mid;\n }\n return lo;\n}\n\nfunction splitBelief(b: KeepBelief): Word[] {\n const tokens = b.text.split(/\\s+/).filter(Boolean);\n const keepTokens = b.keep.toLowerCase().split(/\\s+/).filter(Boolean);\n const core = (s: string) =>\n s.replace(/^[^\\p{L}\\p{N}]+|[^\\p{L}\\p{N}]+$/gu, \"\");\n let at = -1;\n for (let i = 0; i + keepTokens.length <= tokens.length && at < 0; i++) {\n let ok = true;\n for (let j = 0; j < keepTokens.length; j++) {\n if (core(tokens[i + j]).toLowerCase() !== keepTokens[j]) ok = false;\n }\n if (ok) at = i;\n }\n return tokens.map((tk, i) => {\n const k = at >= 0 && i >= at && i < at + keepTokens.length ? i - at : -1;\n if (k < 0) return { segs: [{ text: tk, dx: 0, key: -1 }], w: em(tk) };\n const c = core(tk);\n const start = tk.indexOf(c);\n const lead = tk.slice(0, start);\n const tail = tk.slice(start + c.length);\n const segs: Seg[] = [];\n if (lead) segs.push({ text: lead, dx: 0, key: -1 });\n segs.push({ text: c, dx: em(lead), key: k });\n if (tail) segs.push({ text: tail, dx: em(lead + c), key: -1 });\n return { segs, w: em(tk) };\n });\n}\n\nfunction mottoOf(keep: string) {\n const t = keep.trim();\n return `${t.charAt(0).toUpperCase()}${t.slice(1)}`;\n}\n\nfunction KeepWordsBase({\n beliefs = DEFAULT_BELIEFS,\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: KeepWordsProps) {\n const frame = useCurrentFrame();\n const t = frame * speed;\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 = useFontReady();\n\n if (!ready || beliefs.length === 0) {\n return (\n <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n );\n }\n\n const portrait = H > W * 1.1;\n const n = beliefs.length;\n const D = durationInFrames * speed;\n const seg = Math.max(\n 48,\n (D - START - STACK_MOVE - FINAL_HOLD - 30) / Math.max(1, n),\n );\n const lastTravel =\n START + (n - 1) * seg + seg * 0.66 + Math.min(24, seg * 0.3);\n const stackAt = lastTravel + 14;\n\n const mx = W * (portrait ? 0.08 : 0.07);\n const boxTop = H * (portrait ? 0.1 : 0.1);\n const boxW = W * (portrait ? 0.84 : 0.8);\n const boxH = H * (portrait ? 0.46 : 0.56);\n\n const mottoItems = beliefs.map((b) => mottoOf(b.keep));\n const space = em(\" \");\n const mottoWidthEm =\n mottoItems.reduce((s, m) => s + em(`${m}.`), 0) + space * (n - 1);\n const Fm = Math.min(\n H * (portrait ? 0.05 : 0.085),\n (W - mx * 2) / mottoWidthEm,\n );\n const mottoTop = H * (portrait ? 0.66 : 0.76);\n\n const stackWidest = Math.max(...mottoItems.map((m) => em(`${m}.`)));\n const Ff = Math.min(\n (H * (portrait ? 0.62 : 0.8)) / (n * LH),\n (W - mx * 2) / stackWidest,\n H * 0.3,\n );\n const stackTop = portrait ? H * 0.5 - (n * LH * Ff) / 2 : boxTop;\n\n const mottoSlots: { x: number; y: number }[][] = [];\n const stackSlots: { x: number; y: number }[][] = [];\n let mxEm = 0;\n mottoItems.forEach((m, i) => {\n const parts = m.split(/\\s+/);\n const rowSlots: { x: number; y: number }[] = [];\n const colSlots: { x: number; y: number }[] = [];\n let local = 0;\n for (const p of parts) {\n rowSlots.push({ x: mx + (mxEm + local) * Fm, y: mottoTop });\n colSlots.push({ x: mx + local * Ff, y: stackTop + i * LH * Ff });\n local += em(p) + space;\n }\n rowSlots.push({ x: mx + (mxEm + em(m)) * Fm, y: mottoTop });\n colSlots.push({ x: mx + em(m) * Ff, y: stackTop + i * LH * Ff });\n mottoSlots.push(rowSlots);\n stackSlots.push(colSlots);\n mxEm += em(`${m}.`) + space;\n });\n\n const stackOf = (i: number) =>\n interpolate(t, [stackAt + i * 4, stackAt + i * 4 + STACK_MOVE], [0, 1], {\n ...CLAMP,\n easing: EASE_IO,\n });\n\n const nodes: React.ReactNode[] = [];\n\n const glyph = (\n key: string,\n text: string,\n x: number,\n y: number,\n size: number,\n renderSize: number,\n color: string,\n opacity: number,\n ) => (\n <div\n key={key}\n style={{\n position: \"absolute\",\n left: 0,\n top: 0,\n fontFamily: gloock.fontFamily,\n fontWeight: 400,\n fontSize: renderSize,\n lineHeight: `${LH * renderSize}px`,\n letterSpacing: `${TRACK}em`,\n whiteSpace: \"pre\",\n color,\n opacity,\n transformOrigin: \"0 0\",\n transform: `translate(${x.toFixed(2)}px, ${y.toFixed(2)}px) scale(${(size / renderSize).toFixed(4)})`,\n }}\n >\n {text}\n </div>\n );\n\n beliefs.forEach((b, bi) => {\n const stackP = stackOf(bi);\n const s0 = START + bi * seg;\n const u = t - s0;\n const words = splitBelief(b);\n const F = fitSize(words, boxW, boxH, H * (portrait ? 0.1 : 0.17));\n const lay = balanced(words, boxW / F);\n const markAt = seg * 0.42;\n const fallAt = seg * 0.6;\n const travelAt = seg * 0.66;\n const travelLen = Math.min(24, seg * 0.3);\n const mark = interpolate(u, [markAt, markAt + 8], [0, 1], CLAMP);\n const keyColor = interpolateColors(mark, [0, 1], [c.ink, hi]);\n\n if (u > -1) {\n const stagger = Math.min(2, 22 / Math.max(1, words.length));\n let order = 0;\n words.forEach((w, wi) => {\n const pos = lay.words[wi];\n const wx = mx + pos.x * F;\n const wy = boxTop + pos.line * LH * F;\n const enter = interpolate(\n u,\n [order * stagger, order * stagger + 16],\n [0, 1],\n {\n ...CLAMP,\n easing: EASE_OUT,\n },\n );\n order += 1;\n w.segs.forEach((sg, si) => {\n const id = `b${bi}w${wi}s${si}`;\n if (sg.key < 0) {\n const fall = interpolate(\n u,\n [fallAt + (wi % 4), fallAt + (wi % 4) + 12],\n [0, 1],\n { ...CLAMP, easing: EASE_IN },\n );\n const op = enter * (1 - fall);\n if (op <= 0.001) return;\n nodes.push(\n glyph(\n id,\n sg.text,\n wx + sg.dx * F,\n wy + (1 - enter) * F * 0.32 + fall * F * 0.22,\n F,\n F,\n c.ink,\n op,\n ),\n );\n return;\n }\n const travel = interpolate(\n u,\n [travelAt + sg.key * 2, travelAt + sg.key * 2 + travelLen],\n [0, 1],\n { ...CLAMP, easing: EASE_IO },\n );\n const row = mottoSlots[bi][sg.key];\n const col = stackSlots[bi][sg.key];\n const fx = wx + sg.dx * F;\n const fy = wy + (1 - enter) * F * 0.32;\n const rx = row.x + (col.x - row.x) * stackP;\n const ry = row.y + (col.y - row.y) * stackP;\n const rs = Fm + (Ff - Fm) * stackP;\n const x = fx + (rx - fx) * travel;\n const y = fy + (ry - fy) * travel;\n const size = F + (rs - F) * travel;\n const render = Math.max(F, Ff);\n const landed = interpolate(travel, [0.85, 1], [0, 1], CLAMP);\n const color = interpolateColors(landed, [0, 1], [keyColor, c.ink]);\n const finalText =\n sg.key === 0 ? mottoItems[bi].split(/\\s+/)[0] : sg.text;\n const swap = finalText !== sg.text ? landed : 0;\n if (swap < 1) {\n nodes.push(\n glyph(id, sg.text, x, y, size, render, color, enter * (1 - swap)),\n );\n }\n if (swap > 0) {\n nodes.push(\n glyph(`${id}f`, finalText, x, y, size, render, color, swap),\n );\n }\n });\n });\n }\n\n const dotAt = travelAt + (mottoSlots[bi].length - 2) * 2 + travelLen;\n const dot = interpolate(u, [dotAt - 4, dotAt + 6], [0, 1], CLAMP);\n if (dot > 0) {\n const row = mottoSlots[bi][mottoSlots[bi].length - 1];\n const col = stackSlots[bi][stackSlots[bi].length - 1];\n const rs = Fm + (Ff - Fm) * stackP;\n nodes.push(\n glyph(\n `b${bi}dot`,\n \".\",\n row.x + (col.x - row.x) * stackP,\n row.y + (col.y - row.y) * stackP,\n rs,\n Ff,\n hi,\n dot,\n ),\n );\n }\n });\n\n return (\n <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n {nodes}\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 KeepWordsElementProps = InteractiveBaseProps &\n InteractiveTransformProps &\n ComponentProps<typeof KeepWordsBase>;\n\nconst KeepWordsInner = forwardRef<\n HTMLDivElement,\n KeepWordsElementProps & { 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 ?? \"<KeepWords>\"}\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 <KeepWordsBase\n {...(componentProps as unknown as ComponentProps<typeof KeepWordsBase>)}\n />\n </div>\n </Sequence>\n );\n});\n\nexport const KeepWords = Interactive.withSchema({\n Component: KeepWordsInner,\n componentName: \"<KeepWords>\",\n componentIdentity: null,\n schema: elementSchema,\n supportsEffects: false,\n});\n",
"durationInFrames": 285,
"installationMode": "component-owned-sequence"
},
"durationInFrames": 285
}