🎥 Make videos programmatically with React
@remotion/canvas: Add outline calculation interface
Jonny Burgerd3bc944e291a
diff --git a/packages/canvas/src/canvas-controller.ts b/packages/canvas/src/canvas-controller.tsindex 22d8a6fa26..703dff6f8d 100644--- a/packages/canvas/src/canvas-controller.ts+++ b/packages/canvas/src/canvas-controller.ts@@ -2,6 +2,10 @@ 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,@@ -12,6 +16,7 @@ export type CanvasController = {readonly getSnapshot: () => readonly TimelineTrackData[];readonly subscribe: (listener: () => void) => () => void;};+ readonly outlines: CanvasOutlinesController;readonly selection: CanvasSelectionController;};@@ -46,6 +51,7 @@ export const createCanvasController = (): CanvasController => {return () => timelineListeners.delete(listener);},},+ outlines: createCanvasOutlinesController(),selection: createCanvasSelectionController(),};@@ -58,7 +64,10 @@ export const createCanvasController = (): CanvasController => {}),);},- clear: () => updateTimelineSnapshot([]),+ clear: () => {+ controller.outlines.disconnect();+ updateTimelineSnapshot([]);+ },});return controller;diff --git a/packages/studio/src/helpers/get-box-quads-polyfill-internals.js b/packages/canvas/src/get-box-quads-polyfill-internals.jssimilarity index 100%rename from packages/studio/src/helpers/get-box-quads-polyfill-internals.jsrename to packages/canvas/src/get-box-quads-polyfill-internals.jsdiff --git a/packages/studio/src/helpers/get-box-quads-ponyfill.ts b/packages/canvas/src/get-box-quads-ponyfill.tssimilarity index 100%rename from packages/studio/src/helpers/get-box-quads-ponyfill.tsrename to packages/canvas/src/get-box-quads-ponyfill.tsdiff --git a/packages/canvas/src/index.ts b/packages/canvas/src/index.tsindex e4b4b2edd9..48c8466d4a 100644--- a/packages/canvas/src/index.ts+++ b/packages/canvas/src/index.ts@@ -17,6 +17,23 @@ export type {TimelineTrackData,TimelineTrackWithOriginalTimings,} from './get-timeline-sequence-sort-key';+export {+ canvasOutlinesAreEqual,+ createCanvasOutlinesController,+ cropCanvasOutlinePoints,+ getCanvasOutlinePointAtUv,+ getTransformedSvgViewportPoints,+ measureCanvasOutlines,+ useCanvasOutlines,+ useCanvasOutlinesController,+} from './outlines';+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.tsnew file mode 100644index 0000000000..b0667e5917--- /dev/null+++ b/packages/canvas/src/outlines.ts@@ -0,0 +1,542 @@+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);+};++const mixPoint = (+ from: CanvasOutlinePoint,+ to: CanvasOutlinePoint,+ progress: number,+): CanvasOutlinePoint => ({+ x: from.x + (to.x - from.x) * progress,+ y: from.y + (to.y - from.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 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) > 0.000001 || Math.abs(dy3) > 0.000001) {+ const determinant = dx1 * dy2 - dx2 * dy1;+ if (Math.abs(determinant) < 0.000001) {+ 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 mixPoint(mixPoint(tl, tr, uv[0]), mixPoint(bl, br, uv[0]), uv[1]);+};++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: SVGSVGElement,+ 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.tsnew file mode 100644index 0000000000..c000e70825--- /dev/null+++ b/packages/canvas/src/test/outlines.test.ts@@ -0,0 +1,102 @@+import {expect, test} from 'bun:test';+import {+ createCanvasOutlinesController,+ type CanvasOutlineTarget,+} from '../index';++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.jsonindex 7034960cb3..a32620f568 100644--- a/packages/canvas/tsconfig.json+++ b/packages/canvas/tsconfig.json@@ -7,6 +7,7 @@"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.tsxindex e8bb871d46..8fed119129 100644--- a/packages/player-example/pages/canvas.tsx+++ b/packages/player-example/pages/canvas.tsx@@ -2,13 +2,19 @@ import {Canvas,getCanvasSelectionItemKey,useCanvasController,+ useCanvasOutlines,useCanvasSelection,type CanvasSelectionItem,type SequenceNodePathInfo,type TimelineTrackData,} from '@remotion/canvas';-import React, {useSyncExternalStore} from 'react';-import {AbsoluteFill, Sequence, useCurrentFrame} from 'remotion';+import React, {+ useLayoutEffect,+ useMemo,+ useRef,+ useSyncExternalStore,+} from 'react';+import {AbsoluteFill, Interactive, Sequence, useCurrentFrame} from 'remotion';const CanvasComposition: React.FC = () => {const frame = useCurrentFrame();@@ -31,9 +37,40 @@ 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}}>Canvas</h1>+ <h1+ style={{+ fontSize: 88,+ transform: `translateX(${Math.sin(frame / 12) * 80}px) rotate(${+ Math.sin(frame / 18) * 8+ }deg)`,+ }}+ >+ Canvas+ </h1></Sequence>) : null}{frame >= 60 ? (@@ -73,13 +110,45 @@ const CanvasPage: React.FC = () => {controller.timeline.getSnapshot,);const selection = useCanvasSelection(controller.selection);- const selectedKeys = new Set(- selection.selectedItems.map(getCanvasSelectionItemKey),+ const outlineSnapshot = useCanvasOutlines(controller.outlines);+ const outlineOverlayRef = useRef<SVGSVGElement>(null);+ const selectedKeys = useMemo(+ () => new Set(selection.selectedItems.map(getCanvasSelectionItemKey)),+ [selection.selectedItems],);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@@ -103,17 +172,43 @@ const CanvasPage: React.FC = () => {gridTemplateColumns: 'minmax(0, 2fr) minmax(240px, 1fr)',}}>- <Canvas- controller={controller}- component={CanvasComposition}- compositionWidth={1280}- compositionHeight={720}- durationInFrames={180}- fps={30}- controls- acknowledgeRemotionLicense- style={{width: '100%'}}- />+ <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><div><sectionstyle={{@@ -248,6 +343,8 @@ const CanvasPage: React.FC = () => {Click a layer to replace the selection. Shift-click selects acontiguous range; Command/Ctrl-click toggles a layer. Property andeasing 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 selectiondiff --git a/packages/studio/src/components/SelectedOutlineOverlay.tsx b/packages/studio/src/components/SelectedOutlineOverlay.tsxindex 58cc3faf34..506bbccc47 100644--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx@@ -395,6 +395,7 @@ const calculateOutlineTargets = ({key,containsSelection,crop,+ includeOutsideContainer: showSelectedOutline,keyframeDisplayOffset: nodeKeyframeDisplayOffset,nodePathInfo,ref: sequence.refForOutline,@@ -779,28 +780,32 @@ const ActiveSelectedOutlineOverlayUnmemoized: React.FC<[outlineRuntimeControls, outlineRuntimeSnapshots],);- const outlineTargets = useMemo(- () =>- calculateOutlineTargetsForCurrentState({- mode: 'layout',- runtimeValuesByStore: outlineRuntimeValuesByStore,- selectableOutlines: selectableOutlinesForLayout,- targetKey: null,- targetTimelinePosition: timelinePosition,- }),- [- calculateOutlineTargetsForCurrentState,- outlineRuntimeValuesByStore,- selectableOutlinesForLayout,- timelinePosition,- ],- );+ 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 outlineTargetsRef =- useRef<readonly SelectedOutlineLayoutTarget[]>(outlineTargets);- const getOutlineTargets = useCallback(() => outlineTargetsRef.current, []);useLayoutEffect(() => {- outlineTargetsRef.current = outlineTargets;updateOutlinesRef.current();}, [outlineTargets, scale, translationX, translationY]);return (@@ -809,7 +814,7 @@ const ActiveSelectedOutlineOverlayUnmemoized: React.FC<compositionWidth={compositionWidth}dragging={draggingOutline}getLatestOutlineTargetByKey={getLatestOutlineTargetByKey}- getOutlineTargets={getOutlineTargets}+ outlineTargets={outlineTargets}onDraggingChange={onDraggingChange}onContextMenuOpenChange={onContextMenuOpenChange}onSelect={onSelect}diff --git a/packages/studio/src/components/SelectedOutlineRenderer.tsx b/packages/studio/src/components/SelectedOutlineRenderer.tsxindex 584ffbf58e..0b35b79d56 100644--- a/packages/studio/src/components/SelectedOutlineRenderer.tsx+++ b/packages/studio/src/components/SelectedOutlineRenderer.tsx@@ -1,10 +1,10 @@+import {useCanvasOutlines, useCanvasOutlinesController} from '@remotion/canvas';import React, {useCallback,useEffect,useLayoutEffect,useMemo,useRef,- useState,} from 'react';import {timelineSequenceNodePathToKey} from '../helpers/timeline-node-path-key';import {@@ -13,10 +13,6 @@ 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,@@ -47,16 +43,6 @@ 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;@@ -64,7 +50,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{readonly getLatestOutlineTargetByKey: (key: string,) => SelectedOutlineTarget | undefined;- readonly getOutlineTargets: () => readonly SelectedOutlineLayoutTarget[];+ readonly outlineTargets: readonly SelectedOutlineLayoutTarget[];readonly onDraggingChange: (dragging: boolean) => void;readonly onContextMenuOpenChange: (open: boolean) => void;readonly onSelect: (@@ -81,7 +67,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{compositionWidth,dragging,getLatestOutlineTargetByKey,- getOutlineTargets,+ outlineTargets,onDraggingChange,onContextMenuOpenChange,onSelect,@@ -89,12 +75,10 @@ const SelectedOutlineRendererUnmemoized: React.FC<{sequences,updateOutlinesRef,}) => {- const [renderState, setRenderState] =- useState<SelectedOutlineRenderState>(emptyRenderState);+ const outlinesController =+ useCanvasOutlinesController<SelectedOutlineLayoutTarget>();+ const renderState = useCanvasOutlines(outlinesController);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>(),);@@ -133,34 +117,10 @@ 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(() => {- 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]);+ outlinesController.update(overlayRef.current, outlineTargets);+ }, [outlineTargets, outlinesController]);useLayoutEffect(() => {updateOutlinesRef.current = updateOutlines;@@ -173,65 +133,8 @@ const SelectedOutlineRendererUnmemoized: React.FC<{}, [updateOutlines, updateOutlinesRef]);useLayoutEffect(() => {- 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]);+ return () => outlinesController.disconnect();+ }, [outlinesController]);const targetsByKey = useMemo(() => {return new Map(renderState.targets.map((target) => [target.key, target]));diff --git a/packages/studio/src/components/selected-outline-measurement.ts b/packages/studio/src/components/selected-outline-measurement.tsindex 2aea1b6858..494d5dbecc 100644--- a/packages/studio/src/components/selected-outline-measurement.ts+++ b/packages/studio/src/components/selected-outline-measurement.ts@@ -1,15 +1,13 @@+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 {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 type {SequenceWithSelectedOutline} from './selected-outline-types';import {cropFieldKeys,rotateFieldKey,@@ -174,184 +172,6 @@ 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> => {@@ -641,107 +461,3 @@ export const getSequencesWithSelectableOutlines = ({};});};--export const measureOutlines = (- container: SVGSVGElement,- targets: readonly SelectedOutlineLayoutTarget[],- hoveredTimelineNodePathKey: string | 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 includeOutsideContainer =- target.showSelectedOutline ||- timelineSequenceNodePathToKey(- target.nodePathInfo.sequenceSubscriptionKey,- ) === hoveredTimelineNodePathKey;- const uncroppedPoints = getElementOutlinePoints(- element,- containerRect,- 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/studio/src/components/selected-outline-types.ts b/packages/studio/src/components/selected-outline-types.tsindex ccf1815b56..872f6db429 100644--- a/packages/studio/src/components/selected-outline-types.ts+++ b/packages/studio/src/components/selected-outline-types.ts@@ -1,3 +1,4 @@+import type {CanvasOutlineTarget} from '@remotion/canvas';import type {CanUpdateSequencePropStatus,CanUpdateSequencePropStatusKeyframed,@@ -19,12 +20,11 @@ export type SelectedOutlineContextMenuOpenHandler = () =>| SelectedOutlineContextMenuOpenResult| Promise<SelectedOutlineContextMenuOpenResult>;-export type SelectedOutlineLayoutTarget = {+export type SelectedOutlineLayoutTarget = CanvasOutlineTarget & {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,12 +34,6 @@ export type SelectedOutlineLayoutTarget = {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 & {