Media Sphere
An immersive 3D gallery — image and video cards on the inside of a sphere. Drag to look around; cards blur, fade and soften toward the edges like a floating archive.
Preview
Drag or use arrow keys to explore. Press Enter to open the item in the centre.
Built for scientific research
Rich scientific artifacts, fully reproducible
Installation
- 1
Run the following command
npx shadcn@latest add https://localhostui.com/r/media-sphere.json
No animation library and no three.js — the only runtime dependency is React. Requires Tailwind CSS v4 (or v3.4+ with container query units, which every modern browser supports).
Usage
import { MediaSphere, type SphereItem } from "@/components/lokalhost_io/creativity/media-sphere/media-sphere";
const items: SphereItem[] = [
{ id: "1", type: "image", src: "/photos/01.jpg", alt: "Mist over green hills" },
{ id: "2", type: "image", src: "/photos/02.jpg", alt: "Desert road at dusk", href: "/work/desert" },
{ id: "3", type: "video", src: "/clips/waves.mp4", poster: "/clips/waves.jpg", alt: "Waves at sunrise" },
];
export default function Gallery() {
return <MediaSphere items={items} onItemClick={(item) => console.log(item.id)} />;
}The stage is full width and min(80vh, 720px) high by default. Set the height and background with className:
<MediaSphere items={items} className="h-[560px] bg-white" />Examples
Portfolio with an overlay
Anything passed as children is centred above the stage. The overlay lets pointer events through to the sphere, except on links, buttons and inputs, so the headline never blocks dragging and the CTA stays clickable.
Drag or use arrow keys to explore. Press Enter to open the item in the centre.
Compact picker
A 400px stage with three tight rows. lockScroll stops the page scrolling while a finger drags on the stage, renderItem adds a caption to each card and onItemClick reports the selection. Click a card and it turns to the centre.
Drag or use arrow keys to explore. Press Enter to open the item in the centre.
Click a card to select it — it turns to the centre.
Custom cards
renderItem replaces the content inside the card; the card keeps its position, shadow, rounding and depth cues. Use it for code snippets, charts or text cards:
<MediaSphere
items={items}
renderItem={(item) =>
item.id.startsWith("snippet") ? (
<pre className="size-full overflow-hidden bg-white p-3 font-mono text-[10px] leading-relaxed text-neutral-700">
{snippets[item.id]}
</pre>
) : (
<img src={item.src} alt={item.alt} draggable={false} className="pointer-events-none size-full object-cover" />
)
}
/>If your custom content has a <video> that should only play near the centre, add the data-media-sphere-video attribute to it and the sphere will play and pause it for you.
How it works
The camera sits at the centre of a sphere and every card is glued to its inside wall.
- Layout. Cards sit on latitude rings (
rings). Inside a ring the longitude step is divided bycos(lat)so the gap between cards is the same on every ring. Alternate rings are offset by half a step and every card gets a small seeded jitter (±4° longitude, ±1.5° latitude), so it reads as a cloud, not a grid. The jitter is deterministic, so server and client render the same layout. - Placement. Each card gets
rotateY(lon) rotateX(lat) translateZ(-R), which pushes it out to the sphere wall facing the centre. - Camera. The world container gets
translateZ(P) rotateX(pitch) rotateY(yaw).translateZ(P)moves the sphere centre onto the eye of the CSS perspective, so the viewer is literally inside. Foreshortening at the edges comes for free. - Depth cues. Each card's unit direction is computed once. Every frame, it is rotated by the current yaw and pitch and the angle θ to the centre of view is
acos(-z). From θ:
| θ | Effect |
|---|---|
| under 18° | Crisp, full opacity |
| 18° → 70° | Blur eases up to maxBlur |
| 12° → 72° | Opacity eases to minOpacity, saturation to 0.7 |
| under 45° | Videos play; outside they pause on their poster |
| over 80° | Hidden (visibility: hidden), skipped by screen readers |
Cards are hidden from 80° rather than 90° so no card ever crosses the camera plane, which causes clipping glitches in Safari and Firefox.
Customisation tips
- Density. Lower
spacingfor a denser cloud, raise it for an airy one. Rows closer together (rings) put more rows in view; the defaults are 13° apart so all four fit in a typical hero. Keep the gap above the card's angular height (about 9° at default sizes) so rows don't touch. The layout is capped at 60 cards; past that the spacing widens automatically. - Size on screen.
cardWidthaccepts any CSS length.cqwunits are relative to the stage width, so22cqwmeans 22% of the stage. - Responsive by default.
radiusandperspectiveare given for a 1000px-wide stage and scale with the stage width (×0.5 to ×1.6), like the default card width. The composition looks the same from a phone to an ultrawide screen. Keepperspectiveequal toradiusso the centre card shows at its natural size; a smaller perspective gives a wider, more distorted fish-eye view. - Horizontal-only vs. free look. By default the sphere only turns left and right (
maxPitch={0}). PassmaxPitch={25}with widerrings(e.g.[-30, -10, 10, 30]) to let people look up and down too. - Mood.
maxBlur={4} minOpacity={0.5}gives a calmer, more legible gallery;maxBlur={10} minOpacity={0.1}a more dreamlike one. - Order matters. Item 0 lands closest to the initial view. Put your strongest pieces first. With
fill, repeats go to the back. - Dark stages. Override the background with
className="bg-neutral-950"; the edge fade uses a mask, so it blends into any background.
Performance
- No React state in the loop. Yaw, pitch, velocity and tweens live in refs. One
requestAnimationFrameloop writes styles straight to the DOM. - One transform per frame. Only the world container's transform changes every frame. A card's
filterandopacityare rewritten only when they change by more than 2%, and cards that are fully crisp getfilter: none. will-change: transformis set on the world only, never on every card.- The loop sleeps. It stops while the stage is off-screen (
IntersectionObserver), while the tab is hidden, and whenever nothing is moving (hovered, no inertia, auto-rotate off). - Lazy media. Images use
loading="lazy"anddecoding="async". Videos usepreload="none"and only play inside a 45° cone, so usually one or two decode at a time. - SSR-safe. Nothing touches
windowduring render. The first paint already shows the correct layout and depth cues (radius and perspective are resolved in CSS with container query units), so there is no flash and no hydration mismatch.
For heavy galleries, serve images at about 2× the card width (≈640px) and keep videos short, muted and under 720p.
Props
MediaSphere
| Prop | Type | Default | Description |
|---|---|---|---|
items* | SphereItem[] | — | Images and videos to show. The first items are placed nearest the centre of view. |
radius | number | 900 | Sphere radius in px for a 1000px-wide stage. It scales with the stage width, from ×0.5 to ×1.6. |
perspective | number | 900 | CSS perspective in px, scaled like radius. Keep it equal to radius so the centre card shows at its natural size. |
rings | number[] | [-19.5, -6.5, 6.5, 19.5] | Latitudes of the card rows in degrees. Negative is above the horizon. Rows closer together means more rows in view. |
spacing | number | 24 | Arc distance between neighbouring cards in a ring, in degrees. Widened automatically if the layout would exceed 60 cards. |
cardWidth | number | string | "clamp(140px, 22cqw, 320px)" | Card width. Numbers are px; strings can use cqw (percent of the stage width). |
initialYaw | number | 0 | Starting horizontal angle in degrees. |
autoRotate | boolean | true | Slow idle drift. Pauses on hover and drag and resumes 2s after the last interaction. |
autoRotateSpeed | number | 2 | Idle drift in degrees per second. |
dragSensitivity | number | 0.15 | Degrees of rotation per dragged pixel. |
SphereItem
| Prop | Type | Default | Description |
|---|---|---|---|
id* | string | — | Stable unique id. |
type* | "image""video" | — | Videos are muted, looped and only play near the centre. |
src* | string | — | Image or video URL. |
alt* | string | — | Alt text for images, aria-label for videos. |
poster | string | — | Image shown while a video is paused or still loading. |
href | string | — | Renders the card as a link. |
Interaction
| Input | Action |
|---|---|
| Drag (mouse, pen, touch) | Turn the sphere left or right, with inertia on release. Content follows the pointer. |
| Click / tap a card | onItemClick, then the card turns to the centre (focusOnClick). A drag never triggers a click. |
| Hover | Pauses auto-rotate. It eases back in 2s after the pointer leaves. |
← → | Rotate 10° with an eased animation. |
↑ ↓ | Tilt 10° — only when maxPitch is above 0; otherwise the page scrolls as normal. |
Enter / Space on the stage | Activates the card in the centre. |
Tab | Moves through cards that are links; each one turns into view. |
| Wheel | Rotates only with wheelRotate. |
Rotation is horizontal-only by default: dragging up or down never tilts the view. On touch screens, vertical swipes scroll the page and horizontal swipes rotate the sphere. Pass lockScroll if the page shouldn't scroll while a finger is on the stage.
Accessibility
- The stage is a focusable
regionwitharia-roledescription="3D gallery", anaria-labeland visible focus ring. Screen readers also hear a hidden hint: “Drag or use arrow keys to explore.” - Every card carries its alt text. Cards with
hrefare real links. Hidden cards and repeated (fill) copies getaria-hiddenand are removed from the tab order. - With
prefers-reduced-motion: no auto-rotate, no inertia, no video autoplay, blur capped at 3px, and arrow keys jump instead of animating. - Text and images can't be selected or ghost-dragged while dragging.
Browser support
Chrome / Edge 105+, Firefox 110+ and Safari 16+ (desktop and iOS). It relies on CSS 3D transforms with preserve-3d, container query units (cqw) and mask-image. The edge-fade mask lives on its own wrapper outside the 3D context, which avoids Safari's flattening bugs with masks and filters on preserve-3d elements.