HTML5 game publishing

A 2D Camera That Feels Right: Smoothing, Dead Zones and Look-Ahead

Frame-rate independent smoothing, dead zones, platformer vertical behavior, look-ahead, bounds, shake and parallax.

PlayKovo Team4 min read
Games currently available on PlayKovo. Open a title to see its current listing.

In a 2D game with a scrolling world, the camera is part of the gameplay. A camera that is locked rigidly to the player makes everything shake. One that lags too much hides danger. One that does not look ahead makes players run into things they could not see. This post covers the techniques behind a good 2D camera: smoothing, dead zones, look-ahead, bounds and shake, with code you can adapt.

Start simple: center on the player

camera.x = player.x - view.w / 2;
camera.y = player.y - view.h / 2;
// when drawing:
ctx.setTransform(1, 0, 0, 1, -Math.round(camera.x), -Math.round(camera.y));

This works, but it feels stiff. Every small movement of the player moves the whole world, including tiny jitters from collision resolution. Rounding the final camera position to whole pixels avoids shimmering in pixel art.

Smoothing

Instead of jumping to the target, move the camera part of the way each frame:

const targetX = player.x - view.w / 2;
const targetY = player.y - view.h / 2;
const k = 1 - Math.pow(0.001, dt);   // frame-rate independent
camera.x += (targetX - camera.x) * k;
camera.y += (targetY - camera.y) * k;

The Math.pow form makes the smoothing behave the same at any frame rate. A fixed factor like 0.1 per frame would follow faster on a 144 Hz screen than on a 60 Hz one. Adjust the base (0.001 here) to make the camera tighter or looser.

Too much smoothing makes the player drift to the edge of the screen when moving fast. Tune it while playing at full speed, not while standing still.

Dead zone

A dead zone is a rectangle in the middle of the screen where the player can move without the camera moving. The camera only follows when the player pushes against its edges.

const dz = { w: 120, h: 80 };
const left = camera.x + view.w / 2 - dz.w / 2;
const right = left + dz.w;
if (player.x < left)  camera.x -= left - player.x;
if (player.x > right) camera.x += player.x - right;
// same for y

Dead zones stop the camera from reacting to small movements, which makes the screen calmer. They suit platformers and top-down games. Combine with smoothing for the best feel.

Vertical behavior in platformers

Following every jump vertically makes the screen bob up and down. A common approach in platformers:

  • Follow horizontally all the time.
  • Vertically, only move the camera when the player lands on a new platform height, or when the player goes above or below a larger vertical dead zone.
  • When falling fast, follow quickly so the player can see where they will land.

This "platform snapping" keeps the horizon steady during normal jumps and is one of the most noticeable improvements you can make to a platformer.

Look-ahead

Players need to see where they are going more than where they have been. Shift the camera target in the direction of movement:

const lookTarget = Math.sign(player.vx) * 80;
lookAhead += (lookTarget - lookAhead) * (1 - Math.pow(0.01, dt));
const targetX = player.x + lookAhead - view.w / 2;

Smooth the look-ahead separately, so the camera does not swing back and forth when the player quickly turns around. In top-down shooters, look-ahead toward the aim direction or mouse cursor works well instead of the movement direction.

Bounds

Clamp the camera to the level edges so the player never sees past the end of the world:

camera.x = Math.max(0, Math.min(camera.x, level.w - view.w));
camera.y = Math.max(0, Math.min(camera.y, level.h - view.h));

If the level is smaller than the view in one direction (common on wide desktop screens), center it in that direction instead of clamping, or the world sticks to one side.

The view size depends on the iframe size, which changes between devices and when the player rotates the phone. Recompute bounds whenever the view size changes.

Framing several targets

For two-player games on one screen, or boss fights, the camera should keep several things in view. Center it on the midpoint of all targets, and if your game allows zoom, zoom out as they move apart, with limits so it never zooms so far that sprites become unreadable.

Shake and effects on top

Apply screen shake as an offset after all the logic above, not to the camera position itself. Otherwise smoothing will spread the shake out and it will feel mushy. Keep a separate "shake offset", add it only when drawing, and decay it over time. Also respect a reduced motion setting.

Parallax backgrounds

Drawing background layers at a fraction of the camera movement adds depth cheaply:

drawLayer(farHills,  -camera.x * 0.2);
drawLayer(nearTrees, -camera.x * 0.5);
drawWorld(-camera.x);

Round each layer's position separately, and repeat layer images horizontally for long levels.

Transitions and cutscenes

When the camera needs to show something else, like a door that just opened, move it there smoothly, hold for a moment, then return. Lock player input during the move, or keep it very short. A camera that takes control too often is frustrating.

Debugging the camera

Draw the dead zone, the look-ahead target and the level bounds in a debug mode. Many camera problems become obvious when you see the dead zone rectangle while playing.

Checklist

  • Frame-rate independent smoothing.
  • A dead zone so small movements do not move the screen.
  • Calm vertical behavior in platformers.
  • Look-ahead in the direction of movement or aim.
  • Clamped to level bounds, recomputed on resize.
  • Shake added as a separate offset, with reduced motion support.
  • Rounded positions for pixel art.

For the loop and collisions behind the camera, see our posts on fixed timestep loops and 2D collision detection on the developer blog.

We use optional cookies for site analytics and site advertising. You can change your choice later. Privacy policy.