Skip to content

🎥 Make videos programmatically with React

Add UV coordinate connector lines

Cursor Agente308f89ac081
diff --git a/packages/core/src/sequence-field-schema.ts b/packages/core/src/sequence-field-schema.ts
index cac47cb3aa..75e443600d 100644
--- a/packages/core/src/sequence-field-schema.ts
+++ b/packages/core/src/sequence-field-schema.ts
@@ -62,6 +62,7 @@ export type UvCoordinateFieldSchema = {
min?: number;
max?: number;
step?: number;
+ lineTo?: string;
default: readonly [number, number] | undefined;
description?: string;
keyframable?: boolean;
diff --git a/packages/effects/src/halftone-linear-gradient.ts b/packages/effects/src/halftone-linear-gradient.ts
index 08b1f561a5..c41d4ede68 100644
--- a/packages/effects/src/halftone-linear-gradient.ts
+++ b/packages/effects/src/halftone-linear-gradient.ts
@@ -47,6 +47,7 @@ export const halftoneLinearGradientSchema = {
step: 0.01,
default: DEFAULT_FIRST_STOP_POSITION,
description: 'First stop position',
+ lineTo: 'secondStopPosition',
},
secondStopPosition: {
type: 'uv-coordinate',
diff --git a/packages/effects/src/linear-progressive-blur/index.ts b/packages/effects/src/linear-progressive-blur/index.ts
index c2d1235ad6..d5279bdee8 100644
--- a/packages/effects/src/linear-progressive-blur/index.ts
+++ b/packages/effects/src/linear-progressive-blur/index.ts
@@ -52,6 +52,7 @@ const linearProgressiveBlurSchema = {
step: 0.01,
default: DEFAULT_START,
description: 'Start',
+ lineTo: 'end',
},
end: {
type: 'uv-coordinate',
diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.ts
index 2f0f395323..83f3ed64a3 100644
--- a/packages/effects/src/test/effect-params.test.ts
+++ b/packages/effects/src/test/effect-params.test.ts
@@ -1131,6 +1131,15 @@ test('halftoneLinearGradient() accepts default params', () => {
expect(() => halftoneLinearGradient()).not.toThrow();
});
+test('halftoneLinearGradient() connects its stop position controls', () => {
+ expect(
+ halftoneLinearGradient().definition.schema.firstStopPosition,
+ ).toMatchObject({
+ type: 'uv-coordinate',
+ lineTo: 'secondStopPosition',
+ });
+});
+
test('halftoneLinearGradient() rejects first stop dot size below range', () => {
expect(() => halftoneLinearGradient({firstStopDotSize: -1})).toThrow(
'"firstStopDotSize" must be >= 0',
@@ -1548,6 +1557,13 @@ test('linearProgressiveBlur() accepts default params', () => {
expect(() => linearProgressiveBlur()).not.toThrow();
});
+test('linearProgressiveBlur() connects its start and end controls', () => {
+ expect(linearProgressiveBlur().definition.schema.start).toMatchObject({
+ type: 'uv-coordinate',
+ lineTo: 'end',
+ });
+});
+
test('linearProgressiveBlur() rejects invalid start', () => {
expect(() =>
linearProgressiveBlur({
diff --git a/packages/studio/src/components/SelectedOutlineOverlay.tsx b/packages/studio/src/components/SelectedOutlineOverlay.tsx
index 4e96fc15a0..80fb3bd897 100644
--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx
+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx
@@ -74,6 +74,17 @@ type SelectedOutlineUvHandle = {
readonly value: UvCoordinate;
};
+type UvConnectionHandle = Pick<
+ SelectedOutlineUvHandle,
+ 'effectIndex' | 'fieldKey' | 'fieldSchema' | 'value'
+>;
+
+type UvHandleConnectionLine = {
+ readonly key: string;
+ readonly from: OutlinePoint;
+ readonly to: OutlinePoint;
+};
+
type SelectedOutlineTarget = {
readonly key: string;
readonly nodePathInfo: SequenceNodePathInfo;
@@ -272,6 +283,54 @@ export const getUvHandlePosition = (
: applyProjectiveTransform(transform, uv);
};
+export const getUvHandleConnectionLines = ({
+ handles,
+ points,
+}: {
+ readonly handles: readonly UvConnectionHandle[];
+ readonly points: SelectedOutline['points'];
+}): UvHandleConnectionLine[] => {
+ const handlesByField = new Map(
+ handles.map((handle) => [
+ `${handle.effectIndex}\u0000${handle.fieldKey}`,
+ handle,
+ ]),
+ );
+ const seenPairs = new Set<string>();
+ const lines: UvHandleConnectionLine[] = [];
+
+ for (const handle of handles) {
+ const targetFieldKey = handle.fieldSchema.lineTo;
+ if (targetFieldKey === undefined || targetFieldKey === handle.fieldKey) {
+ continue;
+ }
+
+ const target = handlesByField.get(
+ `${handle.effectIndex}\u0000${targetFieldKey}`,
+ );
+ if (target === undefined) {
+ continue;
+ }
+
+ const pairKey = [
+ handle.effectIndex,
+ ...[handle.fieldKey, targetFieldKey].sort(),
+ ].join('\u0000');
+ if (seenPairs.has(pairKey)) {
+ continue;
+ }
+
+ seenPairs.add(pairKey);
+ lines.push({
+ key: `${handle.effectIndex}-${handle.fieldKey}-${targetFieldKey}`,
+ from: getUvHandlePosition(points, handle.value),
+ to: getUvHandlePosition(points, target.value),
+ });
+ }
+
+ return lines;
+};
+
const vectorBetween = (from: OutlinePoint, to: OutlinePoint): OutlinePoint => {
return {x: to.x - from.x, y: to.y - from.y};
};
@@ -1370,6 +1429,34 @@ const getSvgPointFromPointerEvent = ({
};
};
+const SelectedUvHandleConnectionLines: React.FC<{
+ readonly handles: readonly SelectedOutlineUvHandle[];
+ readonly outline: SelectedOutline;
+}> = ({handles, outline}) => {
+ const lines = useMemo(
+ () => getUvHandleConnectionLines({handles, points: outline.points}),
+ [handles, outline.points],
+ );
+
+ return (
+ <>
+ {lines.map((line) => (
+ <line
+ key={line.key}
+ x1={line.from.x}
+ y1={line.from.y}
+ x2={line.to.x}
+ y2={line.to.y}
+ stroke={BLUE}
+ strokeWidth={2}
+ vectorEffect="non-scaling-stroke"
+ pointerEvents="none"
+ />
+ ))}
+ </>
+ );
+};
+
const SelectedUvHandleCircle: React.FC<{
readonly handle: SelectedOutlineUvHandle;
readonly outline: SelectedOutline;
@@ -1723,15 +1810,25 @@ export const SelectedOutlineOverlay: React.FC<{
))
: null}
{targetsByKey.get(outline.key)?.selected
- ? targetsByKey
- .get(outline.key)
- ?.uvHandles.map((handle) => (
- <SelectedUvHandleCircle
- key={`${handle.effectIndex}-${handle.fieldKey}`}
- handle={handle}
- outline={outline}
- />
- ))
+ ? (() => {
+ const uvHandles =
+ targetsByKey.get(outline.key)?.uvHandles ?? [];
+ return (
+ <>
+ <SelectedUvHandleConnectionLines
+ handles={uvHandles}
+ outline={outline}
+ />
+ {uvHandles.map((handle) => (
+ <SelectedUvHandleCircle
+ key={`${handle.effectIndex}-${handle.fieldKey}`}
+ handle={handle}
+ outline={outline}
+ />
+ ))}
+ </>
+ );
+ })()
: null}
</React.Fragment>
))}
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 7adfbcf780..eae936650d 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -19,6 +19,7 @@ import {
getSelectedOutlineScaleEdgeInfo,
getSequencesWithSelectableOutlines,
getUvCoordinateForPoint,
+ getUvHandleConnectionLines,
getUvHandlePosition,
type SelectedOutlineDragState,
type SelectedOutlineScaleDragState,
@@ -796,6 +797,104 @@ test('UV handle pointer position maps back to UV coordinates', () => {
expect(result[1]).toBeCloseTo(uv[1], 5);
});
+test('UV handle connection lines connect fields from schema metadata', () => {
+ const points = [
+ {x: 0, y: 0},
+ {x: 100, y: 0},
+ {x: 100, y: 100},
+ {x: 0, y: 100},
+ ] as const;
+
+ const lines = getUvHandleConnectionLines({
+ points,
+ handles: [
+ {
+ effectIndex: 0,
+ fieldKey: 'start',
+ fieldSchema: {
+ type: 'uv-coordinate',
+ default: [0, 0],
+ lineTo: 'end',
+ },
+ value: [0.2, 0.3],
+ },
+ {
+ effectIndex: 0,
+ fieldKey: 'end',
+ fieldSchema: {
+ type: 'uv-coordinate',
+ default: [1, 1],
+ },
+ value: [0.8, 0.7],
+ },
+ ],
+ });
+
+ expect(lines).toEqual([
+ {
+ key: '0-start-end',
+ from: {x: 20, y: 30},
+ to: {x: 80, y: 70},
+ },
+ ]);
+});
+
+test('UV handle connection lines stay within the same effect instance', () => {
+ const points = [
+ {x: 0, y: 0},
+ {x: 100, y: 0},
+ {x: 100, y: 100},
+ {x: 0, y: 100},
+ ] as const;
+
+ const lines = getUvHandleConnectionLines({
+ points,
+ handles: [
+ {
+ effectIndex: 0,
+ fieldKey: 'start',
+ fieldSchema: {
+ type: 'uv-coordinate',
+ default: [0, 0],
+ lineTo: 'end',
+ },
+ value: [0.2, 0.3],
+ },
+ {
+ effectIndex: 1,
+ fieldKey: 'end',
+ fieldSchema: {
+ type: 'uv-coordinate',
+ default: [1, 1],
+ },
+ value: [0.8, 0.7],
+ },
+ {
+ effectIndex: 2,
+ fieldKey: 'start',
+ fieldSchema: {
+ type: 'uv-coordinate',
+ default: [0, 0],
+ lineTo: 'end',
+ },
+ value: [0.2, 0.3],
+ },
+ {
+ effectIndex: 2,
+ fieldKey: 'end',
+ fieldSchema: {
+ type: 'uv-coordinate',
+ default: [1, 1],
+ lineTo: 'start',
+ },
+ value: [0.8, 0.7],
+ },
+ ],
+ });
+
+ expect(lines.map((line) => line.key)).toEqual(['2-start-end']);
+});
+
test('UV handles are requested for selected effect children', () => {
const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
const effectNodePathInfo = makeNodePathInfo(['body', 0], ['effects', '1']);