From: cassowarii Date: Sun, 6 Oct 2024 23:23:31 +0000 (-0700) Subject: make it actually work on smartphones X-Git-Url: https://git.cassowary.me/gitweb.cgi?a=commitdiff_plain;h=256d5c5066500f4588c4e58f0a134924c2113cd4;p=ld56.git make it actually work on smartphones --- 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;