{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "backgrounds-gravity-stars",
  "type": "registry:ui",
  "title": "Gravity Stars Background",
  "description": "A background component featuring a subtle yet engaging animated gravity stars effect.",
  "dependencies": [
    "@animate-ui/react",
    "cn",
    "motion"
  ],
  "files": [
    {
      "path": "registry/backgrounds/gravity-stars/index.tsx",
      "type": "registry:ui",
      "target": "@components/animate-ui/backgrounds/gravity-stars.tsx",
      "content": "'use client';\n\nimport * as React from 'react';\nimport { useInView } from 'motion/react';\n\nimport {\n  ReducedMotionConfig,\n  useShouldReduceMotion,\n} from '@animate-ui/react/utils/motion';\nimport { usePageVisible } from '@animate-ui/react/utils/use-page-visible';\nimport { cn } from 'cn';\n\ntype MouseGravity = 'attract' | 'repel';\ntype GlowAnimation = 'instant' | 'ease' | 'spring';\ntype StarsInteractionType = 'bounce' | 'merge';\n\ntype GravityStarsProps = {\n  starsCount?: number;\n  starsSize?: number;\n  starsOpacity?: number;\n  glowIntensity?: number;\n  glowAnimation?: GlowAnimation;\n  movementSpeed?: number;\n  mouseInfluence?: number;\n  mouseGravity?: MouseGravity;\n  gravityStrength?: number;\n  starsInteraction?: boolean;\n  starsInteractionType?: StarsInteractionType;\n} & React.ComponentProps<'div'>;\n\ntype Particle = {\n  x: number;\n  y: number;\n  vx: number;\n  vy: number;\n  size: number;\n  opacity: number;\n  baseOpacity: number;\n  mass: number;\n  glowMultiplier?: number;\n  glowVelocity?: number;\n};\n\nfunction GravityStarsBackgroundContent({\n  starsCount = 75,\n  starsSize = 2,\n  starsOpacity = 0.75,\n  glowIntensity = 15,\n  glowAnimation = 'ease',\n  movementSpeed = 0.3,\n  mouseInfluence = 100,\n  mouseGravity = 'attract',\n  gravityStrength = 75,\n  starsInteraction = false,\n  starsInteractionType = 'bounce',\n  ref,\n  className,\n  children,\n  onMouseMove,\n  onTouchMove,\n  ...props\n}: GravityStarsProps) {\n  const containerRef = React.useRef<HTMLDivElement | null>(null);\n  React.useImperativeHandle(ref, () => containerRef.current as HTMLDivElement);\n  const canvasRef = React.useRef<HTMLCanvasElement | null>(null);\n  const starsRef = React.useRef<Particle[]>([]);\n  const mouseRef = React.useRef<{ x: number; y: number }>({ x: 0, y: 0 });\n\n  const inView = useInView(containerRef);\n  const pageVisible = usePageVisible();\n  const reduce = useShouldReduceMotion();\n  const playing = inView && pageVisible && !reduce;\n\n  const options = {\n    starsCount,\n    starsSize,\n    starsOpacity,\n    glowIntensity,\n    glowAnimation,\n    movementSpeed,\n    mouseInfluence,\n    mouseGravity,\n    gravityStrength,\n    starsInteraction,\n    starsInteractionType,\n  };\n  const optionsRef = React.useRef(options);\n  React.useEffect(() => {\n    optionsRef.current = options;\n  });\n\n  const engineRef = React.useRef<{\n    start: () => void;\n    stop: () => void;\n    reset: () => void;\n    retune: () => void;\n  } | null>(null);\n\n  React.useEffect(() => {\n    const canvas = canvasRef.current;\n    const container = containerRef.current;\n    if (!canvas || !container) return;\n    const ctx = canvas.getContext('2d');\n    if (!ctx) return;\n\n    let width = 0;\n    let height = 0;\n    let dpr = 1;\n    let color = '#ffffff';\n    let running = false;\n    let frameId = 0;\n\n    const createStar = (): Particle => {\n      const { movementSpeed, starsSize, starsOpacity } = optionsRef.current;\n      const angle = Math.random() * Math.PI * 2;\n      const speed = movementSpeed * (0.5 + Math.random() * 0.5);\n      return {\n        x: Math.random() * width,\n        y: Math.random() * height,\n        vx: Math.cos(angle) * speed,\n        vy: Math.sin(angle) * speed,\n        size: Math.random() * starsSize + 1,\n        opacity: starsOpacity,\n        baseOpacity: starsOpacity,\n        mass: Math.random() * 0.5 + 0.5,\n        glowMultiplier: 1,\n        glowVelocity: 0,\n      };\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, canvas.width, canvas.height);\n      ctx.save();\n      ctx.shadowColor = color;\n      ctx.fillStyle = color;\n      const { glowIntensity } = optionsRef.current;\n      let shadowBlur = -1;\n      for (const p of starsRef.current) {\n        const blur = glowIntensity * (p.glowMultiplier || 1) * 2;\n        if (blur !== shadowBlur) {\n          ctx.shadowBlur = blur;\n          shadowBlur = blur;\n        }\n        ctx.globalAlpha = p.opacity;\n        ctx.beginPath();\n        ctx.arc(p.x * dpr, p.y * dpr, p.size * dpr, 0, Math.PI * 2);\n        ctx.fill();\n      }\n      ctx.restore();\n    };\n\n    const readColor = () => {\n      color = getComputedStyle(container).color || '#ffffff';\n      if (!running) draw();\n    };\n\n    const retuneStars = () => {\n      const { starsOpacity, movementSpeed } = optionsRef.current;\n      for (const p of starsRef.current) {\n        p.baseOpacity = starsOpacity;\n        p.opacity = starsOpacity;\n        const spd = Math.hypot(p.vx, p.vy);\n        if (spd > 0) {\n          const ratio = movementSpeed / spd;\n          p.vx *= ratio;\n          p.vy *= ratio;\n        }\n      }\n    };\n\n    const createStars = () => {\n      starsRef.current = Array.from(\n        { length: optionsRef.current.starsCount },\n        createStar,\n      );\n      retuneStars();\n    };\n\n    const resize = () => {\n      const rect = container.getBoundingClientRect();\n      const nextDpr = Math.max(1, Math.min(window.devicePixelRatio || 1, 2));\n      const sizeChanged = rect.width !== width || rect.height !== height;\n      if (!sizeChanged && nextDpr === dpr && starsRef.current.length > 0)\n        return;\n      width = rect.width;\n      height = rect.height;\n      dpr = nextDpr;\n      canvas.width = Math.max(1, Math.floor(width * dpr));\n      canvas.height = Math.max(1, Math.floor(height * dpr));\n      color = getComputedStyle(container).color || '#ffffff';\n      if (starsRef.current.length === 0) {\n        createStars();\n      } else {\n        for (const p of starsRef.current) {\n          p.x = Math.random() * width;\n          p.y = Math.random() * height;\n        }\n        if (sizeChanged) retuneStars();\n      }\n      if (!running) draw();\n    };\n\n    const update = () => {\n      const {\n        mouseInfluence,\n        mouseGravity,\n        gravityStrength,\n        glowAnimation,\n        starsInteraction,\n        starsInteractionType,\n      } = optionsRef.current;\n      const mouse = mouseRef.current;\n      const stars = starsRef.current;\n\n      for (let i = 0; i < stars.length; i++) {\n        const p = stars[i]!;\n\n        const dx = mouse.x - p.x;\n        const dy = mouse.y - p.y;\n        const distSq = dx * dx + dy * dy;\n\n        if (distSq < mouseInfluence * mouseInfluence && distSq > 0) {\n          const dist = Math.sqrt(distSq);\n          const force = (mouseInfluence - dist) / mouseInfluence;\n          const nx = dx / dist;\n          const ny = dy / dist;\n          const g = force * (gravityStrength * 0.001);\n\n          if (mouseGravity === 'attract') {\n            p.vx += nx * g;\n            p.vy += ny * g;\n          } else if (mouseGravity === 'repel') {\n            p.vx -= nx * g;\n            p.vy -= ny * g;\n          }\n\n          p.opacity = Math.min(1, p.baseOpacity + force * 0.4);\n\n          const targetGlow = 1 + force * 2;\n          const currentGlow = p.glowMultiplier || 1;\n\n          if (glowAnimation === 'instant') {\n            p.glowMultiplier = targetGlow;\n          } else if (glowAnimation === 'ease') {\n            const ease = 0.15;\n            p.glowMultiplier = currentGlow + (targetGlow - currentGlow) * ease;\n          } else {\n            const spring = (targetGlow - currentGlow) * 0.2;\n            const damping = 0.85;\n            p.glowVelocity = (p.glowVelocity || 0) * damping + spring;\n            p.glowMultiplier = currentGlow + (p.glowVelocity || 0);\n          }\n        } else {\n          p.opacity = Math.max(p.baseOpacity * 0.3, p.opacity - 0.02);\n          const targetGlow = 1;\n          const currentGlow = p.glowMultiplier || 1;\n          if (glowAnimation === 'instant') {\n            p.glowMultiplier = targetGlow;\n          } else if (glowAnimation === 'ease') {\n            const ease = 0.08;\n            p.glowMultiplier = Math.max(\n              1,\n              currentGlow + (targetGlow - currentGlow) * ease,\n            );\n          } else {\n            const spring = (targetGlow - currentGlow) * 0.15;\n            const damping = 0.9;\n            p.glowVelocity = (p.glowVelocity || 0) * damping + spring;\n            p.glowMultiplier = Math.max(1, currentGlow + (p.glowVelocity || 0));\n          }\n        }\n\n        if (starsInteraction) {\n          for (let j = i + 1; j < stars.length; j++) {\n            const o = stars[j]!;\n            const dx2 = o.x - p.x;\n            const dy2 = o.y - p.y;\n            const dSq = dx2 * dx2 + dy2 * dy2;\n            const minD = p.size + o.size + 5;\n            if (dSq < minD * minD && dSq > 0) {\n              const d = Math.sqrt(dSq);\n              if (starsInteractionType === 'bounce') {\n                const nx = dx2 / d;\n                const ny = dy2 / d;\n                const rvx = p.vx - o.vx;\n                const rvy = p.vy - o.vy;\n                const speed = rvx * nx + rvy * ny;\n                if (speed < 0) continue;\n                const impulse = (2 * speed) / (p.mass + o.mass);\n                p.vx -= impulse * o.mass * nx;\n                p.vy -= impulse * o.mass * ny;\n                o.vx += impulse * p.mass * nx;\n                o.vy += impulse * p.mass * ny;\n                const overlap = minD - d;\n                const sx = nx * overlap * 0.5;\n                const sy = ny * overlap * 0.5;\n                p.x -= sx;\n                p.y -= sy;\n                o.x += sx;\n                o.y += sy;\n              } else {\n                const mergeForce = (minD - d) / minD;\n                p.glowMultiplier = (p.glowMultiplier || 1) + mergeForce * 0.5;\n                o.glowMultiplier = (o.glowMultiplier || 1) + mergeForce * 0.5;\n                const af = mergeForce * 0.01;\n                p.vx += dx2 * af;\n                p.vy += dy2 * af;\n                o.vx -= dx2 * af;\n                o.vy -= dy2 * af;\n              }\n            }\n          }\n        }\n\n        p.x += p.vx;\n        p.y += p.vy;\n\n        p.vx += (Math.random() - 0.5) * 0.001;\n        p.vy += (Math.random() - 0.5) * 0.001;\n\n        p.vx *= 0.999;\n        p.vy *= 0.999;\n\n        if (p.x < 0) p.x = width;\n        if (p.x > width) p.x = 0;\n        if (p.y < 0) p.y = height;\n        if (p.y > height) p.y = 0;\n      }\n    };\n\n    const tick = () => {\n      update();\n      draw();\n      frameId = requestAnimationFrame(tick);\n    };\n\n    const start = () => {\n      if (running) return;\n      running = true;\n      frameId = requestAnimationFrame(tick);\n    };\n\n    const stop = () => {\n      running = false;\n      cancelAnimationFrame(frameId);\n    };\n\n    const reset = () => {\n      createStars();\n      if (!running) draw();\n    };\n\n    const retune = () => {\n      retuneStars();\n      if (!running) draw();\n    };\n\n    resize();\n    window.addEventListener('resize', resize);\n    const resizeObserver = new ResizeObserver(resize);\n    resizeObserver.observe(container);\n\n    const mutationObserver = new MutationObserver(readColor);\n    mutationObserver.observe(document.documentElement, {\n      attributes: true,\n      attributeFilter: ['class', 'style', 'data-theme'],\n    });\n    const mql = window.matchMedia('(prefers-color-scheme: dark)');\n    mql.addEventListener('change', readColor);\n\n    engineRef.current = { start, stop, reset, retune };\n\n    return () => {\n      stop();\n      engineRef.current = null;\n      window.removeEventListener('resize', resize);\n      resizeObserver.disconnect();\n      mutationObserver.disconnect();\n      mql.removeEventListener('change', readColor);\n    };\n  }, []);\n\n  React.useEffect(() => {\n    engineRef.current?.reset();\n  }, [starsCount, starsSize]);\n\n  React.useEffect(() => {\n    engineRef.current?.retune();\n  }, [starsOpacity, movementSpeed, glowIntensity]);\n\n  React.useEffect(() => {\n    const engine = engineRef.current;\n    if (!playing || !engine) return;\n    engine.start();\n    return engine.stop;\n  }, [playing]);\n\n  const handlePointerMove = (e: React.MouseEvent | React.TouchEvent) => {\n    const canvas = canvasRef.current;\n    if (!canvas) return;\n    const rect = canvas.getBoundingClientRect();\n    const point = 'touches' in e ? e.touches[0] : e;\n    if (!point) return;\n    mouseRef.current = {\n      x: point.clientX - rect.left,\n      y: point.clientY - rect.top,\n    };\n  };\n\n  return (\n    <div\n      ref={containerRef}\n      data-slot=\"gravity-stars-background\"\n      className={cn('relative size-full overflow-hidden', className)}\n      onMouseMove={(e) => {\n        handlePointerMove(e);\n        onMouseMove?.(e);\n      }}\n      onTouchMove={(e) => {\n        handlePointerMove(e);\n        onTouchMove?.(e);\n      }}\n      {...props}\n    >\n      <canvas\n        ref={canvasRef}\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 block size-full\"\n      />\n      {children}\n    </div>\n  );\n}\n\nfunction GravityStarsBackground(props: GravityStarsProps) {\n  return (\n    <ReducedMotionConfig>\n      <GravityStarsBackgroundContent {...props} />\n    </ReducedMotionConfig>\n  );\n}\n\nexport { GravityStarsBackground, type GravityStarsProps };\n"
    }
  ]
}