Skip to content
The Moonbase
DE

Dev log: The Moonbase

How the moon phases and constellations on my website are made

Calculating the moon phase for a given date and generating a unique constellation from a name

Contents3 sections

On the homepage, you’ll find a moon next to some articles and a constellation on some topics and projects. They aren’t made by hand, and they aren’t completely random either. The moon shows how it actually looked in the sky on the day the article was published, and every constellation belongs to exactly one name. In this article, I’ll show how that works and how I built it into Moonbase.

The moon as a preview image#

Why the moon has phases#

The moon doesn’t shine on its own. It’s lit by the sun, which always illuminates one half of its surface. It’s the same on Earth, where one half is always in daylight. What changes is our point of view: as the moon orbits the Earth, we see more or less of its lit side.

  • When the moon is between the Earth and the sun, we’re looking at its dark side. That’s the new moon.
  • When it’s on the far side of the Earth, we see the entire lit half, so it’s a full moon.
  • In between are the waxing and waning moon, with the crescent, the half moon, and the slightly chubby gibbous moon.

One lunar cycle

From above
From Earth

Waxing crescent

On the left, the sun, Earth, and moon seen from above. The bright line on the moon marks the half facing Earth. On the right, you can see how much of it is lit.

One cycle takes 29.53 days#

On average, 29.530588 days pass from one new moon to the next. This is called the synodic month. It’s longer than the time the moon needs for one lap around the Earth (about 27.3 days). The reason: while the moon orbits the Earth, the Earth keeps moving around the sun. So the moon has to travel a bit further until the sun, Earth, and moon are lined up the same way again.

Once you know that, all you need for the moon phase is a starting point, meaning any new moon whose exact time you know. I’m using the one on January 6, 2000 at 18:14 UTC, which is commonly used as a reference for this kind of calculation.

From date to moon phase#

The calculation is pretty short:

  1. How many days are there between the reference new moon and the date?
  2. How many days have passed since the last new moon? To find out, divide by 29.53 and take the remainder (modulo).
  3. And what fraction of the cycle is that? Divide that remainder by 29.53 once more, and you get a number between 0 and 1.
ts
const reference = Date.UTC(2000, 0, 6, 18, 14)
const synodicMonth = 29.530588

export function moonPhase(date: Date): number {
  const days = (date.getTime() - reference) / 86_400_000
  return (((days % synodicMonth) + synodicMonth) % synodicMonth) / synodicMonth
}

0 is the new moon, 0.25 the first quarter (waxing half moon), 0.5 the full moon, 0.75 the last quarter (waning half moon), and at 1 it starts over again.

How much of the moon is lit?#

When you hover over the moon, you’ll see what percentage of it is lit. The value comes from this formula:

ts
export function illumination(phase: number): number {
  return Math.round(((1 - Math.cos(2 * Math.PI * phase)) / 2) * 100)
}

The cosine turns the phase into a smooth wave: 0% at the new moon, 100% at the full moon, and exactly 50% at the half moons. Around the full moon, the moon barely changes for days, while around the half moons it changes quite quickly.

Illumination over one cycle

Lit: 50%

Change: +10.6 percentage points per day

The curve shows what percentage of the moon is lit on each day. The change per day is largest around the half moons and almost zero around the new and full moon.

Drawing the moon phase as an SVG#

Now we know how much is lit, but not yet what the lit area looks like. For that, we need to distinguish between the outer edge of the moon and the terminator, the line between light and shadow.

  • The outer edge: It always stays the same. Depending on the phase, either the right or the left side is lit. In the Northern Hemisphere, the right side is typically lit while the moon is waxing, and the left side while it’s waning.
  • The terminator: It separates the lit area from the unlit one. Depending on the phase, it appears as a straight line or as a curved line that bulges inward or outward.

The width of this ellipse is once again a cosine, |cos(2π · phase)| · radius. Whether it bulges inward (crescent) or outward (gibbous) depends on whether the phase is already past the half moon.

In SVG, that’s two arcs in one path:

ts
export function litPath(phase: number, r = 40): string {
  const rx = (Math.abs(Math.cos(2 * Math.PI * phase)) * r).toFixed(2)
  const waxing = phase <= 0.5
  const sweep = waxing ? (phase < 0.25 ? 0 : 1) : phase > 0.75 ? 1 : 0
  return `M0 ${-r}A${r} ${r} 0 0 ${waxing ? 1 : 0} 0 ${r}A${rx} ${r} 0 0 ${sweep} 0 ${-r}Z`
}

The path starts at the top of the moon, follows a semicircle down, and returns to the top along the ellipse. Put a dark circle underneath, and the moon is done. All of this happens at build time, so the browser only receives a finished SVG.

Two arcs, one moon

M0 -40A40 40 0 0 1 0 40A12.36 40 0 0 1 0 -40Z
  • Outer edge
  • Terminator
The dashed ellipse shows the full width of the terminator. Below it is the path that litPath generates for this phase.

A constellation for every name#

Topics and projects that don’t have an image I’ve picked show a constellation instead. My requirements were:

  • Every topic should have a different constellation.
  • The same topic should always have the same constellation, no matter when or how often the site is built.
  • I don’t want to have to configure anything by hand.

That rules out true randomness. With Math.random(), the constellation would look different on every build. What I need is something that looks random but always turns the same input into the same output.

Randomness that isn’t#

That’s exactly what a hash function does. I use SHA-256. You put in some text, for example the slug keyboards, and get 32 bytes back, which means 32 numbers between 0 and 255.

  • Same text, same numbers. Always.
  • The numbers look randomly distributed, with no recognizable connection to the text.

These 32 numbers are the basis for the constellation.

From text to constellation

SHA-256 as 32 bytes
  1. ·
  2. ·
  3. ·
  4. ·
  5. ·
  6. ·
  7. ·
  8. ·
  9. ·
  10. ·
  11. ·
  12. ·
  13. ·
  14. ·
  15. ·
  16. ·
  17. ·
  18. ·
  19. ·
  20. ·
  21. ·
  22. ·
  23. ·
  24. ·
  25. ·
  26. ·
  27. ·
  28. ·
  29. ·
  30. ·
  31. ·
  32. ·
  • Number of stars
  • Positions
  • Stardust
Same calculation as on the homepage. Change a single letter, and all 32 bytes, and with them the constellation, jump around.

Turning bytes into stars#

Let’s look at the code that turns the hash into a constellation:

ts
const hash = createHash('sha256').update(seed).digest()
const count = 5 + (hash[0] % 3)
const stars = Array.from({ length: count }, (_, i) => [
  20 + (hash[1 + 2 * i] / 255) * (width - 40),
  15 + (hash[2 + 2 * i] / 255) * (height - 30),
]).sort((a, b) => a[0] - b[0])

Step by step:

  1. How many stars? The first byte modulo 3 gives 0, 1, or 2. Add 5, and every constellation ends up with 5 to 7 stars.
  2. Where do they go? For each star, I take two bytes, one for the horizontal (x) and one for the vertical (y) position. Dividing by 255 gives a number between 0 and 1, which is then stretched across the area. A margin keeps the stars from sitting right on the edges and getting cut off.
  3. How are they connected? The stars are sorted from left to right and then connected with lines in order. This creates a zigzag path that, to me, looks like a constellation. Without the sorting, the lines would crisscross all over the place.

On top of that, there are nine tiny dots of “stardust” taken from more bytes, and every third star is drawn a little larger so they don’t all look the same.

So the same seed always produces the same constellation. That gives every topic its own visual identity without me having to create or store images by hand.

Why all this?#

Because it was fun. A generic placeholder image would have done the job, but this way every article has a small connection to the day it was published, and every topic has its own identity, without much effort on my part.

And since everything is calculated at build time, the browser doesn’t need any extra JavaScript or images to load, just a few lines of SVG.

I hope I managed to explain clearly how it all works. Maybe you feel like building something similar for your own project.