From 73fa7a7b5925c33f07998e27aa49d82f02be8c7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kry=C5=A1tof=20Rainisch?= Date: Sat, 6 Aug 2022 00:17:53 +0200 Subject: [PATCH] Change pointer position on touch events. While the `pointerup` and `pointerdown` work with touch `pointermoved` does not get called, which means cursor coordinates stay at `{x: 0, y: 0}` making it impossible to react on click on specific part of the canvas. This commit adds listeners for `touchmove`, `touchstart` and `touchend` to update the cursor position. --- src/run.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/run.js b/src/run.js index 24abefa..4993f24 100755 --- a/src/run.js +++ b/src/run.js @@ -140,6 +140,18 @@ export function run(program, runSettings, userData = {}) { pointer.pressed = false eventQueue.push('pointerUp') }) + + const touchHandler = e => { + const rect = settings.element.getBoundingClientRect() + pointer.x = e.touches[0].clientX - rect.left + pointer.y = e.touches[0].clientY - rect.top + eventQueue.push('pointerMove') + } + + settings.element.addEventListener('touchmove', touchHandler) + settings.element.addEventListener('touchstart', touchHandler) + settings.element.addEventListener('touchend', touchHandler) + // CSS fix settings.element.style.fontStrech = 'normal'