feat: latch touch mode on first real touch, add ?touch=1 test hook

(pointer: coarse) misses DevTools device modes and hybrid laptops, so any
real touchstart now arms the thumbs permanently (title page hands the
proof to controls/touch.js via __microduckTouched). ?touch=1 forces the
overlay on any device for desktop testing.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
tfrere
2026-08-21 15:55:53 +02:00
parent 55c1238fd0
commit 0110fa1f33
2 changed files with 27 additions and 11 deletions
+13 -5
View File
@@ -15,10 +15,11 @@
// holds the beak open (axes.jaw = 1) while held.
//
// The overlay DOM lives in index.html (#touch-ui); this module only wires
// pointer events to it. `connected` mirrors (pointer: coarse) - rl.js
// reflects it onto body.touch-mode, which is what actually shows the
// overlay - so a desktop never sees the thumbs and a phone always does,
// even before the first touch.
// pointer events to it. `connected` (mirrored onto body.touch-mode by
// rl.js, which is what actually shows the overlay) arms two ways:
// - (pointer: coarse) matches: phones/tablets, before any touch;
// - OR the first real touch anywhere: catches DevTools device modes and
// hybrid laptops the media query misses. Latching, like a gamepad.
const TOUCH_ALPHA = 0.18; // EMA smoothing toward the stick target
const TOUCH_DEADZONE = 0.12; // normalized deflection ignored around center
@@ -44,9 +45,16 @@ export class TouchSource {
init() {
this.#mq = window.matchMedia("(pointer: coarse)");
this.#onMq = () => { this.connected = this.#mq.matches; };
// Latching: once a device has proven it can touch, keep the thumbs.
// __microduckTouched carries a touch seen by the title page before
// this module booted.
if (window.__microduckTouched) this.connected = true;
this.#onMq = () => { this.connected = this.connected || this.#mq.matches; };
this.#mq.addEventListener("change", this.#onMq);
this.#onMq();
const armTouch = () => { this.connected = true; };
window.addEventListener("touchstart", armTouch, { once: true, passive: true });
this.#disposers.push(() => window.removeEventListener("touchstart", armTouch));
const zone = document.getElementById("touch-zone");
const stick = document.getElementById("touch-stick");
+14 -6
View File
@@ -1280,12 +1280,20 @@
pollPad();
// Touch tutorial variant needs the class before rl.js boots; the
// game loop keeps mirroring the same media query afterwards.
// game loop keeps mirroring the touch source afterwards. Latching:
// (pointer: coarse) OR any real touch proves the device (DevTools
// device modes and hybrid laptops often fail the media query), and
// the __microduckTouched flag hands the proof to controls/touch.js.
const coarse = matchMedia("(pointer: coarse)");
const pollTouch = () =>
document.body.classList.toggle("touch-mode", coarse.matches);
coarse.addEventListener("change", pollTouch);
pollTouch();
const armTouch = () => {
window.__microduckTouched = true;
document.body.classList.add("touch-mode");
};
if (coarse.matches) armTouch();
else coarse.addEventListener("change", () => coarse.matches && armTouch());
window.addEventListener("touchstart", armTouch, { once: true, passive: true });
// ?touch=1 forces the thumbs on any device - desktop testing hook.
if (new URLSearchParams(location.search).get("touch") === "1") armTouch();
// ── Menu open / close ────────────────────────────────────────────
const open = () => {
@@ -1368,7 +1376,7 @@
appended: a plain src attribute can 401 when the browser blocks the
*.static.hf.space auth cookie inside the hub iframe. -->
<script type="module">
const V = "67"; // bump on rl.js changes to defeat heuristic caching
const V = "68"; // bump on rl.js changes to defeat heuristic caching
const sign = new URLSearchParams(location.search).get("__sign");
const suffix = sign ? `&__sign=${encodeURIComponent(sign)}` : "";
import(`./rl.js?v=${V}${suffix}`);