diff --git a/src/app/app.ts b/src/app/app.ts index 4408754a..d38e49da 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -21,6 +21,7 @@ import { } from "../utils"; import GAMEPAD_MAP from "../data/gamepad.json"; +import { gfx } from "../kaplay"; import { type ButtonBinding, type ButtonsDef, @@ -867,7 +868,10 @@ export const initApp = (opt: { const pd = opt.pixelDensity || 1; canvasEvents.mousemove = (e) => { - const mousePos = new Vec2(e.offsetX, e.offsetY); + const mousePos = new Vec2( + e.offsetX - gfx.viewport.x, + e.offsetY - gfx.viewport.y, + ); const mouseDeltaPos = new Vec2(e.movementX, e.movementY); if (isFullscreen()) { diff --git a/src/game/camera.ts b/src/game/camera.ts index 2e8d2be7..4ad5f217 100644 --- a/src/game/camera.ts +++ b/src/game/camera.ts @@ -1,5 +1,5 @@ import { color, fixed, opacity, rect } from "../components"; -import { center, height, width } from "../gfx"; +import { center, getViewportScale, height, width } from "../gfx"; import { game } from "../kaplay"; import { type Color, rgb } from "../math/color"; import { type Mat4, type Vec2, vec2, type Vec2Args } from "../math/math"; @@ -50,9 +50,13 @@ export function shake(intensity: number = 12) { } export function toScreen(p: Vec2): Vec2 { - return game.cam.transform.multVec2(p); + const viewportScale = getViewportScale(); + + return game.cam.transform.multVec2(p).scale(viewportScale); } export function toWorld(p: Vec2): Vec2 { - return game.cam.transform.invert().multVec2(p); + const viewportFactor = 1 / getViewportScale(); + + return game.cam.transform.invert().multVec2(p).scale(viewportFactor); }