🎥 Make videos programmatically with React
`@remotion/studio`: Adapt outline handles to small selections (#10343)
Jonny Burger5df3986b9b28
diff --git a/packages/example/e2e/inspector-section-collapse.test.mts b/packages/example/e2e/inspector-section-collapse.test.mtsindex 9e603f9d49..1bca4407f4 100644--- a/packages/example/e2e/inspector-section-collapse.test.mts+++ b/packages/example/e2e/inspector-section-collapse.test.mts@@ -138,6 +138,28 @@ test.describe('inspector section collapse', () => {() => !document.body.innerText.includes('Loading...'),{timeout: 30_000},);+ const tinyScaleEdges = page.locator(+ '[data-remotion-studio-scale-edge-contains-selection="true"]',+ );+ const tinyTransform = page+ .getByText('Tiny transform', {exact: true})+ .first();+ await expect(async () => {+ await tinyTransform.click();+ await expect(tinyScaleEdges).toHaveCount(2, {timeout: 1_000});+ }).toPass({timeout: 30_000});+ expect(+ await tinyScaleEdges.evaluateAll((edges) =>+ edges.map((edge) =>+ edge.getAttribute('data-remotion-studio-scale-edge'),+ ),+ ),+ ).toEqual(['right', 'bottom']);+ await expect(+ page.locator(+ '[data-remotion-studio-rotation-corner-contains-selection="true"]',+ ),+ ).toHaveCount(0);await select2DTransformAndWaitForInspector(page);await expect(diff --git a/packages/example/src/VisualMode3D.tsx b/packages/example/src/VisualMode3D.tsxindex da48450d2e..b29f6b2e55 100644--- a/packages/example/src/VisualMode3D.tsx+++ b/packages/example/src/VisualMode3D.tsx@@ -25,6 +25,19 @@ export const VisualMode3D: React.FC = () => {><AbsoluteFill style={{backgroundColor: '#ff4d8d'}} /></Sequence>+ <Sequence+ name="Tiny transform"+ durationInFrames={120}+ style={{+ height: 8,+ rotate: '0deg',+ scale: 1,+ translate: '450px 450px',+ width: 8,+ }}+ >+ <AbsoluteFill style={{backgroundColor: '#44cc88'}} />+ </Sequence></AbsoluteFill>);};diff --git a/packages/studio/src/components/SelectedOutlineElement.tsx b/packages/studio/src/components/SelectedOutlineElement.tsxindex af7819d137..ebf1356182 100644--- a/packages/studio/src/components/SelectedOutlineElement.tsx+++ b/packages/studio/src/components/SelectedOutlineElement.tsx@@ -20,6 +20,7 @@ import {callApi} from './call-api';import {useConfirmationDialog} from './ConfirmationDialog';import {useSelectComposition} from './InitialCompositionLoader';import {showNotification} from './Notifications/NotificationCenter';+import {getSelectedOutlineControlLayout} from './selected-outline-control-layout';import type {SelectedOutline} from './selected-outline-geometry';import {type SelectedOutlineSnapPoint} from './selected-outline-snap';import {@@ -114,6 +115,10 @@ const SelectedOutlineElementUnmemoized: React.FC<const {setManuallyEnabled} = useContext(Transform3DModeStateContext);const setHoveredSequence = useSetTimelineSequenceHover();const targetRef = useRef(layoutTarget);+ const controlLayout = useMemo(+ () => getSelectedOutlineControlLayout(outline.points),+ [outline.points],+ );useLayoutEffect(() => {targetRef.current = layoutTarget;}, [layoutTarget]);@@ -537,12 +542,17 @@ const SelectedOutlineElementUnmemoized: React.FC</>{controlTarget?.cropDrag === null &&(layoutTarget?.containsSelection || hovered)- ? (['top', 'right', 'bottom', 'left'] as const).map((edge) => (+ ? controlLayout.scaleEdges.map((edge) => (<SelectedOutlineScaleEdgeLinekey={edge}getAllScaleDragTargets={getAllScaleDragTargets}dragging={dragging}edge={edge}+ hitWidth={+ edge === 'top' || edge === 'bottom'+ ? controlLayout.scaleHitWidth.horizontal+ : controlLayout.scaleHitWidth.vertical+ }outline={outline}onContextMenuOpen={onContextMenuOpen}onContextMenuOpenChange={onContextMenuOpenChange}@@ -555,20 +565,20 @@ const SelectedOutlineElementUnmemoized: React.FC<: null}{controlTarget?.cropDrag === null &&(layoutTarget?.containsSelection || hovered)- ? (- ['top-left', 'top-right', 'bottom-right', 'bottom-left'] as const- ).map((corner) => (+ ? controlLayout.rotationCorners.map(({corner, point}) => (<SelectedOutlineRotationCornerHandlekey={corner}getAllRotationDragTargets={getAllRotationDragTargets}corner={corner}dragging={dragging}+ handlePoint={point}outline={outline}onContextMenuOpen={onContextMenuOpen}onContextMenuOpenChange={onContextMenuOpenChange}onDraggingChange={onDraggingChange}onHoverChange={onHoverChange}onSelect={onSelect}+ radius={controlLayout.rotationHandleRadius}target={controlTarget}/>))diff --git a/packages/studio/src/components/SelectedOutlineRotationCornerHandle.tsx b/packages/studio/src/components/SelectedOutlineRotationCornerHandle.tsxindex ad6265e728..d4d4a39117 100644--- a/packages/studio/src/components/SelectedOutlineRotationCornerHandle.tsx+++ b/packages/studio/src/components/SelectedOutlineRotationCornerHandle.tsx@@ -49,6 +49,7 @@ export const SelectedOutlineRotationCornerHandle: React.FC<{readonly getAllRotationDragTargets: () => readonly SelectedOutlineRotationDragTarget[];readonly corner: SelectedOutlineRotationCorner;readonly dragging: boolean;+ readonly handlePoint: {readonly x: number; readonly y: number};readonly outline: SelectedOutline;readonly onDraggingChange: (dragging: boolean) => void;readonly onContextMenuOpen: SelectedOutlineContextMenuOpenHandler;@@ -58,17 +59,20 @@ export const SelectedOutlineRotationCornerHandle: React.FC<{item: TimelineSelection,interaction: TimelineSelectionInteraction,) => void;+ readonly radius: number;readonly target: SelectedOutlineTarget | undefined;}> = ({getAllRotationDragTargets,corner,dragging,+ handlePoint,outline,onDraggingChange,onContextMenuOpen,onContextMenuOpenChange,onHoverChange,onSelect,+ radius,target,}) => {const {getDragOverrides} = useContext(@@ -340,9 +344,9 @@ export const SelectedOutlineRotationCornerHandle: React.FC<{<><circleref={circleRef}- cx={cornerInfo.point.x}- cy={cornerInfo.point.y}- r={12}+ cx={handlePoint.x}+ cy={handlePoint.y}+ r={radius}fill={TRANSPARENT}stroke={TRANSPARENT}vectorEffect="non-scaling-stroke"diff --git a/packages/studio/src/components/SelectedOutlineScaleEdgeLine.tsx b/packages/studio/src/components/SelectedOutlineScaleEdgeLine.tsxindex a91154e39b..5461bcb319 100644--- a/packages/studio/src/components/SelectedOutlineScaleEdgeLine.tsx+++ b/packages/studio/src/components/SelectedOutlineScaleEdgeLine.tsx@@ -43,6 +43,7 @@ export const SelectedOutlineScaleEdgeLine: React.FC<{readonly getAllScaleDragTargets: () => readonly SelectedOutlineScaleDragTarget[];readonly dragging: boolean;readonly edge: SelectedOutlineScaleEdge;+ readonly hitWidth: number;readonly outline: SelectedOutline;readonly onDraggingChange: (dragging: boolean) => void;readonly onContextMenuOpen: SelectedOutlineContextMenuOpenHandler;@@ -57,6 +58,7 @@ export const SelectedOutlineScaleEdgeLine: React.FC<{getAllScaleDragTargets,dragging,edge,+ hitWidth,outline,onDraggingChange,onContextMenuOpen,@@ -280,7 +282,8 @@ export const SelectedOutlineScaleEdgeLine: React.FC<{x2={edgeInfo.end.x}y2={edgeInfo.end.y}stroke={TRANSPARENT}- strokeWidth={12}+ strokeWidth={hitWidth}+ strokeLinecap="butt"vectorEffect="non-scaling-stroke"pointerEvents="stroke"cursor={edgeInfo.cursor}diff --git a/packages/studio/src/components/selected-outline-control-layout.ts b/packages/studio/src/components/selected-outline-control-layout.tsnew file mode 100644index 0000000000..85b2c3c58b--- /dev/null+++ b/packages/studio/src/components/selected-outline-control-layout.ts@@ -0,0 +1,126 @@+import type {OutlinePoint, SelectedOutline} from './selected-outline-geometry';++export type SelectedOutlineControlCorner =+ | 'top-left'+ | 'top-right'+ | 'bottom-right'+ | 'bottom-left';++// Outline points are measured in overlay pixels, keeping these sizes independent of canvas zoom.+const handleSize = 8;+const targetHalfSize = 4.5;+const smallOutlineSize = handleSize * 4;+const tinyOutlineSize = handleSize * 2;++const distance = (from: OutlinePoint, to: OutlinePoint) =>+ Math.hypot(to.x - from.x, to.y - from.y);++const normalize = (point: OutlinePoint): OutlinePoint | null => {+ const length = Math.hypot(point.x, point.y);+ if (length < 0.001) {+ return null;+ }++ return {x: point.x / length, y: point.y / length};+};++const getRotationHandlePoint = ({+ corner,+ points,+ radius,+}: {+ readonly corner: SelectedOutlineControlCorner;+ readonly points: SelectedOutline['points'];+ readonly radius: number;+}): OutlinePoint => {+ const [topLeft, topRight, bottomRight, bottomLeft] = points;+ const {point, adjacent} = {+ 'top-left': {point: topLeft, adjacent: [topRight, bottomLeft]},+ 'top-right': {point: topRight, adjacent: [topLeft, bottomRight]},+ 'bottom-right': {point: bottomRight, adjacent: [topRight, bottomLeft]},+ 'bottom-left': {point: bottomLeft, adjacent: [topLeft, bottomRight]},+ }[corner];+ const firstDirection = normalize({+ x: point.x - adjacent[0].x,+ y: point.y - adjacent[0].y,+ });+ const secondDirection = normalize({+ x: point.x - adjacent[1].x,+ y: point.y - adjacent[1].y,+ });+ const outlineCenter = {+ x: (topLeft.x + topRight.x + bottomRight.x + bottomLeft.x) / 4,+ y: (topLeft.y + topRight.y + bottomRight.y + bottomLeft.y) / 4,+ };+ const fallbackDirection = normalize({+ x: point.x - outlineCenter.x,+ y: point.y - outlineCenter.y,+ });+ const direction =+ firstDirection === null || secondDirection === null+ ? fallbackDirection+ : normalize({+ x: firstDirection.x + secondDirection.x,+ y: firstDirection.y + secondDirection.y,+ });++ if (direction === null) {+ return point;+ }++ return {+ x: point.x + direction.x * radius,+ y: point.y + direction.y * radius,+ };+};++export const getSelectedOutlineControlLayout = (+ points: SelectedOutline['points'],+) => {+ const [topLeft, topRight, bottomRight, bottomLeft] = points;+ const horizontalExtent =+ (distance(topLeft, topRight) + distance(bottomLeft, bottomRight)) / 2;+ const verticalExtent =+ (distance(topLeft, bottomLeft) + distance(topRight, bottomRight)) / 2;+ const isSmallX = horizontalExtent < smallOutlineSize;+ const isSmallY = verticalExtent < smallOutlineSize;+ const isTinyX = horizontalExtent < tinyOutlineSize;+ const isTinyY = verticalExtent < tinyOutlineSize;+ const targetHalfSizeX = isSmallX ? targetHalfSize / 2 : targetHalfSize;+ const targetHalfSizeY = isSmallY ? targetHalfSize / 2 : targetHalfSize;+ const rotationHandleRadius = Math.max(targetHalfSizeX, targetHalfSizeY) * 1.5;+ const scaleEdges = (['top', 'right', 'bottom', 'left'] as const).filter(+ (edge) => {+ // Preserve one scale control per axis when opposite edge targets would overlap.+ if (edge === 'top') {+ return !isTinyY;+ }++ if (edge === 'left') {+ return !isTinyX;+ }++ return true;+ },+ );+ const rotationCorners = (+ ['top-left', 'top-right', 'bottom-right', 'bottom-left'] as const+ ).map((corner) => ({+ corner,+ point: getRotationHandlePoint({+ corner,+ points,+ radius: rotationHandleRadius,+ }),+ }));++ return {+ rotationCorners: isTinyX || isTinyY ? [] : rotationCorners,+ rotationHandleRadius,+ scaleEdges,+ scaleHitWidth: {+ horizontal: targetHalfSizeY * 2,+ vertical: targetHalfSizeX * 2,+ },+ };+};diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.tsindex 91c45182f3..1498475db7 100644--- a/packages/studio/src/test/timeline-selection.test.ts+++ b/packages/studio/src/test/timeline-selection.test.ts@@ -11,6 +11,7 @@ import {} from 'remotion';import {NoReactInternals} from 'remotion/no-react';import {getInspectorSelectableItems} from '../components/InspectorSequenceSection';+import {getSelectedOutlineControlLayout} from '../components/selected-outline-control-layout';import type {SelectedOutline} from '../components/selected-outline-geometry';import {cropOutlinePoints,@@ -6631,6 +6632,48 @@ test('Selected outline rotation corners use the outline corners and center', ()expect(topRight.cursor).toContain('") 12 12, alias');});+test('Selected outline controls adapt to the screen-space outline size', () => {+ const normal = getSelectedOutlineControlLayout([+ {x: 0, y: 0},+ {x: 100, y: 0},+ {x: 100, y: 50},+ {x: 0, y: 50},+ ]);+ const small = getSelectedOutlineControlLayout([+ {x: 0, y: 0},+ {x: 24, y: 0},+ {x: 24, y: 24},+ {x: 0, y: 24},+ ]);+ const tiny = getSelectedOutlineControlLayout([+ {x: 0, y: 0},+ {x: 8, y: 0},+ {x: 8, y: 8},+ {x: 0, y: 8},+ ]);+ const thin = getSelectedOutlineControlLayout([+ {x: 0, y: 0},+ {x: 8, y: 0},+ {x: 8, y: 40},+ {x: 0, y: 40},+ ]);++ expect(normal.scaleEdges).toEqual(['top', 'right', 'bottom', 'left']);+ expect(normal.scaleHitWidth).toEqual({horizontal: 9, vertical: 9});+ expect(normal.rotationHandleRadius).toBe(6.75);+ expect(normal.rotationCorners).toHaveLength(4);+ expect(normal.rotationCorners[0].point.x).toBeCloseTo(-4.773);+ expect(normal.rotationCorners[0].point.y).toBeCloseTo(-4.773);+ expect(small.scaleHitWidth).toEqual({horizontal: 4.5, vertical: 4.5});+ expect(small.rotationHandleRadius).toBe(3.375);+ expect(small.rotationCorners).toHaveLength(4);+ expect(tiny.scaleEdges).toEqual(['right', 'bottom']);+ expect(tiny.rotationCorners).toEqual([]);+ expect(thin.scaleEdges).toEqual(['top', 'right', 'bottom']);+ expect(thin.scaleHitWidth).toEqual({horizontal: 9, vertical: 4.5});+ expect(thin.rotationCorners).toEqual([]);+});+test('Selected outline rotation pivot follows transform origin', () => {const points = [{x: 0, y: 0},