Starfield
A 2D canvas of stars, each carrying its own z depth, flying at the viewer and growing; the mouse bends the field with a gentle parallax while a travel headline rides on top. The RAF loop parks off screen and settles into a still sky under reduced motion.
Real scenarios
The component as it is used in a product — not a specimen grid.
Stars that fly past the headline
One canvas, one projection: every star owns a z, radius and brightness fall out of 1/z and the pointer eases the vanishing point by ±26px. Warp triples the approach for a second and a half. Density scales with canvas area, the loop parks off screen, and reduced motion gets a single still sky.
Nowhere is far anymore
Board at midnight, land at sunrise. Skyward flies the quiet hours to 42 cities so your first morning is a full one.
2D canvas · star z ∈ (0, w] · r ∝ 1/z · eased ±26px parallax · parks off screen · still sky under reduced motion
Important props
| Prop | Type | Default | What it does |
|---|---|---|---|
| stars | count | '180' | The star count; density scales with canvas area so mobile stays calm. |
| z-speed | px/frame | '0.9' | How fast stars approach; a fraction of depth per frame, dt-normalised. |
| parallax | px | '± 26' | The eased vanishing-point shift with the mouse; stays zero for touch and keyboard. |
| size | formula | 'r ∝ 1/z' | Each star’s radius and brightness derive from depth; the near ones are fat and bright. |
Snippet
<section class="ambs" data-running="true">
<canvas class="ambs-cv"></canvas>
<h2>Nowhere is far anymore</h2>
<p>Red-eye flights to 42 destinations, from $99.</p>
</section>
<style>
.ambs { position: relative; overflow: clip; border-radius: 1.25rem; min-block-size: 22rem;
display: grid; place-content: center; text-align: center;
background: #05070f; color: #e2e8f0; }
.ambs-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; }
</style>
<script>
// Stars fly at the viewer on a z depth; this is the only JS part.
new Starfield(document.querySelector('.ambs'), { stars: 180, speed: .9 });
</script>
// app/Pages/Starfield.jsx — Inertia (React) page; renders the React
// snippet below unchanged, with Inertia's Link wired into the provider.
import { Link } from '@inertiajs/react';
import { NabuXUIProvider } from '@nabuxai/ui-react';
import StarfieldHero from '@/components/StarfieldHero';
export default function Starfield() {
return (
<NabuXUIProvider locale="en" linkComponent={Link}>
<main className="nx-page">
<StarfieldHero />
</main>
</NabuXUIProvider>
);
}
// StarfieldHero.jsx — canvas starfield: z-depth stars flying at the viewer
import { useEffect, useRef } from 'react';
import '@nabuxai/ui-core/css';
const STAR_COUNT = 180;
export default function StarfieldHero() {
const canvasRef = useRef(null);
useEffect(() => {
const canvas = canvasRef.current;
const ctx = canvas.getContext('2d');
if (!ctx) return;
const dpr = Math.min(devicePixelRatio || 1, 2);
let raf = 0, visible = true, w = 0, h = 0, last = performance.now();
const par = { tx: 0, x: 0, ty: 0, y: 0 };
let stars = [];
const resize = () => {
w = canvas.clientWidth; h = canvas.clientHeight;
canvas.width = w * dpr; canvas.height = h * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
stars = Array.from({ length: STAR_COUNT }, () => ({
x: (Math.random() - 0.5) * w * 2,
y: (Math.random() - 0.5) * h * 2,
z: Math.random() * w,
}));
};
const onMove = (e) => {
const r = canvas.getBoundingClientRect();
par.tx = ((e.clientX - r.left) / r.width - 0.5) * 26;
par.ty = ((e.clientY - r.top) / r.height - 0.5) * 26;
};
const draw = (now) => {
const dt = Math.min((now - last) / 16.6, 3); last = now;
par.x += (par.tx - par.x) * 0.06 * dt;
par.y += (par.ty - par.y) * 0.06 * dt;
ctx.fillStyle = '#05070f';
ctx.fillRect(0, 0, w, h);
const cx = w / 2 + par.x, cy = h / 2 + par.y;
for (const s of stars) {
s.z -= 0.9 * dt;
if (s.z <= 1) { s.z = w; s.x = (Math.random() - 0.5) * w * 2; s.y = (Math.random() - 0.5) * h * 2; }
const k = 0.5 / s.z, sx = cx + s.x * k * w, sy = cy + s.y * k * h;
const r = Math.max(0.4, (1 - s.z / w) * 2.2), a = Math.min(1, (1 - s.z / w) * 1.4 + .12);
ctx.beginPath(); ctx.arc(sx, sy, r, 0, 6.2832);
ctx.fillStyle = `rgba(226,232,240,${a})`; ctx.fill();
}
raf = requestAnimationFrame(draw);
};
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const io = new IntersectionObserver(([e]) => {
visible = e.isIntersecting;
cancelAnimationFrame(raf);
if (visible && !reduced.matches) raf = requestAnimationFrame((t) => { last = t; draw(t); });
}, { threshold: 0.15 });
resize();
io.observe(canvas);
canvas.addEventListener('pointermove', onMove);
if (reduced.matches) draw(performance.now()); // one still frame, loop parks
else raf = requestAnimationFrame((t) => { last = t; draw(t); });
const ro = new ResizeObserver(resize); ro.observe(canvas);
return () => {
cancelAnimationFrame(raf); io.disconnect(); ro.disconnect();
canvas.removeEventListener('pointermove', onMove);
};
}, []);
return (
<>
<style>{`
.ambs { position: relative; overflow: clip; border-radius: 1.25rem; min-block-size: 22rem;
display: grid; place-content: center; text-align: center; background: #05070f; color: #e2e8f0; }
.ambs-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; }
.ambs-copy { position: relative; padding: 2rem; }
`}</style>
<section className="ambs">
<canvas ref={canvasRef} className="ambs-cv" aria-hidden="true" />
<div className="ambs-copy">
<h2>Nowhere is far anymore</h2>
<p>Red-eye flights to 42 destinations, from $99.</p>
</div>
</section>
</>
);
}
<!-- StarfieldHero.vue — canvas starfield: z-depth stars flying at the viewer -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue';
import '@nabuxai/ui-core/css';
const STAR_COUNT = 180;
const canvas = ref<HTMLCanvasElement | null>(null);
let cleanup: (() => void) | null = null;
onMounted(() => {
const cv = canvas.value!;
const ctx = cv.getContext('2d');
if (!ctx) return;
const dpr = Math.min(devicePixelRatio || 1, 2);
let raf = 0, visible = true, w = 0, h = 0, last = performance.now();
const par = { tx: 0, x: 0, ty: 0, y: 0 };
let stars: { x: number; y: number; z: number }[] = [];
const resize = () => {
w = cv.clientWidth; h = cv.clientHeight;
cv.width = w * dpr; cv.height = h * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
stars = Array.from({ length: STAR_COUNT }, () => ({
x: (Math.random() - 0.5) * w * 2, y: (Math.random() - 0.5) * h * 2, z: Math.random() * w,
}));
};
const onMove = (e: PointerEvent) => {
const r = cv.getBoundingClientRect();
par.tx = ((e.clientX - r.left) / r.width - 0.5) * 26;
par.ty = ((e.clientY - r.top) / r.height - 0.5) * 26;
};
const draw = (now: number) => {
const dt = Math.min((now - last) / 16.6, 3); last = now;
par.x += (par.tx - par.x) * 0.06 * dt; par.y += (par.ty - par.y) * 0.06 * dt;
ctx.fillStyle = '#05070f'; ctx.fillRect(0, 0, w, h);
const cx = w / 2 + par.x, cy = h / 2 + par.y;
for (const s of stars) {
s.z -= 0.9 * dt;
if (s.z <= 1) { s.z = w; s.x = (Math.random() - 0.5) * w * 2; s.y = (Math.random() - 0.5) * h * 2; }
const k = 0.5 / s.z, sx = cx + s.x * k * w, sy = cy + s.y * k * h;
const r = Math.max(0.4, (1 - s.z / w) * 2.2), a = Math.min(1, (1 - s.z / w) * 1.4 + .12);
ctx.beginPath(); ctx.arc(sx, sy, r, 0, 6.2832);
ctx.fillStyle = `rgba(226,232,240,${a})`; ctx.fill();
}
raf = requestAnimationFrame(draw);
};
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const start = (t: number) => { last = t; draw(t); };
const io = new IntersectionObserver(([e]) => {
visible = e.isIntersecting;
cancelAnimationFrame(raf);
if (visible && !reduced.matches) raf = requestAnimationFrame(start);
}, { threshold: 0.15 });
resize(); io.observe(cv); cv.addEventListener('pointermove', onMove);
if (reduced.matches) draw(performance.now()); // one still frame
else raf = requestAnimationFrame(start);
const ro = new ResizeObserver(resize); ro.observe(cv);
cleanup = () => {
cancelAnimationFrame(raf); io.disconnect(); ro.disconnect();
cv.removeEventListener('pointermove', onMove);
};
});
onBeforeUnmount(() => cleanup?.());
</script>
<template>
<section class="ambs">
<canvas ref="canvas" class="ambs-cv" aria-hidden="true"></canvas>
<div class="ambs-copy">
<h2>Nowhere is far anymore</h2>
<p>Red-eye flights to 42 destinations, from $99.</p>
</div>
</section>
</template>
<style scoped>
.ambs { position: relative; overflow: clip; border-radius: 1.25rem; min-block-size: 22rem;
display: grid; place-content: center; text-align: center; background: #05070f; color: #e2e8f0; }
.ambs-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; }
.ambs-copy { position: relative; padding: 2rem; }
</style>
<!-- StarfieldHero.svelte — canvas starfield: z-depth stars flying at the viewer -->
<script lang="ts">
import { onMount } from 'svelte';
import '@nabuxai/ui-core/css';
const STAR_COUNT = 180;
let canvas: HTMLCanvasElement;
onMount(() => {
const ctx = canvas.getContext('2d');
if (!ctx) return;
const dpr = Math.min(devicePixelRatio || 1, 2);
let raf = 0, visible = true, w = 0, h = 0, last = performance.now();
const par = { tx: 0, x: 0, ty: 0, y: 0 };
let stars: { x: number; y: number; z: number }[] = [];
const resize = () => {
w = canvas.clientWidth; h = canvas.clientHeight;
canvas.width = w * dpr; canvas.height = h * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
stars = Array.from({ length: STAR_COUNT }, () => ({
x: (Math.random() - 0.5) * w * 2, y: (Math.random() - 0.5) * h * 2, z: Math.random() * w,
}));
};
const onMove = (e: PointerEvent) => {
const r = canvas.getBoundingClientRect();
par.tx = ((e.clientX - r.left) / r.width - 0.5) * 26;
par.ty = ((e.clientY - r.top) / r.height - 0.5) * 26;
};
const draw = (now: number) => {
const dt = Math.min((now - last) / 16.6, 3); last = now;
par.x += (par.tx - par.x) * 0.06 * dt; par.y += (par.ty - par.y) * 0.06 * dt;
ctx.fillStyle = '#05070f'; ctx.fillRect(0, 0, w, h);
const cx = w / 2 + par.x, cy = h / 2 + par.y;
for (const s of stars) {
s.z -= 0.9 * dt;
if (s.z <= 1) { s.z = w; s.x = (Math.random() - 0.5) * w * 2; s.y = (Math.random() - 0.5) * h * 2; }
const k = 0.5 / s.z, sx = cx + s.x * k * w, sy = cy + s.y * k * h;
const r = Math.max(0.4, (1 - s.z / w) * 2.2), a = Math.min(1, (1 - s.z / w) * 1.4 + .12);
ctx.beginPath(); ctx.arc(sx, sy, r, 0, 6.2832);
ctx.fillStyle = `rgba(226,232,240,${a})`; ctx.fill();
}
raf = requestAnimationFrame(draw);
};
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const start = (t: number) => { last = t; draw(t); };
const io = new IntersectionObserver(([e]) => {
visible = e.isIntersecting;
cancelAnimationFrame(raf);
if (visible && !reduced.matches) raf = requestAnimationFrame(start);
}, { threshold: 0.15 });
resize(); io.observe(canvas); canvas.addEventListener('pointermove', onMove);
if (reduced.matches) draw(performance.now()); // one still frame
else raf = requestAnimationFrame(start);
const ro = new ResizeObserver(resize); ro.observe(canvas);
return () => {
cancelAnimationFrame(raf); io.disconnect(); ro.disconnect();
canvas.removeEventListener('pointermove', onMove);
};
});
</script>
<section class="ambs">
<canvas bind:this={canvas} class="ambs-cv" aria-hidden="true"></canvas>
<div class="ambs-copy">
<h2>Nowhere is far anymore</h2>
<p>Red-eye flights to 42 destinations, from $99.</p>
</div>
</section>
<style>
.ambs { position: relative; overflow: clip; border-radius: 1.25rem; min-block-size: 22rem;
display: grid; place-content: center; text-align: center; background: #05070f; color: #e2e8f0; }
.ambs-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; }
.ambs-copy { position: relative; padding: 2rem; }
</style>