🎥 Make videos programmatically with React
progress
Jonny Burgere28080c5c33f
diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsxindex 7c9f6b977b..0c31e55f9d 100644--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx@@ -1,9 +1,16 @@import {PlayerInternals} from '@remotion/player';import React, {useCallback, useEffect, useState} from 'react';import {Internals, interpolate} from 'remotion';+import {useGetXPositionOfItemInTimeline} from '../../helpers/get-left-of-timeline-slider';import {TIMELINE_PADDING} from '../../helpers/timeline-layout';import {persistCurrentFrame} from '../FramePersistor';import {sliderAreaRef} from './timeline-refs';+import {+ inPointerHandle,+ outPointerHandle,+ TimelineInPointerHandle,+ TimelineOutPointerHandle,+} from './TimelineInOutPointerHandle';const container: React.CSSProperties = {userSelect: 'none',@@ -38,12 +45,18 @@ const getFrameFromX = (return frame;};-export const TimelineDragHandler: React.FC = ({children}) => {+export const TimelineDragHandler: React.FC = () => {const size = PlayerInternals.useElementSize(sliderAreaRef, {triggerOnWindowResize: true,});const width = size?.width ?? 0;const left = size?.left ?? 0;+ const {+ inFrame,+ outFrame,+ } = Internals.Timeline.useTimelineInOutFramePosition();++ const {get} = useGetXPositionOfItemInTimeline();const [dragging, setDragging] = useState<| {dragging: false;@@ -64,6 +77,16 @@ export const TimelineDragHandler: React.FC = ({children}) => {return;}+ if ((e.target as Node) === inPointerHandle.current) {+ console.log('in');+ return;+ }++ if ((e.target as Node) === outPointerHandle.current) {+ console.log('out');+ return;+ }+const frame = getFrameFromX(e.clientX - left,videoConfig.durationInFrames,@@ -142,7 +165,27 @@ export const TimelineDragHandler: React.FC = ({children}) => {return (<div ref={sliderAreaRef} style={container} onPointerDown={onPointerDown}>- <div style={inner}>{children}</div>+ <div style={inner} />+ {inFrame !== null && (+ <div+ style={{+ ...container,+ transform: `translateX(${get(inFrame)}px)`,+ }}+ >+ <TimelineInPointerHandle />+ </div>+ )}+ {outFrame !== null && (+ <div+ style={{+ ...container,+ transform: `translateX(${get(outFrame)}px)`,+ }}+ >+ <TimelineOutPointerHandle />+ </div>+ )}</div>);};diff --git a/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx b/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsxindex 3ce87e8feb..b15d6f7f97 100644--- a/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx+++ b/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx@@ -1,22 +1,11 @@import {PlayerInternals} from '@remotion/player';import React, {useEffect} from 'react';import {Internals} from 'remotion';-import {TIMELINE_PADDING} from '../../helpers/timeline-layout';-import {sliderAreaRef} from './timeline-refs';-import {- TimelineInPointerHandle,- TimelineOutPointerHandle,-} from './TimelineInOutPointerHandle';--const container: React.CSSProperties = {- position: 'absolute',- bottom: 0,- top: 0,-};+import {useGetXPositionOfItemInTimeline} from '../../helpers/get-left-of-timeline-slider';const areaHighlight: React.CSSProperties = {position: 'absolute',- backgroundColor: '#448cc059',+ backgroundColor: 'rgba(0, 0, 0, 0.3)',height: '100%',bottom: 0,top: 0,@@ -34,10 +23,7 @@ export const TimelineInOutPointer: React.FC = () => {} = Internals.Timeline.useTimelineSetInOutFramePosition();const videoConfig = Internals.useUnsafeVideoConfig();const {playing} = PlayerInternals.usePlayer();- const size = PlayerInternals.useElementSize(sliderAreaRef, {- triggerOnWindowResize: false,- });- const width = size?.width ?? 0;+ const {get, width} = useGetXPositionOfItemInTimeline();useEffect(() => {if (playing) {@@ -53,43 +39,26 @@ export const TimelineInOutPointer: React.FC = () => {return null;}- const getLeft = (frame: number) =>- (frame / (videoConfig.durationInFrames - 1)) *- (width - TIMELINE_PADDING * 2) +- TIMELINE_PADDING;-return (<>- {(inFrame !== null || outFrame !== null) && (+ {inFrame !== null && (<divstyle={{...areaHighlight,- left: getLeft(inFrame ?? 0),- width:- getLeft(outFrame ?? videoConfig.durationInFrames - 1) -- getLeft(inFrame ?? 0),+ left: 0,+ width: get(inFrame ?? 0),}}/>)}- {inFrame !== null && (- <div- style={{- ...container,- transform: `translateX(${getLeft(inFrame)}px)`,- }}- >- <TimelineInPointerHandle />- </div>- )}+{outFrame !== null && (<divstyle={{- ...container,- transform: `translateX(${getLeft(outFrame)}px)`,+ ...areaHighlight,+ left: get(outFrame),+ width: width - get(outFrame),}}- >- <TimelineOutPointerHandle />- </div>+ />)}</>);diff --git a/packages/cli/src/editor/components/Timeline/TimelineInOutPointerHandle.tsx b/packages/cli/src/editor/components/Timeline/TimelineInOutPointerHandle.tsxindex b85a576c34..54bf0a7044 100644--- a/packages/cli/src/editor/components/Timeline/TimelineInOutPointerHandle.tsx+++ b/packages/cli/src/editor/components/Timeline/TimelineInOutPointerHandle.tsx@@ -1,35 +1,22 @@-import React from 'react';+import React, {createRef} from 'react';const line: React.CSSProperties = {height: '100%',- width: 2,+ width: 1,position: 'fixed',- backgroundColor: '#06d639',+ backgroundColor: 'rgba(255, 255, 255, 0.1)',+ cursor: 'ew-resize',+ paddingLeft: 1,+ paddingRight: 1,};-const dash: React.CSSProperties = {- width: 10,- height: 2,- position: 'fixed',- backgroundColor: '#06d639',-};+export const inPointerHandle = createRef<HTMLDivElement>();+export const outPointerHandle = createRef<HTMLDivElement>();export const TimelineInPointerHandle: React.FC = () => {- return (- <>- <div style={{...dash, top: 0, left: 0}} />- <div style={line} />- <div style={{...dash, bottom: 0, left: 0}} />- </>- );+ return <div ref={inPointerHandle} style={line} />;};export const TimelineOutPointerHandle: React.FC = () => {- return (- <>- <div style={{...dash, top: 0, right: 0}} />- <div style={line} />- <div style={{...dash, bottom: 0, right: 0}} />- </>- );+ return <div ref={outPointerHandle} style={line} />;};diff --git a/packages/cli/src/editor/components/Timeline/TimelineSlider.tsx b/packages/cli/src/editor/components/Timeline/TimelineSlider.tsxindex df6c11a10b..8d98747d8a 100644--- a/packages/cli/src/editor/components/Timeline/TimelineSlider.tsx+++ b/packages/cli/src/editor/components/Timeline/TimelineSlider.tsx@@ -1,8 +1,6 @@-import {PlayerInternals} from '@remotion/player';import React from 'react';import {Internals} from 'remotion';-import {TIMELINE_PADDING} from '../../helpers/timeline-layout';-import {sliderAreaRef} from './timeline-refs';+import {useGetXPositionOfItemInTimeline} from '../../helpers/get-left-of-timeline-slider';import {TimelineSliderHandle} from './TimelineSliderHandle';const container: React.CSSProperties = {@@ -20,20 +18,9 @@ const line: React.CSSProperties = {export const TimelineSlider: React.FC = () => {const timelinePosition = Internals.Timeline.useTimelinePosition();- const videoConfig = Internals.useUnsafeVideoConfig();- const size = PlayerInternals.useElementSize(sliderAreaRef, {- triggerOnWindowResize: false,- });- const width = size?.width ?? 0;+ const {get} = useGetXPositionOfItemInTimeline();- if (!videoConfig) {- return null;- }-- const left =- (timelinePosition / (videoConfig.durationInFrames - 1)) *- (width - TIMELINE_PADDING * 2) +- TIMELINE_PADDING;+ const left = get(timelinePosition);return (<div style={{...container, transform: `translateX(${left}px)`}}>diff --git a/packages/cli/src/editor/components/TimelineInOutToggle.tsx b/packages/cli/src/editor/components/TimelineInOutToggle.tsxindex a5c66aa11e..5a25c6c65a 100644--- a/packages/cli/src/editor/components/TimelineInOutToggle.tsx+++ b/packages/cli/src/editor/components/TimelineInOutToggle.tsx@@ -10,6 +10,11 @@ import {ControlButton} from './ControlButton';const MIN_FRAMES_BETWEEN_POINTS = 1;const getTooltipText = (pointType: string) => `Mark ${pointType}`;+const style: React.CSSProperties = {+ width: 16,+ height: 16,+};+export const TimelineInOutPointToggle: React.FC = () => {const [inFrameBtnActive, setInFrameBtnActiveState] = useState(false);const [outFrameBtnActive, setOutFrameBtnActiveState] = useState(false);@@ -138,11 +143,8 @@ export const TimelineInOutPointToggle: React.FC = () => {onClick={onInFrameBtnClick}><TimelineInPointer- style={{- width: 16,- height: 16,- color: inFrameBtnActive ? 'var(--blue)' : 'white',- }}+ color={inFrameBtnActive ? 'var(--blue)' : 'white'}+ style={style}/></ControlButton><ControlButton@@ -151,11 +153,8 @@ export const TimelineInOutPointToggle: React.FC = () => {onClick={onOutFrameBtnClick}><TimelineOutPointer- style={{- width: 16,- height: 16,- color: outFrameBtnActive ? 'var(--blue)' : 'white',- }}+ color={outFrameBtnActive ? 'var(--blue)' : 'white'}+ style={style}/></ControlButton></>diff --git a/packages/cli/src/editor/helpers/get-left-of-timeline-slider.ts b/packages/cli/src/editor/helpers/get-left-of-timeline-slider.tsnew file mode 100644index 0000000000..455b724118--- /dev/null+++ b/packages/cli/src/editor/helpers/get-left-of-timeline-slider.ts@@ -0,0 +1,34 @@+import {PlayerInternals} from '@remotion/player';+import {useCallback, useMemo} from 'react';+import {Internals} from 'remotion';+import {sliderAreaRef} from '../components/Timeline/timeline-refs';+import {TIMELINE_PADDING} from './timeline-layout';++export const useGetXPositionOfItemInTimeline = () => {+ const size = PlayerInternals.useElementSize(sliderAreaRef, {+ triggerOnWindowResize: false,+ });++ const videoConfig = Internals.useUnsafeVideoConfig();++ const width = size?.width ?? 0;++ const get = useCallback(+ (frame: number) => {+ if (!videoConfig) {+ return 0;+ }++ return (+ (frame / (videoConfig.durationInFrames - 1)) *+ (width - TIMELINE_PADDING * 2) ++ TIMELINE_PADDING+ );+ },+ [videoConfig, width]+ );++ return useMemo(() => {+ return {get, width};+ }, [get, width]);+};diff --git a/packages/cli/src/editor/icons/timelineInOutPointer.tsx b/packages/cli/src/editor/icons/timelineInOutPointer.tsxindex 93fd20f847..fe024ca17f 100644--- a/packages/cli/src/editor/icons/timelineInOutPointer.tsx+++ b/packages/cli/src/editor/icons/timelineInOutPointer.tsx@@ -5,7 +5,7 @@ export const TimelineInPointer: React.FC<SVGProps<SVGSVGElement>> = (props) => {<svg viewBox="0 0 256 256" fill="none" {...props}><pathd="M158 25H99V230.5H158"- stroke="currentColor"+ stroke={props.color}strokeWidth="42"strokeLinecap="round"strokeLinejoin="round"@@ -21,7 +21,7 @@ export const TimelineOutPointer: React.FC<SVGProps<SVGSVGElement>> = (<svg viewBox="0 0 256 256" fill="none" {...props}><pathd="M98 25H157V230.5H98"- stroke="currentColor"+ stroke={props.color}strokeWidth="42"strokeLinecap="round"strokeLinejoin="round"