# Camera with zoom that follows the player in javascript

I'm passing the time programming a little game in javascript, I've done what I'm trying this in several in game engine. And what I'm trying to do is generate the world while the player walks around the map. Using engines like unity and godot, I have always had the facilities to deal with the camera and the scene. Now with pure javascript I'm kind of not knowing where to go.

Come on, what I have so far:

A world generated by 2D noise and my character in it can move with the keys of the keyboard. What I want to do now is that when walking he generate the world and also the camera accompany him. And that with the scroll of the mouse I can take the camera to a bigger zoom so that I can better visualize the generated world.

So there are 3 questions I have:

1 - How to create a camera that will follow the player, already thinking about the best way to make the world be generated when walking the player.

2 - How to generate the world while he walks (This I believe I can do.)

3 - How to zoom in with the mouse scroll?

Making the camera follow the player is relatively simple. If you're using canvas, then simply use the rendering context's translate function to translate the current origin to the player's position, then use the scale function to create the zoom effect and lastly translate the camera in the other direction by half of the canvas size to center the player. It's important to not mix these up, because that would make the scaling origin messed up.

In code this would look something like this

ctx.resetTransform(); // You need to make sure to include this
// Keep in mind though, resetTransform doesn't exist in some browsers, you may want to use setTransform instead
ctx.translate(playerPositionX, playerPositionY);
ctx.scale(currentZoom);
ctx.translate(-canvas.width / 2, -canvas.height / 2);


To learn how to modify the zoom using the scroll wheel, read this, but basically, listen to the wheel event and use the deltaY value it gives you.

If you aren't using canvas (e. g. divs, svg-s), then this is much harder. Make sure everything has a position. when the player moves, move everything the same amount in the other direction. When the user zooms in, get the distance vector pointing from the player to the div, multiply it by the amount the scene zooms in (e.g. a value of 2 makes everything twice as big) and add it to the player's position to get the new position of the object. Also make sure the change the size of the divs too.

• When not all your target browsers support resetTransform, then you might want to work with context.save and context.restore (it doesn't save and restore the canvas image as one would think. It only saves and restores the current drawing settings, including the transformation). – Philipp Jul 10 '18 at 9:02
• Thanks for the answer, I got here yesterday to solve question 1 and 2, just missing the same zoom I will see right now. I'm doing with "context.save and context.restore". – PerduGames Jul 10 '18 at 11:18
• @Bálint Could you show an example of how to use "Wheel Event" correctly? I tried and I can not do it here. – PerduGames Jul 10 '18 at 20:57
• I got this way: document.addEventListener('wheel', function(e) { if(e.deltaY >= 2) { ctx.scale(0.5, 0.5); } }); – PerduGames Jul 10 '18 at 21:12
• @PerduGames create a variable that stores the current zoom ("currentZoom" in the code above) and modify that – Bálint Jul 10 '18 at 21:22