@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,593 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCamera,
|
|
3
|
+
worldToScreen,
|
|
4
|
+
screenToWorld,
|
|
5
|
+
getVisibleBounds,
|
|
6
|
+
createExportCamera,
|
|
7
|
+
getImageFit,
|
|
8
|
+
} from '../camera';
|
|
9
|
+
import {
|
|
10
|
+
restrictPanZoom,
|
|
11
|
+
restrictCropRect,
|
|
12
|
+
getCropBounds,
|
|
13
|
+
} from '../containment';
|
|
14
|
+
import { getSourceRegion, getSourceRegionPercent } from '../source-region';
|
|
15
|
+
import { DEFAULT_STATE } from '../constants';
|
|
16
|
+
import type { CropperState, Size } from '../types';
|
|
17
|
+
|
|
18
|
+
const CONTAINER: Size = { width: 800, height: 600 };
|
|
19
|
+
const IMAGE: Size = { width: 1600, height: 900 };
|
|
20
|
+
|
|
21
|
+
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
22
|
+
return {
|
|
23
|
+
...DEFAULT_STATE,
|
|
24
|
+
image: {
|
|
25
|
+
src: 'test.jpg',
|
|
26
|
+
naturalWidth: IMAGE.width,
|
|
27
|
+
naturalHeight: IMAGE.height,
|
|
28
|
+
},
|
|
29
|
+
...overrides,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe( 'createCamera', () => {
|
|
34
|
+
it( 'returns a mat2d', () => {
|
|
35
|
+
const camera = createCamera( makeState(), CONTAINER, IMAGE );
|
|
36
|
+
expect( camera ).toHaveLength( 6 );
|
|
37
|
+
} );
|
|
38
|
+
|
|
39
|
+
it( 'maps image center (0.5, 0.5) to container center at identity state', () => {
|
|
40
|
+
const state = makeState();
|
|
41
|
+
const camera = createCamera( state, CONTAINER, IMAGE );
|
|
42
|
+
const screenPt = worldToScreen( camera, { x: 0.5, y: 0.5 } );
|
|
43
|
+
expect( screenPt.x ).toBeCloseTo( CONTAINER.width / 2, 0 );
|
|
44
|
+
expect( screenPt.y ).toBeCloseTo( CONTAINER.height / 2, 0 );
|
|
45
|
+
} );
|
|
46
|
+
|
|
47
|
+
it( 'worldToScreen and screenToWorld are inverses', () => {
|
|
48
|
+
const state = makeState( {
|
|
49
|
+
zoom: 1.5,
|
|
50
|
+
rotation: 30,
|
|
51
|
+
pan: { x: 0.1, y: -0.05 },
|
|
52
|
+
} );
|
|
53
|
+
const camera = createCamera( state, CONTAINER, IMAGE );
|
|
54
|
+
const worldPt = { x: 0.3, y: 0.7 };
|
|
55
|
+
const screenPt = worldToScreen( camera, worldPt );
|
|
56
|
+
const roundTrip = screenToWorld( camera, screenPt );
|
|
57
|
+
expect( roundTrip.x ).toBeCloseTo( worldPt.x, 5 );
|
|
58
|
+
expect( roundTrip.y ).toBeCloseTo( worldPt.y, 5 );
|
|
59
|
+
} );
|
|
60
|
+
|
|
61
|
+
it( 'zoom=2 makes the image appear twice as large', () => {
|
|
62
|
+
const state1 = makeState();
|
|
63
|
+
const state2 = makeState( { zoom: 2 } );
|
|
64
|
+
const cam1 = createCamera( state1, CONTAINER, IMAGE );
|
|
65
|
+
const cam2 = createCamera( state2, CONTAINER, IMAGE );
|
|
66
|
+
const p1 = worldToScreen( cam1, { x: 0.75, y: 0.5 } );
|
|
67
|
+
const p2 = worldToScreen( cam2, { x: 0.75, y: 0.5 } );
|
|
68
|
+
const center = CONTAINER.width / 2;
|
|
69
|
+
expect( p2.x - center ).toBeCloseTo( 2 * ( p1.x - center ), 0 );
|
|
70
|
+
} );
|
|
71
|
+
|
|
72
|
+
it( 'horizontal flip mirrors x around container center', () => {
|
|
73
|
+
const normal = makeState();
|
|
74
|
+
const flipped = makeState( {
|
|
75
|
+
flip: { horizontal: true, vertical: false },
|
|
76
|
+
} );
|
|
77
|
+
const camN = createCamera( normal, CONTAINER, IMAGE );
|
|
78
|
+
const camF = createCamera( flipped, CONTAINER, IMAGE );
|
|
79
|
+
const ptN = worldToScreen( camN, { x: 0.25, y: 0.5 } );
|
|
80
|
+
const ptF = worldToScreen( camF, { x: 0.25, y: 0.5 } );
|
|
81
|
+
const center = CONTAINER.width / 2;
|
|
82
|
+
expect( ptF.x ).toBeCloseTo( 2 * center - ptN.x, 0 );
|
|
83
|
+
} );
|
|
84
|
+
|
|
85
|
+
it( 'rotation=90 rotates points 90 degrees around center', () => {
|
|
86
|
+
const state = makeState( { rotation: 90 } );
|
|
87
|
+
const camera = createCamera( state, CONTAINER, IMAGE );
|
|
88
|
+
const center = worldToScreen( camera, { x: 0.5, y: 0.5 } );
|
|
89
|
+
expect( center.x ).toBeCloseTo( CONTAINER.width / 2, 0 );
|
|
90
|
+
expect( center.y ).toBeCloseTo( CONTAINER.height / 2, 0 );
|
|
91
|
+
} );
|
|
92
|
+
|
|
93
|
+
it( 'pan shifts the image in screen space', () => {
|
|
94
|
+
const noPan = makeState();
|
|
95
|
+
const withPan = makeState( { pan: { x: 0.1, y: 0 } } );
|
|
96
|
+
const cam1 = createCamera( noPan, CONTAINER, IMAGE );
|
|
97
|
+
const cam2 = createCamera( withPan, CONTAINER, IMAGE );
|
|
98
|
+
const p1 = worldToScreen( cam1, { x: 0.5, y: 0.5 } );
|
|
99
|
+
const p2 = worldToScreen( cam2, { x: 0.5, y: 0.5 } );
|
|
100
|
+
expect( p2.x ).toBeGreaterThan( p1.x );
|
|
101
|
+
} );
|
|
102
|
+
} );
|
|
103
|
+
|
|
104
|
+
describe( 'restrictPanZoom', () => {
|
|
105
|
+
it( 'returns identity pan at default state', () => {
|
|
106
|
+
const state = makeState();
|
|
107
|
+
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
108
|
+
expect( result.pan.x ).toBeCloseTo( 0 );
|
|
109
|
+
expect( result.pan.y ).toBeCloseTo( 0 );
|
|
110
|
+
expect( result.zoom ).toBeCloseTo( 1 );
|
|
111
|
+
} );
|
|
112
|
+
it( 'clamps pan so image covers crop rect', () => {
|
|
113
|
+
const state = makeState( { pan: { x: 5, y: 5 }, zoom: 1 } );
|
|
114
|
+
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
115
|
+
expect( Math.abs( result.pan.x ) ).toBeLessThan( 1 );
|
|
116
|
+
expect( Math.abs( result.pan.y ) ).toBeLessThan( 1 );
|
|
117
|
+
} );
|
|
118
|
+
it( 'increases zoom if too low for rotation', () => {
|
|
119
|
+
const state = makeState( { rotation: 45, zoom: 1 } );
|
|
120
|
+
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
121
|
+
expect( result.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
122
|
+
} );
|
|
123
|
+
it( 'at 90° with zoom=1, allows zero pan on landscape image', () => {
|
|
124
|
+
// At zoom=1, 90° rotation, the image exactly covers the visual area.
|
|
125
|
+
// No pan should be possible in either direction.
|
|
126
|
+
const state = makeState( {
|
|
127
|
+
rotation: 90,
|
|
128
|
+
zoom: 1,
|
|
129
|
+
pan: { x: 0.3, y: 0.3 },
|
|
130
|
+
} );
|
|
131
|
+
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
132
|
+
expect( result.pan.x ).toBeCloseTo( 0, 5 );
|
|
133
|
+
expect( result.pan.y ).toBeCloseTo( 0, 5 );
|
|
134
|
+
} );
|
|
135
|
+
it( 'at 90° with zoom=2, allows symmetric pan range', () => {
|
|
136
|
+
// When zoomed in at 90° rotation, the pan range should be symmetric
|
|
137
|
+
// between x and y (both should allow ±0.5 of visual dimension).
|
|
138
|
+
const state90pos = makeState( {
|
|
139
|
+
rotation: 90,
|
|
140
|
+
zoom: 2,
|
|
141
|
+
pan: { x: 0.5, y: 0 },
|
|
142
|
+
} );
|
|
143
|
+
const state90neg = makeState( {
|
|
144
|
+
rotation: 90,
|
|
145
|
+
zoom: 2,
|
|
146
|
+
pan: { x: -0.5, y: 0 },
|
|
147
|
+
} );
|
|
148
|
+
const resultPos = restrictPanZoom(
|
|
149
|
+
state90pos,
|
|
150
|
+
IMAGE,
|
|
151
|
+
state90pos.cropRect
|
|
152
|
+
);
|
|
153
|
+
const resultNeg = restrictPanZoom(
|
|
154
|
+
state90neg,
|
|
155
|
+
IMAGE,
|
|
156
|
+
state90neg.cropRect
|
|
157
|
+
);
|
|
158
|
+
// Should allow meaningful horizontal pan (not clamped to 0).
|
|
159
|
+
expect( resultPos.pan.x ).toBeGreaterThan( 0.1 );
|
|
160
|
+
expect( resultNeg.pan.x ).toBeLessThan( -0.1 );
|
|
161
|
+
// And the range should be symmetric.
|
|
162
|
+
expect( resultPos.pan.x ).toBeCloseTo( -resultNeg.pan.x, 5 );
|
|
163
|
+
} );
|
|
164
|
+
it( 'at 0° with zoom=1, allows zero pan on landscape image', () => {
|
|
165
|
+
const state = makeState( {
|
|
166
|
+
rotation: 0,
|
|
167
|
+
zoom: 1,
|
|
168
|
+
pan: { x: 0.3, y: 0.3 },
|
|
169
|
+
} );
|
|
170
|
+
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
171
|
+
expect( result.pan.x ).toBeCloseTo( 0, 5 );
|
|
172
|
+
expect( result.pan.y ).toBeCloseTo( 0, 5 );
|
|
173
|
+
} );
|
|
174
|
+
} );
|
|
175
|
+
|
|
176
|
+
describe( 'restrictCropRect', () => {
|
|
177
|
+
it( 'returns same rect when it fits', () => {
|
|
178
|
+
const rect = { x: 0, y: 0, width: 1, height: 1 };
|
|
179
|
+
const result = restrictCropRect( rect, 1, 0, 16 / 9 );
|
|
180
|
+
expect( result ).toBe( rect );
|
|
181
|
+
} );
|
|
182
|
+
it( 'shrinks rect when too large for zoom/rotation', () => {
|
|
183
|
+
const rect = { x: 0, y: 0, width: 1, height: 1 };
|
|
184
|
+
const result = restrictCropRect( rect, 1, 45, 1 );
|
|
185
|
+
expect( result.width ).toBeLessThan( 1 );
|
|
186
|
+
expect( result.height ).toBeLessThan( 1 );
|
|
187
|
+
} );
|
|
188
|
+
} );
|
|
189
|
+
|
|
190
|
+
describe( 'createExportCamera', () => {
|
|
191
|
+
it( 'image center maps to output center at identity state with full crop', () => {
|
|
192
|
+
const state = makeState();
|
|
193
|
+
const outputSize = { width: 400, height: 225 };
|
|
194
|
+
const camera = createExportCamera( state, IMAGE, outputSize );
|
|
195
|
+
const { vec2 } = require( 'gl-matrix' );
|
|
196
|
+
const out = vec2.create();
|
|
197
|
+
vec2.transformMat2d(
|
|
198
|
+
out,
|
|
199
|
+
[ IMAGE.width / 2, IMAGE.height / 2 ],
|
|
200
|
+
camera
|
|
201
|
+
);
|
|
202
|
+
expect( out[ 0 ] ).toBeCloseTo( outputSize.width / 2, 0 );
|
|
203
|
+
expect( out[ 1 ] ).toBeCloseTo( outputSize.height / 2, 0 );
|
|
204
|
+
} );
|
|
205
|
+
|
|
206
|
+
it( 'matches current renderToCanvas: identity maps full image to output', () => {
|
|
207
|
+
const state = makeState();
|
|
208
|
+
const outputSize = { width: IMAGE.width, height: IMAGE.height };
|
|
209
|
+
const camera = createExportCamera( state, IMAGE, outputSize );
|
|
210
|
+
const { vec2 } = require( 'gl-matrix' );
|
|
211
|
+
const topLeft = vec2.create();
|
|
212
|
+
vec2.transformMat2d( topLeft, [ 0, 0 ], camera );
|
|
213
|
+
expect( topLeft[ 0 ] ).toBeCloseTo( 0, 0 );
|
|
214
|
+
expect( topLeft[ 1 ] ).toBeCloseTo( 0, 0 );
|
|
215
|
+
const bottomRight = vec2.create();
|
|
216
|
+
vec2.transformMat2d(
|
|
217
|
+
bottomRight,
|
|
218
|
+
[ IMAGE.width, IMAGE.height ],
|
|
219
|
+
camera
|
|
220
|
+
);
|
|
221
|
+
expect( bottomRight[ 0 ] ).toBeCloseTo( outputSize.width, 0 );
|
|
222
|
+
expect( bottomRight[ 1 ] ).toBeCloseTo( outputSize.height, 0 );
|
|
223
|
+
} );
|
|
224
|
+
|
|
225
|
+
it( 'at fine rotation, exported pixels track the stencil frame (preview / export agree)', () => {
|
|
226
|
+
// Stencil frames some image content via the preview camera; the
|
|
227
|
+
// export camera must pick the SAME image pixels. Before this
|
|
228
|
+
// fix, the export used the true-rotation bbox while the preview
|
|
229
|
+
// used the snap-rotation bbox, so any fine rotation shifted the
|
|
230
|
+
// exported region sideways relative to what the stencil framed.
|
|
231
|
+
const state = makeState( {
|
|
232
|
+
rotation: 15,
|
|
233
|
+
zoom: 1.5,
|
|
234
|
+
cropRect: { x: 0.2, y: 0.15, width: 0.5, height: 0.6 },
|
|
235
|
+
} );
|
|
236
|
+
const container: Size = { width: 800, height: 600 };
|
|
237
|
+
|
|
238
|
+
// Preview path: find the source-image pixel that lands at the
|
|
239
|
+
// stencil center on screen.
|
|
240
|
+
const previewCamera = createCamera( state, container, IMAGE );
|
|
241
|
+
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
242
|
+
const baseCamera = createCamera(
|
|
243
|
+
{
|
|
244
|
+
...state,
|
|
245
|
+
pan: { x: 0, y: 0 },
|
|
246
|
+
zoom: 1,
|
|
247
|
+
rotation: snapRotation,
|
|
248
|
+
},
|
|
249
|
+
container,
|
|
250
|
+
IMAGE
|
|
251
|
+
);
|
|
252
|
+
const vb = getVisibleBounds( baseCamera );
|
|
253
|
+
const stencilCenterScreen = {
|
|
254
|
+
x:
|
|
255
|
+
vb.left +
|
|
256
|
+
( state.cropRect.x + state.cropRect.width / 2 ) * vb.width,
|
|
257
|
+
y:
|
|
258
|
+
vb.top +
|
|
259
|
+
( state.cropRect.y + state.cropRect.height / 2 ) * vb.height,
|
|
260
|
+
};
|
|
261
|
+
const previewWorld = screenToWorld(
|
|
262
|
+
previewCamera,
|
|
263
|
+
stencilCenterScreen
|
|
264
|
+
);
|
|
265
|
+
const previewPx = {
|
|
266
|
+
x: previewWorld.x * IMAGE.width,
|
|
267
|
+
y: previewWorld.y * IMAGE.height,
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// Export path: the source pixel that maps to the center of the
|
|
271
|
+
// output canvas.
|
|
272
|
+
const outputSize = { width: 400, height: 300 };
|
|
273
|
+
const exportCamera = createExportCamera( state, IMAGE, outputSize );
|
|
274
|
+
const { mat2d: m2d, vec2 } = require( 'gl-matrix' );
|
|
275
|
+
const inv = m2d.create();
|
|
276
|
+
m2d.invert( inv, exportCamera );
|
|
277
|
+
const exportPxVec = vec2.create();
|
|
278
|
+
vec2.transformMat2d(
|
|
279
|
+
exportPxVec,
|
|
280
|
+
[ outputSize.width / 2, outputSize.height / 2 ],
|
|
281
|
+
inv
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
// Preview and export must agree on the source pixel at crop center.
|
|
285
|
+
expect( exportPxVec[ 0 ] ).toBeCloseTo( previewPx.x, 0 );
|
|
286
|
+
expect( exportPxVec[ 1 ] ).toBeCloseTo( previewPx.y, 0 );
|
|
287
|
+
} );
|
|
288
|
+
} );
|
|
289
|
+
|
|
290
|
+
describe( 'containment invariant (property-based)', () => {
|
|
291
|
+
/**
|
|
292
|
+
* Verify that the image fully covers the crop rect by projecting all
|
|
293
|
+
* four crop corners through the inverse camera and checking that the
|
|
294
|
+
* resulting world-space points lie within [0,1] x [0,1] (the image).
|
|
295
|
+
*
|
|
296
|
+
* @param state The cropper state to verify.
|
|
297
|
+
* @param imageSize The natural image dimensions.
|
|
298
|
+
*/
|
|
299
|
+
function verifyImageCoversCrop(
|
|
300
|
+
state: CropperState,
|
|
301
|
+
imageSize: Size
|
|
302
|
+
): void {
|
|
303
|
+
const container: Size = { width: 1000, height: 1000 };
|
|
304
|
+
const camera = createCamera( state, container, imageSize );
|
|
305
|
+
|
|
306
|
+
// Build base camera (zero pan, zoom=1) for stencil positioning.
|
|
307
|
+
// The stencil is laid out at the nearest 90° rotation (matching
|
|
308
|
+
// `getImageFit` in production), so build the base camera with
|
|
309
|
+
// that snap rotation for consistent stencil corners.
|
|
310
|
+
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
311
|
+
const baseCamera = createCamera(
|
|
312
|
+
{
|
|
313
|
+
...state,
|
|
314
|
+
pan: { x: 0, y: 0 },
|
|
315
|
+
zoom: 1,
|
|
316
|
+
rotation: snapRotation,
|
|
317
|
+
},
|
|
318
|
+
container,
|
|
319
|
+
imageSize
|
|
320
|
+
);
|
|
321
|
+
const vb = getVisibleBounds( baseCamera );
|
|
322
|
+
const cr = state.cropRect;
|
|
323
|
+
|
|
324
|
+
// Stencil corners in screen space.
|
|
325
|
+
const stencilCorners: [ number, number ][] = [
|
|
326
|
+
[ vb.left + cr.x * vb.width, vb.top + cr.y * vb.height ],
|
|
327
|
+
[
|
|
328
|
+
vb.left + ( cr.x + cr.width ) * vb.width,
|
|
329
|
+
vb.top + cr.y * vb.height,
|
|
330
|
+
],
|
|
331
|
+
[
|
|
332
|
+
vb.left + ( cr.x + cr.width ) * vb.width,
|
|
333
|
+
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
334
|
+
],
|
|
335
|
+
[
|
|
336
|
+
vb.left + cr.x * vb.width,
|
|
337
|
+
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
338
|
+
],
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
// Map to world space via inverse camera.
|
|
342
|
+
for ( const corner of stencilCorners ) {
|
|
343
|
+
const w = screenToWorld( camera, {
|
|
344
|
+
x: corner[ 0 ],
|
|
345
|
+
y: corner[ 1 ],
|
|
346
|
+
} );
|
|
347
|
+
expect( w.x ).toBeGreaterThanOrEqual( -0.001 );
|
|
348
|
+
expect( w.x ).toBeLessThanOrEqual( 1.001 );
|
|
349
|
+
expect( w.y ).toBeGreaterThanOrEqual( -0.001 );
|
|
350
|
+
expect( w.y ).toBeLessThanOrEqual( 1.001 );
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const ROTATIONS = [ 0, 15, 30, 45, 60, 75, 90, 135, 180, 270 ];
|
|
355
|
+
const ZOOMS = [ 1, 1.5, 2, 3, 5 ];
|
|
356
|
+
const CROP_RECTS = [
|
|
357
|
+
{ label: 'full', rect: { x: 0, y: 0, width: 1, height: 1 } },
|
|
358
|
+
{
|
|
359
|
+
label: 'centered-small',
|
|
360
|
+
rect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
|
|
361
|
+
},
|
|
362
|
+
{
|
|
363
|
+
label: 'off-center',
|
|
364
|
+
rect: { x: 0.1, y: 0.1, width: 0.3, height: 0.4 },
|
|
365
|
+
},
|
|
366
|
+
];
|
|
367
|
+
|
|
368
|
+
for ( const rotation of ROTATIONS ) {
|
|
369
|
+
for ( const zoom of ZOOMS ) {
|
|
370
|
+
for ( const { label, rect } of CROP_RECTS ) {
|
|
371
|
+
it( `rotation=${ rotation } zoom=${ zoom } crop=${ label }`, () => {
|
|
372
|
+
const state = makeState( {
|
|
373
|
+
rotation,
|
|
374
|
+
zoom,
|
|
375
|
+
cropRect: rect,
|
|
376
|
+
} );
|
|
377
|
+
const restricted = restrictPanZoom( state, IMAGE, rect );
|
|
378
|
+
expect( restricted.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
379
|
+
const restrictedState = makeState( {
|
|
380
|
+
...state,
|
|
381
|
+
pan: restricted.pan,
|
|
382
|
+
zoom: restricted.zoom,
|
|
383
|
+
cropRect: rect,
|
|
384
|
+
} );
|
|
385
|
+
verifyImageCoversCrop( restrictedState, IMAGE );
|
|
386
|
+
} );
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
it( 'holds across 200 random pan/zoom/rotation/crop combinations', () => {
|
|
392
|
+
let passCount = 0;
|
|
393
|
+
for ( let i = 0; i < 200; i++ ) {
|
|
394
|
+
// Deterministic-ish: use i to seed values.
|
|
395
|
+
const rotation =
|
|
396
|
+
ROTATIONS[ i % ROTATIONS.length ] + ( ( i * 7 ) % 15 );
|
|
397
|
+
const zoom = 1 + ( ( i * 13 ) % 40 ) / 10;
|
|
398
|
+
const cropW = 0.2 + ( ( i * 3 ) % 8 ) / 10;
|
|
399
|
+
const cropH = 0.2 + ( ( i * 5 ) % 8 ) / 10;
|
|
400
|
+
const cropX = Math.min( ( ( i * 11 ) % 10 ) / 10, 1 - cropW );
|
|
401
|
+
const cropY = Math.min( ( ( i * 17 ) % 10 ) / 10, 1 - cropH );
|
|
402
|
+
const rect = {
|
|
403
|
+
x: cropX,
|
|
404
|
+
y: cropY,
|
|
405
|
+
width: cropW,
|
|
406
|
+
height: cropH,
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const panX = ( ( ( i * 19 ) % 20 ) - 10 ) / 10;
|
|
410
|
+
const panY = ( ( ( i * 23 ) % 20 ) - 10 ) / 10;
|
|
411
|
+
|
|
412
|
+
const state = makeState( {
|
|
413
|
+
rotation,
|
|
414
|
+
zoom,
|
|
415
|
+
pan: { x: panX, y: panY },
|
|
416
|
+
cropRect: rect,
|
|
417
|
+
} );
|
|
418
|
+
|
|
419
|
+
const restricted = restrictPanZoom( state, IMAGE, rect );
|
|
420
|
+
const restrictedState = makeState( {
|
|
421
|
+
...state,
|
|
422
|
+
pan: restricted.pan,
|
|
423
|
+
zoom: restricted.zoom,
|
|
424
|
+
cropRect: rect,
|
|
425
|
+
} );
|
|
426
|
+
|
|
427
|
+
verifyImageCoversCrop( restrictedState, IMAGE );
|
|
428
|
+
passCount++;
|
|
429
|
+
}
|
|
430
|
+
expect( passCount ).toBe( 200 );
|
|
431
|
+
} );
|
|
432
|
+
} );
|
|
433
|
+
|
|
434
|
+
describe( 'getSourceRegion', () => {
|
|
435
|
+
it( 'at default state, source region matches full image', () => {
|
|
436
|
+
const state = makeState();
|
|
437
|
+
const region = getSourceRegion( state, IMAGE );
|
|
438
|
+
expect( region.x ).toBeCloseTo( 0, 0 );
|
|
439
|
+
expect( region.y ).toBeCloseTo( 0, 0 );
|
|
440
|
+
expect( region.width ).toBeCloseTo( IMAGE.width, 0 );
|
|
441
|
+
expect( region.height ).toBeCloseTo( IMAGE.height, 0 );
|
|
442
|
+
expect( region.rotation ).toBe( 0 );
|
|
443
|
+
expect( region.zoom ).toBe( 1 );
|
|
444
|
+
} );
|
|
445
|
+
|
|
446
|
+
it( 'at zoom=2 with centered crop, source region is half the image dimensions', () => {
|
|
447
|
+
const state = makeState( { zoom: 2 } );
|
|
448
|
+
const region = getSourceRegion( state, IMAGE );
|
|
449
|
+
expect( region.width ).toBeCloseTo( IMAGE.width / 2, 0 );
|
|
450
|
+
expect( region.height ).toBeCloseTo( IMAGE.height / 2, 0 );
|
|
451
|
+
// Centered: source region center should be at image center.
|
|
452
|
+
expect( region.x + region.width / 2 ).toBeCloseTo( IMAGE.width / 2, 0 );
|
|
453
|
+
expect( region.y + region.height / 2 ).toBeCloseTo(
|
|
454
|
+
IMAGE.height / 2,
|
|
455
|
+
0
|
|
456
|
+
);
|
|
457
|
+
} );
|
|
458
|
+
|
|
459
|
+
it( 'at 90-degree rotation, source region dimensions are swapped', () => {
|
|
460
|
+
const state = makeState( { rotation: 90 } );
|
|
461
|
+
const region = getSourceRegion( state, IMAGE );
|
|
462
|
+
// At 90° the rotated bounding box swaps roles: the crop covers the
|
|
463
|
+
// full visual area, so the visible source width maps from the image
|
|
464
|
+
// height and vice versa. The key invariant is that the region's
|
|
465
|
+
// aspect ratio flips relative to the default.
|
|
466
|
+
const defaultRegion = getSourceRegion( makeState(), IMAGE );
|
|
467
|
+
const defaultAR = defaultRegion.width / defaultRegion.height;
|
|
468
|
+
const rotatedAR = region.width / region.height;
|
|
469
|
+
// Rotated AR should be roughly the inverse of default AR.
|
|
470
|
+
expect( rotatedAR ).toBeCloseTo( 1 / defaultAR, 1 );
|
|
471
|
+
expect( region.rotation ).toBe( 90 );
|
|
472
|
+
} );
|
|
473
|
+
} );
|
|
474
|
+
|
|
475
|
+
describe( 'getSourceRegionPercent', () => {
|
|
476
|
+
it( 'at default state, returns 0/0/100/100', () => {
|
|
477
|
+
const state = makeState();
|
|
478
|
+
const pct = getSourceRegionPercent( state, IMAGE );
|
|
479
|
+
expect( pct.x ).toBeCloseTo( 0, 0 );
|
|
480
|
+
expect( pct.y ).toBeCloseTo( 0, 0 );
|
|
481
|
+
expect( pct.width ).toBeCloseTo( 100, 0 );
|
|
482
|
+
expect( pct.height ).toBeCloseTo( 100, 0 );
|
|
483
|
+
} );
|
|
484
|
+
|
|
485
|
+
it( 'at zoom=2 centered, returns 25/25/50/50', () => {
|
|
486
|
+
const state = makeState( { zoom: 2 } );
|
|
487
|
+
const pct = getSourceRegionPercent( state, IMAGE );
|
|
488
|
+
expect( pct.width ).toBeCloseTo( 50, 0 );
|
|
489
|
+
expect( pct.height ).toBeCloseTo( 50, 0 );
|
|
490
|
+
// Centered: x and y should each be 25%.
|
|
491
|
+
expect( pct.x ).toBeCloseTo( 25, 0 );
|
|
492
|
+
expect( pct.y ).toBeCloseTo( 25, 0 );
|
|
493
|
+
} );
|
|
494
|
+
|
|
495
|
+
it( 'percentages sum correctly (x + width ≤ 100, y + height ≤ 100)', () => {
|
|
496
|
+
const state = makeState( { zoom: 3, pan: { x: 0.1, y: -0.05 } } );
|
|
497
|
+
const pct = getSourceRegionPercent( state, IMAGE );
|
|
498
|
+
expect( pct.x + pct.width ).toBeLessThanOrEqual( 100.01 );
|
|
499
|
+
expect( pct.y + pct.height ).toBeLessThanOrEqual( 100.01 );
|
|
500
|
+
expect( pct.x ).toBeGreaterThanOrEqual( -0.01 );
|
|
501
|
+
expect( pct.y ).toBeGreaterThanOrEqual( -0.01 );
|
|
502
|
+
} );
|
|
503
|
+
|
|
504
|
+
it( 'matches getSourceRegion divided by image dimensions', () => {
|
|
505
|
+
const state = makeState( {
|
|
506
|
+
zoom: 1.5,
|
|
507
|
+
pan: { x: 0.05, y: -0.02 },
|
|
508
|
+
rotation: 15,
|
|
509
|
+
} );
|
|
510
|
+
const region = getSourceRegion( state, IMAGE );
|
|
511
|
+
const pct = getSourceRegionPercent( state, IMAGE );
|
|
512
|
+
expect( pct.x ).toBeCloseTo( ( region.x / IMAGE.width ) * 100, 5 );
|
|
513
|
+
expect( pct.y ).toBeCloseTo( ( region.y / IMAGE.height ) * 100, 5 );
|
|
514
|
+
expect( pct.width ).toBeCloseTo(
|
|
515
|
+
( region.width / IMAGE.width ) * 100,
|
|
516
|
+
5
|
|
517
|
+
);
|
|
518
|
+
expect( pct.height ).toBeCloseTo(
|
|
519
|
+
( region.height / IMAGE.height ) * 100,
|
|
520
|
+
5
|
|
521
|
+
);
|
|
522
|
+
} );
|
|
523
|
+
|
|
524
|
+
it( 'returns zeros for zero-dimension image', () => {
|
|
525
|
+
const state = makeState();
|
|
526
|
+
const pct = getSourceRegionPercent( state, {
|
|
527
|
+
width: 0,
|
|
528
|
+
height: 0,
|
|
529
|
+
} );
|
|
530
|
+
expect( pct.x ).toBe( 0 );
|
|
531
|
+
expect( pct.y ).toBe( 0 );
|
|
532
|
+
expect( pct.width ).toBe( 0 );
|
|
533
|
+
expect( pct.height ).toBe( 0 );
|
|
534
|
+
} );
|
|
535
|
+
|
|
536
|
+
it( 'with small centered crop rect, percentages reflect visible portion', () => {
|
|
537
|
+
// 50% crop rect centered → at zoom=1, the crop covers half the
|
|
538
|
+
// visual area. The percentage region should be roughly 50% of
|
|
539
|
+
// the image in each axis.
|
|
540
|
+
const state = makeState( {
|
|
541
|
+
cropRect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
|
|
542
|
+
zoom: 1,
|
|
543
|
+
} );
|
|
544
|
+
const pct = getSourceRegionPercent( state, IMAGE );
|
|
545
|
+
expect( pct.width ).toBeCloseTo( 50, 0 );
|
|
546
|
+
expect( pct.height ).toBeCloseTo( 50, 0 );
|
|
547
|
+
} );
|
|
548
|
+
} );
|
|
549
|
+
|
|
550
|
+
describe( 'getCropBounds', () => {
|
|
551
|
+
it( 'allows crop handle to reach container edge when 4:3 image is zoomed in 3:2 container', () => {
|
|
552
|
+
// MtBlanc1.jpg is 500×375 (4:3) in a 600×400 container (3:2).
|
|
553
|
+
// At zoom=1, the image is 533×400 — 33px padding on each side.
|
|
554
|
+
// At zoom=1.75, the image fills the container. The crop handle
|
|
555
|
+
// should be able to reach the container left edge (pixel 0),
|
|
556
|
+
// which is at normalized x = -0.0625.
|
|
557
|
+
const nat: Size = { width: 500, height: 375 };
|
|
558
|
+
const container: Size = { width: 600, height: 400 };
|
|
559
|
+
const { elementSize, visualSize } = getImageFit( container, nat, 0 );
|
|
560
|
+
|
|
561
|
+
// Verify the image doesn't fill the container at zoom=1.
|
|
562
|
+
expect( visualSize.width ).toBeLessThan( container.width );
|
|
563
|
+
|
|
564
|
+
const state = makeState( {
|
|
565
|
+
image: {
|
|
566
|
+
src: 'test.jpg',
|
|
567
|
+
naturalWidth: nat.width,
|
|
568
|
+
naturalHeight: nat.height,
|
|
569
|
+
},
|
|
570
|
+
zoom: 1.75,
|
|
571
|
+
rotation: 0,
|
|
572
|
+
} );
|
|
573
|
+
|
|
574
|
+
const bounds = getCropBounds(
|
|
575
|
+
state,
|
|
576
|
+
elementSize,
|
|
577
|
+
visualSize,
|
|
578
|
+
container
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
// boundsMinX should be negative (crop can extend left of visual image origin).
|
|
582
|
+
expect( bounds.minX ).toBeLessThan( 0 );
|
|
583
|
+
// The pixel position of boundsMinX should be the container left edge (pixel 0).
|
|
584
|
+
const offsetX = ( container.width - visualSize.width ) / 2;
|
|
585
|
+
const pixelLeft = offsetX + bounds.minX * visualSize.width;
|
|
586
|
+
expect( pixelLeft ).toBeCloseTo( 0, 0 );
|
|
587
|
+
|
|
588
|
+
// boundsMaxX should be > 1 (crop can extend right of visual image).
|
|
589
|
+
expect( bounds.maxX ).toBeGreaterThan( 1 );
|
|
590
|
+
const pixelRight = offsetX + bounds.maxX * visualSize.width;
|
|
591
|
+
expect( pixelRight ).toBeCloseTo( container.width, 0 );
|
|
592
|
+
} );
|
|
593
|
+
} );
|