Issue #5: Mouse handling — zoom-to-mouse + middle-click pan #10
@@ -52,22 +52,16 @@ export class CameraSystem {
|
|||||||
d: kb.addKey(Phaser.Input.Keyboard.KeyCodes.D),
|
d: kb.addKey(Phaser.Input.Keyboard.KeyCodes.D),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scroll wheel zoom — zoom toward mouse pointer position
|
// Scroll wheel zoom — centers the world point under the mouse, then zooms
|
||||||
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
|
||||||
|
|
||||||
// Sample the world point under the mouse BEFORE the zoom change
|
// Move the world point under the mouse to the viewport center, then zoom toward it
|
||||||
const before = cam.getWorldPoint(ptr.x, ptr.y)
|
const wp = cam.getWorldPoint(ptr.x, ptr.y)
|
||||||
|
cam.scrollX = wp.x - cam.width / (2 * newZoom)
|
||||||
|
cam.scrollY = wp.y - cam.height / (2 * newZoom)
|
||||||
cam.setZoom(newZoom)
|
cam.setZoom(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
|
// Middle-click pan: start on button down
|
||||||
|
|||||||
Reference in New Issue
Block a user