Skip to content

🎥 Make videos programmatically with React

@remotion/canvas: Own outline derivation

Jonny Burger52c53317d9a8
diff --git a/packages/canvas/src/get-canvas-selectable-outlines.ts b/packages/canvas/src/get-canvas-selectable-outlines.ts
new file mode 100644
index 0000000000..06e5a15a4f
--- /dev/null
+++ b/packages/canvas/src/get-canvas-selectable-outlines.ts
@@ -0,0 +1,57 @@
+import type {_InternalTypes, OverrideIdToNodePaths, TSequence} from 'remotion';
+import {calculateTimeline} from './calculate-timeline';
+import type {SequenceNodePathInfo} from './get-timeline-sequence-sort-key';
+import {getCanvasSequenceSelectionKey} from './selection';
+
+export type CanvasSelectableOutline = {
+ readonly depth: number;
+ readonly keyframeDisplayOffset: number;
+ readonly key: string;
+ readonly nodePathInfo: SequenceNodePathInfo;
+ readonly sequence: TSequence;
+};
+
+export const getCanvasSelectableOutlines = ({
+ sequences,
+ overrideIdsToNodePaths,
+ compositions = [],
+ timelinePosition,
+}: {
+ readonly sequences: readonly TSequence[];
+ readonly overrideIdsToNodePaths: OverrideIdToNodePaths;
+ readonly compositions?: readonly _InternalTypes['AnyComposition'][];
+ readonly timelinePosition: number;
+}): CanvasSelectableOutline[] => {
+ return calculateTimeline({
+ sequences: [...sequences],
+ overrideIdsToNodePaths,
+ compositions,
+ })
+ .filter((track) => {
+ if (track.nodePathInfo === null) {
+ return false;
+ }
+
+ return (
+ track.sequence.showInTimeline &&
+ timelinePosition >= track.sequence.from &&
+ timelinePosition < track.sequence.from + track.sequence.duration &&
+ track.nodePathInfo.auxiliaryKeys.length === 0
+ );
+ })
+ .filter((track) => track.sequence.refForOutline !== null)
+ .sort((a, b) => a.depth - b.depth)
+ .map((track) => {
+ if (track.nodePathInfo === null) {
+ throw new Error('Expected selected outline to have a node path');
+ }
+
+ return {
+ depth: track.depth,
+ keyframeDisplayOffset: track.keyframeDisplayOffset,
+ key: getCanvasSequenceSelectionKey(track.nodePathInfo),
+ nodePathInfo: track.nodePathInfo,
+ sequence: track.sequence,
+ };
+ });
+};
diff --git a/packages/canvas/src/index.ts b/packages/canvas/src/index.ts
index 48c8466d4a..4b47ae50ab 100644
--- a/packages/canvas/src/index.ts
+++ b/packages/canvas/src/index.ts
@@ -4,6 +4,8 @@ export type {CanvasProps} from './canvas';
export {createCanvasController, useCanvasController} from './canvas-controller';
export type {CanvasController} from './canvas-controller';
export {getConnectedCompositions} from './get-connected-compositions';
+export {getCanvasSelectableOutlines} from './get-canvas-selectable-outlines';
+export type {CanvasSelectableOutline} from './get-canvas-selectable-outlines';
export {
getCascadedStart,
getCascadedStartWithTrim,
@@ -19,14 +21,20 @@ export type {
} from './get-timeline-sequence-sort-key';
export {
canvasOutlinesAreEqual,
+ clampCanvasOutlineValue,
createCanvasOutlinesController,
cropCanvasOutlinePoints,
getCanvasOutlinePointAtUv,
+ getCanvasOutlineUvForPoint,
getTransformedSvgViewportPoints,
measureCanvasOutlines,
+ mixCanvasOutlinePoints,
+ mixCanvasOutlineValues,
useCanvasOutlines,
useCanvasOutlinesController,
} from './outlines';
+export {orderCanvasOutlinesForRendering} from './order-outlines-for-rendering';
+export type {CanvasOutlineOrderTarget} from './order-outlines-for-rendering';
export type {
CanvasOutline,
CanvasOutlinePoint,
diff --git a/packages/studio/src/components/selected-outline-order.ts b/packages/canvas/src/order-outlines-for-rendering.ts
similarity index 95%
rename from packages/studio/src/components/selected-outline-order.ts
rename to packages/canvas/src/order-outlines-for-rendering.ts
index fc97d4869c..1f1a27e73c 100644
--- a/packages/studio/src/components/selected-outline-order.ts
+++ b/packages/canvas/src/order-outlines-for-rendering.ts
@@ -1,5 +1,17 @@
-import type {SelectedOutline} from './selected-outline-geometry';
-import type {SelectedOutlineLayoutTarget} from './selected-outline-types';
+import type {CanvasOutline} from './outlines';
+
+export type CanvasOutlineOrderTarget = {
+ readonly key: string;
+ readonly containsSelection: boolean;
+ readonly selected: boolean;
+ readonly sequence: {
+ readonly id: string;
+ readonly parent: string | null;
+ };
+};
+
+type SelectedOutline = CanvasOutline;
+type SelectedOutlineLayoutTarget = CanvasOutlineOrderTarget;
const outlinePointEqualityTolerance = 0.5;
const outlineAreaEqualityTolerance = 0.5;
@@ -353,7 +365,7 @@ const orderOutlineGroup = ({
return ordered;
};
-export const orderOutlinesForRendering = ({
+export const orderCanvasOutlinesForRendering = ({
outlines,
sequences,
targetsByKey,
diff --git a/packages/canvas/src/outlines.ts b/packages/canvas/src/outlines.ts
index b0667e5917..e6ee8f7898 100644
--- a/packages/canvas/src/outlines.ts
+++ b/packages/canvas/src/outlines.ts
@@ -202,13 +202,25 @@ const getElementOutlinePoints = (
: rectToPoints(elementRect, containerRect);
};
-const mixPoint = (
+export const clampCanvasOutlineValue = (
+ value: number,
+ min: number,
+ max: number,
+): number => Math.min(max, Math.max(min, value));
+
+export const mixCanvasOutlineValues = (
+ from: number,
+ to: number,
+ progress: number,
+): number => from + (to - from) * progress;
+
+export const mixCanvasOutlinePoints = (
from: CanvasOutlinePoint,
to: CanvasOutlinePoint,
progress: number,
): CanvasOutlinePoint => ({
- x: from.x + (to.x - from.x) * progress,
- y: from.y + (to.y - from.y) * progress,
+ x: mixCanvasOutlineValues(from.x, to.x, progress),
+ y: mixCanvasOutlineValues(from.y, to.y, progress),
});
type ProjectiveTransform = {
@@ -222,6 +234,8 @@ type ProjectiveTransform = {
readonly h: number;
};
+const projectiveEpsilon = 0.000001;
+
const getProjectiveTransform = (
points: CanvasOutline['points'],
): ProjectiveTransform | null => {
@@ -235,9 +249,9 @@ const getProjectiveTransform = (
let g = 0;
let h = 0;
- if (Math.abs(dx3) > 0.000001 || Math.abs(dy3) > 0.000001) {
+ if (Math.abs(dx3) > projectiveEpsilon || Math.abs(dy3) > projectiveEpsilon) {
const determinant = dx1 * dy2 - dx2 * dy1;
- if (Math.abs(determinant) < 0.000001) {
+ if (Math.abs(determinant) < projectiveEpsilon) {
return null;
}
@@ -280,7 +294,84 @@ export const getCanvasOutlinePointAtUv = (
}
const [tl, tr, br, bl] = points;
- return mixPoint(mixPoint(tl, tr, uv[0]), mixPoint(bl, br, uv[0]), uv[1]);
+ return mixCanvasOutlinePoints(
+ mixCanvasOutlinePoints(tl, tr, uv[0]),
+ mixCanvasOutlinePoints(bl, br, uv[0]),
+ uv[1],
+ );
+};
+
+const getBilinearCanvasOutlineUvForPoint = (
+ points: CanvasOutline['points'],
+ point: CanvasOutlinePoint,
+): readonly [number, number] => {
+ const [tl, tr, br, bl] = points;
+ let u = 0.5;
+ let v = 0.5;
+
+ for (let i = 0; i < 8; i++) {
+ const current = getCanvasOutlinePointAtUv(points, [u, v]);
+ const errorX = current.x - point.x;
+ const errorY = current.y - point.y;
+ if (Math.abs(errorX) + Math.abs(errorY) < 0.001) {
+ break;
+ }
+
+ const du = {
+ x: mixCanvasOutlineValues(tr.x - tl.x, br.x - bl.x, v),
+ y: mixCanvasOutlineValues(tr.y - tl.y, br.y - bl.y, v),
+ };
+ const top = mixCanvasOutlinePoints(tl, tr, u);
+ const bottom = mixCanvasOutlinePoints(bl, br, u);
+ const dv = {x: bottom.x - top.x, y: bottom.y - top.y};
+ const determinant = du.x * dv.y - du.y * dv.x;
+ if (Math.abs(determinant) < projectiveEpsilon) {
+ break;
+ }
+
+ u -= (errorX * dv.y - errorY * dv.x) / determinant;
+ v -= (du.x * errorY - du.y * errorX) / determinant;
+ }
+
+ return [u, v];
+};
+
+export const getCanvasOutlineUvForPoint = (
+ points: CanvasOutline['points'],
+ point: CanvasOutlinePoint,
+): readonly [number, number] => {
+ const transform = getProjectiveTransform(points);
+ if (transform === null) {
+ return getBilinearCanvasOutlineUvForPoint(points, point);
+ }
+
+ const determinant =
+ transform.a * (transform.e - transform.f * transform.h) -
+ transform.b * (transform.d - transform.f * transform.g) +
+ transform.c * (transform.d * transform.h - transform.e * transform.g);
+ if (Math.abs(determinant) < projectiveEpsilon) {
+ return getBilinearCanvasOutlineUvForPoint(points, point);
+ }
+
+ const inverseA = transform.e - transform.f * transform.h;
+ const inverseB = transform.c * transform.h - transform.b;
+ const inverseC = transform.b * transform.f - transform.c * transform.e;
+ const inverseD = transform.f * transform.g - transform.d;
+ const inverseE = transform.a - transform.c * transform.g;
+ const inverseF = transform.c * transform.d - transform.a * transform.f;
+ const inverseG = transform.d * transform.h - transform.e * transform.g;
+ const inverseH = transform.b * transform.g - transform.a * transform.h;
+ const inverseI = transform.a * transform.e - transform.b * transform.d;
+
+ const denominator = inverseG * point.x + inverseH * point.y + inverseI;
+ if (Math.abs(denominator) < projectiveEpsilon) {
+ return getBilinearCanvasOutlineUvForPoint(points, point);
+ }
+
+ return [
+ (inverseA * point.x + inverseB * point.y + inverseC) / denominator,
+ (inverseD * point.x + inverseE * point.y + inverseF) / denominator,
+ ];
};
export const cropCanvasOutlinePoints = (
diff --git a/packages/canvas/src/test/outlines.test.ts b/packages/canvas/src/test/outlines.test.ts
index c000e70825..0e92fbbb75 100644
--- a/packages/canvas/src/test/outlines.test.ts
+++ b/packages/canvas/src/test/outlines.test.ts
@@ -1,9 +1,26 @@
import {expect, test} from 'bun:test';
import {
createCanvasOutlinesController,
+ getCanvasOutlinePointAtUv,
+ getCanvasOutlineUvForPoint,
type CanvasOutlineTarget,
} from '../index';
+test('projects outline UV coordinates in both directions', () => {
+ const points = [
+ {x: 10, y: 20},
+ {x: 180, y: 5},
+ {x: 140, y: 130},
+ {x: 30, y: 100},
+ ] as const;
+ const uv = [0.35, 0.7] as const;
+ const point = getCanvasOutlinePointAtUv(points, uv);
+ const projectedUv = getCanvasOutlineUvForPoint(points, point);
+
+ expect(projectedUv[0]).toBeCloseTo(uv[0]);
+ expect(projectedUv[1]).toBeCloseTo(uv[1]);
+});
+
test('publishes cropped outline geometry and keeps unchanged snapshots stable', () => {
const controller = createCanvasOutlinesController();
const container = document.createElementNS(
diff --git a/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx b/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx
index 60e6bc9463..36396e5905 100644
--- a/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx
+++ b/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx
@@ -1,3 +1,4 @@
+import type {CanvasSelectableOutline} from '@remotion/canvas';
import {PlayerInternals} from '@remotion/player';
import type React from 'react';
import {useCallback, useContext, useEffect, useRef} from 'react';
@@ -15,7 +16,6 @@ import {
type SelectedOutlineKeyframedDragChange,
type SelectedOutlineStaticDragChange,
} from './selected-outline-drag';
-import type {getSequencesWithSelectableOutlines} from './selected-outline-measurement';
import {
getSelectedSequenceKeys,
getSequenceKeysContainingSelection,
@@ -35,9 +35,7 @@ export const SelectedOutlineKeyboardControls: React.FC<{
readonly getLatestOutlineTargetByKey: (
key: string,
) => SelectedOutlineTarget | undefined;
- readonly getSelectableOutlines: () => ReturnType<
- typeof getSequencesWithSelectableOutlines
- >;
+ readonly getSelectableOutlines: () => readonly CanvasSelectableOutline[];
}> = ({getLatestOutlineTargetByKey, getSelectableOutlines}) => {
const currentSelection = useCurrentTimelineSelectionStateAsRef();
const {getDragOverrides} = useContext(
diff --git a/packages/studio/src/components/SelectedOutlineOverlay.tsx b/packages/studio/src/components/SelectedOutlineOverlay.tsx
index 506bbccc47..c7a32a21d6 100644
--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx
+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx
@@ -1,3 +1,4 @@
+import {getCanvasSelectableOutlines} from '@remotion/canvas';
import {PlayerInternals} from '@remotion/player';
import React, {
useCallback,
@@ -38,9 +39,7 @@ import {
getSelectedSequenceKeys,
getSelectedTransformOriginInfo,
getSequenceKeysContainingSelection,
- getSequencesWithSelectableOutlines,
} from './selected-outline-measurement';
-import {orderOutlinesForRendering} from './selected-outline-order';
import {
canEditSelectedOutlineCrop,
cropFieldKeys,
@@ -63,8 +62,6 @@ import {
} from './Timeline/TimelineSelection';
import {propStatusHas3DTransformValue} from './Timeline/transform-3d-mode';
-export {orderOutlinesForRendering};
-
export {
applySelectedOutlineDragAxisLock,
applySelectedOutlineTransformOriginAxisLock,
@@ -102,8 +99,6 @@ export {
getSelectedOutlineRotationDeltaDegrees,
getSelectedOutlineRotationPivot,
getSelectedSequenceKeys,
- getSequencesWithSelectableOutlines,
- getTransformedSvgViewportPoints,
} from './selected-outline-measurement';
export {selectedOutlineDragThresholdPx} from './selected-outline-types';
@@ -185,7 +180,7 @@ const getCropDragFields = ({
return fields as SelectedOutlineCropDragTarget['fields'];
};
-type SelectableOutlines = ReturnType<typeof getSequencesWithSelectableOutlines>;
+type SelectableOutlines = ReturnType<typeof getCanvasSelectableOutlines>;
type CalculateOutlineTargetsOptions = {
readonly mode: 'controls' | 'layout';
@@ -674,7 +669,7 @@ type ActiveSelectedOutlineOverlayProps = Omit<
) => SelectedOutlineTarget | undefined;
readonly getSelectableOutlines: (
timelinePosition: number,
- ) => ReturnType<typeof getSequencesWithSelectableOutlines>;
+ ) => ReturnType<typeof getCanvasSelectableOutlines>;
readonly hoveredTimelineNodePathKey: string | null;
readonly measureAllOutlines: boolean;
readonly onDraggingChange: (dragging: boolean) => void;
@@ -896,7 +891,7 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
return [];
}
- return getSequencesWithSelectableOutlines({
+ return getCanvasSelectableOutlines({
sequences,
overrideIdsToNodePaths: overrideIdToNodePathMappings,
compositions,
@@ -961,9 +956,7 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
const getSelectableOutlinesRef = useRef(getSelectableOutlines);
const selectableOutlinesCacheRef = useRef<{
readonly getSelectableOutlines: typeof getSelectableOutlines;
- readonly selectableOutlines: ReturnType<
- typeof getSequencesWithSelectableOutlines
- >;
+ readonly selectableOutlines: ReturnType<typeof getCanvasSelectableOutlines>;
readonly timelinePosition: number;
} | null>(null);
useLayoutEffect(() => {
diff --git a/packages/studio/src/components/SelectedOutlineRenderer.tsx b/packages/studio/src/components/SelectedOutlineRenderer.tsx
index 0b35b79d56..db7cb92d06 100644
--- a/packages/studio/src/components/SelectedOutlineRenderer.tsx
+++ b/packages/studio/src/components/SelectedOutlineRenderer.tsx
@@ -1,4 +1,8 @@
-import {useCanvasOutlines, useCanvasOutlinesController} from '@remotion/canvas';
+import {
+ orderCanvasOutlinesForRendering,
+ useCanvasOutlines,
+ useCanvasOutlinesController,
+} from '@remotion/canvas';
import React, {
useCallback,
useEffect,
@@ -13,7 +17,6 @@ import {
} from '../state/timeline-sequence-hover';
import {ContextMenuForTarget} from './ContextMenu';
import type {SelectedOutline} from './selected-outline-geometry';
-import {orderOutlinesForRendering} from './selected-outline-order';
import type {
SelectedOutlineContextMenuOpenHandler,
SelectedOutlineLayoutTarget,
@@ -59,7 +62,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
) => void;
readonly scale: number;
readonly sequences: Parameters<
- typeof orderOutlinesForRendering
+ typeof orderCanvasOutlinesForRendering
>[0]['sequences'];
readonly updateOutlinesRef: React.MutableRefObject<() => void>;
}> = ({
@@ -153,7 +156,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
const outlineRenderingOrderRef = useRef<readonly string[]>([]);
const outlinesForRendering = useMemo(() => {
if (!dragging || outlineRenderingOrderRef.current.length === 0) {
- const orderedOutlines = orderOutlinesForRendering({
+ const orderedOutlines = orderCanvasOutlinesForRendering({
outlines: renderState.outlines,
sequences,
targetsByKey,
diff --git a/packages/studio/src/components/selected-outline-geometry.ts b/packages/studio/src/components/selected-outline-geometry.ts
index 504f10bec5..42cb7114e2 100644
--- a/packages/studio/src/components/selected-outline-geometry.ts
+++ b/packages/studio/src/components/selected-outline-geometry.ts
@@ -1,40 +1,9 @@
-export type OutlinePoint = {
- readonly x: number;
- readonly y: number;
-};
-
-export type SelectedOutline = {
- readonly key: string;
- readonly dimensions: {
- readonly width: number;
- readonly height: number;
- } | null;
- readonly uncroppedPoints:
- | readonly [OutlinePoint, OutlinePoint, OutlinePoint, OutlinePoint]
- | null;
- readonly points: readonly [
- OutlinePoint,
- OutlinePoint,
- OutlinePoint,
- OutlinePoint,
- ];
-};
-
-export const clamp = (value: number, min: number, max: number): number => {
- return Math.min(max, Math.max(min, value));
-};
-
-export const mix = (from: number, to: number, progress: number): number => {
- return from + (to - from) * progress;
-};
-
-export const mixPoint = (
- from: OutlinePoint,
- to: OutlinePoint,
- progress: number,
-): OutlinePoint => {
- return {
- x: mix(from.x, to.x, progress),
- y: mix(from.y, to.y, progress),
- };
-};
+export {
+ clampCanvasOutlineValue as clamp,
+ mixCanvasOutlinePoints as mixPoint,
+ mixCanvasOutlineValues as mix,
+} from '@remotion/canvas';
+export type {
+ CanvasOutline as SelectedOutline,
+ CanvasOutlinePoint as OutlinePoint,
+} from '@remotion/canvas';
diff --git a/packages/studio/src/components/selected-outline-measurement.ts b/packages/studio/src/components/selected-outline-measurement.ts
index 494d5dbecc..14856f55a7 100644
--- a/packages/studio/src/components/selected-outline-measurement.ts
+++ b/packages/studio/src/components/selected-outline-measurement.ts
@@ -1,13 +1,6 @@
-export {
- cropCanvasOutlinePoints as cropOutlinePoints,
- getTransformedSvgViewportPoints,
-} from '@remotion/canvas';
-import type {_InternalTypes, OverrideIdToNodePaths, TSequence} from 'remotion';
-import {calculateTimeline} from '../helpers/calculate-timeline';
import {BLACK, WHITE} from '../helpers/colors';
import type {OutlinePoint, SelectedOutline} from './selected-outline-geometry';
import {mixPoint} from './selected-outline-geometry';
-import type {SequenceWithSelectedOutline} from './selected-outline-types';
import {
cropFieldKeys,
rotateFieldKey,
@@ -416,48 +409,3 @@ export const getSelectedRotationInfo = (
: selectedItem.fromFrame,
};
};
-
-export const getSequencesWithSelectableOutlines = ({
- sequences,
- overrideIdsToNodePaths,
- compositions = [],
- timelinePosition,
-}: {
- readonly sequences: readonly TSequence[];
- readonly overrideIdsToNodePaths: OverrideIdToNodePaths;
- readonly compositions?: readonly _InternalTypes['AnyComposition'][];
- readonly timelinePosition: number;
-}): SequenceWithSelectedOutline[] => {
- return calculateTimeline({
- sequences: [...sequences],
- overrideIdsToNodePaths,
- compositions,
- })
- .filter((track) => {
- if (track.nodePathInfo === null) {
- return false;
- }
-
- return (
- track.sequence.showInTimeline &&
- timelinePosition >= track.sequence.from &&
- timelinePosition < track.sequence.from + track.sequence.duration &&
- track.nodePathInfo.auxiliaryKeys.length === 0
- );
- })
- .filter((track) => track.sequence.refForOutline !== null)
- .sort((a, b) => a.depth - b.depth)
- .map((track) => {
- if (track.nodePathInfo === null) {
- throw new Error('Expected selected outline to have a node path');
- }
-
- return {
- depth: track.depth,
- keyframeDisplayOffset: track.keyframeDisplayOffset,
- key: getTimelineSequenceSelectionKey(track.nodePathInfo),
- nodePathInfo: track.nodePathInfo,
- sequence: track.sequence,
- };
- });
-};
diff --git a/packages/studio/src/components/selected-outline-types.ts b/packages/studio/src/components/selected-outline-types.ts
index 872f6db429..e3cd213b4b 100644
--- a/packages/studio/src/components/selected-outline-types.ts
+++ b/packages/studio/src/components/selected-outline-types.ts
@@ -218,14 +218,6 @@ export type SelectedOutlineRotationDragState = {
readonly target: SelectedOutlineRotationDragTarget;
};
-export type SequenceWithSelectedOutline = {
- readonly depth: number;
- readonly keyframeDisplayOffset: number;
- readonly key: string;
- readonly nodePathInfo: SequenceNodePathInfo;
- readonly sequence: TSequence;
-};
-
export const translateFieldKey = 'style.translate';
export const scaleFieldKey = 'style.scale';
export const rotateFieldKey = 'style.rotate';
diff --git a/packages/studio/src/components/selected-outline-uv.ts b/packages/studio/src/components/selected-outline-uv.ts
index 64a6dabc4b..8c7b832040 100644
--- a/packages/studio/src/components/selected-outline-uv.ts
+++ b/packages/studio/src/components/selected-outline-uv.ts
@@ -1,3 +1,7 @@
+import {
+ getCanvasOutlinePointAtUv,
+ getCanvasOutlineUvForPoint,
+} from '@remotion/canvas';
import type {
CanUpdateSequencePropStatusKeyframed,
CanUpdateSequencePropStatusStatic,
@@ -11,8 +15,6 @@ import type {
import {Internals} from 'remotion';
import {
clamp,
- mix,
- mixPoint,
type OutlinePoint,
type SelectedOutline,
} from './selected-outline-geometry';
@@ -270,84 +272,10 @@ export const tuplesEqual = (left: unknown, right: UvCoordinate): boolean => {
return left[0] === right[0] && left[1] === right[1];
};
-const getBilinearUvHandlePosition = (
- points: SelectedOutline['points'],
- uv: UvCoordinate,
-): OutlinePoint => {
- const [tl, tr, br, bl] = points;
- const top = mixPoint(tl, tr, uv[0]);
- const bottom = mixPoint(bl, br, uv[0]);
- return mixPoint(top, bottom, uv[1]);
-};
-
-type ProjectiveTransform = {
- readonly a: number;
- readonly b: number;
- readonly c: number;
- readonly d: number;
- readonly e: number;
- readonly f: number;
- readonly g: number;
- readonly h: number;
-};
-
-const projectiveEpsilon = 0.000001;
-
-const getProjectiveTransform = (
- points: SelectedOutline['points'],
-): ProjectiveTransform | null => {
- const [tl, tr, br, bl] = points;
- const dx1 = tr.x - br.x;
- const dx2 = bl.x - br.x;
- const dx3 = tl.x - tr.x + br.x - bl.x;
- const dy1 = tr.y - br.y;
- const dy2 = bl.y - br.y;
- const dy3 = tl.y - tr.y + br.y - bl.y;
-
- let g = 0;
- let h = 0;
- if (Math.abs(dx3) > projectiveEpsilon || Math.abs(dy3) > projectiveEpsilon) {
- const determinant = dx1 * dy2 - dx2 * dy1;
- if (Math.abs(determinant) < projectiveEpsilon) {
- return null;
- }
-
- g = (dx3 * dy2 - dx2 * dy3) / determinant;
- h = (dx1 * dy3 - dx3 * dy1) / determinant;
- }
-
- return {
- a: tr.x - tl.x + g * tr.x,
- b: bl.x - tl.x + h * bl.x,
- c: tl.x,
- d: tr.y - tl.y + g * tr.y,
- e: bl.y - tl.y + h * bl.y,
- f: tl.y,
- g,
- h,
- };
-};
-
-const applyProjectiveTransform = (
- transform: ProjectiveTransform,
- uv: UvCoordinate,
-): OutlinePoint => {
- const denominator = transform.g * uv[0] + transform.h * uv[1] + 1;
- return {
- x: (transform.a * uv[0] + transform.b * uv[1] + transform.c) / denominator,
- y: (transform.d * uv[0] + transform.e * uv[1] + transform.f) / denominator,
- };
-};
-
export const getUvHandlePosition = (
points: SelectedOutline['points'],
uv: UvCoordinate,
-): OutlinePoint => {
- const transform = getProjectiveTransform(points);
- return transform === null
- ? getBilinearUvHandlePosition(points, uv)
- : applyProjectiveTransform(transform, uv);
-};
+): OutlinePoint => getCanvasOutlinePointAtUv(points, uv);
export const getUvHandleConnectionLines = ({
handles,
@@ -606,80 +534,10 @@ export const getUvEllipseInteractiveControls = ({
return controls;
};
-const vectorBetween = (from: OutlinePoint, to: OutlinePoint): OutlinePoint => {
- return {x: to.x - from.x, y: to.y - from.y};
-};
-
-const getBilinearUvCoordinateForPoint = (
- points: SelectedOutline['points'],
- point: OutlinePoint,
-): UvCoordinate => {
- const [tl, tr, br, bl] = points;
- let u = 0.5;
- let v = 0.5;
-
- for (let i = 0; i < 8; i++) {
- const current = getBilinearUvHandlePosition(points, [u, v]);
- const errorX = current.x - point.x;
- const errorY = current.y - point.y;
- if (Math.abs(errorX) + Math.abs(errorY) < 0.001) {
- break;
- }
-
- const du = {
- x: mix(tr.x - tl.x, br.x - bl.x, v),
- y: mix(tr.y - tl.y, br.y - bl.y, v),
- };
- const dv = vectorBetween(mixPoint(tl, tr, u), mixPoint(bl, br, u));
- const determinant = du.x * dv.y - du.y * dv.x;
- if (Math.abs(determinant) < 0.000001) {
- break;
- }
-
- u -= (errorX * dv.y - errorY * dv.x) / determinant;
- v -= (du.x * errorY - du.y * errorX) / determinant;
- }
-
- return [u, v];
-};
-
export const getUvCoordinateForPoint = (
points: SelectedOutline['points'],
point: OutlinePoint,
-): UvCoordinate => {
- const transform = getProjectiveTransform(points);
- if (transform === null) {
- return getBilinearUvCoordinateForPoint(points, point);
- }
-
- const determinant =
- transform.a * (transform.e - transform.f * transform.h) -
- transform.b * (transform.d - transform.f * transform.g) +
- transform.c * (transform.d * transform.h - transform.e * transform.g);
- if (Math.abs(determinant) < projectiveEpsilon) {
- return getBilinearUvCoordinateForPoint(points, point);
- }
-
- const inverseA = transform.e - transform.f * transform.h;
- const inverseB = transform.c * transform.h - transform.b;
- const inverseC = transform.b * transform.f - transform.c * transform.e;
- const inverseD = transform.f * transform.g - transform.d;
- const inverseE = transform.a - transform.c * transform.g;
- const inverseF = transform.c * transform.d - transform.a * transform.f;
- const inverseG = transform.d * transform.h - transform.e * transform.g;
- const inverseH = transform.b * transform.g - transform.a * transform.h;
- const inverseI = transform.a * transform.e - transform.b * transform.d;
-
- const denominator = inverseG * point.x + inverseH * point.y + inverseI;
- if (Math.abs(denominator) < projectiveEpsilon) {
- return getBilinearUvCoordinateForPoint(points, point);
- }
-
- return [
- (inverseA * point.x + inverseB * point.y + inverseC) / denominator,
- (inverseD * point.x + inverseE * point.y + inverseF) / denominator,
- ];
-};
+): UvCoordinate => getCanvasOutlineUvForPoint(points, point);
export function constrainUv(
value: UvCoordinate,
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 2b3caad683..5bae130c3e 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -1,4 +1,11 @@
import {expect, test} from 'bun:test';
+import {
+ cropCanvasOutlinePoints as cropOutlinePoints,
+ getCanvasSelectableOutlines as getSequencesWithSelectableOutlines,
+ getTransformedSvgViewportPoints,
+ orderCanvasOutlinesForRendering as orderOutlinesForRendering,
+ type CanvasOutline as SelectedOutline,
+} from '@remotion/canvas';
import type {RefObject} from 'react';
import {
Internals,
@@ -12,9 +19,7 @@ import {
import {NoReactInternals} from 'remotion/no-react';
import {getInspectorSelectableItems} from '../components/InspectorSequenceSection';
import {getSelectedOutlineControlLayout} from '../components/selected-outline-control-layout';
-import type {SelectedOutline} from '../components/selected-outline-geometry';
import {
- cropOutlinePoints,
getSelectedCropInfo,
getSelectedTransformOriginInfo,
} from '../components/selected-outline-measurement';
@@ -61,10 +66,7 @@ import {
getSelectedOutlineTransformOriginDragChanges,
getSelectedOutlineTransformOriginLockedAxis,
getSelectedSequenceKeys,
- getSequencesWithSelectableOutlines,
- getTransformedSvgViewportPoints,
isSelectedOutlineDragPastThreshold,
- orderOutlinesForRendering,
selectedOutlineDragThresholdPx,
selectedOutlineTransformOriginSnapThresholdPx,
selectedOutlineUvSnapThresholdPx,