🐛 zoom-to-mouse: use ptr.worldX/Y + set scroll after setZoom
This commit is contained in:
@@ -52,16 +52,19 @@ export class CameraSystem {
|
|||||||
d: kb.addKey(Phaser.Input.Keyboard.KeyCodes.D),
|
d: kb.addKey(Phaser.Input.Keyboard.KeyCodes.D),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scroll wheel zoom — centers the world point under the mouse, then zooms
|
// Scroll wheel zoom — zoom toward mouse pointer
|
||||||
this.scene.input.on('wheel', (ptr: Phaser.Input.Pointer, _objs: unknown, _dx: number, dy: number) => {
|
this.scene.input.on('wheel', (ptr: Phaser.Input.Pointer, _objs: unknown, _dx: number, dy: number) => {
|
||||||
const newZoom = Phaser.Math.Clamp(cam.zoom - Math.sign(dy) * ZOOM_STEP, MIN_ZOOM, MAX_ZOOM)
|
const newZoom = Phaser.Math.Clamp(cam.zoom - Math.sign(dy) * ZOOM_STEP, MIN_ZOOM, MAX_ZOOM)
|
||||||
if (newZoom === cam.zoom) return
|
if (newZoom === cam.zoom) return
|
||||||
|
|
||||||
// Move the world point under the mouse to the viewport center, then zoom toward it
|
// ptr.worldX/Y is pre-computed by Phaser and represents the world position under the pointer
|
||||||
const wp = cam.getWorldPoint(ptr.x, ptr.y)
|
const worldX = ptr.worldX
|
||||||
cam.scrollX = wp.x - cam.width / (2 * newZoom)
|
const worldY = ptr.worldY
|
||||||
cam.scrollY = wp.y - cam.height / (2 * newZoom)
|
|
||||||
|
// Apply zoom first, then reposition scroll so worldX stays under ptr.x
|
||||||
cam.setZoom(newZoom)
|
cam.setZoom(newZoom)
|
||||||
|
cam.scrollX = worldX - ptr.x / newZoom
|
||||||
|
cam.scrollY = worldY - ptr.y / newZoom
|
||||||
})
|
})
|
||||||
|
|
||||||
// Middle-click pan: start on button down
|
// Middle-click pan: start on button down
|
||||||
|
|||||||
Reference in New Issue
Block a user