{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ball-crystal",
  "type": "registry:component",
  "title": "Crystal Ball",
  "description": "A faceted low-poly sphere drawn on canvas. It turns slowly, tilts toward the pointer and moves its highlights with it.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "hover",
    "crystal",
    "sphere",
    "canvas",
    "3d",
    "facets",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/ball-crystal",
  "files": [
    {
      "path": "registry/hyperblur/ball-crystal.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/ball-crystal.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BallCrystalProps extends React.HTMLAttributes<HTMLDivElement> {\n  size?: number;\n  accent?: string;\n  speed?: number;\n  tilt?: number;\n  detail?: 0 | 1 | 2;\n}\n\ntype Vec = [number, number, number];\ntype Mesh = { verts: Vec[]; faces: [number, number, number][]; shade: number[] };\n\nconst css = `\n.hb-qr1 {\n  position: relative;\n  display: block;\n  width: 100%;\n  aspect-ratio: 1;\n  box-sizing: border-box;\n  margin: 0;\n  touch-action: pan-y;\n  background: radial-gradient(circle at 50% 46%, color-mix(in srgb, var(--hb-qr1-a) 26%, transparent) 0%, transparent 62%);\n}\n.hb-qr1::after {\n  content: \"\";\n  position: absolute;\n  left: 22%;\n  right: 22%;\n  bottom: 3%;\n  height: 7%;\n  border-radius: 50%;\n  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.55), transparent 70%);\n  pointer-events: none;\n}\n.hb-qr1-c {\n  position: relative;\n  z-index: 1;\n  display: block;\n  width: 100%;\n  height: 100%;\n}\n`;\n\nfunction hash(n: number) {\n  let h = (n ^ 0x9e3779b9) >>> 0;\n  h = Math.imul(h ^ (h >>> 16), 0x85ebca6b);\n  h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);\n  return ((h ^ (h >>> 16)) >>> 0) / 4294967296;\n}\n\nfunction normalize(v: Vec): Vec {\n  const l = Math.hypot(v[0], v[1], v[2]) || 1;\n  return [v[0] / l, v[1] / l, v[2] / l];\n}\n\nfunction parseColor(value: string): [number, number, number] {\n  const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(value.trim());\n  if (!m) return [47, 123, 255];\n  let hex = m[1];\n  if (hex.length === 3) hex = hex.split(\"\").map((c) => c + c).join(\"\");\n  const n = parseInt(hex, 16);\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nconst meshCache = new Map<number, Mesh>();\n\nfunction getMesh(detail: number): Mesh {\n  const cached = meshCache.get(detail);\n  if (cached) return cached;\n  const t = (1 + Math.sqrt(5)) / 2;\n  let verts: Vec[] = (\n    [\n      [-1, t, 0],\n      [1, t, 0],\n      [-1, -t, 0],\n      [1, -t, 0],\n      [0, -1, t],\n      [0, 1, t],\n      [0, -1, -t],\n      [0, 1, -t],\n      [t, 0, -1],\n      [t, 0, 1],\n      [-t, 0, -1],\n      [-t, 0, 1],\n    ] as Vec[]\n  ).map(normalize);\n  let faces: [number, number, number][] = [\n    [0, 11, 5],\n    [0, 5, 1],\n    [0, 1, 7],\n    [0, 7, 10],\n    [0, 10, 11],\n    [1, 5, 9],\n    [5, 11, 4],\n    [11, 10, 2],\n    [10, 7, 6],\n    [7, 1, 8],\n    [3, 9, 4],\n    [3, 4, 2],\n    [3, 2, 6],\n    [3, 6, 8],\n    [3, 8, 9],\n    [4, 9, 5],\n    [2, 4, 11],\n    [6, 2, 10],\n    [8, 6, 7],\n    [9, 8, 1],\n  ];\n  for (let step = 0; step < detail; step += 1) {\n    const base = verts;\n    const out = base.slice();\n    const cache = new Map<string, number>();\n    const mid = (a: number, b: number) => {\n      const key = a < b ? `${a}_${b}` : `${b}_${a}`;\n      const hit = cache.get(key);\n      if (hit !== undefined) return hit;\n      const va = base[a];\n      const vb = base[b];\n      out.push(normalize([(va[0] + vb[0]) / 2, (va[1] + vb[1]) / 2, (va[2] + vb[2]) / 2]));\n      cache.set(key, out.length - 1);\n      return out.length - 1;\n    };\n    const next: [number, number, number][] = [];\n    for (const [a, b, c] of faces) {\n      const ab = mid(a, b);\n      const bc = mid(b, c);\n      const ca = mid(c, a);\n      next.push([a, ab, ca], [b, bc, ab], [c, ca, bc], [ab, bc, ca]);\n    }\n    verts = out;\n    faces = next;\n  }\n  verts = verts.map((v, i) => {\n    const s = 0.92 + 0.14 * hash(i * 7 + 3);\n    return [v[0] * s, v[1] * s, v[2] * s] as Vec;\n  });\n  const shade = faces.map((_, i) => hash(i * 13 + 5) - 0.5);\n  const mesh = { verts, faces, shade };\n  meshCache.set(detail, mesh);\n  return mesh;\n}\n\ntype Item = { i: number; z: number; nx: number; ny: number; nz: number; cx: number; cy: number };\n\nexport function BallCrystal({\n  size = 220,\n  accent = \"#7fb3ff\",\n  speed = 1,\n  tilt = 1,\n  detail = 1,\n  className = \"\",\n  style,\n  ...rest\n}: BallCrystalProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const mesh = getMesh(detail);\n    const base = parseColor(accent);\n    const dark: [number, number, number] = [9, 13, 26];\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const rotated: Vec[] = mesh.verts.map(() => [0, 0, 0]);\n    const items: Item[] = [];\n    let width = 0;\n    let height = 0;\n    let dpr = 1;\n    let raf = 0;\n    let last = 0;\n    let time = 0;\n    let visible = true;\n    let hover = false;\n    let blend = 0;\n    const cur = { x: 0, y: 0 };\n    const pointer = { x: 0, y: 0 };\n\n    const draw = () => {\n      if (!width || !height) return;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, width, height);\n      const r = Math.min(width, height) * 0.4;\n      const cx = width / 2;\n      const cy = height / 2 - r * 0.03;\n      const yaw = time * 0.35 * speed + cur.x * 0.9 * tilt;\n      const pitch = -0.28 + cur.y * 0.6 * tilt;\n      const cyw = Math.cos(yaw);\n      const syw = Math.sin(yaw);\n      const cp = Math.cos(pitch);\n      const sp = Math.sin(pitch);\n      for (let i = 0; i < mesh.verts.length; i += 1) {\n        const v = mesh.verts[i];\n        const x1 = v[0] * cyw + v[2] * syw;\n        const z1 = -v[0] * syw + v[2] * cyw;\n        const y2 = v[1] * cp - z1 * sp;\n        const z2 = v[1] * sp + z1 * cp;\n        const o = rotated[i];\n        o[0] = x1;\n        o[1] = y2;\n        o[2] = z2;\n      }\n      items.length = 0;\n      for (let f = 0; f < mesh.faces.length; f += 1) {\n        const [a, b, c] = mesh.faces[f];\n        const va = rotated[a];\n        const vb = rotated[b];\n        const vc = rotated[c];\n        const mx = (va[0] + vb[0] + vc[0]) / 3;\n        const my = (va[1] + vb[1] + vc[1]) / 3;\n        const mz = (va[2] + vb[2] + vc[2]) / 3;\n        const ux = vb[0] - va[0];\n        const uy = vb[1] - va[1];\n        const uz = vb[2] - va[2];\n        const wx = vc[0] - va[0];\n        const wy = vc[1] - va[1];\n        const wz = vc[2] - va[2];\n        let nx = uy * wz - uz * wy;\n        let ny = uz * wx - ux * wz;\n        let nz = ux * wy - uy * wx;\n        const nl = Math.hypot(nx, ny, nz) || 1;\n        nx /= nl;\n        ny /= nl;\n        nz /= nl;\n        if (nx * mx + ny * my + nz * mz < 0) {\n          nx = -nx;\n          ny = -ny;\n          nz = -nz;\n        }\n        items.push({ i: f, z: mz, nx, ny, nz, cx: mx, cy: my });\n      }\n      items.sort((p, q) => p.z - q.z);\n      const lraw = normalize([-0.35 + cur.x * 0.85, -0.55 + cur.y * 0.85, 0.75]);\n      const hv = normalize([lraw[0], lraw[1], lraw[2] + 1]);\n      let flare: { x: number; y: number; s: number } | null = null;\n      for (const it of items) {\n        const [a, b, c] = mesh.faces[it.i];\n        const pa = rotated[a];\n        const pb = rotated[b];\n        const pc = rotated[c];\n        if (it.nz < 0) {\n          const k = 0.8;\n          const ox = -cur.x * r * 0.07;\n          const oy = -cur.y * r * 0.07;\n          ctx.beginPath();\n          ctx.moveTo(cx + pa[0] * r * k + ox, cy + pa[1] * r * k + oy);\n          ctx.lineTo(cx + pb[0] * r * k + ox, cy + pb[1] * r * k + oy);\n          ctx.lineTo(cx + pc[0] * r * k + ox, cy + pc[1] * r * k + oy);\n          ctx.closePath();\n          const d = Math.max(0, -(it.nx * lraw[0] + it.ny * lraw[1] + it.nz * lraw[2]));\n          const al = 0.1 + 0.2 * d + mesh.shade[it.i] * 0.06;\n          ctx.fillStyle = `rgba(${base[0]},${base[1]},${base[2]},${Math.max(0.04, al).toFixed(3)})`;\n          ctx.fill();\n          ctx.strokeStyle = \"rgba(255,255,255,0.05)\";\n          ctx.lineWidth = 0.6;\n          ctx.stroke();\n          continue;\n        }\n        const diff = Math.max(0, it.nx * lraw[0] + it.ny * lraw[1] + it.nz * lraw[2]);\n        const spec = Math.pow(Math.max(0, it.nx * hv[0] + it.ny * hv[1] + it.nz * hv[2]), 26);\n        const rim = Math.pow(1 - it.nz, 2.4);\n        const inten = Math.min(1, Math.max(0, 0.12 + 0.78 * diff + mesh.shade[it.i] * 0.24 + rim * 0.22));\n        const w = Math.min(1, spec * 0.9 + rim * 0.14);\n        const rr = (dark[0] + (base[0] - dark[0]) * inten) * (1 - w) + 255 * w;\n        const gg = (dark[1] + (base[1] - dark[1]) * inten) * (1 - w) + 255 * w;\n        const bb = (dark[2] + (base[2] - dark[2]) * inten) * (1 - w) + 255 * w;\n        ctx.beginPath();\n        ctx.moveTo(cx + pa[0] * r, cy + pa[1] * r);\n        ctx.lineTo(cx + pb[0] * r, cy + pb[1] * r);\n        ctx.lineTo(cx + pc[0] * r, cy + pc[1] * r);\n        ctx.closePath();\n        ctx.fillStyle = `rgba(${Math.round(rr)},${Math.round(gg)},${Math.round(bb)},0.9)`;\n        ctx.fill();\n        ctx.strokeStyle = `rgba(255,255,255,${(0.1 + spec * 0.4).toFixed(3)})`;\n        ctx.lineWidth = 0.75;\n        ctx.stroke();\n        if (spec > 0.45 && (!flare || spec > flare.s)) {\n          flare = { x: cx + it.cx * r, y: cy + it.cy * r, s: spec };\n        }\n      }\n      if (flare) {\n        const len = r * 0.16 * flare.s;\n        ctx.strokeStyle = `rgba(255,255,255,${Math.min(0.95, flare.s).toFixed(3)})`;\n        ctx.lineWidth = 1.2;\n        ctx.lineCap = \"round\";\n        ctx.beginPath();\n        ctx.moveTo(flare.x - len, flare.y);\n        ctx.lineTo(flare.x + len, flare.y);\n        ctx.moveTo(flare.x, flare.y - len);\n        ctx.lineTo(flare.x, flare.y + len);\n        ctx.stroke();\n      }\n    };\n\n    const frame = (ts: number) => {\n      raf = 0;\n      const dt = last ? Math.min(0.05, (ts - last) / 1000) : 0.016;\n      last = ts;\n      time += dt;\n      blend += ((hover ? 1 : 0) - blend) * Math.min(1, dt * 4);\n      const ix = Math.sin(time * 0.7) * 0.7;\n      const iy = Math.cos(time * 0.5) * 0.5;\n      const tx = ix * (1 - blend) + pointer.x * blend;\n      const ty = iy * (1 - blend) + pointer.y * blend;\n      const k = Math.min(1, dt * 5);\n      cur.x += (tx - cur.x) * k;\n      cur.y += (ty - cur.y) * k;\n      draw();\n      if (visible && !reduced) raf = requestAnimationFrame(frame);\n    };\n\n    const start = () => {\n      if (reduced || raf || !visible) return;\n      last = 0;\n      raf = requestAnimationFrame(frame);\n    };\n\n    const resize = () => {\n      const rect = root.getBoundingClientRect();\n      width = rect.width;\n      height = rect.height;\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      canvas.width = Math.max(1, Math.round(width * dpr));\n      canvas.height = Math.max(1, Math.round(height * dpr));\n      draw();\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const rect = root.getBoundingClientRect();\n      if (!rect.width || !rect.height) return;\n      pointer.x = Math.max(-1, Math.min(1, ((e.clientX - rect.left) / rect.width - 0.5) * 2));\n      pointer.y = Math.max(-1, Math.min(1, ((e.clientY - rect.top) / rect.height - 0.5) * 2));\n      hover = true;\n    };\n    const onLeave = () => {\n      hover = false;\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(root);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      if (visible) start();\n      else if (raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(root);\n    resize();\n    if (!reduced) {\n      root.addEventListener(\"pointermove\", onMove);\n      root.addEventListener(\"pointerdown\", onMove);\n      root.addEventListener(\"pointerleave\", onLeave);\n      root.addEventListener(\"pointercancel\", onLeave);\n      start();\n    }\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"pointercancel\", onLeave);\n    };\n  }, [accent, detail, speed, tilt]);\n\n  return (\n    <div\n      ref={rootRef}\n      role=\"img\"\n      aria-label=\"Rotating crystal sphere\"\n      className={`hb-qr1 ${className}`.trim()}\n      style={{ width: size, maxWidth: \"100%\", [\"--hb-qr1-a\" as string]: accent, ...style } as React.CSSProperties}\n      {...rest}\n    >\n      <style href=\"hb-ball-crystal\" precedence=\"default\">\n        {css}\n      </style>\n      <canvas ref={canvasRef} className=\"hb-qr1-c\" aria-hidden=\"true\" />\n    </div>\n  );\n}\n"
    }
  ]
}