🎥 Make videos programmatically with React
tweak math to at least scroll 1 frame
Jonny Burger766b208ecaf1
diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsxindex 1f4831e2c7..e652fdb33f 100644--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx@@ -7,7 +7,7 @@ import React, {useRef,useState,} from 'react';-import {Internals, interpolate} from 'remotion';+import {Internals} from 'remotion';import {useGetXPositionOfItemInTimeline} from '../../helpers/get-left-of-timeline-slider';import {TIMELINE_PADDING} from '../../helpers/timeline-layout';import {@@ -17,7 +17,12 @@ import {import {TimelineZoomCtx} from '../../state/timeline-zoom';import {persistCurrentFrame} from '../FramePersistor';import {scrollableRef, sliderAreaRef} from './timeline-refs';-import {canScrollTimelineIntoDirection} from './timeline-scroll-logic';+import {+ canScrollTimelineIntoDirection,+ getFrameFromX,+ getFrameWhileScrollingLeft,+ getFrameWhileScrollingRight,+} from './timeline-scroll-logic';import {inMarkerAreaRef, outMarkerAreaRef} from './TimelineInOutPointer';import {inPointerHandle,@@ -37,29 +42,6 @@ const container: React.CSSProperties = {top: 0,};-const SCROLL_INCREMENT = 200;--const getFrameFromX = (- clientX: number,- durationInFrames: number,- width: number,- extrapolate: 'clamp' | 'extend'-) => {- const pos = clientX - TIMELINE_PADDING;- const frame = Math.round(- interpolate(- pos,- [0, width - TIMELINE_PADDING * 2],- [0, durationInFrames - 1 ?? 0],- {- extrapolateLeft: extrapolate,- extrapolateRight: extrapolate,- }- )- );- return frame;-};-const getClientXWithScroll = (x: number) => {return x + (scrollableRef.current?.scrollLeft as number);};@@ -204,11 +186,9 @@ export const TimelineDragHandler: React.FC = () => {return;}- const nextFrame = getFrameFromX(- (scrollableRef.current?.scrollLeft as number) - SCROLL_INCREMENT,+ const nextFrame = getFrameWhileScrollingLeft(videoConfig.durationInFrames,- width,- 'clamp'+ width);const scrollPos = frameIncrement * nextFrame;@@ -237,22 +217,14 @@ export const TimelineDragHandler: React.FC = () => {return;}- const nextFrame = Math.min(- videoConfig.durationInFrames - 1,- getFrameFromX(- (scrollableRef.current?.scrollLeft as number) + SCROLL_INCREMENT,- videoConfig.durationInFrames,- width,- 'clamp'- ) +- Math.ceil(- ((scrollableRef.current?.clientWidth as number) -- TIMELINE_PADDING) /- frameIncrement- )- );+ const nextFrame = getFrameWhileScrollingRight({+ durationInFrames: videoConfig.durationInFrames,+ width,+ frameIncrement,+ });const framesRemaining = videoConfig.durationInFrames - 1 - nextFrame;+const fromRight = framesRemaining * frameIncrement + TIMELINE_PADDING;const scrollPos =@@ -269,7 +241,7 @@ export const TimelineDragHandler: React.FC = () => {(scrollableRef.current?.clientWidth as number);current.scroll({- left: Math.min(maxScroll, scrollPos),+ left: scrollPos,});};diff --git a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.tsindex 151c89bfde..1bafbd8f76 100644--- a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts+++ b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts@@ -1,3 +1,4 @@+import {interpolate} from 'remotion';import {TIMELINE_PADDING} from '../../helpers/timeline-layout';import {scrollableRef} from './timeline-refs';@@ -9,3 +10,94 @@ export const canScrollTimelineIntoDirection = () => {const canScrollLeft = scrollLeft > TIMELINE_PADDING;return {canScrollRight, canScrollLeft};};++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+ )+ );+};++export const getFrameWhileScrollingLeft = (+ durationInFrames: number,+ width: number+) => {+ const nextFrame = getFrameFromX(+ (scrollableRef.current?.scrollLeft as number) - SCROLL_INCREMENT,+ durationInFrames,+ width,+ 'clamp'+ );+ const currentFrame = getFrameFromX(+ scrollableRef.current?.scrollLeft as number,+ durationInFrames,+ width,+ 'clamp'+ );++ // Should go back at least 1 frame, but not less than 0+ return Math.max(0, Math.min(currentFrame - 1, nextFrame));+};++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,+ });++ // Should scroll by at least 1 frame, but not overshoot duration+ return Math.min(durationInFrames - 1, Math.max(nextFrame, currentFrame + 1));+};++export const getFrameFromX = (+ clientX: number,+ durationInFrames: number,+ width: number,+ extrapolate: 'clamp' | 'extend'+) => {+ const pos = clientX - TIMELINE_PADDING;+ const frame = Math.round(+ interpolate(+ pos,+ [0, width - TIMELINE_PADDING * 2],+ [0, durationInFrames - 1 ?? 0],+ {+ extrapolateLeft: extrapolate,+ extrapolateRight: extrapolate,+ }+ )+ );+ return frame;+};