🎥 Make videos programmatically with React
pan
JonnyBurgerb3e74702f20b
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsxindex b477d9a0c9..7651696ff8 100644--- a/packages/convert/app/components/ConvertUi.tsx+++ b/packages/convert/app/components/ConvertUi.tsx@@ -706,7 +706,7 @@ const ConvertUI = ({</ConvertUiSection>{crop ? (<div className="text-gray-700 text-sm mt-2">- Use the handles to crop the video+ Use the handles above to crop the video.</div>) : null}</div>diff --git a/packages/convert/app/components/crop-ui/CropUi.tsx b/packages/convert/app/components/crop-ui/CropUi.tsxindex e77d22f0f7..5ac99e00d9 100644--- a/packages/convert/app/components/crop-ui/CropUi.tsx+++ b/packages/convert/app/components/crop-ui/CropUi.tsx@@ -3,6 +3,7 @@ import type {CropRectangle} from 'mediabunny';import type React from 'react';import {useEffect, useMemo, useRef, useState} from 'react';import {CropBackdrop} from './Backdrop';+import {DragHandle} from './DragHandle';import {EdgeHandle} from './EdgeHandle';import {ResizeHandle} from './ResizeHandle';@@ -87,7 +88,7 @@ export const CropUI: React.FC<{><CropBackdrop rect={rect} dimensions={dimensions} /><div- className="border-brand absolute border-4 rounded-md flex flex-col items-center justify-center"+ className="border-brand absolute border-4 rounded-md flex flex-col items-center justify-center pointer-events-none"style={{width: (rect.width / dimensions.width) * 100 + '%',height: (rect.height / dimensions.height) * 100 + '%',@@ -99,6 +100,12 @@ export const CropUI: React.FC<{{rect.width}x{rect.height}</div></div>+ <DragHandle+ divRef={ref}+ dimensions={dimensions}+ rect={rect}+ updateRect={setUnclampedRect}+ /><EdgeHandledivRef={ref}position="top"diff --git a/packages/convert/app/components/crop-ui/DragHandle.tsx b/packages/convert/app/components/crop-ui/DragHandle.tsxnew file mode 100644index 0000000000..750b876c26--- /dev/null+++ b/packages/convert/app/components/crop-ui/DragHandle.tsx@@ -0,0 +1,113 @@+import type {CropRectangle} from 'mediabunny';+import React, {useCallback, useMemo} from 'react';+import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';+import {+ forceSpecificCursor,+ stopForcingSpecificCursor,+} from './force-specific-cursor';++const clamp = (val: number, min: number, max: number) => {+ return Math.min(Math.max(val, min), max);+};++export const DragHandle: React.FC<{+ readonly divRef: React.RefObject<HTMLDivElement | null>;+ readonly dimensions: Dimensions;+ readonly rect: CropRectangle;+ readonly updateRect: React.Dispatch<React.SetStateAction<CropRectangle>>;+}> = ({divRef, dimensions, rect, updateRect}) => {+ const style: React.CSSProperties = useMemo(() => {+ return {+ position: 'absolute' as const,+ top: (rect.top / dimensions.height) * 100 + '%',+ left: (rect.left / dimensions.width) * 100 + '%',+ width: (rect.width / dimensions.width) * 100 + '%',+ height: (rect.height / dimensions.height) * 100 + '%',+ cursor: 'move',+ };+ }, [rect, dimensions]);++ const onPointerDown = useCallback(+ (e: React.PointerEvent) => {+ e.preventDefault();+ e.stopPropagation();+ if (e.button !== 0) {+ return;+ }++ forceSpecificCursor('move');++ const boxOffset = divRef.current?.getBoundingClientRect()!;+ const factor = boxOffset.width / dimensions.width;++ // Store the initial pointer position relative to the crop rect's top-left corner+ const initialPointerX = e.clientX;+ const initialPointerY = e.clientY;+ const initialLeft = rect.left;+ const initialTop = rect.top;++ const move = (evt: PointerEvent) => {+ evt.preventDefault();+ evt.stopPropagation();++ // Calculate the delta in screen pixels+ const deltaX = evt.clientX - initialPointerX;+ const deltaY = evt.clientY - initialPointerY;++ // Convert to crop pixels+ const deltaCropX = deltaX / factor;+ const deltaCropY = deltaY / factor;++ // Calculate new position+ const newLeft = initialLeft + deltaCropX;+ const newTop = initialTop + deltaCropY;++ // Clamp to boundaries+ 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),+ }));+ };++ const onPointerMove = (evt: PointerEvent) => {+ evt.preventDefault();+ evt.stopPropagation();+ move(evt);+ };++ const onPointerUp = (evt: PointerEvent) => {+ evt.preventDefault();+ evt.stopPropagation();++ stopForcingSpecificCursor();++ window.removeEventListener('pointermove', onPointerMove);+ window.removeEventListener('pointerup', onPointerUp);+ };++ window.addEventListener('pointermove', onPointerMove);+ window.addEventListener('pointerup', onPointerUp);+ },+ [dimensions.height, dimensions.width, divRef, rect, updateRect],+ );++ return (+ <div+ style={style}+ onPointerDown={onPointerDown}+ // No visible styling - invisible hit area+ />+ );+};