Skip to content

🎥 Make videos programmatically with React

implement scrolling with keyboard arrows

Jonny Burgerb1bd0c33d789
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index 3be8331a69..7c3064db4d 100644
--- a/packages/cli/src/editor/components/PlayPause.tsx
+++ b/packages/cli/src/editor/components/PlayPause.tsx
@@ -10,6 +10,7 @@ import {StepBack} from '../icons/step-back';
import {StepForward} from '../icons/step-forward';
import {useTimelineInOutFramePosition} from '../state/in-out';
import {ControlButton} from './ControlButton';
+import {ensureFrameIsInViewport} from './Timeline/timeline-scroll-logic';
const forwardBackStyle = {
height: 16,
@@ -76,6 +77,7 @@ export const PlayPause: React.FC<{
);
const videoFps = video?.fps ?? null;
+ const durationInFrames = video?.durationInFrames ?? null;
const onArrowLeft = useCallback(
(e: KeyboardEvent) => {
@@ -83,17 +85,28 @@ export const PlayPause: React.FC<{
return null;
}
+ if (durationInFrames === null) {
+ return null;
+ }
+
e.preventDefault();
if (e.altKey) {
seek(0);
+ ensureFrameIsInViewport('backwards', durationInFrames, 0);
} else if (e.shiftKey) {
frameBack(videoFps);
+ ensureFrameIsInViewport(
+ 'backwards',
+ durationInFrames,
+ frame - videoFps
+ );
} else {
frameBack(1);
+ ensureFrameIsInViewport('backwards', durationInFrames, frame - 1);
}
},
- [frameBack, seek, videoFps]
+ [durationInFrames, frame, frameBack, seek, videoFps]
);
const onArrowRight = useCallback(
@@ -104,15 +117,26 @@ export const PlayPause: React.FC<{
if (e.altKey) {
seek(video.durationInFrames - 1);
+ ensureFrameIsInViewport(
+ 'forward',
+ video.durationInFrames - 1,
+ video.durationInFrames - 1
+ );
} else if (e.shiftKey) {
frameForward(video.fps);
+ ensureFrameIsInViewport(
+ 'forward',
+ video.durationInFrames,
+ frame + video.fps
+ );
} else {
frameForward(1);
+ ensureFrameIsInViewport('forward', video.durationInFrames, frame + 1);
}
e.preventDefault();
},
- [frameForward, seek, video]
+ [frame, frameForward, seek, video]
);
const oneFrameBack = useCallback(() => {
diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
index 54c7926bd4..0dcc8291f5 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
@@ -9,7 +9,6 @@ import React, {
} from 'react';
import {Internals} from 'remotion';
import {useGetXPositionOfItemInTimeline} from '../../helpers/get-left-of-timeline-slider';
-import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
import {
useTimelineInOutFramePosition,
useTimelineSetInOutFramePosition,
@@ -22,6 +21,9 @@ import {
getFrameFromX,
getFrameWhileScrollingLeft,
getFrameWhileScrollingRight,
+ getScrollPositionForCursorOnLeftEdge,
+ getScrollPositionForCursorOnRightEdge,
+ scrollToTimelineXOffset,
} from './timeline-scroll-logic';
import {inMarkerAreaRef, outMarkerAreaRef} from './TimelineInOutPointer';
import {
@@ -86,10 +88,6 @@ export const TimelineDragHandler: React.FC = () => {
const {playing, play, pause, seek} = PlayerInternals.usePlayer();
const videoConfig = Internals.useUnsafeVideoConfig();
- const frameIncrement =
- ((width as number) - TIMELINE_PADDING * 2) /
- ((videoConfig?.durationInFrames as number) - 1);
-
const scroller = useRef<NodeJS.Timeout | null>(null);
const stopInterval = () => {
@@ -174,7 +172,6 @@ export const TimelineDragHandler: React.FC = () => {
width,
'clamp'
);
- const current = scrollableRef.current as HTMLDivElement;
if (isLeftOfArea && canScrollTimelineIntoDirection().canScrollLeft) {
if (scroller.current) {
@@ -192,13 +189,14 @@ export const TimelineDragHandler: React.FC = () => {
width
);
- const scrollPos = frameIncrement * nextFrame;
+ const scrollPos = getScrollPositionForCursorOnLeftEdge({
+ nextFrame,
+ durationInFrames: videoConfig.durationInFrames,
+ });
redrawTimelineSliderFast.current?.draw(nextFrame);
seek(nextFrame);
- current.scroll({
- left: scrollPos,
- });
+ scrollToTimelineXOffset(scrollPos);
};
scrollEvery();
@@ -222,26 +220,16 @@ export const TimelineDragHandler: React.FC = () => {
const nextFrame = getFrameWhileScrollingRight({
durationInFrames: videoConfig.durationInFrames,
width,
- frameIncrement,
});
- const framesRemaining = videoConfig.durationInFrames - 1 - nextFrame;
-
- const fromRight = framesRemaining * frameIncrement + TIMELINE_PADDING;
-
- const scrollPos =
- (scrollableRef.current?.scrollWidth as number) -
- fromRight -
- (scrollableRef.current?.clientWidth as number) +
- TIMELINE_PADDING +
- 4; // clearfix;
+ const scrollPos = getScrollPositionForCursorOnRightEdge({
+ nextFrame,
+ durationInFrames: videoConfig.durationInFrames,
+ });
redrawTimelineSliderFast.current?.draw(nextFrame);
seek(nextFrame);
-
- current.scroll({
- left: scrollPos,
- });
+ scrollToTimelineXOffset(scrollPos);
};
scrollEvery();
@@ -254,7 +242,7 @@ export const TimelineDragHandler: React.FC = () => {
seek(frame);
}
},
- [videoConfig, dragging.dragging, left, width, frameIncrement, seek]
+ [videoConfig, dragging.dragging, left, width, seek]
);
const onPointerMoveInOut = useCallback(
diff --git a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts
index 1bafbd8f76..e6863e7e81 100644
--- a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts
+++ b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts
@@ -1,6 +1,7 @@
import {interpolate} from 'remotion';
import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
import {scrollableRef} from './timeline-refs';
+import {redrawTimelineSliderFast} from './TimelineSlider';
export const canScrollTimelineIntoDirection = () => {
const current = scrollableRef.current as HTMLDivElement;
@@ -16,19 +17,17 @@ export const SCROLL_INCREMENT = 200;
const calculateFrameWhileScrollingRight = ({
durationInFrames,
width,
- frameIncrement,
scrollLeft,
}: {
durationInFrames: number;
width: number;
- frameIncrement: number;
scrollLeft: number;
}) => {
return (
getFrameFromX(scrollLeft, durationInFrames, width, 'clamp') +
Math.ceil(
((scrollableRef.current?.clientWidth as number) - TIMELINE_PADDING) /
- frameIncrement
+ getFrameIncrement(durationInFrames)
)
);
};
@@ -54,26 +53,123 @@ export const getFrameWhileScrollingLeft = (
return Math.max(0, Math.min(currentFrame - 1, nextFrame));
};
+export const ensureFrameIsInViewport = (
+ direction: 'backwards' | 'forward',
+ durationInFrames: number,
+ frame: number
+) => {
+ redrawTimelineSliderFast.current?.draw(frame);
+ const width = scrollableRef.current?.scrollWidth ?? 0;
+ if (direction === 'backwards') {
+ const currentFrameLeft = getFrameFromX(
+ scrollableRef.current?.scrollLeft as number,
+ durationInFrames,
+ width,
+ 'clamp'
+ );
+ const scrollPos = getScrollPositionForCursorOnLeftEdge({
+ nextFrame: frame,
+ durationInFrames,
+ });
+ const needsToScrollLeft =
+ scrollPos <=
+ getScrollPositionForCursorOnLeftEdge({
+ nextFrame: currentFrameLeft,
+ durationInFrames,
+ });
+ if (needsToScrollLeft) {
+ scrollToTimelineXOffset(scrollPos);
+ }
+ }
+
+ if (direction === 'forward') {
+ const currentFrameRight = calculateFrameWhileScrollingRight({
+ durationInFrames,
+ scrollLeft: scrollableRef.current?.scrollLeft as number,
+ width,
+ });
+ console.log(currentFrameRight, frame);
+ const scrollPos = getScrollPositionForCursorOnRightEdge({
+ nextFrame: frame,
+ durationInFrames,
+ });
+ const needsToScrollRight =
+ scrollPos >=
+ getScrollPositionForCursorOnRightEdge({
+ nextFrame: currentFrameRight,
+ durationInFrames,
+ });
+ if (needsToScrollRight) {
+ scrollToTimelineXOffset(scrollPos);
+ }
+ }
+};
+
+export const scrollToTimelineXOffset = (scrollPos: number) => {
+ scrollableRef.current?.scroll({
+ left: scrollPos,
+ });
+};
+
+export const getScrollPositionForCursorOnLeftEdge = ({
+ nextFrame,
+ durationInFrames,
+}: {
+ nextFrame: number;
+ durationInFrames: number;
+}) => {
+ const frameIncrement = getFrameIncrement(durationInFrames);
+ const scrollPos = frameIncrement * nextFrame;
+ return scrollPos;
+};
+
+export const getScrollPositionForCursorOnRightEdge = ({
+ nextFrame,
+ durationInFrames,
+}: {
+ nextFrame: number;
+ durationInFrames: number;
+}) => {
+ const frameIncrement = getFrameIncrement(durationInFrames);
+ const framesRemaining = durationInFrames - 1 - nextFrame;
+
+ const fromRight = framesRemaining * frameIncrement + TIMELINE_PADDING;
+
+ const scrollPos =
+ (scrollableRef.current?.scrollWidth as number) -
+ fromRight -
+ (scrollableRef.current?.clientWidth as number) +
+ TIMELINE_PADDING +
+ 4; // clearfix;
+
+ return scrollPos;
+};
+
+export const getFrameIncrement = (durationInFrames: number) => {
+ const width = scrollableRef.current?.scrollWidth ?? 0;
+
+ return (
+ ((width as number) - TIMELINE_PADDING * 2) /
+ ((durationInFrames as number) - 1)
+ );
+};
+
export const getFrameWhileScrollingRight = ({
durationInFrames,
width,
- frameIncrement,
}: {
durationInFrames: number;
width: number;
- frameIncrement: number;
}) => {
const nextFrame = calculateFrameWhileScrollingRight({
durationInFrames,
width,
- frameIncrement,
scrollLeft:
(scrollableRef.current?.scrollLeft as number) + SCROLL_INCREMENT,
});
const currentFrame = calculateFrameWhileScrollingRight({
durationInFrames,
width,
- frameIncrement,
scrollLeft: scrollableRef.current?.scrollLeft as number,
});