Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Add selectable timeline easing lines (#8208)

Jonny Burger76c514d05989
diff --git a/packages/studio/src/components/Timeline/TimelineExpandedKeyframeRow.tsx b/packages/studio/src/components/Timeline/TimelineExpandedKeyframeRow.tsx
index a49f2de749..177f2a730c 100644
--- a/packages/studio/src/components/Timeline/TimelineExpandedKeyframeRow.tsx
+++ b/packages/studio/src/components/Timeline/TimelineExpandedKeyframeRow.tsx
@@ -1,9 +1,12 @@
import React, {useContext} from 'react';
import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
import {TIMELINE_ITEM_BORDER_BOTTOM} from '../../helpers/timeline-layout';
+import {getTimelineEasingSegments} from './get-timeline-easing-segments';
import type {getTimelineKeyframes} from './get-timeline-keyframes';
import {TimelineKeyframeDiamond} from './TimelineKeyframeDiamond';
+import {TimelineKeyframeEasingLine} from './TimelineKeyframeEasingLine';
import {
+ ENABLE_OUTLINES,
getTimelineSelectedTrackHighlightStyle,
useTimelineRowSelection,
} from './TimelineSelection';
@@ -25,6 +28,9 @@ const TimelineExpandedKeyframeRowUnmemoized: React.FC<{
}> = ({height, keyframes, nodePathInfo, showSeparator}) => {
const timelineWidth = useContext(TimelineWidthContext);
const {selected: rowSelected} = useTimelineRowSelection(nodePathInfo);
+ const easingSegments = ENABLE_OUTLINES
+ ? getTimelineEasingSegments(keyframes)
+ : [];
return (
<>
@@ -33,6 +39,16 @@ const TimelineExpandedKeyframeRowUnmemoized: React.FC<{
{rowSelected && timelineWidth !== null ? (
<div style={getTimelineSelectedTrackHighlightStyle(timelineWidth)} />
) : null}
+ {easingSegments.map((segment) => (
+ <TimelineKeyframeEasingLine
+ key={`${segment.segmentIndex}-${segment.fromFrame}-${segment.toFrame}`}
+ fromFrame={segment.fromFrame}
+ toFrame={segment.toFrame}
+ rowHeight={height}
+ nodePathInfo={nodePathInfo}
+ segmentIndex={segment.segmentIndex}
+ />
+ ))}
{keyframes.map((keyframe) => (
<TimelineKeyframeDiamond
key={keyframe.frame}
diff --git a/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsx b/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsx
index d6d795a5d1..d75d7a4764 100644
--- a/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsx
+++ b/packages/studio/src/components/Timeline/TimelineKeyframeDiamond.tsx
@@ -46,7 +46,6 @@ const TimelineKeyframeDiamondUnmemoized: React.FC<{
return {
...diamondBase,
- cursor: 'pointer',
left:
getXPositionOfItemInTimelineImperatively(
frame,
diff --git a/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx b/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx
new file mode 100644
index 0000000000..b125584b98
--- /dev/null
+++ b/packages/studio/src/components/Timeline/TimelineKeyframeEasingLine.tsx
@@ -0,0 +1,131 @@
+import React, {useCallback, useContext, useMemo} from 'react';
+import {useVideoConfig} from 'remotion';
+import {BLUE, LINE_COLOR} from '../../helpers/colors';
+import {getXPositionOfItemInTimelineImperatively} from '../../helpers/get-left-of-timeline-slider';
+import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
+import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
+import {useTimelineEasingSelection} from './TimelineSelection';
+import {TimelineWidthContext} from './TimelineWidthProvider';
+
+const hitTargetHeight = 12;
+const lineHeight = 2;
+
+const easingLineButton: React.CSSProperties = {
+ background: 'none',
+ border: 'none',
+ height: hitTargetHeight,
+ padding: 0,
+ position: 'absolute',
+ transform: 'translateY(-50%)',
+};
+
+const easingLine: React.CSSProperties = {
+ backgroundColor: LINE_COLOR,
+ borderRadius: lineHeight / 2,
+ height: lineHeight,
+ left: 0,
+ position: 'absolute',
+ right: 0,
+ top: '50%',
+ transform: 'translateY(-50%)',
+};
+
+const TimelineKeyframeEasingLineUnmemoized: React.FC<{
+ readonly fromFrame: number;
+ readonly toFrame: number;
+ readonly rowHeight: number;
+ readonly nodePathInfo: SequenceNodePathInfo;
+ readonly segmentIndex: number;
+}> = ({fromFrame, toFrame, rowHeight, nodePathInfo, segmentIndex}) => {
+ const videoConfig = useVideoConfig();
+ const timelineWidth = useContext(TimelineWidthContext);
+ const {selected, onSelect, selectable} = useTimelineEasingSelection({
+ nodePathInfo,
+ fromFrame,
+ toFrame,
+ segmentIndex,
+ });
+
+ const style = useMemo((): React.CSSProperties | null => {
+ if (timelineWidth === null) {
+ return null;
+ }
+
+ const fromLeft =
+ getXPositionOfItemInTimelineImperatively(
+ fromFrame,
+ videoConfig.durationInFrames,
+ timelineWidth,
+ ) - TIMELINE_PADDING;
+ const toLeft =
+ getXPositionOfItemInTimelineImperatively(
+ toFrame,
+ videoConfig.durationInFrames,
+ timelineWidth,
+ ) - TIMELINE_PADDING;
+ const left = Math.min(fromLeft, toLeft);
+ const width = Math.abs(toLeft - fromLeft);
+ if (width === 0) {
+ return null;
+ }
+
+ return {
+ ...easingLineButton,
+ left,
+ pointerEvents: selectable ? 'auto' : 'none',
+ top: rowHeight / 2,
+ width,
+ };
+ }, [
+ fromFrame,
+ rowHeight,
+ selectable,
+ timelineWidth,
+ toFrame,
+ videoConfig.durationInFrames,
+ ]);
+
+ const lineStyle = useMemo(
+ (): React.CSSProperties => ({
+ ...easingLine,
+ outline: selected ? `1px solid ${BLUE}` : undefined,
+ }),
+ [selected],
+ );
+
+ const onPointerDown = useCallback(
+ (event: React.PointerEvent<HTMLButtonElement>) => {
+ if (!selectable || event.button !== 0) {
+ return;
+ }
+
+ event.preventDefault();
+ event.stopPropagation();
+ onSelect({
+ shiftKey: event.shiftKey,
+ toggleKey: event.metaKey || event.ctrlKey,
+ });
+ },
+ [onSelect, selectable],
+ );
+
+ if (style === null) {
+ return null;
+ }
+
+ return (
+ <button
+ type="button"
+ style={style}
+ title={`Easing from frame ${fromFrame} to ${toFrame}`}
+ aria-label={`Select easing from frame ${fromFrame} to ${toFrame}`}
+ onPointerDown={selectable ? onPointerDown : undefined}
+ >
+ <div style={lineStyle} />
+ </button>
+ );
+};
+
+export const TimelineKeyframeEasingLine = React.memo(
+ TimelineKeyframeEasingLineUnmemoized,
+);
diff --git a/packages/studio/src/components/Timeline/TimelineSelection.tsx b/packages/studio/src/components/Timeline/TimelineSelection.tsx
index 6a68c57fc5..ac2a7750a7 100644
--- a/packages/studio/src/components/Timeline/TimelineSelection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSelection.tsx
@@ -91,6 +91,12 @@ export type TimelineSelection =
| (TimelineSelectionBase & {
readonly type: 'keyframe';
readonly frame: number;
+ })
+ | (TimelineSelectionBase & {
+ readonly type: 'easing';
+ readonly fromFrame: number;
+ readonly toFrame: number;
+ readonly segmentIndex: number;
});
export type TimelineSelectionInteraction = {
@@ -341,6 +347,10 @@ const getTimelineSelectionKey = (item: TimelineSelection): string => {
return `${timelineNodePathInfoToKey(item.nodePathInfo)}.keyframe.${
item.frame
}`;
+ case 'easing':
+ return `${timelineNodePathInfoToKey(item.nodePathInfo)}.easing.${
+ item.segmentIndex
+ }.${item.fromFrame}.${item.toFrame}`;
default:
throw new Error(
`Unexpected timeline selection type: ${item satisfies never}`,
@@ -676,6 +686,50 @@ export const useTimelineKeyframeSelection = (
};
};
+export const useTimelineEasingSelection = ({
+ nodePathInfo,
+ fromFrame,
+ toFrame,
+ segmentIndex,
+}: {
+ readonly nodePathInfo: SequenceNodePathInfo;
+ readonly fromFrame: number;
+ readonly toFrame: number;
+ readonly segmentIndex: number;
+}) => {
+ const {canSelect, isSelected, selectItem, registerSelectableItem} =
+ useTimelineSelection();
+ const selectionItem = useMemo(
+ (): TimelineSelection => ({
+ type: 'easing',
+ nodePathInfo,
+ fromFrame,
+ toFrame,
+ segmentIndex,
+ }),
+ [nodePathInfo, fromFrame, segmentIndex, toFrame],
+ );
+
+ useEffect(() => {
+ return registerSelectableItem(selectionItem);
+ }, [registerSelectableItem, selectionItem]);
+
+ const selected = isSelected(selectionItem);
+
+ const onSelect = useCallback(
+ (interaction?: TimelineSelectionInteraction) => {
+ selectItem(selectionItem, interaction);
+ },
+ [selectItem, selectionItem],
+ );
+
+ return {
+ onSelect,
+ selectable: canSelect,
+ selected,
+ };
+};
+
export const useTimelineRowContainsSelection = (
nodePathInfo: SequenceNodePathInfo | null,
): boolean => {
diff --git a/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts b/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts
index 92408fb5f6..c7d029e42e 100644
--- a/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts
+++ b/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts
@@ -178,6 +178,7 @@ export const deleteSelectedTimelineItem = ({
]);
case 'sequence-prop':
case 'sequence-effect-prop':
+ case 'easing':
return null;
case 'sequence-all-effects':
return deleteEffects([
@@ -285,6 +286,7 @@ export const deleteSelectedTimelineItems = ({
case 'sequence-prop':
case 'sequence-effect-prop':
+ case 'easing':
return null;
case 'sequence-all-effects':
return deleteEffects(
diff --git a/packages/studio/src/components/Timeline/get-timeline-easing-segments.ts b/packages/studio/src/components/Timeline/get-timeline-easing-segments.ts
new file mode 100644
index 0000000000..bbc4f7c5b5
--- /dev/null
+++ b/packages/studio/src/components/Timeline/get-timeline-easing-segments.ts
@@ -0,0 +1,26 @@
+import type {getTimelineKeyframes} from './get-timeline-keyframes';
+
+export type TimelineEasingSegment = {
+ readonly fromFrame: number;
+ readonly toFrame: number;
+ readonly segmentIndex: number;
+};
+
+export const getTimelineEasingSegments = (
+ keyframes: ReturnType<typeof getTimelineKeyframes>,
+): TimelineEasingSegment[] => {
+ return keyframes.flatMap((keyframe, index) => {
+ const nextKeyframe = keyframes[index + 1];
+ if (!nextKeyframe) {
+ return [];
+ }
+
+ return [
+ {
+ fromFrame: keyframe.frame,
+ toFrame: nextKeyframe.frame,
+ segmentIndex: index,
+ },
+ ];
+ });
+};
diff --git a/packages/studio/src/test/timeline-keyframes.test.ts b/packages/studio/src/test/timeline-keyframes.test.ts
index 41af4f137d..dbd37f71f2 100644
--- a/packages/studio/src/test/timeline-keyframes.test.ts
+++ b/packages/studio/src/test/timeline-keyframes.test.ts
@@ -7,6 +7,7 @@ import type {
import {Internals, type PropStatuses} from 'remotion';
import {getBoundedKeyframeDragDelta} from '../components/Timeline/get-bounded-keyframe-drag-delta';
import {getNodeKeyframes} from '../components/Timeline/get-node-keyframes';
+import {getTimelineEasingSegments} from '../components/Timeline/get-timeline-easing-segments';
import {getTimelineKeyframes} from '../components/Timeline/get-timeline-keyframes';
import {getTimelineKeyframeDragKey} from '../components/Timeline/TimelineKeyframeDragState';
import {calculateTimeline} from '../helpers/calculate-timeline';
@@ -229,6 +230,23 @@ test('keyframe display offsets follow the parent sequence context', () => {
]);
});
+test('timeline easing segments connect adjacent display keyframes', () => {
+ const status: CanUpdateSequencePropStatusKeyframed = {
+ ...makeKeyframedStatus(),
+ keyframes: [
+ {frame: 0, value: 2},
+ {frame: 30, value: 3},
+ {frame: 60, value: 4},
+ ],
+ easing: ['linear', 'linear'],
+ };
+
+ expect(getTimelineEasingSegments(getTimelineKeyframes(status, 30))).toEqual([
+ {fromFrame: 30, toFrame: 60, segmentIndex: 0},
+ {fromFrame: 60, toFrame: 90, segmentIndex: 1},
+ ]);
+});
+
test('bounded keyframe drag delta stays inside the composition timeline', () => {
expect(
getBoundedKeyframeDragDelta({
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 4ab4bdcbf9..11cdec7a53 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -2021,6 +2021,36 @@ test('Shift+click with no matching anchor falls back to single selection', () =>
});
});
+test('Selecting an easing segment replaces keyframe selection with the new type', () => {
+ const keyframe = {
+ type: 'keyframe' as const,
+ nodePathInfo: makeNodePathInfo(['body', 0], []),
+ frame: 10,
+ };
+ const easing = {
+ type: 'easing' as const,
+ nodePathInfo: makeNodePathInfo(['body', 0], []),
+ fromFrame: 10,
+ toFrame: 20,
+ segmentIndex: 0,
+ };
+
+ expect(
+ getTimelineSelectionAfterInteraction({
+ currentState: {
+ selectedItems: [keyframe],
+ anchor: keyframe,
+ },
+ clickedItem: easing,
+ interaction: {shiftKey: false, toggleKey: false},
+ allSelectableItems: [keyframe, easing],
+ }),
+ ).toEqual({
+ selectedItems: [easing],
+ anchor: easing,
+ });
+});
+
test('Timeline double-click actions ignore selection modifier clicks', () => {
expect(
isTimelineSelectionModifierEvent({