Rolando Andrade
Back to projects

Built in 2019

Solar System

Solar System Simulation

Solar System preview

Solar System simulation using HTML Canvas.

Mercury goes around the sun in 88 days. Neptune takes 165 years. I wanted to watch that difference happen on a screen, not read it in a table.

Planets follow predictable math. Johannes Kepler worked this out in the 1600s. Planets move in ellipses and sweep equal areas in equal times. The farther a planet is from the sun, the slower it goes. These rules run our whole solar system.

My challenge was to turn orbital mechanics into a picture anyone could follow. The result is a browser simulation of all eight planets (plus Pluto) circling the sun in real time.

Eight planets and Pluto

The simulation draws the whole solar system on an HTML Canvas. Each planet orbits the sun at its correct relative speed. Mercury races. Neptune crawls. The proportions match reality.

There is more than planets. Hundreds of asteroids fill the main belt between Mars and Jupiter. Another group orbits past Neptune in the Kuiper belt. Five hundred stars sit in the background. Saturn even has its rings, drawn as circles inside circles.

Sistema Solar lab.rolandoandrade.me

Circles from sine and cosine

Each planet’s position comes from basic trigonometry. The x coordinate is the center plus the orbit radius times the cosine of an angle. The y coordinate uses the same formula with sine. As the angle grows, the planet traces a perfect circle.

The trick is that angular speed depends on the orbital period. Each planet has a fixed speed value based on how long its real orbit takes. Mercury moves about 0.07 radians per frame. Neptune moves 0.0001 radians per frame. That gap is the big speed difference you see on screen.

Every frame, the simulation does three things:

  1. Adds each planet’s speed to its angle
  2. Calculates the new x and y from that angle
  3. Clears the canvas and draws everything again

Painting in layers

HTML Canvas has a simple API for drawing shapes. The simulation uses two of them. Circles are for the sun, planets and asteroids. Rectangles are for the background and the stars.

A timer draws at about 125 frames per second. Each frame starts by covering the canvas with a dark gray rectangle. Then come the star field, the sun, the planets and both asteroid belts. The order matters. Stars go behind everything. The sun sits in the center. Planets and asteroids go on top.

The colors are close to the real planets. Earth is blue. Mars is red. Jupiter is a little yellow. Saturn and its rings share a cream color. You recognize them at once.

Shrinking billions of kilometers

The solar system is billions of kilometers wide. The canvas is 700 pixels. I used a scaling factor that keeps everything visible and keeps the relative sizes.

Planet radii are multiplied by 0.3. Jupiter stays large and Pluto stays tiny, but you can see them all. Orbit distances shrink much more. Otherwise the gaps between the outer planets would push them off the screen.

This trades accuracy for clarity. In reality, if Earth were one pixel, Neptune would orbit several screens away. The simulation puts what you can see first.

Two hundred rocks

Real asteroid belts hold millions of rocks. I used 100 per belt. That looks like a belt without slowing the page down.

Each asteroid gets random values. Its size varies a little around 0.8 pixels. Its distance from the sun varies inside a 10 pixel band. Its starting point is anywhere on the circle. The result looks messy in a natural way.

The main belt sits between Mars and Jupiter with a 5 year orbital period. The Kuiper belt lies past Neptune with a 200 year period. Each belt looks different on screen.

Five hundred fixed stars

The stars are tiny white rectangles at random positions. Five hundred of them fill the background without clutter. They stay still while the planets move past.

This small detail adds depth. Without stars, the animation feels flat. With them, space seems to go on forever behind the planets.

No libraries at all

The project uses no libraries or frameworks. No React. No Three.js. Only plain JavaScript drawing on a canvas. That forced me to understand every step.

Canvas drawing is easier than it looks. beginPath, arc and fill draw circles. fillRect draws rectangles. fillStyle changes the color. The browser does the low level graphics work.

The code is under 200 lines in three files. One handles shapes, one handles planet behavior and one runs the main loop. It is easy to read and easy to change.

Simple beats exact

Smooth animation needs steady timing. The 8 millisecond interval gives fluid motion. A longer interval makes the planets jump. A shorter one wastes CPU time.

Objects keep graphics organized. Each planet carries its own position, color, speed and orbit. The main loop only tells everything to move and draw.

A good simulation leaves things out. Perfect accuracy would need elliptical orbits, gravity between planets and true distances. None of that would show at this zoom level. The goal was to explain the solar system, not to measure it.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build