From 216c70dbd9c3de378a41e4e355af6b18ab092505 Mon Sep 17 00:00:00 2001 From: tekki mariani Date: Fri, 20 Mar 2026 20:15:13 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20zoom-to-mouse:=20use=20ptr.world?= =?UTF-8?q?X/Y=20+=20set=20scroll=20after=20setZoom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/systems/CameraSystem.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/systems/CameraSystem.ts b/src/systems/CameraSystem.ts index 3c079f8..a463c75 100644 --- a/src/systems/CameraSystem.ts +++ b/src/systems/CameraSystem.ts @@ -52,16 +52,19 @@ export class CameraSystem { 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) => { const newZoom = Phaser.Math.Clamp(cam.zoom - Math.sign(dy) * ZOOM_STEP, MIN_ZOOM, MAX_ZOOM) if (newZoom === cam.zoom) return - // Move the world point under the mouse to the viewport center, then zoom toward it - const wp = cam.getWorldPoint(ptr.x, ptr.y) - cam.scrollX = wp.x - cam.width / (2 * newZoom) - cam.scrollY = wp.y - cam.height / (2 * newZoom) + // ptr.worldX/Y is pre-computed by Phaser and represents the world position under the pointer + const worldX = ptr.worldX + const worldY = ptr.worldY + + // Apply zoom first, then reposition scroll so worldX stays under ptr.x cam.setZoom(newZoom) + cam.scrollX = worldX - ptr.x / newZoom + cam.scrollY = worldY - ptr.y / newZoom }) // Middle-click pan: start on button down