From da26042a829c66cd3d286a8325e78b1acd70333e Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Wed, 11 Nov 2026 19:16:52 -0800 Subject: [PATCH 1/4] Show3D plays with a third of the main-thread work; resize grips follow touch and pen Playback committed React state ten times a second, and each commit rendered the whole viewer: under a 4x CPU throttle a frame cost 12 ms of main thread for one 512 px panel and 18.7 ms for three, over a 60 Hz budget on a slower laptop or a phone. Stats, histogram and star state now commit once a second while playing; the slider, counters and each panel's frame label are written to the page every frame. Measured: 4.1 ms and 5.3 ms per frame, WebGPU on the Apple adapter. The resize grips of Show3D, Show2D and Show3DSlices' volume, and Show3D's profile height, listened to mouse events only, which a finger drag never sends. They use pointer events with capture, and on touch screens the 16 px grip takes a 44 px hit area. A touch drag at Galaxy Fold cover size now resizes the panel. --- js/show2d/index.tsx | 26 ++++++++++------ js/show3d/index.tsx | 62 ++++++++++++++++++++++++++------------ js/show3d/playback.test.ts | 11 +++++++ js/show3d/playback.ts | 7 +++++ js/show3dslices/index.tsx | 17 +++++++---- 5 files changed, 87 insertions(+), 36 deletions(-) diff --git a/js/show2d/index.tsx b/js/show2d/index.tsx index 695a796d7..1c2b77e1b 100644 --- a/js/show2d/index.tsx +++ b/js/show2d/index.tsx @@ -940,6 +940,8 @@ function Show2D() { touchAction: "none", zIndex: 5, "&:hover": { opacity: 1 }, + // A finger needs about 44 px: the grip stays 16 px drawn, its hit area grows on touch screens. + "@media (pointer: coarse)": { opacity: 0.9, "&::before": { content: '""', position: "absolute", inset: -14 } }, }), [themeColors.accent]); const [showStats] = useModelState("show_stats"); const [statsMean] = useModelState("stats_mean"); @@ -7018,7 +7020,9 @@ function Show2D() { // Resize Handlers // ------------------------------------------------------------------------- - const handleCanvasResizeStart = (e: React.MouseEvent) => { + // Pointer events, so a finger or a pen drags the grip as a mouse does (mouse events never arrive for a touch drag). + const handleCanvasResizeStart = (e: React.PointerEvent) => { + if (e.pointerType === "mouse" && e.button !== 0) return; e.stopPropagation(); e.preventDefault(); canvasResizeCleanupRef.current?.(); @@ -7033,7 +7037,7 @@ function Show2D() { let rafId = 0; let latestSize = start.size; - const handleMouseMove = (event: MouseEvent) => { + const handleMouseMove = (event: PointerEvent) => { const delta = Math.max(event.clientX - start.x, event.clientY - start.y); latestSize = Math.max(200, start.size + delta); if (!rafId) { @@ -7050,15 +7054,17 @@ function Show2D() { applyResizeViewAnchor(latestSize); setCanvasSize(latestSize); setPanelWidthPxTrait(Math.round(latestSize)); - document.removeEventListener("mousemove", handleMouseMove); - document.removeEventListener("mouseup", finishResize); + document.removeEventListener("pointermove", handleMouseMove); + document.removeEventListener("pointerup", finishResize); + document.removeEventListener("pointercancel", finishResize); window.removeEventListener("blur", finishResize); canvasResizeCleanupRef.current = null; window.setTimeout(() => { canvasResizeViewAnchorRef.current = null; }, 0); }; - document.addEventListener("mousemove", handleMouseMove); - document.addEventListener("mouseup", finishResize); + document.addEventListener("pointermove", handleMouseMove); + document.addEventListener("pointerup", finishResize); + document.addEventListener("pointercancel", finishResize); window.addEventListener("blur", finishResize); canvasResizeCleanupRef.current = finishResize; }; @@ -8629,7 +8635,7 @@ function Show2D() { )} {showResizeControls && ( @@ -8798,7 +8804,7 @@ function Show2D() { )} {showResizeControls && ( { + onPointerDown={(e: React.PointerEvent) => { e.stopPropagation(); handleCanvasResizeStart(e); }} @@ -8904,7 +8910,7 @@ function Show2D() { {renderPanelAnnotations(0)} {renderPanelFrameControls(0)} {showResizeControls && ( - + )} )} @@ -9458,7 +9464,7 @@ function Show2D() { )} {showResizeControls && ( - + )} {/* FFT Stats Bar */} diff --git a/js/show3d/index.tsx b/js/show3d/index.tsx index 2e8816c96..084617a73 100644 --- a/js/show3d/index.tsx +++ b/js/show3d/index.tsx @@ -1497,6 +1497,8 @@ function Show3D() { touchAction: "none", zIndex: 5, "&:hover": { opacity: 1 }, + // A finger needs about 44 px: the grip stays 16 px drawn, its hit area grows on touch screens. + "@media (pointer: coarse)": { opacity: 0.9, "&::before": { content: '""', position: "absolute", inset: -14 } }, }), [themeColors.accent]); const [statsMean] = useModelState("stats_mean"); const [statsMin] = useModelState("stats_min"); @@ -4153,9 +4155,11 @@ function Show3D() { setLogScale(nextLogScale); }, [logScale, setLogScale, setVmaxPerPanel, setVminPerPanel]); + // Frame labels for the per-panel titles, written straight to the page while playing (set beside frameLabelForIndex). + const frameLabelForIndexRef = React.useRef<((idx: number) => string) | undefined>(undefined); const updatePlaybackLiveControls = React.useCallback((idx: number) => { const settings = playRef.current; - writeLiveFrameControls(rootRef.current, playbackSliderRef.current, playbackLiveCountRef.current, idx, Math.max(1, settings.nSlices || nSlices || 1), settings.loop); + writeLiveFrameControls(rootRef.current, playbackSliderRef.current, playbackLiveCountRef.current, idx, Math.max(1, settings.nSlices || nSlices || 1), settings.loop, frameLabelForIndexRef.current); }, [nSlices]); const viewportTransformActive = React.useCallback(() => { @@ -4973,7 +4977,11 @@ function Show3D() { const settings = playRef.current; const effectiveFps = clampPlaybackFps(settings.fps); const intervalMs = playbackIntervalMs(effectiveFps); - const uiUpdateIntervalMs = effectiveFps >= 60 ? 250 : 100; + // React commits (stats, histogram, star state) once a second while playing: each commit renders the whole + // viewer, and at 10 per second they were most of the main-thread time a frame cost (18.5 of 18.5 ms vs 3.5 ms + // under a 4x CPU throttle, three 512 px panels). The slider, counters and frame labels are written straight + // to the page every frame by updatePlaybackLiveControls. + const uiUpdateIntervalMs = 1000; // First tick paints immediately; otherwise every playback start drops // one frame before the cadence timer is even allowed to run. @@ -6956,7 +6964,7 @@ function Show3D() { // Profile height resize React.useEffect(() => { if (!isResizingProfile) return; - const handleMouseMove = (e: MouseEvent) => { + const handleMouseMove = (e: PointerEvent) => { if (!profileResizeStart) return; const delta = e.clientY - profileResizeStart.y; setProfileHeight(Math.max(40, Math.min(300, profileResizeStart.height + delta))); @@ -6965,11 +6973,13 @@ function Show3D() { setIsResizingProfile(false); setProfileResizeStart(null); }; - document.addEventListener("mousemove", handleMouseMove); - document.addEventListener("mouseup", handleMouseUp); + document.addEventListener("pointermove", handleMouseMove); + document.addEventListener("pointerup", handleMouseUp); + document.addEventListener("pointercancel", handleMouseUp); return () => { - document.removeEventListener("mousemove", handleMouseMove); - document.removeEventListener("mouseup", handleMouseUp); + document.removeEventListener("pointermove", handleMouseMove); + document.removeEventListener("pointerup", handleMouseUp); + document.removeEventListener("pointercancel", handleMouseUp); }; }, [isResizingProfile, profileResizeStart]); @@ -9717,9 +9727,15 @@ function Show3D() { }; // Resize handlers - const handleMainResizeStart = (e: React.MouseEvent) => { + // Pointer events with capture, so a mouse, a finger (Galaxy Fold, touch laptops) and a pen all drag the grip: + // mouse events alone never arrive for a touch drag. + const handleMainResizeStart = (e: React.PointerEvent) => { + if (e.pointerType === "mouse" && e.button !== 0) return; e.stopPropagation(); e.preventDefault(); + const grip = e.currentTarget; + const pointerId = e.pointerId; + try { grip.setPointerCapture(pointerId); } catch { /* capture is best effort */ } const rect = canvasContainerRef.current?.getBoundingClientRect(); const startSize = rect && rect.width > 0 ? rect.width : mainCanvasSize; const startX = e.clientX; @@ -9727,7 +9743,8 @@ function Show3D() { const visiblePanels = Math.max(1, visiblePanelCount || 1); let rafId = 0; let latestSize = startSize; - const handleMouseMove = (e: MouseEvent) => { + const handleMouseMove = (e: PointerEvent) => { + if (e.pointerId !== pointerId) return; const delta = Math.max(e.clientX - startX, e.clientY - startY); const nextSize = startSize + delta; // Absolute minimum: 200 px per panel column. Lets reader shrink BELOW @@ -9743,16 +9760,20 @@ function Show3D() { }); } }; - const handleMouseUp = () => { + const handleMouseUp = (e: PointerEvent) => { + if (e.pointerId !== pointerId) return; cancelAnimationFrame(rafId); setMainCanvasSize(latestSize); const colsLocal = panelColsForCount(visiblePanels); setCanvasSizeTrait(Math.round(latestSize / colsLocal)); - document.removeEventListener("mousemove", handleMouseMove); - document.removeEventListener("mouseup", handleMouseUp); + document.removeEventListener("pointermove", handleMouseMove); + document.removeEventListener("pointerup", handleMouseUp); + document.removeEventListener("pointercancel", handleMouseUp); + try { grip.releasePointerCapture(pointerId); } catch { /* already released */ } }; - document.addEventListener("mousemove", handleMouseMove); - document.addEventListener("mouseup", handleMouseUp); + document.addEventListener("pointermove", handleMouseMove); + document.addEventListener("pointerup", handleMouseUp); + document.addEventListener("pointercancel", handleMouseUp); }; const clampSlice = (idx: number) => Math.max(0, Math.min(nSlices - 1, Math.round(idx))); @@ -9763,6 +9784,7 @@ function Show3D() { if (!text || text === String(idx) || text === String(idx + 1)) return ""; return text; }, [labels]); + frameLabelForIndexRef.current = frameLabelForIndex; const formatFrameValueLabel = React.useCallback((idx: number) => { const rounded = clampSlice(idx); const label = frameLabelForIndex(rounded); @@ -11446,7 +11468,7 @@ function Show3D() { overflowWrap: "anywhere", }} > - {panelTitleContent(panel)}{frameLabel ? ` · ${frameLabel}` : ""}{" "} + {panelTitleContent(panel)}{frameLabel ? ` · ${frameLabel}` : ""}{" "} {`${shown}/${total}`} @@ -11740,7 +11762,7 @@ function Show3D() { return ( {showResizeControls && (
{ e.preventDefault(); setIsResizingProfile(true); setProfileResizeStart({ y: e.clientY, height: profileHeight }); }} - style={{ width: "100%", height: 4, cursor: "ns-resize", borderLeft: `1px solid ${themeColors.border}`, borderRight: `1px solid ${themeColors.border}`, borderBottom: `1px solid ${themeColors.border}`, background: `linear-gradient(to bottom, ${themeColors.border}, transparent)` }} + onPointerDown={(e) => { e.preventDefault(); setIsResizingProfile(true); setProfileResizeStart({ y: e.clientY, height: profileHeight }); }} + style={{ width: "100%", height: 4, cursor: "ns-resize", touchAction: "none", borderLeft: `1px solid ${themeColors.border}`, borderRight: `1px solid ${themeColors.border}`, borderBottom: `1px solid ${themeColors.border}`, background: `linear-gradient(to bottom, ${themeColors.border}, transparent)` }} /> )} @@ -12391,7 +12413,7 @@ function Show3D() {