Skip to content

🎥 Make videos programmatically with React

feat: split tracks equally inside timeline

Arthur Dennere8eab7c44e14
diff --git a/packages/cli/src/editor/components/Timeline.tsx b/packages/cli/src/editor/components/Timeline.tsx
index 1cf6dad8fa..088bbdb8fa 100644
--- a/packages/cli/src/editor/components/Timeline.tsx
+++ b/packages/cli/src/editor/components/Timeline.tsx
@@ -6,34 +6,28 @@ import {TimelineElements} from './TimelineElements';
import {TimelineSlider} from './TimelineSlider';
import {TimeValue} from './TimeValue';
-const TimelineContainer = styled.div`
- overflow-y: auto;
-`;
-
-const Header = styled.div`
+const Container = styled.div`
display: flex;
flex-direction: column;
height: 100%;
`;
-const Row = styled.div`
- padding: 16px;
+const Details = styled.div`
+ padding: 12px 16px;
display: flex;
justify-content: space-between;
align-items: center;
`;
export const Timeline: React.FC = () => (
- <Header>
- <Row>
+ <Container>
+ <Details>
<TimeValue />
<FpsCounter />
- </Row>
+ </Details>
<TimelineDragHandler>
- <TimelineContainer>
- <TimelineElements />
- </TimelineContainer>
+ <TimelineElements />
<TimelineSlider />
</TimelineDragHandler>
- </Header>
+ </Container>
);
diff --git a/packages/cli/src/editor/components/TimelineDragHandler.tsx b/packages/cli/src/editor/components/TimelineDragHandler.tsx
index 19aad607b3..5fb71c0101 100644
--- a/packages/cli/src/editor/components/TimelineDragHandler.tsx
+++ b/packages/cli/src/editor/components/TimelineDragHandler.tsx
@@ -1,19 +1,14 @@
import React, {useCallback, useState} from 'react';
import {Internals, interpolate} from 'remotion';
import styled from 'styled-components';
-import {
- TIMELINE_LEFT_PADDING,
- TIMELINE_RIGHT_PADDING,
-} from '../helpers/timeline-layout';
import {useWindowSize} from '../hooks/use-window-size';
+const PADDING = 16;
+
const Container = styled.div`
flex: 1;
position: relative;
- padding-left: ${TIMELINE_LEFT_PADDING}px;
- padding-right: ${TIMELINE_RIGHT_PADDING}px;
- padding-top: 20px;
- margin-top: -10px;
+ padding: ${PADDING}px;
user-select: none;
overflow-y: auto;
`;
@@ -23,17 +18,12 @@ const getFrameFromX = (
durationInFrames: number,
width: number
) => {
- const pos = clientX - TIMELINE_LEFT_PADDING;
+ const pos = clientX - PADDING;
const frame = Math.round(
- interpolate(
- pos,
- [0, width - TIMELINE_LEFT_PADDING - TIMELINE_RIGHT_PADDING],
- [0, durationInFrames - 1 ?? 0],
- {
- extrapolateLeft: 'clamp',
- extrapolateRight: 'clamp',
- }
- )
+ interpolate(pos, [0, width - PADDING * 2], [0, durationInFrames - 1 ?? 0], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ })
);
return frame;
};
diff --git a/packages/cli/src/editor/components/TimelineElements.tsx b/packages/cli/src/editor/components/TimelineElements.tsx
index 8518815add..066018aa7b 100644
--- a/packages/cli/src/editor/components/TimelineElements.tsx
+++ b/packages/cli/src/editor/components/TimelineElements.tsx
@@ -2,11 +2,13 @@ import React, {useContext, useMemo} from 'react';
import {Internals} from 'remotion';
import styled from 'styled-components';
import {calculateTimeline, Track} from '../helpers/calculate-timeline';
-import {
- TIMELINE_LEFT_PADDING,
- TIMELINE_RIGHT_PADDING,
-} from '../helpers/timeline-layout';
-import {useWindowSize} from '../hooks/use-window-size';
+
+const Container = styled.div`
+ height: 100%;
+ display: grid;
+ grid-gap: 2px;
+ grid-template-rows: repeat(auto-fit, minmax(25px, 1fr));
+`;
const Pre = styled.pre`
color: white;
@@ -18,7 +20,6 @@ const Pre = styled.pre`
`;
export const TimelineElements: React.FC = () => {
- const {width} = useWindowSize();
const {sequences} = useContext(Internals.CompositionManager);
const videoConfig = Internals.useUnsafeVideoConfig();
@@ -33,59 +34,46 @@ export const TimelineElements: React.FC = () => {
// and therefore frame 99 should be at the right
const lastFrame = (videoConfig?.durationInFrames ?? 1) - 1;
+ if (!videoConfig) {
+ return null;
+ }
+
return (
- <div
- style={{
- width: width - TIMELINE_LEFT_PADDING - TIMELINE_RIGHT_PADDING,
- overflow: 'hidden',
- position: 'relative',
- }}
- >
- {videoConfig
- ? timeline.map((track) => {
+ <Container>
+ {timeline.map((track) => (
+ <div key={track.trackId}>
+ {track.sequences.map((s) => {
+ // If a duration is 1, it is essentially a still and it should have width 0
+ const spatialDuration = Internals.FEATURE_FLAG_V2_BREAKING_CHANGES
+ ? s.sequence.duration - 1
+ : s.sequence.duration;
+
return (
<div
- key={track.trackId}
+ key={s.sequence.id}
style={{
- height: 82,
+ background:
+ 'linear-gradient(to bottom, #3697e1, #348AC7 60%)',
+ border: '1px solid rgba(255, 255, 255, 0.2)',
+ borderRadius: 4,
+ height: '100%',
+ marginTop: 1,
+ marginLeft: `calc(${(s.sequence.from / lastFrame) * 100}%)`,
+ width:
+ s.sequence.duration === Infinity
+ ? 'auto'
+ : `calc(${(spatialDuration / lastFrame) * 100}%)`,
+ color: 'white',
+ overflow: 'hidden',
}}
+ title={s.sequence.displayName}
>
- {track.sequences.map((s) => {
- // If a duration is 1, it is essentially a still and it should have width 0
- const spatialDuration = Internals.FEATURE_FLAG_V2_BREAKING_CHANGES
- ? s.sequence.duration - 1
- : s.sequence.duration;
- return (
- <div
- key={s.sequence.id}
- style={{
- background:
- 'linear-gradient(to bottom, #3697e1, #348AC7 60%)',
- border: '1px solid rgba(255, 255, 255, 0.2)',
- borderRadius: 4,
- position: 'absolute',
- height: 80,
- marginTop: 1,
- marginLeft: `calc(${
- (s.sequence.from / lastFrame) * 100
- }%)`,
- width:
- s.sequence.duration === Infinity
- ? width
- : `calc(${(spatialDuration / lastFrame) * 100}%)`,
- color: 'white',
- overflow: 'hidden',
- }}
- title={s.sequence.displayName}
- >
- <Pre>{s.sequence.displayName}</Pre>
- </div>
- );
- })}
+ <Pre>{s.sequence.displayName}</Pre>
</div>
);
- })
- : null}
- </div>
+ })}
+ </div>
+ ))}
+ </Container>
);
};