Skip to content

🎥 Make videos programmatically with React

show numbers on mobile

JonnyBurger2833576056a8
diff --git a/packages/convert/app/components/crop-ui/CropUi.tsx b/packages/convert/app/components/crop-ui/CropUi.tsx
index 2ec2d4bfbd..5a4a3fc11a 100644
--- a/packages/convert/app/components/crop-ui/CropUi.tsx
+++ b/packages/convert/app/components/crop-ui/CropUi.tsx
@@ -6,6 +6,10 @@ import {CropBackdrop} from './Backdrop';
import {DragHandle} from './DragHandle';
import {EdgeHandle} from './EdgeHandle';
import {ResizeHandle} from './ResizeHandle';
+import {
+ HorizontalPositionIndicator,
+ VerticalPositionIndicator,
+} from './VerticalPositionIndicator';
export const CropUI: React.FC<{
readonly mediaFox: MediaFox;
@@ -18,6 +22,8 @@ export const CropUI: React.FC<{
mediaFox.getState(),
);
+ const [isDragging, setMarkAsDragging] = useState(false);
+
const ref = useRef<HTMLDivElement>(null);
const dimensions = useMemo(() => {
@@ -86,6 +92,26 @@ export const CropUI: React.FC<{
aspectRatio: `${dimensions.width} / ${dimensions.height}`,
}}
>
+ <VerticalPositionIndicator
+ xPosition={rect.left}
+ width={dimensions.width}
+ isDragging={isDragging}
+ />
+ <VerticalPositionIndicator
+ xPosition={rect.left + rect.width}
+ width={dimensions.width}
+ isDragging={isDragging}
+ />
+ <HorizontalPositionIndicator
+ yPosition={rect.top}
+ height={dimensions.height}
+ isDragging={isDragging}
+ />
+ <HorizontalPositionIndicator
+ yPosition={rect.top + rect.height}
+ height={dimensions.height}
+ isDragging={isDragging}
+ />
<CropBackdrop rect={rect} dimensions={dimensions} />
<div
className="border-brand absolute border-2 rounded-md flex flex-col items-center justify-center pointer-events-none"
@@ -105,6 +131,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<EdgeHandle
divRef={ref}
@@ -112,6 +139,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<EdgeHandle
divRef={ref}
@@ -119,6 +147,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<EdgeHandle
divRef={ref}
@@ -126,6 +155,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<EdgeHandle
divRef={ref}
@@ -133,6 +163,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<ResizeHandle
divRef={ref}
@@ -140,6 +171,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<ResizeHandle
divRef={ref}
@@ -147,6 +179,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<ResizeHandle
divRef={ref}
@@ -154,6 +187,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
<ResizeHandle
divRef={ref}
@@ -161,6 +195,7 @@ export const CropUI: React.FC<{
dimensions={dimensions}
rect={rect}
updateRect={setUnclampedRect}
+ setMarkAsDragging={setMarkAsDragging}
/>
</div>
);
diff --git a/packages/convert/app/components/crop-ui/DragHandle.tsx b/packages/convert/app/components/crop-ui/DragHandle.tsx
index 750b876c26..085211efed 100644
--- a/packages/convert/app/components/crop-ui/DragHandle.tsx
+++ b/packages/convert/app/components/crop-ui/DragHandle.tsx
@@ -15,7 +15,8 @@ export const DragHandle: React.FC<{
readonly dimensions: Dimensions;
readonly rect: CropRectangle;
readonly updateRect: React.Dispatch<React.SetStateAction<CropRectangle>>;
-}> = ({divRef, dimensions, rect, updateRect}) => {
+ readonly setMarkAsDragging: React.Dispatch<React.SetStateAction<boolean>>;
+}> = ({divRef, dimensions, rect, updateRect, setMarkAsDragging}) => {
const style: React.CSSProperties = useMemo(() => {
return {
position: 'absolute' as const,
@@ -36,6 +37,7 @@ export const DragHandle: React.FC<{
}
forceSpecificCursor('move');
+ setMarkAsDragging(true);
const boxOffset = divRef.current?.getBoundingClientRect()!;
const factor = boxOffset.width / dimensions.width;
@@ -63,16 +65,8 @@ export const DragHandle: React.FC<{
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,
- );
+ const clampedLeft = clamp(newLeft, 0, dimensions.width - rect.width);
+ const clampedTop = clamp(newTop, 0, dimensions.height - rect.height);
updateRect((prev) => ({
...prev,
@@ -92,6 +86,7 @@ export const DragHandle: React.FC<{
evt.stopPropagation();
stopForcingSpecificCursor();
+ setMarkAsDragging(false);
window.removeEventListener('pointermove', onPointerMove);
window.removeEventListener('pointerup', onPointerUp);
@@ -100,7 +95,14 @@ export const DragHandle: React.FC<{
window.addEventListener('pointermove', onPointerMove);
window.addEventListener('pointerup', onPointerUp);
},
- [dimensions.height, dimensions.width, divRef, rect, updateRect],
+ [
+ dimensions.height,
+ dimensions.width,
+ divRef,
+ rect,
+ updateRect,
+ setMarkAsDragging,
+ ],
);
return (
diff --git a/packages/convert/app/components/crop-ui/EdgeHandle.tsx b/packages/convert/app/components/crop-ui/EdgeHandle.tsx
index 561fd7b5d4..03dfa65e56 100644
--- a/packages/convert/app/components/crop-ui/EdgeHandle.tsx
+++ b/packages/convert/app/components/crop-ui/EdgeHandle.tsx
@@ -35,7 +35,8 @@ export const EdgeHandle: React.FC<{
readonly dimensions: Dimensions;
readonly rect: CropRectangle;
readonly updateRect: React.Dispatch<React.SetStateAction<CropRectangle>>;
-}> = ({position, divRef, dimensions, rect, updateRect}) => {
+ readonly setMarkAsDragging: React.Dispatch<React.SetStateAction<boolean>>;
+}> = ({position, divRef, dimensions, rect, updateRect, setMarkAsDragging}) => {
const style: React.CSSProperties = useMemo(() => {
const hitArea = EDGE_HIT_AREA;
@@ -105,6 +106,7 @@ export const EdgeHandle: React.FC<{
}
forceSpecificCursor(getCursor(position));
+ setMarkAsDragging(true);
const move = (evt: PointerEvent) => {
evt.preventDefault();
@@ -178,6 +180,7 @@ export const EdgeHandle: React.FC<{
evt.stopPropagation();
stopForcingSpecificCursor();
+ setMarkAsDragging(false);
window.removeEventListener('pointermove', onPointerMove);
window.removeEventListener('pointerup', onPointerUp);
@@ -186,7 +189,14 @@ export const EdgeHandle: React.FC<{
window.addEventListener('pointermove', onPointerMove);
window.addEventListener('pointerup', onPointerUp);
},
- [dimensions.height, dimensions.width, divRef, position, updateRect],
+ [
+ dimensions.height,
+ dimensions.width,
+ divRef,
+ position,
+ updateRect,
+ setMarkAsDragging,
+ ],
);
return (
diff --git a/packages/convert/app/components/crop-ui/ResizeHandle.tsx b/packages/convert/app/components/crop-ui/ResizeHandle.tsx
index c6e934ba13..53033f97e5 100644
--- a/packages/convert/app/components/crop-ui/ResizeHandle.tsx
+++ b/packages/convert/app/components/crop-ui/ResizeHandle.tsx
@@ -47,7 +47,8 @@ export const ResizeHandle: React.FC<{
readonly dimensions: Dimensions;
readonly rect: CropRectangle;
readonly updateRect: React.Dispatch<React.SetStateAction<CropRectangle>>;
-}> = ({position, divRef, dimensions, rect, updateRect}) => {
+ readonly setMarkAsDragging: React.Dispatch<React.SetStateAction<boolean>>;
+}> = ({position, divRef, dimensions, rect, updateRect, setMarkAsDragging}) => {
const style: React.CSSProperties = useMemo(() => {
if (position === 'top-left') {
return {
@@ -117,6 +118,7 @@ export const ResizeHandle: React.FC<{
}
forceSpecificCursor(getCursor(position));
+ setMarkAsDragging(true);
const move = (evt: PointerEvent) => {
evt.preventDefault();
@@ -207,6 +209,7 @@ export const ResizeHandle: React.FC<{
evt.stopPropagation();
stopForcingSpecificCursor();
+ setMarkAsDragging(false);
window.removeEventListener('pointermove', onPointerMove);
window.removeEventListener('pointerup', onPointerUp);
@@ -215,7 +218,14 @@ export const ResizeHandle: React.FC<{
window.addEventListener('pointermove', onPointerMove);
window.addEventListener('pointerup', onPointerUp);
},
- [dimensions.height, dimensions.width, divRef, position, updateRect],
+ [
+ dimensions.height,
+ dimensions.width,
+ divRef,
+ position,
+ updateRect,
+ setMarkAsDragging,
+ ],
);
return (
diff --git a/packages/convert/app/components/crop-ui/VerticalPositionIndicator.tsx b/packages/convert/app/components/crop-ui/VerticalPositionIndicator.tsx
new file mode 100644
index 0000000000..7cb457dcfe
--- /dev/null
+++ b/packages/convert/app/components/crop-ui/VerticalPositionIndicator.tsx
@@ -0,0 +1,50 @@
+const INDICATOR_WIDTH = 100;
+const INDICATOR_HEIGHT = 100;
+
+export const VerticalPositionIndicator: React.FC<{
+ xPosition: number;
+ width: number;
+ isDragging: boolean;
+}> = ({xPosition, width, isDragging}) => {
+ return (
+ <div
+ className="absolute font-brand text-sm text-gray-500 transition-opacity"
+ style={{
+ left: `${(xPosition / width) * 100}%`,
+ top: -25,
+ marginLeft: -INDICATOR_WIDTH / 2,
+ width: INDICATOR_WIDTH,
+ textAlign: 'center',
+ height: 25,
+ opacity: Number(isDragging && xPosition !== 0 && width !== xPosition),
+ }}
+ >
+ {xPosition}
+ </div>
+ );
+};
+
+export const HorizontalPositionIndicator: React.FC<{
+ yPosition: number;
+ height: number;
+ isDragging: boolean;
+}> = ({yPosition, height, isDragging}) => {
+ return (
+ <div
+ className="absolute font-brand text-sm text-gray-500 pr-2 transition-opacity"
+ style={{
+ top: `${(yPosition / height) * 100}%`,
+ left: -INDICATOR_HEIGHT,
+ marginTop: -INDICATOR_HEIGHT / 2,
+ width: INDICATOR_WIDTH,
+ textAlign: 'right',
+ height: INDICATOR_HEIGHT,
+ lineHeight: `${INDICATOR_HEIGHT}px`,
+ pointerEvents: 'none',
+ opacity: Number(isDragging && yPosition !== 0 && height !== yPosition),
+ }}
+ >
+ {yPosition}
+ </div>
+ );
+};