{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glowing-plankton-in-deep-water-particle-background","type":"registry:block","title":"Glowing Plankton in Deep Water","description":"","dependencies":[],"files":[{"path":"components/we-inc/glowing-plankton-in-deep-water-particle-background.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Glowing Plankton in Deep Water: a canvas 2D particle background behind a small hero.\n// Sprites are drawn once to small offscreen canvases, motion is scaled by frame\n// time, the loop stops off screen, and reduced motion gets one settled frame.\n// Values worth changing:\nconst PLANKTON_PER_MPX = 320; // glowing plankton per million pixels\nconst GLOW = \"110,245,220\"; // bioluminescent colour, as r,g,b\nconst JELLY = \"96,160,255\"; // the few large, dim drifting glows\nconst FLASH_CHANCE = 0.06; // chance per second that a speck flashes\nconst CURRENT = 1; // water movement speed\nconst WARMUP = 90; // frames simulated before the first paint\n\ntype Ptr = { x: number; y: number; on: boolean };\ntype Scene = { update: (dt: number, t: number) => void; draw: (c: CanvasRenderingContext2D, t: number) => void };\n\nfunction glow(size: number, rgb: string, stops: number[][]): HTMLCanvasElement {\n  const c = document.createElement(\"canvas\");\n  c.width = c.height = size;\n  const g = c.getContext(\"2d\")!;\n  const r = size / 2;\n  const grd = g.createRadialGradient(r, r, 0, r, r, r);\n  for (const [o, a] of stops) grd.addColorStop(o, `rgba(${rgb},${a})`);\n  g.fillStyle = grd;\n  g.fillRect(0, 0, size, size);\n  return c;\n}\n\nfunction mount(canvas: HTMLCanvasElement, root: HTMLElement): () => void {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  const still = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const ptr: Ptr = { x: -1e4, y: -1e4, on: false };\n  let w = 0, h = 0, t = 0, last = 0, raf = 0, seen = true;\n  let scene: Scene | null = null;\n  const fit = () => {\n    const r = canvas.getBoundingClientRect();\n    if (scene && Math.abs(r.width - w) < 1 && Math.abs(r.height - h) < 1) return;\n    const dpr = Math.min(window.devicePixelRatio || 1, 1.5);\n    w = Math.max(1, r.width);\n    h = Math.max(1, r.height);\n    canvas.width = Math.round(w * dpr);\n    canvas.height = Math.round(h * dpr);\n    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    scene = createScene(w, h, ptr);\n    for (let i = 0; i < WARMUP; i++) { t += 1 / 30; scene.update(1 / 30, t); }\n    scene.draw(ctx, t);\n  };\n  const frame = (now: number) => {\n    const dt = Math.min(0.05, Math.max(0, (now - last) / 1000));\n    last = now;\n    t += dt;\n    if (scene) { scene.update(dt, t); scene.draw(ctx, t); }\n    raf = requestAnimationFrame(frame);\n  };\n  const run = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    if (!still && seen && !document.hidden) { last = performance.now(); raf = requestAnimationFrame(frame); }\n  };\n  const ro = new ResizeObserver(fit);\n  ro.observe(canvas);\n  const io = new IntersectionObserver((es) => { seen = es[es.length - 1].isIntersecting; run(); });\n  io.observe(canvas);\n  document.addEventListener(\"visibilitychange\", run);\n  fit();\n  run();\n  return () => {\n    cancelAnimationFrame(raf);\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", run);\n  };\n}\n\nfunction createScene(w: number, h: number, _ptr: Ptr): Scene {\n  const area = (w * h) / 1e6;\n  const speck = glow(32, GLOW, [[0, 1], [0.12, 0.85], [0.35, 0.2], [1, 0]]);\n  const jelly = glow(128, JELLY, [[0, 0.22], [0.5, 0.08], [1, 0]]);\n  const snow = glow(8, \"200,220,225\", [[0, 0.9], [1, 0]]);\n  // Faint light rays from the surface, drawn once.\n  const rays = document.createElement(\"canvas\");\n  rays.width = Math.ceil(w / 2);\n  rays.height = Math.ceil(h / 2);\n  const r = rays.getContext(\"2d\")!;\n  r.scale(0.5, 0.5);\n  for (let i = 0; i < 7; i++) {\n    const x = w * (0.1 + Math.random() * 0.8), wd = 20 + Math.random() * 70;\n    const g = r.createLinearGradient(0, 0, 0, h * 0.8);\n    g.addColorStop(0, \"rgba(140,220,230,0.07)\");\n    g.addColorStop(1, \"rgba(140,220,230,0)\");\n    r.fillStyle = g;\n    r.beginPath();\n    r.moveTo(x, 0); r.lineTo(x + wd, 0); r.lineTo(x + wd * 2.4 - w * 0.12, h * 0.8); r.lineTo(x - w * 0.12, h * 0.8);\n    r.fill();\n  }\n  const ps = Array.from({ length: Math.min(600, Math.round(area * PLANKTON_PER_MPX)) }, () => ({\n    x: Math.random() * w, y: Math.random() * h, z: Math.random(), s: Math.random() * 99, f: 0,\n  }));\n  const big = Array.from({ length: Math.max(3, Math.round(area * 8)) }, () => ({ x: Math.random() * w, y: Math.random() * h, z: Math.random(), s: Math.random() * 99 }));\n  const fall = Array.from({ length: Math.min(200, Math.round(area * 120)) }, () => ({ x: Math.random() * w, y: Math.random() * h, z: Math.random() }));\n  return {\n    update(dt, t) {\n      for (const p of ps) {\n        const k = (0.3 + p.z) * CURRENT;\n        p.x += (Math.sin(t * 0.2 + p.y * 0.004 + p.s) * 9 + 3) * k * dt;\n        p.y += Math.cos(t * 0.15 + p.x * 0.003 + p.s) * 6 * k * dt;\n        p.f = Math.max(0, p.f - dt * 0.9);\n        if (Math.random() < FLASH_CHANCE * dt) p.f = 1;\n        if (p.x > w + 20) p.x = -20; else if (p.x < -20) p.x = w + 20;\n        if (p.y > h + 20) p.y = -20; else if (p.y < -20) p.y = h + 20;\n      }\n      for (const b of big) {\n        b.x += (4 + Math.sin(t * 0.1 + b.s) * 3) * CURRENT * dt;\n        b.y += Math.sin(t * 0.25 + b.s) * 5 * dt;\n        if (b.x > w + 150) b.x = -150;\n      }\n      for (const m of fall) {\n        m.y += (6 + m.z * 10) * dt;\n        m.x += Math.sin(t * 0.4 + m.y * 0.02) * 4 * dt;\n        if (m.y > h + 5) { m.y = -5; m.x = Math.random() * w; }\n      }\n    },\n    draw(c, t) {\n      c.clearRect(0, 0, w, h);\n      c.globalAlpha = 0.75 + 0.25 * Math.sin(t * 0.3);\n      c.drawImage(rays, Math.sin(t * 0.12) * 18, 0, w, h);\n      for (const m of fall) {\n        c.globalAlpha = 0.15 + m.z * 0.3;\n        const s = 1.5 + m.z * 2;\n        c.drawImage(snow, m.x, m.y, s, s);\n      }\n      c.globalCompositeOperation = \"lighter\";\n      for (const b of big) {\n        const s = 120 + b.z * 200 + Math.sin(t * 0.6 + b.s) * 20;\n        c.globalAlpha = 0.6 + 0.4 * Math.sin(t * 0.4 + b.s);\n        c.drawImage(jelly, b.x - s / 2, b.y - s / 2, s, s);\n      }\n      for (const p of ps) {\n        const pulse = 0.5 + 0.5 * Math.sin(t * 0.8 + p.s);\n        const s = (3 + p.z * 7) * (1 + p.f * 1.8);\n        c.globalAlpha = Math.min(1, (0.1 + 0.25 * pulse) * (0.4 + p.z) + p.f * 0.9);\n        c.drawImage(speck, p.x - s / 2, p.y - s / 2, s, s);\n      }\n      c.globalAlpha = 1;\n      c.globalCompositeOperation = \"source-over\";\n    },\n  };\n}\n\nexport default function GlowingPlanktonDeepWater() {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const rootRef = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    if (!canvasRef.current || !rootRef.current) return;\n    return mount(canvasRef.current, rootRef.current);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"plk-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.plk-bg{background:radial-gradient(90% 60% at 50% -10%, #0e4450 0%, rgba(14,68,80,0) 70%), linear-gradient(180deg, #052029 0%, #03141a 55%, #020a0d 100%)}`}</style>\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      <div className=\"mx-auto w-full max-w-6xl\">\n        <div className=\"max-w-xl\">\n          <p className=\"text-sm font-semibold uppercase tracking-[0.14em] text-[#8be6d6]\">Tidewater Night Dives</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#e6fbf7] sm:text-5xl\">Turn off your torch and watch the water glow.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#a9d3cf]\">Guided night dives in small groups, with gear fitted before you board and an instructor beside you the whole way down.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#7be0cf] px-6 text-sm font-semibold text-[#03181c] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#e6fbf7] focus-visible:ring-offset-2 focus-visible:ring-offset-[#04161c]\"\n          >\n            Pick a dive date\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
