diff --git a/packages/webui/components/viewers/model/model-viewer.test.tsx b/packages/webui/components/viewers/model/model-viewer.test.tsx index 2cdf46d4..b8095f84 100644 --- a/packages/webui/components/viewers/model/model-viewer.test.tsx +++ b/packages/webui/components/viewers/model/model-viewer.test.tsx @@ -340,4 +340,40 @@ describe('ModelViewer', () => { expect(notPrevented).toBe(false) expect(mediaWrapper.style.transform).not.toBe(initialTransform) }) + + it('keeps video loop attribute disabled when paused to prevent browser EOF reset, and enables loop when playing', () => { + const { container } = render() + const video = container.querySelector('video') as HTMLVideoElement + expect(video).toBeDefined() + + // Paused initially: loop should be false so seeking to final frame does not trigger browser loop-reset + expect(video.loop).toBe(false) + + // Play: loop should become true + fireEvent.play(video) + expect(video.loop).toBe(true) + + // Pause: loop should become false again + fireEvent.pause(video) + expect(video.loop).toBe(false) + }) + + it('seeks safely to 345° without touching 4.0s EOF boundary', () => { + const onTimeUpdate = vi.fn() + const { container, getByTestId } = render( + , + ) + const video = container.querySelector('video') as HTMLVideoElement + const degreeReadout = getByTestId('degree-readout') + + // At 0°, press ArrowLeft to rotate counterclockwise to 345° (Frame 23) + fireEvent.keyDown(window, { key: 'ArrowLeft' }) + + // Target time should be 23 / 6s = 3.8333s + expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(23 / 6, 2)) + // Video currentTime should be safely offset from 4.0s EOF (3.875s) + expect(video.currentTime).toBeCloseTo(3.875, 3) + expect(video.currentTime).toBeLessThan(4.0) + expect(degreeReadout.textContent).toContain('345° / 360°') + }) }) diff --git a/packages/webui/components/viewers/model/model-viewer.tsx b/packages/webui/components/viewers/model/model-viewer.tsx index b75f42c9..bb9728f1 100644 --- a/packages/webui/components/viewers/model/model-viewer.tsx +++ b/packages/webui/components/viewers/model/model-viewer.tsx @@ -187,8 +187,9 @@ export const ModelViewer = React.forwardRef( (frameIndex: number) => { const normalizedFrame = ((frameIndex % TOTAL_FRAMES) + TOTAL_FRAMES) % TOTAL_FRAMES const targetSec = normalizedFrame * FRAME_DURATION + const centerSec = calculateFrameCenterTime(normalizedFrame) if (videoRef.current) { - videoRef.current.currentTime = calculateFrameCenterTime(normalizedFrame) + videoRef.current.currentTime = centerSec } setCurrentTime(targetSec) onTimeUpdate?.(targetSec) @@ -287,7 +288,8 @@ export const ModelViewer = React.forwardRef( // 12px drag movement corresponds to 1 frame (15°) // Dragging right (deltaX > 0) rotates turntable counterclockwise (decreasing frame index) const deltaFrames = Math.round(deltaX / 12) - seekToFrame(dragStartRef.current.frame - deltaFrames) + const targetFrame = dragStartRef.current.frame - deltaFrames + seekToFrame(targetFrame) } const handleViewerPointerUp = (e: React.PointerEvent) => { @@ -394,7 +396,7 @@ export const ModelViewer = React.forwardRef( ref={videoRef} src={videoSrc} playsInline - loop + loop={isPlaying} muted autoPlay={autoPlay} preload="auto" @@ -415,12 +417,6 @@ export const ModelViewer = React.forwardRef( onEnded={() => { setIsPlaying(false) }} - onTimeUpdate={() => { - if (videoRef.current && !isDraggingRotation && !isPlaying) { - setCurrentTime(videoRef.current.currentTime) - onTimeUpdateRef.current?.(videoRef.current.currentTime) - } - }} onError={() => { setIsLoading(false) setError('Failed to load turntable video') diff --git a/packages/webui/components/viewers/model/utils.test.ts b/packages/webui/components/viewers/model/utils.test.ts index bfeb0ffd..01d7adf6 100644 --- a/packages/webui/components/viewers/model/utils.test.ts +++ b/packages/webui/components/viewers/model/utils.test.ts @@ -14,11 +14,15 @@ describe('model viewer utils', () => { expect(calculateFrameCenterTime(0)).toBeCloseTo(0.5 / FPS) }) - it('calculates the center time for all frames 0 to 23', () => { - for (let i = 0; i < TOTAL_FRAMES; i++) { + it('calculates presentation times safely within frame boundaries for all frames 0 to 23', () => { + for (let i = 0; i < TOTAL_FRAMES - 1; i++) { const center = calculateFrameCenterTime(i) expect(center).toBeCloseTo((i + 0.5) * FRAME_DURATION) } + // For the final frame (frame 23, 345°), ensure it is safely offset away from EOF (4.0s) + const lastCenter = calculateFrameCenterTime(TOTAL_FRAMES - 1) + expect(lastCenter).toBeCloseTo((TOTAL_FRAMES - 1) * FRAME_DURATION + FRAME_DURATION * 0.25) + expect(lastCenter).toBeLessThan(TOTAL_FRAMES * FRAME_DURATION) }) it('handles negative and wrapping frames properly', () => { diff --git a/packages/webui/components/viewers/model/utils.ts b/packages/webui/components/viewers/model/utils.ts index 958d01b5..a01ceea7 100644 --- a/packages/webui/components/viewers/model/utils.ts +++ b/packages/webui/components/viewers/model/utils.ts @@ -3,11 +3,17 @@ export const FPS = 6 export const FRAME_DURATION = 1 / FPS /** - * Calculates the presentation time at the center of a frame. - * Seeking to the frame center avoids PTS boundary inaccuracies in browser video decoders. + * Calculates the presentation time within a frame. + * Seeking near the frame center avoids PTS boundary inaccuracies in browser video decoders. + * For the final frame (TOTAL_FRAMES - 1), we use a safe offset inside the frame + * to avoid grazing the EOF boundary (video duration) which can cause browser decoders + * to trigger end-of-stream or loop behavior. */ export function calculateFrameCenterTime(frameIndex: number): number { const normalizedFrame = ((frameIndex % TOTAL_FRAMES) + TOTAL_FRAMES) % TOTAL_FRAMES + if (normalizedFrame === TOTAL_FRAMES - 1) { + return normalizedFrame * FRAME_DURATION + FRAME_DURATION * 0.25 + } return (normalizedFrame + 0.5) * FRAME_DURATION }