Skip to content
Currently booked
· 13 min read

Building Onigiri: a browser game with my favorite food

The little game hidden in this site's footer: line drawings placed by WebGL, physics that feel the same at any frame rate, gaps that are always reachable, and a game that only loads when someone wants to play.

Request an AI summary

In the footer of this site, a link says “Don’t play with my favorite food”. Behind it is Onigiri: a small arcade game in which the onigiri hops through an endless row of gaps. Tap, click or press Space to hop; touch a stack or the ground and the round is over.

It’s a small game, but the same questions that matter in any interactive web project show up in it: how it feels, how fast it loads, and whether it behaves the same on every device. Here’s how it’s built.

1. Canvas: drawn once, placed by WebGL

The game draws on a single WebGL 2 canvas. The gaps sit between stacks of floppy disks and console memory cards, drawn like the patent-style drawings on the services list: the objects as solid outlines, hidden edges dashed, a dash-dot centre line through every stack and a dimension on every gap. The floppies’ shutters slide open and shut, and behind the stacks a drafting grid and large faint sketches of the logo, the gadgets and the hardware from the services list drift by more slowly, which gives the scene depth. Every background sketch draws itself in line by line as it comes onto the screen, like a pen plotter; a new level sends a scan line across the screen, a rice grain leaves a +1 callout, and a crash rings out from the spot.

Every drawing is made once per screen size with the ordinary 2D drawing tools and then kept only as a texture on the graphics chip: every item, the shutters and the reels’ teeth, the background sketches, a strip of the grid, the centre line, every gap’s dimension in each size it can have, the rice grains, a font sheet per size and colour that every caption is set from letter by letter, and a short stroke per colour and line weight that lines, rings and dashes are cut from. So nothing is drawn or uploaded to the graphics chip mid-round, which on phones costs a few milliseconds each time and shows as a small stutter.

The only exception are the eight large background sketches, by far the biggest drawings: where the game runs in its worker, they’re made right after it opens, so it doesn’t wait for them, and they go up one per frame between rounds, never during one, each drawing itself in once it’s there. Dashed lines and text are what a 2D canvas is slowest at, mobile Safari above all, so none of them gets drawn again while playing.

Each frame only places those textures, moved, turned, scaled and faded, with as few draw calls as possible; it’s plain WebGL 2 with two short shaders, no engine. Where the browser can, the game runs in a Web Worker on an OffscreenCanvas, with animation frames of its own: the page only bakes the drawings, where its fonts are, and hands them over as bitmaps, and taps, scores and sounds travel as short messages. Whatever else the page is doing then can’t hold up a frame. A browser without that runs the same game on the page itself.

A worker’s animation frames don’t always follow the screen, though: Safari runs them on a timer of its own, about 16.9 milliseconds apart while the screen shows a new frame every 16.7. The two drift apart, and about once a second a frame stays up for two refreshes, a small hitch in the scrolling stacks. The page’s own frames do follow the screen, but a phone holds the page’s next frame back after a tap, so the game can’t run on those either. So the page only sends the times of its frames to the worker. The worker works out the screen’s beat from them, the median of the last gaps, so neither a late frame nor frames bunched behind other work on the page throw it off, and draws each frame on a timer of its own just after each refresh. The frames are then in step with the screen, 60 or 120 a second, and no tap on the page can hold them up.

And should the graphics chip still miss its moment, three late frames within two seconds, the canvas draws at a lower resolution from the next frame on, as console games do, with the textures it already has, so nothing is baked again. The sketches still draw themselves in line by line: each one is baked with a map of when every pixel of its lines is reached, and the shader shows the lines up to the current moment.

The shader also gives the stacks a motion blur, the way a film camera with its shutter open the whole frame does: each frame smears them over the whole distance they moved since the last one, so one frame’s smear meets the next one’s. The slower layers behind them stay sharp, as they’d smear by only a pixel or two. On a phone screen, which shows 60 still frames a second, that turns the small steps of sharp lines into smooth movement.

And everything that scrolls moves by a whole number of screen pixels per frame: each frame of a stack is then the same picture shifted, so its thin lines don’t shimmer between pixels. That step only changes once the speed is clearly a pixel faster, so it never flips between two widths from frame to frame, and the grid and the sketches behind move by whole pixels of their own.

The onigiri’s pixel-art sprite stays sharp, also as it tilts and falls by fractions of a screen pixel: each of its pixels is read sharp, blended only across the one screen pixel at its edge, so none of them crawls as it turns; the tips of its hair flutter on one beat, so a short loop of eight baked frames covers them, and its glow and halo add light to the scene behind it.

Bright light also gets the glare of a camera lens: every rice grain carries a small twinkling glint with a flat streak through it, a grain flashes as it’s picked up, the transformed onigiri and the glowing ball throw a wide streak across the scene, and the ball leaves faint ghosts on the line through the screen’s centre, as light does in a lens. The glare is baked once like everything else and added as light, a few quads a frame instead of an extra pass over the whole screen, which is what it costs in most engines.

Over it all lies the look of an old CRT screen: its scanlines and its darker corners, worked out by the shader in a single quad over the scene without reading a texture, the scanlines on whole screen pixels so they stay crisp at any resolution.

The score is drawn by the game too, from ten baked digits, so a point never makes the page lay itself out again mid-round; only the best and the score sheet after a crash sit in the page around it. A small meter in the top left corner shows the frame rate and whether the game runs in its worker or on the page, and whether its frames follow the screen’s beat, with a bar for each of the last frames, the late ones and those with a hop in the site’s green, and a line in the bottom left corner shows the screen’s beat, the longest frame, the late ones and the longest a frame’s own work took.

The game lives in a native <dialog> element. While it’s open, the page behind it doesn’t scroll, and closing it puts the game back on its start screen.

2. World units: the same game on every screen

Screens come in every size, but the game shouldn’t get easier on a large monitor or harder on a small phone. So everything happens in world units: the world is always 640 units tall, and the canvas scales that world to whatever space it has. Gravity, hop strength, gap sizes and speeds are all defined in those units. A hop is the same height on every screen.

3. Physics: the same arc at any frame rate

Screens also refresh at different rates: 60 frames per second, 120 on many newer phones, 30 in power-saving mode. A naive game moves things a fixed amount per frame and so runs faster on faster screens.

The game computes each step from the actual time since the last frame, and it does so with the exact formula for falling under constant gravity. The result: the hop follows the same arc whether the screen draws 30 or 120 frames per second.

Frame timing on phones can be uneven, especially right after a tap. The screen itself shows a new frame on a steady beat, though; only the timestamps the browser reports wobble around it by a millisecond or two. So the game measures that beat over the first frames and moves everything by exactly one beat per frame. The stacks then advance by the same distance every frame.

A frame that comes late, as the one right after a tap often does, still counts as one beat: catching up at once would make the stacks leap on every hop. Instead the game owes that time and pays it back over the next few frames, each a little longer, so the stacks speed up a touch for a tenth of a second rather than stalling or jumping. A stamp that comes early right after a late one settles that debt: the frame was on time, only its stamp wasn’t, and nothing extra moves. Only several late frames in a row, as on a phone in low power mode, make it measure the beat again; 120 Hz is picked up the same way.

The onigiri falls and hops on the same beat: on the stamps it would wobble with them by a few screen pixels a frame at full fall, right where the player is looking, while a tap still makes it hop on the very next frame.

4. Fairness: every gap is reachable

Randomly placed gaps can create impossible situations: a gap at the very top right after one at the very bottom. In the game, each new gap stays within a set distance of the previous one and never gets too close to the edges.

That rule is checked by a test that generates ten thousand gaps in a row and confirms each one stays on screen and within reach of the one before. It runs with every push.

Two more details make it feel fair:

  • A forgiving hitbox: the collision area is a little smaller than the sprite, whose corners are transparent anyway.
  • Difficulty in waves: it follows the stacks passed, not the points, so collecting grains never speeds the game up. It rises over nine stacks, then eases off for three: wider gaps, a calmer pace, time to breathe. Every wave peaks a little higher than the one before, and the rise levels out over time instead of growing endlessly. The pace glides between levels instead of jumping.

From the tenth stack on, some gaps slide slowly up and down. Their slide comes off the allowed distance, so even at the worst moment of both slides the next gap stays on screen and within reach; the same test checks that.

Now and then a rice grain floats between two stacks, a little off the straight line. It’s worth a point for anyone who takes the detour, and it never sits inside a stack. Grains in a row count more and more: the second is worth two points, the third three, with no limit, and one that slips past ends the streak.

The streak also transforms the onigiri: from five in a row it becomes Super Onigiri, with spiky yellow hair, a yellow aura and rising sparks; from ten it turns golden, its hair grows sharper and crackles with lightning, and it pulls nearby grains in like a magnet; from fifteen its hair falls long down its back and a halo floats above it. The halo is one extra life: a crash then charges a glowing ball that clears every stack on screen, the score is halved, and the round goes on. Only the score at the end of the round counts as a best, so the extra life has to be earned back. That works once per round; the next time, the form comes without a halo. The moment a grain slips past, it turns back into a plain onigiri with a small puff.

After a crash, the round’s numbers appear as a drawing’s title block: score, best, grains, best combo, level and date. The scene dims behind it, and a button shares the score, through the phone’s share sheet where there is one, or as text to paste.

Every ten points a new level begins, announced on screen: the stacks start as a mix of floppy disks and memory cards, the second level adds cassettes with turning reels, the third game cartridges, and from then on every stack is a mix of all four.

5. Loading: only when someone plays

Most visitors never open the game, so it shouldn’t cost them anything. The game code and its sprites load only once someone points at, focuses or touches the link, so they’re usually there by the time it’s clicked. The animation loop runs only while the dialog is open; after a crash, it stops until the next tap. The sound effects are made in the browser with WebAudio, so there are no audio files to download either. They start with the first tap, rest while the dialog is closed, and a button next to Close turns them off; the choice is remembered.

6. Touch: feeling right on phones

Taps have to feel instant. On touch screens, browsers often add extra behaviour to a tap: a highlight flash and simulated mouse events afterwards. The game handles touch input directly, so a tap is a hop and nothing else.

7. High score: kept on the device

The best score is stored in the browser’s local storage, so it’s still there on the next visit. If storage isn’t available, for example in a private window, the game keeps the best score for the current visit only. No account, no server, nothing leaves the device.

The short version

  • One WebGL canvas in a native dialog: drawings, captions and pen strokes baked into textures once per screen size, a frame only places them.
  • World units make the game the same on every screen.
  • Exact physics give the same hop at any frame rate, paced by the display’s own beat.
  • Fair gaps, checked by a test with ten thousand rounds, plus a forgiving hitbox.
  • Loads on demand and runs only while someone is playing.
  • High score stays on the device.

Want a game or an interactive experience for your brand? Get in touch.