Skip to content

πŸŽ₯ Make videos programmatically with React

Simplify SDK outlines to composition coordinates

Jonny Burger0c3fb5875450
diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsx
index 490445b6fe..f0f9d1555c 100644
--- a/packages/player/src/PlayerUI.tsx
+++ b/packages/player/src/PlayerUI.tsx
@@ -19,7 +19,10 @@ import {
calculateOuter,
calculateOuterStyle,
} from './calculate-scale.js';
-import {CanvasOverlayContext} from './canvas-overlay-context.js';
+import {
+ CanvasContentContext,
+ CanvasOverlayContext,
+} from './canvas-overlay-context.js';
import {ErrorBoundary} from './error-boundary.js';
import {RenderWarningIfBlacklist} from './license-blacklist.js';
import type {RenderMuteButton} from './MediaVolumeSlider.js';
@@ -187,6 +190,7 @@ const PlayerUI: React.ForwardRefRenderFunction<
const {playerMuted, mediaVolume} = useContext(Internals.MediaVolumeContext);
const canvasOverlay = useContext(CanvasOverlayContext);
+ const canvasContentRef = useRef<HTMLDivElement>(null);
useEffect(() => {
player.emitter.dispatchVolumeChange(mediaVolume);
@@ -336,6 +340,10 @@ const PlayerUI: React.ForwardRefRenderFunction<
}, [canvasSize, config]);
const scale = layout?.scale ?? 1;
+ const canvasContent = useMemo(
+ () => ({rootRef: canvasContentRef, scale}),
+ [scale],
+ );
const initialScaleIgnored = useRef(false);
useEffect(() => {
@@ -658,6 +666,7 @@ const PlayerUI: React.ForwardRefRenderFunction<
onDoubleClick={doubleClickToFullscreen ? handleDoubleClick : undefined}
>
<div
+ ref={canvasContentRef}
style={containerStyle}
className={playerCssClassname(overrideInternalClassName)}
>
@@ -690,7 +699,9 @@ const PlayerUI: React.ForwardRefRenderFunction<
) : null}
</div>
<RenderWarningIfBlacklist />
- {canvasOverlay}
+ <CanvasContentContext.Provider value={canvasContent}>
+ {canvasOverlay}
+ </CanvasContentContext.Provider>
</div>
{shouldShowPoster && posterFillMode === 'player-size' ? (
<div
diff --git a/packages/player/src/canvas-overlay-context.ts b/packages/player/src/canvas-overlay-context.ts
index b331993994..9648a15f9e 100644
--- a/packages/player/src/canvas-overlay-context.ts
+++ b/packages/player/src/canvas-overlay-context.ts
@@ -1,6 +1,12 @@
import {createContext} from 'react';
-import type {ReactNode} from 'react';
+import type {ReactNode, RefObject} 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);
+
+// The SDK measures against the unscaled composition and projects into the overlay.
+export const CanvasContentContext = createContext<{
+ readonly rootRef: RefObject<HTMLDivElement | null>;
+ readonly scale: number;
+} | null>(null);
diff --git a/packages/player/src/index.ts b/packages/player/src/index.ts
index 9251f4d008..88c9d59f30 100644
--- a/packages/player/src/index.ts
+++ b/packages/player/src/index.ts
@@ -1,7 +1,10 @@
import './_check-rsc.js';
import {BufferingIndicator} from './BufferingIndicator.js';
import {calculateCanvasTransformation} from './calculate-scale.js';
-import {CanvasOverlayContext} from './canvas-overlay-context.js';
+import {
+ CanvasContentContext,
+ 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';
@@ -49,4 +52,5 @@ export const PlayerInternals = {
BufferingIndicator,
TimelineSequenceObserverContext,
CanvasOverlayContext,
+ CanvasContentContext,
};
diff --git a/packages/sdk/src/canvas-internals.ts b/packages/sdk/src/canvas-internals.ts
index dc93ab2444..2e552182d7 100644
--- a/packages/sdk/src/canvas-internals.ts
+++ b/packages/sdk/src/canvas-internals.ts
@@ -14,7 +14,11 @@ export type {
CanvasOutlinePath,
CanvasOutlineTarget,
} from './outline-geometry';
-export {getCanvasOutlinePoint, getCanvasOutlineUv} from './outline-geometry';
+export {
+ getCanvasOutlinePoint,
+ getCanvasOutlineUv,
+ scaleCanvasOutline,
+} from './outline-geometry';
export {
measureCanvasOutlineTargets,
cropCanvasOutlinePoints,
diff --git a/packages/sdk/src/canvas-outline-overlay.tsx b/packages/sdk/src/canvas-outline-overlay.tsx
index 1f14d874d4..a400c85da9 100644
--- a/packages/sdk/src/canvas-outline-overlay.tsx
+++ b/packages/sdk/src/canvas-outline-overlay.tsx
@@ -17,6 +17,7 @@ import type {SequenceNodePathInfo} from './get-timeline-sequence-sort-key';
import {useCanvasHover, useCanvasSequenceHover} from './hover';
import {getKeyframeDisplayOffset} from './keyframe-frames';
import type {CanvasOutline} from './outline-geometry';
+import {scaleCanvasOutline} from './outline-geometry';
import {handleCanvasOutlinePointerDown} from './outline-interaction';
import {
getCanvasOutlineSnapTargets,
@@ -377,6 +378,8 @@ const ActiveCanvasOutlines = React.memo(
({
controller,
containerRef,
+ contentRoot,
+ scale,
selectableOutlines,
sequences,
selectableItems,
@@ -387,6 +390,8 @@ const ActiveCanvasOutlines = React.memo(
}: {
readonly controller: CanvasController;
readonly containerRef: RefObject<SVGSVGElement | null>;
+ readonly contentRoot: HTMLElement;
+ readonly scale: number;
readonly selectableOutlines: readonly CanvasSelectableOutline[];
readonly sequences: readonly TSequence[];
readonly selectableItems: readonly CanvasSelectionItem[];
@@ -505,14 +510,11 @@ const ActiveCanvasOutlines = React.memo(
);
const {outlinesForRendering, outlinesByKey, targetsByKey} =
useCanvasOutlines({
- containerRef,
- contentRoot: null,
- scale: null,
+ contentRoot,
targets,
sequences,
hoverController: controller.hover,
freezeOrder: dragging,
- updateOutlinesRef: null,
});
useLayoutEffect(() => {
measuredRef.current = {targets, outlinesByKey};
@@ -526,7 +528,7 @@ const ActiveCanvasOutlines = React.memo(
key={outline.key}
controller={controller}
containerRef={containerRef}
- outline={outline}
+ outline={scaleCanvasOutline(outline, scale)}
target={target}
selectableItems={selectableItems}
dragging={dragging}
@@ -552,6 +554,7 @@ export const CanvasOutlineOverlay = React.memo(
readonly getSequencePropStatuses: CanvasSequencePropStatusResolver | null;
}) => {
const containerRef = useRef<SVGSVGElement | null>(null);
+ const canvasContent = useContext(PlayerInternals.CanvasContentContext);
const [container, setContainer] = useState<SVGSVGElement | null>(null);
const attachContainer = useCallback((element: SVGSVGElement | null) => {
containerRef.current = element;
@@ -731,6 +734,7 @@ export const CanvasOutlineOverlay = React.memo(
},
getSelectedOutlines: () => {
const {targets, outlinesByKey} = measuredRef.current;
+ const scale = getScale();
return targets.flatMap((target) => {
if (
(!target.selected && !target.containsSelection) ||
@@ -740,7 +744,9 @@ export const CanvasOutlineOverlay = React.memo(
}
const outline = outlinesByKey.get(target.key);
- return outline === undefined ? [] : [outline];
+ return outline === undefined
+ ? []
+ : [scaleCanvasOutline(outline, scale)];
});
},
getSnapTargets: () =>
@@ -910,10 +916,14 @@ export const CanvasOutlineOverlay = React.memo(
onKeyUp={onKeyUp}
onBlur={finishNudge}
>
- {measurementActive && container !== null ? (
+ {measurementActive &&
+ container !== null &&
+ canvasContent?.rootRef.current ? (
<ActiveCanvasOutlines
controller={controller}
containerRef={containerRef}
+ contentRoot={canvasContent.rootRef.current}
+ scale={canvasContent.scale}
selectableOutlines={selectableOutlines}
sequences={sequences}
selectableItems={selectableItems}
diff --git a/packages/sdk/src/get-box-quads-polyfill-internals.js b/packages/sdk/src/get-box-quads-polyfill-internals.js
index 999474b6f4..94be7ce6b5 100644
--- a/packages/sdk/src/get-box-quads-polyfill-internals.js
+++ b/packages/sdk/src/get-box-quads-polyfill-internals.js
@@ -511,29 +511,20 @@ export function getBoxQuads(node, options) {
const range = node.ownerDocument.createRange();
range.selectNodeContents(node);
const clientRects = range.getClientRects();
- const viewportRoot =
- node.ownerDocument.documentElement ?? node.ownerDocument.body;
- const viewportRootRect = viewportRoot?.getBoundingClientRect();
-
const convertViewportRectToRelativeQuad = (rect) => {
- if (relativeTo === viewportRoot) {
- return new DOMQuad(
+ // Preserve fractional host offsets when converting text to composition
+ // coordinates, just like viewport quads from elements.
+ return convertViewportQuadToRelativeNode(
+ new DOMQuad(
new DOMPoint(rect.x, rect.y),
new DOMPoint(rect.x + rect.width, rect.y),
new DOMPoint(rect.x + rect.width, rect.y + rect.height),
new DOMPoint(rect.x, rect.y + rect.height),
- );
- }
-
- const rectInViewportRoot = new DOMRect(
- rect.x - (viewportRootRect?.x ?? 0),
- rect.y - (viewportRootRect?.y ?? 0),
- rect.width,
- rect.height,
+ ),
+ node,
+ relativeTo,
+ options?.iframes,
);
- return convertRectFromNode(relativeTo, rectInViewportRoot, viewportRoot, {
- iframes: options?.iframes,
- });
};
if (clientRects.length > 1) {
diff --git a/packages/sdk/src/get-box-quads-ponyfill.ts b/packages/sdk/src/get-box-quads-ponyfill.ts
index ba8862edb7..8b87177010 100644
--- a/packages/sdk/src/get-box-quads-ponyfill.ts
+++ b/packages/sdk/src/get-box-quads-ponyfill.ts
@@ -7,24 +7,24 @@ export type GetBoxQuadsPonyfillOptions = {
readonly relativeTo?: Element;
};
-type ElementWithNativeGetBoxQuads = Element & {
+type NodeWithNativeGetBoxQuads = (Element | Text) & {
getBoxQuads?: (options?: GetBoxQuadsPonyfillOptions) => readonly DOMQuad[];
};
const hasNativeGetBoxQuads = (
- element: Element,
-): element is ElementWithNativeGetBoxQuads => {
+ element: Element | Text,
+): element is NodeWithNativeGetBoxQuads => {
return (
- typeof (element as ElementWithNativeGetBoxQuads).getBoxQuads === 'function'
+ typeof (element as NodeWithNativeGetBoxQuads).getBoxQuads === 'function'
);
};
/**
- * Returns border/margin/padding/content box quads for an element.
+ * Returns border/margin/padding/content box quads for elements and text fragments.
* Uses the native API when available, otherwise the getBoxQuads ponyfill.
*/
export const getBoxQuadsPonyfill = (
- element: Element,
+ element: Element | Text,
options?: GetBoxQuadsPonyfillOptions,
): readonly DOMQuad[] | null => {
try {
diff --git a/packages/sdk/src/outline-geometry.ts b/packages/sdk/src/outline-geometry.ts
index 6d6f50d998..5145af816b 100644
--- a/packages/sdk/src/outline-geometry.ts
+++ b/packages/sdk/src/outline-geometry.ts
@@ -3,7 +3,7 @@ export type CanvasOutlinePoint = {
readonly y: number;
};
-/** Geometry in CSS pixels relative to the unscaled outline container. */
+/** Measured geometry uses composition pixels at scale 1, relative to its root. */
export type CanvasOutline = {
readonly key: string;
readonly dimensions: {
@@ -33,7 +33,7 @@ export type CanvasOutline = {
readonly path: CanvasOutlinePath | null;
};
-/** Affine transform mapping SVG user units to overlay container pixels. */
+/** Affine transform mapping SVG user units into the outline's coordinate space. */
export type CanvasOutlineMatrix = {
readonly a: number;
readonly b: number;
diff --git a/packages/sdk/src/outline-measurement.ts b/packages/sdk/src/outline-measurement.ts
index fee094fff3..ad91bd6019 100644
--- a/packages/sdk/src/outline-measurement.ts
+++ b/packages/sdk/src/outline-measurement.ts
@@ -14,12 +14,15 @@ import {getCanvasOutlineNodes} from './outline-nodes';
const rectToPoints = (
elementRect: DOMRect,
- containerRect: DOMRect,
+ contentRoot: HTMLElement,
+ rootRect: 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;
+ const scaleX = rootRect.width / contentRoot.offsetWidth;
+ const scaleY = rootRect.height / contentRoot.offsetHeight;
+ const left = (elementRect.left - rootRect.left) / scaleX;
+ const top = (elementRect.top - rootRect.top) / scaleY;
+ const right = (elementRect.right - rootRect.left) / scaleX;
+ const bottom = (elementRect.bottom - rootRect.top) / scaleY;
return [
{x: left, y: top},
@@ -65,36 +68,15 @@ export const getTransformedSvgViewportPoints = ({
];
};
-const quadToPoints = (
- quad: DOMQuad,
- containerRect: Pick<DOMRect, 'left' | 'top'>,
- scale: number,
-): CanvasOutline['points'] => {
+const quadToPoints = (quad: DOMQuad): CanvasOutline['points'] => {
return [
- {
- x: (quad.p1.x - containerRect.left) * scale,
- y: (quad.p1.y - containerRect.top) * scale,
- },
- {
- x: (quad.p2.x - containerRect.left) * scale,
- y: (quad.p2.y - containerRect.top) * scale,
- },
- {
- x: (quad.p3.x - containerRect.left) * scale,
- y: (quad.p3.y - containerRect.top) * scale,
- },
- {
- x: (quad.p4.x - containerRect.left) * scale,
- y: (quad.p4.y - containerRect.top) * scale,
- },
+ {x: quad.p1.x, y: quad.p1.y},
+ {x: quad.p2.x, y: quad.p2.y},
+ {x: quad.p3.x, y: quad.p3.y},
+ {x: quad.p4.x, y: quad.p4.y},
];
};
-type OutlineCoordinateSpace = {
- readonly root: Element;
- readonly scale: number;
-};
-
const isSvgSvgElement = (element: Element): element is SVGSVGElement => {
const ownerSvgSvgElement = element.ownerDocument.defaultView?.SVGSVGElement;
return (
@@ -124,7 +106,7 @@ const getSvgSvgElementViewport = (element: SVGSVGElement): SvgViewport => {
};
/**
- * Maps SVG user units to viewport pixels, including ancestor CSS transforms.
+ * Maps SVG user units to composition pixels, including ancestor CSS transforms.
*
* `getScreenCTM()` is the obvious API for this, but WebKit ignores ancestor
* CSS transforms in it, while Blink/Gecko include them β€” so a Studio fit-scale
@@ -132,28 +114,20 @@ const getSvgSvgElementViewport = (element: SVGSVGElement): SvgViewport => {
* we compose two consistent transforms:
*
* 1. The polyfill's CSS-transform-aware walk from the root <svg> to the
- * composition root, or to the document element for viewport measurements.
+ * composition root.
* 2. `element.getCTM()`, the pure SVG-internal transform from user units to
* the root <svg>'s viewport. Cross-browser consistent, no CSS involved.
*
- * Composition coordinates are projected to the host scale. Viewport measurements
- * subtract window scroll and the overlay origin, like HTML quads.
*/
-const getSvgElementScreenMatrix = (
+const getSvgElementCompositionMatrix = (
element: SVGGraphicsElement,
- coordinateSpace: OutlineCoordinateSpace | null,
+ contentRoot: HTMLElement,
): (SvgScreenCtm & {readonly is2D: boolean}) | null => {
const ownerSvg = element.ownerSVGElement;
- const {documentElement} = element.ownerDocument;
- const win = element.ownerDocument.defaultView;
- if (!ownerSvg || !documentElement || !win) {
+ if (!ownerSvg) {
return null;
}
- const relativeSpace = coordinateSpace?.root.contains(ownerSvg)
- ? coordinateSpace
- : null;
-
let walk;
try {
// Walking the root <svg> (not the path) avoids the polyfill's
@@ -161,7 +135,7 @@ const getSvgElementScreenMatrix = (
// coordinates by the path's `getBBox()` origin.
walk = getResultingTransformationBetweenElementAndAllAncestors(
ownerSvg,
- relativeSpace?.root ?? documentElement,
+ contentRoot,
[],
);
} catch {
@@ -174,55 +148,25 @@ const getSvgElementScreenMatrix = (
return null;
}
- const matrix = walk.multiply(ctm);
- const scrollX =
- relativeSpace === null
- ? (win.scrollX ?? documentElement.scrollLeft ?? 0)
- : 0;
- const scrollY =
- relativeSpace === null
- ? (win.scrollY ?? documentElement.scrollTop ?? 0)
- : 0;
- const scale = relativeSpace?.scale ?? 1;
-
- return {
- a: matrix.a * scale,
- b: matrix.b * scale,
- c: matrix.c * scale,
- d: matrix.d * scale,
- e: matrix.e * scale - scrollX,
- f: matrix.f * scale - scrollY,
- is2D: matrix.is2D,
- };
+ return walk.multiply(ctm);
};
const getSvgSvgElementOutlinePoints = (
element: SVGSVGElement,
- containerRect: DOMRect,
- coordinateSpace: OutlineCoordinateSpace | null,
+ contentRoot: HTMLElement,
): CanvasOutline['points'] | null => {
const viewport = getSvgSvgElementViewport(element);
if (viewport.width === 0 && viewport.height === 0) {
return null;
}
- const {documentElement} = element.ownerDocument;
- const win = element.ownerDocument.defaultView;
- if (!documentElement || !win) {
- return null;
- }
-
- const relativeSpace = coordinateSpace?.root.contains(element)
- ? coordinateSpace
- : null;
-
let walk;
try {
// The walk on the root <svg> includes its own CSS transform, unlike
// `getScreenCTM()` in WebKit, which ignores ancestor CSS transforms.
walk = getResultingTransformationBetweenElementAndAllAncestors(
element,
- relativeSpace?.root ?? documentElement,
+ contentRoot,
[],
);
} catch {
@@ -286,27 +230,17 @@ const getSvgSvgElementOutlinePoints = (
translateY -= viewBox.y * scaleY;
}
- const scrollX =
- relativeSpace === null
- ? (win.scrollX ?? documentElement.scrollLeft ?? 0)
- : 0;
- const scrollY =
- relativeSpace === null
- ? (win.scrollY ?? documentElement.scrollTop ?? 0)
- : 0;
- const scale = relativeSpace?.scale ?? 1;
-
return getTransformedSvgViewportPoints({
viewport,
ctm: {
- a: walk.a * scaleX * scale,
- b: walk.b * scaleX * scale,
- c: walk.c * scaleY * scale,
- d: walk.d * scaleY * scale,
- e: (walk.a * translateX + walk.c * translateY + walk.e) * scale - scrollX,
- f: (walk.b * translateX + walk.d * translateY + walk.f) * scale - scrollY,
+ a: walk.a * scaleX,
+ b: walk.b * scaleX,
+ c: walk.c * scaleY,
+ d: walk.d * scaleY,
+ e: walk.a * translateX + walk.c * translateY + walk.e,
+ f: walk.b * translateX + walk.d * translateY + walk.f,
},
- containerRect: relativeSpace === null ? containerRect : {left: 0, top: 0},
+ containerRect: {left: 0, top: 0},
});
};
@@ -322,28 +256,23 @@ const isSvgPathElement = (element: Element): element is SVGPathElement => {
const getPathOutline = ({
element,
- containerRect,
- coordinateSpace,
+ contentRoot,
}: {
readonly element: SVGPathElement;
- readonly containerRect: DOMRect;
- readonly coordinateSpace: OutlineCoordinateSpace | null;
+ readonly contentRoot: HTMLElement;
}): CanvasOutlinePath | null => {
const d = element.getAttribute('d');
if (d === null || d === '') {
return null;
}
- const ctm = getSvgElementScreenMatrix(element, coordinateSpace);
+ const ctm = getSvgElementCompositionMatrix(element, contentRoot);
if (ctm === null || !ctm.is2D) {
// In a 3D CSS context no single 2D matrix can place the path; the
// polygon outline from `getBoxQuads` still applies.
return null;
}
- const origin = coordinateSpace?.root.contains(element.ownerSVGElement)
- ? {left: 0, top: 0}
- : containerRect;
return {
d,
matrix: {
@@ -351,8 +280,8 @@ const getPathOutline = ({
b: ctm.b,
c: ctm.c,
d: ctm.d,
- e: ctm.e - origin.left,
- f: ctm.f - origin.top,
+ e: ctm.e,
+ f: ctm.f,
},
};
};
@@ -360,39 +289,30 @@ const getPathOutline = ({
const getElementOutlinePoints = (
element: Element,
elementRect: DOMRect,
- containerRect: DOMRect,
- coordinateSpace: OutlineCoordinateSpace | null,
+ contentRoot: HTMLElement,
+ rootRect: DOMRect,
): CanvasOutline['points'] | null => {
if (elementRect.width === 0 && elementRect.height === 0) {
return null;
}
if (isSvgSvgElement(element)) {
- return getSvgSvgElementOutlinePoints(
- element,
- containerRect,
- coordinateSpace,
- );
+ return getSvgSvgElementOutlinePoints(element, contentRoot);
}
// The composition root is a real ancestor. Measuring relative to it avoids
// rounding the host's fractional viewport offsets before normalization.
// The sibling overlay cannot be used as `relativeTo` by the ponyfill.
- const relativeTo = coordinateSpace?.root.contains(element)
- ? coordinateSpace.root
- : null;
- const quads = getBoxQuadsPonyfill(
- element,
- relativeTo === null ? {box: 'border'} : {box: 'border', relativeTo},
- );
+ const quads = getBoxQuadsPonyfill(element, {
+ box: 'border',
+ relativeTo: contentRoot,
+ });
const quad = quads?.[0];
if (!quad) {
- return rectToPoints(elementRect, containerRect);
+ return rectToPoints(elementRect, contentRoot, rootRect);
}
- return relativeTo === null
- ? quadToPoints(quad, containerRect, 1)
- : quadToPoints(quad, {left: 0, top: 0}, coordinateSpace?.scale ?? 1);
+ return quadToPoints(quad);
};
export const cropCanvasOutlinePoints = (
@@ -421,26 +341,27 @@ export const cropCanvasOutlinePoints = (
};
/**
- * Measures a batch against an unscaled overlay container. Shared ancestor
- * transforms are cached only within this measurement batch.
+ * Measures a batch in composition pixels at scale 1. Shared ancestor transforms
+ * are cached only within this measurement batch.
*/
export const measureCanvasOutlineTargets = (
- container: Element,
+ contentRoot: HTMLElement,
targets: readonly CanvasOutlineTarget[],
- coordinateSpace: OutlineCoordinateSpace | null,
): CanvasOutline[] => {
- // Reuse shared ancestor geometry within this synchronous batch only.
resetBoxQuadsCache();
- const containerRect = container.getBoundingClientRect();
+ const rootRect = contentRoot.getBoundingClientRect();
const outlines: CanvasOutline[] = [];
- const rects = new Map<Element | Text, DOMRect>();
+ if (rootRect.width === 0 || rootRect.height === 0) {
+ return outlines;
+ }
+
const geometry = new Map<
- Element,
- Pick<CanvasOutline, 'dimensions' | 'path'> & {
- uncroppedPoints: CanvasOutline['points'];
- }
+ Element | Text,
+ | (Pick<CanvasOutline, 'dimensions' | 'path'> & {
+ uncroppedPoints: CanvasOutline['points'];
+ })
+ | null
>();
-
for (const target of targets) {
const nodes = getCanvasOutlineNodes(target.ref);
let left = Infinity;
@@ -449,47 +370,113 @@ export const measureCanvasOutlineTargets = (
let bottom = -Infinity;
const measurableNodes: (Element | Text)[] = [];
for (const node of nodes) {
- let rect = rects.get(node);
- if (rect === undefined) {
+ if (!geometry.has(node)) {
if (node.nodeType === 3) {
- const range = node.ownerDocument.createRange();
- range.selectNodeContents(node);
- rect = range.getBoundingClientRect();
+ const text = node as Text;
+ const quads = getBoxQuadsPonyfill(text, {relativeTo: contentRoot});
+ let textPoints = quads?.flatMap(quadToPoints) ?? [];
+ if (textPoints.length === 0) {
+ const range = text.ownerDocument.createRange();
+ range.selectNodeContents(text);
+ const rect = range.getBoundingClientRect();
+ if (rect.width !== 0 || rect.height !== 0) {
+ textPoints = [...rectToPoints(rect, contentRoot, rootRect)];
+ }
+ }
+
+ if (textPoints.length === 0) {
+ geometry.set(node, null);
+ } else {
+ const xs = textPoints.map((point) => point.x);
+ const ys = textPoints.map((point) => point.y);
+ const textLeft = Math.min(...xs);
+ const textTop = Math.min(...ys);
+ const textRight = Math.max(...xs);
+ const textBottom = Math.max(...ys);
+ geometry.set(
+ node,
+ textLeft === textRight && textTop === textBottom
+ ? null
+ : {
+ dimensions: null,
+ path: null,
+ uncroppedPoints: [
+ {x: textLeft, y: textTop},
+ {x: textRight, y: textTop},
+ {x: textRight, y: textBottom},
+ {x: textLeft, y: textBottom},
+ ],
+ },
+ );
+ }
} else {
- rect = (node as Element).getBoundingClientRect();
+ const element = node as Element;
+ const uncroppedPoints = getElementOutlinePoints(
+ element,
+ element.getBoundingClientRect(),
+ contentRoot,
+ rootRect,
+ );
+ const ownerHTMLElement =
+ element.ownerDocument.defaultView?.HTMLElement;
+ geometry.set(
+ node,
+ uncroppedPoints === null
+ ? null
+ : {
+ dimensions:
+ ownerHTMLElement !== undefined &&
+ element instanceof ownerHTMLElement
+ ? {
+ width: element.offsetWidth,
+ height: element.offsetHeight,
+ }
+ : isSvgSvgElement(element)
+ ? {
+ width: element.width.baseVal.value,
+ height: element.height.baseVal.value,
+ }
+ : null,
+ path: isSvgPathElement(element)
+ ? getPathOutline({element, contentRoot})
+ : null,
+ uncroppedPoints,
+ },
+ );
}
-
- rects.set(node, rect);
}
- if (rect.width === 0 && rect.height === 0) {
+ const measured = geometry.get(node);
+ if (!measured) {
continue;
}
measurableNodes.push(node);
- left = Math.min(left, rect.left);
- top = Math.min(top, rect.top);
- right = Math.max(right, rect.right);
- bottom = Math.max(bottom, rect.bottom);
+ for (const point of measured.uncroppedPoints) {
+ left = Math.min(left, point.x);
+ top = Math.min(top, point.y);
+ right = Math.max(right, point.x);
+ bottom = Math.max(bottom, point.y);
+ }
}
if (
measurableNodes.length === 0 ||
(!target.includeOutsideContainer &&
- (right <= containerRect.left ||
- left >= containerRect.right ||
- bottom <= containerRect.top ||
- top >= containerRect.bottom))
+ (right <= 0 ||
+ left >= contentRoot.offsetWidth ||
+ bottom <= 0 ||
+ top >= contentRoot.offsetHeight))
) {
continue;
}
if (measurableNodes.length !== 1 || measurableNodes[0].nodeType !== 1) {
const groupPoints: CanvasOutline['points'] = [
- {x: left - containerRect.left, y: top - containerRect.top},
- {x: right - containerRect.left, y: top - containerRect.top},
- {x: right - containerRect.left, y: bottom - containerRect.top},
- {x: left - containerRect.left, y: bottom - containerRect.top},
+ {x: left, y: top},
+ {x: right, y: top},
+ {x: right, y: bottom},
+ {x: left, y: bottom},
];
outlines.push({
key: target.key,
@@ -501,59 +488,19 @@ export const measureCanvasOutlineTargets = (
continue;
}
- const element = measurableNodes[0] as Element;
- const cached = geometry.get(element);
- if (cached) {
- outlines.push({
- ...cached,
- key: target.key,
- points: cropCanvasOutlinePoints(cached.uncroppedPoints, target.crop),
- });
- continue;
- }
-
- const elementRect = rects.get(element);
- if (elementRect === undefined) {
- throw new Error('Expected a measured outline element');
- }
-
- const uncroppedPoints = getElementOutlinePoints(
- element,
- elementRect,
- containerRect,
- coordinateSpace,
- );
- if (uncroppedPoints === null) {
- continue;
+ const elementGeometry = geometry.get(measurableNodes[0]);
+ if (!elementGeometry) {
+ throw new Error('Expected measured outline geometry');
}
- const points = cropCanvasOutlinePoints(uncroppedPoints, target.crop);
- const ownerHTMLElement = element.ownerDocument.defaultView?.HTMLElement;
-
- const path = isSvgPathElement(element)
- ? getPathOutline({element, containerRect, coordinateSpace})
- : null;
-
- const measured = {
- 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,
- path,
- };
- geometry.set(element, measured);
- outlines.push({...measured, key: target.key, points});
+ outlines.push({
+ ...elementGeometry,
+ key: target.key,
+ points: cropCanvasOutlinePoints(
+ elementGeometry.uncroppedPoints,
+ target.crop,
+ ),
+ });
}
return outlines;
diff --git a/packages/sdk/src/use-canvas-outline-measurements.ts b/packages/sdk/src/use-canvas-outline-measurements.ts
index 4df5f537ef..3275df864d 100644
--- a/packages/sdk/src/use-canvas-outline-measurements.ts
+++ b/packages/sdk/src/use-canvas-outline-measurements.ts
@@ -1,8 +1,6 @@
-import type {MutableRefObject, RefObject} from 'react';
-import {useCallback, useLayoutEffect, useMemo, useRef, useState} from 'react';
+import {useCallback, useLayoutEffect, useRef, useState} from 'react';
import {Internals} from 'remotion';
import type {CanvasOutline, CanvasOutlineTarget} from './outline-geometry';
-import {scaleCanvasOutline} from './outline-geometry';
import {
canvasOutlinesAreEqual,
measureCanvasOutlineTargets,
@@ -10,19 +8,12 @@ import {
import {getCanvasOutlineNodes} from './outline-nodes';
export const useCanvasOutlineMeasurements = ({
- containerRef,
contentRoot,
- scale,
targets,
- updateOutlinesRef,
}: {
- readonly containerRef: RefObject<SVGSVGElement | null>;
/** Observe composition mutations separately from the host's zoom and pan. */
- readonly contentRoot: Element | null;
- /** Cache at scale 1 and project to this scale. Null keeps container coordinates. */
- readonly scale: number | null;
+ readonly contentRoot: HTMLElement;
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.
@@ -34,15 +25,8 @@ export const useCanvasOutlineMeasurements = ({
const measuredNodesRef = useRef(
new Map<CanvasOutlineTarget['ref'], readonly (Element | Text)[] | null>(),
);
- const scaleRef = useRef(scale);
- const normalize = scale !== null;
- useLayoutEffect(() => {
- scaleRef.current = scale;
- }, [scale]);
-
const updateOutlines = useCallback(() => {
- const container = containerRef.current;
- if (container === null || targets.length === 0) {
+ if (targets.length === 0) {
resizeObserverRef.current?.disconnect();
observedElementsRef.current = new Set();
if (outlinesRef.current.length === 0) {
@@ -57,10 +41,8 @@ export const useCanvasOutlineMeasurements = ({
const resizeObserver = resizeObserverRef.current;
if (resizeObserver !== null) {
const nextObservedElements = new Set<Element>();
- // Zoom changes the overlay's CSS size, but only transforms its contents.
- if (!normalize) {
- nextObservedElements.add(container);
- }
+ // The composition's layout size is independent of its host's zoom.
+ nextObservedElements.add(contentRoot);
for (const target of targets) {
for (const node of getCanvasOutlineNodes(target.ref)) {
@@ -87,20 +69,7 @@ export const useCanvasOutlineMeasurements = ({
observedElementsRef.current = nextObservedElements;
}
- const measurementScale = scaleRef.current;
- const measuredOutlines = measureCanvasOutlineTargets(
- container,
- targets,
- contentRoot === null || measurementScale === null
- ? null
- : {root: contentRoot, scale: measurementScale},
- );
- const nextOutlines =
- measurementScale === null
- ? measuredOutlines
- : measuredOutlines.map((outline) =>
- scaleCanvasOutline(outline, 1 / measurementScale),
- );
+ const nextOutlines = measureCanvasOutlineTargets(contentRoot, targets);
measuredNodesRef.current = new Map(
targets.map((target) => [
target.ref,
@@ -113,13 +82,10 @@ export const useCanvasOutlineMeasurements = ({
outlinesRef.current = nextOutlines;
setOutlines(nextOutlines);
- }, [containerRef, contentRoot, normalize, targets]);
+ }, [contentRoot, targets]);
useLayoutEffect(() => {
latestUpdateRef.current = updateOutlines;
- if (updateOutlinesRef !== null) {
- updateOutlinesRef.current = updateOutlines;
- }
// The Fiber observer discovers nodes after React's layout effects. Batch
// automatic measurements after that commit, including child-only updates
@@ -135,10 +101,9 @@ export const useCanvasOutlineMeasurements = ({
});
};
- const ownerWindow = containerRef.current?.ownerDocument.defaultView;
+ const ownerWindow = contentRoot.ownerDocument.defaultView;
const onCommit = () => {
if (
- !normalize ||
targets.some(
(target) =>
measuredNodesRef.current.get(target.ref) !==
@@ -171,14 +136,11 @@ export const useCanvasOutlineMeasurements = ({
onCommit,
);
latestUpdateRef.current = () => undefined;
- if (updateOutlinesRef?.current === updateOutlines) {
- updateOutlinesRef.current = () => undefined;
- }
};
- }, [containerRef, normalize, targets, updateOutlines, updateOutlinesRef]);
+ }, [contentRoot, targets, updateOutlines]);
useLayoutEffect(() => {
- const ownerWindow = containerRef.current?.ownerDocument.defaultView;
+ const ownerWindow = contentRoot.ownerDocument.defaultView;
if (!ownerWindow || typeof ownerWindow.ResizeObserver === 'undefined') {
return;
}
@@ -206,23 +168,26 @@ export const useCanvasOutlineMeasurements = ({
resizeObserverRef.current = null;
observedElementsRef.current = new Set();
};
- }, [containerRef]);
+ }, [contentRoot]);
useLayoutEffect(() => {
- const ownerWindow = contentRoot?.ownerDocument.defaultView;
- if (
- contentRoot === null ||
- !ownerWindow ||
- typeof ownerWindow.MutationObserver === 'undefined'
- ) {
+ const ownerWindow = contentRoot.ownerDocument.defaultView;
+ if (!ownerWindow || typeof ownerWindow.MutationObserver === 'undefined') {
return;
}
- // Includes descendant-only edits and transforms on composition ancestors,
- // while the host's scaled container and the outline overlay stay outside.
- const observer = new ownerWindow.MutationObserver(() =>
- latestUpdateRef.current(),
- );
+ // The host owns the root's zoom transform. Its layout size is observed
+ // separately; only composition mutations change normalized geometry.
+ const observer = new ownerWindow.MutationObserver((mutations) => {
+ if (
+ mutations.some(
+ (mutation) =>
+ mutation.target !== contentRoot || mutation.type !== 'attributes',
+ )
+ ) {
+ latestUpdateRef.current();
+ }
+ });
observer.observe(contentRoot, {
attributes: true,
characterData: true,
@@ -232,12 +197,5 @@ export const useCanvasOutlineMeasurements = ({
return () => observer.disconnect();
}, [contentRoot]);
- // Controls keep using overlay pixels, while zoom only projects cached geometry.
- return useMemo(
- () =>
- scale === null
- ? outlines
- : outlines.map((outline) => scaleCanvasOutline(outline, scale)),
- [outlines, scale],
- );
+ return outlines;
};
diff --git a/packages/sdk/src/use-canvas-outlines.ts b/packages/sdk/src/use-canvas-outlines.ts
index dda4f13667..713a09c40f 100644
--- a/packages/sdk/src/use-canvas-outlines.ts
+++ b/packages/sdk/src/use-canvas-outlines.ts
@@ -1,4 +1,3 @@
-import type {MutableRefObject, RefObject} from 'react';
import {useEffect, useMemo, useRef} from 'react';
import type {SequenceNodePathInfo} from './get-timeline-sequence-sort-key';
import type {CanvasHoverController} from './hover';
@@ -23,23 +22,17 @@ export type CanvasOutlineRenderTarget = Omit<
/** Measure and order a host's outline targets without subscribing to playback. */
export const useCanvasOutlines = <Target extends CanvasOutlineRenderTarget>({
- containerRef,
contentRoot,
- scale,
targets,
sequences,
hoverController,
freezeOrder,
- updateOutlinesRef,
}: {
- readonly containerRef: RefObject<SVGSVGElement | null>;
- readonly contentRoot: Element | null;
- readonly scale: number | null;
+ readonly contentRoot: HTMLElement;
readonly targets: readonly Target[];
readonly sequences: readonly CanvasOutlineSequenceParent[];
readonly hoverController: CanvasHoverController;
readonly freezeOrder: boolean;
- readonly updateOutlinesRef: MutableRefObject<() => void> | null;
}) => {
const hover = useCanvasHover(hoverController);
const hoveredNodePathKey = hover?.nodePathKey ?? null;
@@ -60,11 +53,8 @@ export const useCanvasOutlines = <Target extends CanvasOutlineRenderTarget>({
[hoveredTimelineNodePathKey, targets],
);
const outlines = useCanvasOutlineMeasurements({
- containerRef,
contentRoot,
- scale,
targets: measurementTargets,
- updateOutlinesRef,
});
const targetsByKey = useMemo(
() => new Map(targets.map((target) => [target.key, target])),
diff --git a/packages/studio/src/components/SelectedOutlineRenderer.tsx b/packages/studio/src/components/SelectedOutlineRenderer.tsx
index 2f83b3d090..ccf36bb548 100644
--- a/packages/studio/src/components/SelectedOutlineRenderer.tsx
+++ b/packages/studio/src/components/SelectedOutlineRenderer.tsx
@@ -36,7 +36,7 @@ import type {
TimelineSelectionInteraction,
} from './Timeline/TimelineSelection';
-const {useCanvasOutlines} = CanvasInternals;
+const {useCanvasOutlines, scaleCanvasOutline} = CanvasInternals;
const outlineContainer: React.CSSProperties = {
position: 'absolute',
@@ -120,14 +120,11 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
targetsByKey,
hoveredNodePathKey,
} = useCanvasOutlines({
- containerRef: overlayRef,
contentRoot: Internals.portalNode(),
- scale,
targets: outlineTargets,
sequences,
hoverController,
freezeOrder: dragging,
- updateOutlinesRef: null,
});
const {
outlinesForEditingHandles,
@@ -169,10 +166,12 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
}, [hoveredNodePathKey, outlinesForRendering, targetsByKey]);
const targetsRef = useRef(outlineTargets);
const outlinesByKeyRef = useRef(outlinesByKey);
+ const scaleRef = useRef(scale);
useLayoutEffect(() => {
targetsRef.current = outlineTargets;
outlinesByKeyRef.current = outlinesByKey;
- }, [outlineTargets, outlinesByKey]);
+ scaleRef.current = scale;
+ }, [outlineTargets, outlinesByKey, scale]);
const getAllDragOutlines = useCallback(
() =>
targetsRef.current.flatMap((target) => {
@@ -184,7 +183,9 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
}
const outline = outlinesByKeyRef.current.get(target.key);
- return outline === undefined ? [] : [outline];
+ return outline === undefined
+ ? []
+ : [scaleCanvasOutline(outline, scaleRef.current)];
}),
[getLatestOutlineTargetByKey],
);
@@ -257,7 +258,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
getAllDragOutlines={getAllDragOutlines}
getAllDragTargets={getAllDragTargets}
getLatestTargetByKey={getLatestOutlineTargetByKey}
- outline={outline}
+ outline={scaleCanvasOutline(outline, scale)}
onDraggingChange={onDraggingChange}
onSnapPointsChange={onSnapPointsChange}
onSelect={onSelect}
@@ -274,7 +275,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
return outline.path !== null && pathDrag !== null ? (
<SelectedOutlinePathPoints
key={`${outline.key}-path-points`}
- outline={outline}
+ outline={scaleCanvasOutline(outline, scale)}
pathDrag={pathDrag}
onDraggingChange={onDraggingChange}
/>
@@ -293,14 +294,14 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
onContextMenuOpenChange={onContextMenuOpenChange}
onDraggingChange={onDraggingChange}
onSelect={onSelect}
- outline={outline}
+ outline={scaleCanvasOutline(outline, scale)}
/>
))}
{/* Keep UV controls above every transparent outline polygon so SVG hit-testing reaches the handles first. */}
{outlinesForUvHandles.map((outline) => (
<SelectedOutlineUvHandleConnectionLayer
key={`${outline.key}-uv-connection-lines`}
- outline={outline}
+ outline={scaleCanvasOutline(outline, scale)}
layoutTarget={targetsByKey.get(outline.key)}
/>
))}
@@ -309,7 +310,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
key={`${outline.key}-uv-handles`}
onDraggingChange={onDraggingChange}
onSelect={onSelect}
- outline={outline}
+ outline={scaleCanvasOutline(outline, scale)}
layoutTarget={targetsByKey.get(outline.key)}
/>
))}
@@ -317,7 +318,7 @@ const SelectedOutlineRendererUnmemoized: React.FC<{
{outlinesForTransformOrigin.map((outline) => (
<SelectedOutlineTransformOriginHandle
key={`${outline.key}-transform-origin`}
- outline={outline}
+ outline={scaleCanvasOutline(outline, scale)}
onDraggingChange={onDraggingChange}
getLatestTargetByKey={getLatestOutlineTargetByKey}
layoutTarget={targetsByKey.get(outline.key)}
diff --git a/packages/studio/src/components/WebMcp.tsx b/packages/studio/src/components/WebMcp.tsx
index cbff84a963..20ed154152 100644
--- a/packages/studio/src/components/WebMcp.tsx
+++ b/packages/studio/src/components/WebMcp.tsx
@@ -1086,19 +1086,7 @@ export const WebMcp: FC = () => {
});
const portalNode = Internals.portalNode();
const portalRect = portalNode.getBoundingClientRect();
- const metadata = currentCompositionMetadataRef.current;
- const compositionWidth =
- metadata?.width ?? composition.width ?? portalNode.offsetWidth;
- const compositionHeight =
- metadata?.height ?? composition.height ?? portalNode.offsetHeight;
- const scaleX = portalRect.width / compositionWidth;
- const scaleY = portalRect.height / compositionHeight;
- if (
- !Number.isFinite(scaleX) ||
- scaleX === 0 ||
- !Number.isFinite(scaleY) ||
- scaleY === 0
- ) {
+ if (portalRect.width === 0 || portalRect.height === 0) {
throw new Error('The Studio canvas is not ready to be measured.');
}
@@ -1116,7 +1104,6 @@ export const WebMcp: FC = () => {
includeOutsideContainer: true,
};
}),
- null,
);
const measurementsByKey = new Map(
measuredOutlines.map((outline) => [outline.key, outline]),
@@ -1151,10 +1138,7 @@ export const WebMcp: FC = () => {
outline.sequence.displayName ||
outline.sequence.controls?.componentName ||
null;
- const points = measurement.points.map((point) => ({
- x: point.x / scaleX,
- y: point.y / scaleY,
- }));
+ const {points} = measurement;
const xValues = points.map((point) => point.x);
const yValues = points.map((point) => point.y);
const left = Math.min(...xValues);