{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"particle-logo","type":"registry:component","title":"Particle Logo","description":"Your logo drawn in thousands of glittering particles. Some drift away in a slow, smooth stream up and down, fanning out and fading, then return to their place, so the logo seems to breathe out dust.","dependencies":[],"files":[{"path":"registry/dot/particle-logo.tsx","type":"registry:component","target":"components/dot/particle-logo.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, type CSSProperties } from \"react\";\n\nexport interface ParticleLogoProps {\n  /** The logo: SVG or transparent PNG (URL, data URL or object URL). Defaults to the Dot mark. */\n  src?: string;\n  /** Logo size as a share of the shorter side (0.2–0.9) */\n  size?: number;\n  /** Number of particles (1,000–14,000) */\n  particles?: number;\n  /** Particle size in px */\n  particleSize?: number;\n  color?: string;\n  /** CSS background; \"transparent\" to sit on your page */\n  background?: string;\n  /** Direction of the flow in degrees: 90 is straight up and down, 60 leans to the right */\n  flowAngle?: number;\n  /** Share of particles that drift away and come back (0–1) */\n  flow?: number;\n  /** How far they drift, relative to the logo size */\n  flowLength?: number;\n  /** How wide the stream fans out (0–2) */\n  spread?: number;\n  /** Speed of the flow */\n  speed?: number;\n  /** Particles glitter */\n  shimmer?: boolean;\n  /** The pointer brushes particles aside */\n  interactive?: boolean;\n  /** Particles gather into the logo when it appears */\n  intro?: boolean;\n  /** Accessible name */\n  alt?: string;\n  className?: string;\n  style?: CSSProperties;\n}\n\nconst SAMPLE = 256;\n\n// The Dot mark, used until you pass your own logo\nconst DOT_MARK = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(\n  '<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 88 104\" fill=\"#fff\"><rect x=\"48\" width=\"16\" height=\"95\"/><rect y=\"104\" width=\"16\" height=\"64\" transform=\"rotate(-90 0 104)\"/><path d=\"M16 80H0V60C0 48.9543 8.95431 40 20 40H80V56H20C17.7909 56 16 57.7909 16 60V80Z\"/><rect x=\"72\" y=\"88\" width=\"16\" height=\"16\" rx=\"8\"/></svg>'\n)}`;\n\n/** Points (centred, the longest side spanning -0.5…0.5) inside the logo */\nasync function logoPoints(src: string): Promise<Float32Array> {\n  const canvas = document.createElement(\"canvas\");\n  canvas.width = canvas.height = SAMPLE;\n  const ctx = canvas.getContext(\"2d\", { willReadFrequently: true });\n  if (!ctx) return new Float32Array(0);\n  const img = new Image();\n  img.crossOrigin = \"anonymous\";\n  img.decoding = \"async\";\n  img.src = src;\n  await img.decode();\n  const w = img.naturalWidth || SAMPLE;\n  const h = img.naturalHeight || SAMPLE;\n  const k = (SAMPLE * 0.98) / Math.max(w, h);\n  ctx.drawImage(img, (SAMPLE - w * k) / 2, (SAMPLE - h * k) / 2, w * k, h * k);\n  const data = ctx.getImageData(0, 0, SAMPLE, SAMPLE).data;\n  let opaque = 0;\n  for (let i = 3; i < data.length; i += 4) if (data[i] > 250) opaque++;\n  // A picture with no transparency: treat its dark parts as the logo\n  const byLuminance = opaque > SAMPLE * SAMPLE * 0.97;\n  const pts: number[] = [];\n  for (let y = 0; y < SAMPLE; y++)\n    for (let x = 0; x < SAMPLE; x++) {\n      const i = (y * SAMPLE + x) * 4;\n      const inside = byLuminance ? data[i] * 0.3 + data[i + 1] * 0.59 + data[i + 2] * 0.11 < 128 : data[i + 3] > 128;\n      if (inside) pts.push(x / SAMPLE - 0.5, y / SAMPLE - 0.5);\n    }\n  return Float32Array.from(pts);\n}\n\n/**\n * Any logo drawn in thousands of glittering particles. Some drift away from it in a smooth stream\n * along one axis, up and down by default, fanning out and fading, then return to their place, so\n * the logo seems to breathe out dust. Particles gather into the logo when it appears and the\n * pointer brushes them aside. Canvas, no dependencies; pauses offscreen and holds still with\n * reduced motion.\n */\nexport function ParticleLogo({\n  src,\n  size = 0.5,\n  particles = 12000,\n  particleSize = 1,\n  color = \"#FFFFFF\",\n  background = \"#000000\",\n  flowAngle = 60,\n  flow = 0.45,\n  flowLength = 1.3,\n  spread = 1,\n  speed = 0.3,\n  shimmer = true,\n  interactive = true,\n  intro = true,\n  alt = \"Logo\",\n  className,\n  style,\n}: ParticleLogoProps) {\n  const canvasRef = useRef<HTMLCanvasElement | null>(null);\n  const count = Math.max(1000, Math.min(14000, Math.round(particles)));\n  const settings = useRef({ size, particleSize, color, flowAngle, flow, flowLength, spread, speed, shimmer, interactive });\n  useEffect(() => {\n    settings.current = { size, particleSize, color, flowAngle, flow, flowLength, spread, speed, shimmer, interactive };\n  });\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!canvas || !ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let cancelled = false;\n    let raf = 0;\n    let width = 0;\n    let height = 0;\n    let time = 0;\n    let prev = 0;\n    let visible = true;\n    let pointer: { x: number; y: number } | null = null;\n\n    // Per particle: home (logo space), and fixed random traits\n    const hx = new Float32Array(count);\n    const hy = new Float32Array(count);\n    const rnd = Array.from({ length: 7 }, () => new Float32Array(count));\n    const ox = new Float32Array(count);\n    const oy = new Float32Array(count);\n    let ready = false;\n\n    const resize = () => {\n      const rect = canvas.getBoundingClientRect();\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      width = rect.width;\n      height = rect.height;\n      canvas.width = Math.max(1, Math.round(width * dpr));\n      canvas.height = Math.max(1, Math.round(height * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (reduce && ready) draw(0);\n    };\n\n    function draw(dt: number) {\n      const s = settings.current;\n      ctx!.clearRect(0, 0, width, height);\n      if (!ready) return;\n      const logo = Math.min(width, height) * Math.max(0.2, Math.min(0.9, s.size));\n      const cx = width / 2;\n      const cy = height / 2;\n      const a = (s.flowAngle * Math.PI) / 180;\n      const ax = Math.cos(a);\n      const ay = -Math.sin(a);\n      const px = -ay;\n      const py = ax;\n      const motion = !reduce;\n      const introT = intro && motion ? time / 2.2 : 1;\n      const pr = Math.max(0.4, s.particleSize);\n      const push = s.interactive && pointer && motion;\n      const reach = logo * 0.16;\n      const relax = Math.exp(-dt * 3);\n      ctx!.fillStyle = s.color;\n\n      for (let i = 0; i < count; i++) {\n        const r0 = rnd[0][i];\n        const r1 = rnd[1][i];\n        const r2 = rnd[2][i];\n        const r3 = rnd[3][i];\n        const r4 = rnd[4][i];\n        const r5 = rnd[5][i];\n        const r6 = rnd[6][i];\n        let x = cx + hx[i] * logo;\n        let y = cy + hy[i] * logo;\n        let alpha = 0.45 + r0 * 0.55;\n\n        // Drifting particles: a loop of resting in place, flowing out along the axis, and fading\n        // back in at home. Which side they leave from depends on where they sit on the logo.\n        if (motion && r1 < s.flow) {\n          const period = (5 + r2 * 5) / Math.max(0.1, s.speed);\n          const u = (time / period + r3) % 1;\n          const rest = 0.45;\n          if (u >= rest) {\n            const p = (u - rest) / (1 - rest);\n            const along = hx[i] * ax + hy[i] * ay + (r4 - 0.5) * 0.25;\n            const side = along >= 0 ? 1 : -1;\n            const dist = p ** 1.35 * s.flowLength * logo * (0.35 + r5 * 0.95);\n            const fan = (r6 - 0.5) * (0.35 + p) * logo * 0.75 * s.spread;\n            const wave = Math.sin(time * 0.7 + r2 * 9 + p * 4) * p * logo * 0.03;\n            x += side * dist * ax + (fan + wave) * px;\n            y += side * dist * ay + (fan + wave) * py;\n            alpha *= (1 - p) ** 1.1;\n          } else if (u < 0.06) {\n            alpha *= u / 0.06;\n          }\n        }\n\n        // Glitter: each particle catches the light at its own rhythm, a few sparkle\n        if (s.shimmer && motion) {\n          const w = Math.sin(time * (0.8 + r4 * 2.6) + r5 * 40);\n          alpha *= r6 > 0.92 ? 0.35 + 0.65 * Math.max(0, w) ** 6 * 1.6 : 0.6 + 0.4 * w;\n        }\n\n        // Gather into the logo when it appears: in from the stream along the axis\n        if (introT < 1) {\n          const t = Math.min(1, Math.max(0, (introT - r3 * 0.35) / 0.65));\n          const e = 1 - (1 - t) ** 3;\n          const side = r4 < 0.5 ? -1 : 1;\n          const far = logo * (0.7 + r5 * 1.4);\n          const sx = cx + hx[i] * logo + side * far * ax + (r6 - 0.5) * logo * px;\n          const sy = cy + hy[i] * logo + side * far * ay + (r6 - 0.5) * logo * py;\n          x = sx + (x - sx) * e;\n          y = sy + (y - sy) * e;\n          alpha *= t;\n        }\n\n        // The pointer brushes particles aside; they ease back\n        if (motion) {\n          if (push) {\n            const dx = x + ox[i] - pointer!.x;\n            const dy = y + oy[i] - pointer!.y;\n            const d2 = dx * dx + dy * dy;\n            if (d2 < reach * reach && d2 > 0.01) {\n              const d = Math.sqrt(d2);\n              const f = (1 - d / reach) * reach * 0.12;\n              ox[i] += (dx / d) * f;\n              oy[i] += (dy / d) * f;\n            }\n          }\n          ox[i] *= relax;\n          oy[i] *= relax;\n          x += ox[i];\n          y += oy[i];\n        }\n\n        if (alpha < 0.02) continue;\n        ctx!.globalAlpha = Math.min(1, alpha);\n        const sz = pr * (0.5 + r0 * 1.1);\n        ctx!.fillRect(x - sz / 2, y - sz / 2, sz, sz);\n      }\n      ctx!.globalAlpha = 1;\n    }\n\n    const tick = (now: number) => {\n      raf = requestAnimationFrame(tick);\n      if (!visible) {\n        prev = now;\n        return;\n      }\n      const dt = prev ? Math.min(0.05, (now - prev) / 1000) : 0;\n      prev = now;\n      if (ready) time += dt;\n      draw(dt);\n    };\n\n    logoPoints(src || DOT_MARK)\n      .then((pts) => {\n        if (cancelled || pts.length < 2) return;\n        const n = pts.length / 2;\n        for (let i = 0; i < count; i++) {\n          const k = Math.floor(Math.random() * n);\n          hx[i] = pts[k * 2] + (Math.random() - 0.5) / SAMPLE;\n          hy[i] = pts[k * 2 + 1] + (Math.random() - 0.5) / SAMPLE;\n          for (const r of rnd) r[i] = Math.random();\n        }\n        ready = true;\n        if (reduce) draw(0);\n      })\n      .catch(() => {\n        // The logo couldn't be read (often a cross-origin image without CORS): leave it empty\n      });\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    resize();\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n    });\n    io.observe(canvas);\n    const onMove = (e: PointerEvent) => {\n      const rect = canvas.getBoundingClientRect();\n      pointer = { x: e.clientX - rect.left, y: e.clientY - rect.top };\n    };\n    const onLeave = () => {\n      pointer = null;\n    };\n    canvas.addEventListener(\"pointermove\", onMove);\n    canvas.addEventListener(\"pointerleave\", onLeave);\n    if (!reduce) raf = requestAnimationFrame(tick);\n\n    return () => {\n      cancelled = true;\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      canvas.removeEventListener(\"pointermove\", onMove);\n      canvas.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, [src, count, intro]);\n\n  return (\n    <div className={className} style={{ position: \"relative\", width: \"100%\", height: \"100%\", background, ...style }}>\n      <canvas ref={canvasRef} role=\"img\" aria-label={alt} style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\", display: \"block\", touchAction: \"pan-y\" }} />\n    </div>\n  );\n}\n"}],"docs":"Particle Logo is ready. Props and a live canvas: https://bydot.co/component/particle-logo","meta":{"tier":"free","category":"3D & Effects","version":"1.0.0"}}