{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-cropper-11",
  "title": "Crop preview",
  "description": "A button crops the selection and shows the result beside it.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/cropper.json",
    "https://ui.ndekocode.dev/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/image-cropper/image-cropper-11.tsx",
      "content": "'use client';\n\nimport { useEffect, useState, useSyncExternalStore } from 'react';\n\nimport { Button } from '@/registry/ndeko/ui/button';\nimport {\n  Cropper,\n  CropperCropArea,\n  CropperDescription,\n  CropperImage,\n} from '@/registry/ndeko/ui/cropper';\n\nconst PLACEHOLDER_WIDTH = 1200;\nconst PLACEHOLDER_HEIGHT = 800;\nconst WASH_ALPHA = 0.35;\nconst BRAND_CIRCLE = { x: 840, y: 280, radius: 150, alpha: 0.45 };\nconst INFO_CIRCLE = { x: 360, y: 640, radius: 260, alpha: 0.3 };\nconst FULL_TURN = Math.PI * 2;\nconst THEME_ATTRIBUTES = ['class', 'style', 'data-theme'];\n\ntype Circle = typeof BRAND_CIRCLE;\n\ntype DrawnImage = { key: string; url: string | null };\n\nlet drawnImage: DrawnImage | null = null;\n\nfunction tokenOf(styles: CSSStyleDeclaration, name: string) {\n  const value = styles.getPropertyValue(name);\n  return value.trim();\n}\n\nfunction fillCircle(\n  context: CanvasRenderingContext2D,\n  circle: Circle,\n  color: string\n) {\n  context.globalAlpha = circle.alpha;\n  context.fillStyle = color;\n  context.beginPath();\n  context.arc(circle.x, circle.y, circle.radius, 0, FULL_TURN);\n  context.fill();\n}\n\nfunction drawPlaceholderImage() {\n  const styles = getComputedStyle(document.documentElement);\n  const base = tokenOf(styles, '--card');\n  const brand = tokenOf(styles, '--primary');\n  const info = tokenOf(styles, '--info');\n  const canvas = document.createElement('canvas');\n  canvas.width = PLACEHOLDER_WIDTH;\n  canvas.height = PLACEHOLDER_HEIGHT;\n  const context = canvas.getContext('2d');\n  if (!context) {\n    return null;\n  }\n  context.fillStyle = base;\n  context.fillRect(0, 0, PLACEHOLDER_WIDTH, PLACEHOLDER_HEIGHT);\n  const wash = context.createLinearGradient(\n    0,\n    0,\n    PLACEHOLDER_WIDTH,\n    PLACEHOLDER_HEIGHT\n  );\n  wash.addColorStop(0, brand);\n  wash.addColorStop(1, info);\n  context.globalAlpha = WASH_ALPHA;\n  context.fillStyle = wash;\n  context.fillRect(0, 0, PLACEHOLDER_WIDTH, PLACEHOLDER_HEIGHT);\n  fillCircle(context, BRAND_CIRCLE, brand);\n  fillCircle(context, INFO_CIRCLE, info);\n  return canvas.toDataURL('image/png');\n}\n\nfunction placeholderSnapshot() {\n  const key = document.documentElement.className;\n  const cachedImage = drawnImage;\n  const isCurrent = cachedImage?.key === key;\n  if (cachedImage && isCurrent) {\n    return cachedImage.url;\n  }\n  const url = drawPlaceholderImage();\n  drawnImage = { key, url };\n  return url;\n}\n\nfunction serverSnapshot() {\n  return null;\n}\n\nfunction subscribeToTheme(onThemeChange: () => void) {\n  const observer = new MutationObserver(onThemeChange);\n  observer.observe(document.documentElement, {\n    attributes: true,\n    attributeFilter: THEME_ATTRIBUTES,\n  });\n  return () => observer.disconnect();\n}\n\nfunction usePlaceholderImage() {\n  return useSyncExternalStore(\n    subscribeToTheme,\n    placeholderSnapshot,\n    serverSnapshot\n  );\n}\n\ntype Area = { x: number; y: number; width: number; height: number };\n\nfunction loadImage(url: string) {\n  return new Promise<HTMLImageElement>((resolve, reject) => {\n    const image = new Image();\n    image.addEventListener('load', () => resolve(image));\n    image.addEventListener('error', reject);\n    image.crossOrigin = 'anonymous';\n    image.src = url;\n  });\n}\n\nfunction blobOf(canvas: HTMLCanvasElement) {\n  return new Promise<Blob | null>((resolve) => {\n    canvas.toBlob(resolve, 'image/png');\n  });\n}\n\nasync function cropImage(imageUrl: string, area: Area) {\n  const image = await loadImage(imageUrl);\n  const canvas = document.createElement('canvas');\n  canvas.width = area.width;\n  canvas.height = area.height;\n  const context = canvas.getContext('2d');\n  if (!context) {\n    return null;\n  }\n  context.drawImage(\n    image,\n    area.x,\n    area.y,\n    area.width,\n    area.height,\n    0,\n    0,\n    area.width,\n    area.height\n  );\n  return blobOf(canvas);\n}\n\nasync function croppedBlobOf(imageUrl: string, area: Area) {\n  try {\n    return await cropImage(imageUrl, area);\n  } catch {\n    return null;\n  }\n}\n\nfunction CroppedPreview({ url }: { url: string | null }) {\n  if (url) {\n    return (\n      <div\n        role='img'\n        aria-label='Cropped result'\n        className='hairline-ring ring-border aspect-square w-full shrink-0 rounded-lg bg-cover bg-center'\n        style={{ backgroundImage: `url(${url})` }}\n      />\n    );\n  }\n  return (\n    <div className='bg-foreground/[0.04] text-muted-foreground flex aspect-square w-full shrink-0 items-center justify-center rounded-lg p-2 text-center text-xs'>\n      Image preview\n    </div>\n  );\n}\n\nexport default function ImageCropper11() {\n  const image = usePlaceholderImage();\n  const [cropArea, setCropArea] = useState<Area | null>(null);\n  const [croppedUrl, setCroppedUrl] = useState<string | null>(null);\n  const canCrop = cropArea !== null;\n\n  useEffect(() => {\n    if (!croppedUrl) {\n      return;\n    }\n    return () => URL.revokeObjectURL(croppedUrl);\n  }, [croppedUrl]);\n\n  async function cropPreview() {\n    if (!image || !cropArea) {\n      return;\n    }\n    const blob = await croppedBlobOf(image, cropArea);\n    if (!blob) {\n      setCroppedUrl(null);\n      return;\n    }\n    const nextUrl = URL.createObjectURL(blob);\n    setCroppedUrl(nextUrl);\n  }\n\n  return (\n    <div className='flex w-full flex-col gap-4 md:flex-row'>\n      {image && (\n        <Cropper\n          className='h-64 md:flex-1'\n          image={image}\n          onCropChange={setCropArea}\n        >\n          <CropperDescription />\n          <CropperImage />\n          <CropperCropArea />\n        </Cropper>\n      )}\n      {!image && (\n        <div\n          aria-hidden='true'\n          className='bg-foreground/[0.04] h-64 w-full rounded-xl md:flex-1'\n        />\n      )}\n      <div className='flex w-28 flex-col gap-4'>\n        <Button onClick={cropPreview} disabled={!canCrop}>\n          Crop preview\n        </Button>\n        <CroppedPreview url={croppedUrl} />\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "image-cropper"
  ],
  "type": "registry:component"
}