Skip to content

Commit

Permalink
fix: screenToPageCoordinates should not offset by contentArea
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Feb 19, 2024
1 parent 652c320 commit bf24511
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sandbox/tests/pointer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pointer</title>
<style>
canvas {
/* canvas {
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, 50%);
}
} */
</style>
</head>
<body>
Expand Down
15 changes: 14 additions & 1 deletion sandbox/tests/pointer/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,23 @@ var game2 = new Game2({
width: 600,
height: 400,
antialiasing: false,
displayMode: ex.DisplayMode.Fixed
displayMode: ex.DisplayMode.FitScreenAndFill
});
game2.debug.collider.showBounds = true;
game2.debug.graphics.showBounds = true;
game2.toggleDebug();

game2.input.pointers.primary.on('down', (evt) => {
const pos = game2.screen.worldToPageCoordinates(evt.worldPos);
const div = document.createElement('div');
div.style.left = pos.x + 'px';
div.style.top = pos.y + 'px';
div.style.position = 'absolute';
div.style.width = '100px';
div.style.height = '100px';
div.style.zIndex = '999';
div.style.backgroundColor = 'black';
document.body.appendChild(div);
});

game2.initialize();
4 changes: 1 addition & 3 deletions src/engine/Screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,7 @@ export class Screen {
let newX = point.x;
let newY = point.y;

// offset by content area
newX = newX + this.contentArea.left;
newY = newY + this.contentArea.top;
// no need to offset by content area, drawing is already offset by this

newX = (newX / this.resolution.width) * this.viewport.width;
newY = (newY / this.resolution.height) * this.viewport.height;
Expand Down

0 comments on commit bf24511

Please sign in to comment.