{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "card-pixel-hover",
  "type": "registry:component",
  "title": "Pixel Hover",
  "description": "A canvas card whose picture breaks into a grid of pixels in a wave that starts where the pointer enters, then reforms on leave.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "components",
    "card",
    "pixel",
    "canvas",
    "dissolve",
    "wave",
    "hover"
  ],
  "docs": "https://hyperblur.dev/effects/card-pixel-hover",
  "files": [
    {
      "path": "registry/hyperblur/card-pixel-hover.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/card-pixel-hover.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CardPixelHoverProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"title\"> {\n  title?: string;\n  subtitle?: string;\n  tag?: string;\n  src?: string;\n  accent?: string;\n  cell?: number;\n  ratio?: number;\n  idle?: boolean;\n}\n\nconst css = `\n.hb-qk3 {\n  position: relative;\n  display: block;\n  width: 100%;\n  aspect-ratio: var(--hb-qk3-r);\n  overflow: hidden;\n  border-radius: 18px;\n  border: 1px solid #262626;\n  background: #0a0a0a;\n  color: #ededed;\n  font-family: inherit;\n  box-sizing: border-box;\n  cursor: pointer;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-qk3 *, .hb-qk3 *::before, .hb-qk3 *::after { box-sizing: border-box; }\n.hb-qk3:focus-visible { outline: 2px solid var(--hb-qk3-c); outline-offset: 3px; }\n.hb-qk3-cv {\n  position: absolute;\n  inset: 0;\n  display: block;\n  width: 100%;\n  height: 100%;\n}\n.hb-qk3-scrim {\n  position: absolute;\n  inset: 0;\n  pointer-events: none;\n  background: linear-gradient(180deg, rgba(5, 5, 8, 0) 40%, rgba(5, 5, 8, 0.86) 100%);\n}\n.hb-qk3-tag {\n  position: absolute;\n  left: 14px;\n  top: 14px;\n  padding: 5px 10px;\n  border-radius: 999px;\n  background: rgba(10, 10, 12, 0.62);\n  border: 1px solid rgba(255, 255, 255, 0.16);\n  font-size: 11px;\n  font-weight: 500;\n  line-height: 1;\n  letter-spacing: 0.02em;\n  color: #ededed;\n  pointer-events: none;\n}\n.hb-qk3-body {\n  position: absolute;\n  left: 0;\n  right: 0;\n  bottom: 0;\n  padding: 14px 16px 15px;\n  pointer-events: none;\n}\n.hb-qk3-title {\n  margin: 0;\n  font-size: 20px;\n  font-weight: 500;\n  line-height: 1.15;\n  letter-spacing: -0.01em;\n  color: #ffffff;\n}\n.hb-qk3-sub {\n  margin: 4px 0 0;\n  font-size: 12px;\n  line-height: 1.3;\n  color: #b8b8b8;\n}\n`;\n\nfunction hash(n: number) {\n  let h = Math.imul(n + 1, 2654435761) >>> 0;\n  h ^= h >>> 15;\n  h = Math.imul(h, 2246822519) >>> 0;\n  h ^= h >>> 13;\n  return (h >>> 0) / 4294967296;\n}\n\nconst points = [\n  [0.2, 0.3],\n  [0.82, 0.7],\n  [0.5, 0.18],\n  [0.14, 0.84],\n];\n\nexport function CardPixelHover({\n  title = \"Sunroom studio, Lisbon\",\n  subtitle = \"38 m2, south facing, from 1,450 / month\",\n  tag = \"Available May 1\",\n  src,\n  accent = \"#2f7bff\",\n  cell = 16,\n  ratio = 1.75,\n  idle = true,\n  className = \"\",\n  style,\n  onPointerEnter,\n  onPointerLeave,\n  onPointerCancel,\n  onFocus,\n  onBlur,\n  ...rest\n}: CardPixelHoverProps) {\n  const root = React.useRef<HTMLDivElement>(null);\n  const canvas = React.useRef<HTMLCanvasElement>(null);\n  const api = React.useRef<((target: number, x: number, y: number) => void) | null>(null);\n  const live = React.useRef({ real: false, cool: 0 });\n\n  React.useEffect(() => {\n    const el = root.current;\n    const cv = canvas.current;\n    const ctx = cv?.getContext(\"2d\");\n    if (!el || !cv || !ctx) return;\n    const s = live.current;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const full = document.createElement(\"canvas\");\n    const small = document.createElement(\"canvas\");\n    const fctx = full.getContext(\"2d\");\n    const sctx = small.getContext(\"2d\");\n    if (!fctx || !sctx) return;\n    let image: HTMLImageElement | null = null;\n    let w = 0;\n    let h = 0;\n    let dpr = 1;\n    let cols = 1;\n    let rows = 1;\n    let cw = 1;\n    let ch = 1;\n    let v = new Float32Array(1);\n    let delay = new Float32Array(1);\n    let target = 0;\n    let t0 = 0;\n    let raf = 0;\n    let last = 0;\n    let dirty = true;\n\n    const drawScene = () => {\n      fctx.setTransform(1, 0, 0, 1, 0, 0);\n      fctx.clearRect(0, 0, full.width, full.height);\n      fctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (image && image.complete && image.naturalWidth) {\n        const k = Math.max(w / image.naturalWidth, h / image.naturalHeight);\n        const iw = image.naturalWidth * k;\n        const ih = image.naturalHeight * k;\n        fctx.drawImage(image, (w - iw) / 2, (h - ih) / 2, iw, ih);\n      } else {\n        fctx.fillStyle = accent;\n        fctx.fillRect(0, 0, w, h);\n        const sky = fctx.createLinearGradient(0, 0, 0, h);\n        sky.addColorStop(0, \"rgba(255,255,255,0.28)\");\n        sky.addColorStop(0.55, \"rgba(10,10,24,0.35)\");\n        sky.addColorStop(1, \"rgba(4,4,8,0.82)\");\n        fctx.fillStyle = sky;\n        fctx.fillRect(0, 0, w, h);\n        const sun = fctx.createRadialGradient(w * 0.74, h * 0.32, 0, w * 0.74, h * 0.32, h * 0.34);\n        sun.addColorStop(0, \"rgba(255,255,255,1)\");\n        sun.addColorStop(0.4, \"rgba(255,244,214,0.85)\");\n        sun.addColorStop(1, \"rgba(255,255,255,0)\");\n        fctx.fillStyle = sun;\n        fctx.fillRect(0, 0, w, h);\n        const ridges: [number[][], string][] = [\n          [[[0, 0.7], [0.14, 0.52], [0.3, 0.64], [0.48, 0.4], [0.64, 0.6], [0.82, 0.46], [1, 0.62], [1, 1], [0, 1]], \"rgba(6,6,14,0.5)\"],\n          [[[0, 0.84], [0.2, 0.72], [0.4, 0.82], [0.6, 0.66], [0.82, 0.8], [1, 0.72], [1, 1], [0, 1]], \"rgba(3,3,8,0.78)\"],\n        ];\n        for (const [poly, color] of ridges) {\n          fctx.beginPath();\n          poly.forEach(([px, py], i) => {\n            if (i === 0) fctx.moveTo(px * w, py * h);\n            else fctx.lineTo(px * w, py * h);\n          });\n          fctx.closePath();\n          fctx.fillStyle = color;\n          fctx.fill();\n        }\n      }\n      small.width = cols;\n      small.height = rows;\n      sctx.imageSmoothingEnabled = true;\n      sctx.imageSmoothingQuality = \"high\";\n      sctx.clearRect(0, 0, cols, rows);\n      sctx.drawImage(full, 0, 0, cols, rows);\n      dirty = true;\n    };\n\n    const spread = (now: number, ox: number, oy: number) => {\n      const diag = Math.hypot(w, h) || 1;\n      for (let r = 0; r < rows; r += 1) {\n        for (let c = 0; c < cols; c += 1) {\n          const i = r * cols + c;\n          const d = Math.hypot((c + 0.5) * cw - ox * w, (r + 0.5) * ch - oy * h) / diag;\n          delay[i] = d * 0.7 + hash(i) * 0.1;\n        }\n      }\n      t0 = now;\n    };\n\n    const trigger = (next: number, x: number, y: number) => {\n      const now = performance.now() / 1000;\n      target = next;\n      spread(now, x, y);\n      dirty = true;\n    };\n    api.current = trigger;\n\n    const render = (now: number) => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, w, h);\n      ctx.imageSmoothingEnabled = true;\n      ctx.drawImage(full, 0, 0, w, h);\n      ctx.imageSmoothingEnabled = false;\n      for (let r = 0; r < rows; r += 1) {\n        for (let c = 0; c < cols; c += 1) {\n          const k = v[r * cols + c];\n          if (k <= 0.001) continue;\n          const x = c * cw;\n          const y = r * ch;\n          ctx.globalAlpha = Math.min(1, k * 1.8);\n          ctx.fillStyle = \"#0a0a0a\";\n          ctx.fillRect(x - 0.5, y - 0.5, cw + 1, ch + 1);\n          const e = k * k * (3 - 2 * k);\n          const pulse = k > 0.98 && target === 1 ? 0.06 * Math.sin(now * 3 + (c + r) * 0.6) : 0;\n          const f = Math.min(0.96, 0.12 + 0.76 * e + pulse);\n          const bw = cw * f;\n          const bh = ch * f;\n          ctx.globalAlpha = Math.min(1, k * 1.5);\n          ctx.drawImage(small, c, r, 1, 1, x + (cw - bw) / 2, y + (ch - bh) / 2, bw, bh);\n        }\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const layout = () => {\n      w = el.clientWidth;\n      h = el.clientHeight;\n      if (!w || !h) return;\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      cv.width = Math.round(w * dpr);\n      cv.height = Math.round(h * dpr);\n      full.width = cv.width;\n      full.height = cv.height;\n      cols = Math.max(2, Math.round(w / Math.max(6, cell)));\n      rows = Math.max(2, Math.round(h / Math.max(6, cell)));\n      cw = w / cols;\n      ch = h / rows;\n      v = new Float32Array(cols * rows).fill(target);\n      delay = new Float32Array(cols * rows);\n      spread(performance.now() / 1000, 0.5, 0.5);\n      drawScene();\n      render(performance.now() / 1000);\n    };\n\n    if (src) {\n      image = new Image();\n      image.onload = () => {\n        drawScene();\n        render(performance.now() / 1000);\n      };\n      image.src = src;\n    }\n\n    layout();\n    const ro = new ResizeObserver(layout);\n    ro.observe(el);\n\n    if (reduce) {\n      return () => {\n        ro.disconnect();\n        if (image) image.onload = null;\n        api.current = null;\n      };\n    }\n\n    const frame = (ms: number) => {\n      const now = ms / 1000;\n      const dt = Math.min(0.05, last ? now - last : 0.016);\n      last = now;\n      if (idle && !s.real && now > s.cool) {\n        const phase = Math.floor(now / 2.6);\n        const want = phase % 2 === 1 ? 1 : 0;\n        if (want !== target) {\n          const p = points[(Math.floor(phase / 2) + (want ? 0 : 2)) % points.length];\n          trigger(want, p[0], p[1]);\n        }\n      }\n      let moving = false;\n      const clock = performance.now() / 1000;\n      for (let i = 0; i < v.length; i += 1) {\n        if (clock - t0 >= delay[i] && v[i] !== target) {\n          const step = dt / 0.42;\n          v[i] = target > v[i] ? Math.min(target, v[i] + step) : Math.max(target, v[i] - step);\n          moving = true;\n        }\n      }\n      if (moving || dirty || target === 1) {\n        render(now);\n        dirty = moving;\n      }\n      raf = requestAnimationFrame(frame);\n    };\n\n    const io = new IntersectionObserver(([entry]) => {\n      cancelAnimationFrame(raf);\n      last = 0;\n      if (entry.isIntersecting) raf = requestAnimationFrame(frame);\n    });\n    io.observe(el);\n\n    return () => {\n      io.disconnect();\n      ro.disconnect();\n      cancelAnimationFrame(raf);\n      if (image) image.onload = null;\n      api.current = null;\n    };\n  }, [src, accent, cell, idle]);\n\n  const enter = (e: React.PointerEvent<HTMLDivElement>) => {\n    onPointerEnter?.(e);\n    const r = e.currentTarget.getBoundingClientRect();\n    live.current.real = true;\n    api.current?.(1, r.width ? (e.clientX - r.left) / r.width : 0.5, r.height ? (e.clientY - r.top) / r.height : 0.5);\n  };\n\n  const leaveWith = (e: React.PointerEvent<HTMLDivElement>) => {\n    const r = e.currentTarget.getBoundingClientRect();\n    live.current.real = false;\n    live.current.cool = performance.now() / 1000 + 2.4;\n    api.current?.(0, r.width ? (e.clientX - r.left) / r.width : 0.5, r.height ? (e.clientY - r.top) / r.height : 0.5);\n  };\n\n  return (\n    <div\n      ref={root}\n      tabIndex={0}\n      className={`hb-qk3 ${className}`}\n      style={{ \"--hb-qk3-r\": ratio, \"--hb-qk3-c\": accent, ...style } as React.CSSProperties}\n      onPointerEnter={enter}\n      onPointerLeave={(e) => {\n        onPointerLeave?.(e);\n        leaveWith(e);\n      }}\n      onPointerCancel={(e) => {\n        onPointerCancel?.(e);\n        leaveWith(e);\n      }}\n      onFocus={(e) => {\n        onFocus?.(e);\n        live.current.real = true;\n        api.current?.(1, 0.5, 0.5);\n      }}\n      onBlur={(e) => {\n        onBlur?.(e);\n        live.current.real = false;\n        live.current.cool = performance.now() / 1000 + 2.4;\n        api.current?.(0, 0.5, 0.5);\n      }}\n      {...rest}\n    >\n      <style href=\"hb-card-pixel-hover\" precedence=\"default\">{css}</style>\n      <canvas ref={canvas} className=\"hb-qk3-cv\" aria-hidden=\"true\" />\n      <div className=\"hb-qk3-scrim\" aria-hidden=\"true\" />\n      {tag ? <span className=\"hb-qk3-tag\">{tag}</span> : null}\n      <div className=\"hb-qk3-body\">\n        <h3 className=\"hb-qk3-title\">{title}</h3>\n        {subtitle ? <p className=\"hb-qk3-sub\">{subtitle}</p> : null}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}