From 141 lines to 28: trimming a moon-orbit keyframe animation

A portfolio rebuild reused a set of CSS planet illustrations and an orbiting-moon animation originally written in 2019. The code was dropped into the new site with only one change: em units were converted to viewport units so the animation would scale. On mobile, the planet appeared to shift a few pixels up and down while the moons orbited — a suspected animation bug that turned out not to be the cause, but the investigation still produced a much smaller animation.

Cutting the duplicated keyframe blocks

The original moon animation ran for 60 seconds, and its keyframes at 0%–20% were identical to those at 20%–40%, and so on through 100%. Repeating one set of keyframes infinitely achieves the same result, and the duration can drop to 12 seconds, one-fifth of 60. Deleting everything from 20% onward takes the code from 141 lines to 36.

#moon1 {
  animation: moon-one 12s infinite;
}

@keyframes moon-one {
  0% {
    transform: translate(0, 0) scale(1);
    z-index: 2;
    animation-timing-function: ease-in;
  }
  5% {
    transform: translate(-3.51217391vw, 3.50608696vw) scale(1.5);
    z-index: 2;
    animation-timing-function: ease-out;
  }
  9.9% {
    z-index: 2;
  }
  10% {
    transform: translate(-5.01043478vw, 6.511304348vw) scale(1);
    z-index: -1;
    animation-timing-function: ease-in;
  }
  15% {
    transform: translate(1.003478261vw, 2.50608696vw) scale(0.25);
    z-index: -1;
    animation-timing-function: ease-out;
  }
  19.9% {
    z-index: -1;
  }
  20% {
    transform: translate(0, 0) scale(1);
    z-index: 2;
    animation-timing-function: ease-in;
  }
}

Rescaling the keyframe percentages

What remains is five main keyframes plus two extra ones that set z-index near the middle and end of the cycle; these keep the moon from dropping behind the planet or emerging from behind it too early. The five points can be rewritten from 0%, 5%, 10%, 15% and 20% to 0%, 25%, 50%, 75% and 100%. Since the original 0% and 20% frames were the same, one can be dropped. The 9.9% frame becomes 49.9%, and 19.9% becomes 99.9%.

#moon1 {
  animation: moon-one 12s infinite;
}

@keyframes moon-one {
  0%, 100% {
    transform: translate(0, 0) scale(1);
    z-index: 2;
    animation-timing-function: ease-in;
  }
  25% {
    transform: translate(-3.51217391vw, 3.50608696vw) scale(1.5);
    z-index: 2;
    animation-timing-function: ease-out;
  }
  49.9% {
    z-index: 2;
  }
  50% {
    transform: translate(-5.01043478vw, 6.511304348vw) scale(1);
    z-index: -1;
    animation-timing-function: ease-in;
  }
  75% {
    transform: translate(1.003478261vw, 2.50608696vw) scale(0.25);
    z-index: -1;
    animation-timing-function: ease-out;
  }
  99.9% {
    z-index: -1;
  }
}

Rounding values and removing no-op declarations

Values such as -3.51217391vw carry far more precision than needed — at 1000px wide that is 35.1217391 pixels, well past any useful resolution. Rounding to one decimal place (and dropping trailing 0) is enough. The z-index declarations in the 25% and 75% keyframes can also go, since the value does not change there.

#moon1 {
  animation: moon-one 12s infinite;
}

@keyframes moon-one {
  0%, 100% {
    transform: translate(0, 0) scale(1);
    z-index: 2;
    animation-timing-function: ease-in;
  }
  25% {
    transform: translate(-3.5vw, 3.5vw) scale(1.5);
    z-index: 2;
    animation-timing-function: ease-out;
  }
  49.9% {
    z-index: 2;
  }
  50% {
    transform: translate(-5vw, 6.5vw) scale(1);
    z-index: -1;
    animation-timing-function: ease-in;
  }
  75% {
    transform: translate(1vw, 2.5vw) scale(0.25);
    z-index: -1;
    animation-timing-function: ease-out;
  }
  99.9% {
    z-index: -1;
  }
}

Separating translate and scale

One weakness of the original animation: the moon pivots at its zenith as it crosses the planet, where a straight path from upper right to lower left and a slight enlargement — as if the moon were approaching the viewer — would look better. Because translation and scaling both lived in the transform property, they could not be controlled independently; omitting either one resets it, forcing a guessed mid-point just to hit the required scale. A wrapping element with scale on one node and translate on the other was the old workaround. With individual scale and translate properties available, the two can be split out of transform instead. This changes nothing visually unless the original declaration order on transform differed from the order of the individual properties.

#moon1 {
  animation: moon-one 12s infinite;
}

@keyframes moon-one {
  0%, 100% {
    translate: 0 0;
    scale: 1;
    z-index: 2;
    animation-timing-function: ease-in;
  }
  25% {
    translate: -3.5vw 3.5vw;
    z-index: 2;
    animation-timing-function: ease-out;
  }
  49.9% {
    z-index: 2;
  }
  50% {
    translate: -5vw 6.5vw;
    scale: 1;
    z-index: -1;
    animation-timing-function: ease-in;
  }
  75% {
    translate: 1vw 2.5vw;
    scale: 0.25;
    animation-timing-function: ease-out;
  }
  99.9% {
    z-index: -1;
  }
}

Once the properties are separate, the translate declaration can be removed from the 25% and 75% keyframes entirely — those points no longer need to be pinned, and browser interpolation handles the translation smoothly alongside the scaling.

#moon1 {
  animation: moon-one 12s infinite;
}

@keyframes moon-one {
  0%, 100% {
    translate: 0 0;
    scale: 1;
    z-index: 2;
    animation-timing-function: ease-in;
  }
  25% {
    scale: 1.5;
    animation-timing-function: ease-out;
  }
  49.9% {
    z-index: 2;
  }
  50% {
    translate: -5vw 6.5vw;
    scale: 1;
    z-index: -1;
    animation-timing-function: ease-in;
  }
  75% {
    scale: 0.25;
    animation-timing-function: ease-out;
  }
  99.9% {
    z-index: -1;
  }
}

Simplifying the timing functions

With the browser doing the interpolation work, the assortment of timing functions no longer makes sense. A single ease-in-out applied throughout produces the intended motion.

#moon1 {
  animation: moon-one 12s infinite ease-in-out;
}

@keyframes moon-one {
  0%, 100% {
    translate: 0 0;
    scale: 1;
    z-index: 2;
  }
  25% {
    scale: 1.5;
  }
  49.9% {
    z-index: 2;
  }
  50% {
    translate: -5vw 6.5vw;
    scale: 1;
    z-index: -1;
  }
  75% {
    scale: 0.25;
  }
  99.9% {
    z-index: -1;
  }
}

The result is 28 lines instead of 141, an animation that arguably looks better than the original, and code that is considerably easier to maintain.