From 59b83ac606254ed5c0d880539352db8dde027dec Mon Sep 17 00:00:00 2001 From: Camila Alvarez Date: Thu, 9 Jan 2020 13:01:22 -0300 Subject: [PATCH 1/2] Fix issue where y coordinate isn't calculated correctly on iOS devices, when the canvas is placed on a modal --- src/index.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 813b72e..12c69ad 100644 --- a/src/index.js +++ b/src/index.js @@ -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; - // 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 }; }; From fb580e6a9c343c519d7e37cf3b03702fa8ac4c95 Mon Sep 17 00:00:00 2001 From: Camila Alvarez Date: Thu, 9 Jan 2020 14:45:19 -0300 Subject: [PATCH 2/2] Changed file style to es6 --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 12c69ad..0dc70ba 100644 --- a/src/index.js +++ b/src/index.js @@ -341,10 +341,10 @@ export default class extends PureComponent { const rect = this.canvas.interface.getBoundingClientRect(); // use cursor pos as default - var clientX = e.clientX; - var clientY = e.clientY; + let clientX = e.clientX; + let clientY = e.clientY; - var offset = { + const offset = { top: rect.top + document.body.scrollTop, left: rect.left + document.body.scrollLeft };