Workshop › Mario-style Platformer › Step 19 of 37

A camera that follows

What we are doing

The level is 2048 pixels wide; the screen shows 640. We need a camera: one number, camera, the world x of the screen's left edge. Before drawing the world we shift the whole canvas left by camera, and shift it back afterwards.

```js
let camera

  camera = 0

  camera = player.x + player.w / 2 - canvas.width / 2

  ctx.save()
  ctx.translate(-Math.round(camera), 0)

  ctx.restore()
```

What it means

Your turn

  1. Above let coyote write let camera; in loadLevel, above coyote = 0, write camera = 0.
  2. In update, under updatePlayer(), leave an empty line and write the camera line.
  3. In draw, after the sky, leave an empty line and write ctx.save() and ctx.translate(...).
  4. At the end of draw, under the player's fillRect, write ctx.restore().

Skills: Drawing on a canvas, Game states

All steps

  1. The sky
  2. A level made of text
  3. Draw the tiles
  4. What is solid?
  5. Is a box touching anything?
  6. Find the start
  7. Draw the player
  8. Gravity
  9. Landing on tiles
  10. Which keys are held?
  11. Speed up
  12. Friction
  13. Walls
  14. Jump
  15. Bump your head
  16. Short hops
  17. A few frames of grace
  18. Coyote time
  19. A camera that follows
  20. Stop at the ends
  21. Draw only what is on screen
  22. Coins
  23. Collect them
  24. Count, and screen versus world
  25. Enemies
  26. Enemies that walk
  27. Do not walk off ledges
  28. Lives and a fresh start
  29. Falling into a pit
  30. Enemies hurt
  31. Stomp!
  32. Squashed enemies are gone
  33. Game over
  34. Play again
  35. The flag
  36. Reach the flag
  37. Build it yourself: jump buffering