{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"cinema-marquee-headline-with-letter-tiles-that-slide-along-rails-and-chasing-bulbs","type":"registry:component","title":"Cinema Marquee Headline with Letter Tiles That Slide Along Rails and Chasing Bulbs","description":"Cinema marquee on a canvas: letter tiles slide along rails and clack together inside a red frame of chasing bulbs. Free React code and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/cinema-marquee-headline-with-letter-tiles-that-slide-along-rails-and-chasing-bulbs.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n/** What the readerboard says when the page opens, one string per rail. */\nconst MESSAGE = [\"TONIGHT ONLY\", \"THE GRAND OPENING\", \"DOORS AT EIGHT\"];\n/** The bottom rail is re-hung with each of these in turn until the visitor types. */\nconst LAST_ROW = [\"DOORS AT EIGHT\", \"LIVE BAND AT TEN\", \"BRING A FRIEND\"];\nconst RED_ROWS = [0]; // rails set in red letters; the rest are black\nconst MAX = 18; // letters per rail\nconst HOLD = 4; // seconds the finished sign stands, every lamp lit, before the chase starts\nconst ENAMEL = [\"#c3242d\", \"#8d1119\", \"#560810\"]; // the painted channel the bulbs sit in: top, middle, bottom\nconst PATTERNS = [\"Chase\", \"Twinkle\", \"Steady\"] as const;\nconst SLUG = \"cinema-marquee-headline\";\n\ntype Tile = { ch: string; tint: number; w: number; home: number; x: number; v: number; at: number; a: number; av: number; tilt: number; dy: number };\ntype Rail = { text: string; tiles: Tile[]; leaving: Tile[] };\ntype Bulb = { x: number; y: number; v: number; dead: boolean; loose: boolean; to: number };\n\nconst clean = (raw: string) =>\n  raw.toUpperCase().replace(/[^A-Z0-9 &!?.,'+:\\-\\n]/g, \"\").split(\"\\n\").slice(0, MESSAGE.length).map((l) => l.slice(0, MAX)).join(\"\\n\");\n\nexport default function CinemaMarqueeHeadline() {\n  const wrap = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [text, setText] = useState(MESSAGE.join(\"\\n\"));\n  const [pattern, setPattern] = useState(0);\n  const [typed, setTyped] = useState(false);\n  const api = useRef<{ words: (lines: string[]) => void; pattern: (n: number) => void } | null>(null);\n  const lines = text.split(\"\\n\");\n\n  useEffect(() => {\n    const cv = canvas.current, box = wrap.current;\n    if (!cv || !box) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dpr = Math.min(2, window.devicePixelRatio || 1);\n    const base = document.createElement(\"canvas\");\n    const sprite = { glow: document.createElement(\"canvas\"), ember: document.createElement(\"canvas\"), hot: document.createElement(\"canvas\"), off: document.createElement(\"canvas\") };\n    let seed = 5;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n    // Everything below is measured from the width the sign is given.\n    const g = { W: 0, H: 0, sx: 0, sy: 0, sw: 0, sh: 0, t: 0, ch: 0, bx: 0, by: 0, bw: 0, bh: 0, rh: 0, th: 0, r: 0, sp: 0, size: 0, cap: 0, squeeze: 1 };\n    let bulbs: Bulb[] = [];\n    const rails: Rail[] = MESSAGE.map(() => ({ text: \"\", tiles: [], leaving: [] }));\n    let mode = 0, step = 0, clock = 0, hold = HOLD, pointer = { x: -999, y: -999 };\n    const FACE = 'Impact, Haettenschweiler, \"Arial Narrow Bold\", \"Helvetica Neue\", Arial, sans-serif';\n\n    const box4 = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) => {\n      c.beginPath(); c.moveTo(x + r, y); c.arcTo(x + w, y, x + w, y + h, r); c.arcTo(x + w, y + h, x, y + h, r); c.arcTo(x, y + h, x, y, r); c.arcTo(x, y, x + w, y, r); c.closePath();\n    };\n    const chrome = (c: CanvasRenderingContext2D, y0: number, y1: number) => {\n      const m = c.createLinearGradient(0, y0, 0, y1);\n      ([[0, \"#fbfbf8\"], [0.07, \"#c6c8cb\"], [0.2, \"#565a61\"], [0.34, \"#e9eaec\"], [0.5, \"#8e9299\"], [0.66, \"#2c2f35\"], [0.8, \"#b9bcc1\"], [0.93, \"#5b5f66\"], [1, \"#16171a\"]] as [number, string][]).forEach(([p, col]) => m.addColorStop(p, col));\n      return m;\n    };\n\n    const paintSprites = () => {\n      const r = g.r * dpr;\n      const disc = (cv2: HTMLCanvasElement, half: number, stops: [number, string][]) => {\n        cv2.width = cv2.height = Math.ceil(half * 2);\n        const c = cv2.getContext(\"2d\"); if (!c) return;\n        const m = c.createRadialGradient(half, half, 0, half, half, half);\n        stops.forEach(([p, col]) => m.addColorStop(p, col));\n        c.fillStyle = m; c.fillRect(0, 0, half * 2, half * 2);\n      };\n      disc(sprite.glow, r * 5.2, [[0, \"rgba(255,186,84,0.5)\"], [0.16, \"rgba(255,150,50,0.26)\"], [0.42, \"rgba(255,112,30,0.08)\"], [1, \"rgba(255,90,20,0)\"]]);\n      disc(sprite.ember, r * 0.98, [[0, \"#ffc061\"], [0.3, \"#f58a25\"], [0.6, \"rgba(200,70,12,0.72)\"], [0.85, \"rgba(120,30,6,0.25)\"], [1, \"rgba(120,30,6,0)\"]]);\n      disc(sprite.hot, r * 1.7, [[0, \"#fffef4\"], [0.3, \"#fff3c2\"], [0.52, \"#ffd67a\"], [0.7, \"rgba(255,176,70,0.75)\"], [1, \"rgba(255,140,40,0)\"]]);\n      // The unlit lamp: a brass collar, smoky clear glass, a filament and one window reflection.\n      const half = r * 1.5; sprite.off.width = sprite.off.height = Math.ceil(half * 2);\n      const c = sprite.off.getContext(\"2d\"); if (!c) return;\n      c.translate(half, half);\n      c.beginPath(); c.arc(0, r * 0.16, r * 1.34, 0, 7); c.fillStyle = \"rgba(0,0,0,0.5)\"; c.fill();\n      const collar = c.createLinearGradient(-r, -r, r, r); collar.addColorStop(0, \"#e2c27c\"); collar.addColorStop(0.45, \"#8a6a2e\"); collar.addColorStop(1, \"#3a2a10\");\n      c.beginPath(); c.arc(0, 0, r * 1.24, 0, 7); c.fillStyle = collar; c.fill();\n      c.beginPath(); c.arc(0, 0, r * 1.04, 0, 7); c.fillStyle = \"#120807\"; c.fill();\n      const glass = c.createRadialGradient(-r * 0.3, -r * 0.36, r * 0.05, 0, 0, r);\n      glass.addColorStop(0, \"#a8998a\"); glass.addColorStop(0.4, \"#5c4e42\"); glass.addColorStop(0.82, \"#2b221c\"); glass.addColorStop(1, \"#4a3e34\");\n      c.beginPath(); c.arc(0, 0, r, 0, 7); c.fillStyle = glass; c.fill();\n      c.lineWidth = Math.max(1, r * 0.09); c.strokeStyle = \"rgba(255,170,90,0.5)\"; c.beginPath(); c.arc(0, r * 0.08, r * 0.3, Math.PI * 1.1, Math.PI * 1.9); c.stroke();\n      c.fillStyle = \"rgba(255,255,255,0.72)\"; c.beginPath(); c.ellipse(-r * 0.36, -r * 0.42, r * 0.24, r * 0.13, -0.7, 0, 7); c.fill();\n      c.lineWidth = Math.max(1, r * 0.07); c.strokeStyle = \"rgba(255,255,255,0.2)\"; c.beginPath(); c.arc(0, 0, r * 0.95, 0.3, 1.5); c.stroke();\n    };\n\n    const paintBase = () => {\n      base.width = cv.width; base.height = cv.height;\n      const c = base.getContext(\"2d\"); if (!c) return;\n      c.setTransform(dpr, 0, 0, dpr, 0, 0);\n      const { sx, sy, sw, sh, t, ch, bx, by, bw, bh, rh } = g, rad = ch * 0.42;\n      // The box stands off the wall: a wide soft shadow and a tight one underneath.\n      c.save(); c.shadowColor = \"rgba(0,0,0,0.85)\"; c.shadowBlur = sw * 0.08; c.shadowOffsetY = sw * 0.035 * dpr; box4(c, sx + 6, sy + 6, sw - 12, sh - 12, rad); c.fillStyle = \"#000\"; c.fill(); c.restore();\n      box4(c, sx, sy, sw, sh, rad); c.fillStyle = chrome(c, sy, sy + sh); c.fill();\n      // Vitreous enamel channel.\n      const en = c.createLinearGradient(0, sy, 0, sy + sh); en.addColorStop(0, ENAMEL[0]); en.addColorStop(0.5, ENAMEL[1]); en.addColorStop(1, ENAMEL[2]);\n      box4(c, sx + t, sy + t, sw - t * 2, sh - t * 2, rad - t * 0.5); c.fillStyle = en; c.fill();\n      c.save(); c.clip();\n      const gloss = c.createLinearGradient(0, sy + t, 0, sy + t + ch); gloss.addColorStop(0, \"rgba(255,255,255,0.42)\"); gloss.addColorStop(0.16, \"rgba(255,255,255,0.1)\"); gloss.addColorStop(0.5, \"rgba(255,255,255,0)\");\n      c.fillStyle = gloss; c.fillRect(sx, sy, sw, ch + t);\n      const side = c.createLinearGradient(sx, 0, sx + sw, 0); side.addColorStop(0, \"rgba(255,255,255,0.16)\"); side.addColorStop(0.05, \"rgba(255,255,255,0)\"); side.addColorStop(0.94, \"rgba(0,0,0,0)\"); side.addColorStop(1, \"rgba(0,0,0,0.4)\");\n      c.fillStyle = side; c.fillRect(sx, sy, sw, sh);\n      c.restore();\n      // Polished trim round the light box, then the opal glass.\n      const ix = bx - t, iy = by - t, iw = bw + t * 2, ih = bh + t * 2;\n      c.save(); c.shadowColor = \"rgba(0,0,0,0.7)\"; c.shadowBlur = 8 * dpr; c.shadowOffsetY = 3 * dpr; c.fillStyle = \"#000\"; c.fillRect(ix, iy, iw, ih); c.restore();\n      c.fillStyle = chrome(c, iy, iy + ih); c.fillRect(ix, iy, iw, ih);\n      const milk = c.createLinearGradient(0, by, 0, by + bh); milk.addColorStop(0, \"#f6e6c0\"); milk.addColorStop(0.2, \"#fff7e2\"); milk.addColorStop(0.8, \"#fff4da\"); milk.addColorStop(1, \"#efd9aa\");\n      c.fillStyle = milk; c.fillRect(bx, by, bw, bh);\n      c.save(); c.beginPath(); c.rect(bx, by, bw, bh); c.clip();\n      // Fluorescent tubes behind the glass show as soft bright bands.\n      for (let i = 0; i < MESSAGE.length; i++) {\n        const y = by + rh * (i + 0.5), band = c.createLinearGradient(0, y - rh * 0.5, 0, y + rh * 0.5);\n        band.addColorStop(0, \"rgba(255,255,255,0)\"); band.addColorStop(0.5, \"rgba(255,255,255,0.7)\"); band.addColorStop(1, \"rgba(255,255,255,0)\");\n        c.fillStyle = band; c.fillRect(bx, y - rh * 0.5, bw, rh);\n      }\n      const ends = c.createLinearGradient(bx, 0, bx + bw, 0); ends.addColorStop(0, \"rgba(196,140,60,0.42)\"); ends.addColorStop(0.07, \"rgba(196,140,60,0)\"); ends.addColorStop(0.93, \"rgba(196,140,60,0)\"); ends.addColorStop(1, \"rgba(196,140,60,0.42)\");\n      c.fillStyle = ends; c.fillRect(bx, by, bw, bh);\n      // Seams between the glass panes, and the rails the letters hang on.\n      for (let i = 1; i < 3; i++) { const x = Math.round(bx + (bw * i) / 3); c.fillStyle = \"rgba(120,86,36,0.5)\"; c.fillRect(x, by, 1, bh); c.fillStyle = \"rgba(255,255,255,0.5)\"; c.fillRect(x + 1, by, 1, bh); }\n      for (let i = 0; i <= MESSAGE.length; i++) {\n        const y = Math.round(by + rh * i), h = Math.max(2, rh * 0.045);\n        c.fillStyle = \"rgba(60,40,14,0.16)\"; c.fillRect(bx, y - h * 0.5 + h, bw, h * 1.6);\n        const rail = c.createLinearGradient(0, y - h * 0.5, 0, y + h * 0.5); rail.addColorStop(0, \"#8d8474\"); rail.addColorStop(0.35, \"#4b4336\"); rail.addColorStop(1, \"#1f1a12\");\n        c.fillStyle = rail; c.fillRect(bx, y - h * 0.5, bw, h);\n      }\n      const lid = c.createLinearGradient(0, by, 0, by + bh * 0.14); lid.addColorStop(0, \"rgba(50,30,8,0.5)\"); lid.addColorStop(1, \"rgba(50,30,8,0)\");\n      c.fillStyle = lid; c.fillRect(bx, by, bw, bh * 0.14);\n      c.restore();\n    };\n\n    const hang = (rail: Rail, row: number, now: number, wait: number, placed = false) => {\n      ctx.font = `${g.size}px ${FACE}`;\n      const chars = [...rail.text];\n      const widths = chars.map((ch) => (ch === \" \" ? g.th * 0.36 : ctx.measureText(ch).width * g.squeeze + g.th * 0.2));\n      const total = widths.reduce((a, b) => a + b, 0), fit = Math.min(1, (g.bw * 0.94) / Math.max(1, total));\n      let x = (g.bw - total * fit) / 2;\n      rail.tiles = [];\n      chars.forEach((ch, i) => {\n        const w = widths[i] * fit;\n        if (ch !== \" \") {\n          rail.tiles.push({ ch, tint: 0.03 + rand() * 0.1, w, home: x, x: reduce || placed ? 0 : g.bw - x + 12, v: 0, at: now + wait + rail.tiles.length * 0.04, a: 0, av: 0, tilt: (rand() - 0.5) * 0.034, dy: (rand() - 0.5) * g.th * 0.03 });\n        }\n        x += w;\n      });\n      void row;\n    };\n\n    const measure = () => {\n      const W = Math.min(box.clientWidth, 1040), narrow = W < 560;\n      const pad = Math.max(18, Math.min(74, W * 0.07));\n      g.W = W; g.sw = Math.min(W - pad * 2, 880); g.sh = Math.round(g.sw * (narrow ? 0.72 : 0.5));\n      g.sx = (W - g.sw) / 2; g.sy = pad; g.H = g.sh + pad * 2;\n      g.t = Math.max(2.5, g.sw * 0.0075); g.ch = Math.max(17, Math.min(50, g.sw * 0.058));\n      g.bx = Math.round(g.sx + g.t * 2 + g.ch); g.by = Math.round(g.sy + g.t * 2 + g.ch);\n      g.bw = Math.round(g.sw - (g.t * 2 + g.ch) * 2); g.bh = Math.round(g.sh - (g.t * 2 + g.ch) * 2);\n      g.rh = g.bh / MESSAGE.length; g.th = g.rh * 0.86; g.r = g.ch * 0.27;\n      cv.width = Math.round(W * dpr); cv.height = Math.round(g.H * dpr);\n      cv.style.height = `${g.H}px`;\n      // Size the face so its capitals are seven tenths of a tile, and squeeze wide fallbacks to marquee proportions.\n      ctx.font = `100px ${FACE}`;\n      const probe = ctx.measureText(\"H\"), capOf100 = probe.actualBoundingBoxAscent || 72;\n      g.cap = g.th * 0.7; g.size = (g.cap / capOf100) * 100; g.squeeze = Math.min(1, 52 / probe.width);\n      // Lamps round the channel, clockwise from the top left, in a count the chase divides evenly.\n      const x0 = g.sx + g.t + g.ch / 2, x1 = g.sx + g.sw - g.t - g.ch / 2, y0 = g.sy + g.t + g.ch / 2, y1 = g.sy + g.sh - g.t - g.ch / 2;\n      g.sp = g.ch * 0.94;\n      let nx = Math.round((x1 - x0) / g.sp); const ny = Math.round((y1 - y0) / g.sp);\n      while ((nx + ny) % 3) nx++;\n      const old = bulbs; bulbs = [];\n      const add = (x: number, y: number) => { const n = bulbs.length; bulbs.push({ x, y, v: old[n]?.v ?? 1, dead: old[n]?.dead ?? false, loose: false, to: 0 }); };\n      for (let i = 0; i < nx; i++) add(x0 + ((x1 - x0) * i) / nx, y0);\n      for (let i = 0; i < ny; i++) add(x1, y0 + ((y1 - y0) * i) / ny);\n      for (let i = 0; i < nx; i++) add(x1 - ((x1 - x0) * i) / nx, y1);\n      for (let i = 0; i < ny; i++) add(x0, y1 - ((y1 - y0) * i) / ny);\n      bulbs[Math.floor(bulbs.length * 0.62)].loose = true;\n      paintSprites(); paintBase();\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, cv.width, cv.height);\n      ctx.drawImage(base, 0, 0);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      const { bx, by, bw, bh, rh, th, r } = g;\n      // Letter tiles: clear acrylic with an opaque letter, seen against the light.\n      ctx.save(); ctx.beginPath(); ctx.rect(bx, by, bw, bh); ctx.clip();\n      ctx.font = `${g.size}px ${FACE}`; ctx.textAlign = \"center\"; ctx.textBaseline = \"alphabetic\";\n      rails.forEach((rail, row) => {\n        const cy = by + rh * (row + 0.5), ink = RED_ROWS.includes(row) ? \"#b3141c\" : \"#17120d\";\n        for (const tile of [...rail.leaving, ...rail.tiles]) {\n          const x = bx + tile.home + tile.x;\n          if (x > bx + bw || x + tile.w < bx) continue;\n          ctx.save(); ctx.translate(x + tile.w / 2, cy - th / 2 + tile.dy); ctx.rotate(tile.tilt + tile.a);\n          ctx.fillStyle = `rgba(150,104,40,${tile.tint})`; ctx.fillRect(-tile.w / 2 + 0.5, 0, tile.w - 1, th);\n          ctx.fillStyle = \"rgba(96,64,20,0.22)\"; ctx.fillRect(-tile.w / 2 + 0.5, 0, 1, th); ctx.fillRect(tile.w / 2 - 1.5, 0, 1, th);\n          ctx.fillStyle = \"rgba(255,255,255,0.55)\"; ctx.fillRect(-tile.w / 2 + 1.5, 0, 1, th);\n          ctx.translate(0, th / 2 + g.cap / 2); ctx.scale(g.squeeze * Math.min(1, (tile.w * 0.9) / Math.max(1, ctx.measureText(tile.ch).width * g.squeeze)), 1);\n          ctx.fillStyle = ink; ctx.fillText(tile.ch, 0, 0);\n          ctx.restore();\n        }\n      });\n      ctx.restore();\n      // Lamps: socket and glass first, then the heat. A filament goes from ember to white as it warms.\n      const o = sprite.off.width / dpr, e = sprite.ember.width / dpr, h = sprite.hot.width / dpr, gl = sprite.glow.width / dpr;\n      for (const b of bulbs) ctx.drawImage(sprite.off, b.x - o / 2, b.y - o / 2, o, o);\n      ctx.globalCompositeOperation = \"lighter\";\n      for (const b of bulbs) { if (b.v < 0.01) continue; ctx.globalAlpha = Math.min(1, Math.pow(b.v, 1.4)); ctx.drawImage(sprite.glow, b.x - gl / 2, b.y - gl / 2, gl, gl); }\n      ctx.globalCompositeOperation = \"source-over\";\n      for (const b of bulbs) {\n        if (b.v < 0.01) continue;\n        ctx.globalAlpha = Math.min(1, b.v * 2.4); ctx.drawImage(sprite.ember, b.x - e / 2, b.y - e / 2, e, e);\n        ctx.globalAlpha = Math.max(0, Math.min(1, (b.v - 0.3) / 0.6)); ctx.drawImage(sprite.hot, b.x - h / 2, b.y - h / 2, h, h);\n      }\n      ctx.globalAlpha = 1;\n      for (const b of bulbs) { ctx.fillStyle = \"rgba(255,255,255,0.8)\"; ctx.beginPath(); ctx.ellipse(b.x - r * 0.36, b.y - r * 0.42, r * 0.2, r * 0.1, -0.7, 0, 7); ctx.fill(); }\n    };\n\n    let raf = 0, last = 0, seen = true;\n    const tick = (dt: number) => {\n      clock += dt;\n      // Bulbs.\n      step += dt * 7.5;\n      const s = Math.floor(step);\n      bulbs.forEach((b, i) => {\n        // The sign opens finished: every lamp is lit and steady until the hold runs out.\n        const held = clock < hold;\n        let to = held ? 1 : mode === 0 ? ((((i - s) % 3) + 3) % 3 === 0 ? 0.04 : 1) : mode === 1 ? b.to : 1;\n        if (mode === 1 && rand() < dt * 2.2) b.to = rand() < 0.62 ? 1 : 0.05;\n        const d = Math.hypot(b.x - pointer.x, b.y - pointer.y);\n        if (d < g.sp * 2.6) to = 1;\n        if (!held && b.loose && Math.sin(clock * 31) * Math.sin(clock * 7.3) > 0.55) to *= 0.25;\n        if (b.dead) to = 0;\n        b.v += (to - b.v) * Math.min(1, dt * (to > b.v ? 20 : 8.5));\n      });\n      // Tiles: a spring pulls each one home along its rail, and none may pass the tile in front.\n      for (const rail of rails) {\n        rail.tiles.forEach((tl, i) => {\n          if (clock < tl.at) return;\n          tl.v += (-150 * tl.x - 19 * tl.v) * dt; tl.x += tl.v * dt;\n          const prev = rail.tiles[i - 1];\n          if (prev) {\n            const min = prev.home + prev.x + prev.w - tl.home;\n            if (tl.x < min) { const rel = tl.v - prev.v; tl.x = min; if (rel < 0) { tl.v -= rel * 0.85; prev.v += rel * 0.4; tl.av -= rel * 0.0011; prev.av += rel * 0.0007; } }\n          }\n          tl.av += (-320 * tl.a - 11 * tl.av) * dt; tl.a = Math.max(-0.1, Math.min(0.1, tl.a + tl.av * dt));\n        });\n        rail.leaving.forEach((tl) => { if (clock < tl.at) return; tl.v -= 3400 * dt; tl.x += tl.v * dt; tl.a += (0.05 - tl.a) * dt * 8; });\n        rail.leaving = rail.leaving.filter((tl) => tl.home + tl.x + tl.w > -20);\n      }\n    };\n    const settle = () => { bulbs.forEach((b) => { b.v = b.dead ? 0 : 0.92; }); rails.forEach((rl) => { rl.leaving = []; rl.tiles.forEach((tl) => { tl.x = 0; tl.v = 0; tl.a = 0; }); }); draw(); };\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.033, (now - last) / 1000 || 0.016); last = now;\n      tick(dt * 0.5); tick(dt * 0.5); draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (reduce) { settle(); return; } if (!raf) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n\n    const words = (next: string[], first = false) => {\n      rails.forEach((rail, row) => {\n        const t = (next[row] ?? \"\").trim();\n        if (t === rail.text && !first) return;\n        rail.leaving = reduce ? [] : [...rail.leaving, ...rail.tiles.map((tl, i) => ({ ...tl, at: clock + i * 0.022 }))];\n        rail.text = t;\n        // The opening words are hung in place; every later change slides in from the right.\n        hang(rail, row, clock, first ? 0 : rail.leaving.length ? 0.34 : 0, first);\n      });\n      wake();\n    };\n    api.current = { words: (l) => words(l), pattern: (n) => { if (n !== mode) hold = 0; mode = n; wake(); } };\n\n    measure(); words(MESSAGE, true);\n    const ro = new ResizeObserver(() => { if (Math.min(box.clientWidth, 1040) === g.W) return; measure(); rails.forEach((rail, row) => { rail.leaving = []; hang(rail, row, clock, 0); rail.tiles.forEach((tl) => { tl.x = 0; tl.at = 0; }); }); wake(); });\n    ro.observe(box);\n\n    const at = (ev: PointerEvent) => { const b = cv.getBoundingClientRect(); return { x: ev.clientX - b.left, y: ev.clientY - b.top }; };\n    const near = (pt: { x: number; y: number }) => bulbs.find((b) => Math.hypot(b.x - pt.x, b.y - pt.y) < g.sp * 0.5);\n    const onMove = (ev: PointerEvent) => { pointer = at(ev); cv.style.cursor = near(pointer) ? \"pointer\" : \"default\"; if (reduce) draw(); };\n    const onLeave = () => { pointer = { x: -999, y: -999 }; };\n    // Tap a lamp to unscrew it, tap again to put it back.\n    const onDown = (ev: PointerEvent) => { const b = near(at(ev)); if (b) { b.dead = !b.dead; if (!b.dead) b.v = 0.2; wake(); } };\n    cv.addEventListener(\"pointermove\", onMove); cv.addEventListener(\"pointerleave\", onLeave); cv.addEventListener(\"pointerdown\", onDown);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      cancelAnimationFrame(raf); api.current = null; ro.disconnect(); io.disconnect();\n      cv.removeEventListener(\"pointermove\", onMove); cv.removeEventListener(\"pointerleave\", onLeave); cv.removeEventListener(\"pointerdown\", onDown);\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  useEffect(() => { api.current?.words(text.split(\"\\n\")); }, [text]);\n  useEffect(() => { api.current?.pattern(pattern); }, [pattern]);\n  // Until somebody types, the bottom rail is changed every few seconds.\n  useEffect(() => {\n    if (typed || window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    let n = 0;\n    const id = window.setInterval(() => { n = (n + 1) % LAST_ROW.length; setText((t) => { const l = t.split(\"\\n\"); l[MESSAGE.length - 1] = LAST_ROW[n]; return l.join(\"\\n\"); }); }, 5200);\n    return () => window.clearInterval(id);\n  }, [typed]);\n\n  return (\n    <div className=\"flex min-h-[640px] w-full flex-col items-center justify-center bg-[#0b0809] bg-[radial-gradient(70%_60%_at_50%_40%,#2a1410_0%,#150c0b_45%,#0b0809_100%)] px-4 py-10 sm:min-h-[760px]\">\n      <h2 className=\"sr-only\">{lines.filter(Boolean).join(\". \")}</h2>\n      <div ref={wrap} className=\"w-full max-w-[1040px]\">\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"mx-auto block w-full touch-manipulation\" style={{ height: 520 }} />\n      </div>\n\n      <div className=\"mt-1 flex w-full max-w-[560px] flex-col items-stretch gap-4 sm:flex-row sm:items-start\">\n        <div className=\"flex-1\">\n          <label htmlFor={`${SLUG}-words`} className=\"mb-2 block font-mono text-[11px] uppercase tracking-[0.18em] text-[#c9a98a]\">\n            Hang your own three lines\n          </label>\n          <textarea\n            id={`${SLUG}-words`}\n            rows={3}\n            value={text}\n            spellCheck={false}\n            onChange={(e) => { setTyped(true); setText(clean(e.target.value)); }}\n            className=\"w-full resize-none rounded-md border border-[#4a2e25] bg-[#1a100e] px-3 py-2 font-mono text-[12px] uppercase leading-5 tracking-[0.14em] text-[#f6e6c8] outline-none focus-visible:ring-2 focus-visible:ring-[#ffbf55]\"\n          />\n        </div>\n        <div role=\"radiogroup\" aria-label=\"Bulb pattern\" className=\"flex shrink-0 gap-1 sm:mt-[26px]\">\n          {PATTERNS.map((name, i) => (\n            <button\n              key={name}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={pattern === i}\n              onClick={() => setPattern(i)}\n              className={`h-10 flex-1 rounded-md px-4 font-mono text-[11px] uppercase tracking-[0.18em] outline-none transition-colors focus-visible:ring-2 focus-visible:ring-[#ffbf55] motion-reduce:transition-none sm:flex-none ${pattern === i ? \"bg-[#f6e6c8] text-[#1a100e]\" : \"bg-[#1a100e] text-[#c9a98a] hover:text-[#f6e6c8]\"}`}\n            >\n              {name}\n            </button>\n          ))}\n        </div>\n      </div>\n    </div>\n  );\n}\n"}]}
