@wordpress/media-editor 0.7.1-next.v.202604201441.0 → 0.8.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/build/components/media-editor-canvas/index.cjs +54 -0
- package/build/components/media-editor-canvas/index.cjs.map +7 -0
- package/build/components/media-editor-crop-panel/index.cjs +116 -0
- package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
- package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
- package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +497 -0
- package/build/components/media-editor-modal/index.cjs.map +7 -0
- package/build/components/media-editor-toolbar/index.cjs +146 -0
- package/build/components/media-editor-toolbar/index.cjs.map +7 -0
- package/build/image-editor/core/camera.cjs +194 -0
- package/build/image-editor/core/camera.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +81 -0
- package/build/image-editor/core/constants.cjs.map +7 -0
- package/build/image-editor/core/containment.cjs +245 -0
- package/build/image-editor/core/containment.cjs.map +7 -0
- package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
- package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +50 -0
- package/build/image-editor/core/index.cjs.map +7 -0
- package/build/image-editor/core/interaction-controller.cjs +608 -0
- package/build/image-editor/core/interaction-controller.cjs.map +7 -0
- package/build/image-editor/core/math/rotation.cjs +41 -0
- package/build/image-editor/core/math/rotation.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +95 -0
- package/build/image-editor/core/source-region.cjs.map +7 -0
- package/build/image-editor/core/state.cjs +269 -0
- package/build/image-editor/core/state.cjs.map +7 -0
- package/build/image-editor/core/stencil-math.cjs +197 -0
- package/build/image-editor/core/stencil-math.cjs.map +7 -0
- package/build/image-editor/core/transform-style.cjs +46 -0
- package/build/image-editor/core/transform-style.cjs.map +7 -0
- package/build/image-editor/core/transforms/pipeline.cjs +43 -0
- package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
- package/build/image-editor/core/types.cjs +19 -0
- package/build/image-editor/core/types.cjs.map +7 -0
- package/build/image-editor/index.cjs +57 -0
- package/build/image-editor/index.cjs.map +7 -0
- package/build/image-editor/react/components/cropper-provider.cjs +50 -0
- package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +365 -0
- package/build/image-editor/react/components/cropper.cjs.map +7 -0
- package/build/image-editor/react/components/index.cjs +36 -0
- package/build/image-editor/react/components/index.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +31 -0
- package/build/image-editor/react/hooks/index.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
- package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
- package/build/index.cjs +5 -2
- package/build/index.cjs.map +2 -2
- package/build/lock-unlock.cjs +37 -0
- package/build/lock-unlock.cjs.map +7 -0
- package/build/private-apis.cjs +38 -0
- package/build/private-apis.cjs.map +7 -0
- package/build/store/actions.cjs +45 -0
- package/build/store/actions.cjs.map +7 -0
- package/build/store/constants.cjs +31 -0
- package/build/store/constants.cjs.map +7 -0
- package/build/store/index.cjs +51 -0
- package/build/store/index.cjs.map +7 -0
- package/build/store/reducer.cjs +51 -0
- package/build/store/reducer.cjs.map +7 -0
- package/build/store/selectors.cjs +43 -0
- package/build/store/selectors.cjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +33 -0
- package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
- package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
- package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +483 -0
- package/build-module/components/media-editor-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-toolbar/index.mjs +130 -0
- package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
- package/build-module/image-editor/core/camera.mjs +163 -0
- package/build-module/image-editor/core/camera.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +51 -0
- package/build-module/image-editor/core/constants.mjs.map +7 -0
- package/build-module/image-editor/core/containment.mjs +218 -0
- package/build-module/image-editor/core/containment.mjs.map +7 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +24 -0
- package/build-module/image-editor/core/index.mjs.map +7 -0
- package/build-module/image-editor/core/interaction-controller.mjs +583 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
- package/build-module/image-editor/core/math/rotation.mjs +15 -0
- package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +69 -0
- package/build-module/image-editor/core/source-region.mjs.map +7 -0
- package/build-module/image-editor/core/state.mjs +242 -0
- package/build-module/image-editor/core/state.mjs.map +7 -0
- package/build-module/image-editor/core/stencil-math.mjs +169 -0
- package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
- package/build-module/image-editor/core/transform-style.mjs +21 -0
- package/build-module/image-editor/core/transform-style.mjs.map +7 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
- package/build-module/image-editor/core/types.mjs +1 -0
- package/build-module/image-editor/core/types.mjs.map +7 -0
- package/build-module/image-editor/index.mjs +30 -0
- package/build-module/image-editor/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +337 -0
- package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
- package/build-module/image-editor/react/components/index.mjs +9 -0
- package/build-module/image-editor/react/components/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +6 -0
- package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
- package/build-module/index.mjs +3 -1
- package/build-module/index.mjs.map +2 -2
- package/build-module/lock-unlock.mjs +11 -0
- package/build-module/lock-unlock.mjs.map +7 -0
- package/build-module/private-apis.mjs +13 -0
- package/build-module/private-apis.mjs.map +7 -0
- package/build-module/store/actions.mjs +19 -0
- package/build-module/store/actions.mjs.map +7 -0
- package/build-module/store/constants.mjs +6 -0
- package/build-module/store/constants.mjs.map +7 -0
- package/build-module/store/index.mjs +16 -0
- package/build-module/store/index.mjs.map +7 -0
- package/build-module/store/reducer.mjs +26 -0
- package/build-module/store/reducer.mjs.map +7 -0
- package/build-module/store/selectors.mjs +16 -0
- package/build-module/store/selectors.mjs.map +7 -0
- package/build-style/style-rtl.css +315 -0
- package/build-style/style.css +315 -0
- package/build-types/components/media-editor-canvas/index.d.ts +22 -0
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +20 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
- package/build-types/image-editor/core/camera.d.ts +107 -0
- package/build-types/image-editor/core/camera.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +46 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -0
- package/build-types/image-editor/core/containment.d.ts +61 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +8 -0
- package/build-types/image-editor/core/index.d.ts.map +1 -0
- package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
- package/build-types/image-editor/core/math/rotation.d.ts +18 -0
- package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts +75 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -0
- package/build-types/image-editor/core/state.d.ts +42 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -0
- package/build-types/image-editor/core/stencil-math.d.ts +79 -0
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
- package/build-types/image-editor/core/transform-style.d.ts +23 -0
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
- package/build-types/image-editor/core/types.d.ts +211 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -0
- package/build-types/image-editor/index.d.ts +16 -0
- package/build-types/image-editor/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper.d.ts +59 -0
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
- package/build-types/image-editor/react/components/index.d.ts +4 -0
- package/build-types/image-editor/react/components/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +3 -0
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/private-apis.d.ts +2 -0
- package/build-types/private-apis.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +27 -0
- package/build-types/store/actions.d.ts.map +1 -0
- package/build-types/store/constants.d.ts +2 -0
- package/build-types/store/constants.d.ts.map +1 -0
- package/build-types/store/index.d.ts +4 -0
- package/build-types/store/index.d.ts.map +1 -0
- package/build-types/store/reducer.d.ts +23 -0
- package/build-types/store/reducer.d.ts.map +1 -0
- package/build-types/store/selectors.d.ts +8 -0
- package/build-types/store/selectors.d.ts.map +1 -0
- package/package.json +21 -7
- package/src/components/media-editor-canvas/index.tsx +56 -0
- package/src/components/media-editor-canvas/style.scss +9 -0
- package/src/components/media-editor-crop-panel/index.tsx +143 -0
- package/src/components/media-editor-modal/build-modifiers.ts +164 -0
- package/src/components/media-editor-modal/index.tsx +639 -0
- package/src/components/media-editor-modal/style.scss +139 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
- package/src/components/media-editor-toolbar/index.tsx +150 -0
- package/src/components/media-editor-toolbar/style.scss +21 -0
- package/src/image-editor/README.md +162 -0
- package/src/image-editor/core/camera.ts +352 -0
- package/src/image-editor/core/constants.ts +93 -0
- package/src/image-editor/core/containment.ts +435 -0
- package/src/image-editor/core/export/canvas-renderer.ts +253 -0
- package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
- package/src/image-editor/core/index.ts +35 -0
- package/src/image-editor/core/interaction-controller.ts +889 -0
- package/src/image-editor/core/math/rotation.ts +26 -0
- package/src/image-editor/core/source-region.ts +166 -0
- package/src/image-editor/core/state.ts +428 -0
- package/src/image-editor/core/stencil-math.ts +330 -0
- package/src/image-editor/core/test/camera.ts +593 -0
- package/src/image-editor/core/test/interaction-controller.ts +996 -0
- package/src/image-editor/core/test/preview-export-parity.ts +242 -0
- package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
- package/src/image-editor/core/test/state.ts +762 -0
- package/src/image-editor/core/test/stencil-math.ts +321 -0
- package/src/image-editor/core/transform-style.ts +44 -0
- package/src/image-editor/core/transforms/pipeline.ts +43 -0
- package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
- package/src/image-editor/core/types.ts +177 -0
- package/src/image-editor/docs/architecture.md +96 -0
- package/src/image-editor/docs/recipes.md +720 -0
- package/src/image-editor/docs/roadmap.md +66 -0
- package/src/image-editor/index.ts +43 -0
- package/src/image-editor/react/components/cropper-provider.tsx +73 -0
- package/src/image-editor/react/components/cropper.scss +195 -0
- package/src/image-editor/react/components/cropper.tsx +534 -0
- package/src/image-editor/react/components/index.ts +3 -0
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
- package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
- package/src/image-editor/react/components/test/cropper.tsx +140 -0
- package/src/image-editor/react/hooks/index.ts +2 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
- package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
- package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
- package/src/image-editor/react/hooks/use-interaction.ts +223 -0
- package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
- package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
- package/src/image-editor/stories/style.css +94 -0
- package/src/image-editor/style.scss +1 -0
- package/src/index.ts +3 -0
- package/src/lock-unlock.ts +10 -0
- package/src/private-apis.ts +12 -0
- package/src/store/actions.ts +33 -0
- package/src/store/constants.ts +1 -0
- package/src/store/index.ts +20 -0
- package/src/store/reducer.ts +48 -0
- package/src/store/selectors.ts +16 -0
- package/src/store/test/reducer.ts +57 -0
- package/src/style.scss +4 -0
- package/tsconfig.json +10 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { NormalizedRect, Size } from './types';
|
|
5
|
+
|
|
6
|
+
/** Minimum crop rect dimension in normalized space (5% of visual area). */
|
|
7
|
+
export const MIN_CROP_SIZE = 0.05;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Handle position identifiers for the 8 resize handles.
|
|
11
|
+
*/
|
|
12
|
+
export type HandlePosition = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Bounds within which the crop rect must stay.
|
|
16
|
+
*/
|
|
17
|
+
export interface CropBounds {
|
|
18
|
+
minX: number;
|
|
19
|
+
minY: number;
|
|
20
|
+
maxX: number;
|
|
21
|
+
maxY: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Drag state for tracking a resize interaction.
|
|
26
|
+
*/
|
|
27
|
+
export interface ResizeDragState {
|
|
28
|
+
/** Which handle is being dragged. */
|
|
29
|
+
handle: HandlePosition;
|
|
30
|
+
/** The mouse position (pixels) when the drag started. */
|
|
31
|
+
startX: number;
|
|
32
|
+
startY: number;
|
|
33
|
+
/** The crop rect (normalized) when the drag started. */
|
|
34
|
+
startRect: NormalizedRect;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Compute the new crop rect for a free (no aspect ratio) resize.
|
|
39
|
+
*
|
|
40
|
+
* Each edge moves independently based on the handle being dragged.
|
|
41
|
+
* Edges are clamped to the provided bounds and maintain a minimum size.
|
|
42
|
+
*
|
|
43
|
+
* @param drag The current drag state.
|
|
44
|
+
* @param clientX Current mouse/touch X position in pixels.
|
|
45
|
+
* @param clientY Current mouse/touch Y position in pixels.
|
|
46
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
47
|
+
* @param bounds The allowed crop area bounds.
|
|
48
|
+
* @return The new crop rect in normalized coordinates.
|
|
49
|
+
*/
|
|
50
|
+
export function computeFreeResizeRect(
|
|
51
|
+
drag: ResizeDragState,
|
|
52
|
+
clientX: number,
|
|
53
|
+
clientY: number,
|
|
54
|
+
imageSize: Size,
|
|
55
|
+
bounds: CropBounds
|
|
56
|
+
): NormalizedRect {
|
|
57
|
+
const dx =
|
|
58
|
+
imageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;
|
|
59
|
+
const dy =
|
|
60
|
+
imageSize.height > 0 ? ( clientY - drag.startY ) / imageSize.height : 0;
|
|
61
|
+
|
|
62
|
+
const s = drag.startRect;
|
|
63
|
+
const handle = drag.handle;
|
|
64
|
+
|
|
65
|
+
let edgeTop = s.y;
|
|
66
|
+
let edgeBottom = s.y + s.height;
|
|
67
|
+
let edgeLeft = s.x;
|
|
68
|
+
let edgeRight = s.x + s.width;
|
|
69
|
+
|
|
70
|
+
if ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {
|
|
71
|
+
edgeTop = Math.max(
|
|
72
|
+
bounds.minY,
|
|
73
|
+
Math.min( s.y + dy, edgeBottom - MIN_CROP_SIZE )
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
if ( handle === 's' || handle === 'sw' || handle === 'se' ) {
|
|
77
|
+
edgeBottom = Math.max(
|
|
78
|
+
edgeTop + MIN_CROP_SIZE,
|
|
79
|
+
Math.min( s.y + s.height + dy, bounds.maxY )
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {
|
|
83
|
+
edgeLeft = Math.max(
|
|
84
|
+
bounds.minX,
|
|
85
|
+
Math.min( s.x + dx, edgeRight - MIN_CROP_SIZE )
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if ( handle === 'e' || handle === 'ne' || handle === 'se' ) {
|
|
89
|
+
edgeRight = Math.max(
|
|
90
|
+
edgeLeft + MIN_CROP_SIZE,
|
|
91
|
+
Math.min( s.x + s.width + dx, bounds.maxX )
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
x: edgeLeft,
|
|
97
|
+
y: edgeTop,
|
|
98
|
+
width: edgeRight - edgeLeft,
|
|
99
|
+
height: edgeBottom - edgeTop,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Compute the new crop rect for a locked-aspect-ratio corner resize.
|
|
105
|
+
*
|
|
106
|
+
* The opposite corner is the anchor. The dragged corner moves freely
|
|
107
|
+
* but the result is clamped to maintain the aspect ratio and stay
|
|
108
|
+
* within bounds.
|
|
109
|
+
*
|
|
110
|
+
* @param drag The current drag state.
|
|
111
|
+
* @param clientX Current mouse/touch X position in pixels.
|
|
112
|
+
* @param clientY Current mouse/touch Y position in pixels.
|
|
113
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
114
|
+
* @param bounds The allowed crop area bounds.
|
|
115
|
+
* @param normalizedRatio The locked aspect ratio (width / height in normalized space).
|
|
116
|
+
* @return The new crop rect in normalized coordinates.
|
|
117
|
+
*/
|
|
118
|
+
export function computeLockedResizeRect(
|
|
119
|
+
drag: ResizeDragState,
|
|
120
|
+
clientX: number,
|
|
121
|
+
clientY: number,
|
|
122
|
+
imageSize: Size,
|
|
123
|
+
bounds: CropBounds,
|
|
124
|
+
normalizedRatio: number
|
|
125
|
+
): NormalizedRect {
|
|
126
|
+
// The math below divides by `normalizedRatio` and `imageSize`, so
|
|
127
|
+
// bail out with the start rect when any of them is zero. This can
|
|
128
|
+
// happen if a resize is triggered (e.g. via keyboard arrows on a
|
|
129
|
+
// focused handle) before the image has loaded.
|
|
130
|
+
if (
|
|
131
|
+
normalizedRatio <= 0 ||
|
|
132
|
+
imageSize.width <= 0 ||
|
|
133
|
+
imageSize.height <= 0
|
|
134
|
+
) {
|
|
135
|
+
return { ...drag.startRect };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const dx = ( clientX - drag.startX ) / imageSize.width;
|
|
139
|
+
const dy = ( clientY - drag.startY ) / imageSize.height;
|
|
140
|
+
|
|
141
|
+
const s = drag.startRect;
|
|
142
|
+
const handle = drag.handle;
|
|
143
|
+
|
|
144
|
+
// Determine the anchor corner (opposite to the dragged corner).
|
|
145
|
+
const anchorX = handle === 'nw' || handle === 'sw' ? s.x + s.width : s.x;
|
|
146
|
+
const anchorY = handle === 'nw' || handle === 'ne' ? s.y + s.height : s.y;
|
|
147
|
+
|
|
148
|
+
// Direction the crop grows from the anchor (+1 = right/down, -1 = left/up).
|
|
149
|
+
const dirX = handle === 'nw' || handle === 'sw' ? -1 : 1;
|
|
150
|
+
const dirY = handle === 'nw' || handle === 'ne' ? -1 : 1;
|
|
151
|
+
|
|
152
|
+
// Desired new position of the dragged corner.
|
|
153
|
+
const draggedX =
|
|
154
|
+
( handle === 'nw' || handle === 'sw' ? s.x : s.x + s.width ) + dx;
|
|
155
|
+
const draggedY =
|
|
156
|
+
( handle === 'nw' || handle === 'ne' ? s.y : s.y + s.height ) + dy;
|
|
157
|
+
|
|
158
|
+
// Raw distances from anchor to dragged corner.
|
|
159
|
+
let distW = ( draggedX - anchorX ) * dirX;
|
|
160
|
+
let distH = ( draggedY - anchorY ) * dirY;
|
|
161
|
+
|
|
162
|
+
// Enforce minimum size.
|
|
163
|
+
distW = Math.max( distW, MIN_CROP_SIZE );
|
|
164
|
+
distH = Math.max( distH, MIN_CROP_SIZE );
|
|
165
|
+
|
|
166
|
+
// Determine which axis "drives" — whichever the user moved more
|
|
167
|
+
// (in pixel space) determines the size, the other follows. The
|
|
168
|
+
// `normalizedRatio` is w/h in normalized space; the equivalent
|
|
169
|
+
// pixel-space ratio is `normalizedRatio * imageW / imageH`. We
|
|
170
|
+
// compare the pixel motion ratio against that pixel-space ratio
|
|
171
|
+
// so the units line up (was a unit mismatch on non-square images).
|
|
172
|
+
const pixelDistW = distW * imageSize.width;
|
|
173
|
+
const pixelDistH = distH * imageSize.height;
|
|
174
|
+
const pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;
|
|
175
|
+
if ( pixelDistW / pixelDistH > pixelRatio ) {
|
|
176
|
+
// Width is the driver — compute height from ratio.
|
|
177
|
+
distH = distW / normalizedRatio;
|
|
178
|
+
} else {
|
|
179
|
+
// Height is the driver — compute width from ratio.
|
|
180
|
+
distW = distH * normalizedRatio;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Clamp to image coverage bounds. If the rect would exceed,
|
|
184
|
+
// shrink it (maintaining ratio) to fit.
|
|
185
|
+
const maxW = dirX > 0 ? bounds.maxX - anchorX : anchorX - bounds.minX;
|
|
186
|
+
const maxH = dirY > 0 ? bounds.maxY - anchorY : anchorY - bounds.minY;
|
|
187
|
+
|
|
188
|
+
if ( distW > maxW ) {
|
|
189
|
+
distW = maxW;
|
|
190
|
+
distH = distW / normalizedRatio;
|
|
191
|
+
}
|
|
192
|
+
if ( distH > maxH ) {
|
|
193
|
+
distH = maxH;
|
|
194
|
+
distW = distH * normalizedRatio;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Enforce minimum after clamping.
|
|
198
|
+
distW = Math.max( distW, MIN_CROP_SIZE );
|
|
199
|
+
distH = Math.max( distH, MIN_CROP_SIZE );
|
|
200
|
+
|
|
201
|
+
// Compute the final rect position from the anchor.
|
|
202
|
+
const newX = dirX > 0 ? anchorX : anchorX - distW;
|
|
203
|
+
const newY = dirY > 0 ? anchorY : anchorY - distH;
|
|
204
|
+
|
|
205
|
+
return { x: newX, y: newY, width: distW, height: distH };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Compute the new crop rect for a resize that temporarily locks the
|
|
210
|
+
* aspect ratio to `drag.startRect`'s ratio (e.g. while Shift is held).
|
|
211
|
+
*
|
|
212
|
+
* Corner handles delegate to {@link computeLockedResizeRect}. Edge
|
|
213
|
+
* handles size the dragged axis freely, then expand the perpendicular
|
|
214
|
+
* axis symmetrically around the rect's center to preserve the ratio,
|
|
215
|
+
* clamping symmetrically so the rect stays within bounds.
|
|
216
|
+
*
|
|
217
|
+
* @param drag The current drag state.
|
|
218
|
+
* @param clientX Current mouse/touch X position in pixels.
|
|
219
|
+
* @param clientY Current mouse/touch Y position in pixels.
|
|
220
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
221
|
+
* @param bounds The allowed crop area bounds.
|
|
222
|
+
* @return The new crop rect in normalized coordinates.
|
|
223
|
+
*/
|
|
224
|
+
export function computeShiftLockedResizeRect(
|
|
225
|
+
drag: ResizeDragState,
|
|
226
|
+
clientX: number,
|
|
227
|
+
clientY: number,
|
|
228
|
+
imageSize: Size,
|
|
229
|
+
bounds: CropBounds
|
|
230
|
+
): NormalizedRect {
|
|
231
|
+
const s = drag.startRect;
|
|
232
|
+
const pixelW = s.width * imageSize.width;
|
|
233
|
+
const pixelH = s.height * imageSize.height;
|
|
234
|
+
if ( pixelH <= 0 || pixelW <= 0 ) {
|
|
235
|
+
return computeFreeResizeRect(
|
|
236
|
+
drag,
|
|
237
|
+
clientX,
|
|
238
|
+
clientY,
|
|
239
|
+
imageSize,
|
|
240
|
+
bounds
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
const normalizedRatio = s.width / s.height;
|
|
244
|
+
|
|
245
|
+
const handle = drag.handle;
|
|
246
|
+
if (
|
|
247
|
+
handle === 'nw' ||
|
|
248
|
+
handle === 'ne' ||
|
|
249
|
+
handle === 'sw' ||
|
|
250
|
+
handle === 'se'
|
|
251
|
+
) {
|
|
252
|
+
return computeLockedResizeRect(
|
|
253
|
+
drag,
|
|
254
|
+
clientX,
|
|
255
|
+
clientY,
|
|
256
|
+
imageSize,
|
|
257
|
+
bounds,
|
|
258
|
+
normalizedRatio
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Edge handle: size the dragged axis with free-resize logic, then
|
|
263
|
+
// expand the perpendicular axis symmetrically around the rect's center.
|
|
264
|
+
const free = computeFreeResizeRect(
|
|
265
|
+
drag,
|
|
266
|
+
clientX,
|
|
267
|
+
clientY,
|
|
268
|
+
imageSize,
|
|
269
|
+
bounds
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
if ( handle === 'n' || handle === 's' ) {
|
|
273
|
+
// Height is the driver; derive width from ratio.
|
|
274
|
+
let newHeight = free.height;
|
|
275
|
+
let newWidth = newHeight * normalizedRatio;
|
|
276
|
+
const centerX = s.x + s.width / 2;
|
|
277
|
+
// Symmetric clamp: limit by the smaller of the two side gaps so
|
|
278
|
+
// the rect stays centered around centerX.
|
|
279
|
+
const maxWidth =
|
|
280
|
+
Math.min( centerX - bounds.minX, bounds.maxX - centerX ) * 2;
|
|
281
|
+
if ( newWidth > maxWidth ) {
|
|
282
|
+
newWidth = maxWidth;
|
|
283
|
+
newHeight = newWidth / normalizedRatio;
|
|
284
|
+
}
|
|
285
|
+
// Enforce the minimum on the driving axis, then derive the
|
|
286
|
+
// other axis from the ratio so it stays consistent. The
|
|
287
|
+
// effective minimum height has to satisfy MIN_CROP_SIZE on
|
|
288
|
+
// both axes simultaneously.
|
|
289
|
+
const minHeight = Math.max(
|
|
290
|
+
MIN_CROP_SIZE,
|
|
291
|
+
MIN_CROP_SIZE / normalizedRatio
|
|
292
|
+
);
|
|
293
|
+
if ( newHeight < minHeight ) {
|
|
294
|
+
newHeight = minHeight;
|
|
295
|
+
newWidth = newHeight * normalizedRatio;
|
|
296
|
+
}
|
|
297
|
+
// Re-anchor the dragged axis to the opposite edge so the
|
|
298
|
+
// height adjustment after clamping keeps that edge fixed.
|
|
299
|
+
const newY = handle === 'n' ? s.y + s.height - newHeight : s.y;
|
|
300
|
+
return {
|
|
301
|
+
x: centerX - newWidth / 2,
|
|
302
|
+
y: newY,
|
|
303
|
+
width: newWidth,
|
|
304
|
+
height: newHeight,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// 'e' or 'w': width is the driver; derive height from ratio.
|
|
309
|
+
let newWidth = free.width;
|
|
310
|
+
let newHeight = newWidth / normalizedRatio;
|
|
311
|
+
const centerY = s.y + s.height / 2;
|
|
312
|
+
const maxHeight =
|
|
313
|
+
Math.min( centerY - bounds.minY, bounds.maxY - centerY ) * 2;
|
|
314
|
+
if ( newHeight > maxHeight ) {
|
|
315
|
+
newHeight = maxHeight;
|
|
316
|
+
newWidth = newHeight * normalizedRatio;
|
|
317
|
+
}
|
|
318
|
+
const minWidth = Math.max( MIN_CROP_SIZE, MIN_CROP_SIZE * normalizedRatio );
|
|
319
|
+
if ( newWidth < minWidth ) {
|
|
320
|
+
newWidth = minWidth;
|
|
321
|
+
newHeight = newWidth / normalizedRatio;
|
|
322
|
+
}
|
|
323
|
+
const newX = handle === 'w' ? s.x + s.width - newWidth : s.x;
|
|
324
|
+
return {
|
|
325
|
+
x: newX,
|
|
326
|
+
y: centerY - newHeight / 2,
|
|
327
|
+
width: newWidth,
|
|
328
|
+
height: newHeight,
|
|
329
|
+
};
|
|
330
|
+
}
|