mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 09:16:55 +00:00
fix(viewer): eliminate 3D model rotation jitter at 345 degrees
This commit is contained in:
@@ -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(<ModelViewer file={mockAsset} />)
|
||||
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(
|
||||
<ModelViewer file={mockAsset} onTimeUpdate={onTimeUpdate} />,
|
||||
)
|
||||
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°')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -187,8 +187,9 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
(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<MediaController, FileViewerProps>(
|
||||
// 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<HTMLDivElement>) => {
|
||||
@@ -394,7 +396,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
ref={videoRef}
|
||||
src={videoSrc}
|
||||
playsInline
|
||||
loop
|
||||
loop={isPlaying}
|
||||
muted
|
||||
autoPlay={autoPlay}
|
||||
preload="auto"
|
||||
@@ -415,12 +417,6 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
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')
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user