2D game cameras that feel smooth and show what matters. Following with a dead zone and look-ahead, frame-rate independent smoothing, clamping to level bounds, zooming around a point (mouse wheel, pinch), fitting several players on screen, screen shake, pixel snapping for pixel art, screen ↔ world conversion for clicks and touches, culling, cutscene pans with promises, and room-by-room transitio...
Download skill (.zip) Files on GitHubCamera system
Drop-in: assets/camera.js (tests: dead zone, follow, bounds clamp, same smoothing at 30 and 144 fps, zoom keeps the point
under the cursor fixed, toScreen∘toWorld, culling, pans, fit several targets; rendered in a browser).
import { Camera } from "./core/camera.js";
const cam = new Camera({ viewW: 480, viewH: 270, bounds: { x: 0, y: 0, w: map.width * 16, h: map.height * 16 },
smooth: 8, deadZone: { w: 60, h: 40 }, lookAhead: 40, pixelSnap: true });
cam.snapTo(player.x, player.y); // on level start (no swoop)
// update: cam.follow(player, dt); cam.setShake(shake.x, shake.y) (game-feel-engineering Shake)
// render: ctx.save(); cam.apply(ctx); drawWorld(cam.view(32)); ctx.restore(); drawHUD();
// input: const w = cam.toWorld(pointer.x, pointer.y) wheel: cam.zoomAt(cam.zoom * 1.1, px, py)
// cutscene: await cam.panTo(door.x, door.y, 1.2);
Feel
| Setting | Effect |
|---|---|
deadZone |
Small player moves don’t scroll (calmer screen). Platformers: wider than tall |
lookAhead |
Shows more in the direction of travel (runners, shooters) |
smooth 6–12 |
Higher = tighter. Too low feels laggy; infinite feels rigid |
bounds |
Never show outside the level; centers small levels |
pixelSnap |
No shimmering pixel art (rounds the final offset) |
- Vertical: platformers often only move the camera up when the player lands on a higher platform (avoid bobbing on jumps).
Implement by following
player.groundYinstead ofywhile airborne. - Room-based games (Zelda / Metroid style):
panTothe next room on exit, pause gameplay during the pan. - Couch co-op:
fitTargets(players)zooms out to keep everyone visible; clamp the zoom.
Pitfalls
Following the un-interpolated position with a fixed-step loop (jitter): follow the rendered position. Clicks without
toWorld (wrong when scrolled or zoomed). Smoothing written as x += (t - x) * 0.1 (frame-rate dependent: the module
uses 1 - exp(-k·dt)). Shake applied to the HUD (shake only the world).