beginner · 22 minute study
A game is state evolving through time
A movement rule that feels correct on your laptop can double its speed on a faster display. Separate elapsed time from the number of drawings before building more mechanics.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Functions — An existing guided foundation in Learn → Try → Test → Build.
First, build a mental model
The world is a notebook of facts: position, velocity, health, phase. Update computes the next page from the previous page, input and elapsed time. Render draws that page. Drawing the same page twice must not secretly move the player twice.
- requestAnimationFrame asks the browser for a callback before a repaint; it is not a promise of 60 updates every second. Use its timestamp to measure elapsed time, convert milliseconds to seconds, and make units explicit.
- Velocity has units of distance per second. Multiplying it by seconds produces distance. If the player moves 120 pixels/second, a 16 millisecond update requests 1.92 pixels of travel.
- Variable-step updates use the measured delta directly. Fixed-step updates accumulate elapsed time and consume it in equal simulation slices. Rendering can interpolate between snapshots. Fixed steps help repeatability but do not automatically make every physics engine deterministic.
- A hidden tab can resume after a long gap. Choose a policy: pause, limit catch-up, or simulate bounded slices. Clamping a delta intentionally discards some elapsed time; it is a gameplay trade-off, not exact wall-clock simulation.
function update(world, input, seconds) {
if (!Number.isFinite(seconds) || seconds < 0) return world;
const speed = 120; // pixels per second
return { ...world, x: world.x + input.moveX * speed * seconds };
}
const previous = { x: 10, health: 3 };
const next = update(previous, { moveX: 1 }, 16 / 1000);
console.log(next.x); // 11.92
console.log(previous.x); // 10: the previous snapshot survives
// A browser loop samples input, calls update, resolves collisions,
// draws the result, then requests another animation frame.- Read
The previous world says x = 10. Input requests positive horizontal movement. No canvas pixels determine authority.
- Integrate
120 px/s × 0.016 s = 1.92 px. The next x is 11.92. The health field is carried forward.
- Present
A renderer draws the accepted next state. Skipping one drawing can reduce smoothness without changing a fixed sequence of simulation updates.
Production thinking
Take this beyond the example.
Keep one owner for the loop. Cancel it when its owner is disposed, release input on blur, and define hidden-tab behavior. Do not run independent render loops from every component. The lab demonstrates one simple movement model; a complete engine must also define collision ordering, pauses and effect lifetimes.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Actions outlive individual input devices →