Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fromScreen not using viewport scale, mousePos() not working with letterbox/stretch #436

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "../utils";

import GAMEPAD_MAP from "../data/gamepad.json" assert { type: "json" };
import { gfx } from "../kaplay";
import {
type ButtonBinding,
type ButtonsDef,
Expand Down Expand Up @@ -868,7 +869,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()) {
Expand Down
10 changes: 7 additions & 3 deletions src/game/camera.ts
Original file line number Diff line number Diff line change
@@ -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 Mat23, type Vec2, vec2, type Vec2Args } from "../math/math";
Expand Down Expand Up @@ -50,9 +50,13 @@
}

export function toScreen(p: Vec2): Vec2 {
return game.cam.transform.transformPoint(p, vec2());
const viewportScale = getViewportScale();

return game.cam.transform.multVec2(p).scale(viewportScale);

Check failure on line 55 in src/game/camera.ts

View workflow job for this annotation

GitHub Actions / build

Property 'multVec2' does not exist on type 'Mat23'.

Check failure on line 55 in src/game/camera.ts

View workflow job for this annotation

GitHub Actions / build

Property 'multVec2' does not exist on type 'Mat23'.
}

export function toWorld(p: Vec2): Vec2 {
return game.cam.transform.inverse.transformPoint(p, vec2());
const viewportFactor = 1 / getViewportScale();

return game.cam.transform.invert().multVec2(p).scale(viewportFactor);

Check failure on line 61 in src/game/camera.ts

View workflow job for this annotation

GitHub Actions / build

Property 'invert' does not exist on type 'Mat23'.

Check failure on line 61 in src/game/camera.ts

View workflow job for this annotation

GitHub Actions / build

Property 'invert' does not exist on type 'Mat23'.
}
Loading