[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) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-03-30 09:44:40 -06:00
1 parent c1550337db
commit 7659baca01
6 files changed
+39 -8

No files matched your search

+12
View File
@@ -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)
{
+5
View File
@@ -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);
+1 -1
View File
@@ -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"
+5 -1
View File
@@ -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;
+6
View File
@@ -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)
{
+10 -6
View File
@@ -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;