From d8f1c44fd39e41c17fe66d980c02ca128df17c18 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 30 Mar 2026 11:36:22 -0600 Subject: [PATCH] [gpu] Skip first touchmove after finger count change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prevents jumps during 3→2→1 finger transitions by dropping one frame and reinitializing pinch state for the new finger count. Co-Authored-By: Claude Opus 4.6 (1M context) --- util/gpu/web/hb-gpu-web-webgpu.cc | 17 +++++++++++++++++ util/gpu/web/shell.html | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index 1c9e4ad5d..e8e1fc910 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -516,10 +516,12 @@ static double pinch_dist; static double pinch_angle; static double pinch_cx, pinch_cy; static bool three_finger_active; +static bool touch_count_changed; static EM_BOOL on_touchstart (int type, const EmscriptenTouchEvent *e, void *ud) { + touch_count_changed = true; if (e->numTouches == 1) { demo_view_motion_func (vu, e->touches[0].targetX, e->touches[0].targetY); @@ -550,6 +552,20 @@ on_touchstart (int type, const EmscriptenTouchEvent *e, void *ud) static EM_BOOL on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud) { + if (touch_count_changed) + { + touch_count_changed = false; + if (e->numTouches == 2) + { + double dx = e->touches[1].targetX - e->touches[0].targetX; + double dy = e->touches[1].targetY - e->touches[0].targetY; + pinch_dist = sqrt (dx * dx + dy * dy); + pinch_angle = atan2 (dy, dx); + pinch_cx = (e->touches[0].targetX + e->touches[1].targetX) / 2.0; + pinch_cy = (e->touches[0].targetY + e->touches[1].targetY) / 2.0; + } + return EM_TRUE; + } if (e->numTouches >= 3 && three_finger_active) { double cx = (e->touches[0].targetX + e->touches[1].targetX + e->touches[2].targetX) / 3.0; @@ -585,6 +601,7 @@ on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud) static EM_BOOL on_touchend (int type, const EmscriptenTouchEvent *e, void *ud) { + touch_count_changed = true; if (three_finger_active && e->numTouches < 3) { three_finger_active = false; diff --git a/util/gpu/web/shell.html b/util/gpu/web/shell.html index a9b5c2f1b..4a0482f84 100644 --- a/util/gpu/web/shell.html +++ b/util/gpu/web/shell.html @@ -207,12 +207,14 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset var pinchAngle = 0; var pinchCX = 0, pinchCY = 0; var threeFingerActive = false; + var touchCountChanged = false; var longPressTimer = null; var longPressFired = false; var lastTapTime = 0; var doubleTapZoom = false; var doubleTapY = 0; canvas.addEventListener('touchstart', function (e) { + touchCountChanged = true; if (e.touches.length === 1) { var now = performance.now(); if (now - lastTapTime < 300) { @@ -262,6 +264,18 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset }, {passive: false}); var longPressX = 0, longPressY = 0; canvas.addEventListener('touchmove', function (e) { + if (touchCountChanged) { + touchCountChanged = false; + /* Reinitialize state for current finger count to avoid jumps */ + if (e.touches.length === 2) { + var t = e.touches; + pinchDist = Math.hypot(t[1].clientX - t[0].clientX, t[1].clientY - t[0].clientY); + pinchAngle = Math.atan2(t[1].clientY - t[0].clientY, t[1].clientX - t[0].clientX); + pinchCX = (t[0].clientX + t[1].clientX) / 2; + pinchCY = (t[0].clientY + t[1].clientY) / 2; + } + return; + } if (longPressTimer && e.touches.length === 1) { var dx = e.touches[0].clientX - longPressX; var dy = e.touches[0].clientY - longPressY; @@ -316,6 +330,7 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset } }, {passive: false}); canvas.addEventListener('touchend', function (e) { + touchCountChanged = true; if (longPressTimer) { clearTimeout(longPressTimer); longPressTimer = null; } if (longPressFired) { longPressFired = false;