🎥 Make videos programmatically with React
`@remotion/canvas`: Add `showOutlines` for hover and selection
Jonny Burgerd69afd02b21e
diff --git a/packages/canvas/README.md b/packages/canvas/README.mdindex a3a589facb..9d32df27fd 100644--- a/packages/canvas/README.md+++ b/packages/canvas/README.md@@ -4,4 +4,49 @@ Headless primitives for Remotion authoring interfaces## Usage-This is an internal package and has no documentation.+`Canvas` accepts the Player props plus a controller. Outlines are opt-in:++```tsx+const controller = useCanvasController();++<Canvas+ controller={controller}+ showOutlines+ component={Video}+ compositionWidth={1920}+ compositionHeight={1080}+ durationInFrames={150}+ fps={30}+/>;+```++In outline mode, the composition handles hover and click selection. Shift-click+selects a range, Cmd/Ctrl-click toggles an item, and clicking empty space or+pressing Escape on the canvas clears selection. Player controls remain usable.+Set `showOutlines={false}` to interact with the composition's own controls.++Subscribe to `controller.timeline` for registered layers and use+`useCanvasSelection(controller.selection)` for selection. Use+`getCanvasSequenceNodePathInfo(track)` to construct each layer's selection item+and `useCanvasSequenceHover(controller.hover, nodePathInfo, 'timeline')` for+bidirectional hover highlighting. The hook returns `hovered`, `onPointerEnter`,+and `onPointerLeave`; each layer subscribes only to its own hover state.++If the host resolves source identities, pass `resolveSequenceNodePathInfo` to+`Canvas` and use the same resolver in the layer list. Return `null` for layers+that should not be selectable. The default uses an existing source identity or+falls back to the mounted sequence ID; that fallback is not a persistent source+reference across remounts. Outlines require a registered DOM ref, so a+`Sequence` with `layout="none"` has no outline of its own.++For custom authoring UIs, `createCanvasHoverController()`,+`measureCanvasOutlineTargets()`, `useCanvasOutlineMeasurements()`, and+`orderCanvasOutlinesForRendering()` are independent primitives also consumed by+Studio. Measurements include transformed HTML/SVG bounds and cropping, in+unscaled overlay pixels. The overlay must share the elements' document and+must not have a CSS transform or SVG viewBox scaling. The measurement hook+observes element sizes; invoke its `updateOutlinesRef` after position, transform,+frame, or viewport changes. Keep frame subscriptions in the active overlay and+omit inactive targets to avoid unnecessary layout reads.++Source writes and transform editing handles remain the host's responsibility.diff --git a/packages/canvas/src/canvas-controller.ts b/packages/canvas/src/canvas-controller.tsindex 22d8a6fa26..d127c36cc8 100644--- a/packages/canvas/src/canvas-controller.ts+++ b/packages/canvas/src/canvas-controller.ts@@ -2,6 +2,7 @@ import {useState} from 'react';import type {TSequence} from 'remotion';import {calculateTimeline} from './calculate-timeline';import type {TimelineTrackData} from './get-timeline-sequence-sort-key';+import {createCanvasHoverController, type CanvasHoverController} from './hover';import {createCanvasSelectionController,type CanvasSelectionController,@@ -13,6 +14,7 @@ export type CanvasController = {readonly subscribe: (listener: () => void) => () => void;};readonly selection: CanvasSelectionController;+ readonly hover: CanvasHoverController;};type CanvasControllerInternals = {@@ -47,6 +49,7 @@ export const createCanvasController = (): CanvasController => {},},selection: createCanvasSelectionController(),+ hover: createCanvasHoverController(),};controllerInternals.set(controller, {@@ -58,7 +61,10 @@ export const createCanvasController = (): CanvasController => {}),);},- clear: () => updateTimelineSnapshot([]),+ clear: () => {+ updateTimelineSnapshot([]);+ controller.hover.clear(null);+ },});return controller;diff --git a/packages/canvas/src/canvas-outline-overlay.tsx b/packages/canvas/src/canvas-outline-overlay.tsxnew file mode 100644index 0000000000..f7f0ce83a5--- /dev/null+++ b/packages/canvas/src/canvas-outline-overlay.tsx@@ -0,0 +1,380 @@+import type {RefObject} from 'react';+import React, {useEffect, useCallback, useMemo, useRef, useState} from 'react';+import {Internals} from 'remotion';+import type {CanvasController} from './canvas-controller';+import type {+ SequenceNodePathInfo,+ TimelineTrackData,+} from './get-timeline-sequence-sort-key';+import {useCanvasHover} from './hover';+import type {CanvasOutlineTarget} from './outline-geometry';+import {orderCanvasOutlinesForRendering} from './outline-order';+import {+ getCanvasSequenceSelectionKey,+ useCanvasSelection,+ type CanvasSelectionItem,+} from './selection';+import type {CanvasSequenceNodePathResolver} from './sequence-node-path';+import {timelineSequenceNodePathToKey} from './timeline-sequence-node-path-to-key';+import {useCanvasOutlineMeasurements} from './use-canvas-outline-measurements';+import {useCanvasRuntimeValueSnapshots} from './use-runtime-value-snapshots';+import {useSyncExternalStore} from './use-sync-external-store';++type OutlineLayer = {+ readonly track: TimelineTrackData;+ readonly key: string;+ readonly nodePathKey: string;+ readonly nodePathInfo: SequenceNodePathInfo;+ readonly selection: CanvasSelectionItem;+};++const overlayStyle: React.CSSProperties = {+ position: 'absolute',+ inset: 0,+ overflow: 'hidden',+ touchAction: 'none',+ outline: 'none',+};++// Only this leaf subscribes to the frame. An idle, unselected Canvas does not+// measure its composition or render its layer list on every playback frame.+const ActiveCanvasOutlines = React.memo(+ ({+ controller,+ containerRef,+ layers,+ measureAll,+ }: {+ readonly controller: CanvasController;+ readonly containerRef: RefObject<SVGSVGElement | null>;+ readonly layers: readonly OutlineLayer[];+ readonly measureAll: boolean;+ }) => {+ const frame = Internals.Timeline.useTimelinePosition();+ const selection = useCanvasSelection(controller.selection);+ const hover = useCanvasHover(controller.hover);+ const selectedSourceKeys = useMemo(+ () =>+ new Set(+ selection.selectedItems.flatMap((item) =>+ item.type === 'guide'+ ? []+ : [+ timelineSequenceNodePathToKey(+ item.nodePathInfo.sequenceSubscriptionKey,+ ),+ ],+ ),+ ),+ [selection.selectedItems],+ );+ const selectedKeys = useMemo(+ () =>+ new Set(+ selection.selectedItems.flatMap((item) =>+ item.type === 'guide'+ ? []+ : [getCanvasSequenceSelectionKey(item.nodePathInfo)],+ ),+ ),+ [selection.selectedItems],+ );+ const activeLayers = useMemo(+ () =>+ layers.filter(+ ({track, nodePathKey}) =>+ track.sequence.refForOutline !== null &&+ track.sequence.showInTimeline &&+ (measureAll ||+ selectedSourceKeys.has(nodePathKey) ||+ hover?.nodePathKey === nodePathKey),+ ),+ [hover?.nodePathKey, layers, measureAll, selectedSourceKeys],+ );+ const runtimeControls = useMemo(+ () =>+ activeLayers.flatMap(({track}) =>+ track.sequence.controls ? [track.sequence.controls] : [],+ ),+ [activeLayers],+ );+ const runtimeSnapshots = useCanvasRuntimeValueSnapshots(runtimeControls);+ const valuesByStore = useMemo(+ () =>+ new Map(+ runtimeControls.map((controls, index) => [+ controls.runtimeValues,+ runtimeSnapshots[index],+ ]),+ ),+ [runtimeControls, runtimeSnapshots],+ );+ const visibleLayers = useMemo(+ () =>+ activeLayers.filter(+ ({track: {sequence}}) =>+ frame >= sequence.from && frame < sequence.from + sequence.duration,+ ),+ [activeLayers, frame],+ );+ const targets = useMemo(+ () =>+ visibleLayers.map((layer) => {+ const {sequence} = layer.track;+ const values = sequence.controls+ ? (valuesByStore.get(sequence.controls.runtimeValues) ?? {})+ : {};+ const cropValue = (key: string) =>+ typeof values[key] === 'number' && Number.isFinite(values[key])+ ? values[key]+ : 0;+ return {+ ...layer,+ sequence,+ ref: sequence.refForOutline!,+ crop: Internals.resolveSequenceCrop({+ cropLeft: cropValue('cropLeft'),+ cropRight: cropValue('cropRight'),+ cropTop: cropValue('cropTop'),+ cropBottom: cropValue('cropBottom'),+ }),+ selected: selectedKeys.has(layer.key),+ containsSelection: selectedKeys.has(layer.key),+ includeOutsideContainer:+ selectedSourceKeys.has(layer.nodePathKey) ||+ hover?.nodePathKey === layer.nodePathKey,+ };+ }),+ [+ hover?.nodePathKey,+ valuesByStore,+ selectedKeys,+ selectedSourceKeys,+ visibleLayers,+ ],+ );+ const outlines = useCanvasOutlineMeasurements({+ containerRef,+ targets: targets satisfies readonly CanvasOutlineTarget[],+ updateOutlinesRef: null,+ });+ const targetsByKey = useMemo(+ () => new Map(targets.map((target) => [target.key, target])),+ [targets],+ );+ const sequences = useMemo(+ () => layers.map(({track}) => track.sequence),+ [layers],+ );+ const orderedOutlines = useMemo(+ () =>+ orderCanvasOutlinesForRendering({+ outlines,+ sequences,+ targetsByKey,+ }),+ [outlines, sequences, targetsByKey],+ );+ const selectableItems = useMemo(+ () => layers.map((layer) => layer.selection),+ [layers],+ );+ useEffect(() => {+ const current = controller.hover.getSnapshot();+ if (+ current?.source === 'canvas' &&+ !outlines.some((outline) => outline.key === current.key)+ ) {+ controller.hover.clear('canvas');+ }+ }, [controller.hover, outlines]);++ return (+ <>+ {orderedOutlines.map((outline) => {+ const target = targetsByKey.get(outline.key);+ if (!target) {+ return null;+ }++ const visible =+ selectedSourceKeys.has(target.nodePathKey) ||+ hover?.nodePathKey === target.nodePathKey;+ return (+ <polygon+ key={outline.key}+ data-remotion-canvas-outline-key={outline.key}+ data-selected={target.selected}+ points={outline.points+ .map((point) => `${point.x},${point.y}`)+ .join(' ')}+ fill="transparent"+ stroke="#0b84f3"+ strokeWidth={2}+ strokeOpacity={visible ? 1 : 0}+ vectorEffect="non-scaling-stroke"+ pointerEvents="all"+ onPointerEnter={() =>+ controller.hover.setHoveredSequence({+ key: target.key,+ nodePathKey: target.nodePathKey,+ source: 'canvas',+ })+ }+ onPointerLeave={() =>+ controller.hover.setHoveredSequence((current) =>+ current?.source === 'canvas' && current.key === target.key+ ? null+ : current,+ )+ }+ onPointerDown={(event) => {+ if (event.button !== 0) {+ return;+ }++ event.preventDefault();+ event.stopPropagation();+ containerRef.current?.focus({preventScroll: true});+ controller.selection.select(+ target.selection,+ {+ shiftKey: event.shiftKey,+ toggleKey: event.metaKey || event.ctrlKey,+ },+ selectableItems,+ );+ }}+ />+ );+ })}+ </>+ );+ },+);++export const CanvasOutlineOverlay = React.memo(+ ({+ controller,+ resolveSequenceNodePathInfo,+ }: {+ readonly controller: CanvasController;+ readonly resolveSequenceNodePathInfo: CanvasSequenceNodePathResolver;+ }) => {+ const containerRef = useRef<SVGSVGElement | null>(null);+ const [container, setContainer] = useState<SVGSVGElement | null>(null);+ const attachContainer = useCallback((element: SVGSVGElement | null) => {+ containerRef.current = element;+ setContainer(element);+ }, []);+ const [hovered, setHovered] = useState(false);+ const tracks = useSyncExternalStore(+ controller.timeline.subscribe,+ controller.timeline.getSnapshot,+ controller.timeline.getSnapshot,+ );+ const selection = useCanvasSelection(controller.selection);+ const hover = useCanvasHover(controller.hover);+ const layers = useMemo(+ () =>+ tracks.flatMap((track, index): OutlineLayer[] => {+ const nodePathInfo = resolveSequenceNodePathInfo(track, index);+ if (+ nodePathInfo === null ||+ nodePathInfo.auxiliaryKeys.length !== 0+ ) {+ return [];+ }++ return [+ {+ track,+ key: getCanvasSequenceSelectionKey(nodePathInfo),+ nodePathKey: timelineSequenceNodePathToKey(+ nodePathInfo.sequenceSubscriptionKey,+ ),+ nodePathInfo,+ selection: {type: 'sequence', nodePathInfo},+ },+ ];+ }),+ [resolveSequenceNodePathInfo, tracks],+ );+ useEffect(() => {+ controller.hover.setHoveredSequence((current) => {+ if (current === null) {+ return current;+ }++ return layers.some((layer) =>+ current.source === 'timeline'+ ? layer.nodePathKey === current.nodePathKey+ : layer.key === current.key,+ )+ ? current+ : null;+ });+ }, [controller.hover, layers]);+ useEffect(() => {+ const ownerWindow = containerRef.current?.ownerDocument.defaultView;+ const clearHover = () => {+ setHovered(false);+ controller.hover.clear(null);+ };++ ownerWindow?.addEventListener('blur', clearHover);++ return () => {+ ownerWindow?.removeEventListener('blur', clearHover);+ controller.hover.clear('canvas');+ };+ }, [controller.hover]);+ const active =+ hovered ||+ selection.selectedItems.some((item) => item.type !== 'guide') ||+ hover?.source === 'timeline';+ return (+ <svg+ ref={attachContainer}+ style={overlayStyle}+ width="100%"+ height="100%"+ tabIndex={0}+ role="group"+ aria-label="Canvas selection"+ onPointerEnter={() => setHovered(true)}+ onPointerLeave={() => {+ setHovered(false);+ controller.hover.clear('canvas');+ }}+ onPointerDown={(event) => {+ if (event.button !== 0) {+ return;+ }++ event.preventDefault();+ event.stopPropagation();+ containerRef.current?.focus({preventScroll: true});+ controller.selection.clear();+ }}+ onDoubleClick={(event) => event.stopPropagation()}+ onKeyDown={(event) => {+ if (event.key === 'Escape') {+ event.preventDefault();+ event.stopPropagation();+ controller.selection.clear();+ }+ }}+ >+ {active && container !== null ? (+ <ActiveCanvasOutlines+ controller={controller}+ containerRef={containerRef}+ layers={layers}+ measureAll={hovered}+ />+ ) : null}+ </svg>+ );+ },+);diff --git a/packages/canvas/src/canvas.tsx b/packages/canvas/src/canvas.tsxindex 3a40889663..33b10a02e5 100644--- a/packages/canvas/src/canvas.tsx+++ b/packages/canvas/src/canvas.tsx@@ -1,23 +1,35 @@import type {PlayerProps, PlayerRef} from '@remotion/player';import {Player, PlayerInternals} from '@remotion/player';import type {RefObject} from 'react';-import React, {forwardRef, useCallback, useEffect} from 'react';+import React, {forwardRef, useCallback, useEffect, useMemo} from 'react';import type {AnyZodObject, TSequence} from 'remotion';import type {CanvasController} from './canvas-controller';import {getCanvasControllerInternals} from './canvas-controller';+import {CanvasOutlineOverlay} from './canvas-outline-overlay';+import type {CanvasSequenceNodePathResolver} from './sequence-node-path';+import {getCanvasSequenceNodePathInfo} from './sequence-node-path';export type CanvasProps<Schema extends AnyZodObject,Props extends Record<string, unknown>,> = PlayerProps<Schema, Props> & {readonly controller: CanvasController;+ /** Enable hover and selection on the composition. Defaults to false. */+ readonly showOutlines?: boolean;+ /** Use the same resolver for your layer list and canvas selection. */+ readonly resolveSequenceNodePathInfo?: CanvasSequenceNodePathResolver;};const CanvasFn = <Schema extends AnyZodObject,Props extends Record<string, unknown>,>(- {controller, ...playerProps}: CanvasProps<Schema, Props>,+ {+ controller,+ showOutlines = false,+ resolveSequenceNodePathInfo = getCanvasSequenceNodePathInfo,+ ...playerProps+ }: CanvasProps<Schema, Props>,ref: RefObject<PlayerRef>,) => {const internals = getCanvasControllerInternals(controller);@@ -29,16 +41,30 @@ const CanvasFn = <useEffect(() => {return () => internals.clear();}, [internals]);+ const overlay = useMemo(+ () =>+ showOutlines ? (+ <CanvasOutlineOverlay+ controller={controller}+ resolveSequenceNodePathInfo={resolveSequenceNodePathInfo}+ />+ ) : null,+ [controller, resolveSequenceNodePathInfo, showOutlines],+ );return React.createElement(PlayerInternals.TimelineSequenceObserverContext.Provider,{value: onTimelineSequenceChange},React.createElement(- Player as React.ComponentType,- {- ...(playerProps as Record<string, unknown>),- ref,- } as Record<string, unknown>,+ PlayerInternals.CanvasOverlayContext.Provider,+ {value: overlay},+ React.createElement(+ Player as React.ComponentType,+ {+ ...(playerProps as Record<string, unknown>),+ ref,+ } as Record<string, unknown>,+ ),),);};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/hover.ts b/packages/canvas/src/hover.tsnew file mode 100644index 0000000000..dddcd82c9a--- /dev/null+++ b/packages/canvas/src/hover.ts@@ -0,0 +1,136 @@+import type {Dispatch, SetStateAction} from 'react';+import {useCallback, useMemo} from 'react';+import type {SequenceNodePathInfo} from './get-timeline-sequence-sort-key';+import {getCanvasSequenceSelectionKey} from './selection';+import {timelineSequenceNodePathToKey} from './timeline-sequence-node-path-to-key';+import {useSyncExternalStore} from './use-sync-external-store';++export type CanvasHover = {+ readonly key: string;+ readonly nodePathKey: string;+ readonly source: 'canvas' | 'timeline';+};++export type CanvasHoverController = {+ readonly getSnapshot: () => CanvasHover | null;+ readonly subscribe: (listener: () => void) => () => void;+ readonly setHoveredSequence: Dispatch<SetStateAction<CanvasHover | null>>;+ readonly clear: (source: CanvasHover['source'] | null) => void;+};++export const createCanvasHoverController = (): CanvasHoverController => {+ let hoveredSequence: CanvasHover | null = null;+ const listeners = new Set<() => void>();+ const setHoveredSequence: CanvasHoverController['setHoveredSequence'] = (+ action,+ ) => {+ const nextHoveredSequence =+ typeof action === 'function' ? action(hoveredSequence) : action;+ if (+ nextHoveredSequence === hoveredSequence ||+ (nextHoveredSequence !== null &&+ hoveredSequence !== null &&+ nextHoveredSequence.key === hoveredSequence.key &&+ nextHoveredSequence.nodePathKey === hoveredSequence.nodePathKey &&+ nextHoveredSequence.source === hoveredSequence.source)+ ) {+ return;+ }++ hoveredSequence = nextHoveredSequence;+ for (const listener of listeners) {+ listener();+ }+ };++ return {+ getSnapshot: () => hoveredSequence,+ subscribe: (listener) => {+ listeners.add(listener);+ return () => listeners.delete(listener);+ },+ setHoveredSequence,+ clear: (source) => {+ if (source === null || hoveredSequence?.source === source) {+ setHoveredSequence(null);+ }+ },+ };+};++export const useCanvasHover = (+ controller: CanvasHoverController,+): CanvasHover | null => {+ return useSyncExternalStore(+ controller.subscribe,+ controller.getSnapshot,+ controller.getSnapshot,+ );+};++export const useIsCanvasSequenceHovered = (+ controller: CanvasHoverController,+ nodePathKey: string | null,+): boolean => {+ const getSnapshot = useCallback(+ () =>+ nodePathKey !== null &&+ controller.getSnapshot()?.nodePathKey === nodePathKey,+ [controller, nodePathKey],+ );++ return useSyncExternalStore(controller.subscribe, getSnapshot, getSnapshot);+};++export const useCanvasSequenceHover = (+ controller: CanvasHoverController,+ nodePathInfo: SequenceNodePathInfo | null,+ source: CanvasHover['source'],+) => {+ const sequenceKey = useMemo(+ () =>+ nodePathInfo === null+ ? null+ : getCanvasSequenceSelectionKey(nodePathInfo),+ [nodePathInfo],+ );+ const nodePathKey = useMemo(+ () =>+ nodePathInfo === null+ ? null+ : timelineSequenceNodePathToKey(nodePathInfo.sequenceSubscriptionKey),+ [nodePathInfo],+ );+ const hovered = useIsCanvasSequenceHovered(controller, nodePathKey);++ const onPointerEnter = useCallback(() => {+ if (sequenceKey === null || nodePathKey === null) {+ return;+ }++ controller.setHoveredSequence({+ key: sequenceKey,+ nodePathKey,+ source,+ });+ }, [controller, nodePathKey, sequenceKey, source]);++ const onPointerLeave = useCallback(() => {+ controller.setHoveredSequence((currentHover) => {+ if (+ currentHover?.source !== source ||+ currentHover.key !== sequenceKey ||+ currentHover.nodePathKey !== nodePathKey+ ) {+ return currentHover;+ }++ return null;+ });+ }, [controller, nodePathKey, sequenceKey, source]);++ return useMemo(+ () => ({hovered, onPointerEnter, onPointerLeave}),+ [hovered, onPointerEnter, onPointerLeave],+ );+};diff --git a/packages/canvas/src/index.ts b/packages/canvas/src/index.tsindex 332104140b..2a508cf0da 100644--- a/packages/canvas/src/index.ts+++ b/packages/canvas/src/index.ts@@ -3,6 +3,35 @@ export {Canvas} from './canvas';export type {CanvasProps} from './canvas';export {createCanvasController, useCanvasController} from './canvas-controller';export type {CanvasController} from './canvas-controller';+export {+ createCanvasHoverController,+ useCanvasHover,+ useCanvasSequenceHover,+ useIsCanvasSequenceHovered,+} from './hover';+export type {CanvasHover, CanvasHoverController} from './hover';+export type {+ CanvasOutline,+ CanvasOutlinePoint,+ CanvasOutlineUv,+ CanvasOutlineCrop,+ CanvasOutlineTarget,+} from './outline-geometry';+export {getCanvasOutlinePoint, getCanvasOutlineUv} from './outline-geometry';+export {+ measureCanvasOutlineTargets,+ cropCanvasOutlinePoints,+ canvasOutlinesAreEqual,+ getTransformedSvgViewportPoints,+} from './outline-measurement';+export {orderCanvasOutlinesForRendering} from './outline-order';+export type {+ CanvasOutlineOrderTarget,+ CanvasOutlineSequenceParent,+} from './outline-order';+export {useCanvasOutlineMeasurements} from './use-canvas-outline-measurements';+export {getCanvasSequenceNodePathInfo} from './sequence-node-path';+export type {CanvasSequenceNodePathResolver} from './sequence-node-path';export {getConnectedCompositions} from './get-connected-compositions';export {getCascadedStart,@@ -34,3 +63,4 @@ export type {} from './selection';export {sortItemsByCommitOrder} from './sort-by-commit-order';export {timelineSequenceNodePathToKey} from './timeline-sequence-node-path-to-key';+export {useCanvasRuntimeValueSnapshots} from './use-runtime-value-snapshots';diff --git a/packages/canvas/src/outline-geometry.ts b/packages/canvas/src/outline-geometry.tsnew file mode 100644index 0000000000..733a15939f--- /dev/null+++ b/packages/canvas/src/outline-geometry.ts@@ -0,0 +1,218 @@+export type CanvasOutlinePoint = {+ readonly x: number;+ readonly y: number;+};++/** Geometry in CSS pixels relative to the unscaled outline container. */+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,+ ];+};++const mix = (from: number, to: number, progress: number): number => {+ return from + (to - from) * progress;+};++const mixPoint = (+ from: CanvasOutlinePoint,+ to: CanvasOutlinePoint,+ progress: number,+): CanvasOutlinePoint => {+ return {+ x: mix(from.x, to.x, progress),+ y: mix(from.y, to.y, progress),+ };+};++export type CanvasOutlineUv = readonly [number, number];++const getBilinearUvHandlePosition = (+ points: CanvasOutline['points'],+ uv: CanvasOutlineUv,+): CanvasOutlinePoint => {+ 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: 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,+ };+};++const applyProjectiveTransform = (+ transform: ProjectiveTransform,+ uv: CanvasOutlineUv,+): CanvasOutlinePoint => {+ 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,+ };+};++/** Maps normalized coordinates onto a quad, including perspective transforms. */+export const getCanvasOutlinePoint = (+ points: CanvasOutline['points'],+ uv: CanvasOutlineUv,+): CanvasOutlinePoint => {+ const transform = getProjectiveTransform(points);+ return transform === null+ ? getBilinearUvHandlePosition(points, uv)+ : applyProjectiveTransform(transform, uv);+};++const vectorBetween = (+ from: CanvasOutlinePoint,+ to: CanvasOutlinePoint,+): CanvasOutlinePoint => {+ return {x: to.x - from.x, y: to.y - from.y};+};++const getBilinearUvCoordinateForPoint = (+ points: CanvasOutline['points'],+ point: CanvasOutlinePoint,+): CanvasOutlineUv => {+ 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];+};++/** Maps a point back to normalized coordinates within an outline quad. */+export const getCanvasOutlineUv = (+ points: CanvasOutline['points'],+ point: CanvasOutlinePoint,+): CanvasOutlineUv => {+ 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,+ ];+};++/** The fraction cropped from each edge of the original element. */+export type CanvasOutlineCrop = {+ readonly left: number;+ readonly right: number;+ readonly top: number;+ readonly bottom: number;+};++export type CanvasOutlineTarget = {+ readonly key: string;+ readonly ref: React.RefObject<Element | null>;+ readonly crop: CanvasOutlineCrop;+ readonly includeOutsideContainer: boolean;+};diff --git a/packages/canvas/src/outline-measurement.ts b/packages/canvas/src/outline-measurement.tsnew file mode 100644index 0000000000..a308772a4e--- /dev/null+++ b/packages/canvas/src/outline-measurement.ts@@ -0,0 +1,291 @@+import {useCache as resetBoxQuadsCache} from './get-box-quads-polyfill-internals.js';+import {getBoxQuadsPonyfill} from './get-box-quads-ponyfill';+import type {+ CanvasOutline,+ CanvasOutlinePoint,+ CanvasOutlineTarget,+} from './outline-geometry';+import {getCanvasOutlinePoint} from './outline-geometry';++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'] => {+ // `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,+): 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 quads = getBoxQuadsPonyfill(element, {+ box: 'border',+ });+ const quad = quads?.[0];+ if (!quad) {+ return rectToPoints(elementRect, containerRect);+ }++ return quadToPoints(quad, containerRect);+};++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 {left, top} = crop;+ const right = 1 - crop.right;+ const bottom = 1 - crop.bottom;++ return [+ getCanvasOutlinePoint(points, [left, top]),+ getCanvasOutlinePoint(points, [right, top]),+ getCanvasOutlinePoint(points, [right, bottom]),+ getCanvasOutlinePoint(points, [left, bottom]),+ ];+};++/**+ * Measures a batch against an unscaled overlay container. Shared ancestor+ * transforms are cached only within this measurement batch.+ */+export const measureCanvasOutlineTargets = (+ container: Element,+ targets: readonly CanvasOutlineTarget[],+): CanvasOutline[] => {+ // Reuse shared ancestor geometry within this synchronous batch only.+ 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;+ }++ const points = cropCanvasOutlinePoints(uncroppedPoints, target.crop);+ const ownerHTMLElement = element.ownerDocument.defaultView?.HTMLElement;++ outlines.push({+ key: target.key,+ dimensions:+ (typeof HTMLElement !== 'undefined' &&+ element instanceof HTMLElement) ||+ (ownerHTMLElement !== undefined && element instanceof ownerHTMLElement)+ ? {+ width: element.offsetWidth,+ height: element.offsetHeight,+ }+ : isSvgSvgElement(element)+ ? {+ width: element.width.baseVal.value,+ height: element.height.baseVal.value,+ }+ : null,+ uncroppedPoints,+ points,+ });+ }++ 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) {+ 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/outline-order.ts b/packages/canvas/src/outline-order.tsnew file mode 100644index 0000000000..f5733e6f84--- /dev/null+++ b/packages/canvas/src/outline-order.ts@@ -0,0 +1,383 @@+import type {CanvasOutline} from './outline-geometry';++export type CanvasOutlineOrderTarget = {+ readonly sequence: CanvasOutlineSequenceParent;+ readonly selected: boolean;+ readonly containsSelection: boolean;+};++const outlinePointEqualityTolerance = 0.5;+const outlineAreaEqualityTolerance = 0.5;+const outlineBoundsOverlapTolerance = 0.5;++const outlinePointsAreEquivalent = (+ a: CanvasOutline['points'][number],+ b: CanvasOutline['points'][number],+) => {+ return (+ Math.abs(a.x - b.x) <= outlinePointEqualityTolerance &&+ Math.abs(a.y - b.y) <= outlinePointEqualityTolerance+ );+};++const outlinesHaveEquivalentHitArea = (+ a: CanvasOutline,+ b: CanvasOutline,+): boolean => {+ if (a.points.length !== b.points.length) {+ return false;+ }++ for (let startIndex = 0; startIndex < b.points.length; startIndex++) {+ for (const direction of [1, -1]) {+ const pointsMatch = a.points.every((point, index) => {+ const bIndex =+ (startIndex + direction * index + b.points.length) % b.points.length;+ return outlinePointsAreEquivalent(point, b.points[bIndex]);+ });++ if (pointsMatch) {+ return true;+ }+ }+ }++ return false;+};++const getOutlineHitArea = (outline: CanvasOutline): number => {+ let area = 0;++ for (let i = 0; i < outline.points.length; i++) {+ const current = outline.points[i];+ const next = outline.points[(i + 1) % outline.points.length];+ area += current.x * next.y - next.x * current.y;+ }++ return Math.abs(area) / 2;+};++const getOutlineBounds = (outline: CanvasOutline) => {+ const xs = outline.points.map((point) => point.x);+ const ys = outline.points.map((point) => point.y);++ return {+ maxX: Math.max(...xs),+ maxY: Math.max(...ys),+ minX: Math.min(...xs),+ minY: Math.min(...ys),+ };+};++const outlineBoundsOverlap = (+ aBounds: ReturnType<typeof getOutlineBounds>,+ bBounds: ReturnType<typeof getOutlineBounds>,+): boolean => {+ return (+ aBounds.minX <= bBounds.maxX + outlineBoundsOverlapTolerance &&+ aBounds.maxX + outlineBoundsOverlapTolerance >= bBounds.minX &&+ aBounds.minY <= bBounds.maxY + outlineBoundsOverlapTolerance &&+ aBounds.maxY + outlineBoundsOverlapTolerance >= bBounds.minY+ );+};++export type CanvasOutlineSequenceParent = {+ readonly id: string;+ readonly parent: string | null;+};++const getSequenceId = (target: CanvasOutlineOrderTarget | undefined) => {+ return target?.sequence?.id ?? null;+};++const getParentBySequenceId = ({+ sequences,+ targetsByKey,+}: {+ readonly sequences: readonly CanvasOutlineSequenceParent[];+ readonly targetsByKey: ReadonlyMap<string, CanvasOutlineOrderTarget>;+}) => {+ const parentBySequenceId = new Map<string, string | null>();++ for (const sequence of sequences) {+ parentBySequenceId.set(sequence.id, sequence.parent);+ }++ for (const target of targetsByKey.values()) {+ const sequenceId = getSequenceId(target);+ if (sequenceId !== null && !parentBySequenceId.has(sequenceId)) {+ parentBySequenceId.set(sequenceId, target.sequence.parent);+ }+ }++ return parentBySequenceId;+};++const isAncestorTarget = ({+ ancestor,+ descendant,+ parentBySequenceId,+}: {+ readonly ancestor: CanvasOutlineOrderTarget;+ readonly descendant: CanvasOutlineOrderTarget;+ readonly parentBySequenceId: ReadonlyMap<string, string | null>;+}): boolean => {+ const ancestorId = getSequenceId(ancestor);+ if (ancestorId === null) {+ return false;+ }++ let parentId = descendant.sequence?.parent ?? null;+ while (parentId !== null) {+ if (parentId === ancestorId) {+ return true;+ }++ parentId = parentBySequenceId.get(parentId) ?? null;+ }++ return false;+};++const orderOutlineGroup = ({+ outlines,+ parentBySequenceId,+ targetsByKey,+}: {+ readonly outlines: readonly CanvasOutline[];+ readonly parentBySequenceId: ReadonlyMap<string, string | null>;+ readonly targetsByKey: ReadonlyMap<string, CanvasOutlineOrderTarget>;+}): readonly CanvasOutline[] => {+ const incomingEdges = new Map<string, Set<string>>();+ const outgoingEdges = new Map<string, Set<string>>();+ const outlinesByKey = new Map(+ outlines.map((outline) => [outline.key, outline]),+ );++ for (const outline of outlines) {+ incomingEdges.set(outline.key, new Set());+ outgoingEdges.set(outline.key, new Set());+ }++ const hasPath = ({+ fromKey,+ seen,+ toKey,+ }: {+ readonly fromKey: string;+ readonly seen: Set<string>;+ readonly toKey: string;+ }): boolean => {+ if (fromKey === toKey) {+ return true;+ }++ if (seen.has(fromKey)) {+ return false;+ }++ seen.add(fromKey);+ for (const nextKey of outgoingEdges.get(fromKey) ?? []) {+ if (hasPath({fromKey: nextKey, seen, toKey})) {+ return true;+ }+ }++ return false;+ };++ const addEdge = (+ before: CanvasOutline,+ after: CanvasOutline,+ options: {readonly skipIfCycle: boolean} | null,+ ) => {+ if (before.key === after.key) {+ return;+ }++ const incoming = incomingEdges.get(after.key);+ const outgoing = outgoingEdges.get(before.key);+ if (incoming === undefined || outgoing === undefined) {+ throw new Error('Expected outline to be registered before adding edge');+ }++ if (outgoing.has(after.key)) {+ return;+ }++ if (hasPath({fromKey: after.key, seen: new Set(), toKey: before.key})) {+ if (options?.skipIfCycle) {+ return;+ }++ throw new Error('Could not determine a stable outline rendering order');+ }++ incoming.add(before.key);+ outgoing.add(after.key);+ };++ const addAncestorConstraint = ({+ ancestor,+ descendant,+ descendantContainsSelection,+ equivalentHitArea,+ }: {+ readonly ancestor: CanvasOutline;+ readonly descendant: CanvasOutline;+ readonly descendantContainsSelection: boolean;+ readonly equivalentHitArea: boolean;+ }) => {+ // Usually, children should be above parents so nested elements are directly+ // selectable. If an unselected child has the same hit area as its parent, put+ // it below the parent so the wrapper can be selected. Once the child or+ // one of its properties is selected, keep it above the parent so it remains+ // directly draggable.+ if (equivalentHitArea && !descendantContainsSelection) {+ addEdge(descendant, ancestor, null);+ } else {+ addEdge(ancestor, descendant, null);+ }+ };++ const outlineBySequenceId = new Map<string, CanvasOutline>();+ for (const outline of outlines) {+ const sequenceId = getSequenceId(targetsByKey.get(outline.key));+ if (sequenceId !== null) {+ outlineBySequenceId.set(sequenceId, outline);+ }+ }++ // Only constrain each outline against its nearest rendered ancestor. The+ // resulting graph follows the sequence tree, so pairwise subpixel equivalence+ // cannot introduce contradictory edges across three nested outlines.+ for (const descendant of outlines) {+ const descendantTarget = targetsByKey.get(descendant.key);+ let parentId = descendantTarget?.sequence?.parent ?? null;++ while (parentId !== null) {+ const ancestor = outlineBySequenceId.get(parentId);+ if (ancestor !== undefined) {+ addAncestorConstraint({+ ancestor,+ descendant,+ descendantContainsSelection:+ (descendantTarget?.selected ?? false) ||+ (descendantTarget?.containsSelection ?? false),+ equivalentHitArea: outlinesHaveEquivalentHitArea(+ ancestor,+ descendant,+ ),+ });+ break;+ }++ parentId = parentBySequenceId.get(parentId) ?? null;+ }+ }++ // For unrelated overlapping outlines, put broader hit targets below+ // smaller ones so a large selected sequence cannot swallow clicks on+ // a more specific element in the same canvas area.+ const bounds = outlines.map(getOutlineBounds);+ const areas = outlines.map(getOutlineHitArea);+ for (let i = 0; i < outlines.length; i++) {+ const a = outlines[i];+ const aTarget = targetsByKey.get(a.key);+ if (aTarget === undefined) {+ continue;+ }++ for (let j = i + 1; j < outlines.length; j++) {+ if (!outlineBoundsOverlap(bounds[i], bounds[j])) {+ continue;+ }++ const aArea = areas[i];+ const bArea = areas[j];+ if (Math.abs(aArea - bArea) <= outlineAreaEqualityTolerance) {+ continue;+ }++ const b = outlines[j];+ const bTarget = targetsByKey.get(b.key);+ if (bTarget === undefined) {+ continue;+ }++ const aAncestorOfB = isAncestorTarget({+ ancestor: aTarget,+ descendant: bTarget,+ parentBySequenceId,+ });+ const bAncestorOfA = isAncestorTarget({+ ancestor: bTarget,+ descendant: aTarget,+ parentBySequenceId,+ });++ if (aAncestorOfB || bAncestorOfA) {+ continue;+ }++ if (aArea > bArea) {+ addEdge(a, b, {skipIfCycle: true});+ } else {+ addEdge(b, a, {skipIfCycle: true});+ }+ }+ }++ const emitted = new Set<string>();+ const visiting = new Set<string>();+ const ordered: CanvasOutline[] = [];++ const visit = (outline: CanvasOutline) => {+ if (emitted.has(outline.key)) {+ return;+ }++ if (visiting.has(outline.key)) {+ throw new Error('Could not determine a stable outline rendering order');+ }++ visiting.add(outline.key);+ for (const dependencyKey of incomingEdges.get(outline.key) ?? []) {+ const dependency = outlinesByKey.get(dependencyKey);+ if (dependency === undefined) {+ throw new Error('Expected outline dependency to exist');+ }++ visit(dependency);+ }++ visiting.delete(outline.key);+ emitted.add(outline.key);+ ordered.push(outline);+ };++ for (const outline of outlines) {+ visit(outline);+ }++ return ordered;+};++/** Orders quads from back to front while preserving access to nested targets. */+export const orderCanvasOutlinesForRendering = ({+ outlines,+ sequences,+ targetsByKey,+}: {+ readonly outlines: readonly CanvasOutline[];+ readonly sequences: readonly CanvasOutlineSequenceParent[];+ readonly targetsByKey: ReadonlyMap<string, CanvasOutlineOrderTarget>;+}): readonly CanvasOutline[] => {+ const parentBySequenceId = getParentBySequenceId({sequences, targetsByKey});++ return orderOutlineGroup({+ outlines,+ parentBySequenceId,+ targetsByKey,+ });+};diff --git a/packages/canvas/src/sequence-node-path.ts b/packages/canvas/src/sequence-node-path.tsnew file mode 100644index 0000000000..3b1c55f4c8--- /dev/null+++ b/packages/canvas/src/sequence-node-path.ts@@ -0,0 +1,30 @@+import type {+ SequenceNodePathInfo,+ TimelineTrackData,+} from './get-timeline-sequence-sort-key';++export type CanvasSequenceNodePathResolver = (+ track: TimelineTrackData,+ index: number,+) => SequenceNodePathInfo | null;++/** Resolve a source identity when available, otherwise use the mounted instance. */+export const getCanvasSequenceNodePathInfo = (+ track: TimelineTrackData,+): SequenceNodePathInfo => {+ return (+ track.nodePathInfo ?? {+ sequenceSubscriptionKey: {+ absolutePath: 'remotion-canvas',+ nodePath: ['sequence', track.sequence.id],+ sequenceKeys: [],+ effectKeys: [],+ videoConfigValues: null,+ },+ auxiliaryKeys: [],+ index: 0,+ numberOfSequencesWithThisNodePath: 1,+ supportsEffects: track.sequence.controls?.supportsEffects === true,+ }+ );+};diff --git a/packages/canvas/src/use-canvas-outline-measurements.ts b/packages/canvas/src/use-canvas-outline-measurements.tsnew file mode 100644index 0000000000..0e8e00a6ca--- /dev/null+++ b/packages/canvas/src/use-canvas-outline-measurements.ts@@ -0,0 +1,125 @@+import type {MutableRefObject, RefObject} from 'react';+import {useCallback, useLayoutEffect, useRef, useState} from 'react';+import type {CanvasOutline, CanvasOutlineTarget} from './outline-geometry';+import {+ canvasOutlinesAreEqual,+ measureCanvasOutlineTargets,+} from './outline-measurement';++export const useCanvasOutlineMeasurements = ({+ containerRef,+ targets,+ updateOutlinesRef,+}: {+ readonly containerRef: RefObject<SVGSVGElement | null>;+ readonly targets: readonly CanvasOutlineTarget[];+ readonly updateOutlinesRef: MutableRefObject<() => void> | null;+}): readonly CanvasOutline[] => {+ // Derived targets can change identity on every render. Keep only geometry in+ // state so measuring in a layout effect cannot cause an update loop.+ const [outlines, setOutlines] = useState<readonly CanvasOutline[]>([]);+ const outlinesRef = useRef(outlines);+ const latestUpdateRef = useRef<() => void>(() => undefined);+ const resizeObserverRef = useRef<ResizeObserver | null>(null);+ const observedElementsRef = useRef<ReadonlySet<Element>>(new Set());++ const updateOutlines = useCallback(() => {+ const container = containerRef.current;+ if (container === null || targets.length === 0) {+ if (outlinesRef.current.length === 0) {+ return;+ }++ outlinesRef.current = [];+ setOutlines(outlinesRef.current);+ return;+ }++ const nextOutlines = measureCanvasOutlineTargets(container, targets);+ if (canvasOutlinesAreEqual(outlinesRef.current, nextOutlines)) {+ return;+ }++ outlinesRef.current = nextOutlines;+ setOutlines(nextOutlines);+ }, [containerRef, targets]);++ useLayoutEffect(() => {+ latestUpdateRef.current = updateOutlines;+ if (updateOutlinesRef !== null) {+ updateOutlinesRef.current = updateOutlines;+ }++ updateOutlines();+ return () => {+ latestUpdateRef.current = () => undefined;+ if (updateOutlinesRef?.current === updateOutlines) {+ updateOutlinesRef.current = () => undefined;+ }+ };+ }, [updateOutlines, updateOutlinesRef]);++ useLayoutEffect(() => {+ const ownerWindow = containerRef.current?.ownerDocument.defaultView;+ if (!ownerWindow || typeof ownerWindow.ResizeObserver === 'undefined') {+ return;+ }++ let animationFrame: number | null = null;+ const resizeObserver = new ownerWindow.ResizeObserver(() => {+ if (animationFrame !== null) {+ return;+ }++ animationFrame = ownerWindow.requestAnimationFrame(() => {+ animationFrame = null;+ latestUpdateRef.current();+ });+ });+ resizeObserverRef.current = resizeObserver;++ return () => {+ if (animationFrame !== null) {+ ownerWindow.cancelAnimationFrame(animationFrame);+ }++ resizeObserver.disconnect();+ resizeObserverRef.current = null;+ observedElementsRef.current = new Set();+ };+ }, [containerRef]);++ useLayoutEffect(() => {+ const resizeObserver = resizeObserverRef.current;+ if (resizeObserver === null) {+ return;+ }++ const nextObservedElements = new Set<Element>();+ if (containerRef.current !== null) {+ nextObservedElements.add(containerRef.current);+ }++ for (const target of targets) {+ if (target.ref.current !== null) {+ nextObservedElements.add(target.ref.current);+ }+ }++ for (const element of observedElementsRef.current) {+ if (!nextObservedElements.has(element)) {+ resizeObserver.unobserve(element);+ }+ }++ for (const element of nextObservedElements) {+ if (!observedElementsRef.current.has(element)) {+ resizeObserver.observe(element);+ }+ }++ observedElementsRef.current = nextObservedElements;+ }, [containerRef, targets]);++ return outlines;+};diff --git a/packages/canvas/src/use-runtime-value-snapshots.ts b/packages/canvas/src/use-runtime-value-snapshots.tsnew file mode 100644index 0000000000..8e439ee2b1--- /dev/null+++ b/packages/canvas/src/use-runtime-value-snapshots.ts@@ -0,0 +1,47 @@+import {useMemo} from 'react';+import type {SequenceRegistrationControls} from 'remotion';+import {useSyncExternalStore} from './use-sync-external-store';++export const useCanvasRuntimeValueSnapshots = (+ controls: readonly SequenceRegistrationControls[],+): readonly Readonly<Record<string, unknown>>[] => {+ const stores = useMemo(+ () => controls.map((control) => control.runtimeValues),+ [controls],+ );+ const aggregateStore = useMemo(() => {+ let lastSnapshots = stores.map((store) => store.getSnapshot());+ const getSnapshot = () => {+ const nextSnapshots = stores.map((store) => store.getSnapshot());+ if (+ nextSnapshots.length === lastSnapshots.length &&+ nextSnapshots.every(+ (snapshot, index) => snapshot === lastSnapshots[index],+ )+ ) {+ return lastSnapshots;+ }++ lastSnapshots = nextSnapshots;+ return lastSnapshots;+ };++ return {+ getSnapshot,+ subscribe: (listener: () => void) => {+ const unsubscribers = stores.map((store) => store.subscribe(listener));+ return () => {+ for (const unsubscribe of unsubscribers) {+ unsubscribe();+ }+ };+ },+ };+ }, [stores]);++ return useSyncExternalStore(+ aggregateStore.subscribe,+ aggregateStore.getSnapshot,+ aggregateStore.getSnapshot,+ );+};diff --git a/packages/canvas/tsconfig.json b/packages/canvas/tsconfig.jsonindex 7034960cb3..ebcfea2f5d 100644--- a/packages/canvas/tsconfig.json+++ b/packages/canvas/tsconfig.json@@ -4,6 +4,7 @@"rootDir": "src","outDir": "dist","skipLibCheck": true,+ "allowJs": true,"target": "ES2022","module": "es2020","moduleResolution": "bundler",diff --git a/packages/player-example/src/BrowserBundlerExample.tsx b/packages/player-example/src/BrowserBundlerExample.tsxindex 13f4c93a9a..35fd20d102 100644--- a/packages/player-example/src/BrowserBundlerExample.tsx+++ b/packages/player-example/src/BrowserBundlerExample.tsx@@ -28,11 +28,11 @@ export const Video: React.FC<{title: string; accent: string}> = ({title, accent}<Sequence name="Background" layout="none"><AbsoluteFill style={{background: 'linear-gradient(135deg, #0f172a, #312e81)'}} /></Sequence>- <Sequence name="Orb" layout="none">+ <Sequence name="Orb" style={{left: 910, top: -80, width: 450, height: 450}}><Orb accent={accent} /></Sequence>- <Sequence name="Content" layout="none">- <div style={{position: 'relative', transform: \`translateY(\${Math.sin(frame / fps) * 12}px)\`}}>+ <Sequence name="Content" style={{left: 80, top: 210, width: 1000, height: 300, transform: \`translateY(\${Math.sin(frame / fps) * 12}px)\`}}>+ <div style={{position: 'relative'}}><h1 style={{fontSize: 76, margin: 0}}>{title}</h1><p style={{fontSize: 28, color: accent}}>Frame {frame} of {durationInFrames}</p><button type="button" style={{fontSize: 28, padding: '8px 16px'}}@@ -86,7 +86,7 @@ export const Orb: React.FC<{accent: string}> = ({accent}) => {const frame = useCurrentFrame();return <div style={{position: 'absolute', width: 450, height: 450, borderRadius: '50%',- right: -80, top: -80, backgroundColor: accent, opacity: 0.3,+ right: 0, top: 0, backgroundColor: accent, opacity: 0.3,transform: \`translateY(\${Math.sin(frame / 25) * 100}px)\`,}} />;};diff --git a/packages/player-example/src/browser-bundler-preview/runtime.tsx b/packages/player-example/src/browser-bundler-preview/runtime.tsxindex c56c19162c..b8699a3a73 100644--- a/packages/player-example/src/browser-bundler-preview/runtime.tsx+++ b/packages/player-example/src/browser-bundler-preview/runtime.tsx@@ -8,11 +8,20 @@ import {getCanvasSelectionItemKey,useCanvasController,useCanvasSelection,+ useCanvasSequenceHover,+ type CanvasController,type CanvasSelectionItem,type SequenceNodePathInfo,type TimelineTrackData,} from '@remotion/canvas';-import React, {useEffect, useMemo, useSyncExternalStore} from 'react';+import React, {+ memo,+ useCallback,+ useEffect,+ useMemo,+ useState,+ useSyncExternalStore,+} from 'react';import {createRoot} from 'react-dom/client';import type {BrowserBundlerPreviewNode,@@ -42,6 +51,102 @@ const getExampleNodePathInfo = ();};+const LayerRow = memo(function LayerRow({+ controller,+ layer,+ layerSelection,+ selectableLayers,+ selected,+ durationInFrames,+}: {+ readonly controller: CanvasController;+ readonly layer: TimelineTrackData;+ readonly layerSelection: Extract<CanvasSelectionItem, {type: 'sequence'}>;+ readonly selectableLayers: readonly CanvasSelectionItem[];+ readonly selected: boolean;+ readonly durationInFrames: number;+}) {+ const {hovered, onPointerEnter, onPointerLeave} = useCanvasSequenceHover(+ controller.hover,+ layerSelection.nodePathInfo,+ 'timeline',+ );++ return (+ <li style={{borderBottom: '1px solid #242a34'}}>+ <button+ type="button"+ aria-pressed={selected}+ onPointerEnter={onPointerEnter}+ onPointerLeave={onPointerLeave}+ onFocus={onPointerEnter}+ onBlur={onPointerLeave}+ onClick={(event) => {+ controller.selection.select(+ layerSelection,+ {+ shiftKey: event.shiftKey,+ toggleKey: event.metaKey || event.ctrlKey,+ },+ selectableLayers,+ );+ }}+ style={{+ alignItems: 'center',+ backgroundColor: selected+ ? '#242c48'+ : hovered+ ? '#1c2637'+ : 'transparent',+ border: 0,+ color: '#e5e7eb',+ display: 'grid',+ fontSize: 11,+ gridTemplateColumns: 'minmax(100px, 26%) minmax(0, 1fr)',+ padding: '4px 14px',+ textAlign: 'left',+ width: '100%',+ }}+ >+ <strong style={{paddingLeft: layer.depth * 12}}>+ {layer.sequence.displayName ?? layer.sequence.type}+ </strong>+ <div+ style={{+ backgroundColor: '#0b0f16',+ borderRadius: 3,+ height: 22,+ overflow: 'hidden',+ position: 'relative',+ }}+ >+ <div+ style={{+ alignItems: 'center',+ backgroundColor: selected+ ? '#6366f1'+ : hovered+ ? '#4b6383'+ : '#374151',+ borderRadius: 3,+ boxSizing: 'border-box',+ display: 'flex',+ height: '100%',+ left: `${(Math.max(0, layer.sequence.from) / durationInFrames) * 100}%`,+ padding: '0 7px',+ position: 'absolute',+ whiteSpace: 'nowrap',+ width: `${(Math.min(layer.sequence.duration, durationInFrames) / durationInFrames) * 100}%`,+ }}+ >+ frame {layer.sequence.from} · {layer.sequence.duration}f+ </div>+ </div>+ </button>+ </li>+ );+});+const Preview: React.FC<{readonly composition: BrowserComposition;readonly revision: number;@@ -58,6 +163,14 @@ const Preview: React.FC<{onReady,}) => {const controller = useCanvasController();+ const [interactionMode, setInteractionMode] = useState<'select' | 'interact'>(+ 'select',+ );+ const resolveSequenceNodePathInfo = useCallback(+ (layer: TimelineTrackData, index: number) =>+ getExampleNodePathInfo(layer, sourceNodes[index]),+ [sourceNodes],+ );const layers = useSyncExternalStore(controller.timeline.subscribe,controller.timeline.getSnapshot,@@ -68,20 +181,20 @@ const Preview: React.FC<{() => new Set(selection.selectedItems.map(getCanvasSelectionItemKey)),[selection.selectedItems],);- const selectableLayers: CanvasSelectionItem[] = layers.map(- (layer, index) => ({- type: 'sequence',- nodePathInfo: getExampleNodePathInfo(layer, sourceNodes[index]),- }),+ const selectableLayers = useMemo(+ () =>+ layers.map(+ (layer, index): Extract<CanvasSelectionItem, {type: 'sequence'}> => ({+ type: 'sequence',+ nodePathInfo: resolveSequenceNodePathInfo(layer, index),+ }),+ ),+ [layers, resolveSequenceNodePathInfo],);const selectedNodes = useMemo(() => {const nodes = new Map<string, BrowserBundlerPreviewNode>();- for (let index = 0; index < layers.length; index++) {- const layer = layers[index];- const layerSelection: CanvasSelectionItem = {- type: 'sequence',- nodePathInfo: getExampleNodePathInfo(layer, sourceNodes[index]),- };+ for (let index = 0; index < selectableLayers.length; index++) {+ const layerSelection = selectableLayers[index];if (!selectedKeys.has(getCanvasSelectionItemKey(layerSelection))) {continue;}@@ -95,7 +208,7 @@ const Preview: React.FC<{}return Array.from(nodes.values());- }, [layers, selectedKeys, sourceNodes]);+ }, [selectableLayers, selectedKeys, sourceNodes]);useEffect(() => onReady(revision), [onReady, preview, revision]);useEffect(() => {@@ -143,7 +256,7 @@ const Preview: React.FC<{overflow: 'hidden',}}>- <p+ <headerstyle={{alignItems: 'center',backgroundColor: '#141820',@@ -155,9 +268,36 @@ const Preview: React.FC<{padding: '0 14px',}}>- {preview.width} x {preview.height} / {preview.fps} fps /{' '}- {preview.durationInFrames} frames- </p>+ <span>+ {preview.width} x {preview.height} / {preview.fps} fps /{' '}+ {preview.durationInFrames} frames+ </span>+ <div+ role="group"+ aria-label="Canvas interaction mode"+ style={{display: 'flex', gap: 4, marginLeft: 'auto'}}+ >+ {(['select', 'interact'] as const).map((mode) => (+ <button+ key={mode}+ type="button"+ aria-pressed={interactionMode === mode}+ onClick={() => setInteractionMode(mode)}+ style={{+ backgroundColor:+ interactionMode === mode ? '#374151' : 'transparent',+ border: '1px solid #374151',+ borderRadius: 4,+ color: '#e5e7eb',+ fontSize: 11,+ padding: '4px 9px',+ }}+ >+ {mode === 'select' ? 'Select' : 'Interact'}+ </button>+ ))}+ </div>+ </header><divstyle={{alignItems: 'center',@@ -171,6 +311,8 @@ const Preview: React.FC<{><Canvascontroller={controller}+ showOutlines={interactionMode === 'select'}+ resolveSequenceNodePathInfo={resolveSequenceNodePathInfo}component={preview.component}inputProps={preview.props}compositionWidth={preview.width}@@ -259,88 +401,19 @@ const Preview: React.FC<{padding: 0,}}>- {layers.map((layer, index) => {- const nodePathInfo = getExampleNodePathInfo(- layer,- sourceNodes[index],- );- const layerSelection: CanvasSelectionItem = {- type: 'sequence',- nodePathInfo,- };- const layerSelected = selectedKeys.has(- getCanvasSelectionItemKey(layerSelection),- );-- return (- <li- key={layer.sequence.id}- style={{borderBottom: '1px solid #242a34'}}- >- <button- type="button"- aria-pressed={layerSelected}- onClick={(event) => {- controller.selection.select(- layerSelection,- {- shiftKey: event.shiftKey,- toggleKey: event.metaKey || event.ctrlKey,- },- selectableLayers,- );- }}- style={{- alignItems: 'center',- backgroundColor: layerSelected- ? '#242c48'- : 'transparent',- border: 0,- color: '#e5e7eb',- display: 'grid',- fontSize: 11,- gridTemplateColumns: 'minmax(100px, 26%) minmax(0, 1fr)',- padding: '4px 14px',- textAlign: 'left',- width: '100%',- }}- >- <strong style={{paddingLeft: layer.depth * 12}}>- {layer.sequence.displayName ?? layer.sequence.type}- </strong>- <div- style={{- backgroundColor: '#0b0f16',- borderRadius: 3,- height: 22,- overflow: 'hidden',- position: 'relative',- }}- >- <div- style={{- alignItems: 'center',- backgroundColor: layerSelected- ? '#6366f1'- : '#374151',- borderRadius: 3,- boxSizing: 'border-box',- display: 'flex',- height: '100%',- left: `${(Math.max(0, layer.sequence.from) / preview.durationInFrames) * 100}%`,- padding: '0 7px',- position: 'absolute',- whiteSpace: 'nowrap',- width: `${(Math.min(layer.sequence.duration, preview.durationInFrames) / preview.durationInFrames) * 100}%`,- }}- >- frame {layer.sequence.from} · {layer.sequence.duration}f- </div>- </div>- </button>- </li>- );- })}+ {layers.map((layer, index) => (+ <LayerRow+ key={layer.sequence.id}+ controller={controller}+ layer={layer}+ layerSelection={selectableLayers[index]}+ selectableLayers={selectableLayers}+ selected={selectedKeys.has(+ getCanvasSelectionItemKey(selectableLayers[index]),+ )}+ durationInFrames={preview.durationInFrames}+ />+ ))}</ol>)}</section>diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsxindex 0ddc9e7723..490445b6fe 100644--- a/packages/player/src/PlayerUI.tsx+++ b/packages/player/src/PlayerUI.tsx@@ -19,6 +19,7 @@ import {calculateOuter,calculateOuterStyle,} from './calculate-scale.js';+import {CanvasOverlayContext} from './canvas-overlay-context.js';import {ErrorBoundary} from './error-boundary.js';import {RenderWarningIfBlacklist} from './license-blacklist.js';import type {RenderMuteButton} from './MediaVolumeSlider.js';@@ -185,6 +186,7 @@ const PlayerUI: React.ForwardRefRenderFunction<);const {playerMuted, mediaVolume} = useContext(Internals.MediaVolumeContext);+ const canvasOverlay = useContext(CanvasOverlayContext);useEffect(() => {player.emitter.dispatchVolumeChange(mediaVolume);@@ -662,10 +664,12 @@ const PlayerUI: React.ForwardRefRenderFunction<{VideoComponent ? (<ErrorBoundary onError={onError} errorFallback={errorFallback}><Internals.CurrentScaleContext.Provider value={currentScale}>- <VideoComponent- {...(video?.props ?? {})}- {...(inputProps ?? {})}- />+ <CanvasOverlayContext.Provider value={null}>+ <VideoComponent+ {...(video?.props ?? {})}+ {...(inputProps ?? {})}+ />+ </CanvasOverlayContext.Provider></Internals.CurrentScaleContext.Provider></ErrorBoundary>) : null}@@ -686,6 +690,7 @@ const PlayerUI: React.ForwardRefRenderFunction<) : null}</div><RenderWarningIfBlacklist />+ {canvasOverlay}</div>{shouldShowPoster && posterFillMode === 'player-size' ? (<divdiff --git a/packages/player/src/canvas-overlay-context.ts b/packages/player/src/canvas-overlay-context.tsnew file mode 100644index 0000000000..b331993994--- /dev/null+++ b/packages/player/src/canvas-overlay-context.ts@@ -0,0 +1,6 @@+import {createContext} from 'react';+import type {ReactNode} from 'react';++// Canvas supplies its authoring UI here so it shares the Player's timeline,+// clipping and fullscreen container without wrapping the user's composition.+export const CanvasOverlayContext = createContext<ReactNode>(null);diff --git a/packages/player/src/index.ts b/packages/player/src/index.tsindex 4a380d0e10..9251f4d008 100644--- a/packages/player/src/index.ts+++ b/packages/player/src/index.ts@@ -1,6 +1,7 @@import './_check-rsc.js';import {BufferingIndicator} from './BufferingIndicator.js';import {calculateCanvasTransformation} from './calculate-scale.js';+import {CanvasOverlayContext} from './canvas-overlay-context.js';import {PlayerEventEmitterContext} from './emitter-context.js';import {PlayerEmitterProvider} from './EmitterProvider.js';import type {CallbackListener, PlayerEventTypes} from './event-emitter.js';@@ -47,4 +48,5 @@ export const PlayerInternals = {PlayerEmitterProvider,BufferingIndicator,TimelineSequenceObserverContext,+ CanvasOverlayContext,};diff --git a/packages/studio/src/components/SelectedOutlineRenderer.tsx b/packages/studio/src/components/SelectedOutlineRenderer.tsxindex 604acd8820..4cf7e96889 100644--- a/packages/studio/src/components/SelectedOutlineRenderer.tsx+++ b/packages/studio/src/components/SelectedOutlineRenderer.tsx@@ -1,10 +1,10 @@+import {useCanvasOutlineMeasurements} 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,@@ -79,14 +75,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{sequences,updateOutlinesRef,}) => {- // Targets are derived props and can receive a new identity on every render.- // Keep only measured geometry in state to avoid layout-effect update loops.- const [outlines, setOutlines] = useState<readonly SelectedOutline[]>([]);- const outlinesRef = useRef<readonly SelectedOutline[]>(outlines);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>(),);@@ -128,100 +117,25 @@ const SelectedOutlineRendererUnmemoized: React.FC<{const hoveredTimelineNodePathKey =hoveredSequence?.source === 'timeline' ? hoveredNodePathKey : null;- const updateOutlines = useCallback(() => {- if (overlayRef.current === null || outlineTargets.length === 0) {- if (outlinesRef.current.length === 0) {- return;- }-- outlinesRef.current = [];- setOutlines(outlinesRef.current);- return;- }-- const nextOutlines = measureOutlines(- overlayRef.current,- outlineTargets,- hoveredTimelineNodePathKey,- );- if (outlinesAreEqual(outlinesRef.current, nextOutlines)) {- return;- }-- outlinesRef.current = nextOutlines;- setOutlines(nextOutlines);- }, [hoveredTimelineNodePathKey, outlineTargets]);-- useLayoutEffect(() => {- updateOutlinesRef.current = updateOutlines;- updateOutlines();- return () => {- if (updateOutlinesRef.current === updateOutlines) {- updateOutlinesRef.current = () => undefined;- }- };- }, [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 outlineTargets) {- 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;- }, [outlineTargets]);+ const measurementTargets = useMemo(+ () =>+ outlineTargets.map((target) => ({+ crop: target.crop,+ includeOutsideContainer:+ target.showSelectedOutline ||+ timelineSequenceNodePathToKey(+ target.nodePathInfo.sequenceSubscriptionKey,+ ) === hoveredTimelineNodePathKey,+ key: target.key,+ ref: target.ref,+ })),+ [hoveredTimelineNodePathKey, outlineTargets],+ );+ const outlines = useCanvasOutlineMeasurements({+ containerRef: overlayRef,+ targets: measurementTargets,+ updateOutlinesRef,+ });const targetsByKey = useMemo(() => {return new Map(outlineTargets.map((target) => [target.key, target]));diff --git a/packages/studio/src/components/ShowOutlinesProvider.tsx b/packages/studio/src/components/ShowOutlinesProvider.tsxindex 02326b0174..5ca42503b4 100644--- a/packages/studio/src/components/ShowOutlinesProvider.tsx+++ b/packages/studio/src/components/ShowOutlinesProvider.tsx@@ -23,9 +23,7 @@ export const ShowOutlinesProvider: React.FC<{);useEffect(() => {const onPointerLeave = () => {- timelineSequenceHoverStore.setHoveredSequence((hover) =>- hover?.source === 'timeline' ? null : hover,- );+ timelineSequenceHoverStore.clear('timeline');};window.addEventListener('pointerleave', onPointerLeave);diff --git a/packages/studio/src/components/selected-outline-geometry.ts b/packages/studio/src/components/selected-outline-geometry.tsindex 504f10bec5..3f5fd3c69b 100644--- a/packages/studio/src/components/selected-outline-geometry.ts+++ b/packages/studio/src/components/selected-outline-geometry.ts@@ -1,24 +1,9 @@-export type OutlinePoint = {- readonly x: number;- readonly y: number;-};+import type {CanvasOutlinePoint as OutlinePoint} from '@remotion/canvas';-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 type {+ CanvasOutline as SelectedOutline,+ CanvasOutlinePoint as OutlinePoint,+} from '@remotion/canvas';export const clamp = (value: number, min: number, max: number): number => {return Math.min(max, Math.max(min, value));diff --git a/packages/studio/src/components/selected-outline-measurement.ts b/packages/studio/src/components/selected-outline-measurement.tsindex 24efb02093..703b3e5a43 100644--- a/packages/studio/src/components/selected-outline-measurement.ts+++ b/packages/studio/src/components/selected-outline-measurement.ts@@ -1,16 +1,10 @@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 {studioCssVariables} from '../helpers/studio-css-variables';-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,@@ -28,6 +22,12 @@ import {parseTransformOrigin,} from './Timeline/transform-origin-utils';+export {+ cropCanvasOutlinePoints as cropOutlinePoints,+ getTransformedSvgViewportPoints,+ measureCanvasOutlineTargets as measureOutlineTargets,+} from '@remotion/canvas';+export const pointToString = (point: OutlinePoint) => `${point.x},${point.y}`;export const midpoint = (@@ -175,184 +175,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> => {@@ -642,126 +464,3 @@ export const getSequencesWithSelectableOutlines = ({};});};--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/studio/src/components/selected-outline-order.ts b/packages/studio/src/components/selected-outline-order.tsindex 3cb832621d..1db8887490 100644--- a/packages/studio/src/components/selected-outline-order.ts+++ b/packages/studio/src/components/selected-outline-order.ts@@ -1,377 +1 @@-import type {SelectedOutline} from './selected-outline-geometry';-import type {SelectedOutlineLayoutTarget} from './selected-outline-types';--const outlinePointEqualityTolerance = 0.5;-const outlineAreaEqualityTolerance = 0.5;-const outlineBoundsOverlapTolerance = 0.5;--const outlinePointsAreEquivalent = (- a: SelectedOutline['points'][number],- b: SelectedOutline['points'][number],-) => {- return (- Math.abs(a.x - b.x) <= outlinePointEqualityTolerance &&- Math.abs(a.y - b.y) <= outlinePointEqualityTolerance- );-};--const outlinesHaveEquivalentHitArea = (- a: SelectedOutline,- b: SelectedOutline,-): boolean => {- if (a.points.length !== b.points.length) {- return false;- }-- for (let startIndex = 0; startIndex < b.points.length; startIndex++) {- for (const direction of [1, -1]) {- const pointsMatch = a.points.every((point, index) => {- const bIndex =- (startIndex + direction * index + b.points.length) % b.points.length;- return outlinePointsAreEquivalent(point, b.points[bIndex]);- });-- if (pointsMatch) {- return true;- }- }- }-- return false;-};--const getOutlineHitArea = (outline: SelectedOutline): number => {- let area = 0;-- for (let i = 0; i < outline.points.length; i++) {- const current = outline.points[i];- const next = outline.points[(i + 1) % outline.points.length];- area += current.x * next.y - next.x * current.y;- }-- return Math.abs(area) / 2;-};--const getOutlineBounds = (outline: SelectedOutline) => {- const xs = outline.points.map((point) => point.x);- const ys = outline.points.map((point) => point.y);-- return {- maxX: Math.max(...xs),- maxY: Math.max(...ys),- minX: Math.min(...xs),- minY: Math.min(...ys),- };-};--const outlineBoundsOverlap = (- aBounds: ReturnType<typeof getOutlineBounds>,- bBounds: ReturnType<typeof getOutlineBounds>,-): boolean => {- return (- aBounds.minX <= bBounds.maxX + outlineBoundsOverlapTolerance &&- aBounds.maxX + outlineBoundsOverlapTolerance >= bBounds.minX &&- aBounds.minY <= bBounds.maxY + outlineBoundsOverlapTolerance &&- aBounds.maxY + outlineBoundsOverlapTolerance >= bBounds.minY- );-};--type OutlineSequenceParent = {- readonly id: string;- readonly parent: string | null;-};--const getSequenceId = (target: SelectedOutlineLayoutTarget | undefined) => {- return target?.sequence?.id ?? null;-};--const getParentBySequenceId = ({- sequences,- targetsByKey,-}: {- readonly sequences: readonly OutlineSequenceParent[];- readonly targetsByKey: ReadonlyMap<string, SelectedOutlineLayoutTarget>;-}) => {- const parentBySequenceId = new Map<string, string | null>();-- for (const sequence of sequences) {- parentBySequenceId.set(sequence.id, sequence.parent);- }-- for (const target of targetsByKey.values()) {- const sequenceId = getSequenceId(target);- if (sequenceId !== null && !parentBySequenceId.has(sequenceId)) {- parentBySequenceId.set(sequenceId, target.sequence.parent);- }- }-- return parentBySequenceId;-};--const isAncestorTarget = ({- ancestor,- descendant,- parentBySequenceId,-}: {- readonly ancestor: SelectedOutlineLayoutTarget;- readonly descendant: SelectedOutlineLayoutTarget;- readonly parentBySequenceId: ReadonlyMap<string, string | null>;-}): boolean => {- const ancestorId = getSequenceId(ancestor);- if (ancestorId === null) {- return false;- }-- let parentId = descendant.sequence?.parent ?? null;- while (parentId !== null) {- if (parentId === ancestorId) {- return true;- }-- parentId = parentBySequenceId.get(parentId) ?? null;- }-- return false;-};--const orderOutlineGroup = ({- outlines,- parentBySequenceId,- targetsByKey,-}: {- readonly outlines: readonly SelectedOutline[];- readonly parentBySequenceId: ReadonlyMap<string, string | null>;- readonly targetsByKey: ReadonlyMap<string, SelectedOutlineLayoutTarget>;-}): readonly SelectedOutline[] => {- const incomingEdges = new Map<string, Set<string>>();- const outgoingEdges = new Map<string, Set<string>>();- const outlinesByKey = new Map(- outlines.map((outline) => [outline.key, outline]),- );-- for (const outline of outlines) {- incomingEdges.set(outline.key, new Set());- outgoingEdges.set(outline.key, new Set());- }-- const hasPath = ({- fromKey,- seen,- toKey,- }: {- readonly fromKey: string;- readonly seen: Set<string>;- readonly toKey: string;- }): boolean => {- if (fromKey === toKey) {- return true;- }-- if (seen.has(fromKey)) {- return false;- }-- seen.add(fromKey);- for (const nextKey of outgoingEdges.get(fromKey) ?? []) {- if (hasPath({fromKey: nextKey, seen, toKey})) {- return true;- }- }-- return false;- };-- const addEdge = (- before: SelectedOutline,- after: SelectedOutline,- options?: {readonly skipIfCycle: boolean},- ) => {- if (before.key === after.key) {- return;- }-- const incoming = incomingEdges.get(after.key);- const outgoing = outgoingEdges.get(before.key);- if (incoming === undefined || outgoing === undefined) {- throw new Error('Expected outline to be registered before adding edge');- }-- if (outgoing.has(after.key)) {- return;- }-- if (hasPath({fromKey: after.key, seen: new Set(), toKey: before.key})) {- if (options?.skipIfCycle) {- return;- }-- throw new Error('Could not determine a stable outline rendering order');- }-- incoming.add(before.key);- outgoing.add(after.key);- };-- const addAncestorConstraint = ({- ancestor,- descendant,- descendantContainsSelection,- equivalentHitArea,- }: {- readonly ancestor: SelectedOutline;- readonly descendant: SelectedOutline;- readonly descendantContainsSelection: boolean;- readonly equivalentHitArea: boolean;- }) => {- // Usually, children should be above parents so nested elements are directly- // selectable. If an unselected child has the same hit area as its parent, put- // it below the parent so the wrapper can be selected. Once the child or- // one of its properties is selected, keep it above the parent so it remains- // directly draggable.- if (equivalentHitArea && !descendantContainsSelection) {- addEdge(descendant, ancestor);- } else {- addEdge(ancestor, descendant);- }- };-- const outlineBySequenceId = new Map<string, SelectedOutline>();- for (const outline of outlines) {- const sequenceId = getSequenceId(targetsByKey.get(outline.key));- if (sequenceId !== null) {- outlineBySequenceId.set(sequenceId, outline);- }- }-- // Only constrain each outline against its nearest rendered ancestor. The- // resulting graph follows the sequence tree, so pairwise subpixel equivalence- // cannot introduce contradictory edges across three nested outlines.- for (const descendant of outlines) {- const descendantTarget = targetsByKey.get(descendant.key);- let parentId = descendantTarget?.sequence?.parent ?? null;-- while (parentId !== null) {- const ancestor = outlineBySequenceId.get(parentId);- if (ancestor !== undefined) {- addAncestorConstraint({- ancestor,- descendant,- descendantContainsSelection:- (descendantTarget?.selected ?? false) ||- (descendantTarget?.containsSelection ?? false),- equivalentHitArea: outlinesHaveEquivalentHitArea(- ancestor,- descendant,- ),- });- break;- }-- parentId = parentBySequenceId.get(parentId) ?? null;- }- }-- // For unrelated overlapping outlines, put broader hit targets below- // smaller ones so a large selected sequence cannot swallow clicks on- // a more specific element in the same canvas area.- const bounds = outlines.map(getOutlineBounds);- const areas = outlines.map(getOutlineHitArea);- for (let i = 0; i < outlines.length; i++) {- const a = outlines[i];- const aTarget = targetsByKey.get(a.key);- if (aTarget === undefined) {- continue;- }-- for (let j = i + 1; j < outlines.length; j++) {- if (!outlineBoundsOverlap(bounds[i], bounds[j])) {- continue;- }-- const aArea = areas[i];- const bArea = areas[j];- if (Math.abs(aArea - bArea) <= outlineAreaEqualityTolerance) {- continue;- }-- const b = outlines[j];- const bTarget = targetsByKey.get(b.key);- if (bTarget === undefined) {- continue;- }-- const aAncestorOfB = isAncestorTarget({- ancestor: aTarget,- descendant: bTarget,- parentBySequenceId,- });- const bAncestorOfA = isAncestorTarget({- ancestor: bTarget,- descendant: aTarget,- parentBySequenceId,- });-- if (aAncestorOfB || bAncestorOfA) {- continue;- }-- if (aArea > bArea) {- addEdge(a, b, {skipIfCycle: true});- } else {- addEdge(b, a, {skipIfCycle: true});- }- }- }-- const emitted = new Set<string>();- const visiting = new Set<string>();- const ordered: SelectedOutline[] = [];-- const visit = (outline: SelectedOutline) => {- if (emitted.has(outline.key)) {- return;- }-- if (visiting.has(outline.key)) {- throw new Error('Could not determine a stable outline rendering order');- }-- visiting.add(outline.key);- for (const dependencyKey of incomingEdges.get(outline.key) ?? []) {- const dependency = outlinesByKey.get(dependencyKey);- if (dependency === undefined) {- throw new Error('Expected outline dependency to exist');- }-- visit(dependency);- }-- visiting.delete(outline.key);- emitted.add(outline.key);- ordered.push(outline);- };-- for (const outline of outlines) {- visit(outline);- }-- return ordered;-};--export const orderOutlinesForRendering = ({- outlines,- sequences,- targetsByKey,-}: {- readonly outlines: readonly SelectedOutline[];- readonly sequences: readonly OutlineSequenceParent[];- readonly targetsByKey: ReadonlyMap<string, SelectedOutlineLayoutTarget>;-}): readonly SelectedOutline[] => {- const parentBySequenceId = getParentBySequenceId({sequences, targetsByKey});-- return orderOutlineGroup({- outlines,- parentBySequenceId,- targetsByKey,- });-};+export {orderCanvasOutlinesForRendering as orderOutlinesForRendering} from '@remotion/canvas';diff --git a/packages/studio/src/components/selected-outline-uv.ts b/packages/studio/src/components/selected-outline-uv.tsindex 64a6dabc4b..9889911f0e 100644--- a/packages/studio/src/components/selected-outline-uv.ts+++ b/packages/studio/src/components/selected-outline-uv.ts@@ -1,3 +1,4 @@+import {getCanvasOutlinePoint as getUvHandlePosition} from '@remotion/canvas';import type {CanUpdateSequencePropStatusKeyframed,CanUpdateSequencePropStatusStatic,@@ -11,8 +12,6 @@ import type {import {Internals} from 'remotion';import {clamp,- mix,- mixPoint,type OutlinePoint,type SelectedOutline,} from './selected-outline-geometry';@@ -21,6 +20,11 @@ import {roundToDecimalPlaces,} from './Timeline/timeline-field-utils';+export {+ getCanvasOutlinePoint as getUvHandlePosition,+ getCanvasOutlineUv as getUvCoordinateForPoint,+} from '@remotion/canvas';+export type UvCoordinate = readonly [number, number];type UvEllipseDimensions = {@@ -270,85 +274,6 @@ export const tuplesEqual = (left: unknown, right: UvCoordinate): boolean => {return left[0] === right[0] && left[1] === right[1];};-const getBilinearUvHandlePosition = (- points: SelectedOutline['points'],- uv: UvCoordinate,-): OutlinePoint => {- const [tl, tr, br, bl] = points;- const top = mixPoint(tl, tr, uv[0]);- const bottom = mixPoint(bl, br, uv[0]);- return mixPoint(top, bottom, uv[1]);-};--type ProjectiveTransform = {- readonly a: number;- readonly b: number;- readonly c: number;- readonly d: number;- readonly e: number;- readonly f: number;- readonly g: number;- readonly h: number;-};--const projectiveEpsilon = 0.000001;--const getProjectiveTransform = (- points: SelectedOutline['points'],-): ProjectiveTransform | null => {- const [tl, tr, br, bl] = points;- const dx1 = tr.x - br.x;- const dx2 = bl.x - br.x;- const dx3 = tl.x - tr.x + br.x - bl.x;- const dy1 = tr.y - br.y;- const dy2 = bl.y - br.y;- const dy3 = tl.y - tr.y + br.y - bl.y;-- let g = 0;- let h = 0;- if (Math.abs(dx3) > projectiveEpsilon || Math.abs(dy3) > projectiveEpsilon) {- const determinant = dx1 * dy2 - dx2 * dy1;- if (Math.abs(determinant) < projectiveEpsilon) {- return null;- }-- g = (dx3 * dy2 - dx2 * dy3) / determinant;- h = (dx1 * dy3 - dx3 * dy1) / determinant;- }-- return {- a: tr.x - tl.x + g * tr.x,- b: bl.x - tl.x + h * bl.x,- c: tl.x,- d: tr.y - tl.y + g * tr.y,- e: bl.y - tl.y + h * bl.y,- f: tl.y,- g,- h,- };-};--const applyProjectiveTransform = (- transform: ProjectiveTransform,- uv: UvCoordinate,-): OutlinePoint => {- const denominator = transform.g * uv[0] + transform.h * uv[1] + 1;- return {- x: (transform.a * uv[0] + transform.b * uv[1] + transform.c) / denominator,- y: (transform.d * uv[0] + transform.e * uv[1] + transform.f) / denominator,- };-};--export const getUvHandlePosition = (- points: SelectedOutline['points'],- uv: UvCoordinate,-): OutlinePoint => {- const transform = getProjectiveTransform(points);- return transform === null- ? getBilinearUvHandlePosition(points, uv)- : applyProjectiveTransform(transform, uv);-};-export const getUvHandleConnectionLines = ({handles,points,@@ -606,81 +531,6 @@ export const getUvEllipseInteractiveControls = ({return controls;};-const vectorBetween = (from: OutlinePoint, to: OutlinePoint): OutlinePoint => {- return {x: to.x - from.x, y: to.y - from.y};-};--const getBilinearUvCoordinateForPoint = (- points: SelectedOutline['points'],- point: OutlinePoint,-): UvCoordinate => {- const [tl, tr, br, bl] = points;- let u = 0.5;- let v = 0.5;-- for (let i = 0; i < 8; i++) {- const current = getBilinearUvHandlePosition(points, [u, v]);- const errorX = current.x - point.x;- const errorY = current.y - point.y;- if (Math.abs(errorX) + Math.abs(errorY) < 0.001) {- break;- }-- const du = {- x: mix(tr.x - tl.x, br.x - bl.x, v),- y: mix(tr.y - tl.y, br.y - bl.y, v),- };- const dv = vectorBetween(mixPoint(tl, tr, u), mixPoint(bl, br, u));- const determinant = du.x * dv.y - du.y * dv.x;- if (Math.abs(determinant) < 0.000001) {- break;- }-- u -= (errorX * dv.y - errorY * dv.x) / determinant;- v -= (du.x * errorY - du.y * errorX) / determinant;- }-- return [u, v];-};--export const getUvCoordinateForPoint = (- points: SelectedOutline['points'],- point: OutlinePoint,-): UvCoordinate => {- const transform = getProjectiveTransform(points);- if (transform === null) {- return getBilinearUvCoordinateForPoint(points, point);- }-- const determinant =- transform.a * (transform.e - transform.f * transform.h) -- transform.b * (transform.d - transform.f * transform.g) +- transform.c * (transform.d * transform.h - transform.e * transform.g);- if (Math.abs(determinant) < projectiveEpsilon) {- return getBilinearUvCoordinateForPoint(points, point);- }-- const inverseA = transform.e - transform.f * transform.h;- const inverseB = transform.c * transform.h - transform.b;- const inverseC = transform.b * transform.f - transform.c * transform.e;- const inverseD = transform.f * transform.g - transform.d;- const inverseE = transform.a - transform.c * transform.g;- const inverseF = transform.c * transform.d - transform.a * transform.f;- const inverseG = transform.d * transform.h - transform.e * transform.g;- const inverseH = transform.b * transform.g - transform.a * transform.h;- const inverseI = transform.a * transform.e - transform.b * transform.d;-- const denominator = inverseG * point.x + inverseH * point.y + inverseI;- if (Math.abs(denominator) < projectiveEpsilon) {- return getBilinearUvCoordinateForPoint(points, point);- }-- return [- (inverseA * point.x + inverseB * point.y + inverseC) / denominator,- (inverseD * point.x + inverseE * point.y + inverseF) / denominator,- ];-};-export function constrainUv(value: UvCoordinate,schema: UvCoordinateFieldSchema,diff --git a/packages/studio/src/helpers/use-runtime-values.ts b/packages/studio/src/helpers/use-runtime-values.tsindex 3ce91f2d95..f2fedddefb 100644--- a/packages/studio/src/helpers/use-runtime-values.ts+++ b/packages/studio/src/helpers/use-runtime-values.ts@@ -81,46 +81,4 @@ export const useRuntimeValueSelector = <T>({);};-export const useRuntimeValueSnapshots = (- controls: readonly SequenceRegistrationControls[],-): readonly Readonly<Record<string, unknown>>[] => {- const stores = useMemo(- () => controls.map((control) => control.runtimeValues),- [controls],- );- const aggregateStore = useMemo(() => {- let lastSnapshots = stores.map((store) => store.getSnapshot());- const getSnapshot = () => {- const nextSnapshots = stores.map((store) => store.getSnapshot());- if (- nextSnapshots.length === lastSnapshots.length &&- nextSnapshots.every(- (snapshot, index) => snapshot === lastSnapshots[index],- )- ) {- return lastSnapshots;- }-- lastSnapshots = nextSnapshots;- return lastSnapshots;- };-- return {- getSnapshot,- subscribe: (listener: () => void) => {- const unsubscribers = stores.map((store) => store.subscribe(listener));- return () => {- for (const unsubscribe of unsubscribers) {- unsubscribe();- }- };- },- };- }, [stores]);-- return useSyncExternalStore(- aggregateStore.subscribe,- aggregateStore.getSnapshot,- aggregateStore.getSnapshot,- );-};+export {useCanvasRuntimeValueSnapshots as useRuntimeValueSnapshots} from '@remotion/canvas';diff --git a/packages/studio/src/state/timeline-sequence-hover.ts b/packages/studio/src/state/timeline-sequence-hover.tsindex 9d9cd57d63..6add9040be 100644--- a/packages/studio/src/state/timeline-sequence-hover.ts+++ b/packages/studio/src/state/timeline-sequence-hover.ts@@ -1,62 +1,22 @@-import type {Dispatch, SetStateAction} from 'react';-import {createContext, useCallback, useContext, useMemo} from 'react';-import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';+import type {CanvasHover, CanvasHoverController} from '@remotion/canvas';import {- timelineNodePathInfoToKey,- timelineSequenceNodePathToKey,-} from '../helpers/timeline-node-path-key';-import {useSyncExternalStore} from '../helpers/use-sync-external-store';--export type TimelineSequenceHover = {- readonly key: string;- readonly nodePathKey: string;- readonly source: 'canvas' | 'timeline';-};--type TimelineSequenceHoverStore = {- readonly getSnapshot: () => TimelineSequenceHover | null;- readonly subscribe: (listener: () => void) => () => void;- readonly setHoveredSequence: Dispatch<- SetStateAction<TimelineSequenceHover | null>- >;-};--export const createTimelineSequenceHoverStore =- (): TimelineSequenceHoverStore => {- let hoveredSequence: TimelineSequenceHover | null = null;- const listeners = new Set<() => void>();+ createCanvasHoverController,+ useCanvasHover,+ useCanvasSequenceHover,+ useIsCanvasSequenceHovered,+} from '@remotion/canvas';+import {createContext, useContext} from 'react';+import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';- return {- getSnapshot: () => hoveredSequence,- subscribe: (listener) => {- listeners.add(listener);- return () => listeners.delete(listener);- },- setHoveredSequence: (action) => {- const nextHoveredSequence =- typeof action === 'function' ? action(hoveredSequence) : action;- if (nextHoveredSequence === hoveredSequence) {- return;- }+export type TimelineSequenceHover = CanvasHover;- hoveredSequence = nextHoveredSequence;- for (const listener of listeners) {- listener();- }- },- };- };+export const createTimelineSequenceHoverStore = createCanvasHoverController;export const TimelineSequenceHoverContext =- createContext<TimelineSequenceHoverStore>(createTimelineSequenceHoverStore());+ createContext<CanvasHoverController>(createTimelineSequenceHoverStore());export const useTimelineSequenceHoverState = () => {- const store = useContext(TimelineSequenceHoverContext);- return useSyncExternalStore(- store.subscribe,- store.getSnapshot,- store.getSnapshot,- );+ return useCanvasHover(useContext(TimelineSequenceHoverContext));};export const useSetTimelineSequenceHover = () => {@@ -64,73 +24,18 @@ export const useSetTimelineSequenceHover = () => {};export const useIsTimelineSequenceHovered = (nodePathKey: string | null) => {- const store = useContext(TimelineSequenceHoverContext);- const getSnapshot = useCallback(- () =>- nodePathKey !== null && store.getSnapshot()?.nodePathKey === nodePathKey,- [nodePathKey, store],+ return useIsCanvasSequenceHovered(+ useContext(TimelineSequenceHoverContext),+ nodePathKey,);-- return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);};export const useTimelineSequenceHover = (nodePathInfo: SequenceNodePathInfo | null,) => {- const store = useContext(TimelineSequenceHoverContext);- const sequenceKey = useMemo(- () =>- nodePathInfo === null- ? null- : timelineNodePathInfoToKey({...nodePathInfo, auxiliaryKeys: []}),- [nodePathInfo],- );- const nodePathKey = useMemo(- () =>- nodePathInfo === null- ? null- : timelineSequenceNodePathToKey(nodePathInfo.sequenceSubscriptionKey),- [nodePathInfo],- );- const hovered = useIsTimelineSequenceHovered(nodePathKey);-- const onPointerEnter = useCallback(() => {- if (sequenceKey === null || nodePathKey === null) {- return;- }-- store.setHoveredSequence((currentHover) => {- if (- currentHover?.key === sequenceKey &&- currentHover.nodePathKey === nodePathKey &&- currentHover.source === 'timeline'- ) {- return currentHover;- }-- return {- key: sequenceKey,- nodePathKey,- source: 'timeline',- };- });- }, [nodePathKey, sequenceKey, store]);-- const onPointerLeave = useCallback(() => {- store.setHoveredSequence((currentHover) => {- if (- currentHover?.source !== 'timeline' ||- currentHover.key !== sequenceKey- ) {- return currentHover;- }-- return null;- });- }, [sequenceKey, store]);-- return useMemo(- () => ({hovered, onPointerEnter, onPointerLeave}),- [hovered, onPointerEnter, onPointerLeave],+ return useCanvasSequenceHover(+ useContext(TimelineSequenceHoverContext),+ nodePathInfo,+ 'timeline',);};