[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) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-03-30 09:40:56 -06:00
1 parent 16c30ac052
commit c1550337db
5 files changed
+38 -5

No files matched your search

+21
View File
@@ -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)
{
+5
View File
@@ -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);
+5 -1
View File
@@ -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;
}
+2 -2
View File
@@ -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" */
+5 -2
View File
@@ -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});