Skip to content

🎥 Make videos programmatically with React

`@remotion/convert`: Preserve crop size when panning (#8853)

Jonny Burger4c3473d7268f
diff --git a/packages/convert/app/components/crop-ui/DragHandle.tsx b/packages/convert/app/components/crop-ui/DragHandle.tsx
index 95f47ba76f..9f8f68a53d 100644
--- a/packages/convert/app/components/crop-ui/DragHandle.tsx
+++ b/packages/convert/app/components/crop-ui/DragHandle.tsx
@@ -5,6 +5,7 @@ import {
forceSpecificCursor,
stopForcingSpecificCursor,
} from './force-specific-cursor';
+import {moveCropRect} from './move-crop-rect';
import {preventBodyScroll} from './prevent-body-scroll';
const clamp = (val: number, min: number, max: number) => {
@@ -71,11 +72,14 @@ export const DragHandle: React.FC<{
const clampedLeft = clamp(newLeft, 0, dimensions.width - rect.width);
const clampedTop = clamp(newTop, 0, dimensions.height - rect.height);
- updateRect((prev) => ({
- ...prev,
- left: Math.round(clampedLeft),
- top: Math.round(clampedTop),
- }));
+ updateRect((previousRect) =>
+ moveCropRect({
+ previousRect,
+ visibleRect: rect,
+ left: clampedLeft,
+ top: clampedTop,
+ }),
+ );
};
const onPointerMove = (evt: PointerEvent) => {
diff --git a/packages/convert/app/components/crop-ui/move-crop-rect.ts b/packages/convert/app/components/crop-ui/move-crop-rect.ts
new file mode 100644
index 0000000000..6d7899cc6d
--- /dev/null
+++ b/packages/convert/app/components/crop-ui/move-crop-rect.ts
@@ -0,0 +1,21 @@
+import type {CropRectangle} from 'mediabunny';
+
+export const moveCropRect = ({
+ previousRect,
+ visibleRect,
+ left,
+ top,
+}: {
+ readonly previousRect: CropRectangle;
+ readonly visibleRect: CropRectangle;
+ readonly left: number;
+ readonly top: number;
+}): CropRectangle => {
+ return {
+ ...previousRect,
+ left: Math.round(left),
+ top: Math.round(top),
+ width: visibleRect.width,
+ height: visibleRect.height,
+ };
+};
diff --git a/packages/convert/test/crop-ui.test.ts b/packages/convert/test/crop-ui.test.ts
new file mode 100644
index 0000000000..1d7067895c
--- /dev/null
+++ b/packages/convert/test/crop-ui.test.ts
@@ -0,0 +1,30 @@
+import {expect, test} from 'bun:test';
+import {moveCropRect} from '../app/components/crop-ui/move-crop-rect';
+
+test('keeps crop area size when moving a crop made from the left edge', () => {
+ const movedRect = moveCropRect({
+ previousRect: {
+ left: 300,
+ top: 0,
+ width: Infinity,
+ height: Infinity,
+ },
+ visibleRect: {
+ left: 300,
+ top: 0,
+ width: 1620,
+ height: 1080,
+ },
+ left: 0,
+ top: 0,
+ });
+
+ expect(movedRect).toEqual({
+ left: 0,
+ top: 0,
+ width: 1620,
+ height: 1080,
+ });
+
+ expect(1920 - (movedRect.left + movedRect.width)).toBe(300);
+});