Level Discoverystart here Intermediatethe architecture Expertin depth
Expert Β· Batch 2 Β· Desert
Chapter 2 Β· expert level

Asset-free rendering: procedural synthesis

Not a single image or audio file ships. Everything is synthesized: textures baked at boot, sound built from oscillators and envelopes, colours computed by interpolation, geometry generated each frame β€” and effects scaled to the device.

1Textures generated at runtime

There are no PNGs. Every particle sprite is drawn once into an off-screen texture at boot, then reused, tinted and blended for the rest of the session.

src/systems/textures.js β€” a soft smoke puff from stacked circles
g = make();
for (let r = 16; r > 0; r -= 2) {
  g.fillStyle(0xffffff, 0.06);   // faint, additive translucent rings
  g.fillCircle(16, 16, r);     // β†’ a soft gradient, no image
}
g.generateTexture('smoke', 32, 32);

Four sprites cover the whole game: a hard spark dot, a soft smoke puff, a bright additive flash, and a blobby cloud. White by default, recoloured per biome at runtime.

Why bake at boot. Generating a texture costs a little CPU once; sampling it costs nothing afterward. We pay the synthesis price a single time, then enjoy GPU-cheap, tintable sprites β€” with zero download.

2Sound: oscillators & envelopes

Every sound effect is a tiny Web Audio graph: a source (an oscillator or a buffer of noise), a filter, and a gain envelope that shapes the volume over time. An explosion is filtered noise whose low-pass sweeps down while the gain decays.

src/systems/Sfx.js β€” the explosion envelope
const src = this.noise(0.55);            // white noise (random buffer)
lp.frequency.setValueAtTime(1200, now);
lp.frequency.exponentialRampToValueAtTime(120, now + 0.5); // darkens
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.55);  // decays

Richer effects just stack graphs: the tower rubble() layers a low rumble, a deep sine thud, and a scatter of short high-passed β€œdebris” bursts at random times. The continuous flyby() whizz keeps one band-passed sawtooth alive and nudges its gain and pitch toward a target with setTargetAtTime as a shell nears. On iOS, where the Vibration API is blocked, this audio whizz stands in for the haptics you'd otherwise feel.

The suspended context. Browsers create the AudioContext suspended and only resume it after a user gesture. A single shared Sfx instance lives on the game registry and is unlock()-ed on first interaction β€” otherwise the opening sounds vanish silently.
Remember. A synthesized sound = source + filters + envelope. We store no samples: we describe a recipe the browser runs. The bundle stays tiny.

3Colour maths: lerp, shade, barrelColor

Colours are computed, not stored. Linear interpolation (lerp) between two RGB triples drives the charging barrel (steel β†’ glowing red), and a multiplicative shade brightens or darkens any colour. All of it lives in the renderer-agnostic src/render/visuals.js.

src/render/visuals.js β€” shade & the crater rim
export function shade(color, factor) {
  const r = clamp(Math.round(((color >> 16) & 0xff) * factor), 0, 255);
  const g = clamp(Math.round(((color >> 8)  & 0xff) * factor), 0, 255);
  const b = clamp(Math.round(( color        & 0xff) * factor), 0, 255);
  return (r << 16) | (g << 8) | b; // repacks the integer
}

barrelColor(power) lerps from cool steel toward burning red on a power ratio clamped to [0, 1]; craterRimColor(edge) pulls the biome's edge tint toward dark brown for a β€œscorched earth” rim rather than a black ring. The bit-shifts (>> 16, & 0xff) extract each channel from a 0xRRGGBB integer.

For the curious. Colours are packed integers (0xRRGGBB). Bit-shifting splits the channels, the maths runs per channel, and the shift-OR repacks them β€” the same representation Phaser and a plain canvas both accept, which is why this code is shared.

4Animated geometry: the windsock

The windsock isn't a sprite β€” it's geometry generated every frame. From the base point, the wind strength and the time, computeWindsock returns a pole plus tapering striped quads that point downwind, lift with strength, and flutter with a time-driven sine.

src/render/visuals.js β€” computeWindsock (the centreline)
const length = 16 + 50 * k;          // 0..1 strength
const droop  = lerp(1.2, 0.14, k);   // flatter when windy
const flutter = Math.sin(time * 0.006 + f * 6.2) * 7 * k * f; // tip waves most

Because it returns plain points and colours, the TV draws it with Phaser graphics and the controller draws the same shape on a 2D canvas β€” one definition, two renderers.

5Particle systems & blend modes

Ambient scenery is a single emitter configured by data. Each biome picks a behaviour β€” drifting leaves, falling snow, rising embers, blowing sand β€” and embers add an ADD blend mode so they glow. The live wind even bends the stream by setting the emitter's gravity.

src/objects/Background.js β€” embers rise and glow
config = {
  y: GAME_HEIGHT + 10,            // born at the bottom
  speedY: { min: -110, max: -45 }, // upward
  scale: { start: 0.7, end: 0 },   // shrink as they die
  blendMode: 'ADD',             // additive: bright embers glow over the dark sky
};

All this synthesis can overwhelm a smart TV. So at boot the host samples real frame deltas for a short window, computes the average fps, and records a tier the renderer respects (fewer clouds, no ambient layer on lite).

src/objects/Background.js β€” quality decides what we draw
// the ambient layer is the easiest to sacrifice on a weak device
if (quality !== 'lite') this.spawnAmbient();
Describe behaviour, not particles. We never spawn each flake by hand. We hand the engine rules β€” speed, lifespan, start/end size and alpha β€” and it produces hundreds of particles that obey them, the wind shaping the whole stream at once.
← Chapter 1 Β· Deterministic loop Chapter 3 Β· Network protocol & consistency β†’