From 7659baca01ca0cbb13889503c9bb9f8cbbb8a5f1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 30 Mar 2026 09:44:40 -0600 Subject: [PATCH] [gpu] Pinch-zoom around pinch center Adds demo_view_zoom_around() which scales while compensating translation so the midpoint between fingers stays fixed. Same maps-like behavior as the pinch rotation. Co-Authored-By: Claude Opus 4.6 (1M context) --- util/gpu/demo-view.cc | 12 ++++++++++++ util/gpu/demo-view.h | 5 +++++ util/gpu/web/build.sh | 2 +- util/gpu/web/hb-gpu-web-webgpu.cc | 6 +++++- util/gpu/web/hb-gpu-web.cc | 6 ++++++ util/gpu/web/shell.html | 16 ++++++++++------ 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/util/gpu/demo-view.cc b/util/gpu/demo-view.cc index c41f06208..7b32ede7d 100644 --- a/util/gpu/demo-view.cc +++ b/util/gpu/demo-view.cc @@ -505,6 +505,18 @@ demo_view_scroll_func (demo_view_t *vu, double xoffset, double yoffset) vu->needs_redraw = true; } +void +demo_view_zoom_around (demo_view_t *vu, double factor, + double cx, double cy, + int width, int height) +{ + demo_view_scale (vu, factor, factor); + demo_view_translate (vu, + +(2. * cx / width - 1) * (1 - factor), + -(2. * cy / height - 1) * (1 - factor)); + vu->needs_redraw = true; +} + void demo_view_rotate_z (demo_view_t *vu, double angle) { diff --git a/util/gpu/demo-view.h b/util/gpu/demo-view.h index 212bc9e1b..59f1ed184 100644 --- a/util/gpu/demo-view.h +++ b/util/gpu/demo-view.h @@ -45,6 +45,11 @@ demo_view_mouse_func (demo_view_t *vu, int button, int action, int mods); void demo_view_scroll_func (demo_view_t *vu, double xoffset, double yoffset); +void +demo_view_zoom_around (demo_view_t *vu, double factor, + double cx, double cy, + int width, int height); + void demo_view_rotate_z (demo_view_t *vu, double angle); diff --git a/util/gpu/web/build.sh b/util/gpu/web/build.sh index 216bc55d0..f21bb2144 100755 --- a/util/gpu/web/build.sh +++ b/util/gpu/web/build.sh @@ -31,7 +31,7 @@ em++ \ -sUSE_WEBGL2=1 \ -sFULL_ES3=1 \ -sALLOW_MEMORY_GROWTH=1 \ - -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_rotate_z","_malloc","_free"]' \ + -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_zoom_around","_web_rotate_z","_malloc","_free"]' \ -sEXPORTED_RUNTIME_METHODS='["UTF8ToString","lengthBytesUTF8","stringToUTF8"]' \ --shell-file "$SRCDIR/util/gpu/web/shell.html" \ -o "$OUTDIR/index.html" diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index 4083904c3..d50e13abc 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -552,7 +552,11 @@ on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud) double dist = sqrt (dx * dx + dy * dy); double angle = atan2 (dy, dx); if (pinch_dist > 0) - demo_view_scroll_func (vu, 0, (dist - pinch_dist) * 0.05); + { + double mx = (e->touches[0].targetX + e->touches[1].targetX) / 2.0; + double my = (e->touches[0].targetY + e->touches[1].targetY) / 2.0; + demo_view_zoom_around (vu, dist / pinch_dist, mx, my, css_w, css_h); + } double dAngle = angle - pinch_angle; if (dAngle > M_PI) dAngle -= 2 * M_PI; if (dAngle < -M_PI) dAngle += 2 * M_PI; diff --git a/util/gpu/web/hb-gpu-web.cc b/util/gpu/web/hb-gpu-web.cc index 3d9890116..a2cb0be8c 100644 --- a/util/gpu/web/hb-gpu-web.cc +++ b/util/gpu/web/hb-gpu-web.cc @@ -120,6 +120,12 @@ web_get_text () return current_text; } +EMSCRIPTEN_KEEPALIVE void +web_zoom_around (float factor, float cx, float cy, int w, int h) +{ + demo_view_zoom_around (vu, factor, cx, cy, w, h); +} + EMSCRIPTEN_KEEPALIVE void web_rotate_z (float angle, float cx, float cy, int w, int h) { diff --git a/util/gpu/web/shell.html b/util/gpu/web/shell.html index 210eadd46..db75d1735 100644 --- a/util/gpu/web/shell.html +++ b/util/gpu/web/shell.html @@ -227,13 +227,17 @@ space: animate · b: dark mode · g: gamma · s: stem · d: debug · r: reset · /* Zoom from distance change */ var change = dist - pinchDist; if (Math.abs(change) >= 2) { + var factor = dist / pinchDist; pinchDist = dist; - var delta = change / dist * 5; - canvas.dispatchEvent(new WheelEvent('wheel', { - deltaY: -delta * 120, - clientX: (t[0].clientX + t[1].clientX) / 2, - clientY: (t[0].clientY + t[1].clientY) / 2 - })); + var mx = (t[0].clientX + t[1].clientX) / 2; + var my = (t[0].clientY + t[1].clientY) / 2; + if (typeof _web_zoom_around === 'function') + _web_zoom_around(factor, mx, my, canvas.clientWidth, canvas.clientHeight); + else { + /* Fallback to wheel event */ + var delta = (factor - 1) * 5; + canvas.dispatchEvent(new WheelEvent('wheel', { deltaY: -delta * 120 })); + } } /* Rotate from angle change */ var dAngle = angle - pinchAngle;