// devlog/002

Nine clocks and a bird

The phoenix in Emberbound has no animation frames. It's one drawing moved by nine timers, and the timers won't line up again for 166,361 years.

emberbound ~7 min read

There's a phoenix on the main screen of Emberbound. It sits in a card, flaps its wings, twitches its crest and throws off the odd spark. On a phone it's 60 by 65 points, so most people look at it for about a second and move on, which is roughly the right amount of time to spend on it.

This post spends a lot longer than that. Before you scroll down, have a guess at how many frames of animation it has.

Zero frames

It doesn't have any. There's no sprite sheet and no keyframes. The bird is a single drawing: 195 coloured rectangles on a 48 by 52 grid, split into 12 layers. Two of those layers, the body and the base of the tail, never move. The other ten are wings, crest tufts, tail feathers and three little groups of sparks.

What moves them is nine loops. Each loop counts from 0 to 1 over a fixed time, jumps back to 0 and starts again, forever. Every moving layer watches one of the loops, and when its loop passes certain values, the layer jumps to a different position.

bob.value    = loop(1900);
wings.value  = loop(1100);
crestA.value = loop(520);
crestB.value = loop(730);
tailA.value  = loop(830);
tailB.value  = loop(1130);
sparkA.value = loop(760);
sparkB.value = loop(1030);
sparkC.value = loop(1370);  // durations in ms

Here it is, redrawn for the browser from the same pixel data and the same timings. Each row underneath is one of the loops. The orange blocks show where its layer changes position, and the line moving across is where the loop is right now.

demo · nine clocks and a birdclick a row to stop that clock

Form 1 doesn't get a bird. Nothing to animate.

speed

phoenix form

    Every row is a button. Stop a few and the number under the bird drops. Stop all of them except wings and bob and you're down to 20.9 seconds.

    Snapping, not sliding

    Nothing on the bird ever moves smoothly. A wing frame is either showing or it isn't, and a spark is either in its spot or one pixel up and to the right. Everything moves in whole pixels and never stops anywhere in between.

    That's what keeps it looking like pixel art. If you slide a pixel-art layer from one spot to the next, it spends most of the trip at positions like 0.4 of a pixel, and the screen has to blur it to draw it there. The edges go soft and the whole thing starts to look like a vector drawing in a pixel-art costume. Snapping keeps every edge sharp.

    const crestAProps = useAnimatedProps(() => ({
      opacity: crestA.value >= 0.7 ? 0.8 : 1,
      transform: crestA.value < 0.4 ? "translate(0 0)"
               : crestA.value < 0.7 ? "translate(0 -1)"
               : "translate(1 0)",
    }));

    That's one of the crest tufts. It stays put for the first 40% of its loop, moves up a pixel, then moves right a pixel and goes slightly see-through. Every other layer works the same way with different numbers.

    The wings are the only part with real frames, and there are three of them: up, middle and down. In each 1.1 second beat they go up, middle, down, middle and back up, but the time isn't shared out evenly. The up frame is on screen 64% of the time, the middle one 24%, and the down one only 12%. That's why it reads as a quick, strong downstroke rather than a wing going up and down like a metronome. Slow the demo down to 0.1× and you can watch it happen.

    When does it repeat?

    Every loop repeats on its own schedule. The wings are back where they started every 1.1 seconds and the bob every 1.9. To get both of them back at the start at the same moment, you need the least common multiple of 1,100 and 1,900 milliseconds, which is 20,900, or 20.9 seconds. If you ever sat in a maths lesson wondering when you'd actually need LCM, it turns out the answer is a bird.

    Do the same for all nine loops and you get this:

    lcm(1900, 1100, 520, 730, 830, 1130, 760, 1030, 1370)
    // 5,249,970,002,505,800 ms
    // about 166,361 years

    It gets that big because the durations have almost no factors in common. They're all multiples of 10, but once you take that out, what's left contains 11, 13, 19, 73, 83, 103, 113 and 137, and nearly all of those are primes that only turn up once. Nothing cancels, so everything multiplies.

    So if you open the game and leave it running, the exact sequence you're watching comes back sometime around the year 168,000. Your phone won't last that long, and in practice neither will the sequence, because the animation starts over from zero every time the screen is opened again.

    The bad news

    Then I counted the poses, and it got a lot less impressive.

    Every loop has two or three states. Multiply them together and you get 2 × 3 × 3 × 2 × 3 × 3 × 3 × 3 × 3 = 8,748. That's every pose the bird can possibly be in.

    So I wrote a loop that walks through the animation one millisecond at a time and notes the moment each pose shows up for the first time. Below, every one of the 8,748 poses is a square. A square lights up the moment the bird first does that pose, and its colour shows whether that happened in the first minute, later in the first hour, or only after that. Press play, or drag the slider through four hours of bird time.

    demo · every pose the bird hasone square per pose

    0 / 8,748

    poses seen after 0s of bird time

    A square's colour tells you when the bird first did that pose:

    • in the first minute · 1,508 poses
    • later in the first hour · 7,099 poses
    • only after the first hour · 141 poses
    • hasn't happened yet at this point on the slider

    Click any square to see that pose here.

    Working out when each pose first shows up…

    After a minute the bird has shown 1,508 poses. After an hour it's 8,607, which is 98% of them. The other 141 take three more hours, and the very last one turns up at millisecond 14,443,462, or 4 hours and 43 seconds in.

    Click a few of the orange squares and you'll see why. Most of them have the wings down, which is only on screen 12% of the time, while the bird is bobbed up and the second crest tuft is shifted, each of which happens 30% of the time. That combination alone shows up about 1% of the time, and the other six layers still have to be in exactly the right states on the same millisecond.

    So the order never repeats, but if you start watching at noon, there's nothing new left to see by just after four. That's assuming you check every single millisecond. A real screen redraws about 60 times a second, so on an actual phone it would take somewhat longer, and a few of the rarest poses might never get drawn at all.

    One drawing, ten colours

    97 of the 195 rectangles don't have a fixed colour. They're marked as tint, and they take the colour of whatever form your phoenix is in at the moment. That way one drawing covers every form, and a new form only needs one extra colour in a list. You can try them with the swatches in the demo above.

    Form 1 doesn't have a colour in that list, so on form 1 there's no bird at all. It's the cheapest form to animate by a long way.

    If you'd rather it stood still

    The bird is decoration, so screen readers skip it completely. Every loop is also set to follow the phone's reduce motion setting, and I went digging through the animation library's source to find out what that actually does. As far as I can tell, each loop jumps straight to its end and stops there. So with reduced motion on, the bird picks one pose and holds it, like it's waiting for someone to take a photo.

    This page does the same. If your system asks for reduced motion, the demo starts on that pose and waits for you to press play.

    In total

    The phoenix is 195 rectangles, nine timers and a few dozen comparisons. It has 8,748 poses, shows you all of them in about four hours, and won't show them in the same order again for 166,361 years. It's 60 points wide and sits in the corner of a card.

    If you've read this far, you've looked at it for longer than anyone was ever supposed to.