{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "out-of-context",
  "title": "Out of Context",
  "description": "\"Stop making launch videos by hand.\" with the last words in pink on a blue ground.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/out-of-context/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Outfit\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst outfit = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface OutOfContextProps {\n  claim?: string;\n  rest?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: { bg: \"#2445E5\", ink: \"#FFFFFF\", accent: \"#FF8FB1\" },\n  light: { bg: \"#FFFFFF\", ink: \"#2445E5\", accent: \"#E0457B\" },\n} as const;\n\nconst WGHT = 500;\nconst TRACK = -0.035;\nconst PITCH = 1.02;\nconst MEASURE_PX = 100;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_CAM = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_DROP = Easing.bezier(0.55, 0, 0.8, 0.4);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nconst IN_LEN = 14;\nconst IN_STAGGER = 4;\nconst DROP_AT = 40;\nconst DROP_LEN = 9;\nconst PULL_AT = 46;\nconst PULL_LEN = 40;\nconst REST_AT = 0.42;\nconst REST_LEN = 14;\nconst REST_STAGGER = 3;\nconst MOTION_END = 100;\nconst MIN_HOLD = 24;\n\nlet measureCtx: CanvasRenderingContext2D | null = null;\n\nfunction ctx2d() {\n  if (!measureCtx) {\n    measureCtx = document.createElement(\"canvas\").getContext(\"2d\");\n  }\n  if (!measureCtx) throw new Error(\"out-of-context: no 2d context\");\n  measureCtx.font = `${WGHT} ${MEASURE_PX}px \"${outfit.fontFamily}\"`;\n  return measureCtx;\n}\n\nfunction em(s: string) {\n  return (\n    ctx2d().measureText(s).width / MEASURE_PX + TRACK * Array.from(s).length\n  );\n}\n\nfunction useFontsReady() {\n  const [ready, setReady] = useState(false);\n  const [handle] = useState(() => delayRender(\"out-of-context: fonts\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      outfit.waitUntilDone(),\n      document.fonts.load(`${WGHT} 100px \"${outfit.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (live) setReady(true);\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [handle]);\n  return ready;\n}\n\ninterface PlacedWord {\n  text: string;\n  line: number;\n  x: number;\n  rest: boolean;\n  order: number;\n}\n\ninterface Layout {\n  words: PlacedWord[];\n  lineCount: number;\n  claimLines: number;\n  claimW: number;\n  blockW: number;\n  dotX: number;\n  baseline: number;\n  cap: number;\n  zoomF: number;\n  finalF: number;\n}\n\nfunction split(s: string) {\n  return s.split(/\\s+/).filter(Boolean);\n}\n\nfunction balanced(words: string[], n: number): string[][] {\n  if (n <= 1) return [words];\n  let best: string[][] = [words];\n  let bestW = Number.POSITIVE_INFINITY;\n  const walk = (start: number, left: number, acc: string[][]) => {\n    if (left === 1) {\n      const rows = [...acc, words.slice(start)];\n      const w = Math.max(...rows.map((r) => em(r.join(\" \"))));\n      if (w < bestW) {\n        bestW = w;\n        best = rows;\n      }\n      return;\n    }\n    for (let i = start + 1; i <= words.length - left + 1; i++) {\n      walk(i, left - 1, [...acc, words.slice(start, i)]);\n    }\n  };\n  walk(0, n, []);\n  return best;\n}\n\nfunction buildLayout(\n  claim: string,\n  rest: string,\n  W: number,\n  H: number,\n): Layout {\n  const portrait = H > W * 1.1;\n  const clean = claim.trim().replace(/[.!?…,;:]+$/u, \"\");\n  const explicit = clean\n    .split(\"\\n\")\n    .map((l) => split(l))\n    .filter((l) => l.length > 0);\n  const flat = explicit.flat();\n\n  let claimRows: string[][];\n  if (!portrait && explicit.length > 1) {\n    claimRows = explicit;\n  } else {\n    let bestF = 0;\n    claimRows = [flat];\n    const maxN = Math.min(flat.length, portrait ? 6 : 3);\n    for (let n = 1; n <= maxN; n++) {\n      const rows = balanced(flat, n);\n      const w = Math.max(...rows.map((r) => em(r.join(\" \"))));\n      const f = Math.min((W * 0.9) / (w + 0.3), (H * 0.84) / (n * PITCH));\n      if (f > bestF * 1.04) {\n        bestF = f;\n        claimRows = rows;\n      }\n    }\n  }\n\n  const space = em(\" \");\n  const dot = em(\".\");\n  const rowW = (r: string[]) => em(r.join(\" \"));\n  const claimW = Math.max(\n    ...claimRows.map((r, i) =>\n      i === claimRows.length - 1 ? rowW(r) + dot : rowW(r),\n    ),\n  );\n\n  const words: PlacedWord[] = [];\n  claimRows.forEach((row, li) => {\n    let x = 0;\n    for (const w of row) {\n      words.push({ text: w, line: li, x, rest: false, order: li });\n      x += em(w) + space;\n    }\n  });\n\n  const restWords = split(rest);\n  const measure = Math.max(claimW * 1.3, ...restWords.map((w) => em(w)));\n  const greedy: string[][] = [];\n  for (const w of restWords) {\n    const cur = greedy[greedy.length - 1];\n    if (cur && em([...cur, w].join(\" \")) <= measure) cur.push(w);\n    else greedy.push([w]);\n  }\n  const restRows =\n    greedy.length > 1 ? balanced(restWords, greedy.length) : greedy;\n  let line = claimRows.length - 1;\n  let order = 0;\n  for (const row of restRows) {\n    line += 1;\n    let cursor = 0;\n    for (const w of row) {\n      words.push({ text: w, line, x: cursor, rest: true, order: order++ });\n      cursor += em(w) + space;\n    }\n  }\n  const lineCount = line + 1;\n  const blockW = Math.max(claimW, ...words.map((w) => w.x + em(w.text)));\n\n  const c = ctx2d();\n  const fm = c.measureText(\"Hxg\");\n  const asc = fm.fontBoundingBoxAscent / MEASURE_PX;\n  const desc = fm.fontBoundingBoxDescent / MEASURE_PX;\n  const cap = c.measureText(\"H\").actualBoundingBoxAscent / MEASURE_PX;\n  const baseline = (1 - asc - desc) / 2 + asc;\n\n  const zoomF = Math.min(\n    (W * 0.9) / claimW,\n    (H * 0.84) / ((claimRows.length - 1) * PITCH + 1),\n  );\n  const finalF = Math.min(\n    (W * 0.84) / blockW,\n    (H * 0.74) / ((lineCount - 1) * PITCH + 1),\n    zoomF / 1.45,\n  );\n\n  return {\n    words,\n    lineCount,\n    claimLines: claimRows.length,\n    claimW,\n    blockW,\n    dotX: rowW(claimRows[claimRows.length - 1]),\n    baseline,\n    cap,\n    zoomF,\n    finalF,\n  };\n}\n\nexport function OutOfContext({\n  claim = \"Stop making\\nlaunch videos\",\n  rest = \"by hand.\",\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: OutOfContextProps) {\n  const frame = useCurrentFrame();\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 = useFontsReady();\n\n  const layout = useMemo(\n    () => (ready ? buildLayout(claim, rest, W, H) : null),\n    [ready, claim, rest, W, H],\n  );\n\n  if (!layout) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const room = durationInFrames * speed - MIN_HOLD;\n  const k = Math.max(0.55, Math.min(1, room / MOTION_END));\n  const t = (frame * speed) / k;\n\n  const R = layout.zoomF;\n  const sEnd = layout.finalF / layout.zoomF;\n  const { baseline, cap } = layout;\n\n  const visual = (lines: number, width: number) => ({\n    x: (width / 2) * R,\n    y: ((baseline - cap + (lines - 1) * PITCH + baseline) / 2) * R,\n  });\n  const f0 = visual(layout.claimLines, layout.claimW);\n  const f1 = visual(layout.lineCount, layout.blockW);\n\n  const p = interpolate(t, [PULL_AT, PULL_AT + PULL_LEN], [0, 1], {\n    ...CLAMP,\n    easing: EASE_CAM,\n  });\n  const creep = interpolate(t, [IN_LEN, PULL_AT], [0, 0.018], CLAMP);\n  const s = Math.exp(Math.log(sEnd) * p) * (1 + creep * (1 - p));\n  const fx = f0.x + (f1.x - f0.x) * p;\n  const fy = f0.y + (f1.y - f0.y) * p;\n  const camera = `translate(${(W / 2).toFixed(2)}px, ${(H / 2).toFixed(2)}px) scale(${s.toFixed(5)}) translate(${(-fx).toFixed(2)}px, ${(-fy).toFixed(2)}px)`;\n\n  const clipTop = (baseline - 1.0) * R;\n  const clipH = 1.3 * R;\n  const travel = 1.15 * R;\n\n  const lineIn = (li: number) =>\n    interpolate(t, [li * IN_STAGGER, li * IN_STAGGER + IN_LEN], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n  const restIn = (i: number) => {\n    const at = PULL_AT + PULL_LEN * REST_AT + i * REST_STAGGER;\n    return interpolate(t, [at, at + REST_LEN], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n  };\n\n  const drop = interpolate(t, [DROP_AT, DROP_AT + DROP_LEN], [0, 1], {\n    ...CLAMP,\n    easing: EASE_DROP,\n  });\n\n  const glyphStyle = {\n    position: \"absolute\" as const,\n    top: 0,\n    height: R,\n    fontFamily: outfit.fontFamily,\n    fontWeight: WGHT,\n    fontSize: R,\n    lineHeight: `${R}px`,\n    letterSpacing: `${TRACK}em`,\n    whiteSpace: \"pre\" as const,\n  };\n\n  const lines = Array.from({ length: layout.lineCount }, (_, li) => li);\n  const lastClaim = layout.claimLines - 1;\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      <div\n        style={{\n          position: \"absolute\",\n          left: 0,\n          top: 0,\n          transformOrigin: \"0 0\",\n          transform: camera,\n        }}\n      >\n        {lines.map((li) => {\n          const q = li < layout.claimLines ? lineIn(li) : 1;\n          return (\n            <div\n              key={li}\n              style={{\n                position: \"absolute\",\n                left: -0.2 * R,\n                top: li * PITCH * R + clipTop,\n                width: (layout.blockW + 0.6) * R,\n                height: clipH,\n                overflow: \"hidden\",\n              }}\n            >\n              {layout.words\n                .filter((w) => w.line === li)\n                .map((w) => {\n                  const r = w.rest ? restIn(w.order) : q;\n                  return (\n                    <div\n                      key={`${w.rest ? \"r\" : \"c\"}-${w.order}-${w.x.toFixed(3)}`}\n                      style={{\n                        ...glyphStyle,\n                        left: (w.x + 0.2) * R,\n                        top: -clipTop,\n                        color: w.rest ? hi : c.ink,\n                        transform: `translateY(${((1 - r) * travel).toFixed(2)}px)`,\n                      }}\n                    >\n                      {w.text}\n                    </div>\n                  );\n                })}\n              {li === lastClaim && drop < 1 ? (\n                <div\n                  style={{\n                    ...glyphStyle,\n                    left: (layout.dotX + 0.2) * R,\n                    top: -clipTop,\n                    color: c.ink,\n                    opacity: 1 - drop,\n                    transform: `translateY(${((1 - q) * travel + drop * 0.5 * R).toFixed(2)}px)`,\n                  }}\n                >\n                  .\n                </div>\n              ) : null}\n            </div>\n          );\n        })}\n      </div>\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/out-of-context.tsx"
    }
  ],
  "type": "registry:component"
}
