Skip to content

🎥 Make videos programmatically with React

Speed up canvas controls for large selections

Jonny Burger8b66ca61d857
diff --git a/packages/sdk/src/canvas-internals.ts b/packages/sdk/src/canvas-internals.ts
index 6f027858ca..8f0b0ee165 100644
--- a/packages/sdk/src/canvas-internals.ts
+++ b/packages/sdk/src/canvas-internals.ts
@@ -41,6 +41,7 @@ export {
} from './outline-interaction';
export type {CanvasOutlinePointerDownDecision} from './outline-interaction';
export {
+ createCanvasOutlineLayoutTargetLookup,
getCanvasActiveOutlineTargets,
getCanvasOutlineActivity,
getCanvasOutlineLayoutTargets,
diff --git a/packages/sdk/src/outline-targets.ts b/packages/sdk/src/outline-targets.ts
index 304513f879..2809a0ed86 100644
--- a/packages/sdk/src/outline-targets.ts
+++ b/packages/sdk/src/outline-targets.ts
@@ -157,18 +157,16 @@ export const getCanvasActiveOutlineTargets = <
);
};
-/** Builds selection state shared by canvas instances of the same source. */
-export const getCanvasOutlineLayoutTargets = ({
+/** Reuse source metadata within one immutable outline and selection snapshot. */
+export const createCanvasOutlineLayoutTargetLookup = ({
selectableOutlines,
selectedSequenceKeys,
sequenceKeysContainingSelection,
- targetKey,
}: {
readonly selectableOutlines: readonly CanvasSelectableOutline[];
readonly selectedSequenceKeys: ReadonlySet<string>;
readonly sequenceKeysContainingSelection: ReadonlySet<string>;
- readonly targetKey: string | null;
-}): CanvasOutlineLayoutTarget[] => {
+}) => {
const firstNodePathInfoBySourceNode = new Map<string, SequenceNodePathInfo>();
const selectedSourceNodeKeys = new Set<string>();
for (const {key, nodePathInfo} of selectableOutlines) {
@@ -185,28 +183,40 @@ export const getCanvasOutlineLayoutTargets = ({
}
}
- return selectableOutlines.flatMap((selectableOutline) => {
- const {key, nodePathInfo, sequence} = selectableOutline;
- if (targetKey !== null && targetKey !== key) {
- return [];
- }
+ let outlinesByKey: Map<string, CanvasSelectableOutline[]> | null = null;
+ return (targetKey: string | null): CanvasOutlineLayoutTarget[] => {
+ let matchingOutlines = selectableOutlines;
+ if (targetKey !== null) {
+ // Bulk layout calculations do not need a per-key index.
+ if (outlinesByKey === null) {
+ outlinesByKey = new Map();
+ for (const outline of selectableOutlines) {
+ const matches = outlinesByKey.get(outline.key) ?? [];
+ matches.push(outline);
+ outlinesByKey.set(outline.key, matches);
+ }
+ }
- if (sequence.refForOutline === null) {
- throw new Error('Expected sequence to have a ref for outline');
+ matchingOutlines = outlinesByKey.get(targetKey) ?? [];
}
- const nodePathKey = timelineSequenceNodePathToKey(
- nodePathInfo.sequenceSubscriptionKey,
- );
- const selectionNodePathInfo =
- firstNodePathInfoBySourceNode.get(nodePathKey);
- if (selectionNodePathInfo === undefined) {
- throw new Error('Expected a first sequence for the source node');
- }
+ return matchingOutlines.map((selectableOutline) => {
+ const {key, nodePathInfo, sequence} = selectableOutline;
+ if (sequence.refForOutline === null) {
+ throw new Error('Expected sequence to have a ref for outline');
+ }
+
+ const nodePathKey = timelineSequenceNodePathToKey(
+ nodePathInfo.sequenceSubscriptionKey,
+ );
+ const selectionNodePathInfo =
+ firstNodePathInfoBySourceNode.get(nodePathKey);
+ if (selectionNodePathInfo === undefined) {
+ throw new Error('Expected a first sequence for the source node');
+ }
- const containsSelection = sequenceKeysContainingSelection.has(key);
- return [
- {
+ const containsSelection = sequenceKeysContainingSelection.has(key);
+ return {
...selectableOutline,
nodePathKey,
ref: sequence.refForOutline,
@@ -218,9 +228,28 @@ export const getCanvasOutlineLayoutTargets = ({
type: 'sequence' as const,
nodePathInfo: selectionNodePathInfo,
},
- },
- ];
- });
+ };
+ });
+ };
+};
+
+/** Builds selection state shared by canvas instances of the same source. */
+export const getCanvasOutlineLayoutTargets = ({
+ selectableOutlines,
+ selectedSequenceKeys,
+ sequenceKeysContainingSelection,
+ targetKey,
+}: {
+ readonly selectableOutlines: readonly CanvasSelectableOutline[];
+ readonly selectedSequenceKeys: ReadonlySet<string>;
+ readonly sequenceKeysContainingSelection: ReadonlySet<string>;
+ readonly targetKey: string | null;
+}): CanvasOutlineLayoutTarget[] => {
+ return createCanvasOutlineLayoutTargetLookup({
+ selectableOutlines,
+ selectedSequenceKeys,
+ sequenceKeysContainingSelection,
+ })(targetKey);
};
/** Activation keeps frame subscriptions out of an idle outline overlay. */
diff --git a/packages/studio/src/components/SelectedOutlineOverlay.tsx b/packages/studio/src/components/SelectedOutlineOverlay.tsx
index a7c3149e45..227cdfb148 100644
--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx
+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx
@@ -66,6 +66,7 @@ import {
import {propStatusHas3DTransformValue} from './Timeline/transform-3d-mode';
const {
+ createCanvasOutlineLayoutTargetLookup,
getCanvasSelectableOutlines,
getCanvasVisibleOutlineTargets,
getCanvasActiveOutlineTargets,
@@ -192,6 +193,9 @@ type SelectableOutlines = Readonly<
>;
type CalculateOutlineTargetsOptions = {
+ readonly getCanvasLayoutTargets:
+ | (() => ReturnType<typeof getCanvasOutlineLayoutTargets>)
+ | null;
readonly mode: 'controls' | 'layout';
readonly runtimeValuesByStore: ReadonlyMap<
RuntimeValueStore,
@@ -209,6 +213,7 @@ type CalculateOutlineTargets = (
const calculateOutlineTargets = ({
connectedClientId,
editorShowOutlines,
+ getCanvasLayoutTargets,
getDragOverrides,
getScaleLockState,
isFullscreen,
@@ -266,12 +271,16 @@ const calculateOutlineTargets = ({
const previewInteractive =
connectedClientId !== null && studioInteractivityEnabled;
- return getCanvasOutlineLayoutTargets({
- selectableOutlines,
- selectedSequenceKeys,
- sequenceKeysContainingSelection,
- targetKey,
- }).flatMap((canvasLayoutTarget) => {
+ const canvasLayoutTargets =
+ getCanvasLayoutTargets === null
+ ? getCanvasOutlineLayoutTargets({
+ selectableOutlines,
+ selectedSequenceKeys,
+ sequenceKeysContainingSelection,
+ targetKey,
+ })
+ : getCanvasLayoutTargets();
+ return canvasLayoutTargets.flatMap((canvasLayoutTarget) => {
const {
key,
keyframeDisplayOffset,
@@ -808,6 +817,7 @@ const ActiveSelectedOutlineOverlayUnmemoized: React.FC<
const outlineTargets = useMemo(
() =>
calculateOutlineTargetsForCurrentState({
+ getCanvasLayoutTargets: null,
mode: 'layout',
runtimeValuesByStore: outlineRuntimeValuesByStore,
selectableOutlines: selectableOutlinesForLayout,
@@ -1050,24 +1060,43 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
},
[],
);
- const getLatestOutlineTargetByKey = useCallback(
- (key: string) => {
+ const getLatestOutlineTargetByKey = useMemo(() => {
+ let cachedOutlines: SelectableOutlines | null = null;
+ let cachedLookup: ReturnType<
+ typeof createCanvasOutlineLayoutTargetLookup
+ > | null = null;
+ return (key: string) => {
const timelinePosition = getCurrentFrame();
+ const selectableOutlines = getSelectableOutlinesAtFrame(timelinePosition);
const target = calculateOutlineTargetsForCurrentState({
+ getCanvasLayoutTargets: () => {
+ // Share only source metadata; controls below still read live values.
+ if (cachedOutlines !== selectableOutlines || cachedLookup === null) {
+ cachedLookup = createCanvasOutlineLayoutTargetLookup({
+ selectableOutlines,
+ selectedSequenceKeys,
+ sequenceKeysContainingSelection,
+ });
+ cachedOutlines = selectableOutlines;
+ }
+
+ return cachedLookup(key);
+ },
mode: 'controls',
runtimeValuesByStore: new Map(),
- selectableOutlines: getSelectableOutlinesAtFrame(timelinePosition),
+ selectableOutlines,
targetKey: key,
targetTimelinePosition: timelinePosition,
})[0];
return target as SelectedOutlineTarget | undefined;
- },
- [
- calculateOutlineTargetsForCurrentState,
- getCurrentFrame,
- getSelectableOutlinesAtFrame,
- ],
- );
+ };
+ }, [
+ calculateOutlineTargetsForCurrentState,
+ getCurrentFrame,
+ getSelectableOutlinesAtFrame,
+ selectedSequenceKeys,
+ sequenceKeysContainingSelection,
+ ]);
const getAllDragTargets = useCallback((): SelectedOutlineDragTarget[] => {
// Selected layers can be outside the current frame and have no mounted DOM.
// Only outline measurement and snapping need to be restricted to visible layers.
@@ -1082,6 +1111,7 @@ const SelectedOutlineOverlayUnmemoized: React.FC<
selectedKeys.has(key),
);
const targets = calculateOutlineTargetsForCurrentState({
+ getCanvasLayoutTargets: null,
mode: 'controls',
runtimeValuesByStore: new Map(),
selectableOutlines,