I guys,
I’m fairly new to Phaser and still playing around a bit.
I have come so far to create a world map that gets data from a map created in Tiled and tiles and objects are working as it should.
I have also created a method to handle panning around the world map on mousedown and also a zoom method on wheel event.
However, i’m having some issues figuring out how to properly calculate how I keep zooming in on the same position on the map.
For example when I place my cursor on an island on the map and start zooming, it doesn’t zoom in on the island, but panning to the right of the viewport, if that makes sense.
This is my code:
in the create() method I have this for the “wheel” event:
this.input.on('wheel', (pointer, gameObjects, deltaX, deltaY, deltaZ) => {
this.handleZoom(pointer, deltaX, deltaY)
});
The handleZoom method:
handleZoom(pointer, deltaX, deltaY) {
const zoomDirection = deltaY > 0 ? -1 : 1;
let newZoom = Phaser.Math.Clamp(
this.cameras.main.zoom + zoomDirection * this.zoomState.zoomStep,
this.zoomState.minZoom,
this.zoomState.maxZoom
);
this.cameras.main.setZoom(newZoom);
}
I’ve have tried adding this to the handleZoom method:
this.cameras.main.pan(pointer.worldX, pointer.worldY)
But that doesn’t seem to do the trick.
Does anyone know the trick to do this properly?
Thanks!