🎥 Make videos programmatically with React
tweak keyboard logic
Jonny Burger7720ff21c2aa
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsxindex 9f964a24b3..21aee36b16 100644--- a/packages/cli/src/editor/components/PlayPause.tsx+++ b/packages/cli/src/editor/components/PlayPause.tsx@@ -93,17 +93,17 @@ export const PlayPause: React.FC<{if (e.altKey) {seek(0);- ensureFrameIsInViewport('backwards-press', durationInFrames, 0);+ ensureFrameIsInViewport('fit-left', durationInFrames, 0);} else if (e.shiftKey) {frameBack(videoFps);ensureFrameIsInViewport(- 'backwards-press',+ 'fit-left',durationInFrames,- frame - videoFps+ Math.max(0, frame - videoFps));} else {frameBack(1);- ensureFrameIsInViewport('backwards-press', durationInFrames, frame - 1);+ ensureFrameIsInViewport('fit-left', durationInFrames, frame - 1);}},[durationInFrames, frame, frameBack, seek, videoFps]@@ -118,24 +118,20 @@ export const PlayPause: React.FC<{if (e.altKey) {seek(video.durationInFrames - 1);ensureFrameIsInViewport(- 'forwards-press',+ 'fit-right',video.durationInFrames - 1,video.durationInFrames - 1);} else if (e.shiftKey) {frameForward(video.fps);ensureFrameIsInViewport(- 'forwards-press',+ 'fit-right',video.durationInFrames,- frame + video.fps+ Math.min(video.durationInFrames - 1, frame + video.fps));} else {frameForward(1);- ensureFrameIsInViewport(- 'forwards-press',- video.durationInFrames,- frame + 1- );+ ensureFrameIsInViewport('fit-right', video.durationInFrames, frame + 1);}e.preventDefault();diff --git a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsxindex 16fca824b2..068403c0b6 100644--- a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx+++ b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx@@ -1,19 +1,95 @@import type React from 'react';-import {useEffect} from 'react';-import {Internals, useCurrentFrame} from 'remotion';-import {ensureFrameIsInViewport} from './timeline-scroll-logic';+import {useContext, useEffect, useRef} from 'react';+import {Internals} from 'remotion';+import {TimelineZoomCtx} from '../../state/timeline-zoom';+import {scrollableRef} from './timeline-refs';+import {+ ensureFrameIsInViewport,+ isCursorInViewport,+} from './timeline-scroll-logic';++// Keep track of the state when the last play happened+// so when "Enter" is pressed, we can restore the last timeline scroll position+type TimelinePosition = {+ scrollLeft: number;+ frame: number;+ zoomLevel: number;+ durationInFrames: number;+};++let lastTimelinePositionWhileScrolling: TimelinePosition | null = null;export const TimelinePlayCursorSyncer: React.FC = () => {const video = Internals.useVideo();- const frame = useCurrentFrame();+ const timelineContext = useContext(Internals.Timeline.TimelineContext);+ const {zoom} = useContext(TimelineZoomCtx);+ const currentFrame = useRef<number>(timelineContext.frame);+ const currentZoom = useRef<number>(zoom);+ const currentDuration = useRef<number>(video?.durationInFrames ?? 1);++ currentFrame.current = timelineContext.frame;+ currentZoom.current = zoom;+ currentDuration.current = video?.durationInFrames ?? 1;++ const playing = timelineContext.playing ?? false;+ /**+ * While playing (forwards or backwards), jump one viewport width to the left or right when the cursor goes out of the viewport.+ */useEffect(() => {if (!video) {return;}- ensureFrameIsInViewport('forwards-play', video.durationInFrames, frame);- }, [frame, video]);+ if (!playing) {+ return;+ }++ ensureFrameIsInViewport(+ timelineContext.playbackRate > 0 ? 'page-right' : 'page-left',+ video.durationInFrames,+ timelineContext.frame+ );+ }, [playing, timelineContext, video]);++ /**+ * Restore state if `enter` is being pressed+ */++ useEffect(() => {+ const {current} = scrollableRef;++ if (!current) {+ return;+ }++ if (playing) {+ lastTimelinePositionWhileScrolling = {+ scrollLeft: current.scrollLeft,+ frame: currentFrame.current,+ zoomLevel: currentZoom.current,+ durationInFrames: currentDuration.current,+ };+ } else if (lastTimelinePositionWhileScrolling !== null) {+ if (isCursorInViewport(currentFrame.current, currentDuration.current)) {+ return;+ }++ if (+ lastTimelinePositionWhileScrolling.zoomLevel === currentZoom.current &&+ lastTimelinePositionWhileScrolling.durationInFrames ===+ currentDuration.current+ ) {+ current.scrollLeft = lastTimelinePositionWhileScrolling.scrollLeft;+ } else {+ ensureFrameIsInViewport(+ 'center',+ currentDuration.current,+ lastTimelinePositionWhileScrolling.frame+ );+ }+ }+ }, [playing]);return null;};diff --git a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.tsindex cc22958709..fc289d823e 100644--- a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts+++ b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts@@ -53,15 +53,43 @@ export const getFrameWhileScrollingLeft = (return Math.max(0, Math.min(currentFrame - 1, nextFrame));};+export const isCursorInViewport = (frame: number, durationInFrames: number) => {+ const width = scrollableRef.current?.scrollWidth ?? 0;+ const scrollLeft = scrollableRef.current?.scrollLeft ?? 0;++ const scrollPosOnRightEdge = getScrollPositionForCursorOnRightEdge({+ nextFrame: frame,+ durationInFrames,+ });+ const scrollPosOnLeftEdge = getScrollPositionForCursorOnLeftEdge({+ nextFrame: frame,+ durationInFrames,+ });++ const currentFrameRight = calculateFrameWhileScrollingRight({+ durationInFrames,+ scrollLeft,+ width,+ });++ return !(+ scrollPosOnRightEdge >=+ getScrollPositionForCursorOnRightEdge({+ nextFrame: currentFrameRight,+ durationInFrames,+ }) || scrollPosOnLeftEdge < scrollLeft+ );+};+export const ensureFrameIsInViewport = (- direction: 'backwards-press' | 'forwards-press' | 'forwards-play',+ direction: 'fit-left' | 'fit-right' | 'page-right' | 'page-left' | 'center',durationInFrames: number,frame: number) => {redrawTimelineSliderFast.current?.draw(frame);const width = scrollableRef.current?.scrollWidth ?? 0;const scrollLeft = scrollableRef.current?.scrollLeft ?? 0;- if (direction === 'backwards-press') {+ if (direction === 'fit-left') {const currentFrameLeft = getFrameFromX(scrollLeft,durationInFrames,@@ -83,7 +111,7 @@ export const ensureFrameIsInViewport = (}}- if (direction === 'forwards-press') {+ if (direction === 'fit-right') {const currentFrameRight = calculateFrameWhileScrollingRight({durationInFrames,scrollLeft: scrollLeft as number,@@ -105,13 +133,23 @@ export const ensureFrameIsInViewport = (}}- if (direction === 'forwards-play') {- const currentFrameRight = calculateFrameWhileScrollingRight({- durationInFrames,- scrollLeft,- width,- });+ if (direction === 'page-right' || direction === 'page-left') {+ if (!isCursorInViewport(frame, durationInFrames)) {+ scrollToTimelineXOffset(+ direction === 'page-left'+ ? getScrollPositionForCursorOnRightEdge({+ nextFrame: frame,+ durationInFrames,+ })+ : getScrollPositionForCursorOnLeftEdge({+ nextFrame: frame,+ durationInFrames,+ })+ );+ }+ }+ if (direction === 'center') {const scrollPosOnRightEdge = getScrollPositionForCursorOnRightEdge({nextFrame: frame,durationInFrames,@@ -120,15 +158,7 @@ export const ensureFrameIsInViewport = (nextFrame: frame,durationInFrames,});- const needsToPageRight =- scrollPosOnRightEdge >=- getScrollPositionForCursorOnRightEdge({- nextFrame: currentFrameRight,- durationInFrames,- }) || scrollPosOnLeftEdge < scrollLeft;- if (needsToPageRight) {- scrollToTimelineXOffset(scrollPosOnLeftEdge);- }+ scrollToTimelineXOffset((scrollPosOnLeftEdge + scrollPosOnRightEdge) / 2);}};