Containment
Metaballs
Soft gooey blobs drifting behind a hero. Three to five blobs drift and merge through the goo filter and lean toward the pointer.
Examples and variants
Hero
Move the pointer over it.
Make it yours
Move the pointer over the blobs
function MetaballHero() {
return (
<Metaballs className="flex h-56 w-full max-w-sm flex-col items-center justify-center rounded-(--shape-xl) bg-canvas text-center">
<p className="text-title">Make it yours</p>
<p className="text-sm text-muted-foreground">Move the pointer over the blobs</p>
</Metaballs>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/metaballs.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cnCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/metaballs.tsxmetaballs
components/ui/metaballs.tsx"use client" import * as React from "react" import { cn } from "cn" import { GooLayer } from "@/components/motion/goo-layer" import { motionLevel } from "@/lib/overlay" /* Metaballs: soft gooey blobs drifting behind a hero. 3 to 5 blobs drift on slow orbits and merge through the goo filter; they lean gently toward the pointer. Calm drifts slower and ignores the pointer; still is a static composition. The loop pauses while the background is off screen. Decorative only: hidden from screen readers. */ const fills = ["--metaball-1", "--metaball-2", "--metaball-3"] // Each blob: home position (0..1), orbit radius (0..1), speed and phase, size (share of the shorter side). const seeds = [ { x: 0.38, y: 0.48, orbit: 0.12, speed: 0.05, phase: 0, r: 0.3 }, { x: 0.6, y: 0.44, orbit: 0.14, speed: 0.04, phase: 2.1, r: 0.34 }, { x: 0.5, y: 0.62, orbit: 0.13, speed: 0.06, phase: 4.2, r: 0.24 }, { x: 0.74, y: 0.6, orbit: 0.1, speed: 0.05, phase: 1.3, r: 0.2 }, { x: 0.26, y: 0.62, orbit: 0.1, speed: 0.055, phase: 3.4, r: 0.18 }, ] function Metaballs({ blobs = 4, children, className, }: { /** 3 to 5. */ blobs?: 3 | 4 | 5 children?: React.ReactNode className?: string }) { const ref = React.useRef<HTMLDivElement>(null) const circles = React.useRef<(SVGCircleElement | null)[]>([]) const [box, setBox] = React.useState({ w: 400, h: 240 }) const count = Math.min(5, Math.max(3, blobs)) const used = seeds.slice(0, count) React.useEffect(() => { const node = ref.current if (!node) return const resize = new ResizeObserver(([entry]) => { const { width, height } = entry.contentRect if (width && height) setBox({ w: width, h: height }) }) resize.observe(node) return () => resize.disconnect() }, []) React.useEffect(() => { const node = ref.current if (!node) return let frame = 0 let visible = true const pointer = { x: 0.5, y: 0.5, on: false } const lean = { x: 0, y: 0 } const start = performance.now() const draw = (now: number) => { const level = motionLevel(node) const t = level === "still" ? 0 : ((now - start) / 1000) * (level === "calm" ? 0.5 : 1) const pull = level === "liquid" && pointer.on ? 1 : 0 lean.x += ((pointer.x - 0.5) * 0.12 * pull - lean.x) * 0.06 lean.y += ((pointer.y - 0.5) * 0.12 * pull - lean.y) * 0.06 seeds.slice(0, count).forEach((seed, i) => { const circle = circles.current[i] if (!circle) return const a = t * seed.speed * Math.PI * 2 + seed.phase circle.setAttribute("cx", String((seed.x + Math.cos(a) * seed.orbit + lean.x * (1 + i * 0.3)) * box.w)) circle.setAttribute("cy", String((seed.y + Math.sin(a * 1.3) * seed.orbit + lean.y * (1 + i * 0.3)) * box.h)) }) if (visible && level !== "still") frame = requestAnimationFrame(draw) } const io = new IntersectionObserver(([entry]) => { visible = entry.isIntersecting cancelAnimationFrame(frame) frame = requestAnimationFrame(draw) }) io.observe(node) const move = (event: PointerEvent) => { const rect = node.getBoundingClientRect() pointer.x = (event.clientX - rect.left) / rect.width pointer.y = (event.clientY - rect.top) / rect.height pointer.on = true if (visible) { cancelAnimationFrame(frame) frame = requestAnimationFrame(draw) } } const leave = () => (pointer.on = false) node.addEventListener("pointermove", move) node.addEventListener("pointerleave", leave) return () => { cancelAnimationFrame(frame) io.disconnect() node.removeEventListener("pointermove", move) node.removeEventListener("pointerleave", leave) } }, [box, count]) const short = Math.min(box.w, box.h) return ( <div ref={ref} data-slot="metaballs" className={cn("relative isolate overflow-hidden", className)}> <GooLayer className="absolute inset-0 -z-10" viewBox={`0 0 ${box.w} ${box.h}`}> {used.map((seed, i) => ( <circle key={i} ref={(el) => { circles.current[i] = el }} cx={seed.x * box.w} cy={seed.y * box.h} r={seed.r * short} style={{ fill: `var(${fills[i % fills.length]})` }} /> ))} </GooLayer> {children} </div> ) } export { Metaballs }
Imports
What the examples on this page import:
import { Button } from "@/components/ui/button"
import { CardStack } from "@/components/ui/card-stack"
import { Confetti } from "@/components/ui/confetti"
import { Dice } from "@/components/ui/dice"
import { FlipBoard } from "@/components/ui/flip-board"
import { Metaballs } from "@/components/ui/metaballs"
import { tokenMs } from "@/lib/motion/token-ms"Anatomy
- 1Blobs · --metaball-1 to --metaball-3 through the goo filter (--goo-blur, --goo-threshold).
- 2Content · Children sit above the blobs.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
blobs | 3 | 4 | 5 | 4 | How many blobs. |
children | ReactNode | — | Hero content. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--metaball-1 | color-mix(in oklab, var(--color-info) 22%, var(--surface-container-lowest)) | color-mix(in oklab, var(--color-info) 22%, var(--surface-container-lowest)) | same |
--metaball-2 | color-mix(in oklab, var(--color-success) 18%, var(--surface-container-lowest)) | color-mix(in oklab, var(--color-success) 18%, var(--surface-container-lowest)) | same |
--metaball-3 | color-mix(in oklab, var(--color-warning) 20%, var(--surface-container-lowest)) | color-mix(in oklab, var(--color-warning) 20%, var(--surface-container-lowest)) | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Ambient / pointer | Slow orbits; lean toward the pointer. Calm: half speed, no pointer. Still: static. Pauses off screen. | --goo-blur |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Decorative and hidden from screen readers; keep text contrast against the blobs.
Usage
Do
- Use behind one hero or empty state.
Don’t
- Don't put it behind dense content or several on one page.
Code
"use client"
import * as React from "react"
import { cn } from "cn"
import { GooLayer } from "@/components/motion/goo-layer"
import { motionLevel } from "@/lib/overlay"
/*
Metaballs: soft gooey blobs drifting behind a hero. 3 to 5 blobs drift on
slow orbits and merge through the goo filter; they lean gently toward the
pointer. Calm drifts slower and ignores the pointer; still is a static
composition. The loop pauses while the background is off screen.
Decorative only: hidden from screen readers.
*/
const fills = ["--metaball-1", "--metaball-2", "--metaball-3"]
// Each blob: home position (0..1), orbit radius (0..1), speed and phase, size (share of the shorter side).
const seeds = [
{ x: 0.38, y: 0.48, orbit: 0.12, speed: 0.05, phase: 0, r: 0.3 },
{ x: 0.6, y: 0.44, orbit: 0.14, speed: 0.04, phase: 2.1, r: 0.34 },
{ x: 0.5, y: 0.62, orbit: 0.13, speed: 0.06, phase: 4.2, r: 0.24 },
{ x: 0.74, y: 0.6, orbit: 0.1, speed: 0.05, phase: 1.3, r: 0.2 },
{ x: 0.26, y: 0.62, orbit: 0.1, speed: 0.055, phase: 3.4, r: 0.18 },
]
function Metaballs({
blobs = 4,
children,
className,
}: {
/** 3 to 5. */
blobs?: 3 | 4 | 5
children?: React.ReactNode
className?: string
}) {
const ref = React.useRef<HTMLDivElement>(null)
const circles = React.useRef<(SVGCircleElement | null)[]>([])
const [box, setBox] = React.useState({ w: 400, h: 240 })
const count = Math.min(5, Math.max(3, blobs))
const used = seeds.slice(0, count)
React.useEffect(() => {
const node = ref.current
if (!node) return
const resize = new ResizeObserver(([entry]) => {
const { width, height } = entry.contentRect
if (width && height) setBox({ w: width, h: height })
})
resize.observe(node)
return () => resize.disconnect()
}, [])
React.useEffect(() => {
const node = ref.current
if (!node) return
let frame = 0
let visible = true
const pointer = { x: 0.5, y: 0.5, on: false }
const lean = { x: 0, y: 0 }
const start = performance.now()
const draw = (now: number) => {
const level = motionLevel(node)
const t = level === "still" ? 0 : ((now - start) / 1000) * (level === "calm" ? 0.5 : 1)
const pull = level === "liquid" && pointer.on ? 1 : 0
lean.x += ((pointer.x - 0.5) * 0.12 * pull - lean.x) * 0.06
lean.y += ((pointer.y - 0.5) * 0.12 * pull - lean.y) * 0.06
seeds.slice(0, count).forEach((seed, i) => {
const circle = circles.current[i]
if (!circle) return
const a = t * seed.speed * Math.PI * 2 + seed.phase
circle.setAttribute("cx", String((seed.x + Math.cos(a) * seed.orbit + lean.x * (1 + i * 0.3)) * box.w))
circle.setAttribute("cy", String((seed.y + Math.sin(a * 1.3) * seed.orbit + lean.y * (1 + i * 0.3)) * box.h))
})
if (visible && level !== "still") frame = requestAnimationFrame(draw)
}
const io = new IntersectionObserver(([entry]) => {
visible = entry.isIntersecting
cancelAnimationFrame(frame)
frame = requestAnimationFrame(draw)
})
io.observe(node)
const move = (event: PointerEvent) => {
const rect = node.getBoundingClientRect()
pointer.x = (event.clientX - rect.left) / rect.width
pointer.y = (event.clientY - rect.top) / rect.height
pointer.on = true
if (visible) {
cancelAnimationFrame(frame)
frame = requestAnimationFrame(draw)
}
}
const leave = () => (pointer.on = false)
node.addEventListener("pointermove", move)
node.addEventListener("pointerleave", leave)
return () => {
cancelAnimationFrame(frame)
io.disconnect()
node.removeEventListener("pointermove", move)
node.removeEventListener("pointerleave", leave)
}
}, [box, count])
const short = Math.min(box.w, box.h)
return (
<div ref={ref} data-slot="metaballs" className={cn("relative isolate overflow-hidden", className)}>
<GooLayer className="absolute inset-0 -z-10" viewBox={`0 0 ${box.w} ${box.h}`}>
{used.map((seed, i) => (
<circle
key={i}
ref={(el) => {
circles.current[i] = el
}}
cx={seed.x * box.w}
cy={seed.y * box.h}
r={seed.r * short}
style={{ fill: `var(${fills[i % fills.length]})` }}
/>
))}
</GooLayer>
{children}
</div>
)
}
export { Metaballs }