Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท Tilemaps and Cameras

Camera Basics

Learn how Phaser cameras work. Scroll a large world, follow a player with smoothing and a deadzone, set bounds, zoom, and build parallax and fixed HUD layers.

Your game world can be much bigger than the screen. A platformer level might be 5,000 pixels wide while the canvas is 800. The piece of the world the player sees at any moment is decided by a camera. Every Phaser scene starts with one camera, called the main camera, and you move it, follow things with it, zoom it and shake it to make the game feel alive.

Understanding cameras mostly means understanding two coordinate systems: the world (where your sprites live) and the screen (the pixels of the canvas). The camera is the window that maps one to the other. Once that is clear, following a player, fixing a score display to the screen and building parallax backgrounds all become small, easy jobs.

World versus screen

Imagine a huge painting and a cardboard frame. The painting is the world, the frame is the camera. Moving the frame to the right shows a different part of the painting, and the painting itself never moves. In Phaser:

  • camera.scrollX and camera.scrollY are the world position of the camera's top-left corner.
  • A sprite at world x = 1000 appears on screen at 1000 - scrollX (when the zoom is 1).
  • A pointer has both pointer.x, pointer.y (screen position) and pointer.worldX, pointer.worldY (the world position under it). Use the world values to click on things in a scrolling level.
flowchart LR
  W["World<br/>1600 x 480 px"] -->|"camera window<br/>scrollX, scrollY, zoom"| S["Screen<br/>480 x 270 px"]

The camera decides which part of the world reaches the screen.

The main camera

Inside a scene, this.cameras.main is the default camera. These are the properties and methods you will use constantly:

Call Purpose
cam.setBounds(x, y, w, h) Keeps the camera inside a rectangle of the world
cam.startFollow(target, roundPixels, lerpX, lerpY, offsetX, offsetY) Follows a game object each frame
cam.stopFollow() Stops following
cam.setZoom(z) 1 is normal, 2 is twice as big, 0.5 shows more world
cam.setDeadzone(w, h) A box in the middle in which the target moves without scrolling
cam.centerOn(x, y) Jumps so that world point x, y is in the center
cam.setBackgroundColor(color) Paints the camera view before sprites
cam.setViewport(x, y, w, h) Moves and resizes the camera's rectangle on the canvas
cam.setRoundPixels(true) Rounds scroll values to avoid blurry or jittery pixel art

The lerp values in startFollow are a smoothing factor between 0 and 1: 1 locks the camera onto the target, and 0.1 lets it trail behind and catch up smoothly. Lower values feel floaty, higher values feel rigid. Many games use between 0.08 and 0.2.

Following a target

The first example builds a world twice as wide as the screen with posts every 200 pixels so that you can see the scrolling. Click anywhere to send the red circle there with a tween; the camera follows it. The mouse wheel zooms.

Camera follow, bounds and zoom

Notice the following points:

  • setBounds stops the camera at the edges of the world. Without bounds the camera happily shows empty space beyond the level when the player stands near a wall.
  • setScrollFactor(0) pins the HUD text to the screen. The default scroll factor is 1, which means "move with the world".
  • The HUD still works at other zoom levels because the zoom is applied around the center of the camera and a fixed object keeps its screen position (it is slightly scaled with zoom; for crisp UI use a second camera, covered in the next lesson).
  • Click uses p.worldX and p.worldY. With p.x the player would go to the wrong place whenever the camera had scrolled.

Smoothing, offsets and the deadzone

startFollow(target, roundPixels, lerpX, lerpY, offsetX, offsetY) gives you three further controls:

  • Lerp smooths the follow. You can set different values per axis, for example 0.1, 1 in a platformer to keep vertical movement snappy.
  • Offset shifts the focus point. In a runner that scrolls to the right, an offsetX of -100 keeps the player on the left third, so the player can see what is ahead.
  • Deadzone (cam.setDeadzone(120, 80)) creates a box in the middle of the screen. The camera does not move while the target stays inside it, which stops tiny nudges from shaking the picture and is very comfortable in platformers. When you enable the debug option cam.setDeadzone shows nothing by itself, but cam.deadzone is a rectangle you can draw while testing.

A common pairing is setBounds + startFollow with lerp 0.1 + a modest deadzone. Check how it feels when the target is fast: if the camera lags so much that the player leaves the screen, increase the lerp.

Scroll factor and parallax

Every game object has a scrollFactor. It says how much of the camera's movement the object follows:

  • 1 (default): moves normally with the world.
  • 0: fixed to the screen, perfect for HUD elements and menus.
  • Between 0 and 1: moves slower than the world, which looks far away. Distant mountains might use 0.2, nearby hills 0.5.
  • Greater than 1: moves faster, which suggests a foreground object close to the lens.

Parallax is just different scroll factors on different layers.

Parallax layers

Here the scene has no player; the camera itself scrolls by changing scrollX directly. Hold the right arrow to move faster, and see how the mountains lag behind the posts. This is the whole trick behind the layered backgrounds of side-scrollers. Phaser also offers TileSprite, which lets a single image repeat forever and scroll by changing tilePositionX, handy for endless runners.

Converting between world and screen

Sometimes you need to translate coordinates by hand. The camera has helpers:

JavaScript
const cam = this.cameras.main;
const point = cam.getWorldPoint(screenX, screenY);   // screen to world
cam.worldView;                                       // a Rectangle: the part of the world currently visible
cam.cull(arrayOfObjects);                            // objects inside the view

cam.worldView.contains(enemy.x, enemy.y) is a quick way to know whether an enemy is on screen, for example to start its AI only when the player can see it.

Common mistakes

  • Using pointer.x instead of pointer.worldX in a scrolling world.
  • Forgetting setScrollFactor(0) on HUD elements, so the score slides away.
  • Setting bounds smaller than the camera view. The camera then centers the world and ignores follow offsets; make the bounds at least as big as the screen.
  • Changing scrollX while startFollow is active. The follow overwrites your change each frame; call stopFollow() first.
  • Zooming below 1 on a world that has no content outside its edges, which reveals the empty background.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A sprite is at world x = 900 and the camera's scrollX is 700 (zoom 1). Where does it appear on screen?

    Screen x equals world x minus scrollX: 900 minus 700 is 200.
  2. 2. Which setting pins a score text to the screen so that it does not scroll with the world?

    A scroll factor of 0 means the object ignores camera movement. Depth only changes draw order.
  3. 3. What effect does a lerp of 0.1 in startFollow have?

    Lerp is a smoothing factor. Small values trail and ease in, 1 locks the camera on.
  4. 4. Which pointer properties should you use to find the object under the cursor in a scrolling level?

    worldX and worldY account for the camera's scroll and zoom. The plain x and y are screen positions.
  5. 5. To make a distant mountain layer appear far away, what do you do?

    A smaller scroll factor makes a layer move less than the camera, which our eyes read as distance.

Summary

  • The camera is a window onto the world: scrollX and scrollY are its world position, and zoom scales the view.
  • setBounds limits scrolling, startFollow tracks a target with optional lerp, offset and deadzone.
  • setScrollFactor(0) fixes objects to the screen; factors between 0 and 1 create parallax.
  • Use pointer.worldX/worldY for clicks in the world and cam.worldView to test what is visible.

Next: Camera Effects and Multiple Cameras