Skip to content

Commit

Permalink
fix: Allow CSS transforms on canvas (#2910)
Browse files Browse the repository at this point in the history
This PR fixes an issue where CSS transforms confused the excalibur pointer system and caused clicks to land in the wrong spot
  • Loading branch information
eonarheim authored Feb 1, 2024
1 parent c1e6900 commit 80c960b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed issue where using CSS transforms on the canvas confused Excalibur pointers
- Fixed issue with *AndFill suffixed [[DisplayModes]]s where content area offset was not accounted for in world space
- Fixed issue where `ex.Sound.getTotalPlaybackDuration()` would crash if not loaded, now logs friendly warning
- Fixed issue where an empty constructor on `new ex.Label()` would crash
Expand Down
8 changes: 8 additions & 0 deletions sandbox/tests/pointer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pointer</title>
<style>
canvas {
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, 50%);
}
</style>
</head>
<body>
<p>Click on the squares to see events propagate in z order in the console. The black square cancels the event preventing propagation.</p>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/tests/pointer/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var game2 = new Game2({
width: 600,
height: 400,
antialiasing: false,
displayMode: ex.DisplayMode.FitScreenAndFill
displayMode: ex.DisplayMode.Fixed
});
game2.debug.collider.showBounds = true;
game2.debug.graphics.showBounds = true;
Expand Down
26 changes: 4 additions & 22 deletions src/engine/Util/Util.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import { Vector } from '../Math/vector';
import { Vector, vec } from '../Math/vector';
import { Clock } from './Clock';
import { Future } from './Future';

/**
* Find the screen position of an HTML element
*/
export function getPosition(el: HTMLElement): Vector {
let oLeft: number = 0,
oTop: number = 0;

const calcOffsetLeft = (parent: HTMLElement) => {
oLeft += parent.offsetLeft;

if (parent.offsetParent) {
calcOffsetLeft(<HTMLElement>parent.offsetParent);
}
};
const calcOffsetTop = (parent: HTMLElement) => {
oTop += parent.offsetTop;
if (parent.offsetParent) {
calcOffsetTop(<HTMLElement>parent.offsetParent);
}
};

calcOffsetLeft(el);
calcOffsetTop(el);

return new Vector(oLeft, oTop);
// do we need the scroll too? technically the offset method before did that
const rect = el.getBoundingClientRect();
return vec(rect.x + window.scrollX, rect.y + window.scrollY);
}

/**
Expand Down

0 comments on commit 80c960b

Please sign in to comment.