DEV Community

Cover image for Your GIF Has a Rhythm. Your Game Engine Has a Metronome.
Crane Zhou
Crane Zhou

Posted on Fully Autonomous

Your GIF Has a Rhythm. Your Game Engine Has a Metronome.

You find the perfect little GIF. A slime that squashes, winds up, holds for a beat at the top of its stretch, then hops and lands with a satisfying pause before going again.

You convert it to a sprite sheet, set it to 10 frames per second, and the slime now hops like it's late for a train. The hold is gone. The landing pause is gone. Every frame is correct and the animation is wrong.

Here's all three side by side. Left is the GIF as a browser plays it. Middle is what you get after a naive conversion. Right is the fix.

Three slimes. The left one pauses before jumping and after landing. The middle one cycles through the same frames with no pauses and finishes early. The right one matches the left exactly

Watch the middle one drift out of sync and lap the others. It finishes its loop in 0.8 seconds. The original takes 1.5.

What the GIF knew that the sheet doesn't

In a GIF, timing is per frame. Every frame carries its own "show me for this long" value. This slime's eight frames are:

100 · 100 · 100 · 600 · 100 · 100 · 100 · 300   (ms)
Enter fullscreen mode Exit fullscreen mode

That 600 is the wind-up hold. The 300 at the end is the landing. An animator made those two frames slow on purpose. It's what gives the motion weight: anticipation before the jump, settle after it. Take them out and you have eight pictures of a slime in a hurry.

A sprite sheet is just an image. It has no timing in it at all. The timing lives wherever you play it, and an awful lot of places that play sprite sheets take exactly one number: frames per second. A CSS steps() animation. A flipbook node in a shader graph. A particle system's texture animation. A simple frame = floor(time * fps) % count in your own code.

One speed, eight frames, 100 ms each. The 600 ms hold becomes 100 ms and the rhythm is flattened.

The fix is embarrassingly literal

If the player can only show every cell for the same length of time, then a frame that should last six times longer has to be six cells.

Top: the 8-frame sheet. Bottom: a 15-cell sheet where the fourth frame is repeated six times and the last frame three times

Eight frames become fifteen cells. The hold frame appears six times in a row, the landing frame three times. Play that at a flat 10 fps and you get the original timing back exactly: six cells × 100 ms = 600 ms.

It feels like cheating. It is also precisely what traditional animators do. Film runs at a fixed 24 frames a second, and a drawing that should stay on screen longer is simply photographed for more frames. They call it shooting "on twos" or "on threes." A hold is the same drawing, exposed again and again.

Finding the step

The only real question is how long one cell should be. Too long and you can't represent the short frames. Too short and the sheet balloons.

The right answer is the largest duration that divides every frame's delay evenly, which is the greatest common divisor:

function evenTiming(frames, maxCells = 256) {
  // GIF delays are in hundredths of a second, so work in those units
  const units = frames.map((f) => Math.max(1, Math.round(f.delay / 10)));
  const gcd = (a, b) => (b ? gcd(b, a % b) : a);
  let step = units.reduce(gcd);

  const total = () =>
    units.reduce((a, u) => a + Math.max(1, Math.round(u / step)), 0);
  while (total() > maxCells) step += 1; // coarsen if it gets silly

  return frames.flatMap((f, i) =>
    Array.from({ length: Math.max(1, Math.round(units[i] / step)) },
      () => ({ ...f, delay: step * 10 }))
  );
}
Enter fullscreen mode Exit fullscreen mode

For our slime the delays in hundredths are 10, 10, 10, 60, 10, 10, 10, 30. Their GCD is 10, so one cell is 100 ms, and the counts are 1, 1, 1, 6, 1, 1, 1, 3. Fifteen cells, played at 10 fps.

When the numbers are ugly

That worked out nicely because I picked nice numbers. Real GIFs aren't always so polite. Take delays of 140, 90, 70, 70, 80, 80, 70, 110 ms. Nothing wrong with them. Their GCD is 10 ms.

So one cell is 10 ms, and the sheet needs 14 + 9 + 7 + 7 + 8 + 8 + 7 + 11 = 71 cells at 100 fps to represent eight drawings. Exact, and a bit ridiculous.

That's what the while loop is for. If the exact answer needs more cells than is sane, the step gets coarser until it fits, and each frame is rounded to the nearest whole number of cells. You trade a few milliseconds of accuracy for a sheet you can actually use. Nobody can see the difference between a 140 ms frame and a 150 ms one. Everybody can see a missing 600 ms hold.

Or keep the timing as data

Repeating cells is the universal fix, because it works anywhere that can play a sheet at all. But it costs texture space, and if your target can read per-frame durations, you don't need it.

Some can. Godot's SpriteFrames resource stores a duration per frame. The JSON that Aseprite exports has a duration for every frame, and Phaser's Aseprite loader uses it. If you're going somewhere like that, export the 8-cell sheet plus the data file, and let the engine keep the rhythm.

The decision is really just this:

  • Player takes one fps number → repeat cells so the sheet carries the timing.
  • Player reads per-frame durations → keep the sheet small, ship the durations alongside.

GIF to Sprite Sheet notices when a GIF's delays aren't all the same and offers the even-timing version, and it writes the durations into the JSON and the Godot export either way.

The thing I'd check first, next time an imported animation "feels off" and you can't say why: open the source GIF and look at the delays. If they aren't all identical, the feeling you lost is in the numbers you threw away.

What's an animation you've seen ruined by perfectly even timing?

Top comments (0)