{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"confetti","type":"registry:component","title":"Confetti","description":"Confetti for success moments, called like a toast: confetti() from any handler throws a burst from a button or a point, on one canvas that exists only while pieces fall.","author":"Saurabh <saurabh10102@gmail.com>","dependencies":[],"registryDependencies":[],"docs":"Confetti: usage, props and types at https://beui.dev/micro-interactions/confetti.md","files":[{"path":"components/motion/confetti.tsx","type":"registry:component","target":"@components/motion/confetti.tsx","content":"// beui.dev/micro-interactions/confetti\n/**\n * Confetti for success moments, called like a toast: `confetti()` from any\n * event handler, no provider, hook or element to mount.\n *\n * A burst draws on one shared canvas laid over the page. The canvas exists only\n * while pieces are in the air and is removed when the last one has fallen, so\n * nothing stays fixed over the viewport between bursts. Overlapping bursts\n * share it. With reduced motion, nothing is drawn.\n */\n\nexport interface ConfettiOptions {\n  /** Where the burst starts: an element (from its centre) or a point in viewport px. Default: the centre of the viewport */\n  origin?: Element | { x: number; y: number };\n  /** How many pieces to throw. Default: 80 */\n  count?: number;\n  /** Piece colours, any CSS colour. Default: the theme's primary colour plus 5 warm and cool accents */\n  colors?: string[];\n  /** Direction of the throw in degrees: 90 is straight up, 0 is right. Default: 90 */\n  angle?: number;\n  /** How wide the throw fans out around `angle`, in degrees. Default: 70 */\n  spread?: number;\n  /** Launch speed multiplier: 0.5 is a small pop, 1.5 a big one. Default: 1 */\n  power?: number;\n}\n\ntype Piece = {\n  x: number;\n  y: number;\n  vx: number;\n  vy: number;\n  rotation: number;\n  spin: number;\n  flip: number;\n  flipSpeed: number;\n  sway: number;\n  swaySpeed: number;\n  swayWidth: number;\n  width: number;\n  height: number;\n  round: boolean;\n  color: string;\n  born: number;\n  life: number;\n};\n\nconst ACCENTS = [\"#f97316\", \"#facc15\", \"#22c55e\", \"#38bdf8\", \"#f43f5e\"];\n// Paper physics, tuned per frame at 60 fps and scaled by real frame time so\n// every screen plays the same. Strong air drag turns the launch into a quick\n// pop that slows at once; with gravity it also caps the fall at a gentle\n// terminal speed (GRAVITY × DRAG / (1 − DRAG), about 3.4 px a frame), so\n// pieces drift down instead of dropping like stones.\nconst GRAVITY = 0.22;\nconst DRAG = 0.94;\n// Launch speed in px a frame, before `power`.\nconst SPEED_MIN = 11;\nconst SPEED_RANGE = 9;\n// Pieces leave over this many ms, not in one frame, so the burst blooms\n// instead of starting as one dense dot.\nconst STAGGER_MS = 70;\nconst FADE_MS = 600;\n\nlet canvas: HTMLCanvasElement | null = null;\nlet context: CanvasRenderingContext2D | null = null;\nlet pieces: Piece[] = [];\nlet frame = 0;\nlet last = 0;\nconst settled: Array<() => void> = [];\n\nfunction themeColors() {\n  const primary = getComputedStyle(document.documentElement).getPropertyValue(\"--primary\").trim();\n  return primary ? [primary, ...ACCENTS] : ACCENTS;\n}\n\nfunction resolveOrigin(origin: ConfettiOptions[\"origin\"]) {\n  if (!origin) return { x: window.innerWidth / 2, y: window.innerHeight / 2 };\n  if (\"getBoundingClientRect\" in origin) {\n    const box = origin.getBoundingClientRect();\n    return { x: box.left + box.width / 2, y: box.top + box.height / 2 };\n  }\n  return origin;\n}\n\nfunction mountCanvas() {\n  if (canvas) return;\n  canvas = document.createElement(\"canvas\");\n  canvas.setAttribute(\"aria-hidden\", \"true\");\n  Object.assign(canvas.style, {\n    position: \"fixed\",\n    inset: \"0\",\n    width: \"100vw\",\n    height: \"100vh\",\n    pointerEvents: \"none\",\n    zIndex: \"250\",\n  });\n  document.body.appendChild(canvas);\n  context = canvas.getContext(\"2d\");\n  resize();\n  window.addEventListener(\"resize\", resize);\n}\n\nfunction resize() {\n  if (!canvas || !context) return;\n  const ratio = Math.min(window.devicePixelRatio || 1, 2);\n  canvas.width = Math.round(window.innerWidth * ratio);\n  canvas.height = Math.round(window.innerHeight * ratio);\n  context.setTransform(ratio, 0, 0, ratio, 0, 0);\n}\n\nfunction unmountCanvas() {\n  window.removeEventListener(\"resize\", resize);\n  canvas?.remove();\n  canvas = null;\n  context = null;\n  for (const resolve of settled.splice(0)) resolve();\n}\n\nfunction tick(now: number) {\n  const ctx = context;\n  if (!ctx) return;\n  // A frame's timestamp can be a little older than the moment the burst was\n  // fired; never step backwards, and never leap after a stall.\n  const step = Math.min(Math.max(0, (now - last) / (1000 / 60)), 3);\n  last = Math.max(last, now);\n  ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);\n\n  pieces = pieces.filter((piece) => {\n    const age = now - piece.born;\n    // Not launched yet: keep it, draw nothing.\n    if (age < 0) return true;\n    if (age > piece.life || piece.y > window.innerHeight + 40) return false;\n    const drag = DRAG ** step;\n    piece.vx *= drag;\n    piece.vy = piece.vy * drag + GRAVITY * step;\n    // Paper sways side to side as it falls.\n    piece.sway += piece.swaySpeed * step;\n    piece.x += (piece.vx + Math.sin(piece.sway) * piece.swayWidth) * step;\n    piece.y += piece.vy * step;\n    piece.rotation += piece.spin * step;\n    piece.flip += piece.flipSpeed * step;\n\n    const fade = Math.min(1, (piece.life - age) / FADE_MS);\n    ctx.save();\n    ctx.globalAlpha = Math.max(0, fade);\n    ctx.translate(piece.x, piece.y);\n    ctx.rotate(piece.rotation);\n    // Turning about one axis: the piece narrows to an edge and back, which\n    // reads as paper tumbling.\n    ctx.scale(1, Math.cos(piece.flip));\n    ctx.fillStyle = piece.color;\n    if (piece.round) {\n      ctx.beginPath();\n      ctx.arc(0, 0, piece.width / 2, 0, Math.PI * 2);\n      ctx.fill();\n    } else {\n      ctx.fillRect(-piece.width / 2, -piece.height / 2, piece.width, piece.height);\n    }\n    ctx.restore();\n    return true;\n  });\n\n  if (pieces.length) {\n    frame = requestAnimationFrame(tick);\n  } else {\n    frame = 0;\n    unmountCanvas();\n  }\n}\n\n/**\n * Throws a burst of confetti and resolves when the last piece has gone.\n * Does nothing on the server or with reduced motion.\n */\nexport function confetti(options: ConfettiOptions = {}): Promise<void> {\n  if (typeof window === \"undefined\") return Promise.resolve();\n  if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return Promise.resolve();\n\n  const {\n    origin,\n    count = 80,\n    colors = themeColors(),\n    angle = 90,\n    spread = 70,\n    power = 1,\n  } = options;\n  const start = resolveOrigin(origin);\n  const now = performance.now();\n\n  mountCanvas();\n  for (let index = 0; index < count; index++) {\n    const direction = ((angle + (Math.random() - 0.5) * spread) * Math.PI) / 180;\n    const speed = (SPEED_MIN + Math.random() * SPEED_RANGE) * power;\n    const width = 5 + Math.random() * 5;\n    pieces.push({\n      // A few px of scatter, so pieces do not all leave from one pixel.\n      x: start.x + (Math.random() - 0.5) * 8,\n      y: start.y + (Math.random() - 0.5) * 8,\n      vx: Math.cos(direction) * speed,\n      vy: -Math.sin(direction) * speed,\n      rotation: Math.random() * Math.PI * 2,\n      spin: (Math.random() - 0.5) * 0.24,\n      flip: Math.random() * Math.PI,\n      flipSpeed: 0.06 + Math.random() * 0.1,\n      sway: Math.random() * Math.PI * 2,\n      swaySpeed: 0.05 + Math.random() * 0.06,\n      swayWidth: 0.4 + Math.random() * 0.7,\n      width,\n      height: width * (1.4 + Math.random() * 0.6),\n      round: Math.random() < 0.25,\n      color: colors[index % colors.length],\n      born: now + Math.random() * STAGGER_MS,\n      life: 2400 + Math.random() * 1000,\n    });\n  }\n\n  const done = new Promise<void>((resolve) => settled.push(resolve));\n  if (!frame) {\n    last = now;\n    frame = requestAnimationFrame(tick);\n  }\n  return done;\n}\n"}]}