Skip to content

Commit

Permalink
Scale cursor speed to native resolution
Browse files Browse the repository at this point in the history
Try to scale pointer coordinate deltas from a display with one resolution in the browser to a display (frame size) of the emulator.
  • Loading branch information
sergystepanov committed Feb 28, 2024
1 parent 1487be7 commit 52c54b2
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions web/js/stream/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ const stream = (() => {

if (fullscreen && !pointerLocked) {
// event.pub(POINTER_LOCK_CHANGE, screen);
await screen.requestPointerLock();
await screen.requestPointerLock(
// { unadjustedMovement: true,}
);
}

screen.onpointerdown = fullscreen ? handlePointerDown : null;
Expand All @@ -188,8 +190,29 @@ const stream = (() => {
// !to flipped
})

const scaleCursorPos = (x, y) => {
const rez = {dx: 0, dy: 0}
const nzx = x !== 0;
const nzy = y !== 0;

const arW = screen.offsetHeight * state.aspect;

const sw = arW / screen.videoWidth;
const sh = screen.offsetHeight / screen.videoHeight;

rez.dx = Math.round(x / sw);
rez.dy = Math.round(y / sh);

// revert scaled to zero values
rez.dx = (nzx && rez.dx === 0) ? x : rez.dx;
rez.dy = (nzy && rez.dy === 0) ? y : rez.dy;

return rez;
}

const handlePointerMove = (e) => {
event.pub(MOUSE_MOVED, {dx: e.movementX, dy: e.movementY});
const delta = scaleCursorPos(e.movementX, e.movementY);
event.pub(MOUSE_MOVED, delta);
}

event.sub(POINTER_LOCK_CHANGE, (lockedEl) => {
Expand Down

0 comments on commit 52c54b2

Please sign in to comment.