Live preview · fully interactive
The prompt
Build a forward-moving starfield. ## Look 120 square stars in #f2f2f7 on #08080d. Size and alpha grow as z approaches the camera. ## Behavior Decrease z by 4 each frame. When z drops to 1 or below, respawn the star. Project with perspective 180. ## Design tokens - Stars rgba(242,242,247, alpha); clear color #08080d. ## Motion - Speed 4px of z per frame. Perspective divisor 180. ## Constraints - Single self-contained HTML file. No libraries. Canvas 2D. - Honor prefers-reduced-motion: reduce by skipping the loop and leaving a readable static frame - Readable at 390px width with no horizontal page scroll
The code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { min-height: 100%; }
body {
background: #08080d; color: #f2f2f7; font-family: Inter, system-ui, sans-serif;
min-height: 100vh; overflow: hidden;
}
.hint { position: absolute; left: 16px; top: 16px; z-index: 5; font-size: 11px; letter-spacing: 0.14em; text-transform: uppercase; color: #8a8a99; }
canvas { position: fixed; inset: 0; width: 100%; height: 100%; background: #08080d; }
@media (max-width: 420px) {
.hint { font-size: 10px; letter-spacing: 0.08em; }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
var reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
var stars = [];
function resize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }
resize();
function make() {
return { x: (Math.random() - 0.5) * canvas.width, y: (Math.random() - 0.5) * canvas.height, z: Math.random() * canvas.width };
}
for (var i = 0; i < 120; i++) stars.push(make());
function frame() {
ctx.fillStyle = "#08080d";
ctx.fillRect(0, 0, canvas.width, canvas.height);
var cx = canvas.width / 2, cy = canvas.height / 2;
stars.forEach(function (s) {
if (!reduce) s.z -= 4;
if (s.z <= 1) Object.assign(s, make());
var k = 180 / s.z;
var px = cx + s.x * k, py = cy + s.y * k;
var size = Math.max(0.4, (1 - s.z / canvas.width) * 2.4);
ctx.fillStyle = "rgba(242,242,247," + (1 - s.z / canvas.width) + ")";
ctx.fillRect(px, py, size, size);
});
if (!reduce) requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>About this effect
A canvas starfield of 120 points moves on Z and projects to 2D. Faster stars are larger and brighter. Suits space, science, and night themes.