·10 min read

Game juice in React: sequencing animations

My puzzle's lift, landing and row celebration kept overlapping. Chaining every beat as a promise fixed it, and frame timings showed what it cost.

gamedevreactjavascriptdesign

Contents

ASMR Logo Craft Block is a sorting puzzle: 32 picture tiles on a four-column board, eight hidden groups of four. You drag a tile onto another to swap them, and a row of four from one group lights up, turns into a banner and leaves the board. It is React, Vite and Capacitor, with no game engine.

The game played correctly from the first build. It did not feel like anything. Adding the game juice — the lift, the landing, the celebration — took several rounds, and almost every problem in them was the same one: two effects running at once that should have run one after the other. This is how the animation sequencing ended up, what I tried first, and the numbers I measured on the way.

The tile that flew in from the wrong row

The first complaint was mine. Drag a tile from row 1 to row 5, let go, and it played a swap animation from row 1, as if it had never been under my finger.

The board used a FLIP layout animation: after each change, compare every tile's slot with its previous slot and animate the difference. For the tile I dropped, the previous slot was row 1, so that is where it flew in from. The answer was to remember where the dragged copy was drawn at the moment of release — its position, its size and its tilt — and start the landing from there. The tile it displaced still slides from its old slot, because for that one the old slot is the truth.

In the test that followed, a tile dropped on row 5 went straight from the release point into its slot and never passed through row 1. The row check also had to wait for the landing: it used to fire 150 ms after a swap, which collapsed solved rows while the tile was still in the air. I moved it to 360 ms, which was still a guess about how long another animation took.

Top-down, not side-on

Then the landing needed weight. My first attempt squashed the block against its bottom edge and puffed dust out to the left and right. In a headless browser its height went 62 → 56 → 65 → 62 px over about 300 ms, which is a textbook squash and stretch.

It was the wrong one. The board is seen from above. A picked-up block is lifted towards the player, so it lands on its back face and presses into the board; nothing in this game has a floor under it. Squashing against the bottom edge is a side-view platformer effect.

The top-down version keeps the block scaled up with a big soft shadow for the whole glide, then brings it down to slightly smaller than its resting size, with the shadow collapsing to a tight contact shadow, before it springs back. On the first version of that I measured widths of 83 px lifted, 71 pressed, 76 on the rebound and 75 at rest. Dust bursts out from under all four edges instead of two. The keyframes ended up like this (simplified from the game's code):

// A top-down landing; offsets are fractions of a 400 ms animation
el.animate([
  { transform: 'scale(1.1)',   filter: LIFTED_SHADOW },              // still in the air
  { transform: 'scale(0.915)', filter: CONTACT_SHADOW, offset: 0.3 }, // contact
  { transform: 'scale(0.915)', filter: CONTACT_SHADOW, offset: 0.42 },// hold the press
  { transform: 'scale(1.025)', offset: 0.72 },                        // rebound
  { transform: 'scale(1)' },
], { duration: 400 });

The two identical frames are a hit-stop: the block holds the press for about 50 ms, which is most of what makes it read as heavy. The thump sound and the haptic fire at contact — 30% of the way in — not on release, and not at the end of the glide.

Sequencing animations with promises

With the move feeling right, the row celebration was still a mess. The landing, the row removal, the banner, the success sound and the win screen were each started by their own timer or their own useEffect watching the store. The store itself solved rows on timers: one after each swap, and another 300 ms after each solved row to look for the next. Every timer was a guess about how long some other animation took, and each new effect made one of the guesses wrong. Effects overlapped and arrived out of order.

The options were to keep tuning the timers, or to stop guessing. Tuning would have worked until the next effect changed a duration. So the store stopped solving rows on its own. It now answers two questions — which rows are solved, and commit this one — and the board decides when.

Every effect returns a promise that resolves when its animation ends. The Web Animations API already gives you that in animation.finished; the only care needed is that a cancelled animation rejects, and a cancelled effect should still let the sequence move on. A simplified version of the loop:

const done = (anim) => anim.finished.then(() => {}, () => {}); // finished or cancelled
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
const within = (p, ms) => Promise.race([p, wait(ms)]);          // never hang the board

async function afterDrop(landings) {
  setBoardBusy(true);
  await within(Promise.all(landings), 2000);  // every block has touched down
  let row;
  while ((row = findSolvedRow())) {
    await within(lightUp(row), 2000);         // tiles light one by one, 85 ms apart
    await within(fuse(row), 800);             // squeeze into one bar
    commitRow(row);                           // the state changes only now
    await within(bannerArrives(row), 1500);   // flight, confetti, settle
    await wait(90);
  }
  setBoardBusy(false);                        // the win screen waits for this flag
}

within is there because a promise can stall: an app sent to the background stops running its animations, and without a ceiling the board would stay locked until it came back.

A solved row now plays as four beats. The tiles light up in the group's colour one at a time, each with a sparkle and a chime climbing C–E–G–C. They fuse into a bar, the bar becomes the banner and flies to its place while the other rows slide down, and confetti goes off when it lands. If the last row is solved automatically, it gets the same beats, and only after it does the win sound play and the win screen open. From one recorded run: release at 849 ms, all four tiles lit at 1,520, row committed at 2,409, board idle at 3,367.

Three bugs only the measurement found

I had Claude Code write a Playwright script that drives the game in headless Chromium at a phone-sized viewport, records the screencast, logs each beat, and watches the frame rate with a probe in the page:

let last = performance.now();
window.__gaps = [];
(function tick() {
  const now = performance.now();
  if (now - last > 34) window.__gaps.push(Math.round(now - last)); // a dropped frame or worse
  last = now;
  requestAnimationFrame(tick);
})();

A quarter-second freeze. The first run logged gaps of 220 and 264 ms, the second one just after the row committed. Most of it was the sound code creating an audio context in the middle of the commit; one shared context brought the commit down to 34 ms. I wrote that one up with the profile and the fix.

Hint badges that spun on every move. A tile with a hint badge replayed the badge's entrance animation whenever the tile changed rows. Rows were separate elements, so moving a tile meant React moved its DOM node, and a node taken out of the document and put back starts its CSS animations again. I put every tile in one grid with explicit gridRow/gridColumn, and a MutationObserver still counted 66 tile elements and 8 badges re-inserted over three drops: React keeps keyed DOM nodes in array order, so it still moved them. Rendering the tiles in a fixed order — sorted by id — and letting the grid cells place them took both counts to zero.

A tile that left its slot. Later I moved the game onto a shared template whose board positions tiles absolutely. After that, every lift and landing showed an empty square where the tile should be. I recorded the old build and the new one playing the same moves and compared them frame by frame. The effects set position: relative on a moving tile so that its z-index would apply — harmless on a CSS grid, but on an absolutely positioned tile it pulled the tile out of its slot for the length of the animation. Now the effects only touch position on an element that is not already positioned. I wrote up more of that layering trouble here.

The celebration that held the board

A careful sequence has a cost: while it runs, the player waits. I had locked dragging for the whole celebration so rows could not vanish under a finger, and that lock was the next complaint.

The full celebration runs 1.71–1.76 s, and the lock covered all of it. Only the light-up and the fuse actually use the tiles; the banner flies above the grid. Unlocking as soon as the row leaves the board brought the lock to 0.88–0.91 s over three solves, timed from two data attributes the board now sets on itself for each phase, with the banner and confetti still playing.

That broke the ordering rule in a new place: a move made during the banner's flight can solve the next row. The fix keeps the banner's promise around, and the next row's light-up waits on it. In the test I made two moves during a flight, the second one completing a row, and that row started lighting about 0.1 s after the first banner settled.

Then the celebration froze if I grabbed another tile straight after a solve, and carried on when I let go. That one was my own guard: after the landings, run the solves unless a finger is down, on the theory that the next drop would check. It meant the row sat lit and motionless for as long as I held a tile. Now the celebration always starts. You can pick up any tile except the four lighting up, and a drop made during the light-up waits for that row to leave. On the emulator I grabbed a tile 0.37 s after a solve, and the screen recording shows the light-up, fuse and banner playing with the tile held.

Before that freeze, touching a tile mid-celebration had also cut its sound. That was a separate bug in how the sound code unlocks audio on a touch, and it has its own write-up.

What the frame timings say

Eight recorded solves at the end of that round, five and then three, showed no frame gap over 34 ms. The win sequence is the exception: in each of four recordings, a single 48–49 ms frame lands at the moment the winning row commits. That commit also saves the level as completed, but I never profiled the frame, so the save is a suspect rather than a finding. After a later reskin it measured about 59 ms. One of the four win runs also had a 219 ms gap between the landing and the light-up that I never traced, and another a 105 ms gap after the win screen had opened.

The limits matter. The frame probe ran in headless Chromium on a Mac, not on a phone. The emulator recordings prove the order of the beats and that nothing froze, not frame times. The haptics were never felt at all, because an emulator does not vibrate.

What I would keep: the store says what is true, and one place says when it becomes visible. Every effect is a promise with a ceiling. And a timer that waits for some other animation to finish is a guess about a number that someone will change — usually me, the next day, while adding more juice.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.