Visualizations
173 / 500

173 · Astronomy

Spiral Galaxy

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.

  • Density wave theory
  • Elliptical orbits
  • Additive glow

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

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.
PreviousMinnaert RainWatercolor rain whose sound is built from first principles: ringing bubbles, not samples. NextBall PitHundreds of colorful balls tumble, stack and bounce off each other.

Related visualizations

  • Dark Matter CurveAstronomy Switch off the dark halo and watch a spiral galaxy fly apart.
  • Galaxy CollisionAstronomy Two spiral galaxies collide and fling out long tidal tails.
  • Star TrailsAstronomy A long exposure builds over a mountain camp as the sky wheels around the celestial pole.
  • Lensing ClusterAstronomy Drag a galaxy cluster and its gravity bends a deep field into giant arcs.
  • RingmakerAstronomy Backlit planetary rings with shepherd moons, wavy gap edges, propellers and waves.
  • Stellar VampireAstronomy A star overflows its Roche lobe and feeds a white dwarf's disk until it ignites a nova.
  • Star Cluster AgesAstronomy A star cluster and its HR diagram age side by side across 13 billion years.
  • Earth-Rotation SynthesisAstronomy Radio dishes ride the turning Earth to image a hidden galaxy, then CLEAN it.
  • Prime SpiralsMath A million integers on Ulam and Sacks spirals, where prime-rich polynomials glow as rays.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs