Skip to content

🎥 Make videos programmatically with React

fix timeline position

Jonny Burger14783f004f03
diff --git a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
index 1001bece1b..8a4847d5c0 100644
--- a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
@@ -31,9 +31,10 @@ let lastTimelinePositionWhileScrolling: TimelinePosition | null = null;
export const TimelinePlayCursorSyncer: React.FC = () => {
const video = Internals.useVideo();
const timelineContext = useContext(Internals.Timeline.TimelineContext);
+ const timelinePosition = Internals.Timeline.useTimelinePosition();
const {zoom} = useContext(TimelineZoomCtx);
- setCurrentFrame(timelineContext.frame);
+ setCurrentFrame(timelinePosition);
setCurrentZoom(zoom);
setCurrentDuration(video?.durationInFrames ?? 1);
setCurrentFps(video?.fps ?? 1);
@@ -55,9 +56,9 @@ export const TimelinePlayCursorSyncer: React.FC = () => {
ensureFrameIsInViewport({
direction: timelineContext.playbackRate > 0 ? 'page-right' : 'page-left',
durationInFrames: video.durationInFrames,
- frame: timelineContext.frame,
+ frame: timelinePosition,
});
- }, [playing, timelineContext, video]);
+ }, [playing, timelineContext, timelinePosition, video]);
/**
* Restore state if `enter` is being pressed
diff --git a/packages/core/src/timeline-position-state.ts b/packages/core/src/timeline-position-state.ts
index b66ef0850f..61670b9f9f 100644
--- a/packages/core/src/timeline-position-state.ts
+++ b/packages/core/src/timeline-position-state.ts
@@ -1,5 +1,6 @@
import type {MutableRefObject} from 'react';
import {createContext, useContext, useMemo} from 'react';
+import {useUnsafeVideoConfig} from './use-unsafe-video-config.js';
export type PlayableMediaTag = {
play: () => void;
@@ -45,8 +46,17 @@ export const SetTimelineContext = createContext<SetTimelineContextValue>({
});
export const useTimelinePosition = (): number => {
+ const videoConfig = useUnsafeVideoConfig();
const state = useContext(TimelineContext);
- return state.frame;
+
+ // A dynamically calculated duration using calculateMetadata()
+ // may lead to the frame being bigger than the duration.
+ // If we have the config, we clamp the frame to the duration.
+ if (!videoConfig) {
+ return state.frame;
+ }
+
+ return Math.min(videoConfig.durationInFrames - 1, state.frame);
};
export const useTimelineSetFrame = (): ((