{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"particle-headline-assembling-into-letters-canvas","type":"registry:block","title":"Particle Headline Assembling Into Letters","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/particle-headline-assembling-into-letters-canvas.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\nconst WORD = \"Lantern\";\nconst COLORS = [\"#efdcbc\", \"#d9a35b\", \"#f7ecd8\"]; // sand, amber, pale cream\nconst ASSEMBLE_MS = 1500; // how long the dust takes to settle into letters\nconst CYCLE_MS = 11000; // full loop: assemble, rest, drift apart\nconst MAX_DOTS = 4200; // upper limit on particles, keeps each frame cheap\n\nexport default function ParticleHeadlineAssemblingIntoLetters() {\n  const box = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const host = box.current;\n    if (!canvas || !host) return;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    let w = 0;\n    let h = 0;\n    let n = 0;\n    let tx = new Float32Array(0);\n    let ty = new Float32Array(0);\n    let sx = new Float32Array(0);\n    let sy = new Float32Array(0);\n    let ox = new Float32Array(0);\n    let oy = new Float32Array(0);\n    let delay = new Float32Array(0);\n    let tone = new Uint8Array(0);\n    let size = new Float32Array(0);\n    let frame = 0;\n    let visible = true;\n    let start = performance.now();\n    const pointer = { x: -1e4, y: -1e4 };\n\n    const build = () => {\n      const r = host.getBoundingClientRect();\n      w = Math.max(1, r.width);\n      h = Math.max(1, r.height);\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n\n      const off = document.createElement(\"canvas\");\n      off.width = Math.ceil(w);\n      off.height = Math.ceil(h);\n      const o = off.getContext(\"2d\", { willReadFrequently: true });\n      if (!o) return;\n      let fs = h * 0.78;\n      o.font = `700 ${fs}px ui-serif, Georgia, \"Times New Roman\", serif`;\n      const tw = o.measureText(WORD).width;\n      if (tw > w * 0.92) fs *= (w * 0.92) / tw;\n      o.font = `700 ${fs}px ui-serif, Georgia, \"Times New Roman\", serif`;\n      o.textAlign = \"center\";\n      o.textBaseline = \"middle\";\n      o.fillStyle = \"#000\";\n      o.fillText(WORD, w / 2, h * 0.53);\n      const data = o.getImageData(0, 0, off.width, off.height).data;\n      const gap = w < 640 ? 2.5 : 3.5;\n      const xs: number[] = [];\n      const ys: number[] = [];\n      for (let y = 0; y < off.height; y += gap) {\n        for (let x = 0; x < off.width; x += gap) {\n          if (data[(Math.floor(y) * off.width + Math.floor(x)) * 4 + 3] > 140) {\n            xs.push(x + (Math.random() - 0.5) * 0.8);\n            ys.push(y + (Math.random() - 0.5) * 0.8);\n          }\n        }\n      }\n      const stride = Math.max(1, Math.ceil(xs.length / MAX_DOTS));\n      tx = Float32Array.from(xs.filter((_, i) => i % stride === 0));\n      ty = Float32Array.from(ys.filter((_, i) => i % stride === 0));\n      n = tx.length;\n      sx = new Float32Array(n);\n      sy = new Float32Array(n);\n      ox = new Float32Array(n);\n      oy = new Float32Array(n);\n      delay = new Float32Array(n);\n      tone = new Uint8Array(n);\n      size = new Float32Array(n);\n      for (let i = 0; i < n; i += 1) {\n        const a = Math.random() * Math.PI * 2;\n        const d = 0.35 + Math.random() * 0.75;\n        sx[i] = w / 2 + Math.cos(a) * w * 0.55 * d;\n        sy[i] = h / 2 + Math.sin(a) * h * 0.9 * d;\n        delay[i] = (tx[i] / w) * 380 + Math.random() * 260;\n        const roll = Math.random();\n        tone[i] = roll < 0.62 ? 0 : roll < 0.9 ? 1 : 2;\n        size[i] = gap * (0.62 + Math.random() * 0.4);\n      }\n    };\n\n    const ease = (p: number) => 1 - Math.pow(1 - p, 3);\n\n    const draw = (now: number, still: boolean) => {\n      const t = still ? ASSEMBLE_MS + 2000 : (now - start) % CYCLE_MS;\n      ctx.clearRect(0, 0, w, h);\n      const out = CYCLE_MS - 1400;\n      for (let c = 0; c < COLORS.length; c += 1) {\n        ctx.fillStyle = COLORS[c];\n        for (let i = 0; i < n; i += 1) {\n          if (tone[i] !== c) continue;\n          let p = Math.min(1, Math.max(0, (t - delay[i]) / (ASSEMBLE_MS - 600)));\n          if (t > out) p = 1 - Math.min(1, (t - out) / 1300) * 0.85;\n          const e = ease(p);\n          let x = sx[i] + (tx[i] - sx[i]) * e;\n          let y = sy[i] + (ty[i] - sy[i]) * e;\n          if (!still) {\n            x += Math.sin(now * 0.0011 + i) * 0.45;\n            y += Math.cos(now * 0.0013 + i * 1.7) * 0.45;\n            const dx = x - pointer.x;\n            const dy = y - pointer.y;\n            const d2 = dx * dx + dy * dy;\n            if (d2 < 3600) {\n              const f = (1 - d2 / 3600) * 2.2;\n              const d = Math.sqrt(d2) || 1;\n              ox[i] += (dx / d) * f;\n              oy[i] += (dy / d) * f;\n            }\n            ox[i] *= 0.9;\n            oy[i] *= 0.9;\n            x += ox[i];\n            y += oy[i];\n          }\n          ctx.globalAlpha = 0.35 + 0.65 * e;\n          ctx.fillRect(x, y, size[i], size[i]);\n        }\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const loop = (now: number) => {\n      draw(now, false);\n      frame = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (!frame && visible && !reduce) frame = requestAnimationFrame(loop);\n    };\n    const stop = () => {\n      cancelAnimationFrame(frame);\n      frame = 0;\n    };\n\n    build();\n    if (reduce) draw(0, true);\n    else play();\n\n    const ro = new ResizeObserver(() => {\n      build();\n      if (reduce) draw(0, true);\n    });\n    ro.observe(host);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) play();\n      else stop();\n    });\n    io.observe(host);\n    const move = (e: PointerEvent) => {\n      const r = host.getBoundingClientRect();\n      pointer.x = e.clientX - r.left;\n      pointer.y = e.clientY - r.top;\n    };\n    const leave = () => {\n      pointer.x = -1e4;\n      pointer.y = -1e4;\n    };\n    host.addEventListener(\"pointermove\", move);\n    host.addEventListener(\"pointerleave\", leave);\n    start = performance.now();\n\n    return () => {\n      stop();\n      ro.disconnect();\n      io.disconnect();\n      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n    };\n  }, [reduce]);\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#15110e] px-5 py-16 sm:px-10 sm:py-20\">\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 bg-[radial-gradient(55%_50%_at_50%_42%,rgba(217,163,91,0.16),transparent_70%)]\" />\n      <div className=\"relative mx-auto max-w-5xl text-center\">\n        <p className=\"text-xs font-semibold uppercase tracking-[0.32em] text-[#c9b08a]\">Small-batch coffee roasters</p>\n        <h1 className=\"sr-only\">{WORD}</h1>\n        <div ref={box} className=\"relative mx-auto mt-4 h-[150px] w-full touch-pan-y sm:h-[240px] lg:h-[280px]\">\n          <canvas ref={canvasRef} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        </div>\n        <p className=\"mx-auto mt-4 max-w-lg text-base leading-relaxed text-[#d8c6aa] sm:text-lg\">\n          Beans roasted every Tuesday in a converted boathouse, shipped the same afternoon so they reach you within the week.\n        </p>\n        <a\n          href=\"#\"\n          className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#efdcbc] px-7 text-sm font-semibold text-[#15110e] transition hover:bg-[#f7ecd8] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#d9a35b] focus-visible:ring-offset-2 focus-visible:ring-offset-[#15110e]\"\n        >\n          Choose this week&apos;s roast\n        </a>\n      </div>\n    </section>\n  );\n}\n"}]}
