Workshop › Doodle Jump-style Climber › Step 11 of 24

A camera that follows

What we are doing

To climb forever, the world must be taller than the screen. Positions are now in the world, and cameraY is the world height shown at the top of the screen. Everything is drawn shifted by the camera, and the camera moves up when the player gets near the top.

```js
let cameraY = 0 // the world y shown at the top of the screen: the camera

  // The camera only ever moves up, keeping the player in the upper part of the screen.
  if (player.y < cameraY + 200) cameraY = player.y - 200

    ctx.fillRect(p.x, p.y - cameraY, p.w, p.h)

  ctx.fillRect(player.x, player.y - cameraY, player.w, player.h)
  // Half off one side: draw the other half on the far side.
  if (player.x < 0) ctx.fillRect(player.x + canvas.width, player.y - cameraY, player.w, player.h)
  if (player.x + player.w > canvas.width) ctx.fillRect(player.x - canvas.width, player.y - cameraY, player.w, player.h)
```

What it means

Your turn

  1. Under platforms, write cameraY.
  2. In update, after the landing block, write the camera lines.
  3. In draw, subtract cameraY in the platform and the three player fillRects.

Skills: Game states, Drawing on a canvas

All steps

  1. The jumper
  2. Draw again and again
  3. Gravity
  4. Bounce
  5. Steer left and right
  6. Through the sides
  7. Half here, half there
  8. Touch steering
  9. Platforms
  10. Land on platforms
  11. A camera that follows
  12. Lines on the wall
  13. How hard should it be?
  14. Endless platforms
  15. Miss and fall
  16. Play again
  17. Tap to play again
  18. Score is height
  19. Best score
  20. Kinds of platform
  21. Moving platforms
  22. Traps
  23. It breaks!
  24. Build it yourself: springs