[gpu] Add pinch-to-rotate alongside pinch-to-zoom

Two-finger pinch now does both zoom (distance change) and Z-axis
rotation (angle change) simultaneously. Adds demo_view_rotate_z()
for direct Z-axis rotation from touch input.

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:35:53 -06:00
1 parent 29f1e02c03
commit 16c30ac052
6 files changed
+54 -12

No files matched your search

+10
View File
@@ -505,6 +505,16 @@ demo_view_scroll_func (demo_view_t *vu, double xoffset, double yoffset)
vu->needs_redraw = true;
}
void
demo_view_rotate_z (demo_view_t *vu, double angle)
{
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)
{
+3
View File
@@ -45,6 +45,9 @@ 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_rotate_z (demo_view_t *vu, double angle);
void
demo_view_motion_func (demo_view_t *vu, double x, double y);
+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","_malloc","_free"]' \
-sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_rotate_z","_malloc","_free"]' \
-sEXPORTED_RUNTIME_METHODS='["UTF8ToString","lengthBytesUTF8","stringToUTF8"]' \
--shell-file "$SRCDIR/util/gpu/web/shell.html" \
-o "$OUTDIR/index.html"
+9
View File
@@ -501,6 +501,7 @@ on_keydown (int type, const EmscriptenKeyboardEvent *e, void *ud)
/* Touch handling */
static double pinch_dist;
static double pinch_angle;
static bool three_finger_active;
static EM_BOOL
@@ -517,6 +518,7 @@ on_touchstart (int type, const EmscriptenTouchEvent *e, void *ud)
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);
}
else if (e->numTouches >= 3)
{
@@ -548,9 +550,16 @@ on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud)
double dx = e->touches[1].targetX - e->touches[0].targetX;
double dy = e->touches[1].targetY - e->touches[0].targetY;
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 dAngle = angle - pinch_angle;
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);
pinch_dist = dist;
pinch_angle = angle;
}
return EM_TRUE;
}
+6
View File
@@ -120,6 +120,12 @@ web_get_text ()
return current_text;
}
EMSCRIPTEN_KEEPALIVE void
web_rotate_z (float angle)
{
demo_view_rotate_z (vu, angle);
}
} /* extern "C" */
static void
+25 -11
View File
@@ -114,7 +114,7 @@ Shortcuts: Text editor:
<div id="help">
<span id="help-desktop">drag: pan · scroll: zoom · click: animate · right-drag: rotate<br>
space: animate · b: dark mode · g: gamma · s: stem · d: debug · r: reset · ?: help</span>
<span id="help-mobile">drag: pan · pinch: zoom · 3-finger: rotate · tap: animate</span>
<span id="help-mobile">drag: pan · pinch: zoom/rotate · 3-finger: 3D rotate · tap: animate</span>
</div>
<canvas id="canvas" oncontextmenu="event.preventDefault()" tabindex="-1"></canvas>
<script>
@@ -184,6 +184,7 @@ space: animate · b: dark mode · g: gamma · s: stem · d: debug · r: reset ·
/* Multi-touch: 2-finger pinch → zoom, 3-finger drag → 3D rotate. */
var pinchDist = 0;
var pinchAngle = 0;
var threeFingerActive = false;
canvas.addEventListener('touchstart', function (e) {
if (e.touches.length === 2) {
@@ -191,6 +192,8 @@ space: animate · b: dark mode · g: gamma · s: stem · d: debug · r: reset ·
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);
} else if (e.touches.length >= 3) {
e.preventDefault();
threeFingerActive = true;
@@ -219,17 +222,28 @@ space: animate · b: dark mode · g: gamma · s: stem · d: debug · r: reset ·
var t = e.touches;
var dist = Math.hypot(t[1].clientX - t[0].clientX,
t[1].clientY - t[0].clientY);
var angle = Math.atan2(t[1].clientY - t[0].clientY,
t[1].clientX - t[0].clientX);
/* Zoom from distance change */
var change = dist - pinchDist;
/* Dead zone: ignore jitter below 2 pixels. */
if (Math.abs(change) < 2) return;
pinchDist = dist;
/* Scale relative to finger distance for consistent feel. */
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
}));
if (Math.abs(change) >= 2) {
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
}));
}
/* Rotate from angle change */
var dAngle = angle - pinchAngle;
if (dAngle > Math.PI) dAngle -= 2 * Math.PI;
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);
}
}
}, {passive: false});
canvas.addEventListener('touchend', function (e) {