Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Fix issue where y coordinate isn't calculated correctly on iOS #58

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,23 +333,38 @@ export default class extends PureComponent {
canvas.style.height = height;
};

isTouchEvent = e => {
return e.type.indexOf('touch') !== -1;
}

getPointerPos = e => {
const rect = this.canvas.interface.getBoundingClientRect();

// use cursor pos as default
let clientX = e.clientX;
let clientY = e.clientY;
var clientX = e.clientX;
var clientY = e.clientY;
CamilaAlvarez marked this conversation as resolved.
Show resolved Hide resolved

// use first touch if available
var offset = {
top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft
};
// use first touch if available
if (e.changedTouches && e.changedTouches.length > 0) {
clientX = e.changedTouches[0].clientX;
clientY = e.changedTouches[0].clientY;
}
if (this.isTouchEvent(e)) {
clientX = clientX - offset.left;
clientY = clientY - offset.top;
} else {
clientX = clientX - rect.left;
clientY = clientY - rect.top;
}

// return mouse/touch position inside canvas
return {
x: clientX - rect.left,
y: clientY - rect.top
x: clientX ,
y: clientY
};
};

Expand Down