Skip to content

Commit

Permalink
fix: map outer coords(window) into inner coords(canvas)
Browse files Browse the repository at this point in the history
  • Loading branch information
threedalpeng committed Jan 29, 2024
1 parent 9d85e67 commit 230cfbe
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/lib/canvas/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ export class CanvasContext {

handleMouseEvent(ev: MouseEvent) {
const bounding = this.canvas.getBoundingClientRect();
const [r, g, b] = this.hitContext2d.getImageData(
ev.clientX - bounding.left,
ev.clientY - bounding.top,
1,
1
).data;
const [x, y] = [
((ev.clientX - bounding.left) * this.canvas.width) / bounding.width,
((ev.clientY - bounding.top) * this.canvas.height) / bounding.height
];
const [r, g, b] = this.hitContext2d.getImageData(x, y, 1, 1).data;
const code = `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
const onHit = this.#onHitMap.get(code);
if (onHit !== undefined) {
Expand Down

0 comments on commit 230cfbe

Please sign in to comment.