@wordpress/media-editor 0.11.0 → 0.11.2-next.v.202606191442.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/build/components/media-editor/index.cjs +41 -51
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs.map +1 -1
- package/build/components/media-editor-canvas/index.cjs +63 -15
- package/build/components/media-editor-canvas/index.cjs.map +3 -3
- package/build/components/media-editor-crop-panel/index.cjs +15 -45
- package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +230 -0
- package/build/components/media-editor-image-controls/index.cjs.map +7 -0
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +4 -4
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
- package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -1
- package/build/components/media-form/index.cjs +12 -23
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/rotation-ruler/index.cjs +1 -1
- package/build/components/rotation-ruler/index.cjs.map +1 -1
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
- package/build/image-editor/core/camera.cjs +13 -0
- package/build/image-editor/core/camera.cjs.map +2 -2
- package/build/image-editor/core/constants.cjs +15 -3
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs.map +1 -1
- package/build/image-editor/core/crop-rect.cjs.map +1 -1
- package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -1
- package/build/image-editor/core/interaction-controller.cjs +9 -7
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/math/sanitize.cjs.map +1 -1
- package/build/image-editor/core/setter-helpers.cjs.map +1 -1
- package/build/image-editor/core/source-region.cjs +111 -2
- package/build/image-editor/core/source-region.cjs.map +2 -2
- package/build/image-editor/core/state.cjs.map +1 -1
- package/build/image-editor/core/stencil-math.cjs +14 -6
- package/build/image-editor/core/stencil-math.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs.map +1 -1
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/react/components/cropper.cjs +231 -49
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -1
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +1 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +65 -19
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -1
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +1 -1
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -1
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -1
- package/build/index.cjs.map +1 -1
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +1 -1
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +41 -51
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
- package/build-module/components/media-editor-canvas/index.mjs +55 -17
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +8 -51
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +221 -0
- package/build-module/components/media-editor-image-controls/index.mjs.map +7 -0
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +4 -4
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -1
- package/build-module/components/media-form/index.mjs +12 -13
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +1 -1
- package/build-module/components/rotation-ruler/index.mjs.map +1 -1
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -1
- package/build-module/image-editor/core/camera.mjs +12 -0
- package/build-module/image-editor/core/camera.mjs.map +2 -2
- package/build-module/image-editor/core/constants.mjs +10 -2
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs.map +1 -1
- package/build-module/image-editor/core/crop-rect.mjs.map +1 -1
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -1
- package/build-module/image-editor/core/interaction-controller.mjs +9 -7
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/math/sanitize.mjs.map +1 -1
- package/build-module/image-editor/core/setter-helpers.mjs.map +1 -1
- package/build-module/image-editor/core/source-region.mjs +108 -1
- package/build-module/image-editor/core/source-region.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs.map +1 -1
- package/build-module/image-editor/core/stencil-math.mjs +12 -5
- package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs.map +1 -1
- package/build-module/image-editor/react/components/cropper.mjs +242 -52
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -1
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +1 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +65 -19
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -1
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -1
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -1
- package/build-module/index.mjs.map +1 -1
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +1 -1
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-style/style-rtl.css +179 -47
- package/build-style/style.css +179 -46
- package/build-types/components/media-editor/index.d.ts +6 -7
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +11 -6
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts +4 -3
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts +46 -0
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -0
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts +25 -1
- package/build-types/image-editor/core/camera.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +30 -1
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/source-region.d.ts +23 -1
- package/build-types/image-editor/core/source-region.d.ts.map +1 -1
- package/build-types/image-editor/core/stencil-math.d.ts +22 -4
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
- package/build-types/image-editor/core/types.d.ts +14 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +17 -14
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/package.json +30 -20
- package/src/components/media-editor/index.tsx +69 -77
- package/src/components/media-editor/style.scss +136 -4
- package/src/components/media-editor-canvas/index.tsx +82 -13
- package/src/components/media-editor-canvas/style.scss +41 -0
- package/src/components/media-editor-crop-panel/index.tsx +16 -62
- package/src/components/media-editor-crop-panel/test/index.tsx +25 -41
- package/src/components/media-editor-fine-rotation/index.tsx +4 -3
- package/src/components/media-editor-image-controls/index.tsx +262 -0
- package/src/components/media-editor-image-controls/style.scss +48 -0
- package/src/components/media-editor-image-controls/test/index.tsx +150 -0
- package/src/components/media-editor-modal/style.scss +32 -42
- package/src/components/media-form/index.tsx +17 -21
- package/src/components/media-form/style.scss +6 -0
- package/src/components/rotation-ruler/style.scss +10 -3
- package/src/image-editor/core/camera.ts +60 -1
- package/src/image-editor/core/constants.ts +34 -1
- package/src/image-editor/core/interaction-controller.ts +25 -11
- package/src/image-editor/core/source-region.ts +202 -1
- package/src/image-editor/core/stencil-math.ts +42 -10
- package/src/image-editor/core/test/camera.ts +272 -1
- package/src/image-editor/core/test/interaction-controller.ts +100 -0
- package/src/image-editor/core/test/stencil-math.ts +153 -0
- package/src/image-editor/core/types.ts +17 -0
- package/src/image-editor/react/components/cropper.scss +28 -17
- package/src/image-editor/react/components/cropper.tsx +362 -62
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +1 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +101 -32
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +169 -17
- package/src/image-editor/react/components/test/cropper.tsx +689 -6
- package/src/style.scss +2 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/build/components/media-editor-transform-controls/index.cjs +0 -84
- package/build/components/media-editor-transform-controls/index.cjs.map +0 -7
- package/build/components/media-form/sidebar-datetime-view.cjs +0 -53
- package/build/components/media-form/sidebar-datetime-view.cjs.map +0 -7
- package/build-module/components/media-editor-transform-controls/index.mjs +0 -68
- package/build-module/components/media-editor-transform-controls/index.mjs.map +0 -7
- package/build-module/components/media-form/sidebar-datetime-view.mjs +0 -32
- package/build-module/components/media-form/sidebar-datetime-view.mjs.map +0 -7
- package/build-types/components/media-editor-transform-controls/index.d.ts +0 -8
- package/build-types/components/media-editor-transform-controls/index.d.ts.map +0 -1
- package/build-types/components/media-form/sidebar-datetime-view.d.ts +0 -10
- package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +0 -1
- package/src/components/media-editor-transform-controls/index.tsx +0 -70
- package/src/components/media-editor-transform-controls/style.scss +0 -8
- package/src/components/media-form/sidebar-datetime-view.tsx +0 -47
- package/src/components/media-form/test/sidebar-datetime-view.test.tsx +0 -96
|
@@ -6,7 +6,13 @@ import { mat2d, vec2 } from 'gl-matrix';
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
CropperState,
|
|
11
|
+
NormalizedPoint,
|
|
12
|
+
NormalizedRect,
|
|
13
|
+
Size,
|
|
14
|
+
Camera,
|
|
15
|
+
} from './types';
|
|
10
16
|
import { degreesToRadians } from './math/rotation';
|
|
11
17
|
import {
|
|
12
18
|
isSafeNumber,
|
|
@@ -111,6 +117,59 @@ export function getImageFit(
|
|
|
111
117
|
};
|
|
112
118
|
}
|
|
113
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Resolve the presentational view magnification that makes the crop fill the
|
|
122
|
+
* canvas.
|
|
123
|
+
*
|
|
124
|
+
* The crop overlay is laid out as `cropRect * visualSize` (the contain-fit
|
|
125
|
+
* footprint, with no zoom), so its on-screen size is capped by that footprint.
|
|
126
|
+
* When a crop's aspect differs from the image's, it can be much smaller than
|
|
127
|
+
* the canvas could show (e.g. a square crop in a tall image is bound by the
|
|
128
|
+
* footprint's narrow axis). Scaling `elementSize` and `visualSize` together by
|
|
129
|
+
* the returned factor uniformly magnifies the whole scene — image, crop
|
|
130
|
+
* overlay, and overlays — so the crop reaches `targetFill` of its constraining
|
|
131
|
+
* canvas axis, with the image bleeding past the canvas (clipped by the host).
|
|
132
|
+
*
|
|
133
|
+
* Purely presentational: it does not touch `zoom`/`pan`/`cropRect`/`rotation`,
|
|
134
|
+
* so the framed source region — and therefore the export — is unchanged.
|
|
135
|
+
*
|
|
136
|
+
* @param cropRect The crop rectangle, normalized to the footprint.
|
|
137
|
+
* @param canvasSize The canvas size in pixels.
|
|
138
|
+
* @param visualSize The contain-fit (rotated) image footprint in pixels.
|
|
139
|
+
* @param targetFill Fraction of the constraining canvas axis to fill (0–1].
|
|
140
|
+
* @param maxScale Upper bound on the magnification.
|
|
141
|
+
* @return A magnification factor in [1, maxScale]; 1 when none is needed.
|
|
142
|
+
*/
|
|
143
|
+
export function getViewScale(
|
|
144
|
+
cropRect: NormalizedRect,
|
|
145
|
+
canvasSize: Size,
|
|
146
|
+
visualSize: Size,
|
|
147
|
+
targetFill: number,
|
|
148
|
+
maxScale: number
|
|
149
|
+
): number {
|
|
150
|
+
const cropScreenW = cropRect.width * visualSize.width;
|
|
151
|
+
const cropScreenH = cropRect.height * visualSize.height;
|
|
152
|
+
// `> 0` comparisons (rather than `<= 0`) reject NaN as well as zero and
|
|
153
|
+
// negatives, so a non-finite rect/dimension/scale can't propagate to a
|
|
154
|
+
// `scale(NaN)`. `isValidSize` already guards finiteness of the sizes.
|
|
155
|
+
if (
|
|
156
|
+
! isValidSize( canvasSize ) ||
|
|
157
|
+
! isValidSize( visualSize ) ||
|
|
158
|
+
! ( cropScreenW > 0 ) ||
|
|
159
|
+
! ( cropScreenH > 0 ) ||
|
|
160
|
+
! Number.isFinite( targetFill ) ||
|
|
161
|
+
! Number.isFinite( maxScale ) ||
|
|
162
|
+
! ( targetFill > 0 ) ||
|
|
163
|
+
! ( maxScale >= 1 )
|
|
164
|
+
) {
|
|
165
|
+
return 1;
|
|
166
|
+
}
|
|
167
|
+
const kW = ( targetFill * canvasSize.width ) / cropScreenW;
|
|
168
|
+
const kH = ( targetFill * canvasSize.height ) / cropScreenH;
|
|
169
|
+
const k = Math.min( kW, kH );
|
|
170
|
+
return Math.min( maxScale, Math.max( 1, k ) );
|
|
171
|
+
}
|
|
172
|
+
|
|
114
173
|
/**
|
|
115
174
|
* Compose a camera matrix from cropper state, container, and image dimensions.
|
|
116
175
|
*
|
|
@@ -28,7 +28,40 @@ export const MAX_ZOOM = 10;
|
|
|
28
28
|
* enough to allow tight crops (favicons, icons). Adjust here to tune the
|
|
29
29
|
* floor globally.
|
|
30
30
|
*/
|
|
31
|
-
export const MIN_CROP_PIXELS =
|
|
31
|
+
export const MIN_CROP_PIXELS = 16;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Minimum on-screen crop rect dimension in CSS pixels. The source-pixel
|
|
35
|
+
* floor (`MIN_CROP_PIXELS`) keeps crops from going sub-pixel, but on a large
|
|
36
|
+
* image fit into a small canvas it can render only a few CSS pixels wide —
|
|
37
|
+
* too small to grab the resize handles. This floor keeps the crop operable
|
|
38
|
+
* regardless of display scale; it binds when zoomed out and yields to the
|
|
39
|
+
* source-pixel floor once the image is shown large enough. Sized to one
|
|
40
|
+
* handle touch target (`$handle-touch-target-size` in `cropper.scss`).
|
|
41
|
+
*/
|
|
42
|
+
export const MIN_CROP_SCREEN_PX = 44;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Fraction of the constraining canvas axis the settled crop should occupy on
|
|
46
|
+
* screen. When a crop is shown smaller than this (e.g. a square crop in a tall
|
|
47
|
+
* image, capped by the contain-fit footprint's narrow axis), the view magnifies
|
|
48
|
+
* to fill the canvas. `1` = always fill the canvas; lower = rescue only the
|
|
49
|
+
* under-filled cases. See `getViewScale`.
|
|
50
|
+
*/
|
|
51
|
+
export const SETTLE_TARGET_CANVAS_FILL = 0.8;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Upper bound on the presentational view magnification (`getViewScale`).
|
|
55
|
+
* Prevents a near-degenerate crop from magnifying the scene without limit.
|
|
56
|
+
*/
|
|
57
|
+
export const MAX_VIEW_SCALE = 8;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* CSS pixels per source pixel required before resize handles snap the selected
|
|
61
|
+
* source region to whole-pixel edges. Below 1:1, a source pixel is sub-pixel on
|
|
62
|
+
* screen, so snapping would feel notchy without a visible pixel boundary.
|
|
63
|
+
*/
|
|
64
|
+
export const PIXEL_SNAP_DISPLAY_SCALE = 1;
|
|
32
65
|
|
|
33
66
|
/**
|
|
34
67
|
* Wheel zoom sensitivity. A deltaY of 100 changes zoom by 0.25.
|
|
@@ -286,6 +286,11 @@ export class InteractionController {
|
|
|
286
286
|
if ( e.button !== 0 ) {
|
|
287
287
|
return;
|
|
288
288
|
}
|
|
289
|
+
// Touch gestures are handled by touchstart/touchmove. Starting the
|
|
290
|
+
// pointer drag path too can briefly toggle drag UI before pinch begins.
|
|
291
|
+
if ( e.pointerType === 'touch' ) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
289
294
|
e.preventDefault();
|
|
290
295
|
|
|
291
296
|
// Blur any focused handle so its focus ring doesn't linger,
|
|
@@ -568,6 +573,7 @@ export class InteractionController {
|
|
|
568
573
|
if ( ! touch ) {
|
|
569
574
|
return;
|
|
570
575
|
}
|
|
576
|
+
moveEvent.preventDefault();
|
|
571
577
|
|
|
572
578
|
cancelAnimationFrame( this.rafId );
|
|
573
579
|
this.rafId = requestAnimationFrame( () => {
|
|
@@ -641,20 +647,28 @@ export class InteractionController {
|
|
|
641
647
|
? ( my - startMy ) / visSize.height
|
|
642
648
|
: 0;
|
|
643
649
|
|
|
644
|
-
// Focal-point zoom correction.
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
const
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
650
|
+
// Focal-point zoom correction. Keep this based on the
|
|
651
|
+
// pinch-start state; using the current reducer state here
|
|
652
|
+
// compounds prior touchmove frames and makes a fixed pinch
|
|
653
|
+
// center drift after the image is already zoomed or panned.
|
|
654
|
+
const zoomRatio =
|
|
655
|
+
touch.startZoom !== 0
|
|
656
|
+
? 1 - newZoom / touch.startZoom
|
|
657
|
+
: 0;
|
|
658
|
+
const startFocalNormX = startMx / visSize.width;
|
|
659
|
+
const startFocalNormY = startMy / visSize.height;
|
|
652
660
|
|
|
653
|
-
// Combined:
|
|
661
|
+
// Combined: midpoint drift + zoom around the pinch-start
|
|
662
|
+
// midpoint, so moving both fingers together pans while
|
|
663
|
+
// symmetric pinches keep the start center stationary.
|
|
654
664
|
const newCropX =
|
|
655
|
-
touch.startPanX +
|
|
665
|
+
touch.startPanX +
|
|
666
|
+
panDx +
|
|
667
|
+
( startFocalNormX - touch.startPanX ) * zoomRatio;
|
|
656
668
|
const newCropY =
|
|
657
|
-
touch.startPanY +
|
|
669
|
+
touch.startPanY +
|
|
670
|
+
panDy +
|
|
671
|
+
( startFocalNormY - touch.startPanY ) * zoomRatio;
|
|
658
672
|
|
|
659
673
|
const { pan: clampedCrop } = restrictPanZoom(
|
|
660
674
|
{
|
|
@@ -6,9 +6,15 @@ import { mat2d, vec2 } from 'gl-matrix';
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
CropperState,
|
|
11
|
+
HandlePosition,
|
|
12
|
+
NormalizedRect,
|
|
13
|
+
Size,
|
|
14
|
+
} from './types';
|
|
10
15
|
import { createCamera, getRotatedBBox, getVisibleBounds } from './camera';
|
|
11
16
|
import { isValidSize, sanitizeCropperState } from './math/sanitize';
|
|
17
|
+
import { normalizeRotation } from './math/rotation';
|
|
12
18
|
|
|
13
19
|
/**
|
|
14
20
|
* The selected image region in source-pixel coordinates.
|
|
@@ -127,6 +133,201 @@ export function getSourceRegion(
|
|
|
127
133
|
};
|
|
128
134
|
}
|
|
129
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Convert a source-pixel region back to the cropper's normalized crop rect.
|
|
138
|
+
*
|
|
139
|
+
* The math intentionally mirrors `getSourceRegion`: map the requested source
|
|
140
|
+
* center through the current camera, then express that screen point inside the
|
|
141
|
+
* same base visible bounds used to position crop overlays.
|
|
142
|
+
*
|
|
143
|
+
* @param state The current cropper state.
|
|
144
|
+
* @param imageSize The natural dimensions of the source image.
|
|
145
|
+
* @param region The source-pixel region to represent as a crop rect.
|
|
146
|
+
* @return The normalized crop rect, or null when inputs cannot be mapped.
|
|
147
|
+
*/
|
|
148
|
+
function getCropRectFromSourceRegion(
|
|
149
|
+
state: CropperState,
|
|
150
|
+
imageSize: Size,
|
|
151
|
+
region: SourceRegion
|
|
152
|
+
): NormalizedRect | null {
|
|
153
|
+
if (
|
|
154
|
+
! isValidSize( imageSize ) ||
|
|
155
|
+
region.width <= 0 ||
|
|
156
|
+
region.height <= 0
|
|
157
|
+
) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const safeState = sanitizeCropperState( state );
|
|
162
|
+
const syntheticContainer: Size = { width: 1000, height: 1000 };
|
|
163
|
+
const baseCamera = createCamera(
|
|
164
|
+
{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },
|
|
165
|
+
syntheticContainer,
|
|
166
|
+
imageSize
|
|
167
|
+
);
|
|
168
|
+
const visibleBounds = getVisibleBounds( baseCamera );
|
|
169
|
+
if ( visibleBounds.width <= 0 || visibleBounds.height <= 0 ) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const sourceCenter = vec2.fromValues(
|
|
174
|
+
( region.x + region.width / 2 ) / imageSize.width,
|
|
175
|
+
( region.y + region.height / 2 ) / imageSize.height
|
|
176
|
+
);
|
|
177
|
+
const screenCenter = vec2.create();
|
|
178
|
+
const camera = createCamera( safeState, syntheticContainer, imageSize );
|
|
179
|
+
vec2.transformMat2d( screenCenter, sourceCenter, camera );
|
|
180
|
+
|
|
181
|
+
const snapRotation = Math.round( safeState.rotation / 90 ) * 90;
|
|
182
|
+
const { width: rotW, height: rotH } = getRotatedBBox(
|
|
183
|
+
imageSize.width,
|
|
184
|
+
imageSize.height,
|
|
185
|
+
snapRotation
|
|
186
|
+
);
|
|
187
|
+
if ( rotW <= 0 || rotH <= 0 ) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const width = ( region.width * safeState.zoom ) / rotW;
|
|
192
|
+
const height = ( region.height * safeState.zoom ) / rotH;
|
|
193
|
+
const centerX =
|
|
194
|
+
( screenCenter[ 0 ] - visibleBounds.left ) / visibleBounds.width;
|
|
195
|
+
const centerY =
|
|
196
|
+
( screenCenter[ 1 ] - visibleBounds.top ) / visibleBounds.height;
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
x: centerX - width / 2,
|
|
200
|
+
y: centerY - height / 2,
|
|
201
|
+
width,
|
|
202
|
+
height,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface SourcePixelSnapEdges {
|
|
207
|
+
left: boolean;
|
|
208
|
+
top: boolean;
|
|
209
|
+
right: boolean;
|
|
210
|
+
bottom: boolean;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const CARDINAL_ROTATION_EPSILON = 1e-6;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Check whether a rotation is effectively at a 90-degree stop.
|
|
217
|
+
*
|
|
218
|
+
* @param rotation The rotation angle in degrees.
|
|
219
|
+
* @return Whether the rotation is close enough to a cardinal angle.
|
|
220
|
+
*/
|
|
221
|
+
function isCardinalRotation( rotation: number ): boolean {
|
|
222
|
+
const normalizedRotation = normalizeRotation( rotation );
|
|
223
|
+
const nearestCardinal = Math.round( normalizedRotation / 90 ) * 90;
|
|
224
|
+
return (
|
|
225
|
+
Math.abs( normalizedRotation - nearestCardinal ) <
|
|
226
|
+
CARDINAL_ROTATION_EPSILON
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Snap a crop rect so the selected source-region edges land on whole pixels.
|
|
232
|
+
*
|
|
233
|
+
* This is a display-time affordance for visible pixel grids; it does not alter
|
|
234
|
+
* pan/zoom/rotation. Callers decide when snapping is appropriate.
|
|
235
|
+
*
|
|
236
|
+
* @param state The current cropper state.
|
|
237
|
+
* @param imageSize The natural dimensions of the source image.
|
|
238
|
+
* @param cropRect The candidate crop rect from resize math.
|
|
239
|
+
* @param handle The active resize handle. Only edges moved by the handle snap.
|
|
240
|
+
* @return A crop rect whose selected source-region edges are whole pixels.
|
|
241
|
+
*/
|
|
242
|
+
export function snapCropRectToSourcePixels(
|
|
243
|
+
state: CropperState,
|
|
244
|
+
imageSize: Size,
|
|
245
|
+
cropRect: NormalizedRect,
|
|
246
|
+
handle: HandlePosition
|
|
247
|
+
): NormalizedRect {
|
|
248
|
+
const safeState = sanitizeCropperState( state );
|
|
249
|
+
if ( ! isCardinalRotation( safeState.rotation ) ) {
|
|
250
|
+
return cropRect;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return snapCropRectEdgesToSourcePixels( safeState, imageSize, cropRect, {
|
|
254
|
+
left: handle.includes( 'w' ),
|
|
255
|
+
top: handle.includes( 'n' ),
|
|
256
|
+
right: handle.includes( 'e' ),
|
|
257
|
+
bottom: handle.includes( 's' ),
|
|
258
|
+
} );
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Snap all source-region edges for a crop rect to the nearest whole pixels.
|
|
263
|
+
*
|
|
264
|
+
* @param state The current cropper state.
|
|
265
|
+
* @param imageSize The natural dimensions of the source image.
|
|
266
|
+
* @param cropRect The crop rect to align to the source-pixel grid.
|
|
267
|
+
* @return A crop rect whose source-region edges are whole pixels.
|
|
268
|
+
*/
|
|
269
|
+
export function snapCropRectToSourcePixelGrid(
|
|
270
|
+
state: CropperState,
|
|
271
|
+
imageSize: Size,
|
|
272
|
+
cropRect: NormalizedRect
|
|
273
|
+
): NormalizedRect {
|
|
274
|
+
return snapCropRectEdgesToSourcePixels( state, imageSize, cropRect, {
|
|
275
|
+
left: true,
|
|
276
|
+
top: true,
|
|
277
|
+
right: true,
|
|
278
|
+
bottom: true,
|
|
279
|
+
} );
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function snapCropRectEdgesToSourcePixels(
|
|
283
|
+
state: CropperState,
|
|
284
|
+
imageSize: Size,
|
|
285
|
+
cropRect: NormalizedRect,
|
|
286
|
+
edges: SourcePixelSnapEdges
|
|
287
|
+
): NormalizedRect {
|
|
288
|
+
if ( ! isValidSize( imageSize ) ) {
|
|
289
|
+
return cropRect;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const stateWithCrop = sanitizeCropperState( { ...state, cropRect } );
|
|
293
|
+
const region = getSourceRegion( stateWithCrop, imageSize );
|
|
294
|
+
const shouldSnapX = edges.left || edges.right;
|
|
295
|
+
const shouldSnapY = edges.top || edges.bottom;
|
|
296
|
+
const left = edges.left ? Math.round( region.x ) : region.x;
|
|
297
|
+
const top = edges.top ? Math.round( region.y ) : region.y;
|
|
298
|
+
const right = edges.right
|
|
299
|
+
? Math.round( region.x + region.width )
|
|
300
|
+
: region.x + region.width;
|
|
301
|
+
const bottom = edges.bottom
|
|
302
|
+
? Math.round( region.y + region.height )
|
|
303
|
+
: region.y + region.height;
|
|
304
|
+
if ( right <= left || bottom <= top ) {
|
|
305
|
+
return cropRect;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const snappedCropRect = getCropRectFromSourceRegion(
|
|
309
|
+
stateWithCrop,
|
|
310
|
+
imageSize,
|
|
311
|
+
{
|
|
312
|
+
...region,
|
|
313
|
+
x: left,
|
|
314
|
+
y: top,
|
|
315
|
+
width: right - left,
|
|
316
|
+
height: bottom - top,
|
|
317
|
+
}
|
|
318
|
+
);
|
|
319
|
+
if ( ! snappedCropRect ) {
|
|
320
|
+
return cropRect;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
x: shouldSnapX ? snappedCropRect.x : cropRect.x,
|
|
325
|
+
y: shouldSnapY ? snappedCropRect.y : cropRect.y,
|
|
326
|
+
width: shouldSnapX ? snappedCropRect.width : cropRect.width,
|
|
327
|
+
height: shouldSnapY ? snappedCropRect.height : cropRect.height,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
130
331
|
/**
|
|
131
332
|
* The selected crop region expressed as percentages of the source image.
|
|
132
333
|
*/
|
|
@@ -1,10 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
+
import { MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX } from './constants';
|
|
4
5
|
import type { HandlePosition, NormalizedRect, Size } from './types';
|
|
5
6
|
|
|
6
7
|
export type { HandlePosition };
|
|
7
8
|
|
|
9
|
+
/** Axis whose resize distance controls the locked-ratio size projection. */
|
|
10
|
+
export type ResizeDriverAxis = 'width' | 'height';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Resolve the minimum crop dimension, in source-image pixels, for the current
|
|
14
|
+
* display scale.
|
|
15
|
+
*
|
|
16
|
+
* Two floors compete: a fixed source-pixel floor (`MIN_CROP_PIXELS`) that
|
|
17
|
+
* avoids sub-pixel crops, and an on-screen floor (`MIN_CROP_SCREEN_PX`) that
|
|
18
|
+
* keeps the crop large enough to grab. The on-screen floor converted to source
|
|
19
|
+
* pixels is `MIN_CROP_SCREEN_PX / displayScale`; the binding constraint is the
|
|
20
|
+
* larger of the two. When the image is shown small (small `displayScale`) the
|
|
21
|
+
* on-screen floor dominates; once it is shown large enough the source-pixel
|
|
22
|
+
* floor takes over.
|
|
23
|
+
*
|
|
24
|
+
* @param displayScale CSS pixels rendered per source pixel
|
|
25
|
+
* (fit scale × zoom × view scale).
|
|
26
|
+
* @return Minimum crop dimension in source-image pixels.
|
|
27
|
+
*/
|
|
28
|
+
export function getMinCropPixels( displayScale: number ): number {
|
|
29
|
+
if ( displayScale <= 0 ) {
|
|
30
|
+
return MIN_CROP_PIXELS;
|
|
31
|
+
}
|
|
32
|
+
return Math.max( MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX / displayScale );
|
|
33
|
+
}
|
|
34
|
+
|
|
8
35
|
/**
|
|
9
36
|
* Default minimum crop rect dimension in normalized space, used when no
|
|
10
37
|
* explicit per-axis minimum is supplied (e.g. before the source image
|
|
@@ -118,6 +145,8 @@ export function computeFreeResizeRect(
|
|
|
118
145
|
* @param bounds The allowed crop area bounds.
|
|
119
146
|
* @param normalizedRatio The locked aspect ratio (width / height in normalized space).
|
|
120
147
|
* @param minCropSize Minimum crop rect dimension in normalized space, per axis.
|
|
148
|
+
* @param driverAxis Optional explicit driver axis. Used for keyboard
|
|
149
|
+
* resize, where one arrow axis should determine the step.
|
|
121
150
|
* @return The new crop rect in normalized coordinates.
|
|
122
151
|
*/
|
|
123
152
|
export function computeLockedResizeRect(
|
|
@@ -127,7 +156,8 @@ export function computeLockedResizeRect(
|
|
|
127
156
|
imageSize: Size,
|
|
128
157
|
bounds: CropBounds,
|
|
129
158
|
normalizedRatio: number,
|
|
130
|
-
minCropSize: Size = DEFAULT_MIN_CROP_SIZE
|
|
159
|
+
minCropSize: Size = DEFAULT_MIN_CROP_SIZE,
|
|
160
|
+
driverAxis?: ResizeDriverAxis
|
|
131
161
|
): NormalizedRect {
|
|
132
162
|
// The math below divides by `normalizedRatio` and `imageSize`, so
|
|
133
163
|
// bail out with the start rect when any of them is zero. This can
|
|
@@ -175,16 +205,18 @@ export function computeLockedResizeRect(
|
|
|
175
205
|
distW = Math.max( distW, minDistW );
|
|
176
206
|
distH = Math.max( distH, minDistH );
|
|
177
207
|
|
|
178
|
-
// Determine which axis "drives"
|
|
179
|
-
//
|
|
180
|
-
//
|
|
181
|
-
//
|
|
182
|
-
// compare the pixel motion ratio against that pixel-space ratio
|
|
183
|
-
// so the units line up (was a unit mismatch on non-square images).
|
|
184
|
-
const pixelDistW = distW * imageSize.width;
|
|
185
|
-
const pixelDistH = distH * imageSize.height;
|
|
208
|
+
// Determine which axis "drives". Pointer drags use the dragged-corner
|
|
209
|
+
// geometry so the projected crop changes continuously as the pointer moves.
|
|
210
|
+
// Keyboard resize passes an explicit axis because a single-axis arrow step
|
|
211
|
+
// should drive that axis instead of being cancelled by the unchanged axis.
|
|
186
212
|
const pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;
|
|
187
|
-
|
|
213
|
+
const isWidthDriver =
|
|
214
|
+
driverAxis === 'width' ||
|
|
215
|
+
( ! driverAxis &&
|
|
216
|
+
( distW * imageSize.width ) / ( distH * imageSize.height ) >
|
|
217
|
+
pixelRatio );
|
|
218
|
+
|
|
219
|
+
if ( isWidthDriver ) {
|
|
188
220
|
// Width is the driver — compute height from ratio.
|
|
189
221
|
distH = distW / normalizedRatio;
|
|
190
222
|
} else {
|