fix(viewer): handle touchpad zoom gestures and prevent page zoom in 3D viewer

This commit is contained in:
yiling
2026-10-11 10:20:40 +08:00
parent 7722b6cdc8
commit 22c518d70c
3 changed files with 106 additions and 7 deletions
+1
View File
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Fixed
- **viewer**: Fix an issue where zooming with a touchpad in the 3D asset viewer zoomed the entire browser page instead of zooming the model viewer area
- **viewer**: Fix an issue where certain angles in the 3D asset viewer appeared stuck on the previous frame, and eliminate view jitter when rotating at 345°
- **viewer**: Fix an issue where 3D asset auto-rotation during playback skipped 15° increments in the degree indicator
@@ -271,4 +271,73 @@ describe('ModelViewer', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (HTMLVideoElement.prototype as any).cancelVideoFrameCallback
})
it('zooms viewer and prevents browser default on touchpad pinch (ctrl+wheel)', () => {
const { getByTestId, getByRole } = render(<ModelViewer file={mockAsset} />)
const surface = getByTestId('model-viewer-surface')
const resetZoomBtn = getByRole('button', { name: 'Reset zoom' })
const initialZoomText = resetZoomBtn.textContent
surface.getBoundingClientRect = () =>
({
left: 0,
top: 0,
width: 800,
height: 600,
right: 800,
bottom: 600,
x: 0,
y: 0,
toJSON: () => {},
}) as DOMRect
const wheelEvent = new WheelEvent('wheel', {
deltaY: -100,
deltaMode: 0,
bubbles: true,
cancelable: true,
})
Object.defineProperty(wheelEvent, 'ctrlKey', { value: true })
Object.defineProperty(wheelEvent, 'clientX', { value: 400 })
Object.defineProperty(wheelEvent, 'clientY', { value: 300 })
let notPrevented = true
act(() => {
notPrevented = surface.dispatchEvent(wheelEvent)
})
expect(notPrevented).toBe(false)
expect(resetZoomBtn.textContent).not.toBe(initialZoomText)
})
it('pans viewer content and prevents scroll on two-finger wheel when zoomed in', () => {
const { getByTestId, getByRole } = render(<ModelViewer file={mockAsset} />)
const surface = getByTestId('model-viewer-surface')
const zoomInBtn = getByRole('button', { name: 'Zoom in' })
// Zoom in multiple times so zoom exceeds baseScale significantly
fireEvent.click(zoomInBtn)
fireEvent.click(zoomInBtn)
fireEvent.click(zoomInBtn)
const mediaWrapper = surface.querySelector('div.absolute.pointer-events-none') as HTMLElement
const initialTransform = mediaWrapper.style.transform
const scrollEvent = new WheelEvent('wheel', {
deltaX: 20,
deltaY: 30,
bubbles: true,
cancelable: true,
})
Object.defineProperty(scrollEvent, 'ctrlKey', { value: false })
let notPrevented = true
act(() => {
notPrevented = surface.dispatchEvent(scrollEvent)
})
expect(notPrevented).toBe(false)
expect(mediaWrapper.style.transform).not.toBe(initialTransform)
})
})
@@ -5,6 +5,7 @@ import DrawingCanvas from '@/ui/components/drawing-canvas'
import { useAnnotationStore } from '@/ui/stores/annotation-store'
import type { FileViewerProps, MediaController } from '../types'
import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom'
import { usePanZoomGestures } from '../use-pan-zoom'
import { ModelControlBar } from './model-control-bar'
import {
secondToDegree,
@@ -45,6 +46,10 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
const [pan, setPan] = useState({ x: 0, y: 0 })
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
useEffect(() => {
setHasManuallyZoomed(false)
}, [data.id])
// Drag-to-rotate state
const [isDraggingRotation, setIsDraggingRotation] = useState(false)
const dragStartRef = useRef<{ clientX: number; frame: number }>({ clientX: 0, frame: 0 })
@@ -66,6 +71,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
const mediaWidth = data.media?.metadata?.originalWidth || 1080
const mediaHeight = data.media?.metadata?.originalHeight || 1080
const baseScale = fitScale(containerSize.width, containerSize.height, mediaWidth, mediaHeight)
// Resize observer
useEffect(() => {
@@ -86,13 +92,31 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
// Initial scale and center
useEffect(() => {
if (containerSize.width > 0 && containerSize.height > 0 && !hasManuallyZoomed) {
const scale = fitScale(containerSize.width, containerSize.height, mediaWidth, mediaHeight)
setZoom(scale)
setZoom(baseScale)
setPan(
centeredPan(containerSize.width, containerSize.height, mediaWidth, mediaHeight, scale),
centeredPan(
containerSize.width,
containerSize.height,
mediaWidth,
mediaHeight,
baseScale,
),
)
}
}, [containerSize, mediaWidth, mediaHeight, hasManuallyZoomed])
}, [containerSize, mediaWidth, mediaHeight, baseScale, hasManuallyZoomed])
usePanZoomGestures({
containerRef,
zoom,
pan,
baseScale,
onZoomChange: (next) => {
setHasManuallyZoomed(true)
setZoom(next.zoom)
setPan(next.pan)
},
onPanChange: setPan,
})
// Current angle
const currentDegree = secondToDegree(currentTime)
@@ -310,10 +334,15 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
const handleZoomReset = () => {
setHasManuallyZoomed(false)
if (containerSize.width > 0 && containerSize.height > 0) {
const scale = fitScale(containerSize.width, containerSize.height, mediaWidth, mediaHeight)
setZoom(scale)
setZoom(baseScale)
setPan(
centeredPan(containerSize.width, containerSize.height, mediaWidth, mediaHeight, scale),
centeredPan(
containerSize.width,
containerSize.height,
mediaWidth,
mediaHeight,
baseScale,
),
)
}
}