Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/media-editor/src/image-editor/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import type { NormalizedRect, Flip, CropperState } from './types';
export const MIN_ZOOM = 1;
export const MAX_ZOOM = 10;

/**
* Minimum crop rect dimension in source-image pixels, enforced per axis
* during resize. Prevents accidental sub-pixel crops while staying small
* enough to allow tight crops (favicons, icons). Adjust here to tune the
* floor globally.
*/
export const MIN_CROP_PIXELS = 24;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up for debate. 16... 48...

Later it can be overridden by consumer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't go any smaller for now. If we were to go even smaller, I feel like we'd want some kind of snap interface with grid lines as we're delving into pixel art territory 😄


/**
* Wheel zoom sensitivity. A deltaY of 100 changes zoom by 0.25.
* This could be made configurable as a prop to the Cropper component.
Expand Down
27 changes: 15 additions & 12 deletions packages/media-editor/src/image-editor/core/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,24 +345,27 @@ export function cropperReducer(
return state;
}

// New crop: fill height (or width), maintain aspect ratio, center.
const normalizedRatio = rect.width / rect.height;
let newH = 1;
let newW = normalizedRatio;
if ( newW > 1 ) {
newW = 1;
newH = 1 / normalizedRatio;
}
// Scale factor: how much the crop grows. fitScale fills the
// larger axis to 1 (viewport-filling settle). Cap so the
// resulting zoom stays at or below MAX_ZOOM — tight crops
// would otherwise push state.zoom past the user-facing cap
// and break wheel/slider interactions, which clamp to
// MAX_ZOOM and snap state.zoom down on the first input.
const fitScale = 1 / Math.max( rect.width, rect.height );
const zoomCap = state.zoom > 0 ? MAX_ZOOM / state.zoom : fitScale;
const s = Math.min( fitScale, zoomCap );

// Scale factor: how much the crop grew.
const s = newH / rect.height;
const newW = rect.width * s;
const newH = rect.height * s;

// The old crop center in normalized visual space.
const oldCx = rect.x + rect.width / 2;
const oldCy = rect.y + rect.height / 2;

// Zoom scales by s so the same image region fills the
// larger crop at the same relative size.
// Zoom scales by s so the same image region fills the new
// crop at the same relative size. When s is capped, the new
// crop is smaller than the viewport but the visible content
// inside it remains the source region the user framed.
// Pan: the visible content center was at
// (cropCx - crop.x, cropCy - crop.y)
// in visual-normalized space. After centering the crop to
Expand Down
87 changes: 55 additions & 32 deletions packages/media-editor/src/image-editor/core/stencil-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import type { HandlePosition, NormalizedRect, Size } from './types';

export type { HandlePosition };

/** Minimum crop rect dimension in normalized space (5% of visual area). */
export const MIN_CROP_SIZE = 0.05;
/**
* Default minimum crop rect dimension in normalized space, used when no
* explicit per-axis minimum is supplied (e.g. before the source image
* has loaded). Real usage derives a pixel-based min from the source
* image dimensions and `MIN_CROP_PIXELS` in `constants.ts`.
*/
export const DEFAULT_MIN_CROP_SIZE: Size = { width: 0.05, height: 0.05 };

/**
* Bounds within which the crop rect must stay.
Expand Down Expand Up @@ -37,19 +42,21 @@ export interface ResizeDragState {
* Each edge moves independently based on the handle being dragged.
* Edges are clamped to the provided bounds and maintain a minimum size.
*
* @param drag The current drag state.
* @param clientX Current mouse/touch X position in pixels.
* @param clientY Current mouse/touch Y position in pixels.
* @param imageSize The rendered image dimensions in pixels.
* @param bounds The allowed crop area bounds.
* @param drag The current drag state.
* @param clientX Current mouse/touch X position in pixels.
* @param clientY Current mouse/touch Y position in pixels.
* @param imageSize The rendered image dimensions in pixels.
* @param bounds The allowed crop area bounds.
* @param minCropSize Minimum crop rect dimension in normalized space, per axis.
* @return The new crop rect in normalized coordinates.
*/
export function computeFreeResizeRect(
drag: ResizeDragState,
clientX: number,
clientY: number,
imageSize: Size,
bounds: CropBounds
bounds: CropBounds,
minCropSize: Size = DEFAULT_MIN_CROP_SIZE
): NormalizedRect {
const dx =
imageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;
Expand All @@ -67,24 +74,24 @@ export function computeFreeResizeRect(
if ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {
edgeTop = Math.max(
bounds.minY,
Math.min( s.y + dy, edgeBottom - MIN_CROP_SIZE )
Math.min( s.y + dy, edgeBottom - minCropSize.height )
);
}
if ( handle === 's' || handle === 'sw' || handle === 'se' ) {
edgeBottom = Math.max(
edgeTop + MIN_CROP_SIZE,
edgeTop + minCropSize.height,
Math.min( s.y + s.height + dy, bounds.maxY )
);
}
if ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {
edgeLeft = Math.max(
bounds.minX,
Math.min( s.x + dx, edgeRight - MIN_CROP_SIZE )
Math.min( s.x + dx, edgeRight - minCropSize.width )
);
}
if ( handle === 'e' || handle === 'ne' || handle === 'se' ) {
edgeRight = Math.max(
edgeLeft + MIN_CROP_SIZE,
edgeLeft + minCropSize.width,
Math.min( s.x + s.width + dx, bounds.maxX )
);
}
Expand All @@ -110,6 +117,7 @@ export function computeFreeResizeRect(
* @param imageSize The rendered image dimensions in pixels.
* @param bounds The allowed crop area bounds.
* @param normalizedRatio The locked aspect ratio (width / height in normalized space).
* @param minCropSize Minimum crop rect dimension in normalized space, per axis.
* @return The new crop rect in normalized coordinates.
*/
export function computeLockedResizeRect(
Expand All @@ -118,7 +126,8 @@ export function computeLockedResizeRect(
clientY: number,
imageSize: Size,
bounds: CropBounds,
normalizedRatio: number
normalizedRatio: number,
minCropSize: Size = DEFAULT_MIN_CROP_SIZE
): NormalizedRect {
// The math below divides by `normalizedRatio` and `imageSize`, so
// bail out with the start rect when any of them is zero. This can
Expand Down Expand Up @@ -156,9 +165,15 @@ export function computeLockedResizeRect(
let distW = ( draggedX - anchorX ) * dirX;
let distH = ( draggedY - anchorY ) * dirY;

// Enforce minimum size.
distW = Math.max( distW, MIN_CROP_SIZE );
distH = Math.max( distH, MIN_CROP_SIZE );
// Enforce minimum size on both axes, projecting through the ratio so
// neither axis falls below its per-axis floor.
const minDistW = Math.max(
minCropSize.width,
minCropSize.height * normalizedRatio
);
const minDistH = minDistW / normalizedRatio;
distW = Math.max( distW, minDistW );
distH = Math.max( distH, minDistH );
Comment thread
ramonjd marked this conversation as resolved.
Resolved

// Determine which axis "drives" — whichever the user moved more
// (in pixel space) determines the size, the other follows. The
Expand Down Expand Up @@ -192,8 +207,8 @@ export function computeLockedResizeRect(
}

// Enforce minimum after clamping.
distW = Math.max( distW, MIN_CROP_SIZE );
distH = Math.max( distH, MIN_CROP_SIZE );
distW = Math.max( distW, minDistW );
distH = Math.max( distH, minDistH );

// Compute the final rect position from the anchor.
const newX = dirX > 0 ? anchorX : anchorX - distW;
Expand All @@ -211,19 +226,21 @@ export function computeLockedResizeRect(
* axis symmetrically around the rect's center to preserve the ratio,
* clamping symmetrically so the rect stays within bounds.
*
* @param drag The current drag state.
* @param clientX Current mouse/touch X position in pixels.
* @param clientY Current mouse/touch Y position in pixels.
* @param imageSize The rendered image dimensions in pixels.
* @param bounds The allowed crop area bounds.
* @param drag The current drag state.
* @param clientX Current mouse/touch X position in pixels.
* @param clientY Current mouse/touch Y position in pixels.
* @param imageSize The rendered image dimensions in pixels.
* @param bounds The allowed crop area bounds.
* @param minCropSize Minimum crop rect dimension in normalized space, per axis.
* @return The new crop rect in normalized coordinates.
*/
export function computeShiftLockedResizeRect(
drag: ResizeDragState,
clientX: number,
clientY: number,
imageSize: Size,
bounds: CropBounds
bounds: CropBounds,
minCropSize: Size = DEFAULT_MIN_CROP_SIZE
): NormalizedRect {
const s = drag.startRect;
const pixelW = s.width * imageSize.width;
Expand All @@ -234,7 +251,8 @@ export function computeShiftLockedResizeRect(
clientX,
clientY,
imageSize,
bounds
bounds,
minCropSize
);
}
const normalizedRatio = s.width / s.height;
Expand All @@ -252,7 +270,8 @@ export function computeShiftLockedResizeRect(
clientY,
imageSize,
bounds,
normalizedRatio
normalizedRatio,
minCropSize
);
}

Expand All @@ -263,7 +282,8 @@ export function computeShiftLockedResizeRect(
clientX,
clientY,
imageSize,
bounds
bounds,
minCropSize
);

if ( handle === 'n' || handle === 's' ) {
Expand All @@ -281,11 +301,11 @@ export function computeShiftLockedResizeRect(
}
// Enforce the minimum on the driving axis, then derive the
// other axis from the ratio so it stays consistent. The
// effective minimum height has to satisfy MIN_CROP_SIZE on
// both axes simultaneously.
// effective minimum height has to satisfy the per-axis floor
// on both axes simultaneously.
const minHeight = Math.max(
MIN_CROP_SIZE,
MIN_CROP_SIZE / normalizedRatio
minCropSize.height,
minCropSize.width / normalizedRatio
);
if ( newHeight < minHeight ) {
newHeight = minHeight;
Expand All @@ -312,7 +332,10 @@ export function computeShiftLockedResizeRect(
newHeight = maxHeight;
newWidth = newHeight * normalizedRatio;
}
const minWidth = Math.max( MIN_CROP_SIZE, MIN_CROP_SIZE * normalizedRatio );
const minWidth = Math.max(
minCropSize.width,
minCropSize.height * normalizedRatio
);
if ( newWidth < minWidth ) {
newWidth = minWidth;
newHeight = newWidth / normalizedRatio;
Expand Down
24 changes: 23 additions & 1 deletion packages/media-editor/src/image-editor/core/test/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CropperState, Size } from '../types';
import { DEFAULT_STATE } from '../constants';
import { DEFAULT_STATE, MAX_ZOOM } from '../constants';
import { cropperReducer, enforceContainment, isStateDirty } from '../state';
import {
createCamera,
Expand Down Expand Up @@ -268,6 +268,28 @@ describe( 'cropperReducer — SETTLE_CROP', () => {
expectSameVisibleRegion( state, settled );
} );

it( 'caps zoom at MAX_ZOOM for tight crops and keeps the visible region', () => {
// A 5% crop at zoom=1 would otherwise settle to zoom=20 (1/0.05),
// pushing state past the user-facing zoom cap and breaking wheel
// and slider interactions that clamp to MAX_ZOOM. With the cap,
// the post-settle crop is smaller than the viewport but the
// content the user framed is preserved.
const state = makeState( {
cropRect: { x: 0.4, y: 0.4, width: 0.05, height: 0.05 },
zoom: 1,
pan: { x: 0, y: 0 },
} );
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );

expect( settled.zoom ).toBeCloseTo( MAX_ZOOM, 5 );
// The scale was capped at MAX_ZOOM/zoom (=10), not at the uncapped
// fit scale of 20 — so the new crop is 5% × 10 = 50% of the viewport,
// not 100%.
expect( settled.cropRect.width ).toBeCloseTo( 0.5, 5 );
expect( settled.cropRect.height ).toBeCloseTo( 0.5, 5 );
expectSameVisibleRegion( state, settled );
} );

it( 'is a no-op when crop is already full-sized and centered', () => {
const state = makeState( {
cropRect: { x: 0, y: 0, width: 1, height: 1 },
Expand Down
Loading
Loading