🎥 Make videos programmatically with React
page right if playing forwards
Jonny Burger8266be8ede44
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsxindex 7c3064db4d..9f964a24b3 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', durationInFrames, 0);+ ensureFrameIsInViewport('backwards-press', durationInFrames, 0);} else if (e.shiftKey) {frameBack(videoFps);ensureFrameIsInViewport(- 'backwards',+ 'backwards-press',durationInFrames,frame - videoFps);} else {frameBack(1);- ensureFrameIsInViewport('backwards', durationInFrames, frame - 1);+ ensureFrameIsInViewport('backwards-press', durationInFrames, frame - 1);}},[durationInFrames, frame, frameBack, seek, videoFps]@@ -118,20 +118,24 @@ export const PlayPause: React.FC<{if (e.altKey) {seek(video.durationInFrames - 1);ensureFrameIsInViewport(- 'forward',+ 'forwards-press',video.durationInFrames - 1,video.durationInFrames - 1);} else if (e.shiftKey) {frameForward(video.fps);ensureFrameIsInViewport(- 'forward',+ 'forwards-press',video.durationInFrames,frame + video.fps);} else {frameForward(1);- ensureFrameIsInViewport('forward', video.durationInFrames, frame + 1);+ ensureFrameIsInViewport(+ 'forwards-press',+ video.durationInFrames,+ frame + 1+ );}e.preventDefault();@@ -266,8 +270,8 @@ export const PlayPause: React.FC<{</ControlButton><ControlButton- aria-label="Step forward one frame"- title="Step forward one frame"+ aria-label="Step forward-press one frame"+ title="Step forward-press one frame"disabled={isLastFrame}onClick={oneFrameForward}>diff --git a/packages/cli/src/editor/components/Timeline/Timeline.tsx b/packages/cli/src/editor/components/Timeline/Timeline.tsxindex 15ecdf0925..705e3bdd69 100644--- a/packages/cli/src/editor/components/Timeline/Timeline.tsx+++ b/packages/cli/src/editor/components/Timeline/Timeline.tsx@@ -21,6 +21,7 @@ import {timelineStateReducer} from './timeline-state-reducer';import {TimelineDragHandler} from './TimelineDragHandler';import {TimelineInOutPointer} from './TimelineInOutPointer';import {TimelineList} from './TimelineList';+import {TimelinePlayCursorSyncer} from './TimelinePlayCursorSyncer';import {TimelineScrollable} from './TimelineScrollable';import {TimelineSlider} from './TimelineSlider';import {TimelineTracks} from './TimelineTracks';@@ -120,6 +121,7 @@ export const Timeline: React.FC = () => {<TimelineInOutPointer /><TimelineDragHandler /><TimelineSlider />+ <TimelinePlayCursorSyncer /></TimelineScrollable></SplitterElement></SplitterContainer>diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsxindex 0dcc8291f5..a849149dde 100644--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx@@ -9,6 +9,7 @@ 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,@@ -162,9 +163,12 @@ export const TimelineDragHandler: React.FC = () => {}const isRightOfArea =- e.clientX > (scrollableRef.current?.clientWidth as number) + left;+ e.clientX >=+ (scrollableRef.current?.clientWidth as number) ++ left -+ TIMELINE_PADDING;- const isLeftOfArea = e.clientX < left;+ const isLeftOfArea = e.clientX <= left;const frame = getFrameFromX(getClientXWithScroll(e.clientX) - left,diff --git a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsxnew file mode 100644index 0000000000..16fca824b2--- /dev/null+++ b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx@@ -0,0 +1,19 @@+import type React from 'react';+import {useEffect} from 'react';+import {Internals, useCurrentFrame} from 'remotion';+import {ensureFrameIsInViewport} from './timeline-scroll-logic';++export const TimelinePlayCursorSyncer: React.FC = () => {+ const video = Internals.useVideo();+ const frame = useCurrentFrame();++ useEffect(() => {+ if (!video) {+ return;+ }++ ensureFrameIsInViewport('forwards-play', video.durationInFrames, frame);+ }, [frame, video]);++ 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 e6863e7e81..cc22958709 100644--- a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts+++ b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts@@ -54,15 +54,16 @@ export const getFrameWhileScrollingLeft = (};export const ensureFrameIsInViewport = (- direction: 'backwards' | 'forward',+ direction: 'backwards-press' | 'forwards-press' | 'forwards-play',durationInFrames: number,frame: number) => {redrawTimelineSliderFast.current?.draw(frame);const width = scrollableRef.current?.scrollWidth ?? 0;- if (direction === 'backwards') {+ const scrollLeft = scrollableRef.current?.scrollLeft ?? 0;+ if (direction === 'backwards-press') {const currentFrameLeft = getFrameFromX(- scrollableRef.current?.scrollLeft as number,+ scrollLeft,durationInFrames,width,'clamp'@@ -82,13 +83,13 @@ export const ensureFrameIsInViewport = (}}- if (direction === 'forward') {+ if (direction === 'forwards-press') {const currentFrameRight = calculateFrameWhileScrollingRight({durationInFrames,- scrollLeft: scrollableRef.current?.scrollLeft as number,+ scrollLeft: scrollLeft as number,width,});- console.log(currentFrameRight, frame);+const scrollPos = getScrollPositionForCursorOnRightEdge({nextFrame: frame,durationInFrames,@@ -103,6 +104,32 @@ export const ensureFrameIsInViewport = (scrollToTimelineXOffset(scrollPos);}}++ if (direction === 'forwards-play') {+ const currentFrameRight = calculateFrameWhileScrollingRight({+ durationInFrames,+ scrollLeft,+ width,+ });++ const scrollPosOnRightEdge = getScrollPositionForCursorOnRightEdge({+ nextFrame: frame,+ durationInFrames,+ });+ const scrollPosOnLeftEdge = getScrollPositionForCursorOnLeftEdge({+ nextFrame: frame,+ durationInFrames,+ });+ const needsToPageRight =+ scrollPosOnRightEdge >=+ getScrollPositionForCursorOnRightEdge({+ nextFrame: currentFrameRight,+ durationInFrames,+ }) || scrollPosOnLeftEdge < scrollLeft;+ if (needsToPageRight) {+ scrollToTimelineXOffset(scrollPosOnLeftEdge);+ }+ }};export const scrollToTimelineXOffset = (scrollPos: number) => {