From c1550337dbcf6d05001237cd20dfc4e98d54bbd2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 30 Mar 2026 09:40:56 -0600 Subject: [PATCH] [gpu] Pinch-rotate around pinch center, not screen center Adds demo_view_rotate_z_around() which compensates translation so the point between the two fingers stays fixed during rotation, like maps apps. Co-Authored-By: Claude Opus 4.6 (1M context) --- util/gpu/demo-view.cc | 21 +++++++++++++++++++++ util/gpu/demo-view.h | 5 +++++ util/gpu/web/hb-gpu-web-webgpu.cc | 6 +++++- util/gpu/web/hb-gpu-web.cc | 4 ++-- util/gpu/web/shell.html | 7 +++++-- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/util/gpu/demo-view.cc b/util/gpu/demo-view.cc index 761c4779c..c41f06208 100644 --- a/util/gpu/demo-view.cc +++ b/util/gpu/demo-view.cc @@ -515,6 +515,27 @@ demo_view_rotate_z (demo_view_t *vu, double angle) vu->needs_redraw = true; } +void +demo_view_rotate_z_around (demo_view_t *vu, double angle, + double cx, double cy, + int width, int height) +{ + /* Normalized screen coords [-1, 1] */ + double nx = (2.0 * cx / width - 1.0) / vu->scalex; + double ny = -(2.0 * cy / height - 1.0) / vu->scaley; + + /* Translate so center is at origin, rotate, translate back */ + double c = cos (angle), s = sin (angle); + vu->translate.x += nx - (nx * c - ny * s); + vu->translate.y += ny - (nx * s + ny * c); + + float dquat[4]; + float axis[3] = {0, 0, 1}; + axis_to_quat (axis, (float) angle, dquat); + add_quats (dquat, vu->quat, vu->quat); + vu->needs_redraw = true; +} + void demo_view_motion_func (demo_view_t *vu, double x, double y) { diff --git a/util/gpu/demo-view.h b/util/gpu/demo-view.h index 99f632717..212bc9e1b 100644 --- a/util/gpu/demo-view.h +++ b/util/gpu/demo-view.h @@ -48,6 +48,11 @@ demo_view_scroll_func (demo_view_t *vu, double xoffset, double yoffset); void demo_view_rotate_z (demo_view_t *vu, double angle); +void +demo_view_rotate_z_around (demo_view_t *vu, double angle, + double cx, double cy, + int width, int height); + void demo_view_motion_func (demo_view_t *vu, double x, double y); diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index 6db26b338..4083904c3 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -557,7 +557,11 @@ on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud) if (dAngle > M_PI) dAngle -= 2 * M_PI; if (dAngle < -M_PI) dAngle += 2 * M_PI; if (fabs (dAngle) > 0.01) - demo_view_rotate_z (vu, dAngle); + { + 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_rotate_z_around (vu, dAngle, mx, my, css_w, css_h); + } pinch_dist = dist; pinch_angle = angle; } diff --git a/util/gpu/web/hb-gpu-web.cc b/util/gpu/web/hb-gpu-web.cc index 5582d4cdf..3d9890116 100644 --- a/util/gpu/web/hb-gpu-web.cc +++ b/util/gpu/web/hb-gpu-web.cc @@ -121,9 +121,9 @@ web_get_text () } EMSCRIPTEN_KEEPALIVE void -web_rotate_z (float angle) +web_rotate_z (float angle, float cx, float cy, int w, int h) { - demo_view_rotate_z (vu, angle); + demo_view_rotate_z_around (vu, angle, cx, cy, w, h); } } /* extern "C" */ diff --git a/util/gpu/web/shell.html b/util/gpu/web/shell.html index 974b77862..210eadd46 100644 --- a/util/gpu/web/shell.html +++ b/util/gpu/web/shell.html @@ -241,8 +241,11 @@ space: animate · b: dark mode · g: gamma · s: stem · d: debug · r: reset · if (dAngle < -Math.PI) dAngle += 2 * Math.PI; if (Math.abs(dAngle) > 0.01) { pinchAngle = angle; - if (typeof _web_rotate_z === 'function') - _web_rotate_z(dAngle); + if (typeof _web_rotate_z === 'function') { + var mx = (t[0].clientX + t[1].clientX) / 2; + var my = (t[0].clientY + t[1].clientY) / 2; + _web_rotate_z(dAngle, mx, my, canvas.clientWidth, canvas.clientHeight); + } } } }, {passive: false});