Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Revert canvas outline migration

JonnyBurgerc1d6cd4a053e
diff --git a/packages/canvas/src/canvas-controller.ts b/packages/canvas/src/canvas-controller.ts
index 703dff6f8d..22d8a6fa26 100644
--- a/packages/canvas/src/canvas-controller.ts
+++ b/packages/canvas/src/canvas-controller.ts
@@ -2,10 +2,6 @@ import {useState} from 'react';
import type {TSequence} from 'remotion';
import {calculateTimeline} from './calculate-timeline';
import type {TimelineTrackData} from './get-timeline-sequence-sort-key';
-import {
- createCanvasOutlinesController,
- type CanvasOutlinesController,
-} from './outlines';
import {
createCanvasSelectionController,
type CanvasSelectionController,
@@ -16,7 +12,6 @@ export type CanvasController = {
readonly getSnapshot: () => readonly TimelineTrackData[];
readonly subscribe: (listener: () => void) => () => void;
};
- readonly outlines: CanvasOutlinesController;
readonly selection: CanvasSelectionController;
};
@@ -51,7 +46,6 @@ export const createCanvasController = (): CanvasController => {
return () => timelineListeners.delete(listener);
},
},
- outlines: createCanvasOutlinesController(),
selection: createCanvasSelectionController(),
};
@@ -64,10 +58,7 @@ export const createCanvasController = (): CanvasController => {
}),
);
},
- clear: () => {
- controller.outlines.disconnect();
- updateTimelineSnapshot([]);
- },
+ clear: () => updateTimelineSnapshot([]),
});
return controller;
diff --git a/packages/canvas/src/get-canvas-selectable-outlines.ts b/packages/canvas/src/get-canvas-selectable-outlines.ts
deleted file mode 100644
index 06e5a15a4f..0000000000
--- a/packages/canvas/src/get-canvas-selectable-outlines.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-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 4b47ae50ab..e4b4b2edd9 100644
--- a/packages/canvas/src/index.ts
+++ b/packages/canvas/src/index.ts
@@ -4,8 +4,6 @@ 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,29 +17,6 @@ export type {
TimelineTrackData,
TimelineTrackWithOriginalTimings,
} 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,
- CanvasOutlinesController,
- CanvasOutlinesSnapshot,
- CanvasOutlineTarget,
-} from './outlines';
export {
createCanvasSelectionController,
EMPTY_CANVAS_SELECTION,
diff --git a/packages/canvas/src/outlines.ts b/packages/canvas/src/outlines.ts
deleted file mode 100644
index 915d375b44..0000000000
--- a/packages/canvas/src/outlines.ts
+++ /dev/null
@@ -1,633 +0,0 @@
-import {useState, useSyncExternalStore, type RefObject} from 'react';
-import {useCache as resetBoxQuadsCache} from './get-box-quads-polyfill-internals.js';
-import {getBoxQuadsPonyfill} from './get-box-quads-ponyfill';
-
-export type CanvasOutlinePoint = {
- readonly x: number;
- readonly y: number;
-};
-
-export type CanvasOutline = {
- readonly key: string;
- readonly dimensions: {
- readonly width: number;
- readonly height: number;
- } | null;
- readonly uncroppedPoints:
- | readonly [
- CanvasOutlinePoint,
- CanvasOutlinePoint,
- CanvasOutlinePoint,
- CanvasOutlinePoint,
- ]
- | null;
- readonly points: readonly [
- CanvasOutlinePoint,
- CanvasOutlinePoint,
- CanvasOutlinePoint,
- CanvasOutlinePoint,
- ];
-};
-
-export type CanvasOutlineTarget = {
- readonly key: string;
- readonly ref: RefObject<Element | null>;
- readonly crop: {
- readonly left: number;
- readonly right: number;
- readonly top: number;
- readonly bottom: number;
- };
- readonly includeOutsideContainer: boolean;
-};
-
-export type CanvasOutlinesSnapshot<
- Target extends CanvasOutlineTarget = CanvasOutlineTarget,
-> = {
- readonly outlines: readonly CanvasOutline[];
- readonly targets: readonly Target[];
-};
-
-export type CanvasOutlinesController<
- Target extends CanvasOutlineTarget = CanvasOutlineTarget,
-> = {
- readonly getSnapshot: () => CanvasOutlinesSnapshot<Target>;
- readonly subscribe: (listener: () => void) => () => void;
- readonly update: (
- container: SVGSVGElement | null,
- targets: readonly Target[],
- ) => void;
- readonly disconnect: () => void;
-};
-
-const rectToPoints = (
- elementRect: DOMRect,
- containerRect: DOMRect,
-): CanvasOutline['points'] => {
- const left = elementRect.left - containerRect.left;
- const top = elementRect.top - containerRect.top;
- const right = elementRect.right - containerRect.left;
- const bottom = elementRect.bottom - containerRect.top;
-
- return [
- {x: left, y: top},
- {x: right, y: top},
- {x: right, y: bottom},
- {x: left, y: bottom},
- ];
-};
-
-type SvgViewport = {
- readonly x: number;
- readonly y: number;
- readonly width: number;
- readonly height: number;
-};
-
-type SvgScreenCtm = Pick<DOMMatrixReadOnly, 'a' | 'b' | 'c' | 'd' | 'e' | 'f'>;
-
-export const getTransformedSvgViewportPoints = ({
- viewport,
- ctm,
- containerRect,
-}: {
- readonly viewport: SvgViewport;
- readonly ctm: SvgScreenCtm;
- readonly containerRect: Pick<DOMRect, 'left' | 'top'>;
-}): CanvasOutline['points'] => {
- const transformPoint = (x: number, y: number): CanvasOutlinePoint => ({
- x: ctm.a * x + ctm.c * y + ctm.e - containerRect.left,
- y: ctm.b * x + ctm.d * y + ctm.f - containerRect.top,
- });
-
- const left = viewport.x;
- const top = viewport.y;
- const right = viewport.x + viewport.width;
- const bottom = viewport.y + viewport.height;
-
- return [
- transformPoint(left, top),
- transformPoint(right, top),
- transformPoint(right, bottom),
- transformPoint(left, bottom),
- ];
-};
-
-const quadToPoints = (
- quad: DOMQuad,
- containerRect: DOMRect,
-): CanvasOutline['points'] => {
- return [
- {x: quad.p1.x - containerRect.left, y: quad.p1.y - containerRect.top},
- {x: quad.p2.x - containerRect.left, y: quad.p2.y - containerRect.top},
- {x: quad.p3.x - containerRect.left, y: quad.p3.y - containerRect.top},
- {x: quad.p4.x - containerRect.left, y: quad.p4.y - containerRect.top},
- ];
-};
-
-const isSvgSvgElement = (element: Element): element is SVGSVGElement => {
- const svgElement = element.ownerDocument.defaultView?.SVGSVGElement;
- return svgElement !== undefined && element instanceof svgElement;
-};
-
-const isHtmlElement = (element: Element): element is HTMLElement => {
- const htmlElement = element.ownerDocument.defaultView?.HTMLElement;
- return htmlElement !== undefined && element instanceof htmlElement;
-};
-
-const getSvgSvgElementViewport = (element: SVGSVGElement): SvgViewport => {
- const viewBox = element.viewBox.baseVal;
- if (viewBox.width > 0 && viewBox.height > 0) {
- return {
- x: viewBox.x,
- y: viewBox.y,
- width: viewBox.width,
- height: viewBox.height,
- };
- }
-
- return {
- x: 0,
- y: 0,
- width: element.width.baseVal.value,
- height: element.height.baseVal.value,
- };
-};
-
-const getSvgSvgElementOutlinePoints = (
- element: SVGSVGElement,
- containerRect: DOMRect,
-): CanvasOutline['points'] | null => {
- const ctm = element.getScreenCTM();
- const viewport = getSvgSvgElementViewport(element);
- if (ctm === null || (viewport.width === 0 && viewport.height === 0)) {
- return null;
- }
-
- return getTransformedSvgViewportPoints({
- viewport,
- ctm,
- containerRect,
- });
-};
-
-const getElementOutlinePoints = (
- element: Element,
- containerRect: DOMRect,
- includeOutsideContainer: boolean,
-): CanvasOutline['points'] | null => {
- const elementRect = element.getBoundingClientRect();
-
- if (elementRect.width === 0 && elementRect.height === 0) {
- return null;
- }
-
- if (
- !includeOutsideContainer &&
- (elementRect.right <= containerRect.left ||
- elementRect.left >= containerRect.right ||
- elementRect.bottom <= containerRect.top ||
- elementRect.top >= containerRect.bottom)
- ) {
- return null;
- }
-
- if (isSvgSvgElement(element)) {
- return getSvgSvgElementOutlinePoints(element, containerRect);
- }
-
- const quad = getBoxQuadsPonyfill(element, {box: 'border'})?.[0];
- return quad
- ? quadToPoints(quad, containerRect)
- : rectToPoints(elementRect, containerRect);
-};
-
-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: mixCanvasOutlineValues(from.x, to.x, progress),
- y: mixCanvasOutlineValues(from.y, to.y, progress),
-});
-
-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: CanvasOutline['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,
- };
-};
-
-export const getCanvasOutlinePointAtUv = (
- points: CanvasOutline['points'],
- uv: readonly [number, number],
-): CanvasOutlinePoint => {
- const projectiveTransform = getProjectiveTransform(points);
- if (projectiveTransform !== null) {
- const denominator =
- projectiveTransform.g * uv[0] + projectiveTransform.h * uv[1] + 1;
- return {
- x:
- (projectiveTransform.a * uv[0] +
- projectiveTransform.b * uv[1] +
- projectiveTransform.c) /
- denominator,
- y:
- (projectiveTransform.d * uv[0] +
- projectiveTransform.e * uv[1] +
- projectiveTransform.f) /
- denominator,
- };
- }
-
- const [tl, tr, br, bl] = points;
- 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 = (
- points: CanvasOutline['points'],
- crop: CanvasOutlineTarget['crop'],
-): CanvasOutline['points'] => {
- if (
- crop.left === 0 &&
- crop.right === 0 &&
- crop.top === 0 &&
- crop.bottom === 0
- ) {
- return points;
- }
-
- const right = 1 - crop.right;
- const bottom = 1 - crop.bottom;
- return [
- getCanvasOutlinePointAtUv(points, [crop.left, crop.top]),
- getCanvasOutlinePointAtUv(points, [right, crop.top]),
- getCanvasOutlinePointAtUv(points, [right, bottom]),
- getCanvasOutlinePointAtUv(points, [crop.left, bottom]),
- ];
-};
-
-export const measureCanvasOutlines = <Target extends CanvasOutlineTarget>(
- container: Element,
- targets: readonly Target[],
-): CanvasOutline[] => {
- resetBoxQuadsCache();
- const containerRect = container.getBoundingClientRect();
- const outlines: CanvasOutline[] = [];
-
- for (const target of targets) {
- const element = target.ref.current;
- if (element === null) {
- continue;
- }
-
- const uncroppedPoints = getElementOutlinePoints(
- element,
- containerRect,
- target.includeOutsideContainer,
- );
- if (uncroppedPoints === null) {
- continue;
- }
-
- outlines.push({
- key: target.key,
- dimensions: isHtmlElement(element)
- ? {width: element.offsetWidth, height: element.offsetHeight}
- : isSvgSvgElement(element)
- ? {
- width: element.width.baseVal.value,
- height: element.height.baseVal.value,
- }
- : null,
- uncroppedPoints,
- points: cropCanvasOutlinePoints(uncroppedPoints, target.crop),
- });
- }
-
- return outlines;
-};
-
-export const canvasOutlinesAreEqual = (
- a: readonly CanvasOutline[],
- b: readonly CanvasOutline[],
-): boolean => {
- if (a.length !== b.length) {
- return false;
- }
-
- for (let i = 0; i < a.length; i++) {
- if (
- a[i].key !== b[i].key ||
- a[i].dimensions?.width !== b[i].dimensions?.width ||
- a[i].dimensions?.height !== b[i].dimensions?.height
- ) {
- return false;
- }
-
- const aUncropped = a[i].uncroppedPoints;
- const bUncropped = b[i].uncroppedPoints;
- if ((aUncropped === null) !== (bUncropped === null)) {
- return false;
- }
-
- if (aUncropped !== null && bUncropped !== null) {
- for (let j = 0; j < aUncropped.length; j++) {
- if (
- Math.abs(aUncropped[j].x - bUncropped[j].x) > 0.01 ||
- Math.abs(aUncropped[j].y - bUncropped[j].y) > 0.01
- ) {
- return false;
- }
- }
- }
-
- for (let j = 0; j < a[i].points.length; j++) {
- if (
- Math.abs(a[i].points[j].x - b[i].points[j].x) > 0.01 ||
- Math.abs(a[i].points[j].y - b[i].points[j].y) > 0.01
- ) {
- return false;
- }
- }
- }
-
- return true;
-};
-
-export const createCanvasOutlinesController = <
- Target extends CanvasOutlineTarget = CanvasOutlineTarget,
->(): CanvasOutlinesController<Target> => {
- let snapshot: CanvasOutlinesSnapshot<Target> = {outlines: [], targets: []};
- let container: SVGSVGElement | null = null;
- let targets: readonly Target[] = [];
- let resizeObserver: ResizeObserver | null = null;
- let resizeAnimationFrame: number | null = null;
- let observedElements = new Set<Element>();
- const listeners = new Set<() => void>();
-
- const publish = (outlines: readonly CanvasOutline[]) => {
- if (
- snapshot.targets === targets &&
- canvasOutlinesAreEqual(snapshot.outlines, outlines)
- ) {
- return;
- }
-
- snapshot = {
- outlines: canvasOutlinesAreEqual(snapshot.outlines, outlines)
- ? snapshot.outlines
- : outlines,
- targets,
- };
- for (const listener of listeners) {
- listener();
- }
- };
-
- const calculate = () => {
- publish(
- container === null || targets.length === 0
- ? []
- : measureCanvasOutlines(container, targets),
- );
- };
-
- const cancelScheduledCalculation = () => {
- if (resizeAnimationFrame === null) {
- return;
- }
-
- container?.ownerDocument.defaultView?.cancelAnimationFrame(
- resizeAnimationFrame,
- );
- resizeAnimationFrame = null;
- };
-
- const disconnectObserver = () => {
- cancelScheduledCalculation();
- resizeObserver?.disconnect();
- resizeObserver = null;
- observedElements = new Set();
- };
-
- const scheduleCalculation = () => {
- if (resizeAnimationFrame !== null || container === null) {
- return;
- }
-
- const ownerWindow = container.ownerDocument.defaultView;
- if (ownerWindow?.requestAnimationFrame === undefined) {
- calculate();
- return;
- }
-
- resizeAnimationFrame = ownerWindow.requestAnimationFrame(() => {
- resizeAnimationFrame = null;
- calculate();
- });
- };
-
- const updateObserver = (containerChanged: boolean) => {
- const ResizeObserverConstructor =
- container?.ownerDocument.defaultView?.ResizeObserver;
- if (container === null || ResizeObserverConstructor === undefined) {
- disconnectObserver();
- return;
- }
-
- if (resizeObserver === null || containerChanged) {
- disconnectObserver();
- resizeObserver = new ResizeObserverConstructor(scheduleCalculation);
- }
-
- const nextObservedElements = new Set<Element>([container]);
- for (const target of targets) {
- if (target.ref.current !== null) {
- nextObservedElements.add(target.ref.current);
- }
- }
-
- for (const element of observedElements) {
- if (!nextObservedElements.has(element)) {
- resizeObserver.unobserve(element);
- }
- }
-
- for (const element of nextObservedElements) {
- if (!observedElements.has(element)) {
- resizeObserver.observe(element);
- }
- }
-
- observedElements = nextObservedElements;
- };
-
- return {
- getSnapshot: () => snapshot,
- subscribe: (listener) => {
- listeners.add(listener);
- return () => listeners.delete(listener);
- },
- update: (nextContainer, nextTargets) => {
- const containerChanged = container !== nextContainer;
- container = nextContainer;
- targets = nextTargets;
- updateObserver(containerChanged);
- calculate();
- },
- disconnect: () => {
- disconnectObserver();
- container = null;
- targets = [];
- publish([]);
- },
- };
-};
-
-export const useCanvasOutlinesController = <
- Target extends CanvasOutlineTarget = CanvasOutlineTarget,
->(): CanvasOutlinesController<Target> => {
- const [controller] = useState(createCanvasOutlinesController<Target>);
- return controller;
-};
-
-export const useCanvasOutlines = <Target extends CanvasOutlineTarget>(
- controller: CanvasOutlinesController<Target>,
-): CanvasOutlinesSnapshot<Target> => {
- return useSyncExternalStore(
- controller.subscribe,
- controller.getSnapshot,
- controller.getSnapshot,
- );
-};
diff --git a/packages/canvas/src/test/outlines.test.ts b/packages/canvas/src/test/outlines.test.ts
deleted file mode 100644
index 0e92fbbb75..0000000000
--- a/packages/canvas/src/test/outlines.test.ts
+++ /dev/null
@@ -1,119 +0,0 @@
-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(
- 'http://www.w3.org/2000/svg',
- 'svg',
- );
- container.getBoundingClientRect = () =>
- ({
- bottom: 220,
- height: 200,
- left: 10,
- right: 210,
- top: 20,
- width: 200,
- x: 10,
- y: 20,
- toJSON: () => undefined,
- }) as DOMRect;
-
- const element = document.createElement('div');
- Object.defineProperties(element, {
- offsetHeight: {value: 50},
- offsetWidth: {value: 100},
- });
- let left = 20;
- Object.assign(element, {
- getBoundingClientRect: () =>
- ({
- bottom: 90,
- height: 50,
- left,
- right: left + 100,
- top: 40,
- width: 100,
- x: left,
- y: 40,
- toJSON: () => undefined,
- }) as DOMRect,
- getBoxQuads: () => [
- {
- p1: {x: left, y: 40},
- p2: {x: left + 100, y: 40},
- p3: {x: left + 100, y: 90},
- p4: {x: left, y: 90},
- },
- ],
- });
-
- const targets: readonly CanvasOutlineTarget[] = [
- {
- key: 'title',
- ref: {current: element},
- crop: {bottom: 0.1, left: 0.1, right: 0.1, top: 0.1},
- includeOutsideContainer: false,
- },
- ];
- let updates = 0;
- const unsubscribe = controller.subscribe(() => updates++);
-
- controller.update(container, targets);
- expect(controller.getSnapshot()).toEqual({
- outlines: [
- {
- key: 'title',
- dimensions: {height: 50, width: 100},
- uncroppedPoints: [
- {x: 10, y: 20},
- {x: 110, y: 20},
- {x: 110, y: 70},
- {x: 10, y: 70},
- ],
- points: [
- {x: 20, y: 25},
- {x: 100, y: 25},
- {x: 100, y: 65},
- {x: 20, y: 65},
- ],
- },
- ],
- targets,
- });
- expect(updates).toBe(1);
-
- controller.update(container, targets);
- expect(updates).toBe(1);
-
- left = 30;
- controller.update(container, targets);
- expect(controller.getSnapshot().outlines[0].points[0].x).toBe(30);
- expect(updates).toBe(2);
-
- controller.disconnect();
- expect(controller.getSnapshot()).toEqual({outlines: [], targets: []});
- expect(updates).toBe(3);
- unsubscribe();
-});
diff --git a/packages/canvas/tsconfig.json b/packages/canvas/tsconfig.json
index a32620f568..7034960cb3 100644
--- a/packages/canvas/tsconfig.json
+++ b/packages/canvas/tsconfig.json
@@ -7,7 +7,6 @@
"target": "ES2022",
"module": "es2020",
"moduleResolution": "bundler",
- "allowJs": true,
"declaration": true,
"emitDeclarationOnly": true
},
diff --git a/packages/player-example/pages/canvas.tsx b/packages/player-example/pages/canvas.tsx
index 8fed119129..e8bb871d46 100644
--- a/packages/player-example/pages/canvas.tsx
+++ b/packages/player-example/pages/canvas.tsx
@@ -2,19 +2,13 @@ import {
Canvas,
getCanvasSelectionItemKey,
useCanvasController,
- useCanvasOutlines,
useCanvasSelection,
type CanvasSelectionItem,
type SequenceNodePathInfo,
type TimelineTrackData,
} from '@remotion/canvas';
-import React, {
- useLayoutEffect,
- useMemo,
- useRef,
- useSyncExternalStore,
-} from 'react';
-import {AbsoluteFill, Interactive, Sequence, useCurrentFrame} from 'remotion';
+import React, {useSyncExternalStore} from 'react';
+import {AbsoluteFill, Sequence, useCurrentFrame} from 'remotion';
const CanvasComposition: React.FC = () => {
const frame = useCurrentFrame();
@@ -37,40 +31,9 @@ const CanvasComposition: React.FC = () => {
}}
/>
</Sequence>
- <Interactive.Div
- name="Outline test card"
- style={{
- alignItems: 'center',
- backgroundColor: '#f59e0b',
- borderRadius: 28,
- boxShadow: '0 24px 60px rgba(0, 0, 0, 0.35)',
- color: '#111827',
- display: 'flex',
- fontSize: 44,
- fontWeight: 700,
- height: 180,
- justifyContent: 'center',
- left: 100,
- position: 'absolute',
- rotate: '-6deg',
- top: 90,
- width: 360,
- }}
- >
- Outline test
- </Interactive.Div>
{frame < 90 ? (
<Sequence name="Intro card" durationInFrames={90}>
- <h1
- style={{
- fontSize: 88,
- transform: `translateX(${Math.sin(frame / 12) * 80}px) rotate(${
- Math.sin(frame / 18) * 8
- }deg)`,
- }}
- >
- Canvas
- </h1>
+ <h1 style={{fontSize: 88}}>Canvas</h1>
</Sequence>
) : null}
{frame >= 60 ? (
@@ -110,45 +73,13 @@ const CanvasPage: React.FC = () => {
controller.timeline.getSnapshot,
);
const selection = useCanvasSelection(controller.selection);
- const outlineSnapshot = useCanvasOutlines(controller.outlines);
- const outlineOverlayRef = useRef<SVGSVGElement>(null);
- const selectedKeys = useMemo(
- () => new Set(selection.selectedItems.map(getCanvasSelectionItemKey)),
- [selection.selectedItems],
+ const selectedKeys = new Set(
+ selection.selectedItems.map(getCanvasSelectionItemKey),
);
const selectableLayers: CanvasSelectionItem[] = layers.map((layer) => ({
type: 'sequence',
nodePathInfo: getExampleNodePathInfo(layer),
}));
- const outlineTargets = useMemo(() => {
- return layers.flatMap((layer) => {
- if (layer.sequence.refForOutline === null) {
- return [];
- }
-
- const selectionItem: CanvasSelectionItem = {
- type: 'sequence',
- nodePathInfo: getExampleNodePathInfo(layer),
- };
- const key = getCanvasSelectionItemKey(selectionItem);
- if (!selectedKeys.has(key)) {
- return [];
- }
-
- return [
- {
- key,
- ref: layer.sequence.refForOutline,
- crop: {bottom: 0, left: 0, right: 0, top: 0},
- includeOutsideContainer: true,
- },
- ];
- });
- }, [layers, selectedKeys]);
-
- useLayoutEffect(() => {
- controller.outlines.update(outlineOverlayRef.current, outlineTargets);
- }, [controller, outlineTargets]);
return (
<main
@@ -172,43 +103,17 @@ const CanvasPage: React.FC = () => {
gridTemplateColumns: 'minmax(0, 2fr) minmax(240px, 1fr)',
}}
>
- <div style={{position: 'relative'}}>
- <Canvas
- controller={controller}
- component={CanvasComposition}
- compositionWidth={1280}
- compositionHeight={720}
- durationInFrames={180}
- fps={30}
- controls
- acknowledgeRemotionLicense
- style={{width: '100%'}}
- />
- <svg
- ref={outlineOverlayRef}
- aria-hidden="true"
- style={{
- inset: 0,
- overflow: 'visible',
- pointerEvents: 'none',
- position: 'absolute',
- }}
- width="100%"
- height="100%"
- >
- {outlineSnapshot.outlines.map((outline) => (
- <polygon
- key={outline.key}
- fill="rgba(124, 58, 237, 0.15)"
- points={outline.points
- .map((point) => `${point.x},${point.y}`)
- .join(' ')}
- stroke="#a78bfa"
- strokeWidth={2}
- />
- ))}
- </svg>
- </div>
+ <Canvas
+ controller={controller}
+ component={CanvasComposition}
+ compositionWidth={1280}
+ compositionHeight={720}
+ durationInFrames={180}
+ fps={30}
+ controls
+ acknowledgeRemotionLicense
+ style={{width: '100%'}}
+ />
<div>
<section
style={{
@@ -343,8 +248,6 @@ const CanvasPage: React.FC = () => {
Click a layer to replace the selection. Shift-click selects a
contiguous range; Command/Ctrl-click toggles a layer. Property and
easing buttons demonstrate different selection item types.
- Selected layers with a mounted outline ref are drawn over the
- Canvas by the outline controller.
</p>
<button type="button" onClick={controller.selection.clear}>
Clear selection
diff --git a/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx b/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx
index 36396e5905..60e6bc9463 100644
--- a/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx
+++ b/packages/studio/src/components/SelectedOutlineKeyboardControls.tsx
@@ -1,4 +1,3 @@
-import type {CanvasSelectableOutline} from '@remotion/canvas';
import {PlayerInternals} from '@remotion/player';
import type React from 'react';
import {useCallback, useContext, useEffect, useRef} from 'react';
@@ -16,6 +15,7 @@ import {
type SelectedOutlineKeyframedDragChange,
type SelectedOutlineStaticDragChange,
} from './selected-outline-drag';
+import type {getSequencesWithSelectableOutlines} from './selected-outline-measurement';
import {
getSelectedSequenceKeys,
getSequenceKeysContainingSelection,
@@ -35,7 +35,9 @@ export const SelectedOutlineKeyboardControls: React.FC<{
readonly getLatestOutlineTargetByKey: (
key: string,
) => SelectedOutlineTarget | undefined;
- readonly getSelectableOutlines: () => readonly CanvasSelectableOutline[];
+ readonly getSelectableOutlines: () => ReturnType<
+ typeof getSequencesWithSelectableOutlines
+ >;
}> = ({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 c7a32a21d6..58cc3faf34 100644
--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx
+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx
@@ -1,4 +1,3 @@
-import {getCanvasSelectableOutlines} from '@remotion/canvas';
import {PlayerInternals} from '@remotion/player';
import React, {
useCallback,
@@ -39,7 +38,9 @@ import {
getSelectedSequenceKeys,
getSelectedTransformOriginInfo,
getSequenceKeysContainingSelection,
+ getSequencesWithSelectableOutlines,
} from './selected-outline-measurement';
+import {orderOutlinesForRendering} from './selected-outline-order';
import {
canEditSelectedOutlineCrop,
cropFieldKeys,
@@ -62,6 +63,8 @@ import {
} from './Timeline/TimelineSelection';
import {propStatusHas3DTransformValue} from './Timeline/transform-3d-mode';
+export {orderOutlinesForRendering};
+
export {
applySelectedOutlineDragAxisLock,
applySelectedOutlineTransformOriginAxisLock,
@@ -99,6 +102,8 @@ export {
getSelectedOutlineRotationDeltaDegrees,
getSelectedOutlineRotationPivot,
getSelectedSequenceKeys,
+ getSequencesWithSelectableOutlines,
+ getTransformedSvgViewportPoints,
} from './selected-outline-measurement';
export {selectedOutlineDragThresholdPx} from './selected-outline-types';
@@ -180,7 +185,7 @@ const getCropDragFields = ({
return fields as SelectedOutlineCropDragTarget['fields'];
};
-type SelectableOutlines = ReturnType<typeof getCanvasSelectableOutlines>;
+type SelectableOutlines = ReturnType<typeof getSequencesWithSelectableOutlines>;
type CalculateOutlineTargetsOptions = {
readonly mode: 'controls' | 'layout';
@@ -390,7 +395,6 @@ const calculateOutlineTargets = ({
key,
containsSelection,
crop,
- includeOutsideContainer: showSelectedOutline,
keyframeDisplayOffset: nodeKeyframeDisplayOffset,
nodePathInfo,
ref: sequence.refForOutline,
@@ -669,7 +673,7 @@ type ActiveSelectedOutlineOverlayProps = Omit<
) => SelectedOutlineTarget | undefined;
readonly getSelectableOutlines: (
timelinePosition: number,
- ) => ReturnType<typeof getCanvasSelectableOutlines>;
+ ) => ReturnType<typeof getSequencesWithSelectableOutlines>;
readonly hoveredTimelineNodePathKey: string | null;
readonly measureAllOutlines: boolean;
readonly onDraggingChange: (dragging: boolean) => void;
@@ -775,32 +779,28 @@ const ActiveSelectedOutlineOverlayUnmemoized: React.FC<
[outlineRuntimeControls, outlineRuntimeSnapshots],
);
- const outlineTargets = useMemo(() => {
- const targets = calculateOutlineTargetsForCurrentState({
- mode: 'layout',
- runtimeValuesByStore: outlineRuntimeValuesByStore,
- selectableOutlines: selectableOutlinesForLayout,
- targetKey: null,
- targetTimelinePosition: timelinePosition,
- });
- return targets.map((target) => {
- const hovered =
- timelineSequenceNodePathToKey(
- target.nodePathInfo.sequenceSubscriptionKey,
- ) === hoveredTimelineNodePathKey;
- return hovered && !target.includeOutsideContainer
- ? {...target, includeOutsideContainer: true}
- : target;
- });
- }, [
- calculateOutlineTargetsForCurrentState,
- hoveredTimelineNodePathKey,
- outlineRuntimeValuesByStore,
- selectableOutlinesForLayout,
- timelinePosition,
- ]);
+ const outlineTargets = useMemo(
+ () =>
+ calculateOutlineTargetsForCurrentState({
+ mode: 'layout',
+ runtimeValuesByStore: outlineRuntimeValuesByStore,
+ selectableOutlines: selectableOutlinesForLayout,
+ targetKey: null,
+ targetTimelinePosition: timelinePosition,
+ }),
+ [
+ calculateOutlineTargetsForCurrentState,
+ outlineRuntimeValuesByStore,
+ selectableOutlinesForLayout,
+ timelinePosition,
+ ],
+ );
+ const outlineTargetsRef =
+ useRef<readonly SelectedOutlineLayoutTarget[]>(outlineTargets);
+ const getOutlineTargets = useCallback(() => outlineTargetsRef.current, []);
useLayoutEffect(() => {
+ outlineTargetsRef.current = outlineTargets;
updateOutlinesRef.current();
}, [outlineTargets, scale, translationX, translationY]);
return (
@@ -809,7 +809,7 @@ const ActiveSelectedOutlineOverlayUnmemoized: React.FC<
compositionWidth={compositionWidth}
dragging={draggingOutline}
getLatestOutlineTargetByKey={getLatestOutlineTargetByKey}
- outlineTargets={outlineTargets}
+ getOutlineTargets={getOutlineTargets}
onDraggingChange={onDraggingChange}
onContextMenuOpenChange={onContextMenuOpenChange}
onSelect={onSelect}
@@ -891,7 +891,7 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
return [];
}
- return getCanvasSelectableOutlines({
+ return getSequencesWithSelectableOutlines({
sequences,
overrideIdsToNodePaths: overrideIdToNodePathMappings,
compositions,
@@ -956,7 +956,9 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
const getSelectableOutlinesRef = useRef(getSelectableOutlines);
const selectableOutlinesCacheRef = useRef<{
readonly getSelectableOutlines: typeof getSelectableOutlines;
- readonly selectableOutlines: ReturnType<typeof getCanvasSelectableOutlines>;
+ readonly selectableOutlines: ReturnType<
+ typeof getSequencesWithSelectableOutlines
+ >;
readonly timelinePosition: number;
} | null>(null);
useLayoutEffect(() => {
diff --git a/packages/studio/src/components/SelectedOutlineRenderer.tsx b/packages/studio/src/components/SelectedOutlineRenderer.tsx
index db7cb92d06..584ffbf58e 100644
--- a/packages/studio/src/components/SelectedOutlineRenderer.tsx
+++ b/packages/studio/src/components/SelectedOutlineRenderer.tsx
@@ -1,14 +1,10 @@
-import {
- orderCanvasOutlinesForRendering,
- useCanvasOutlines,
- useCanvasOutlinesController,
-} from '@remotion/canvas';
import React, {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
+ useState,
} from 'react';
import {timelineSequenceNodePathToKey} from '../helpers/timeline-node-path-key';
import {
@@ -17,6 +13,11 @@ import {
} from '../state/timeline-sequence-hover';
import {ContextMenuForTarget} from './ContextMenu';
import type {SelectedOutline} from './selected-outline-geometry';
+import {
+ measureOutlines,
+ outlinesAreEqual,
+} from './selected-outline-measurement';
+import {orderOutlinesForRendering} from './selected-outline-order';
import type {
SelectedOutlineContextMenuOpenHandler,
SelectedOutlineLayoutTarget,
@@ -46,6 +47,16 @@ const outlineContainer: React.CSSProperties = {
overflow: 'visible',
};
+type SelectedOutlineRenderState = {
+ readonly outlines: readonly SelectedOutline[];
+ readonly targets: readonly SelectedOutlineLayoutTarget[];
+};
+
+const emptyRenderState: SelectedOutlineRenderState = {
+ outlines: [],
+ targets: [],
+};
+
const SelectedOutlineRendererUnmemoized: React.FC<{
readonly compositionHeight: number;
readonly compositionWidth: number;
@@ -53,7 +64,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
readonly getLatestOutlineTargetByKey: (
key: string,
) => SelectedOutlineTarget | undefined;
- readonly outlineTargets: readonly SelectedOutlineLayoutTarget[];
+ readonly getOutlineTargets: () => readonly SelectedOutlineLayoutTarget[];
readonly onDraggingChange: (dragging: boolean) => void;
readonly onContextMenuOpenChange: (open: boolean) => void;
readonly onSelect: (
@@ -62,7 +73,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
) => void;
readonly scale: number;
readonly sequences: Parameters<
- typeof orderCanvasOutlinesForRendering
+ typeof orderOutlinesForRendering
>[0]['sequences'];
readonly updateOutlinesRef: React.MutableRefObject<() => void>;
}> = ({
@@ -70,7 +81,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
compositionWidth,
dragging,
getLatestOutlineTargetByKey,
- outlineTargets,
+ getOutlineTargets,
onDraggingChange,
onContextMenuOpenChange,
onSelect,
@@ -78,10 +89,12 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
sequences,
updateOutlinesRef,
}) => {
- const outlinesController =
- useCanvasOutlinesController<SelectedOutlineLayoutTarget>();
- const renderState = useCanvasOutlines(outlinesController);
+ const [renderState, setRenderState] =
+ useState<SelectedOutlineRenderState>(emptyRenderState);
const overlayRef = useRef<SVGSVGElement>(null);
+ const resizeObserverRef = useRef<ResizeObserver | null>(null);
+ const resizeObserverAnimationFrameRef = useRef<number | null>(null);
+ const observedOutlineElementsRef = useRef<ReadonlySet<Element>>(new Set());
const contextMenuOpenHandlersRef = useRef(
new Map<string, SelectedOutlineContextMenuOpenHandler>(),
);
@@ -120,10 +133,34 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
const hoveredSequence = useTimelineSequenceHoverState();
const setHoveredSequence = useSetTimelineSequenceHover();
const hoveredNodePathKey = hoveredSequence?.nodePathKey ?? null;
+ const hoveredTimelineNodePathKey =
+ hoveredSequence?.source === 'timeline' ? hoveredNodePathKey : null;
const updateOutlines = useCallback(() => {
- outlinesController.update(overlayRef.current, outlineTargets);
- }, [outlineTargets, outlinesController]);
+ const targets = getOutlineTargets();
+ if (overlayRef.current === null || targets.length === 0) {
+ setRenderState((prevState) =>
+ prevState.targets.length === 0 ? prevState : emptyRenderState,
+ );
+ return;
+ }
+
+ const nextOutlines = measureOutlines(
+ overlayRef.current,
+ targets,
+ hoveredTimelineNodePathKey,
+ );
+ setRenderState((prevState) => {
+ const outlines = outlinesAreEqual(prevState.outlines, nextOutlines)
+ ? prevState.outlines
+ : nextOutlines;
+ if (prevState.targets === targets && prevState.outlines === outlines) {
+ return prevState;
+ }
+
+ return {outlines, targets};
+ });
+ }, [getOutlineTargets, hoveredTimelineNodePathKey]);
useLayoutEffect(() => {
updateOutlinesRef.current = updateOutlines;
@@ -136,8 +173,65 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
}, [updateOutlines, updateOutlinesRef]);
useLayoutEffect(() => {
- return () => outlinesController.disconnect();
- }, [outlinesController]);
+ if (typeof ResizeObserver === 'undefined') {
+ return;
+ }
+
+ const resizeObserver = new ResizeObserver(() => {
+ if (resizeObserverAnimationFrameRef.current !== null) {
+ return;
+ }
+
+ resizeObserverAnimationFrameRef.current = requestAnimationFrame(() => {
+ resizeObserverAnimationFrameRef.current = null;
+ updateOutlinesRef.current();
+ });
+ });
+ resizeObserverRef.current = resizeObserver;
+
+ return () => {
+ if (resizeObserverAnimationFrameRef.current !== null) {
+ cancelAnimationFrame(resizeObserverAnimationFrameRef.current);
+ resizeObserverAnimationFrameRef.current = null;
+ }
+
+ resizeObserver.disconnect();
+ resizeObserverRef.current = null;
+ observedOutlineElementsRef.current = new Set();
+ };
+ }, [updateOutlinesRef]);
+
+ useLayoutEffect(() => {
+ const resizeObserver = resizeObserverRef.current;
+ if (resizeObserver === null) {
+ return;
+ }
+
+ const nextObservedElements = new Set<Element>();
+ if (overlayRef.current !== null) {
+ nextObservedElements.add(overlayRef.current);
+ }
+
+ for (const target of renderState.targets) {
+ if (target.ref.current !== null) {
+ nextObservedElements.add(target.ref.current);
+ }
+ }
+
+ for (const element of observedOutlineElementsRef.current) {
+ if (!nextObservedElements.has(element)) {
+ resizeObserver.unobserve(element);
+ }
+ }
+
+ for (const element of nextObservedElements) {
+ if (!observedOutlineElementsRef.current.has(element)) {
+ resizeObserver.observe(element);
+ }
+ }
+
+ observedOutlineElementsRef.current = nextObservedElements;
+ }, [renderState.targets]);
const targetsByKey = useMemo(() => {
return new Map(renderState.targets.map((target) => [target.key, target]));
@@ -156,7 +250,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
const outlineRenderingOrderRef = useRef<readonly string[]>([]);
const outlinesForRendering = useMemo(() => {
if (!dragging || outlineRenderingOrderRef.current.length === 0) {
- const orderedOutlines = orderCanvasOutlinesForRendering({
+ const orderedOutlines = orderOutlinesForRendering({
outlines: renderState.outlines,
sequences,
targetsByKey,
diff --git a/packages/studio/src/components/WebMcp.tsx b/packages/studio/src/components/WebMcp.tsx
index 5cc5312303..5132946e60 100644
--- a/packages/studio/src/components/WebMcp.tsx
+++ b/packages/studio/src/components/WebMcp.tsx
@@ -1,7 +1,3 @@
-import {
- getCanvasSelectableOutlines,
- measureCanvasOutlines,
-} from '@remotion/canvas';
import {useContext, useEffect, useMemo, useRef, type FC} from 'react';
import {Internals} from 'remotion';
import {pause} from '../api/pause';
@@ -31,6 +27,10 @@ import {persistMuteOption} from '../state/mute';
import {commonPlaybackRates, persistPlaybackRate} from '../state/playbackrate';
import {TimelineZoomCtx} from '../state/timeline-zoom';
import {useSelectComposition} from './InitialCompositionLoader';
+import {
+ getSequencesWithSelectableOutlines,
+ measureOutlineTargets,
+} from './selected-outline-measurement';
import {findTrackForNodePathInfo} from './Timeline/find-track-for-node-path-info';
import {getCurrentDuration, getCurrentFrame} from './Timeline/imperative-state';
import {parseKeyframeFieldFromNodePath} from './Timeline/parse-keyframe-field-from-node-path';
@@ -542,7 +542,7 @@ export const WebMcp: FC = () => {
}
const currentFrame = getCurrentFrame();
- const selectableOutlines = getCanvasSelectableOutlines({
+ const selectableOutlines = getSequencesWithSelectableOutlines({
sequences: sequencesRef.current,
overrideIdsToNodePaths: overrideIdToNodePathMappingsRef.current,
compositions: compositionsRef.current,
@@ -566,7 +566,7 @@ export const WebMcp: FC = () => {
throw new Error('The Studio canvas is not ready to be measured.');
}
- const measuredOutlines = measureCanvasOutlines(
+ const measuredOutlines = measureOutlineTargets(
portalNode,
selectableOutlines.map((outline) => {
if (outline.sequence.refForOutline === null) {
diff --git a/packages/studio/src/components/selected-outline-geometry.ts b/packages/studio/src/components/selected-outline-geometry.ts
index 42cb7114e2..504f10bec5 100644
--- a/packages/studio/src/components/selected-outline-geometry.ts
+++ b/packages/studio/src/components/selected-outline-geometry.ts
@@ -1,9 +1,40 @@
-export {
- clampCanvasOutlineValue as clamp,
- mixCanvasOutlinePoints as mixPoint,
- mixCanvasOutlineValues as mix,
-} from '@remotion/canvas';
-export type {
- CanvasOutline as SelectedOutline,
- CanvasOutlinePoint as OutlinePoint,
-} from '@remotion/canvas';
+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),
+ };
+};
diff --git a/packages/studio/src/components/selected-outline-measurement.ts b/packages/studio/src/components/selected-outline-measurement.ts
index 14856f55a7..04a06b83af 100644
--- a/packages/studio/src/components/selected-outline-measurement.ts
+++ b/packages/studio/src/components/selected-outline-measurement.ts
@@ -1,6 +1,15 @@
+import type {_InternalTypes, OverrideIdToNodePaths, TSequence} from 'remotion';
+import {calculateTimeline} from '../helpers/calculate-timeline';
import {BLACK, WHITE} from '../helpers/colors';
+import {useCache as resetBoxQuadsCache} from '../helpers/get-box-quads-polyfill-internals.js';
+import {getBoxQuadsPonyfill} from '../helpers/get-box-quads-ponyfill';
+import {timelineSequenceNodePathToKey} from '../helpers/timeline-node-path-key';
import type {OutlinePoint, SelectedOutline} from './selected-outline-geometry';
import {mixPoint} from './selected-outline-geometry';
+import type {
+ SelectedOutlineLayoutTarget,
+ SequenceWithSelectedOutline,
+} from './selected-outline-types';
import {
cropFieldKeys,
rotateFieldKey,
@@ -165,6 +174,184 @@ export const getSelectedOutlineRotationPivot = ({
return getUvHandlePosition(points, uv);
};
+const rectToPoints = (
+ elementRect: DOMRect,
+ containerRect: DOMRect,
+): SelectedOutline['points'] => {
+ const left = elementRect.left - containerRect.left;
+ const top = elementRect.top - containerRect.top;
+ const right = elementRect.right - containerRect.left;
+ const bottom = elementRect.bottom - containerRect.top;
+
+ return [
+ {x: left, y: top},
+ {x: right, y: top},
+ {x: right, y: bottom},
+ {x: left, y: bottom},
+ ];
+};
+
+type SvgViewport = {
+ readonly x: number;
+ readonly y: number;
+ readonly width: number;
+ readonly height: number;
+};
+
+type SvgScreenCtm = Pick<DOMMatrixReadOnly, 'a' | 'b' | 'c' | 'd' | 'e' | 'f'>;
+
+export const getTransformedSvgViewportPoints = ({
+ viewport,
+ ctm,
+ containerRect,
+}: {
+ readonly viewport: SvgViewport;
+ readonly ctm: SvgScreenCtm;
+ readonly containerRect: Pick<DOMRect, 'left' | 'top'>;
+}): SelectedOutline['points'] => {
+ const transformPoint = (x: number, y: number): OutlinePoint => ({
+ x: ctm.a * x + ctm.c * y + ctm.e - containerRect.left,
+ y: ctm.b * x + ctm.d * y + ctm.f - containerRect.top,
+ });
+
+ const left = viewport.x;
+ const top = viewport.y;
+ const right = viewport.x + viewport.width;
+ const bottom = viewport.y + viewport.height;
+
+ return [
+ transformPoint(left, top),
+ transformPoint(right, top),
+ transformPoint(right, bottom),
+ transformPoint(left, bottom),
+ ];
+};
+
+const quadToPoints = (
+ quad: DOMQuad,
+ containerRect: DOMRect,
+): SelectedOutline['points'] => {
+ // `getBoxQuads`/the ponyfill returns the quad in viewport coordinates.
+ // The overlay <svg> is unscaled (the canvas `scale()`/pan live on a sibling
+ // container, not the svg), so 1 user unit == 1 px and we only need to move
+ // the quad into the svg's local space by subtracting its viewport origin.
+ // We deliberately do not pass `relativeTo` to the ponyfill: when the target
+ // is not an ancestor of the element, the polyfill cannot resolve the
+ // coordinate space and leaves the quad in viewport coordinates.
+ return [
+ {x: quad.p1.x - containerRect.left, y: quad.p1.y - containerRect.top},
+ {x: quad.p2.x - containerRect.left, y: quad.p2.y - containerRect.top},
+ {x: quad.p3.x - containerRect.left, y: quad.p3.y - containerRect.top},
+ {x: quad.p4.x - containerRect.left, y: quad.p4.y - containerRect.top},
+ ];
+};
+
+const isSvgSvgElement = (element: Element): element is SVGSVGElement => {
+ const ownerSvgSvgElement = element.ownerDocument.defaultView?.SVGSVGElement;
+ return (
+ (typeof SVGSVGElement !== 'undefined' &&
+ element instanceof SVGSVGElement) ||
+ (ownerSvgSvgElement !== undefined && element instanceof ownerSvgSvgElement)
+ );
+};
+
+const getSvgSvgElementViewport = (element: SVGSVGElement): SvgViewport => {
+ const viewBox = element.viewBox.baseVal;
+ if (viewBox.width > 0 && viewBox.height > 0) {
+ return {
+ x: viewBox.x,
+ y: viewBox.y,
+ width: viewBox.width,
+ height: viewBox.height,
+ };
+ }
+
+ return {
+ x: 0,
+ y: 0,
+ width: element.width.baseVal.value,
+ height: element.height.baseVal.value,
+ };
+};
+
+const getSvgSvgElementOutlinePoints = (
+ element: SVGSVGElement,
+ containerRect: DOMRect,
+): SelectedOutline['points'] | null => {
+ const ctm = element.getScreenCTM();
+ const viewport = getSvgSvgElementViewport(element);
+ if (ctm === null || (viewport.width === 0 && viewport.height === 0)) {
+ return null;
+ }
+
+ return getTransformedSvgViewportPoints({
+ viewport,
+ ctm,
+ containerRect,
+ });
+};
+
+const getElementOutlinePoints = (
+ element: Element,
+ containerRect: DOMRect,
+ includeOutsideContainer: boolean,
+): SelectedOutline['points'] | null => {
+ const elementRect = element.getBoundingClientRect();
+
+ if (elementRect.width === 0 && elementRect.height === 0) {
+ return null;
+ }
+
+ if (
+ !includeOutsideContainer &&
+ (elementRect.right <= containerRect.left ||
+ elementRect.left >= containerRect.right ||
+ elementRect.bottom <= containerRect.top ||
+ elementRect.top >= containerRect.bottom)
+ ) {
+ return null;
+ }
+
+ if (isSvgSvgElement(element)) {
+ return getSvgSvgElementOutlinePoints(element, containerRect);
+ }
+
+ const quads = getBoxQuadsPonyfill(element, {
+ box: 'border',
+ });
+ const quad = quads?.[0];
+ if (!quad) {
+ return rectToPoints(elementRect, containerRect);
+ }
+
+ return quadToPoints(quad, containerRect);
+};
+
+export const cropOutlinePoints = (
+ points: SelectedOutline['points'],
+ crop: SelectedOutlineLayoutTarget['crop'],
+): SelectedOutline['points'] => {
+ if (
+ crop.left === 0 &&
+ crop.right === 0 &&
+ crop.top === 0 &&
+ crop.bottom === 0
+ ) {
+ return points;
+ }
+
+ const {left, top} = crop;
+ const right = 1 - crop.right;
+ const bottom = 1 - crop.bottom;
+
+ return [
+ getUvHandlePosition(points, [left, top]),
+ getUvHandlePosition(points, [right, top]),
+ getUvHandlePosition(points, [right, bottom]),
+ getUvHandlePosition(points, [left, bottom]),
+ ];
+};
+
export const getSelectedSequenceKeys = (
selectedItems: readonly TimelineSelection[],
): Set<string> => {
@@ -409,3 +596,171 @@ 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,
+ };
+ });
+};
+
+export const measureOutlines = (
+ container: SVGSVGElement,
+ targets: readonly SelectedOutlineLayoutTarget[],
+ hoveredTimelineNodePathKey: string | null,
+): SelectedOutline[] => {
+ return measureOutlineTargets(
+ container,
+ targets.map((target) => ({
+ crop: target.crop,
+ includeOutsideContainer:
+ target.showSelectedOutline ||
+ timelineSequenceNodePathToKey(
+ target.nodePathInfo.sequenceSubscriptionKey,
+ ) === hoveredTimelineNodePathKey,
+ key: target.key,
+ ref: target.ref,
+ })),
+ );
+};
+
+export const measureOutlineTargets = (
+ container: Element,
+ targets: readonly {
+ readonly crop: SelectedOutlineLayoutTarget['crop'];
+ readonly includeOutsideContainer: boolean;
+ readonly key: string;
+ readonly ref: React.RefObject<Element | null>;
+ }[],
+): SelectedOutline[] => {
+ // Reuse shared ancestor geometry within this synchronous batch only.
+ resetBoxQuadsCache();
+ const containerRect = container.getBoundingClientRect();
+ const outlines: SelectedOutline[] = [];
+
+ for (const target of targets) {
+ const element = target.ref.current;
+ if (element === null) {
+ continue;
+ }
+
+ const uncroppedPoints = getElementOutlinePoints(
+ element,
+ containerRect,
+ target.includeOutsideContainer,
+ );
+ if (uncroppedPoints === null) {
+ continue;
+ }
+
+ const points = cropOutlinePoints(uncroppedPoints, target.crop);
+
+ outlines.push({
+ key: target.key,
+ dimensions:
+ element instanceof HTMLElement
+ ? {
+ width: element.offsetWidth,
+ height: element.offsetHeight,
+ }
+ : element instanceof SVGSVGElement
+ ? {
+ width: element.width.baseVal.value,
+ height: element.height.baseVal.value,
+ }
+ : null,
+ uncroppedPoints,
+ points,
+ });
+ }
+
+ return outlines;
+};
+
+export const outlinesAreEqual = (
+ a: readonly SelectedOutline[],
+ b: readonly SelectedOutline[],
+): boolean => {
+ if (a.length !== b.length) {
+ return false;
+ }
+
+ for (let i = 0; i < a.length; i++) {
+ if (a[i].key !== b[i].key) {
+ return false;
+ }
+
+ if (
+ a[i].dimensions?.width !== b[i].dimensions?.width ||
+ a[i].dimensions?.height !== b[i].dimensions?.height
+ ) {
+ return false;
+ }
+
+ const aUncropped = a[i].uncroppedPoints;
+ const bUncropped = b[i].uncroppedPoints;
+ if ((aUncropped === null) !== (bUncropped === null)) {
+ return false;
+ }
+
+ if (aUncropped !== null && bUncropped !== null) {
+ for (let j = 0; j < aUncropped.length; j++) {
+ if (
+ Math.abs(aUncropped[j].x - bUncropped[j].x) > 0.01 ||
+ Math.abs(aUncropped[j].y - bUncropped[j].y) > 0.01
+ ) {
+ return false;
+ }
+ }
+ }
+
+ for (let j = 0; j < a[i].points.length; j++) {
+ if (
+ Math.abs(a[i].points[j].x - b[i].points[j].x) > 0.01 ||
+ Math.abs(a[i].points[j].y - b[i].points[j].y) > 0.01
+ ) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+};
diff --git a/packages/canvas/src/order-outlines-for-rendering.ts b/packages/studio/src/components/selected-outline-order.ts
similarity index 95%
rename from packages/canvas/src/order-outlines-for-rendering.ts
rename to packages/studio/src/components/selected-outline-order.ts
index 1f1a27e73c..fc97d4869c 100644
--- a/packages/canvas/src/order-outlines-for-rendering.ts
+++ b/packages/studio/src/components/selected-outline-order.ts
@@ -1,17 +1,5 @@
-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;
+import type {SelectedOutline} from './selected-outline-geometry';
+import type {SelectedOutlineLayoutTarget} from './selected-outline-types';
const outlinePointEqualityTolerance = 0.5;
const outlineAreaEqualityTolerance = 0.5;
@@ -365,7 +353,7 @@ const orderOutlineGroup = ({
return ordered;
};
-export const orderCanvasOutlinesForRendering = ({
+export const orderOutlinesForRendering = ({
outlines,
sequences,
targetsByKey,
diff --git a/packages/studio/src/components/selected-outline-types.ts b/packages/studio/src/components/selected-outline-types.ts
index e3cd213b4b..ccf1815b56 100644
--- a/packages/studio/src/components/selected-outline-types.ts
+++ b/packages/studio/src/components/selected-outline-types.ts
@@ -1,4 +1,3 @@
-import type {CanvasOutlineTarget} from '@remotion/canvas';
import type {
CanUpdateSequencePropStatus,
CanUpdateSequencePropStatusKeyframed,
@@ -20,11 +19,12 @@ export type SelectedOutlineContextMenuOpenHandler = () =>
| SelectedOutlineContextMenuOpenResult
| Promise<SelectedOutlineContextMenuOpenResult>;
-export type SelectedOutlineLayoutTarget = CanvasOutlineTarget & {
+export type SelectedOutlineLayoutTarget = {
readonly key: string;
readonly containsSelection: boolean;
readonly keyframeDisplayOffset: number;
readonly nodePathInfo: SequenceNodePathInfo;
+ readonly ref: React.RefObject<Element | null>;
readonly selected: boolean;
readonly selectedForCrop: boolean;
readonly selectedForRotation: boolean;
@@ -34,6 +34,12 @@ export type SelectedOutlineLayoutTarget = CanvasOutlineTarget & {
readonly transformOriginValue: string;
readonly selection: TimelineSelection;
readonly sequence: TSequence;
+ readonly crop: {
+ readonly left: number;
+ readonly right: number;
+ readonly top: number;
+ readonly bottom: number;
+ };
};
export type SelectedOutlineTarget = SelectedOutlineLayoutTarget & {
@@ -218,6 +224,14 @@ 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 8c7b832040..64a6dabc4b 100644
--- a/packages/studio/src/components/selected-outline-uv.ts
+++ b/packages/studio/src/components/selected-outline-uv.ts
@@ -1,7 +1,3 @@
-import {
- getCanvasOutlinePointAtUv,
- getCanvasOutlineUvForPoint,
-} from '@remotion/canvas';
import type {
CanUpdateSequencePropStatusKeyframed,
CanUpdateSequencePropStatusStatic,
@@ -15,6 +11,8 @@ import type {
import {Internals} from 'remotion';
import {
clamp,
+ mix,
+ mixPoint,
type OutlinePoint,
type SelectedOutline,
} from './selected-outline-geometry';
@@ -272,10 +270,84 @@ 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 => getCanvasOutlinePointAtUv(points, uv);
+): OutlinePoint => {
+ const transform = getProjectiveTransform(points);
+ return transform === null
+ ? getBilinearUvHandlePosition(points, uv)
+ : applyProjectiveTransform(transform, uv);
+};
export const getUvHandleConnectionLines = ({
handles,
@@ -534,10 +606,80 @@ 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 => getCanvasOutlineUvForPoint(points, point);
+): 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,
+ ];
+};
export function constrainUv(
value: UvCoordinate,
diff --git a/packages/canvas/src/get-box-quads-polyfill-internals.js b/packages/studio/src/helpers/get-box-quads-polyfill-internals.js
similarity index 100%
rename from packages/canvas/src/get-box-quads-polyfill-internals.js
rename to packages/studio/src/helpers/get-box-quads-polyfill-internals.js
diff --git a/packages/canvas/src/get-box-quads-ponyfill.ts b/packages/studio/src/helpers/get-box-quads-ponyfill.ts
similarity index 100%
rename from packages/canvas/src/get-box-quads-ponyfill.ts
rename to packages/studio/src/helpers/get-box-quads-ponyfill.ts
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 5bae130c3e..2b3caad683 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -1,11 +1,4 @@
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,
@@ -19,7 +12,9 @@ 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';
@@ -66,7 +61,10 @@ import {
getSelectedOutlineTransformOriginDragChanges,
getSelectedOutlineTransformOriginLockedAxis,
getSelectedSequenceKeys,
+ getSequencesWithSelectableOutlines,
+ getTransformedSvgViewportPoints,
isSelectedOutlineDragPastThreshold,
+ orderOutlinesForRendering,
selectedOutlineDragThresholdPx,
selectedOutlineTransformOriginSnapThresholdPx,
selectedOutlineUvSnapThresholdPx,