Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Align matching compositions on drop (#9251)

Jonny Burgere0300d84b1fb
diff --git a/packages/studio/src/components/Canvas.tsx b/packages/studio/src/components/Canvas.tsx
index 69219d0847..0bc12822dc 100644
--- a/packages/studio/src/components/Canvas.tsx
+++ b/packages/studio/src/components/Canvas.tsx
@@ -1113,6 +1113,8 @@ export const Canvas: React.FC<{
composition: compositionDragData,
compositionFile,
compositionId: currentCompositionId,
+ destinationDimensions:
+ contentDimensions === 'none' ? null : contentDimensions,
dropPosition,
});
} else {
diff --git a/packages/studio/src/components/import-assets.ts b/packages/studio/src/components/import-assets.ts
index 2542a2b6e2..3bbd6f1eca 100644
--- a/packages/studio/src/components/import-assets.ts
+++ b/packages/studio/src/components/import-assets.ts
@@ -251,6 +251,30 @@ export const getElementPositionForDrop = ({
return getCenteredPosition({dimensions, dropPosition});
};
+export const getCompositionPositionForDrop = ({
+ compositionDimensions,
+ destinationDimensions,
+ dropPosition,
+}: {
+ compositionDimensions: Dimensions;
+ destinationDimensions: Dimensions | null;
+ dropPosition: InsertElementDropPosition | null;
+}): InsertableCompositionElementPosition | null => {
+ // No translation makes an equal-sized composition fill the destination.
+ if (
+ destinationDimensions !== null &&
+ compositionDimensions.width === destinationDimensions.width &&
+ compositionDimensions.height === destinationDimensions.height
+ ) {
+ return null;
+ }
+
+ return getCenteredPosition({
+ dimensions: compositionDimensions,
+ dropPosition,
+ });
+};
+
const getComponentPropNumber = (props: ComponentProp[], name: string) => {
const prop = props.find((p) => p.name === name);
return typeof prop?.value === 'number' ? prop.value : null;
@@ -855,11 +879,13 @@ export const insertComposition = async ({
composition,
compositionFile,
compositionId,
+ destinationDimensions,
dropPosition,
}: {
composition: CompositionDragData;
compositionFile: string;
compositionId: string;
+ destinationDimensions: Dimensions | null;
dropPosition: InsertElementDropPosition | null;
}) => {
if (composition.compositionId === compositionId) {
@@ -897,8 +923,9 @@ export const insertComposition = async ({
height: calculated.height,
serializedResolvedPropsWithCustomSchema:
serializeResolvedPropsForSourceCode(calculated.props),
- position: getCenteredPosition({
- dimensions,
+ position: getCompositionPositionForDrop({
+ compositionDimensions: dimensions,
+ destinationDimensions,
dropPosition,
}),
},
diff --git a/packages/studio/src/test/import-assets.test.ts b/packages/studio/src/test/import-assets.test.ts
index 5216e82943..057a2fc2c4 100644
--- a/packages/studio/src/test/import-assets.test.ts
+++ b/packages/studio/src/test/import-assets.test.ts
@@ -3,6 +3,7 @@ import {
getAssetElement,
getAssetElementFromPath,
getComponentDimensions,
+ getCompositionPositionForDrop,
getElementPositionForDrop,
} from '../components/import-assets';
@@ -156,3 +157,23 @@ test('does not position Elements without a drop position', () => {
}),
).toBe(null);
});
+
+test('aligns a composition with matching destination dimensions', () => {
+ expect(
+ getCompositionPositionForDrop({
+ compositionDimensions: {width: 1920, height: 1080},
+ destinationDimensions: {width: 1920, height: 1080},
+ dropPosition: {centerX: 400, centerY: 300},
+ }),
+ ).toBe(null);
+});
+
+test('centers a composition with different destination dimensions', () => {
+ expect(
+ getCompositionPositionForDrop({
+ compositionDimensions: {width: 1280, height: 720},
+ destinationDimensions: {width: 1920, height: 1080},
+ dropPosition: {centerX: 400, centerY: 300},
+ }),
+ ).toEqual({x: -240, y: -60});
+});