{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-dithering-team",
  "title": "Image Dithering Team",
  "description": "A team grid that dithers every headshot into one consistent look, with the original color on hover.",
  "dependencies": [
    "@paper-design/shaders-react"
  ],
  "files": [
    {
      "path": "registry/shaders/image-dithering-team/image-dithering-team.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { ImageDithering } from \"@paper-design/shaders-react\"\n\nimport { cn } from \"@/lib/utils\"\n\ntype TeamMember = {\n  name: string\n  role: string\n  /** Any image URL. Remote images must be served with CORS headers. */\n  image: string\n  href?: string\n}\n\nconst DEFAULT_MEMBERS: TeamMember[] = [\n  {\n    name: \"Maya Lindqvist\",\n    role: \"Co-founder, CEO\",\n    image: portrait(\"1534528741775-53994a69daeb\"),\n  },\n  {\n    name: \"Daniel Okafor\",\n    role: \"Co-founder, CTO\",\n    image: portrait(\"1507003211169-0a1dd7228f2d\"),\n  },\n  {\n    name: \"Priya Raman\",\n    role: \"Head of Design\",\n    image: portrait(\"1494790108377-be9c29b29330\"),\n  },\n  {\n    name: \"Tomás Herrera\",\n    role: \"Staff Engineer\",\n    image: portrait(\"1500648767791-00dcc994a43e\"),\n  },\n  {\n    name: \"Hana Sato\",\n    role: \"Brand Designer\",\n    image: portrait(\"1438761681033-6461ffad8d80\"),\n  },\n  {\n    name: \"Lukas Brandt\",\n    role: \"Product Engineer\",\n    image: portrait(\"1472099645785-5658abf4ff4e\"),\n  },\n  {\n    name: \"Amara Nwosu\",\n    role: \"Product Manager\",\n    image: portrait(\"1544005313-94ddf0286df2\"),\n  },\n  {\n    name: \"Ethan Cole\",\n    role: \"Infrastructure\",\n    image: portrait(\"1506794778202-cad84cf45f1d\"),\n  },\n]\n\n// Browsers cap live WebGL contexts per page (around 16), so a big team can't\n// keep a canvas per photo. Instead each photo renders once, is saved as an\n// image, and frees its context. Only a few render at the same time.\nconst MAX_RENDERING = 4\n\nconst queue: Array<() => void> = []\nlet rendering = 0\n\nfunction acquireSlot() {\n  if (rendering < MAX_RENDERING) {\n    rendering++\n    return Promise.resolve()\n  }\n  return new Promise<void>((resolve) => queue.push(resolve))\n}\n\nfunction releaseSlot() {\n  const next = queue.shift()\n  if (next) next()\n  else rendering--\n}\n\ntype DitherParams = {\n  colorBack: string\n  colorFront: string\n  pixelSize: number\n}\n\ntype ImageDitheringTeamProps = React.ComponentProps<\"section\"> &\n  Partial<DitherParams> & {\n    eyebrow?: string\n    title?: string\n    description?: string\n    members?: TeamMember[]\n    /** Background and text color of the section. */\n    colorPaper?: string\n    /** Text color of the section. */\n    colorInk?: string\n  }\n\nfunction ImageDitheringTeam({\n  eyebrow = \"The team\",\n  title = \"Small team, big screens\",\n  description = \"Every photo goes through the same dither, so a dozen different cameras still look like one set. Hover a face to see the original.\",\n  members = DEFAULT_MEMBERS,\n  colorPaper = \"#f2f1e8\",\n  colorInk = \"#1c1b18\",\n  colorBack = \"#1c1b18\",\n  colorFront = \"#f2f1e8\",\n  pixelSize = 2,\n  className,\n  style,\n  ...props\n}: ImageDitheringTeamProps) {\n  return (\n    <section\n      data-slot=\"image-dithering-team\"\n      className={cn(\n        \"relative isolate flex w-full flex-col gap-12 px-6 py-20 md:px-12 lg:px-16\",\n        className\n      )}\n      style={{ backgroundColor: colorPaper, color: colorInk, ...style }}\n      {...props}\n    >\n      <div className=\"flex max-w-2xl flex-col gap-4\">\n        <span className=\"font-mono text-[11px] tracking-widest uppercase opacity-60\">\n          {eyebrow}\n        </span>\n        <h2 className=\"font-heading text-3xl leading-tight tracking-tight text-balance sm:text-5xl\">\n          {title}\n        </h2>\n        <p className=\"max-w-md text-base text-pretty opacity-70\">\n          {description}\n        </p>\n      </div>\n      <ul className=\"grid grid-cols-2 gap-x-4 gap-y-8 sm:grid-cols-3 lg:grid-cols-4\">\n        {members.map((member) => (\n          <li key={member.name} className=\"group flex flex-col gap-3\">\n            <DitheredPhoto\n              src={member.image}\n              params={{ colorBack, colorFront, pixelSize }}\n            />\n            <div className=\"flex flex-col\">\n              <span className=\"font-medium\">\n                {member.href ? (\n                  <a\n                    href={member.href}\n                    className=\"underline-offset-4 outline-none hover:underline focus-visible:underline\"\n                  >\n                    {member.name}\n                  </a>\n                ) : (\n                  member.name\n                )}\n              </span>\n              <span className=\"text-sm opacity-60\">{member.role}</span>\n            </div>\n          </li>\n        ))}\n      </ul>\n    </section>\n  )\n}\n\n/**\n * The original photo, covered by its dithered version until hovered. Until\n * the dither is ready, or if it can't render, a grayscale photo shows instead.\n */\nfunction DitheredPhoto({ src, params }: { src: string; params: DitherParams }) {\n  // Compared by value, so inline params don't trigger a new render.\n  const key = JSON.stringify([src, params])\n  const [loaded, setLoaded] = React.useState<{\n    src: string\n    image: HTMLImageElement\n  }>()\n  const [snapshot, setSnapshot] = React.useState<{ key: string; url: string }>()\n  const dithered = snapshot?.key === key ? snapshot.url : undefined\n\n  // Load the photo with CORS so the shader can read its pixels. A photo that\n  // fails just stays in grayscale.\n  React.useEffect(() => {\n    const element = new Image()\n    element.crossOrigin = \"anonymous\"\n    element.onload = () => setLoaded({ src, image: element })\n    element.src = src\n    return () => {\n      element.onload = null\n    }\n  }, [src])\n\n  React.useEffect(() => {\n    if (!snapshot) return\n    return () => URL.revokeObjectURL(snapshot.url)\n  }, [snapshot])\n\n  return (\n    <div\n      className=\"relative aspect-4/5 w-full overflow-hidden\"\n      style={{ backgroundColor: params.colorBack }}\n    >\n      {/* eslint-disable-next-line @next/next/no-img-element */}\n      <img\n        src={src}\n        alt=\"\"\n        className=\"absolute inset-0 size-full object-cover grayscale transition-[filter] duration-300 group-focus-within:grayscale-0 group-hover:grayscale-0\"\n      />\n      {dithered && (\n        // eslint-disable-next-line @next/next/no-img-element\n        <img\n          src={dithered}\n          alt=\"\"\n          className=\"absolute inset-0 size-full object-cover transition-opacity duration-300 [image-rendering:pixelated] group-focus-within:opacity-0 group-hover:opacity-0\"\n        />\n      )}\n      {loaded?.src === src && !dithered && (\n        <Snapshot\n          key={key}\n          image={loaded.image}\n          params={params}\n          onRender={(url) => setSnapshot({ key, url })}\n        />\n      )}\n    </div>\n  )\n}\n\nfunction Snapshot({\n  image,\n  params,\n  onRender,\n}: {\n  image: HTMLImageElement\n  params: DitherParams\n  onRender: (url: string) => void\n}) {\n  const ref = React.useRef<HTMLDivElement>(null)\n  const [ready, setReady] = React.useState(false)\n  const handleRender = React.useEffectEvent(onRender)\n\n  // Wait for a free slot before mounting the shader.\n  React.useEffect(() => {\n    const element = ref.current\n    let acquired = false\n    let cancelled = false\n\n    acquireSlot().then(() => {\n      if (cancelled) return releaseSlot()\n      acquired = true\n      setReady(true)\n    })\n\n    return () => {\n      cancelled = true\n      if (!acquired) return\n      // Unmounting doesn't free the context until it's garbage collected, so\n      // drop it explicitly before handing the slot to the next photo.\n      element\n        ?.querySelector(\"canvas\")\n        ?.getContext(\"webgl2\")\n        ?.getExtension(\"WEBGL_lose_context\")\n        ?.loseContext()\n      releaseSlot()\n    }\n  }, [])\n\n  // The shader mounts asynchronously, so poll until it exists. It draws when\n  // its resize observer first fires, and observers run in the order they were\n  // created, so one added now fires right after that first draw. Then save the\n  // canvas as an image. PNG keeps the dither pixels crisp.\n  React.useEffect(() => {\n    if (!ready) return\n\n    let cancelled = false\n    let observer: ResizeObserver | undefined\n    let frame = requestAnimationFrame(function check() {\n      const shader = ref.current?.querySelector(\"[data-paper-shader]\")\n      const canvas = shader?.querySelector(\"canvas\")\n      if (!shader || !canvas) {\n        frame = requestAnimationFrame(check)\n        return\n      }\n      observer = new ResizeObserver(() => {\n        observer?.disconnect()\n        canvas.toBlob((blob) => {\n          if (blob && !cancelled) handleRender(URL.createObjectURL(blob))\n        }, \"image/png\")\n      })\n      observer.observe(shader)\n    })\n\n    return () => {\n      cancelled = true\n      cancelAnimationFrame(frame)\n      observer?.disconnect()\n    }\n  }, [ready])\n\n  return (\n    // Rendered in place at the photo's size, so dither pixels stay square.\n    <div\n      ref={ref}\n      aria-hidden\n      className=\"pointer-events-none absolute inset-0 opacity-0\"\n    >\n      {ready && (\n        <ImageDithering\n          className=\"size-full\"\n          // Keeps the pixels around after drawing so they can be read back.\n          webGlContextAttributes={{ preserveDrawingBuffer: true }}\n          image={image}\n          colorBack={params.colorBack}\n          colorFront={params.colorFront}\n          colorHighlight={params.colorFront}\n          size={params.pixelSize}\n          type=\"8x8\"\n          colorSteps={1}\n          fit=\"cover\"\n        />\n      )}\n    </div>\n  )\n}\n\nfunction portrait(id: string) {\n  return `https://images.unsplash.com/photo-${id}?w=600&h=750&fit=crop&crop=faces&q=80&fm=jpg`\n}\n\nexport { ImageDitheringTeam, type TeamMember }\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "sections",
    "team"
  ],
  "type": "registry:block"
}