Stars

Three parallax star layers drifting at different speeds.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
stars.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/backgrounds/stars.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/stars.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
stars.tsx
"use client";

import * as React from "react";
import { motion, useMotionValue, useSpring } from "motion/react";
import { cn } from "@/lib/utils";
import { seeded, useBackgroundPlayback } from "./shared";

export type StarsBackgroundProps = React.ComponentProps<"div"> & {
  speed?: number;
  paused?: boolean;
  starColor?: string;
  factor?: number;
  pointerEvents?: boolean;
};

const LAYERS = [
  { count: 1100, size: 1, multiplier: 1, opacity: 0.55 },
  { count: 480, size: 2, multiplier: 2, opacity: 0.75 },
  { count: 220, size: 3, multiplier: 3, opacity: 0.95 },
];

function buildShadows(count: number, salt: number) {
  const shadows: string[] = [];
  for (let index = 0; index < count; index++) {
    const x = Math.round((seeded(index + salt * 1000, 1.7) - 0.5) * 2800);
    const y = Math.round((seeded(index + salt * 1000, 2.9) - 0.5) * 2800);
    shadows.push(`${x}px ${y}px`);
  }
  return shadows.join(",");
}

function StarLayer({
  count,
  size,
  multiplier,
  opacity,
  speed,
  starColor,
  playing,
  reduce,
}: {
  count: number;
  size: number;
  multiplier: number;
  opacity: number;
  speed: number;
  starColor: string;
  playing: boolean;
  reduce: boolean;
}) {
  const shadows = React.useMemo(
    () => buildShadows(count, multiplier),
    [count, multiplier],
  );
  const duration = (50 * multiplier) / Math.max(0.1, speed);

  return (
    <motion.div
      aria-hidden="true"
      animate={playing ? { y: [0, -2000] } : { y: 0 }}
      transition={
        playing
          ? { duration, repeat: Infinity, ease: "linear" }
          : { duration: 0 }
      }
      className="absolute left-0 top-0 h-[2000px] w-full"
      style={{ willChange: "transform" }}
    >
      <div
        className="absolute rounded-full bg-transparent"
        style={{
          width: size,
          height: size,
          color: starColor,
          boxShadow: shadows,
          opacity: reduce ? opacity * 0.8 : opacity,
        }}
      />
      <div
        className="absolute top-[2000px] rounded-full bg-transparent"
        style={{
          width: size,
          height: size,
          color: starColor,
          boxShadow: shadows,
          opacity: reduce ? opacity * 0.8 : opacity,
        }}
      />
    </motion.div>
  );
}

export function StarsBackground({
  speed = 50,
  paused = false,
  starColor = "#f8fafc",
  factor = 0.05,
  pointerEvents = true,
  className,
  children,
  ...props
}: StarsBackgroundProps) {
  const ref = React.useRef<HTMLDivElement>(null);
  const { playing: live, reduce } = useBackgroundPlayback(ref);
  const playing = live && !paused;

  const offsetX = useMotionValue(0);
  const offsetY = useMotionValue(0);
  const x = useSpring(offsetX, { stiffness: 50, damping: 20 });
  const y = useSpring(offsetY, { stiffness: 50, damping: 20 });

  const onPointerMove = (event: React.PointerEvent<HTMLDivElement>) => {
    if (reduce) return;
    const rect = event.currentTarget.getBoundingClientRect();
    offsetX.set(-(event.clientX - rect.left - rect.width / 2) * factor);
    offsetY.set(-(event.clientY - rect.top - rect.height / 2) * factor);
  };

  return (
    <div
      ref={ref}
      data-slot="stars-background"
      onPointerMove={onPointerMove}
      className={cn(
        "relative size-full overflow-hidden bg-[radial-gradient(ellipse_at_bottom,#17172a_0%,#05050a_75%)]",
        className,
      )}
      {...props}
    >
      <motion.div
        aria-hidden="true"
        style={{ x, y }}
        className={cn("absolute inset-0", !pointerEvents && "pointer-events-none")}
      >
        {LAYERS.map((layer) => (
          <StarLayer
            key={layer.multiplier}
            {...layer}
            speed={speed}
            starColor={starColor}
            playing={playing}
            reduce={reduce}
          />
        ))}
      </motion.div>
      <div
        aria-hidden="true"
        className="pointer-events-none absolute inset-0 bg-[radial-gradient(ellipse_at_center,transparent_35%,rgba(5,5,10,0.7))]"
      />
      {children}
    </div>
  );
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/stars.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

stars.tsx
import { StarsBackground } from "@/components/prequelui/backgrounds/stars";

export default function Demo() {
  return (
    <div className="relative h-96 overflow-hidden rounded-xl">
      <StarsBackground />
    </div>
  );
}
import { Stars } from "@/components/prequelui/stars";

export function Variant() {
  return (
    <Stars tempo={1.2}>
      …
    </Stars>
  );
}
03

Animation

Three layers of stars are painted as box shadows instead of nodes. Each layer falls at its own speed and repeats seamlessly; the pointer adds a small parallax offset on a soft spring.

  • Layer multipliers: 1×, 2× and 3× the base speed; 50 s, 100 s and 150 s at speed 1.
  • Stars are generated from a seeded distribution, so the field is stable between renders.
  • Pointer parallax: stiffness 50, damping 20, factor 0.05.
  • Reduced motion stops every layer and keeps the composed field.
50 / 100 / 150 s3 layersbox-shadow fieldparallax 50 / 20
04

API Reference

Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.

StarsBackground

PropTypeDefault
starColorstring"#f8fafc"
factornumber0.05
pointerEventsbooleantrue
speednumber1
pausedbooleanfalse
classNamestring—

Next component

Topography

Living contour lines with a hill under the pointer.