# Soft Spheres

> Matte 3D spheres hanging in soft studio light, each floating on its own rhythm, with far ones out of focus and the whole scene parallaxing with the cursor.

Category: Backgrounds · Free · Updated 2026-11-10 · Live demo: https://www.motionkitui.com/components/soft-spheres

## When to use it

Friendly SaaS and fintech heroes, design tools and agencies, onboarding screens and playful but modern brands.

## How it works

Each sphere is a circle filled with a radial gradient whose centre sits up and to the left: near white there, your colour through the middle, then darker towards the far edge, which is what makes a flat circle read as a matte ball. A second, faint gradient adds bounced light on the lower rim, and a blurred ellipse beneath gives it a shadow. Spheres are sorted by depth, the far ones are blurred like a shallow depth of field, each bobs on its own sine, and the pointer offsets them by depth for parallax.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/soft-spheres.json
```

Or copy `components/motionkit/soft-spheres.tsx` below. Requires react and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `colors` | `string[]` | `pink, blue, lilac, peach, mint` | Sphere colours; each sphere picks one. |
| `background` | `string` | `"#0F1016"` | Colour behind the spheres. |
| `count` | `number` | `9` | Number of spheres. |
| `float` | `number` | `1` | How far they float. 0 for still. |
| `interactive` | `boolean` | `true` | Parallax with the pointer by depth. |
| `children` | `ReactNode` |  | Your content, layered above. |

## Accessibility

- **Keyboard: Not applicable.** Decorative, nothing to focus.
- **Screen readers: Pass.** The effect is hidden from assistive tech; only your content is read.
- **Reduced motion: Pass.** With reduced motion on, the spheres are drawn once and stay put. It also stops animating off screen.
- **Colour contrast: Pass.** White text measures 19:1 on the default background; keep it off the largest, lightest spheres.

## Source

```tsx
"use client";

import { useEffect, useRef, type ReactNode } from "react";

type SoftSpheresProps = {
  children?: ReactNode;
  /** Sphere colours; each sphere picks one. */
  colors?: string[];
  /** Background behind the spheres. */
  background?: string;
  /** Number of spheres. */
  count?: number;
  /** How far they float, 0 for still. */
  float?: number;
  /** Let the spheres shift with the pointer, nearer ones more, for depth. */
  interactive?: boolean;
  className?: string;
};

type Sphere = { x: number; y: number; z: number; r: number; color: string; phase: number; rate: number };

// canvas colours must be plain values, so lighter and darker shades are mixed here
const rgb = (hex: string) => {
  const value = parseInt(hex.replace("#", "").padEnd(6, "0").slice(0, 6), 16);
  return [(value >> 16) & 255, (value >> 8) & 255, value & 255];
};
const shade = (hex: string, toward: number, amount: number, alpha = 1) => {
  const [r, g, b] = rgb(hex).map((channel) => Math.round(channel + (toward - channel) * amount));
  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};

/**
 * Matte 3D spheres hanging in soft light, like a studio render. Each is lit from the upper left with a gentle
 * rim of bounced light, floats on its own slow rhythm, and drifts with the pointer by its depth, so the scene parallaxes.
 */
export function SoftSpheres({
  children,
  colors = ["#FF9EBB", "#8FB4FF", "#C7A9FF", "#FFD59A", "#8FE6D1"],
  background = "#0F1016",
  count = 9,
  float = 1,
  interactive = true,
  className = "",
}: SoftSpheresProps) {
  const canvasRef = useRef<HTMLCanvasElement>(null);

  useEffect(() => {
    const canvas = canvasRef.current;
    const area = canvas?.parentElement;
    const context = canvas?.getContext("2d");
    if (!canvas || !area || !context) return;
    const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

    let width = 0;
    let height = 0;
    let ratio = 1;
    let frame = 0;
    let visible = true;
    let last = 0;
    let time = 0;
    const pointer = { x: 0, y: 0, tx: 0, ty: 0 };
    let spheres: Sphere[] = [];

    let seed = 3;
    const random = () => ((seed = (seed * 16807) % 2147483647) - 1) / 2147483646;

    function layout() {
      seed = 3;
      const unit = Math.min(width, height);
      spheres = Array.from({ length: count }, (_, index) => {
        const z = random();
        return {
          x: (0.08 + random() * 0.84) * width,
          y: (0.1 + random() * 0.8) * height,
          z,
          r: unit * (0.04 + z * 0.12) * (index === 0 ? 1.6 : 1),
          color: colors[index % colors.length],
          phase: random() * Math.PI * 2,
          rate: 0.3 + random() * 0.4,
        };
      }).sort((a, b) => a.z - b.z);
    }

    function resize() {
      const rect = canvas!.getBoundingClientRect();
      ratio = Math.min(window.devicePixelRatio || 1, 2);
      width = rect.width;
      height = rect.height;
      canvas!.width = Math.max(1, Math.round(width * ratio));
      canvas!.height = Math.max(1, Math.round(height * ratio));
      layout();
    }

    function drawSphere(sphere: Sphere) {
      const c = context!;
      const bob = still ? 0 : Math.sin(time * sphere.rate + sphere.phase) * sphere.r * 0.25 * float;
      const depth = 0.3 + sphere.z;
      const x = sphere.x + pointer.x * 40 * depth;
      const y = sphere.y + bob + pointer.y * 30 * depth;
      const r = sphere.r;
      // far spheres are softly out of focus
      c.filter = sphere.z < 0.3 ? `blur(${((0.3 - sphere.z) * 14).toFixed(1)}px)` : "none";
      // a soft shadow it casts below and to the right
      c.fillStyle = "rgba(0, 0, 0, 0.35)";
      c.beginPath();
      c.ellipse(x + r * 0.25, y + r * 1.15, r * 0.8, r * 0.18, 0, 0, Math.PI * 2);
      c.save();
      c.filter = `blur(${Math.max(4, r * 0.25).toFixed(1)}px)`;
      c.fill();
      c.restore();
      // the body: lit from the upper left, falling into shade at the far edge
      const body = c.createRadialGradient(x - r * 0.38, y - r * 0.42, r * 0.05, x - r * 0.1, y - r * 0.1, r * 1.12);
      body.addColorStop(0, shade(sphere.color, 255, 0.6));
      body.addColorStop(0.35, sphere.color);
      body.addColorStop(0.8, shade(sphere.color, 0, 0.35));
      body.addColorStop(1, shade(sphere.color, 0, 0.6));
      c.fillStyle = body;
      c.beginPath();
      c.arc(x, y, r, 0, Math.PI * 2);
      c.fill();
      // bounced light along the lower right rim
      const rim = c.createRadialGradient(x + r * 0.55, y + r * 0.6, r * 0.1, x + r * 0.4, y + r * 0.45, r * 0.9);
      rim.addColorStop(0, shade(sphere.color, 255, 0.35, 0.45));
      rim.addColorStop(1, shade(sphere.color, 255, 0.35, 0));
      c.fillStyle = rim;
      c.beginPath();
      c.arc(x, y, r, 0, Math.PI * 2);
      c.fill();
      c.filter = "none";
    }

    function draw() {
      const c = context!;
      c.setTransform(ratio, 0, 0, ratio, 0, 0);
      c.clearRect(0, 0, width, height);
      for (const sphere of spheres) drawSphere(sphere);
    }

    function loop(now: number) {
      const dt = last ? Math.min(0.05, (now - last) / 1000) : 0;
      last = now;
      time += dt;
      pointer.x += (pointer.tx - pointer.x) * 0.05;
      pointer.y += (pointer.ty - pointer.y) * 0.05;
      draw();
      frame = visible ? requestAnimationFrame(loop) : 0;
      if (!frame) last = 0;
    }

    resize();
    draw();
    if (!still) frame = requestAnimationFrame(loop);
    const resizeObserver = new ResizeObserver(() => {
      resize();
      draw();
    });
    resizeObserver.observe(canvas);
    const visibilityObserver = new IntersectionObserver(([entry]) => {
      visible = entry.isIntersecting;
      if (visible && !frame && !still) frame = requestAnimationFrame(loop);
    });
    visibilityObserver.observe(canvas);

    function move(event: PointerEvent) {
      if (!interactive || still || event.pointerType === "touch") return;
      const rect = canvas!.getBoundingClientRect();
      pointer.tx = ((event.clientX - rect.left) / rect.width - 0.5) * 2;
      pointer.ty = ((event.clientY - rect.top) / rect.height - 0.5) * 2;
    }
    const leave = () => {
      pointer.tx = 0;
      pointer.ty = 0;
    };
    area.addEventListener("pointermove", move);
    area.addEventListener("pointerleave", leave);

    return () => {
      cancelAnimationFrame(frame);
      resizeObserver.disconnect();
      visibilityObserver.disconnect();
      area.removeEventListener("pointermove", move);
      area.removeEventListener("pointerleave", leave);
    };
    // the colours are compared by value, so a new array with the same colours doesn't restart it
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [colors.join(), count, float, interactive]);

  return (
    <div className={`relative isolate overflow-hidden ${className}`} style={{ background: `radial-gradient(ellipse at 30% 20%, ${shade(background, 255, 0.06)}, ${background} 70%)` }}>
      <canvas ref={canvasRef} aria-hidden className="pointer-events-none absolute inset-0 -z-10 size-full" />
      {children}
    </div>
  );
}
```
