Tens of thousands of stars wheel through glowing spiral arms.
The spiral arms are a density wave, not solid structures. Every star travels on an ellipse, and each successive ellipse is rotated a little more than the one inside it. Where the ellipses crowd together, arms appear. Stars are drawn with additive blending over a soft dust lane and a bright core, and the disc is tilted for perspective.
Try it. Drag to tilt and turn the galaxy. Scroll or pinch to zoom.
Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.
Build an animated spiral galaxy with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.
Start simple:
- Make a canvas that fills the window and resizes with it, on a near-black background with a few hundred faint background stars.
- Create about 30,000 stars. Give each a radius r between 0 and 1 (more of them near the center) and a random angle along its orbit.
- Here is the key idea, density wave theory: every star moves on an ellipse, not a circle. Make the ellipse's short axis about 0.7 times its long axis, and rotate each ellipse by an angle proportional to its radius (about 4 radians from center to edge). Where neighboring ellipses crowd together, two spiral arms appear on their own.
- Each frame, advance every star along its ellipse, faster near the center, and draw it as a tiny dot.
Once that works, make it beautiful:
- For speed, write the stars into Float32Array color buffers yourself (one per channel), adding their light so dense areas glow, then tone map with 1 - exp(-value) into an ImageData and putImageData once per frame.
- Color the core warm yellow and add bright blue young stars and a few pink nebulae that are born in the arms and fade as they drift out. Draw a downscaled, blurry copy of the image back on top with "lighter" blending for bloom.
- Tilt the disc in 3D: give stars a small height, rotate around the x axis, and let me drag to tilt and use the mouse wheel to zoom.
Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as dark dust lanes, a colliding second galaxy, or a barred spiral.