From f0065a0cda520d807ecd9176851c3e70d20964bb Mon Sep 17 00:00:00 2001 From: tekki mariani Date: Fri, 20 Mar 2026 19:29:53 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20zoom-to-mouse=20using=20ge?= =?UTF-8?q?tWorldPoint=20diff=20instead=20of=20manual=20formula?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/systems/CameraSystem.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/systems/CameraSystem.ts b/src/systems/CameraSystem.ts index 4a7ec2c..2b765c9 100644 --- a/src/systems/CameraSystem.ts +++ b/src/systems/CameraSystem.ts @@ -54,19 +54,20 @@ export class CameraSystem { // Scroll wheel zoom — zoom toward mouse pointer position this.scene.input.on('wheel', (ptr: Phaser.Input.Pointer, _objs: unknown, _dx: number, dy: number) => { - const oldZoom = cam.zoom - const newZoom = Phaser.Math.Clamp(oldZoom - Math.sign(dy) * ZOOM_STEP, MIN_ZOOM, MAX_ZOOM) - if (newZoom === oldZoom) return + const newZoom = Phaser.Math.Clamp(cam.zoom - Math.sign(dy) * ZOOM_STEP, MIN_ZOOM, MAX_ZOOM) + if (newZoom === cam.zoom) return - // World point under mouse before zoom - const worldX = cam.scrollX + ptr.x / oldZoom - const worldY = cam.scrollY + ptr.y / oldZoom + // Sample the world point under the mouse BEFORE the zoom change + const before = cam.getWorldPoint(ptr.x, ptr.y) cam.setZoom(newZoom) - // Adjust scroll so the same world point stays under the mouse - cam.scrollX = worldX - ptr.x / newZoom - cam.scrollY = worldY - ptr.y / newZoom + // Sample the same screen position AFTER zoom — it now maps to a different world point + const after = cam.getWorldPoint(ptr.x, ptr.y) + + // Shift scroll by the difference so the original world point snaps back under the mouse + cam.scrollX -= after.x - before.x + cam.scrollY -= after.y - before.y }) // Middle-click pan: start on button down