Skip to content

Commit

Permalink
making sure bbox is there
Browse files Browse the repository at this point in the history
  • Loading branch information
JosuaKrause committed Jul 28, 2023
1 parent 85e923c commit b236b6e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions js/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,20 @@ export async function loadText(path) {
return (await fetch(path)).text();
}

function getBBox(elem) {
if (elem.getBoundingClientRect) {
return elem.getBoundingClientRect();
}
return {
left: elem.offsetLeft,
top: elem.offsetTop,
width: elem.offsetWidth,
height: elem.offsetHeight,
};
}

export function convertMousePosition(canvas, measures, snap, e) {
const rect = canvas.getBoundingClientRect();
const rect = getBBox(canvas);
const pixelX = ((e.clientX - rect.left) / rect.width) * measures.width;
const pixelY = ((e.clientY - rect.top) / rect.height) * measures.height;
const halfW = measures.width * 0.5;
Expand All @@ -145,7 +157,7 @@ export function convertMousePosition(canvas, measures, snap, e) {
export function convertTouchPosition(canvas, measures, snap, e) {
const [x, y, w] = [...e.touches].reduce(
(p, t) => {
const weight = t.force;
const weight = Number.isFinite(+t.force) ? t.force : 1;
return [
p[0] + t.clientX * weight,
p[1] + t.clientY * weight,
Expand All @@ -155,8 +167,8 @@ export function convertTouchPosition(canvas, measures, snap, e) {
[0, 0, 0],
);
return convertMousePosition(canvas, measures, snap, {
clientX: x / w,
clientY: y / w,
clientX: w > 0 ? x / w : x,
clientY: w > 0 ? y / w : y,
});
}

Expand Down

0 comments on commit b236b6e

Please sign in to comment.