All workCase study 03 of 03

Shadow of the Crimson Gate

Role
Game design, engine and graphics programming
Year
2026
Built with
Three.js, TypeScript, WebGL, Web Audio API, Vite

A Japanese dark-fantasy action game that opens from a link. Third-person katana combat, yokai, a three-phase boss and saved progress, running in a browser tab on Three.js and my own TypeScript systems.

The title screen: the game's title and menu beside a crimson temple gate with teal doors, stone lanterns and a full moon
This is the real game, in a frame. Click inside to control it. Starting a new game loads about 6 MB of audio.The game needs a keyboard and mouse, so try this one on a computer.
Compressed JavaScript at first load
237 KB
Measured
Average FPS during gameplay at 1080p on an Apple M5 MacBook Air
46
Measured
Lines of TypeScript across 163 files
19,895
From the code
Phases in the Crimson Oni boss fight
3
From the code

Measured on the live game in November 2026 after the visual overhaul: five back-to-back runs of 600 frames averaged 43–49 FPS, with a median frame of 16.7 ms and a 95th percentile of 33 ms. This laptop has no fan and slows as it warms up. An earlier run on a cooler machine held 60.

The problem

It had to feel like a real game and still open from a link.

Most browser games feel like browser games. I wanted weight: katana combat with dashes and combos, enemies that behave differently, and a boss that changes as the fight goes on.

It also had to open from a link, with no store, install or plugin. So the whole game has to live within a browser's limits on memory, shaders and audio, on hardware I'll never see.

Without a commercial engine, the loop, combat, AI, world generation, audio and saves all had to be built as my own systems on top of Three.js.

The architecture

Everything runs through one loop.

Click a part, or a step belowOne game loop

One game loop

In the machine: the crankshaft and the dial with a stop pin

One requestAnimationFrame loop drives the whole game, updating and then rendering. A slow frame can only push delta time up to the pin at 100 ms, so a hitch slows the game instead of throwing enemies through walls.

  1. One game loop: One requestAnimationFrame loop drives the whole game, updating and then rendering. A slow frame can only push delta time up to the pin at 100 ms, so a hitch slows the game instead of throwing enemies through walls. (In the machine: the crankshaft and the dial with a stop pin.)
  2. Combat and AI: Dashes, combos and hitboxes sit alongside state machines for the yokai and the Crimson Oni. The boss changes phase at 70% and 35% health, which the third cam shows by growing another lobe. (In the machine: three cams and the health column.)
  3. World and VFX: Every piece on the tray is the same mesh, placed by one action, the way the game places its rocks and path stones: an InstancedMesh is one draw call. Particles come from a pool, so petals are reused rather than created and collected in the middle of a fight. (In the machine: the tree tray, its lever and the petal ring.)
  4. Three.js renderer: WebGL with ACES filmic tone mapping turns the raw frame into the final picture. Pixel ratio is capped at 1.5, so a very sharp display can't quietly double the work. (In the machine: the lantern and screen.)
  5. Audio engine: Web Audio buses over 81 audio samples, with ambience by zone. These faders sit at the real levels from the code: master 0.7, music 0.5, ambience 0.6, SFX 0.8 and UI 0.5. (In the machine: the mixing desk.)
  6. Versioned save: Progress goes to localStorage with a version number. On load, broken JSON or missing fields reset the save, so a damaged file can't stop the game from starting. (In the machine: the stamped tin.)

Each system is its own TypeScript module, updated by the game loop. The rig is an illustration with every system running at once. Its speeds aren't to scale: the real loop runs about 60 times a second.

Fig. 1The new third-person opening and the first fight, two moments captured from the live game.
The ronin lands a glowing ink slash on a yokai beside the torii gate, with a damage number, enemy health bar and the player's health bar on screen
Fig. 2A hit in that fight: the ink slash, damage number, enemy bar and new HUD.

The decisions

The calls that shaped it.

  1. Three.js with my own game systems

    Instead of a full game engine

    It keeps the first load to 237 KB of compressed JavaScript, and I control the whole frame.

    Trade-off Combat, AI, cameras and saves were all mine to build and debug.

  2. Instanced and merged meshes, plus particle pools

    Instead of one object per tree, rock and petal

    Rocks and path stones are instanced, each tree merges into a few meshes that share materials, and pooled particles avoid creating and collecting objects in the middle of a fight.

    Trade-off Individual pieces are harder to animate and change.

  3. Frame-safety limits

    Instead of trusting the browser's timing

    Delta time is capped at 100 ms so a hitch can't throw enemies through walls, pixel ratio is capped at 1.5, and the game pauses when the tab is hidden.

    Trade-off A long stall briefly slows the game instead of catching up.

  4. State machines for enemies and bosses

    Instead of scripted sequences

    Yokai and the Crimson Oni react to the player, phases change on health, and attacks are defined as data.

    Trade-off Transitions became a real source of bugs, as you'll see below.

src/game/core/GameLoop.ts
// Cap max delta time to prevent huge spikes when tab is backgrounded
// Also prevent negative dt if requestAnimationFrame uses a different time origin
if (dt < 0) dt = 0;
if (dt > 0.1) dt = 0.1;

this.updateCallback(dt);
this.renderCallback();

The middle of the game loop. Those two checks are why a slow frame slows the game down instead of breaking it.

The failures

What broke.

  1. Too many lanterns for the shader

    Giving every lantern a real point light ran into shader light limits, so I cut the world's 20 lanterns to 2 and the arena's 8 to none.

    That emptied the world, so I restored them 15 minutes later. The proper fix came in November: a small pool of real lights that moves to the lanterns nearest the player, while the rest glow on their own.

  2. Shadows cost too much

    Having the terrain and arena floor receive shadows hurt performance.

    I turned off shadow reception on those large surfaces.

  3. Enemies with no direction

    Enemy AI steered along the raw vector to the player, height included, and had no real direction to face when an enemy stood exactly on the player.

    The vector is now flattened to the ground, with a fixed fallback direction at zero distance.

  4. A boss that got stuck between states

    The Crimson Oni had state transition bugs, plus wrong ability cooldowns and phase changes.

    I fixed the transition logic, and later corrected the cooldown and phase handling.

  5. Synthesized audio didn't last a day

    I added procedural boss audio on August 29 and replaced it on August 30.

    The effects are sample-based now: a registry, routing buses and 81 audio files, with footsteps by terrain and ambience by zone.

Where it stands

  • Playable in a browser tab, with four chapters, dialogue, saved progress and a three-phase final boss.
  • An average of 43–49 FPS in my gameplay test on a fan-less laptop, with automatic quality scaling and graphics and VFX settings for slower machines.
  • Fully open source, with 275 commits on GitHub.

What I'd do next

  • Get back to a steady 60 FPS on a fan-less laptop now that post-processing is in.
  • Test and publish frame rates on mid-range and low-end laptops.
  • Finish save migration, which currently only updates the version number.