{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"halftone-dots","type":"registry:ui","title":"Halftone Dots","description":"An image rebuilt as a field of dots.","docs":"Sound: the data-slot attributes on this component are read by the Sound layer, installed separately with `npx shadcn@latest add @kobra/sound`.\nKeep the data-slot and data-sound attributes as written — they are the wiring, not decoration.\nWrap your app once in <SoundEffects> from components/ui/sound.tsx and every Kobra component answers a press.","files":[{"path":"components/ui/halftone-dots.tsx","content":"'use client'\n\nimport { useEffect, useRef, useState, type CSSProperties } from 'react'\nimport { createPortal } from 'react-dom'\n\nimport { cn } from '@/lib/utils'\n\ntype Field = {\n  r: Float32Array\n  rgb: Float32Array\n  alpha: Float32Array\n  reveal: Float32Array\n}\n\nconst hexRgb = (hex: string): [number, number, number] => {\n  const n = hex.replace('#', '')\n  return [parseInt(n.slice(0, 2), 16), parseInt(n.slice(2, 4), 16), parseInt(n.slice(4, 6), 16)]\n}\n\nconst easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2)\n\nconst MORPH = 200\nconst MORPH_STAGGER = 220\nconst MORPH_DIP = 0.5\n\nconst CELL = 6\nconst SUP = 4\nconst SPILL = 72\n\nconst DOT_FLOOR = 0.22\nconst DOT_COVERAGE = 0.18\nconst DOT_TONE = 0.52\n\nconst TONE_CONTRAST = 1.6\nconst COLOR_CONTRAST = 1.25\nconst PAPER_R = 0.3\nconst PAPER_ALPHA = 0.28\n\nconst LATENT_FLOOR = 0.14\nconst LATENT_COVERAGE = 0.1\nconst LATENT_TONE = 0.26\n\nconst LATENT_ALPHA = 0.2\n\nconst LATENT_BOOST = 0.45\nconst LATENT_LIFT = 210\nconst MARK_INSET = 24\nconst MARK_CLEAR = CELL * 2 + 6\nconst MARK_R = 0.7\n\nconst FULL_FROM = 0.8\nconst FULL_CORNER = 0.82\nconst FULL_GROW = 1.05\n\nconst LENS_RADIUS = 64\nconst LENS_BOOST = 0.55\nconst LENS_FOLLOW = 55\nconst LENS_FADE_IN = 140\nconst LENS_FADE_OUT = 220\nconst LENS_WOBBLE = 0.22\nconst LENS_DRIFT = 0.0011\nconst LENS_ATTACK = 60\nconst LENS_RELEASE = 420\n\nconst LENS_PUSH = 4.5\nconst SPRING_STIFFNESS = 1600\nconst SPRING_DAMPING = 68\nconst SPRING_STEP = 8\nconst RIPPLE_SPEED = 0.75\nconst RIPPLE_WIDTH = 30\nconst RIPPLE_PUSH = 5.8\nconst RIPPLE_LIFE = 400\n\nconst BURST_PRESSES = 7\nconst BURST_GAP = 500\n\nconst BURST_MAX = 2600\n\ntype BurstDot = {\n  x: number\n  y: number\n  r: number\n  color: string\n  vx: number\n  vy: number\n  spin: number\n}\n\nconst BURST_SPEED = 800\n\nconst BURST_GRAVITY = 850\n\nconst BURST_DRAG = 0.72\nconst BURST_BOUNCE = 0.34\nconst BURST_FRICTION = 0.82\nconst BURST_REST = 90\n\nconst SOLID = '[data-halftone-solid]'\n\nfunction scatter(\n  lat: {\n    x: Float32Array\n    y: Float32Array\n    width: number\n    height: number\n    now: { r: Float32Array; rgb: Float32Array; alpha: Float32Array }\n  },\n  rect: DOMRect,\n  from: { clientX: number; clientY: number },\n): BurstDot[] {\n\n  const scale = rect.width / lat.width\n  const dots: BurstDot[] = []\n\n  const stride = Math.max(1, Math.ceil(lat.x.length / (BURST_MAX * 2)))\n\n  for (let i = 0; i < lat.x.length; i += stride) {\n\n    const alpha = lat.now.alpha[i]!\n    if (lat.now.r[i]! <= 0.06 || alpha <= 0.004) continue\n    const r = lat.now.r[i]! * scale\n\n    const x = rect.left + lat.x[i]! * scale\n    const y = rect.top + lat.y[i]! * scale\n\n    const dx = x - from.clientX\n    const dy = y - from.clientY\n    const reach = Math.max(40, Math.hypot(rect.width, rect.height) / 2)\n\n    const near = Math.max(0.55, 1 - Math.hypot(dx, dy) / reach)\n    const angle = Math.atan2(dy, dx) + (Math.random() - 0.5) * 2\n    const speed = BURST_SPEED * near * (0.35 + Math.random() * 1.1)\n\n    dots.push({\n      x,\n      y,\n      r,\n      color: `rgb(${String(Math.round(lat.now.rgb[i * 3]!))} ${String(\n        Math.round(lat.now.rgb[i * 3 + 1]!),\n      )} ${String(Math.round(lat.now.rgb[i * 3 + 2]!))} / ${alpha.toFixed(2)})`,\n      vx: Math.cos(angle) * speed,\n      vy: Math.sin(angle) * speed - 200 * near,\n      spin: 0,\n    })\n  }\n\n  return dots\n}\n\nfunction BurstLayer({ dots, onDone }: { dots: BurstDot[]; onDone: () => void }) {\n  const canvas = useRef<HTMLCanvasElement>(null)\n  const done = useRef(onDone)\n  done.current = onDone\n\n  useEffect(() => {\n    const surface = canvas.current\n    const ctx = surface?.getContext('2d')\n    if (!surface || !ctx) return\n\n    const ss = Math.min(2, window.devicePixelRatio || 1)\n    const size = () => {\n      surface.width = Math.round(window.innerWidth * ss)\n      surface.height = Math.round(window.innerHeight * ss)\n    }\n    size()\n\n    const flying = dots.map((dot) => ({ ...dot, life: 1 }))\n    const solids = [...document.querySelectorAll(SOLID)].map((element) =>\n      element.getBoundingClientRect(),\n    )\n    let raf = 0\n    let last = 0\n    let gone = false\n\n    const frame = (now: number) => {\n      if (gone) return\n      const dt = Math.min(64, last ? now - last : 16) / 1000\n      last = now\n      ctx.setTransform(ss, 0, 0, ss, 0, 0)\n      ctx.clearRect(0, 0, window.innerWidth, window.innerHeight)\n\n      let alive = 0\n      for (const dot of flying) {\n        dot.vy += BURST_GRAVITY * dt\n\n        const drag = Math.pow(BURST_DRAG, dt)\n        dot.vx *= drag\n        dot.vy *= drag\n        dot.x += dot.vx * dt\n        dot.y += dot.vy * dt\n\n        for (const box of solids) {\n          if (dot.x < box.left - dot.r || dot.x > box.right + dot.r) continue\n          if (dot.vy <= 0 || dot.y < box.top || dot.y > box.bottom) continue\n          dot.y = box.top\n          dot.vy = -dot.vy * BURST_BOUNCE\n          dot.vx *= BURST_FRICTION\n\n          if (Math.abs(dot.vy) < BURST_REST) dot.vy = 0\n          dot.vx += (dot.x < (box.left + box.right) / 2 ? -1 : 1) * 40\n        }\n        dot.life = Math.max(0, dot.life - dt * 0.26)\n        if (dot.life <= 0) continue\n        const onScreen =\n          dot.x > -40 && dot.x < window.innerWidth + 40 && dot.y < window.innerHeight + 40\n        if (!onScreen) continue\n        alive += 1\n        ctx.globalAlpha = dot.life\n        ctx.beginPath()\n        ctx.arc(dot.x, dot.y, dot.r * (0.4 + dot.life * 0.6), 0, Math.PI * 2)\n        ctx.fillStyle = dot.color\n        ctx.fill()\n      }\n      ctx.globalAlpha = 1\n\n      if (alive === 0) {\n        done.current()\n        return\n      }\n      raf = requestAnimationFrame(frame)\n    }\n\n    raf = requestAnimationFrame(frame)\n    window.addEventListener('resize', size)\n    return () => {\n      gone = true\n      cancelAnimationFrame(raf)\n      window.removeEventListener('resize', size)\n    }\n  }, [dots])\n\n  return (\n    <canvas\n      ref={canvas}\n      aria-hidden\n      data-slot=\"halftone-burst\"\n      className=\"pointer-events-none fixed inset-0 z-50 block size-full\"\n    />\n  )\n}\n\nexport function HalftoneDots({\n  src,\n  backdrop,\n  gradient,\n  accent = '#2563eb',\n  displace = false,\n  onBurst,\n  className,\n  style,\n}: {\n  src: string\n  backdrop?: string\n  gradient?: [string, string]\n  accent?: string\n  displace?: boolean\n  onBurst?: (presses: readonly number[]) => void\n  className?: string\n  style?: CSSProperties\n}) {\n  const box = useRef<HTMLDivElement>(null)\n  const canvas = useRef<HTMLCanvasElement>(null)\n  const raf = useRef(0)\n  const lattice = useRef<{\n    cols: number\n    rows: number\n    x: Float32Array\n    y: Float32Array\n    w: Float32Array\n    maxR: number\n    width: number\n    height: number\n    now: Field\n    from: Field\n    to: Field\n    started: number\n    settled: boolean\n  } | null>(null)\n  const lens = useRef({ x: 0, y: 0, tx: 0, ty: 0, strength: 0, target: 0 })\n  const swells = useRef(new Float32Array(0))\n\n  const parting = useRef(displace)\n  parting.current = displace\n  const springs = useRef({\n    ox: new Float32Array(0),\n    oy: new Float32Array(0),\n    vx: new Float32Array(0),\n    vy: new Float32Array(0),\n  })\n  const ripples = useRef<Array<{ x: number; y: number; at: number }>>([])\n  const ink = useRef<{ text: string[]; key: Int32Array }>({ text: [], key: new Int32Array(0) })\n  const [burst, setBurst] = useState<BurstDot[] | null>(null)\n\n  const exploded = useRef(false)\n  const presses = useRef<number[]>([])\n\n  const gradientKey = gradient ? gradient.join('|') : ''\n\n  const announce = useRef(onBurst)\n  announce.current = onBurst\n  const told = useRef(false)\n  useEffect(() => {\n    if (!burst || told.current) return\n    told.current = true\n    announce.current?.([...presses.current])\n  }, [burst])\n\n  useEffect(() => {\n    const frame_ = box.current\n    const surface = canvas.current\n    if (!frame_ || !surface) return\n    const still = window.matchMedia('(prefers-reduced-motion: reduce)').matches\n    let gone = false\n    let running = false\n    let last = 0\n\n    let image: HTMLImageElement | null = null\n    let scene: HTMLImageElement | null = null\n    let crop = { sx: 0, sy: 0, sw: 1, sh: 1 }\n\n    const blank = (n: number): Field => ({\n      r: new Float32Array(n),\n      rgb: new Float32Array(n * 3),\n      alpha: new Float32Array(n),\n      reveal: new Float32Array(n),\n    })\n\n    const paperRgb = (): [number, number, number] => {\n      const probe = document.createElement('canvas')\n      probe.width = 1\n      probe.height = 1\n      const pctx = probe.getContext('2d', { willReadFrequently: true })\n      if (!pctx) return [128, 128, 128]\n      pctx.fillStyle = getComputedStyle(frame_).color\n      pctx.fillRect(0, 0, 1, 1)\n      const [r = 0, g = 0, b = 0] = pctx.getImageData(0, 0, 1, 1).data\n      return [r, g, b]\n    }\n\n    const sample = (ctx: CanvasRenderingContext2D, img: HTMLImageElement) => {\n      const boxW = frame_.clientWidth\n      const boxH = frame_.clientHeight\n      const width = boxW + SPILL * 2\n      const height = boxH + SPILL * 2\n      const cols = Math.max(1, Math.round(width / CELL))\n      const rows = Math.max(1, Math.round(height / CELL))\n      const cw = width / cols\n      const ch = height / rows\n      const maxR = Math.min(cw, ch) / 2\n      const n = cols * rows\n\n      const frameX = SPILL - MARK_INSET + MARK_CLEAR\n      const frameY = SPILL - MARK_INSET + MARK_CLEAR\n      const frameW = boxW + MARK_INSET * 2 - MARK_CLEAR * 2\n      const frameH = boxH + MARK_INSET * 2 - MARK_CLEAR * 2\n      const fadeX = SPILL - MARK_INSET\n      const fadeY = SPILL - MARK_INSET\n      const fadeW = boxW + MARK_INSET * 2\n      const fadeH = boxH + MARK_INSET * 2\n      const fadeReach = SPILL - MARK_INSET\n\n      const bw = cols * SUP\n      const bh = rows * SUP\n      const px = bw / width\n      const { sx, sy, sw, sh } = crop\n      const fit = Math.min((frameW * px) / sw, (frameH * px) / sh)\n      const dw = sw * fit\n      const dh = sh * fit\n      const dx0 = (frameX + frameW / 2) * px - dw / 2\n      const dy0 = (frameY + frameH) * px - dh\n      const buffer = () => {\n        const sampler = document.createElement('canvas')\n        sampler.width = bw\n        sampler.height = bh\n        return sampler.getContext('2d')\n      }\n      const sctx = buffer()\n      if (!sctx) return null\n      sctx.drawImage(img, sx, sy, sw, sh, dx0, dy0, dw, dh)\n      const data = sctx.getImageData(0, 0, bw, bh).data\n\n      let behind: Uint8ClampedArray | null = null\n      if (scene) {\n        const bctx = buffer()\n        if (bctx) {\n          bctx.beginPath()\n          bctx.rect(frameX * px, frameY * px, frameW * px, frameH * px)\n          bctx.clip()\n          bctx.drawImage(\n            scene,\n            dx0 - sx * fit,\n            dy0 - sy * fit,\n            scene.width * fit,\n            scene.height * fit,\n          )\n          behind = bctx.getImageData(0, 0, bw, bh).data\n        }\n      }\n\n      const paper = paperRgb()\n\n      const darkPaper = (0.299 * paper[0] + 0.587 * paper[1] + 0.114 * paper[2]) / 255 > 0.5\n      const ink = hexRgb(accent)\n      const top = gradient ? hexRgb(gradient[0]) : null\n      const bottom = gradient ? hexRgb(gradient[1]) : null\n      const rowTop = frameY / ch\n      const rowSpan = frameH / ch\n\n      const x = new Float32Array(n)\n      const y = new Float32Array(n)\n      const w = new Float32Array(n)\n      const field = blank(n)\n      const coverage = new Float32Array(n)\n      const lum = new Float32Array(n)\n      let darkest = 1\n      let lightest = 0\n\n      const latent = new Uint8Array(n)\n      let sceneDarkest = 1\n      let sceneLightest = 0\n      const cx = width / 2\n      const cy = height / 2\n      const reachOut = Math.hypot(frameW / 2, frameH / 2) || 1\n\n      for (let yy = 0; yy < rows; yy += 1) {\n        for (let xx = 0; xx < cols; xx += 1) {\n          const i = yy * cols + xx\n          x[i] = (xx + 0.5) * cw\n          y[i] = (yy + 0.5) * ch\n          w[i] = Math.min(1, Math.hypot(x[i]! - cx, y[i]! - cy) / reachOut)\n          let cr = 0\n          let cg = 0\n          let cb = 0\n          let ink = 0\n          const read = (pixels: Uint8ClampedArray) => {\n            for (let sy2 = 0; sy2 < SUP; sy2 += 1) {\n              for (let sx2 = 0; sx2 < SUP; sx2 += 1) {\n                const k = ((yy * SUP + sy2) * bw + (xx * SUP + sx2)) * 4\n                if (pixels[k + 3]! < 24) continue\n                const r0 = pixels[k]!\n                const g0 = pixels[k + 1]!\n                const b0 = pixels[k + 2]!\n\n                if ((0.299 * r0 + 0.587 * g0 + 0.114 * b0) / 255 > 0.92) continue\n                cr += r0\n                cg += g0\n                cb += b0\n                ink += 1\n              }\n            }\n          }\n          const color = () => {\n            if (top && bottom) {\n              const t = Math.min(1, Math.max(0, (yy - rowTop) / (rowSpan || 1)))\n              field.rgb[i * 3] = top[0] + (bottom[0] - top[0]) * t\n              field.rgb[i * 3 + 1] = top[1] + (bottom[1] - top[1]) * t\n              field.rgb[i * 3 + 2] = top[2] + (bottom[2] - top[2]) * t\n            } else {\n              field.rgb[i * 3] = cr / ink\n              field.rgb[i * 3 + 1] = cg / ink\n              field.rgb[i * 3 + 2] = cb / ink\n            }\n          }\n          read(data)\n          if (ink === 0 && behind) {\n            read(behind)\n            if (ink > 0) {\n              latent[i] = 1\n              coverage[i] = ink / (SUP * SUP)\n              lum[i] = (0.299 * cr + 0.587 * cg + 0.114 * cb) / ink / 255\n              if (lum[i]! < sceneDarkest) sceneDarkest = lum[i]!\n              if (lum[i]! > sceneLightest) sceneLightest = lum[i]!\n              field.alpha[i] = LATENT_ALPHA\n              field.reveal[i] = 1 - LATENT_ALPHA\n              color()\n              if (!top || !bottom) {\n\n                const peak = Math.max(\n                  field.rgb[i * 3]!,\n                  field.rgb[i * 3 + 1]!,\n                  field.rgb[i * 3 + 2]!,\n                )\n                if (peak > 0 && peak < LATENT_LIFT) {\n                  const lift = LATENT_LIFT / peak\n                  field.rgb[i * 3] = field.rgb[i * 3]! * lift\n                  field.rgb[i * 3 + 1] = field.rgb[i * 3 + 1]! * lift\n                  field.rgb[i * 3 + 2] = field.rgb[i * 3 + 2]! * lift\n                }\n              }\n              continue\n            }\n          }\n          if (ink === 0) {\n            const dx = Math.max(fadeX - x[i]!, 0, x[i]! - (fadeX + fadeW))\n            const dy = Math.max(fadeY - y[i]!, 0, y[i]! - (fadeY + fadeH))\n            const out = Math.hypot(dx, dy) / fadeReach\n            const fade = out >= 1 ? 0 : (1 - out) * (1 - out)\n            field.r[i] = fade > 0 ? maxR * PAPER_R : 0\n            field.alpha[i] = PAPER_ALPHA * fade\n            field.rgb[i * 3] = paper[0]\n            field.rgb[i * 3 + 1] = paper[1]\n            field.rgb[i * 3 + 2] = paper[2]\n            continue\n          }\n          coverage[i] = ink / (SUP * SUP)\n          lum[i] = (0.299 * cr + 0.587 * cg + 0.114 * cb) / ink / 255\n          if (lum[i]! < darkest) darkest = lum[i]!\n          if (lum[i]! > lightest) lightest = lum[i]!\n          field.alpha[i] = 1\n          color()\n        }\n      }\n\n      const range = lightest - darkest || 1\n      let inked = 0\n      const mean = [0, 0, 0]\n      for (let i = 0; i < n; i += 1) {\n        if (field.alpha[i] !== 1) continue\n        inked += 1\n        mean[0]! += field.rgb[i * 3]!\n        mean[1]! += field.rgb[i * 3 + 1]!\n        mean[2]! += field.rgb[i * 3 + 2]!\n      }\n      for (let k = 0; k < 3; k += 1) mean[k]! /= inked || 1\n      const spread = (t: number) => {\n        const c = t - 0.5\n        return Math.min(\n          1,\n          Math.max(0, 0.5 + Math.sign(c) * Math.pow(Math.abs(c) * 2, 1 / TONE_CONTRAST) * 0.5),\n        )\n      }\n      const sceneRange = sceneLightest - sceneDarkest || 1\n      for (let i = 0; i < n; i += 1) {\n        if (latent[i]) {\n\n          const tone = spread(\n            (darkPaper ? lum[i]! - sceneDarkest : sceneLightest - lum[i]!) / sceneRange,\n          )\n          field.r[i] = maxR * (LATENT_FLOOR + coverage[i]! * LATENT_COVERAGE + tone * LATENT_TONE)\n          continue\n        }\n        if (field.alpha[i] !== 1) continue\n        const tone = spread((lightest - lum[i]!) / range)\n        field.r[i] =\n          maxR * Math.min(0.92, DOT_FLOOR + coverage[i]! * DOT_COVERAGE + tone * DOT_TONE)\n        if (!top || !bottom) {\n          for (let k = 0; k < 3; k += 1) {\n            const v = mean[k]! + (field.rgb[i * 3 + k]! - mean[k]!) * COLOR_CONTRAST\n            field.rgb[i * 3 + k] = Math.min(255, Math.max(0, v))\n          }\n        }\n      }\n\n      const setMark = (col: number, row: number) => {\n        if (col < 0 || row < 0 || col >= cols || row >= rows) return\n        const i = row * cols + col\n        field.r[i] = maxR * MARK_R\n        field.alpha[i] = 1\n        field.rgb[i * 3] = ink[0]\n        field.rgb[i * 3 + 1] = ink[1]\n        field.rgb[i * 3 + 2] = ink[2]\n      }\n      const colA = Math.round(fadeX / cw)\n      const colB = Math.round((fadeX + fadeW) / cw) - 1\n      const rowA = Math.round(fadeY / ch)\n      const rowB = Math.round((fadeY + fadeH) / ch) - 1\n      for (const [col, row, dc, dr] of [\n        [colA, rowA, 1, 1],\n        [colB, rowA, -1, 1],\n        [colA, rowB, 1, -1],\n        [colB, rowB, -1, -1],\n      ] as const) {\n        setMark(col + dc, row)\n        setMark(col, row + dr)\n        setMark(col + dc, row + dr)\n      }\n\n      return { cols, rows, x, y, w, maxR, width, height, field }\n    }\n\n    const render = (animate: boolean) => {\n      const ctx = surface.getContext('2d')\n      if (!image || !ctx || gone) return\n      const laid = sample(ctx, image)\n      if (!laid) return\n      const { cols, rows, x, y, w, maxR, width, height, field } = laid\n      const n = cols * rows\n\n      const ss = Math.min(3, (window.devicePixelRatio || 1) * 1.5)\n      surface.width = Math.round(width * ss)\n      surface.height = Math.round(height * ss)\n      ctx.setTransform(ss, 0, 0, ss, 0, 0)\n\n      const previous = lattice.current\n      const sameGrid = previous !== null && previous.cols === cols && previous.rows === rows\n\n      const carried = !animate && sameGrid && previous !== null && !previous.settled\n      const morph = (animate || carried) && !still && sameGrid\n      const origin = !morph || !previous ? field : carried ? previous.from : previous.now\n      const from = blank(n)\n      from.r.set(origin.r)\n      from.rgb.set(origin.rgb)\n      from.alpha.set(origin.alpha)\n      from.reveal.set(origin.reveal)\n      const now = blank(n)\n      now.r.set(from.r)\n      now.rgb.set(from.rgb)\n      now.alpha.set(from.alpha)\n      now.reveal.set(from.reveal)\n\n      lattice.current = {\n        cols,\n        rows,\n        x,\n        y,\n        w,\n        maxR,\n        width,\n        height,\n        now,\n        from,\n        to: field,\n        started: carried && previous ? previous.started : performance.now(),\n        settled: !morph,\n      }\n      if (!sameGrid) {\n        swells.current = new Float32Array(n)\n        springs.current = {\n          ox: new Float32Array(n),\n          oy: new Float32Array(n),\n          vx: new Float32Array(n),\n          vy: new Float32Array(n),\n        }\n      }\n      ensureLoop()\n    }\n\n    const reach = (angle: number, now: number) =>\n      LENS_RADIUS *\n      (1 +\n        LENS_WOBBLE *\n          (0.5 * Math.sin(3 * angle + now * LENS_DRIFT) +\n            0.32 * Math.sin(5 * angle - now * LENS_DRIFT * 1.7) +\n            0.18 * Math.sin(2 * angle + now * LENS_DRIFT * 0.6)))\n    let pushX = 0\n    let pushY = 0\n    const ask = (x: number, y: number, now: number) => {\n      pushX = 0\n      pushY = 0\n      const l = lens.current\n      if (l.strength <= 0.001) return 0\n      const dx = x - l.x\n      const dy = y - l.y\n      const dist = Math.hypot(dx, dy)\n      if (dist >= LENS_RADIUS * (1 + LENS_WOBBLE)) return 0\n      const d = dist / reach(Math.atan2(dy, dx), now)\n      if (d >= 1) return 0\n      const falloff = (1 - d * d) * (1 - d * d)\n\n      if (parting.current) {\n        const shove = (l.strength * LENS_PUSH * 6.75 * d * (1 - d) * (1 - d)) / (dist || 1)\n        pushX = dx * shove\n        pushY = dy * shove\n      }\n      return l.strength * falloff\n    }\n\n    const frame = (now: number) => {\n      const ctx = surface.getContext('2d')\n      const lat = lattice.current\n      if (!ctx || !lat || gone) return\n      if (exploded.current) {\n        ctx.clearRect(0, 0, lat.width, lat.height)\n        running = false\n        last = 0\n        return\n      }\n      const dt = last ? Math.min(64, now - last) : 16\n      last = now\n\n      const l = lens.current\n      const follow = 1 - Math.exp(-dt / LENS_FOLLOW)\n      l.x += (l.tx - l.x) * follow\n      l.y += (l.ty - l.y) * follow\n      const fade = 1 - Math.exp(-dt / (l.target > l.strength ? LENS_FADE_IN : LENS_FADE_OUT))\n      l.strength += (l.target - l.strength) * fade\n      if (Math.abs(l.target - l.strength) < 0.002) l.strength = l.target\n      const up = 1 - Math.exp(-dt / LENS_ATTACK)\n      const down = 1 - Math.exp(-dt / LENS_RELEASE)\n      const held = swells.current\n      const { ox, oy, vx, vy } = springs.current\n\n      const pieces = Math.max(1, Math.ceil(dt / SPRING_STEP))\n      const step = dt / pieces / 1000\n      let residue = 0\n\n      const rings = ripples.current\n      for (let k = rings.length - 1; k >= 0; k -= 1) {\n        if (now - rings[k]!.at > RIPPLE_LIFE * 2.5) rings.splice(k, 1)\n      }\n      const ringR = rings.map((ring) => (now - ring.at) * RIPPLE_SPEED)\n      const ringAmp = rings.map((ring) => Math.exp(-(now - ring.at) / RIPPLE_LIFE))\n\n      const el = now - lat.started\n      let settled = true\n      const { x, y, w, maxR, now: cur, from, to } = lat\n\n      ctx.clearRect(0, 0, lat.width, lat.height)\n\n      if (ink.current.key.length !== x.length) {\n        ink.current = { text: new Array<string>(x.length).fill(''), key: new Int32Array(x.length) }\n      }\n      const inkText = ink.current.text\n      const inkKey = ink.current.key\n\n      for (let i = 0; i < x.length; i += 1) {\n        if (!lat.settled) {\n          const t = Math.min(1, Math.max(0, (el - w[i]! * MORPH_STAGGER) / MORPH))\n          if (t < 1) settled = false\n          const e = easeInOut(t)\n\n          const dip = 1 - MORPH_DIP * Math.sin(Math.PI * t)\n          cur.r[i] = (from.r[i]! + (to.r[i]! - from.r[i]!) * e) * dip\n          cur.alpha[i] = from.alpha[i]! + (to.alpha[i]! - from.alpha[i]!) * e\n          cur.reveal[i] = from.reveal[i]! + (to.reveal[i]! - from.reveal[i]!) * e\n          cur.rgb[i * 3] = from.rgb[i * 3]! + (to.rgb[i * 3]! - from.rgb[i * 3]!) * e\n          cur.rgb[i * 3 + 1] =\n            from.rgb[i * 3 + 1]! + (to.rgb[i * 3 + 1]! - from.rgb[i * 3 + 1]!) * e\n          cur.rgb[i * 3 + 2] =\n            from.rgb[i * 3 + 2]! + (to.rgb[i * 3 + 2]! - from.rgb[i * 3 + 2]!) * e\n        }\n        const want = ask(x[i]!, y[i]!, now)\n\n        let ring = 0\n        for (let k = 0; k < rings.length; k += 1) {\n          const dx = x[i]! - rings[k]!.x\n          const dy = y[i]! - rings[k]!.y\n          const dist = Math.hypot(dx, dy)\n          const u = (dist - ringR[k]!) / RIPPLE_WIDTH\n          if (u < -2.5 || u > 2.5) continue\n          const bell = Math.exp(-u * u) * ringAmp[k]!\n          if (bell > ring) ring = bell\n          const shove = (RIPPLE_PUSH * bell) / (dist || 1)\n          pushX += dx * shove\n          pushY += dy * shove\n        }\n        const have = held[i]!\n        const next = have + (want - have) * (want > have ? up : down)\n        held[i] = next < 0.002 && want === 0 ? 0 : next\n        if (held[i]! > residue) residue = held[i]!\n        for (let p = 0; p < pieces; p += 1) {\n          vx[i] = vx[i]! + (SPRING_STIFFNESS * (pushX - ox[i]!) - SPRING_DAMPING * vx[i]!) * step\n          vy[i] = vy[i]! + (SPRING_STIFFNESS * (pushY - oy[i]!) - SPRING_DAMPING * vy[i]!) * step\n          ox[i] = ox[i]! + vx[i]! * step\n          oy[i] = oy[i]! + vy[i]! * step\n        }\n        const moving = Math.abs(ox[i]!) + Math.abs(oy[i]!) + Math.abs(vx[i]!) + Math.abs(vy[i]!)\n        if (moving < 0.02 && pushX === 0 && pushY === 0) {\n          ox[i] = oy[i] = vx[i] = vy[i] = 0\n        } else if (moving > residue) {\n          residue = moving\n        }\n        const lift = Math.max(held[i]!, ring)\n\n        const boost = cur.reveal[i]! > 0 ? LENS_BOOST * LATENT_BOOST : LENS_BOOST\n        const r = Math.min(maxR * FULL_GROW, cur.r[i]! * (1 + boost * lift))\n        const a = cur.alpha[i]! + cur.reveal[i]! * lift\n        if (r <= 0.06 || a <= 0.004) continue\n        ctx.globalAlpha = a\n\n        const rr = Math.round(cur.rgb[i * 3]!)\n        const gg = Math.round(cur.rgb[i * 3 + 1]!)\n        const bb = Math.round(cur.rgb[i * 3 + 2]!)\n        const key = (rr << 16) | (gg << 8) | bb\n        if (inkKey[i] !== key || inkText[i] === '') {\n          inkKey[i] = key\n          inkText[i] = `rgb(${String(rr)} ${String(gg)} ${String(bb)})`\n        }\n        ctx.fillStyle = inkText[i]!\n        ctx.beginPath()\n\n        const cx = x[i]! + ox[i]!\n        const cy = y[i]! + oy[i]!\n        const full = Math.min(1, Math.max(0, (r / maxR - FULL_FROM) / (1 - FULL_FROM)))\n        if (full > 0) {\n          const corner = r * (1 - (1 - FULL_CORNER) * full)\n          ctx.roundRect(cx - r, cy - r, r * 2, r * 2, corner)\n        } else {\n          ctx.arc(cx, cy, r, 0, Math.PI * 2)\n        }\n        ctx.fill()\n      }\n      ctx.globalAlpha = 1\n      if (settled) lat.settled = true\n\n      if (!lat.settled || l.strength > 0 || residue > 0 || rings.length > 0) {\n        raf.current = requestAnimationFrame(frame)\n      } else {\n        running = false\n        last = 0\n      }\n    }\n\n    const ensureLoop = () => {\n      if (running) return\n      running = true\n      cancelAnimationFrame(raf.current)\n      raf.current = requestAnimationFrame(frame)\n    }\n\n    const hoverable = window.matchMedia('(hover: hover) and (pointer: fine)').matches && !still\n    const aim = (event: PointerEvent) => {\n      const lat = lattice.current\n      if (!lat) return\n      const rect = surface.getBoundingClientRect()\n      const l = lens.current\n      const px = ((event.clientX - rect.left) / rect.width) * lat.width\n      const py = ((event.clientY - rect.top) / rect.height) * lat.height\n      if (l.target === 0) {\n        l.x = px\n        l.y = py\n      }\n      l.tx = px\n      l.ty = py\n      l.target = 1\n      ensureLoop()\n    }\n    const leave = () => {\n      lens.current.target = 0\n      ensureLoop()\n    }\n    const drop = (event: PointerEvent) => {\n      const lat = lattice.current\n      if (!lat) return\n\n      if (!still && !exploded.current) {\n        const at = performance.now()\n        const previous = presses.current.at(-1) ?? 0\n        presses.current = at - previous < BURST_GAP ? [...presses.current, at] : [at]\n        if (presses.current.length >= BURST_PRESSES) {\n          const dots = scatter(lat, surface.getBoundingClientRect(), event)\n\n          if (dots.length > 0) {\n            exploded.current = true\n            setBurst(dots)\n            ensureLoop()\n            return\n          }\n        }\n      }\n\n      if (still) return\n      const rect = surface.getBoundingClientRect()\n      ripples.current.push({\n        x: ((event.clientX - rect.left) / rect.width) * lat.width,\n        y: ((event.clientY - rect.top) / rect.height) * lat.height,\n        at: performance.now(),\n      })\n      ensureLoop()\n    }\n    frame_.addEventListener('pointerdown', drop)\n    if (hoverable) {\n      frame_.addEventListener('pointermove', aim)\n      frame_.addEventListener('pointerleave', leave)\n    }\n\n    const measure = (img: HTMLImageElement) => {\n      const scale = Math.min(1, 256 / Math.max(img.width, img.height))\n      const w = Math.max(1, Math.round(img.width * scale))\n      const h = Math.max(1, Math.round(img.height * scale))\n      const probe = document.createElement('canvas')\n      probe.width = w\n      probe.height = h\n      const pctx = probe.getContext('2d')\n      if (!pctx) return { sx: 0, sy: 0, sw: img.width, sh: img.height }\n      pctx.drawImage(img, 0, 0, w, h)\n      const { data } = pctx.getImageData(0, 0, w, h)\n      let left = w\n      let right = -1\n      let top = h\n      let bottom = -1\n      for (let yy = 0; yy < h; yy += 1) {\n        for (let xx = 0; xx < w; xx += 1) {\n          const i = (yy * w + xx) * 4\n          if (data[i + 3]! < 24) continue\n          if ((0.299 * data[i]! + 0.587 * data[i + 1]! + 0.114 * data[i + 2]!) / 255 > 0.92)\n            continue\n          if (xx < left) left = xx\n          if (xx > right) right = xx\n          if (yy < top) top = yy\n          if (yy > bottom) bottom = yy\n        }\n      }\n      if (right < left) return { sx: 0, sy: 0, sw: img.width, sh: img.height }\n      return {\n        sx: left / scale,\n        sy: top / scale,\n        sw: (right - left + 1) / scale,\n        sh: (bottom - top + 1) / scale,\n      }\n    }\n\n    let waiting = backdrop ? 2 : 1\n    const arrived = () => {\n      waiting -= 1\n      if (waiting > 0 || gone || !image) return\n      crop = measure(image)\n      render(true)\n    }\n    const img = new Image()\n    img.crossOrigin = 'anonymous'\n    img.onload = () => {\n      image = img\n      arrived()\n    }\n    img.src = src\n    if (backdrop) {\n      const behind = new Image()\n      behind.crossOrigin = 'anonymous'\n      behind.onload = () => {\n        scene = behind\n        arrived()\n      }\n      behind.onerror = arrived\n      behind.src = backdrop\n    }\n\n    const watcher = new ResizeObserver(() => render(false))\n    watcher.observe(frame_)\n\n    const themed = new MutationObserver(() => render(false))\n    themed.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] })\n    return () => {\n      gone = true\n      cancelAnimationFrame(raf.current)\n      watcher.disconnect()\n      themed.disconnect()\n      frame_.removeEventListener('pointermove', aim)\n      frame_.removeEventListener('pointerleave', leave)\n      frame_.removeEventListener('pointerdown', drop)\n    }\n\n  }, [src, backdrop, gradientKey, accent])\n\n  return (\n    <div ref={box} data-slot=\"halftone-dots\" className={cn('relative', className)} style={style}>\n\n      <canvas\n        ref={canvas}\n        aria-hidden\n        className=\"pointer-events-none absolute block\"\n        style={{\n          inset: -SPILL,\n          width: `calc(100% + ${String(SPILL * 2)}px)`,\n          height: `calc(100% + ${String(SPILL * 2)}px)`,\n        }}\n      />\n\n      {burst\n        ? createPortal(<BurstLayer dots={burst} onDone={() => setBurst(null)} />, document.body)\n        : null}\n    </div>\n  )\n}\n","type":"registry:ui","target":"components/ui/halftone-dots.tsx"}]}