Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Speed up canvas outline ordering

Jonny Burger1e007a08f733
diff --git a/packages/studio/src/components/selected-outline-order.ts b/packages/studio/src/components/selected-outline-order.ts
index fc97d4869c..3cb832621d 100644
--- a/packages/studio/src/components/selected-outline-order.ts
+++ b/packages/studio/src/components/selected-outline-order.ts
@@ -65,12 +65,9 @@ const getOutlineBounds = (outline: SelectedOutline) => {
};
const outlineBoundsOverlap = (
- a: SelectedOutline,
- b: SelectedOutline,
+ aBounds: ReturnType<typeof getOutlineBounds>,
+ bBounds: ReturnType<typeof getOutlineBounds>,
): boolean => {
- const aBounds = getOutlineBounds(a);
- const bBounds = getOutlineBounds(b);
-
return (
aBounds.minX <= bBounds.maxX + outlineBoundsOverlapTolerance &&
aBounds.maxX + outlineBoundsOverlapTolerance >= bBounds.minX &&
@@ -277,14 +274,29 @@ const orderOutlineGroup = ({
// 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++) {
- const a = outlines[i];
+ 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 aTarget = targetsByKey.get(a.key);
const bTarget = targetsByKey.get(b.key);
-
- if (aTarget === undefined || bTarget === undefined) {
+ if (bTarget === undefined) {
continue;
}
@@ -299,14 +311,7 @@ const orderOutlineGroup = ({
parentBySequenceId,
});
- if (aAncestorOfB || bAncestorOfA || !outlineBoundsOverlap(a, b)) {
- continue;
- }
-
- const aArea = getOutlineHitArea(a);
- const bArea = getOutlineHitArea(b);
-
- if (Math.abs(aArea - bArea) <= outlineAreaEqualityTolerance) {
+ if (aAncestorOfB || bAncestorOfA) {
continue;
}
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 66de615117..bea4e27b6c 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -3838,6 +3838,109 @@ const makeOutlineSequence = ({
readonly parent: string | null;
}) => ({id, parent});
+test('Canvas outline rendering measures each disjoint hit target only once', () => {
+ let pointReads = 0;
+ const outlines = Array.from({length: 1000}, (_, index): SelectedOutline => {
+ const outline = makeTestOutline({
+ key: String(index),
+ left: (index % 40) * 20,
+ top: Math.floor(index / 40) * 20,
+ width: 10,
+ height: 10,
+ });
+ return {
+ ...outline,
+ get points() {
+ pointReads++;
+ return outline.points;
+ },
+ };
+ });
+ const orderedOutlines = orderOutlinesForRendering({
+ outlines,
+ sequences: outlines.map(({key}) =>
+ makeOutlineSequence({id: key, parent: null}),
+ ),
+ targetsByKey: new Map(
+ outlines.map(({key}) => [
+ key,
+ makeOutlineTarget({id: key, parent: null}),
+ ]),
+ ),
+ });
+
+ expect(orderedOutlines.map(({key}) => key)).toEqual(
+ outlines.map(({key}) => key),
+ );
+ expect(pointReads).toBeLessThanOrEqual(outlines.length * 32);
+});
+
+test('Canvas outline rendering preserves overlap and area tolerances', () => {
+ const sequences = [
+ makeOutlineSequence({id: 'small', parent: null}),
+ makeOutlineSequence({id: 'large', parent: null}),
+ ];
+ const targetsByKey = new Map(
+ sequences.map(({id, parent}) => [id, makeOutlineTarget({id, parent})]),
+ );
+
+ for (const axis of ['left', 'top'] as const) {
+ for (const gap of [0.5, 0.51]) {
+ const outlines = [
+ makeTestOutline({
+ key: 'small',
+ left: axis === 'left' ? 20 + gap : 0,
+ top: axis === 'top' ? 20 + gap : 0,
+ width: 10,
+ height: 10,
+ }),
+ makeTestOutline({
+ key: 'large',
+ left: 0,
+ top: 0,
+ width: 20,
+ height: 20,
+ }),
+ ];
+
+ expect(
+ orderOutlinesForRendering({
+ outlines,
+ sequences,
+ targetsByKey,
+ }).map(({key}) => key),
+ ).toEqual(gap === 0.5 ? ['large', 'small'] : ['small', 'large']);
+ }
+ }
+
+ for (const extraWidth of [0.05, 0.051]) {
+ const outlines = [
+ makeTestOutline({
+ key: 'small',
+ left: 0,
+ top: 0,
+ width: 10,
+ height: 10,
+ }),
+ makeTestOutline({
+ key: 'large',
+ left: 0,
+ top: 0,
+ width: 10 + extraWidth,
+ height: 10,
+ }),
+ ];
+
+ expect(
+ orderOutlinesForRendering({
+ outlines,
+ sequences,
+ targetsByKey,
+ }).map(({key}) => key),
+ ).toEqual(extraWidth === 0.05 ? ['small', 'large'] : ['large', 'small']);
+ }
+});
+
test('Canvas outline rendering keeps smaller nested hit targets above parents', () => {
const orderedOutlines = orderOutlinesForRendering({
outlines: [