-
-
Notifications
You must be signed in to change notification settings - Fork 962
/
use-current-frame.ts
33 lines (28 loc) 路 1.27 KB
/
use-current-frame.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {useContext} from 'react';
import {CanUseRemotionHooks} from './CanUseRemotionHooks.js';
import {SequenceContext} from './SequenceContext.js';
import {getRemotionEnvironment} from './get-remotion-environment.js';
import {useTimelinePosition} from './timeline-position-state.js';
/**
* @description Get the current frame of the video. Frames are 0-indexed, meaning the first frame is 0, the last frame is the duration of the composition in frames minus one.
* @see [Documentation](https://remotion.dev/docs/use-current-frame)
*/
export const useCurrentFrame = (): number => {
const canUseRemotionHooks = useContext(CanUseRemotionHooks);
if (!canUseRemotionHooks) {
if (getRemotionEnvironment().isPlayer) {
throw new Error(
`useCurrentFrame can only be called inside a component that was passed to <Player>. See: https://www.remotion.dev/docs/player/examples`,
);
}
throw new Error(
`useCurrentFrame() can only be called inside a component that was registered as a composition. See https://www.remotion.dev/docs/the-fundamentals#defining-compositions`,
);
}
const frame = useTimelinePosition();
const context = useContext(SequenceContext);
const contextOffset = context
? context.cumulatedFrom + context.relativeFrom
: 0;
return frame - contextOffset;
};