From 256d5c5066500f4588c4e58f0a134924c2113cd4 Mon Sep 17 00:00:00 2001 From: cassowarii Date: Sun, 6 Oct 2024 16:23:31 -0700 Subject: [PATCH] make it actually work on smartphones --- zucchinibread.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/zucchinibread.js b/zucchinibread.js index d3c820a..7ce6ff5 100644 --- a/zucchinibread.js +++ b/zucchinibread.js @@ -576,11 +576,15 @@ let zb = (function() { /* ---- Touch (defaults to same as mouse) ---- */ + let _last_touch_event; + function _handle_touchstart(game, e) { if (!game.playing) return; if (e.touches) e = e.touches[0]; + _last_touch_event = e; + const rect = game.canvas.getBoundingClientRect(); let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1; let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1; @@ -602,8 +606,8 @@ let zb = (function() { } const rect = game.canvas.getBoundingClientRect(); - let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1; - let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1; + let x = Math.round((_last_touch_event.clientX - rect.left) / rect.width * game.screen_w) - 1; + let y = Math.round((_last_touch_event.clientY - rect.top) / rect.height * game.screen_h) - 1; if (game.events.touchend) { game.events.touchend(game, e, x, y); } else if (game.events.mouseup) { @@ -619,6 +623,8 @@ let zb = (function() { if (e.touches) e = e.touches[0]; + _last_touch_event = e; + const rect = game.canvas.getBoundingClientRect(); let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1; let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1; -- 1.8.3.1