Back to the library

Night sky — moon phase

Variant: Moon phase

A synodic month at a day a second: 29.53s, a number chosen precisely because it will never land on a round minute with anything else on the page. The phase is four layers — two lit halves that swap at full, and a full-height ellipse squashed by the cosine of the phase, dark while it eats a crescent, lit while it fills toward gibbous, changing colour at the quarters where its width is zero. Its easing is not a taste call: cos(phase) is a sine-in from new to quarter and a sine-out from quarter to full, so the terminator crawls at new and full and races at the quarters, as it does. The dark limb is earthshine, never black, so a thin crescent still reads as a sphere. And the phase is not only on the disc — a full moon lifts a glow over the sky, all but erases the faint stars and dims the bright ones, one sine-in-out curve run in opposite directions. Use it where a long, slow cycle should be felt rather than read.

ambientloop

The source

A complete HTML document. Paste it into an empty .html file and it plays — no stylesheet, script, font, or image to fetch.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Night sky — moon phase</title>
    <style>
      :root {
        /* Neutrals — fixed, never remixed. */
        --ivory: #faf9f5;
        --paper: #ffffff;
        --oat: #e3dacc;
        --slate: #141413;

        /* Knobs — the remix API. A knob exists iff declared here and used below. */
        --hue: 38.8;
        --lift: 0;
        --chroma: 1;
        --round: 1;
        --speed: 1;

        /* Accents — derived from the knobs with the family's offsets baked in,
           so one hue roll moves them together. Defaults reproduce the original hex. */
        --clay: oklch(calc(0.6724 + var(--lift)) calc(0.1308 * var(--chroma)) var(--hue));
        --clay-d: oklch(calc(0.5797 + var(--lift)) calc(0.127 * var(--chroma)) var(--hue));
        --olive: oklch(
          calc(0.6118 + var(--lift)) calc(0.0713 * var(--chroma)) calc(var(--hue) + 88.3)
        );
        --rust: oklch(
          calc(0.5408 + var(--lift)) calc(0.1357 * var(--chroma)) calc(var(--hue) - 10.3)
        );

        /* Clock — one synodic month at a day a second. The odd number is the
           point: it will never land on a round minute with anything else on
           the page. */
        --cycle: 29.53s;
        --t: calc(var(--cycle) / var(--speed));

        /* The ground is a band the copy sits on, sized like the copy rather
           than like the panel, so the sky keeps whatever height is left. */
        --_ground: clamp(60px, 19vw, 80px);
        --_dot: 2.4px;
        --_moon: clamp(52px, 17vw, 74px);
        --_lit: color-mix(in oklab, var(--ivory) 80%, var(--oat));
        --_dark: color-mix(in oklab, var(--slate) 78%, var(--oat));

        /* The terminator's apparent width is the cosine of the phase angle,
           so between the quarters it is a sine curve, not a design choice:
           slow at new and full, fastest at the quarters. These are those
           curves, and their sum is the illuminated fraction the sky follows. */
        --_sine-in: cubic-bezier(0.12, 0, 0.39, 0);
        --_sine-out: cubic-bezier(0.61, 1, 0.88, 1);
        --_sine-in-out: cubic-bezier(0.37, 0, 0.63, 1);
      }

      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }

      body {
        min-height: 100vh;
        display: grid;
        place-items: center;
        background: var(--ivory);
        font-family:
          system-ui,
          -apple-system,
          'Segoe UI',
          Roboto,
          sans-serif;
        color: var(--slate);
      }

      /* Lifted off pure slate at the zenith and warmed toward the horizon, so
         the ground — which is pure slate — reads as nearer than the sky. */
      .panel {
        position: relative;
        overflow: hidden;
        width: min(392px, calc(100% - 26px));
        height: clamp(150px, 46vw, 214px);
        border-radius: calc(16px * var(--round));
        background: linear-gradient(
          to bottom,
          color-mix(in oklab, var(--slate) 93%, var(--ivory)) 0%,
          color-mix(in oklab, var(--slate) 84%, var(--clay-d)) 100%
        );
      }

      .sky {
        position: absolute;
        inset: 0 0 var(--_ground) 0;
      }

      .ground {
        position: absolute;
        right: 0;
        bottom: 0;
        left: 0;
        display: block;
        width: 100%;
        height: calc(var(--_ground) + 8px);
        fill: var(--slate);
      }

      .copy {
        position: absolute;
        right: clamp(16px, 4.6vw, 24px);
        bottom: clamp(12px, 3.8vw, 20px);
        left: clamp(16px, 4.6vw, 24px);
        color: var(--ivory);
      }

      .head {
        font-size: clamp(15px, 4.4vw, 19px);
        font-weight: 600;
        letter-spacing: -0.01em;
      }

      .sub {
        margin-top: 3px;
        font-size: clamp(11.5px, 3.3vw, 13px);
        color: color-mix(in srgb, var(--ivory) 58%, transparent);
      }

      /* One clock for every layer of the month, so reduced motion can stretch
         it in one place and nothing falls out of phase. */
      .sky {
        --_month: var(--t);
      }

      .stars {
        position: absolute;
        inset: 0;
        animation: washout var(--_month) var(--_sine-in-out) infinite;
      }

      .stars > i {
        position: absolute;
        top: var(--_y);
        left: var(--_x);
        width: calc(var(--_dot) * var(--_s));
        height: calc(var(--_dot) * var(--_s));
        border-radius: 50%;
        translate: -50% -50%;
        background: var(--ivory);
        opacity: var(--_b);
      }

      /* The phase is not only on the disc. A full moon lifts the whole sky and
         drowns the faint stars almost entirely and dims the bright ones; a new
         one gives them back. Glow and stars are one curve run in opposite
         directions. */
      .stars.faint {
        animation-name: washout-faint;
      }

      @keyframes washout-faint {
        0%,
        100% {
          opacity: 1;
        }
        50% {
          opacity: 0.08;
        }
      }

      @keyframes washout {
        0%,
        100% {
          opacity: 1;
        }
        50% {
          opacity: 0.55;
        }
      }

      .glow {
        position: absolute;
        top: 44%;
        left: 74%;
        width: calc(var(--_moon) * 4.4);
        aspect-ratio: 1;
        border-radius: 50%;
        translate: -50% -50%;
        background: radial-gradient(
          circle,
          color-mix(in oklab, var(--oat) 26%, transparent) 0%,
          transparent 62%
        );
        animation: glow var(--_month) var(--_sine-in-out) infinite;
      }

      @keyframes glow {
        0%,
        100% {
          opacity: 0;
        }
        50% {
          opacity: 1;
        }
      }

      /* The dark limb is never black — earthshine, sunlight bounced off the
         earth, keeps the whole disc faintly there, which is what lets the eye
         read a thin crescent as a sphere and not a sliver. */
      .moon {
        position: absolute;
        top: 44%;
        left: 74%;
        overflow: hidden;
        width: var(--_moon);
        aspect-ratio: 1;
        border-radius: 50%;
        translate: -50% -50%;
        background: var(--_dark);
      }

      .moon > span {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 50%;
        background: var(--_lit);
      }

      /* Waxing lights the right half, waning the left; each half is on for
         exactly half the month and swaps at full, where the terminator ellipse
         covers the seam. */
      .moon > .r {
        right: 0;
        animation: wax var(--_month) steps(1) infinite;
      }

      .moon > .l {
        left: 0;
        animation: wax var(--_month) steps(1) infinite;
        animation-delay: calc(var(--_month) / -2);
      }

      @keyframes wax {
        0% {
          opacity: 1;
        }
        50%,
        100% {
          opacity: 0;
        }
      }

      /* The terminator is a full-height ellipse squashed by cos(phase). It is
         dark while it eats a crescent out of the lit half and lit while it
         fills the dark half toward gibbous; colour changes at the quarters,
         where its width is zero and the swap cannot be seen. */
      /* A hair oversized, so at new moon it covers the lit half's antialiased
         rim instead of leaving a ring of light around a disc that should be
         gone. */
      .moon > .term {
        top: -2%;
        bottom: -2%;
        left: -2%;
        width: 104%;
        border-radius: 50%;
        animation:
          squash var(--_month) infinite,
          side var(--_month) steps(1) infinite;
      }

      @keyframes squash {
        0%,
        50% {
          transform: scaleX(1);
          animation-timing-function: var(--_sine-in);
        }
        25%,
        75% {
          transform: scaleX(0);
          animation-timing-function: var(--_sine-out);
        }
        100% {
          transform: scaleX(1);
        }
      }

      @keyframes side {
        0%,
        75%,
        100% {
          background: var(--_dark);
        }
        25% {
          background: var(--_lit);
        }
      }

      /* Maria sit above the phase, at low contrast, so they show on the
         earthshine side too — as they really do. Static: the moon keeps one
         face to us, and a texture that slid would say it was turning. */
      .maria {
        position: absolute;
        inset: 0;
        border-radius: 50%;
        background:
          radial-gradient(
            circle at 36% 38%,
            color-mix(in oklab, var(--slate) 13%, transparent) 0 14%,
            transparent 15%
          ),
          radial-gradient(
            circle at 58% 30%,
            color-mix(in oklab, var(--slate) 10%, transparent) 0 10%,
            transparent 11%
          ),
          radial-gradient(
            circle at 52% 58%,
            color-mix(in oklab, var(--slate) 9%, transparent) 0 12%,
            transparent 13%
          ),
          radial-gradient(
            circle at 50% 50%,
            transparent 58%,
            color-mix(in oklab, var(--slate) 16%, transparent) 100%
          );
      }

      @media (prefers-reduced-motion: reduce) {
        /* The month runs at half speed and nothing else changes. Every moving
           part here is already a scale or an opacity in one spot — the moon
           never travels — so slowing it is the whole adjustment. */
        .sky {
          --_month: calc(var(--t) * 2);
        }
      }
    </style>
  </head>
  <body>
    <div class="panel">
      <div class="sky" aria-hidden="true">
        <div class="stars">
          <i style="--_x: 36.3%; --_y: 81.7%; --_s: 0.96; --_b: 0.64"></i>
          <i style="--_x: 55.2%; --_y: 56.6%; --_s: 1.26; --_b: 0.58"></i>
          <i style="--_x: 67.5%; --_y: 91%; --_s: 1.06; --_b: 0.58"></i>
          <i style="--_x: 42%; --_y: 14.3%; --_s: 0.86; --_b: 0.6"></i>
          <i style="--_x: 53.9%; --_y: 84.2%; --_s: 1.08; --_b: 0.62"></i>
          <i style="--_x: 11%; --_y: 90.4%; --_s: 0.75; --_b: 0.56"></i>
          <i style="--_x: 33.5%; --_y: 45.1%; --_s: 1.08; --_b: 0.68"></i>
          <i style="--_x: 51.6%; --_y: 23.1%; --_s: 1.25; --_b: 0.64"></i>
          <i style="--_x: 36%; --_y: 59.1%; --_s: 0.96; --_b: 0.64"></i>
          <i style="--_x: 8.8%; --_y: 51.8%; --_s: 1.23; --_b: 0.73"></i>
          <i style="--_x: 47.4%; --_y: 45.1%; --_s: 1.19; --_b: 0.64"></i>
          <i style="--_x: 15.1%; --_y: 8.8%; --_s: 1.11; --_b: 0.69"></i>
          <i style="--_x: 46.1%; --_y: 75%; --_s: 1.22; --_b: 0.71"></i>
        </div>
        <div class="stars faint">
          <i style="--_x: 83.2%; --_y: 88.3%; --_s: 0.97; --_b: 0.54"></i>
          <i style="--_x: 15.8%; --_y: 31.1%; --_s: 0.63; --_b: 0.52"></i>
          <i style="--_x: 86.7%; --_y: 70%; --_s: 0.99; --_b: 0.49"></i>
          <i style="--_x: 80.9%; --_y: 13.8%; --_s: 0.95; --_b: 0.46"></i>
          <i style="--_x: 94.9%; --_y: 54.6%; --_s: 0.74; --_b: 0.45"></i>
          <i style="--_x: 62.8%; --_y: 72.7%; --_s: 0.93; --_b: 0.47"></i>
          <i style="--_x: 21.3%; --_y: 68%; --_s: 0.71; --_b: 0.55"></i>
        </div>
        <div class="glow"></div>
        <div class="moon">
          <span class="r"></span>
          <span class="l"></span>
          <span class="term"></span>
          <div class="maria"></div>
        </div>
      </div>
      <svg class="ground" viewBox="0 0 400 88" preserveAspectRatio="none" aria-hidden="true">
        <path d="M0 14 C 46 6, 92 4, 140 10 S 228 20, 276 12 S 360 2, 400 9 V 88 H 0 Z" />
      </svg>
      <div class="copy">
        <p class="head">A day a second</p>
        <p class="sub">Ambient sky. Nothing here reports state.</p>
      </div>
    </div>
  </body>
</html>

The variant family

Same idea, one axis moved. Compare them side by side before you commit.