@wordpress/media-editor 0.19.0 → 0.21.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 +34 -0
- package/README.md +2 -1
- package/build/components/media-editor/index.cjs +136 -63
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +2 -3
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor/use-save-media-editor.cjs +36 -20
- package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +8 -8
- package/build/components/media-editor-canvas/index.cjs.map +3 -3
- package/build/components/media-editor-crop-panel/index.cjs +12 -8
- package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
- package/build/components/media-editor-fine-rotation/index.cjs +1 -1
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +46 -29
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +19 -9
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- package/build/components/media-editor-provider/index.cjs +2 -1
- package/build/components/media-editor-provider/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs +28 -28
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-preview/index.cjs.map +3 -3
- package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +5 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +4 -14
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +35 -10
- package/build/state/composite-reducer.cjs.map +3 -3
- package/build/state/index.cjs.map +1 -1
- package/build/state/media-editor-state-provider.cjs +1 -6
- package/build/state/media-editor-state-provider.cjs.map +2 -2
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +57 -42
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build/store/actions.cjs +2 -0
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +3 -1
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +151 -67
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +2 -3
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +10 -10
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +1 -1
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +47 -36
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +19 -9
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-provider/index.mjs +2 -1
- package/build-module/components/media-editor-provider/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +33 -30
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-preview/index.mjs +2 -2
- package/build-module/components/media-preview/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +5 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +4 -14
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +25 -10
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/index.mjs.map +1 -1
- package/build-module/state/media-editor-state-provider.mjs +1 -6
- package/build-module/state/media-editor-state-provider.mjs.map +2 -2
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +57 -42
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-module/store/actions.mjs +2 -0
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +3 -1
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +5 -5
- package/build-style/style.css +5 -5
- package/build-types/components/media-editor/index.d.ts +4 -3
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts +15 -3
- package/build-types/components/media-editor/use-save-media-editor.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.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts +7 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +7 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +5 -2
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.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/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +5 -5
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +2 -2
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/media-editor-state-provider.d.ts +8 -16
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
- package/build-types/state/types.d.ts +34 -5
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +53 -35
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +9 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +41 -25
- package/src/components/media-editor/index.tsx +213 -79
- package/src/components/media-editor/use-crop-options.ts +1 -3
- package/src/components/media-editor/use-save-media-editor.ts +61 -20
- package/src/components/media-editor-canvas/index.tsx +9 -10
- package/src/components/media-editor-crop-panel/index.tsx +17 -9
- package/src/components/media-editor-fine-rotation/index.tsx +1 -1
- package/src/components/media-editor-image-controls/index.tsx +41 -41
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-modal/index.tsx +26 -11
- package/src/components/media-editor-provider/index.tsx +8 -1
- package/src/components/media-form/index.tsx +48 -42
- package/src/components/media-preview/index.tsx +2 -2
- package/src/hooks/use-crop-gesture-handlers.ts +3 -1
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/docs/architecture.md +10 -2
- package/src/image-editor/docs/recipes.md +8 -8
- package/src/image-editor/react/components/cropper.scss +5 -5
- package/src/image-editor/react/components/cropper.tsx +9 -18
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/state/composite-reducer.ts +29 -17
- package/src/state/index.ts +3 -1
- package/src/state/media-editor-state-provider.tsx +9 -22
- package/src/state/types.ts +37 -5
- package/src/state/use-media-editor-state.ts +124 -78
- package/src/store/actions.ts +9 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/global.d.ts +0 -5
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -104
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -74
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -180
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -165
- package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
- package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
- package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
- package/src/image-editor/core/math/test/sanitize.ts +0 -242
- package/src/image-editor/core/test/camera.ts +0 -1218
- package/src/image-editor/core/test/interaction-controller.ts +0 -1254
- package/src/image-editor/core/test/preview-export-parity.ts +0 -249
- package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
- package/src/image-editor/core/test/state.ts +0 -876
- package/src/image-editor/core/test/stencil-math.ts +0 -665
- package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
- package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1460
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
- package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
- package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
- package/src/image-editor/stories/style.module.css +0 -65
- package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -324
- package/src/store/test/reducer.ts +0 -64
- package/tsconfig.build.json +0 -26
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -37
|
@@ -1,1218 +0,0 @@
|
|
|
1
|
-
import { mat2d as m2d, vec2 } from 'gl-matrix';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
import {
|
|
4
|
-
createCamera,
|
|
5
|
-
worldToScreen,
|
|
6
|
-
screenToWorld,
|
|
7
|
-
getVisibleBounds,
|
|
8
|
-
createExportCamera,
|
|
9
|
-
getImageFit,
|
|
10
|
-
getRotatedBBox,
|
|
11
|
-
getViewScale,
|
|
12
|
-
} from '../camera';
|
|
13
|
-
import {
|
|
14
|
-
restrictPanZoom,
|
|
15
|
-
restrictCropRect,
|
|
16
|
-
getImageCropBounds,
|
|
17
|
-
getMinZoom,
|
|
18
|
-
} from '../containment';
|
|
19
|
-
import {
|
|
20
|
-
getSourceRegion,
|
|
21
|
-
getSourceRegionPercent,
|
|
22
|
-
snapCropRectToSourcePixelGrid,
|
|
23
|
-
snapCropRectToSourcePixels,
|
|
24
|
-
} from '../source-region';
|
|
25
|
-
import { computeTransformStyle } from '../transform-style';
|
|
26
|
-
import { DEFAULT_STATE, MAX_ZOOM, MIN_ZOOM } from '../constants';
|
|
27
|
-
import type { CropperState, Size } from '../types';
|
|
28
|
-
|
|
29
|
-
const CONTAINER: Size = { width: 800, height: 600 };
|
|
30
|
-
const IMAGE: Size = { width: 1600, height: 900 };
|
|
31
|
-
const PORTRAIT_IMAGE: Size = { width: 900, height: 1600 };
|
|
32
|
-
|
|
33
|
-
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
34
|
-
return {
|
|
35
|
-
...DEFAULT_STATE,
|
|
36
|
-
image: {
|
|
37
|
-
src: 'test.jpg',
|
|
38
|
-
naturalWidth: IMAGE.width,
|
|
39
|
-
naturalHeight: IMAGE.height,
|
|
40
|
-
},
|
|
41
|
-
...overrides,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function expectImageCoversCrop( state: CropperState, imageSize: Size ): void {
|
|
46
|
-
const container: Size = { width: 1000, height: 1000 };
|
|
47
|
-
const camera = createCamera( state, container, imageSize );
|
|
48
|
-
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
49
|
-
const baseCamera = createCamera(
|
|
50
|
-
{
|
|
51
|
-
...state,
|
|
52
|
-
pan: { x: 0, y: 0 },
|
|
53
|
-
zoom: 1,
|
|
54
|
-
rotation: snapRotation,
|
|
55
|
-
},
|
|
56
|
-
container,
|
|
57
|
-
imageSize
|
|
58
|
-
);
|
|
59
|
-
const vb = getVisibleBounds( baseCamera );
|
|
60
|
-
const cr = state.cropRect;
|
|
61
|
-
const stencilCorners: [ number, number ][] = [
|
|
62
|
-
[ vb.left + cr.x * vb.width, vb.top + cr.y * vb.height ],
|
|
63
|
-
[ vb.left + ( cr.x + cr.width ) * vb.width, vb.top + cr.y * vb.height ],
|
|
64
|
-
[
|
|
65
|
-
vb.left + ( cr.x + cr.width ) * vb.width,
|
|
66
|
-
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
67
|
-
],
|
|
68
|
-
[
|
|
69
|
-
vb.left + cr.x * vb.width,
|
|
70
|
-
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
71
|
-
],
|
|
72
|
-
];
|
|
73
|
-
|
|
74
|
-
for ( const corner of stencilCorners ) {
|
|
75
|
-
const w = screenToWorld( camera, {
|
|
76
|
-
x: corner[ 0 ],
|
|
77
|
-
y: corner[ 1 ],
|
|
78
|
-
} );
|
|
79
|
-
expect( w.x ).toBeGreaterThanOrEqual( -0.001 );
|
|
80
|
-
expect( w.x ).toBeLessThanOrEqual( 1.001 );
|
|
81
|
-
expect( w.y ).toBeGreaterThanOrEqual( -0.001 );
|
|
82
|
-
expect( w.y ).toBeLessThanOrEqual( 1.001 );
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
describe( 'createCamera', () => {
|
|
87
|
-
it( 'returns a mat2d', () => {
|
|
88
|
-
const camera = createCamera( makeState(), CONTAINER, IMAGE );
|
|
89
|
-
expect( camera ).toHaveLength( 6 );
|
|
90
|
-
} );
|
|
91
|
-
|
|
92
|
-
it( 'maps image center (0.5, 0.5) to container center at identity state', () => {
|
|
93
|
-
const state = makeState();
|
|
94
|
-
const camera = createCamera( state, CONTAINER, IMAGE );
|
|
95
|
-
const screenPt = worldToScreen( camera, { x: 0.5, y: 0.5 } );
|
|
96
|
-
expect( screenPt.x ).toBeCloseTo( CONTAINER.width / 2, 0 );
|
|
97
|
-
expect( screenPt.y ).toBeCloseTo( CONTAINER.height / 2, 0 );
|
|
98
|
-
} );
|
|
99
|
-
|
|
100
|
-
it( 'worldToScreen and screenToWorld are inverses', () => {
|
|
101
|
-
const state = makeState( {
|
|
102
|
-
zoom: 1.5,
|
|
103
|
-
rotation: 30,
|
|
104
|
-
pan: { x: 0.1, y: -0.05 },
|
|
105
|
-
} );
|
|
106
|
-
const camera = createCamera( state, CONTAINER, IMAGE );
|
|
107
|
-
const worldPt = { x: 0.3, y: 0.7 };
|
|
108
|
-
const screenPt = worldToScreen( camera, worldPt );
|
|
109
|
-
const roundTrip = screenToWorld( camera, screenPt );
|
|
110
|
-
expect( roundTrip.x ).toBeCloseTo( worldPt.x, 5 );
|
|
111
|
-
expect( roundTrip.y ).toBeCloseTo( worldPt.y, 5 );
|
|
112
|
-
} );
|
|
113
|
-
|
|
114
|
-
it( 'zoom=2 makes the image appear twice as large', () => {
|
|
115
|
-
const state1 = makeState();
|
|
116
|
-
const state2 = makeState( { zoom: 2 } );
|
|
117
|
-
const cam1 = createCamera( state1, CONTAINER, IMAGE );
|
|
118
|
-
const cam2 = createCamera( state2, CONTAINER, IMAGE );
|
|
119
|
-
const p1 = worldToScreen( cam1, { x: 0.75, y: 0.5 } );
|
|
120
|
-
const p2 = worldToScreen( cam2, { x: 0.75, y: 0.5 } );
|
|
121
|
-
const center = CONTAINER.width / 2;
|
|
122
|
-
expect( p2.x - center ).toBeCloseTo( 2 * ( p1.x - center ), 0 );
|
|
123
|
-
} );
|
|
124
|
-
|
|
125
|
-
it( 'horizontal flip mirrors x around container center', () => {
|
|
126
|
-
const normal = makeState();
|
|
127
|
-
const flipped = makeState( {
|
|
128
|
-
flip: { horizontal: true, vertical: false },
|
|
129
|
-
} );
|
|
130
|
-
const camN = createCamera( normal, CONTAINER, IMAGE );
|
|
131
|
-
const camF = createCamera( flipped, CONTAINER, IMAGE );
|
|
132
|
-
const ptN = worldToScreen( camN, { x: 0.25, y: 0.5 } );
|
|
133
|
-
const ptF = worldToScreen( camF, { x: 0.25, y: 0.5 } );
|
|
134
|
-
const center = CONTAINER.width / 2;
|
|
135
|
-
expect( ptF.x ).toBeCloseTo( 2 * center - ptN.x, 0 );
|
|
136
|
-
} );
|
|
137
|
-
|
|
138
|
-
it( 'rotation=90 rotates points 90 degrees around center', () => {
|
|
139
|
-
const state = makeState( { rotation: 90 } );
|
|
140
|
-
const camera = createCamera( state, CONTAINER, IMAGE );
|
|
141
|
-
const center = worldToScreen( camera, { x: 0.5, y: 0.5 } );
|
|
142
|
-
expect( center.x ).toBeCloseTo( CONTAINER.width / 2, 0 );
|
|
143
|
-
expect( center.y ).toBeCloseTo( CONTAINER.height / 2, 0 );
|
|
144
|
-
} );
|
|
145
|
-
|
|
146
|
-
it( 'pan shifts the image in screen space', () => {
|
|
147
|
-
const noPan = makeState();
|
|
148
|
-
const withPan = makeState( { pan: { x: 0.1, y: 0 } } );
|
|
149
|
-
const cam1 = createCamera( noPan, CONTAINER, IMAGE );
|
|
150
|
-
const cam2 = createCamera( withPan, CONTAINER, IMAGE );
|
|
151
|
-
const p1 = worldToScreen( cam1, { x: 0.5, y: 0.5 } );
|
|
152
|
-
const p2 = worldToScreen( cam2, { x: 0.5, y: 0.5 } );
|
|
153
|
-
expect( p2.x ).toBeGreaterThan( p1.x );
|
|
154
|
-
} );
|
|
155
|
-
} );
|
|
156
|
-
|
|
157
|
-
describe( 'restrictPanZoom', () => {
|
|
158
|
-
it( 'returns identity pan at default state', () => {
|
|
159
|
-
const state = makeState();
|
|
160
|
-
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
161
|
-
expect( result.pan.x ).toBeCloseTo( 0 );
|
|
162
|
-
expect( result.pan.y ).toBeCloseTo( 0 );
|
|
163
|
-
expect( result.zoom ).toBeCloseTo( 1 );
|
|
164
|
-
} );
|
|
165
|
-
it( 'clamps pan so image covers crop rect', () => {
|
|
166
|
-
const state = makeState( { pan: { x: 5, y: 5 }, zoom: 1 } );
|
|
167
|
-
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
168
|
-
expect( Math.abs( result.pan.x ) ).toBeLessThan( 1 );
|
|
169
|
-
expect( Math.abs( result.pan.y ) ).toBeLessThan( 1 );
|
|
170
|
-
} );
|
|
171
|
-
it( 'increases zoom if too low for rotation', () => {
|
|
172
|
-
const state = makeState( { rotation: 45, zoom: 1 } );
|
|
173
|
-
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
174
|
-
expect( result.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
175
|
-
} );
|
|
176
|
-
it( 'caps zoom at MAX_ZOOM', () => {
|
|
177
|
-
const state = makeState( { zoom: MAX_ZOOM * 2 } );
|
|
178
|
-
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
179
|
-
expect( result.zoom ).toBe( MAX_ZOOM );
|
|
180
|
-
} );
|
|
181
|
-
it( 'allows zoom below 1 when a fine-rotated portrait crop remains covered', () => {
|
|
182
|
-
const cropRect = { x: 0.46, y: 0, width: 0.08, height: 1 };
|
|
183
|
-
const state = makeState( {
|
|
184
|
-
image: {
|
|
185
|
-
src: 'portrait.jpg',
|
|
186
|
-
naturalWidth: PORTRAIT_IMAGE.width,
|
|
187
|
-
naturalHeight: PORTRAIT_IMAGE.height,
|
|
188
|
-
},
|
|
189
|
-
rotation: 19,
|
|
190
|
-
zoom: 0.9,
|
|
191
|
-
cropRect,
|
|
192
|
-
} );
|
|
193
|
-
const result = restrictPanZoom( state, PORTRAIT_IMAGE, cropRect );
|
|
194
|
-
|
|
195
|
-
expect( result.zoom ).toBeGreaterThan( 0.9 );
|
|
196
|
-
expect( result.zoom ).toBeLessThan( 1 );
|
|
197
|
-
|
|
198
|
-
const restrictedState = makeState( {
|
|
199
|
-
...state,
|
|
200
|
-
pan: result.pan,
|
|
201
|
-
zoom: result.zoom,
|
|
202
|
-
} );
|
|
203
|
-
expectImageCoversCrop( restrictedState, PORTRAIT_IMAGE );
|
|
204
|
-
} );
|
|
205
|
-
it( 'at 90° with zoom=1, allows zero pan on landscape image', () => {
|
|
206
|
-
// At zoom=1, 90° rotation, the image exactly covers the visual area.
|
|
207
|
-
// No pan should be possible in either direction.
|
|
208
|
-
const state = makeState( {
|
|
209
|
-
rotation: 90,
|
|
210
|
-
zoom: 1,
|
|
211
|
-
pan: { x: 0.3, y: 0.3 },
|
|
212
|
-
} );
|
|
213
|
-
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
214
|
-
expect( result.pan.x ).toBeCloseTo( 0, 5 );
|
|
215
|
-
expect( result.pan.y ).toBeCloseTo( 0, 5 );
|
|
216
|
-
} );
|
|
217
|
-
it( 'at 90° with zoom=2, allows symmetric pan range', () => {
|
|
218
|
-
// When zoomed in at 90° rotation, the pan range should be symmetric
|
|
219
|
-
// between x and y (both should allow ±0.5 of visual dimension).
|
|
220
|
-
const state90pos = makeState( {
|
|
221
|
-
rotation: 90,
|
|
222
|
-
zoom: 2,
|
|
223
|
-
pan: { x: 0.5, y: 0 },
|
|
224
|
-
} );
|
|
225
|
-
const state90neg = makeState( {
|
|
226
|
-
rotation: 90,
|
|
227
|
-
zoom: 2,
|
|
228
|
-
pan: { x: -0.5, y: 0 },
|
|
229
|
-
} );
|
|
230
|
-
const resultPos = restrictPanZoom(
|
|
231
|
-
state90pos,
|
|
232
|
-
IMAGE,
|
|
233
|
-
state90pos.cropRect
|
|
234
|
-
);
|
|
235
|
-
const resultNeg = restrictPanZoom(
|
|
236
|
-
state90neg,
|
|
237
|
-
IMAGE,
|
|
238
|
-
state90neg.cropRect
|
|
239
|
-
);
|
|
240
|
-
// Should allow meaningful horizontal pan (not clamped to 0).
|
|
241
|
-
expect( resultPos.pan.x ).toBeGreaterThan( 0.1 );
|
|
242
|
-
expect( resultNeg.pan.x ).toBeLessThan( -0.1 );
|
|
243
|
-
// And the range should be symmetric.
|
|
244
|
-
expect( resultPos.pan.x ).toBeCloseTo( -resultNeg.pan.x, 5 );
|
|
245
|
-
} );
|
|
246
|
-
it( 'at 0° with zoom=1, allows zero pan on landscape image', () => {
|
|
247
|
-
const state = makeState( {
|
|
248
|
-
rotation: 0,
|
|
249
|
-
zoom: 1,
|
|
250
|
-
pan: { x: 0.3, y: 0.3 },
|
|
251
|
-
} );
|
|
252
|
-
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
253
|
-
expect( result.pan.x ).toBeCloseTo( 0, 5 );
|
|
254
|
-
expect( result.pan.y ).toBeCloseTo( 0, 5 );
|
|
255
|
-
} );
|
|
256
|
-
} );
|
|
257
|
-
|
|
258
|
-
describe( 'getMinZoom', () => {
|
|
259
|
-
it( 'falls back to MIN_ZOOM for invalid image dimensions', () => {
|
|
260
|
-
const state = makeState( {
|
|
261
|
-
image: {
|
|
262
|
-
src: 'invalid.jpg',
|
|
263
|
-
naturalWidth: 1000,
|
|
264
|
-
naturalHeight: 0,
|
|
265
|
-
},
|
|
266
|
-
} );
|
|
267
|
-
|
|
268
|
-
expect( getMinZoom( state ) ).toBe( MIN_ZOOM );
|
|
269
|
-
} );
|
|
270
|
-
|
|
271
|
-
it( 'returns a finite value for non-finite rotation and crop fields', () => {
|
|
272
|
-
const state = makeState( {
|
|
273
|
-
rotation: Number.NaN,
|
|
274
|
-
cropRect: {
|
|
275
|
-
x: 0,
|
|
276
|
-
y: 0,
|
|
277
|
-
width: Number.NaN,
|
|
278
|
-
height: 1,
|
|
279
|
-
},
|
|
280
|
-
} );
|
|
281
|
-
|
|
282
|
-
const minZoom = getMinZoom( state );
|
|
283
|
-
expect( Number.isFinite( minZoom ) ).toBe( true );
|
|
284
|
-
expect( minZoom ).toBeGreaterThan( 0 );
|
|
285
|
-
} );
|
|
286
|
-
} );
|
|
287
|
-
|
|
288
|
-
describe( 'restrictCropRect', () => {
|
|
289
|
-
it( 'returns same rect when it fits', () => {
|
|
290
|
-
const rect = { x: 0, y: 0, width: 1, height: 1 };
|
|
291
|
-
const result = restrictCropRect( rect, 1, 0, 16 / 9 );
|
|
292
|
-
expect( result ).toBe( rect );
|
|
293
|
-
} );
|
|
294
|
-
it( 'shrinks rect when too large for zoom/rotation', () => {
|
|
295
|
-
const rect = { x: 0, y: 0, width: 1, height: 1 };
|
|
296
|
-
const result = restrictCropRect( rect, 1, 45, 1 );
|
|
297
|
-
expect( result.width ).toBeLessThan( 1 );
|
|
298
|
-
expect( result.height ).toBeLessThan( 1 );
|
|
299
|
-
} );
|
|
300
|
-
} );
|
|
301
|
-
|
|
302
|
-
describe( 'createExportCamera', () => {
|
|
303
|
-
it( 'image center maps to output center at identity state with full crop', () => {
|
|
304
|
-
const state = makeState();
|
|
305
|
-
const outputSize = { width: 400, height: 225 };
|
|
306
|
-
const camera = createExportCamera( state, IMAGE, outputSize );
|
|
307
|
-
const out = vec2.create();
|
|
308
|
-
vec2.transformMat2d(
|
|
309
|
-
out,
|
|
310
|
-
[ IMAGE.width / 2, IMAGE.height / 2 ],
|
|
311
|
-
camera
|
|
312
|
-
);
|
|
313
|
-
expect( out[ 0 ] ).toBeCloseTo( outputSize.width / 2, 0 );
|
|
314
|
-
expect( out[ 1 ] ).toBeCloseTo( outputSize.height / 2, 0 );
|
|
315
|
-
} );
|
|
316
|
-
|
|
317
|
-
it( 'matches current renderToCanvas: identity maps full image to output', () => {
|
|
318
|
-
const state = makeState();
|
|
319
|
-
const outputSize = { width: IMAGE.width, height: IMAGE.height };
|
|
320
|
-
const camera = createExportCamera( state, IMAGE, outputSize );
|
|
321
|
-
const topLeft = vec2.create();
|
|
322
|
-
vec2.transformMat2d( topLeft, [ 0, 0 ], camera );
|
|
323
|
-
expect( topLeft[ 0 ] ).toBeCloseTo( 0, 0 );
|
|
324
|
-
expect( topLeft[ 1 ] ).toBeCloseTo( 0, 0 );
|
|
325
|
-
const bottomRight = vec2.create();
|
|
326
|
-
vec2.transformMat2d(
|
|
327
|
-
bottomRight,
|
|
328
|
-
[ IMAGE.width, IMAGE.height ],
|
|
329
|
-
camera
|
|
330
|
-
);
|
|
331
|
-
expect( bottomRight[ 0 ] ).toBeCloseTo( outputSize.width, 0 );
|
|
332
|
-
expect( bottomRight[ 1 ] ).toBeCloseTo( outputSize.height, 0 );
|
|
333
|
-
} );
|
|
334
|
-
|
|
335
|
-
it( 'at fine rotation, exported pixels track the stencil frame (preview / export agree)', () => {
|
|
336
|
-
// Stencil frames some image content via the preview camera; the
|
|
337
|
-
// export camera must pick the SAME image pixels. Before this
|
|
338
|
-
// fix, the export used the true-rotation bbox while the preview
|
|
339
|
-
// used the snap-rotation bbox, so any fine rotation shifted the
|
|
340
|
-
// exported region sideways relative to what the stencil framed.
|
|
341
|
-
const state = makeState( {
|
|
342
|
-
rotation: 15,
|
|
343
|
-
zoom: 1.5,
|
|
344
|
-
cropRect: { x: 0.2, y: 0.15, width: 0.5, height: 0.6 },
|
|
345
|
-
} );
|
|
346
|
-
const container: Size = { width: 800, height: 600 };
|
|
347
|
-
|
|
348
|
-
// Preview path: find the source-image pixel that lands at the
|
|
349
|
-
// stencil center on screen.
|
|
350
|
-
const previewCamera = createCamera( state, container, IMAGE );
|
|
351
|
-
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
352
|
-
const baseCamera = createCamera(
|
|
353
|
-
{
|
|
354
|
-
...state,
|
|
355
|
-
pan: { x: 0, y: 0 },
|
|
356
|
-
zoom: 1,
|
|
357
|
-
rotation: snapRotation,
|
|
358
|
-
},
|
|
359
|
-
container,
|
|
360
|
-
IMAGE
|
|
361
|
-
);
|
|
362
|
-
const vb = getVisibleBounds( baseCamera );
|
|
363
|
-
const stencilCenterScreen = {
|
|
364
|
-
x:
|
|
365
|
-
vb.left +
|
|
366
|
-
( state.cropRect.x + state.cropRect.width / 2 ) * vb.width,
|
|
367
|
-
y:
|
|
368
|
-
vb.top +
|
|
369
|
-
( state.cropRect.y + state.cropRect.height / 2 ) * vb.height,
|
|
370
|
-
};
|
|
371
|
-
const previewWorld = screenToWorld(
|
|
372
|
-
previewCamera,
|
|
373
|
-
stencilCenterScreen
|
|
374
|
-
);
|
|
375
|
-
const previewPx = {
|
|
376
|
-
x: previewWorld.x * IMAGE.width,
|
|
377
|
-
y: previewWorld.y * IMAGE.height,
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
// Export path: the source pixel that maps to the center of the
|
|
381
|
-
// output canvas.
|
|
382
|
-
const outputSize = { width: 400, height: 300 };
|
|
383
|
-
const exportCamera = createExportCamera( state, IMAGE, outputSize );
|
|
384
|
-
const inv = m2d.create();
|
|
385
|
-
m2d.invert( inv, exportCamera );
|
|
386
|
-
const exportPxVec = vec2.create();
|
|
387
|
-
vec2.transformMat2d(
|
|
388
|
-
exportPxVec,
|
|
389
|
-
[ outputSize.width / 2, outputSize.height / 2 ],
|
|
390
|
-
inv
|
|
391
|
-
);
|
|
392
|
-
|
|
393
|
-
// Preview and export must agree on the source pixel at crop center.
|
|
394
|
-
expect( exportPxVec[ 0 ] ).toBeCloseTo( previewPx.x, 0 );
|
|
395
|
-
expect( exportPxVec[ 1 ] ).toBeCloseTo( previewPx.y, 0 );
|
|
396
|
-
} );
|
|
397
|
-
} );
|
|
398
|
-
|
|
399
|
-
describe( 'containment invariant (property-based)', () => {
|
|
400
|
-
/**
|
|
401
|
-
* Verify that the image fully covers the crop rect by projecting all
|
|
402
|
-
* four crop corners through the inverse camera and checking that the
|
|
403
|
-
* resulting world-space points lie within [0,1] x [0,1] (the image).
|
|
404
|
-
*
|
|
405
|
-
* @param state The cropper state to verify.
|
|
406
|
-
* @param imageSize The natural image dimensions.
|
|
407
|
-
*/
|
|
408
|
-
function verifyImageCoversCrop(
|
|
409
|
-
state: CropperState,
|
|
410
|
-
imageSize: Size
|
|
411
|
-
): void {
|
|
412
|
-
const container: Size = { width: 1000, height: 1000 };
|
|
413
|
-
const camera = createCamera( state, container, imageSize );
|
|
414
|
-
|
|
415
|
-
// Build base camera (zero pan, zoom=1) for stencil positioning.
|
|
416
|
-
// The stencil is laid out at the nearest 90° rotation (matching
|
|
417
|
-
// `getImageFit` in production), so build the base camera with
|
|
418
|
-
// that snap rotation for consistent stencil corners.
|
|
419
|
-
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
420
|
-
const baseCamera = createCamera(
|
|
421
|
-
{
|
|
422
|
-
...state,
|
|
423
|
-
pan: { x: 0, y: 0 },
|
|
424
|
-
zoom: 1,
|
|
425
|
-
rotation: snapRotation,
|
|
426
|
-
},
|
|
427
|
-
container,
|
|
428
|
-
imageSize
|
|
429
|
-
);
|
|
430
|
-
const vb = getVisibleBounds( baseCamera );
|
|
431
|
-
const cr = state.cropRect;
|
|
432
|
-
|
|
433
|
-
// Stencil corners in screen space.
|
|
434
|
-
const stencilCorners: [ number, number ][] = [
|
|
435
|
-
[ vb.left + cr.x * vb.width, vb.top + cr.y * vb.height ],
|
|
436
|
-
[
|
|
437
|
-
vb.left + ( cr.x + cr.width ) * vb.width,
|
|
438
|
-
vb.top + cr.y * vb.height,
|
|
439
|
-
],
|
|
440
|
-
[
|
|
441
|
-
vb.left + ( cr.x + cr.width ) * vb.width,
|
|
442
|
-
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
443
|
-
],
|
|
444
|
-
[
|
|
445
|
-
vb.left + cr.x * vb.width,
|
|
446
|
-
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
447
|
-
],
|
|
448
|
-
];
|
|
449
|
-
|
|
450
|
-
// Map to world space via inverse camera.
|
|
451
|
-
for ( const corner of stencilCorners ) {
|
|
452
|
-
const w = screenToWorld( camera, {
|
|
453
|
-
x: corner[ 0 ],
|
|
454
|
-
y: corner[ 1 ],
|
|
455
|
-
} );
|
|
456
|
-
expect( w.x ).toBeGreaterThanOrEqual( -0.001 );
|
|
457
|
-
expect( w.x ).toBeLessThanOrEqual( 1.001 );
|
|
458
|
-
expect( w.y ).toBeGreaterThanOrEqual( -0.001 );
|
|
459
|
-
expect( w.y ).toBeLessThanOrEqual( 1.001 );
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const ROTATIONS = [ 0, 15, 30, 45, 60, 75, 90, 135, 180, 270 ];
|
|
464
|
-
const ZOOMS = [ 1, 1.5, 2, 3, 5 ];
|
|
465
|
-
const CROP_RECTS = [
|
|
466
|
-
{ label: 'full', rect: { x: 0, y: 0, width: 1, height: 1 } },
|
|
467
|
-
{
|
|
468
|
-
label: 'centered-small',
|
|
469
|
-
rect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
|
|
470
|
-
},
|
|
471
|
-
{
|
|
472
|
-
label: 'off-center',
|
|
473
|
-
rect: { x: 0.1, y: 0.1, width: 0.3, height: 0.4 },
|
|
474
|
-
},
|
|
475
|
-
];
|
|
476
|
-
|
|
477
|
-
for ( const rotation of ROTATIONS ) {
|
|
478
|
-
for ( const zoom of ZOOMS ) {
|
|
479
|
-
for ( const { label, rect } of CROP_RECTS ) {
|
|
480
|
-
it( `rotation=${ rotation } zoom=${ zoom } crop=${ label }`, () => {
|
|
481
|
-
const state = makeState( {
|
|
482
|
-
rotation,
|
|
483
|
-
zoom,
|
|
484
|
-
cropRect: rect,
|
|
485
|
-
} );
|
|
486
|
-
const restricted = restrictPanZoom( state, IMAGE, rect );
|
|
487
|
-
expect( restricted.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
488
|
-
const restrictedState = makeState( {
|
|
489
|
-
...state,
|
|
490
|
-
pan: restricted.pan,
|
|
491
|
-
zoom: restricted.zoom,
|
|
492
|
-
cropRect: rect,
|
|
493
|
-
} );
|
|
494
|
-
verifyImageCoversCrop( restrictedState, IMAGE );
|
|
495
|
-
} );
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
it( 'holds across 200 random pan/zoom/rotation/crop combinations', () => {
|
|
501
|
-
let passCount = 0;
|
|
502
|
-
for ( let i = 0; i < 200; i++ ) {
|
|
503
|
-
// Deterministic-ish: use i to seed values.
|
|
504
|
-
const rotation =
|
|
505
|
-
ROTATIONS[ i % ROTATIONS.length ] + ( ( i * 7 ) % 15 );
|
|
506
|
-
const zoom = 1 + ( ( i * 13 ) % 40 ) / 10;
|
|
507
|
-
const cropW = 0.2 + ( ( i * 3 ) % 8 ) / 10;
|
|
508
|
-
const cropH = 0.2 + ( ( i * 5 ) % 8 ) / 10;
|
|
509
|
-
const cropX = Math.min( ( ( i * 11 ) % 10 ) / 10, 1 - cropW );
|
|
510
|
-
const cropY = Math.min( ( ( i * 17 ) % 10 ) / 10, 1 - cropH );
|
|
511
|
-
const rect = {
|
|
512
|
-
x: cropX,
|
|
513
|
-
y: cropY,
|
|
514
|
-
width: cropW,
|
|
515
|
-
height: cropH,
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
const panX = ( ( ( i * 19 ) % 20 ) - 10 ) / 10;
|
|
519
|
-
const panY = ( ( ( i * 23 ) % 20 ) - 10 ) / 10;
|
|
520
|
-
|
|
521
|
-
const state = makeState( {
|
|
522
|
-
rotation,
|
|
523
|
-
zoom,
|
|
524
|
-
pan: { x: panX, y: panY },
|
|
525
|
-
cropRect: rect,
|
|
526
|
-
} );
|
|
527
|
-
|
|
528
|
-
const restricted = restrictPanZoom( state, IMAGE, rect );
|
|
529
|
-
const restrictedState = makeState( {
|
|
530
|
-
...state,
|
|
531
|
-
pan: restricted.pan,
|
|
532
|
-
zoom: restricted.zoom,
|
|
533
|
-
cropRect: rect,
|
|
534
|
-
} );
|
|
535
|
-
|
|
536
|
-
verifyImageCoversCrop( restrictedState, IMAGE );
|
|
537
|
-
passCount++;
|
|
538
|
-
}
|
|
539
|
-
expect( passCount ).toBe( 200 );
|
|
540
|
-
} );
|
|
541
|
-
} );
|
|
542
|
-
|
|
543
|
-
describe( 'getSourceRegion', () => {
|
|
544
|
-
it( 'at default state, source region matches full image', () => {
|
|
545
|
-
const state = makeState();
|
|
546
|
-
const region = getSourceRegion( state, IMAGE );
|
|
547
|
-
expect( region.x ).toBeCloseTo( 0, 0 );
|
|
548
|
-
expect( region.y ).toBeCloseTo( 0, 0 );
|
|
549
|
-
expect( region.width ).toBeCloseTo( IMAGE.width, 0 );
|
|
550
|
-
expect( region.height ).toBeCloseTo( IMAGE.height, 0 );
|
|
551
|
-
expect( region.rotation ).toBe( 0 );
|
|
552
|
-
expect( region.zoom ).toBe( 1 );
|
|
553
|
-
} );
|
|
554
|
-
|
|
555
|
-
it( 'at zoom=2 with centered crop, source region is half the image dimensions', () => {
|
|
556
|
-
const state = makeState( { zoom: 2 } );
|
|
557
|
-
const region = getSourceRegion( state, IMAGE );
|
|
558
|
-
expect( region.width ).toBeCloseTo( IMAGE.width / 2, 0 );
|
|
559
|
-
expect( region.height ).toBeCloseTo( IMAGE.height / 2, 0 );
|
|
560
|
-
// Centered: source region center should be at image center.
|
|
561
|
-
expect( region.x + region.width / 2 ).toBeCloseTo( IMAGE.width / 2, 0 );
|
|
562
|
-
expect( region.y + region.height / 2 ).toBeCloseTo(
|
|
563
|
-
IMAGE.height / 2,
|
|
564
|
-
0
|
|
565
|
-
);
|
|
566
|
-
} );
|
|
567
|
-
|
|
568
|
-
it( 'at 90-degree rotation, source region dimensions are swapped', () => {
|
|
569
|
-
const state = makeState( { rotation: 90 } );
|
|
570
|
-
const region = getSourceRegion( state, IMAGE );
|
|
571
|
-
// At 90° the rotated bounding box swaps roles: the crop covers the
|
|
572
|
-
// full visual area, so the visible source width maps from the image
|
|
573
|
-
// height and vice versa. The key invariant is that the region's
|
|
574
|
-
// aspect ratio flips relative to the default.
|
|
575
|
-
const defaultRegion = getSourceRegion( makeState(), IMAGE );
|
|
576
|
-
const defaultAR = defaultRegion.width / defaultRegion.height;
|
|
577
|
-
const rotatedAR = region.width / region.height;
|
|
578
|
-
// Rotated AR should be roughly the inverse of default AR.
|
|
579
|
-
expect( rotatedAR ).toBeCloseTo( 1 / defaultAR, 1 );
|
|
580
|
-
expect( region.rotation ).toBe( 90 );
|
|
581
|
-
} );
|
|
582
|
-
} );
|
|
583
|
-
|
|
584
|
-
describe( 'snapCropRectToSourcePixels', () => {
|
|
585
|
-
const getSourceEdges = ( state: CropperState ) => {
|
|
586
|
-
const region = getSourceRegion( state, IMAGE );
|
|
587
|
-
return {
|
|
588
|
-
left: region.x,
|
|
589
|
-
top: region.y,
|
|
590
|
-
right: region.x + region.width,
|
|
591
|
-
bottom: region.y + region.height,
|
|
592
|
-
};
|
|
593
|
-
};
|
|
594
|
-
|
|
595
|
-
it( 'snaps the selected source-region edges to whole pixels under pan and zoom', () => {
|
|
596
|
-
const state = makeState( {
|
|
597
|
-
zoom: 2.25,
|
|
598
|
-
pan: { x: 0.013, y: -0.017 },
|
|
599
|
-
cropRect: { x: 0.12, y: 0.18, width: 0.33, height: 0.41 },
|
|
600
|
-
} );
|
|
601
|
-
const candidate = { x: 0.123, y: 0.187, width: 0.337, height: 0.419 };
|
|
602
|
-
|
|
603
|
-
const unsnapped = getSourceRegion(
|
|
604
|
-
{ ...state, cropRect: candidate },
|
|
605
|
-
IMAGE
|
|
606
|
-
);
|
|
607
|
-
expect(
|
|
608
|
-
Math.abs(
|
|
609
|
-
unsnapped.x +
|
|
610
|
-
unsnapped.width -
|
|
611
|
-
Math.round( unsnapped.x + unsnapped.width )
|
|
612
|
-
)
|
|
613
|
-
).toBeGreaterThan( 0.01 );
|
|
614
|
-
expect(
|
|
615
|
-
Math.abs(
|
|
616
|
-
unsnapped.y +
|
|
617
|
-
unsnapped.height -
|
|
618
|
-
Math.round( unsnapped.y + unsnapped.height )
|
|
619
|
-
)
|
|
620
|
-
).toBeGreaterThan( 0.01 );
|
|
621
|
-
|
|
622
|
-
const snappedCropRect = snapCropRectToSourcePixels(
|
|
623
|
-
state,
|
|
624
|
-
IMAGE,
|
|
625
|
-
candidate,
|
|
626
|
-
'se'
|
|
627
|
-
);
|
|
628
|
-
|
|
629
|
-
const snapped = getSourceEdges( {
|
|
630
|
-
...state,
|
|
631
|
-
cropRect: snappedCropRect,
|
|
632
|
-
} );
|
|
633
|
-
expect( snapped.left ).toBeCloseTo( unsnapped.x, 3 );
|
|
634
|
-
expect( snapped.top ).toBeCloseTo( unsnapped.y, 3 );
|
|
635
|
-
expect( snapped.right ).toBeCloseTo( Math.round( snapped.right ), 3 );
|
|
636
|
-
expect( snapped.bottom ).toBeCloseTo( Math.round( snapped.bottom ), 3 );
|
|
637
|
-
} );
|
|
638
|
-
|
|
639
|
-
it( 'snaps all source-region edges to whole pixels', () => {
|
|
640
|
-
const state = makeState( {
|
|
641
|
-
rotation: 30,
|
|
642
|
-
zoom: 2.25,
|
|
643
|
-
pan: { x: 0.013, y: -0.017 },
|
|
644
|
-
cropRect: { x: 0.12, y: 0.18, width: 0.33, height: 0.41 },
|
|
645
|
-
} );
|
|
646
|
-
const candidate = { x: 0.123, y: 0.187, width: 0.337, height: 0.419 };
|
|
647
|
-
|
|
648
|
-
const snappedCropRect = snapCropRectToSourcePixelGrid(
|
|
649
|
-
state,
|
|
650
|
-
IMAGE,
|
|
651
|
-
candidate
|
|
652
|
-
);
|
|
653
|
-
const snapped = getSourceEdges( {
|
|
654
|
-
...state,
|
|
655
|
-
cropRect: snappedCropRect,
|
|
656
|
-
} );
|
|
657
|
-
|
|
658
|
-
expect( snapped.left ).toBeCloseTo( Math.round( snapped.left ), 3 );
|
|
659
|
-
expect( snapped.top ).toBeCloseTo( Math.round( snapped.top ), 3 );
|
|
660
|
-
expect( snapped.right ).toBeCloseTo( Math.round( snapped.right ), 3 );
|
|
661
|
-
expect( snapped.bottom ).toBeCloseTo( Math.round( snapped.bottom ), 3 );
|
|
662
|
-
} );
|
|
663
|
-
|
|
664
|
-
it( 'does not snap selected edges while fine rotation couples source axes', () => {
|
|
665
|
-
const state = makeState( {
|
|
666
|
-
rotation: 30,
|
|
667
|
-
zoom: 2.25,
|
|
668
|
-
pan: { x: 0.013, y: -0.017 },
|
|
669
|
-
cropRect: { x: 0.12, y: 0.18, width: 0.33, height: 0.41 },
|
|
670
|
-
} );
|
|
671
|
-
const candidate = { x: 0.123, y: 0.187, width: 0.337, height: 0.419 };
|
|
672
|
-
|
|
673
|
-
expect(
|
|
674
|
-
snapCropRectToSourcePixels( state, IMAGE, candidate, 'e' )
|
|
675
|
-
).toBe( candidate );
|
|
676
|
-
} );
|
|
677
|
-
|
|
678
|
-
it( 'returns the input crop rect when the image dimensions are invalid', () => {
|
|
679
|
-
const cropRect = { x: 0.1, y: 0.2, width: 0.3, height: 0.4 };
|
|
680
|
-
|
|
681
|
-
expect(
|
|
682
|
-
snapCropRectToSourcePixels(
|
|
683
|
-
makeState(),
|
|
684
|
-
{ width: 0, height: 0 },
|
|
685
|
-
cropRect,
|
|
686
|
-
'se'
|
|
687
|
-
)
|
|
688
|
-
).toBe( cropRect );
|
|
689
|
-
} );
|
|
690
|
-
} );
|
|
691
|
-
|
|
692
|
-
describe( 'getSourceRegionPercent', () => {
|
|
693
|
-
it( 'at default state, returns 0/0/100/100', () => {
|
|
694
|
-
const state = makeState();
|
|
695
|
-
const pct = getSourceRegionPercent( state, IMAGE );
|
|
696
|
-
expect( pct.x ).toBeCloseTo( 0, 0 );
|
|
697
|
-
expect( pct.y ).toBeCloseTo( 0, 0 );
|
|
698
|
-
expect( pct.width ).toBeCloseTo( 100, 0 );
|
|
699
|
-
expect( pct.height ).toBeCloseTo( 100, 0 );
|
|
700
|
-
} );
|
|
701
|
-
|
|
702
|
-
it( 'at zoom=2 centered, returns 25/25/50/50', () => {
|
|
703
|
-
const state = makeState( { zoom: 2 } );
|
|
704
|
-
const pct = getSourceRegionPercent( state, IMAGE );
|
|
705
|
-
expect( pct.width ).toBeCloseTo( 50, 0 );
|
|
706
|
-
expect( pct.height ).toBeCloseTo( 50, 0 );
|
|
707
|
-
// Centered: x and y should each be 25%.
|
|
708
|
-
expect( pct.x ).toBeCloseTo( 25, 0 );
|
|
709
|
-
expect( pct.y ).toBeCloseTo( 25, 0 );
|
|
710
|
-
} );
|
|
711
|
-
|
|
712
|
-
it( 'percentages sum correctly (x + width ≤ 100, y + height ≤ 100)', () => {
|
|
713
|
-
const state = makeState( { zoom: 3, pan: { x: 0.1, y: -0.05 } } );
|
|
714
|
-
const pct = getSourceRegionPercent( state, IMAGE );
|
|
715
|
-
expect( pct.x + pct.width ).toBeLessThanOrEqual( 100.01 );
|
|
716
|
-
expect( pct.y + pct.height ).toBeLessThanOrEqual( 100.01 );
|
|
717
|
-
expect( pct.x ).toBeGreaterThanOrEqual( -0.01 );
|
|
718
|
-
expect( pct.y ).toBeGreaterThanOrEqual( -0.01 );
|
|
719
|
-
} );
|
|
720
|
-
|
|
721
|
-
it( 'matches getSourceRegion divided by image dimensions', () => {
|
|
722
|
-
const state = makeState( {
|
|
723
|
-
zoom: 1.5,
|
|
724
|
-
pan: { x: 0.05, y: -0.02 },
|
|
725
|
-
rotation: 15,
|
|
726
|
-
} );
|
|
727
|
-
const region = getSourceRegion( state, IMAGE );
|
|
728
|
-
const pct = getSourceRegionPercent( state, IMAGE );
|
|
729
|
-
expect( pct.x ).toBeCloseTo( ( region.x / IMAGE.width ) * 100, 5 );
|
|
730
|
-
expect( pct.y ).toBeCloseTo( ( region.y / IMAGE.height ) * 100, 5 );
|
|
731
|
-
expect( pct.width ).toBeCloseTo(
|
|
732
|
-
( region.width / IMAGE.width ) * 100,
|
|
733
|
-
5
|
|
734
|
-
);
|
|
735
|
-
expect( pct.height ).toBeCloseTo(
|
|
736
|
-
( region.height / IMAGE.height ) * 100,
|
|
737
|
-
5
|
|
738
|
-
);
|
|
739
|
-
} );
|
|
740
|
-
|
|
741
|
-
it( 'returns zeros for zero-dimension image', () => {
|
|
742
|
-
const state = makeState();
|
|
743
|
-
const pct = getSourceRegionPercent( state, {
|
|
744
|
-
width: 0,
|
|
745
|
-
height: 0,
|
|
746
|
-
} );
|
|
747
|
-
expect( pct.x ).toBe( 0 );
|
|
748
|
-
expect( pct.y ).toBe( 0 );
|
|
749
|
-
expect( pct.width ).toBe( 0 );
|
|
750
|
-
expect( pct.height ).toBe( 0 );
|
|
751
|
-
} );
|
|
752
|
-
|
|
753
|
-
it( 'with small centered crop rect, percentages reflect visible portion', () => {
|
|
754
|
-
// 50% crop rect centered → at zoom=1, the crop covers half the
|
|
755
|
-
// visual area. The percentage region should be roughly 50% of
|
|
756
|
-
// the image in each axis.
|
|
757
|
-
const state = makeState( {
|
|
758
|
-
cropRect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
|
|
759
|
-
zoom: 1,
|
|
760
|
-
} );
|
|
761
|
-
const pct = getSourceRegionPercent( state, IMAGE );
|
|
762
|
-
expect( pct.width ).toBeCloseTo( 50, 0 );
|
|
763
|
-
expect( pct.height ).toBeCloseTo( 50, 0 );
|
|
764
|
-
} );
|
|
765
|
-
} );
|
|
766
|
-
|
|
767
|
-
describe( 'getImageCropBounds', () => {
|
|
768
|
-
it( 'returns the image AABB — handles can reach the image edge regardless of container size', () => {
|
|
769
|
-
// 500×375 (4:3) in a 600×400 container (3:2).
|
|
770
|
-
// At zoom=1.75 the image overflows the container. Bounds should reflect
|
|
771
|
-
// the image footprint, not the container edge, so one drag can reach the
|
|
772
|
-
// image edge (viewport pan reveals content outside the canvas boundary).
|
|
773
|
-
const nat: Size = { width: 500, height: 375 };
|
|
774
|
-
const container: Size = { width: 600, height: 400 };
|
|
775
|
-
const { elementSize, visualSize } = getImageFit( container, nat, 0 );
|
|
776
|
-
|
|
777
|
-
expect( visualSize.width ).toBeLessThan( container.width );
|
|
778
|
-
|
|
779
|
-
const state = makeState( {
|
|
780
|
-
image: {
|
|
781
|
-
src: 'test.jpg',
|
|
782
|
-
naturalWidth: nat.width,
|
|
783
|
-
naturalHeight: nat.height,
|
|
784
|
-
},
|
|
785
|
-
zoom: 1.75,
|
|
786
|
-
rotation: 0,
|
|
787
|
-
} );
|
|
788
|
-
|
|
789
|
-
const bounds = getImageCropBounds( state, elementSize, visualSize );
|
|
790
|
-
|
|
791
|
-
// At zoom=1.75 the image extends well beyond the container on all sides.
|
|
792
|
-
// Bounds should be the image AABB corners (≈ ±0.375), not the container
|
|
793
|
-
// edges (which would be ≈ ±0.0625).
|
|
794
|
-
expect( bounds.minX ).toBeLessThan( -0.3 );
|
|
795
|
-
expect( bounds.maxX ).toBeGreaterThan( 1.3 );
|
|
796
|
-
expect( bounds.minY ).toBeLessThan( -0.3 );
|
|
797
|
-
expect( bounds.maxY ).toBeGreaterThan( 1.3 );
|
|
798
|
-
} );
|
|
799
|
-
} );
|
|
800
|
-
|
|
801
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
802
|
-
// Non-finite input regressions.
|
|
803
|
-
//
|
|
804
|
-
// The reducer normalizes rotation/zoom/pan on every action, so these paths
|
|
805
|
-
// aren't user-reachable today. But programmatic callers and deserialized
|
|
806
|
-
// state could still feed corrupted values, which used to silently propagate
|
|
807
|
-
// NaN/Infinity into the crop rect, camera matrix, or REST /edit payload.
|
|
808
|
-
// These tests pin the defense-in-depth guards in place.
|
|
809
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
810
|
-
|
|
811
|
-
const HOSTILE_STATE: CropperState = makeState( {
|
|
812
|
-
pan: { x: Number.NaN, y: Number.NEGATIVE_INFINITY },
|
|
813
|
-
zoom: Number.POSITIVE_INFINITY,
|
|
814
|
-
rotation: Number.NaN,
|
|
815
|
-
} );
|
|
816
|
-
|
|
817
|
-
describe( 'getRotatedBBox — non-finite input regression', () => {
|
|
818
|
-
it.each( [
|
|
819
|
-
[ 'NaN width', Number.NaN, 100, 30 ],
|
|
820
|
-
[ '±Infinity height', 100, Number.POSITIVE_INFINITY, 30 ],
|
|
821
|
-
[ 'NaN rotation', 100, 100, Number.NaN ],
|
|
822
|
-
[ '-Infinity rotation', 100, 100, Number.NEGATIVE_INFINITY ],
|
|
823
|
-
// MAX_VALUE is finite but degreesToRadians( MAX_VALUE ) overflows
|
|
824
|
-
// to Infinity, then Math.cos/sin return NaN. The magnitude bound
|
|
825
|
-
// in `isSafeNumber` catches this before the trig.
|
|
826
|
-
[ 'MAX_VALUE rotation', 100, 100, Number.MAX_VALUE ],
|
|
827
|
-
] )( 'returns {0, 0} for %s', ( _label, w, h, rot ) => {
|
|
828
|
-
expect( getRotatedBBox( w, h, rot ) ).toEqual( {
|
|
829
|
-
width: 0,
|
|
830
|
-
height: 0,
|
|
831
|
-
} );
|
|
832
|
-
} );
|
|
833
|
-
|
|
834
|
-
it( 'still returns correct dimensions for normal inputs', () => {
|
|
835
|
-
// Sanity check that the guard doesn't break the happy path.
|
|
836
|
-
const out = getRotatedBBox( 200, 100, 90 );
|
|
837
|
-
expect( out.width ).toBeCloseTo( 100, 5 );
|
|
838
|
-
expect( out.height ).toBeCloseTo( 200, 5 );
|
|
839
|
-
} );
|
|
840
|
-
} );
|
|
841
|
-
|
|
842
|
-
describe( 'getImageFit — non-finite input regression', () => {
|
|
843
|
-
it( 'returns finite sizes when rotation is NaN (treated as 0)', () => {
|
|
844
|
-
const out = getImageFit( CONTAINER, IMAGE, Number.NaN );
|
|
845
|
-
expect( Number.isFinite( out.elementSize.width ) ).toBe( true );
|
|
846
|
-
expect( Number.isFinite( out.elementSize.height ) ).toBe( true );
|
|
847
|
-
expect( Number.isFinite( out.visualSize.width ) ).toBe( true );
|
|
848
|
-
expect( Number.isFinite( out.visualSize.height ) ).toBe( true );
|
|
849
|
-
} );
|
|
850
|
-
|
|
851
|
-
it( 'returns zero sizes when containerSize has a non-finite dimension', () => {
|
|
852
|
-
const out = getImageFit( { width: Number.NaN, height: 600 }, IMAGE, 0 );
|
|
853
|
-
expect( out.elementSize ).toEqual( { width: 0, height: 0 } );
|
|
854
|
-
expect( out.visualSize ).toEqual( { width: 0, height: 0 } );
|
|
855
|
-
} );
|
|
856
|
-
|
|
857
|
-
it( 'returns zero sizes when imageSize has a non-finite dimension', () => {
|
|
858
|
-
const out = getImageFit(
|
|
859
|
-
CONTAINER,
|
|
860
|
-
{ width: Number.POSITIVE_INFINITY, height: 900 },
|
|
861
|
-
0
|
|
862
|
-
);
|
|
863
|
-
expect( out.elementSize ).toEqual( { width: 0, height: 0 } );
|
|
864
|
-
expect( out.visualSize ).toEqual( { width: 0, height: 0 } );
|
|
865
|
-
} );
|
|
866
|
-
} );
|
|
867
|
-
|
|
868
|
-
describe( 'createCamera — non-finite input regression', () => {
|
|
869
|
-
it( 'returns a matrix with all finite entries even for hostile state', () => {
|
|
870
|
-
const m = createCamera( HOSTILE_STATE, CONTAINER, IMAGE );
|
|
871
|
-
expect( m ).toHaveLength( 6 );
|
|
872
|
-
for ( let i = 0; i < 6; i++ ) {
|
|
873
|
-
expect( Number.isFinite( m[ i ] ) ).toBe( true );
|
|
874
|
-
}
|
|
875
|
-
} );
|
|
876
|
-
|
|
877
|
-
it( 'returns identity matrix when imageSize has non-finite dimensions', () => {
|
|
878
|
-
const m = createCamera( makeState(), CONTAINER, {
|
|
879
|
-
width: Number.NaN,
|
|
880
|
-
height: 900,
|
|
881
|
-
} );
|
|
882
|
-
// mat2d.create() returns [1, 0, 0, 1, 0, 0] (identity).
|
|
883
|
-
expect( Array.from( m ) ).toEqual( [ 1, 0, 0, 1, 0, 0 ] );
|
|
884
|
-
} );
|
|
885
|
-
} );
|
|
886
|
-
|
|
887
|
-
describe( 'createExportCamera — non-finite input regression', () => {
|
|
888
|
-
it( 'returns a matrix with all finite entries even for hostile state', () => {
|
|
889
|
-
// This is the matrix passed to ctx.setTransform → ctx.drawImage at
|
|
890
|
-
// export time, so NaN here would corrupt the saved file.
|
|
891
|
-
const m = createExportCamera( HOSTILE_STATE, IMAGE, {
|
|
892
|
-
width: 600,
|
|
893
|
-
height: 400,
|
|
894
|
-
} );
|
|
895
|
-
for ( let i = 0; i < 6; i++ ) {
|
|
896
|
-
expect( Number.isFinite( m[ i ] ) ).toBe( true );
|
|
897
|
-
}
|
|
898
|
-
} );
|
|
899
|
-
|
|
900
|
-
it( 'returns identity matrix when outputSize has non-finite dimensions', () => {
|
|
901
|
-
const m = createExportCamera( makeState(), IMAGE, {
|
|
902
|
-
width: Number.POSITIVE_INFINITY,
|
|
903
|
-
height: 400,
|
|
904
|
-
} );
|
|
905
|
-
expect( Array.from( m ) ).toEqual( [ 1, 0, 0, 1, 0, 0 ] );
|
|
906
|
-
} );
|
|
907
|
-
} );
|
|
908
|
-
|
|
909
|
-
describe( 'restrictCropRect — non-finite input regression', () => {
|
|
910
|
-
const cropRect = { x: 0.1, y: 0.1, width: 0.5, height: 0.5 };
|
|
911
|
-
|
|
912
|
-
it.each( [
|
|
913
|
-
[ 'NaN zoom', cropRect, Number.NaN, 0, 16 / 9 ],
|
|
914
|
-
[ '-Infinity rotation', cropRect, 1, Number.NEGATIVE_INFINITY, 16 / 9 ],
|
|
915
|
-
[ 'NaN aspectRatio', cropRect, 1, 0, Number.NaN ],
|
|
916
|
-
[
|
|
917
|
-
'MAX_VALUE zoom (overflow guard)',
|
|
918
|
-
cropRect,
|
|
919
|
-
Number.MAX_VALUE,
|
|
920
|
-
0,
|
|
921
|
-
16 / 9,
|
|
922
|
-
],
|
|
923
|
-
] )( 'returns a finite rect for %s', ( _label, rect, z, r, a ) => {
|
|
924
|
-
const out = restrictCropRect( rect, z, r, a );
|
|
925
|
-
expect( Number.isFinite( out.x ) ).toBe( true );
|
|
926
|
-
expect( Number.isFinite( out.y ) ).toBe( true );
|
|
927
|
-
expect( Number.isFinite( out.width ) ).toBe( true );
|
|
928
|
-
expect( Number.isFinite( out.height ) ).toBe( true );
|
|
929
|
-
} );
|
|
930
|
-
|
|
931
|
-
it( 'sanitizes cropRect fields on the no-resize (fit-through) path', () => {
|
|
932
|
-
// When the crop already fits (t >= 1 - EPSILON) the function used to
|
|
933
|
-
// return the raw cropRect, letting non-finite fields slip through.
|
|
934
|
-
const hostileRect = {
|
|
935
|
-
x: Number.NaN,
|
|
936
|
-
y: 0.1,
|
|
937
|
-
width: 0.3,
|
|
938
|
-
height: 0.3,
|
|
939
|
-
};
|
|
940
|
-
const out = restrictCropRect( hostileRect, 10, 0, 16 / 9 );
|
|
941
|
-
expect( Number.isFinite( out.x ) ).toBe( true );
|
|
942
|
-
expect( out.x ).toBe( 0 );
|
|
943
|
-
} );
|
|
944
|
-
} );
|
|
945
|
-
|
|
946
|
-
describe( 'restrictPanZoom — non-finite input regression', () => {
|
|
947
|
-
const cropRect = { x: 0.1, y: 0.1, width: 0.5, height: 0.5 };
|
|
948
|
-
|
|
949
|
-
it( 'returns finite pan and zoom for hostile state', () => {
|
|
950
|
-
const out = restrictPanZoom( HOSTILE_STATE, IMAGE, cropRect );
|
|
951
|
-
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
952
|
-
expect( Number.isFinite( out.pan.x ) ).toBe( true );
|
|
953
|
-
expect( Number.isFinite( out.pan.y ) ).toBe( true );
|
|
954
|
-
} );
|
|
955
|
-
|
|
956
|
-
it( 'returns finite pan and zoom when only state.zoom is corrupted', () => {
|
|
957
|
-
const state = makeState( { zoom: Number.NaN } );
|
|
958
|
-
const out = restrictPanZoom( state, IMAGE, cropRect );
|
|
959
|
-
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
960
|
-
expect( out.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
961
|
-
} );
|
|
962
|
-
|
|
963
|
-
it( 'sanitizes the cropRect argument before feeding it into the math', () => {
|
|
964
|
-
// A NaN cropRect.width would drive getMinZoomForCover to NaN via
|
|
965
|
-
// Math.max(1, NaN, NaN), and that NaN would be returned as zoom.
|
|
966
|
-
const hostileRect = {
|
|
967
|
-
x: 0.1,
|
|
968
|
-
y: 0.1,
|
|
969
|
-
width: Number.NaN,
|
|
970
|
-
height: 0.5,
|
|
971
|
-
};
|
|
972
|
-
const out = restrictPanZoom( makeState(), IMAGE, hostileRect );
|
|
973
|
-
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
974
|
-
expect( Number.isFinite( out.pan.x ) ).toBe( true );
|
|
975
|
-
expect( Number.isFinite( out.pan.y ) ).toBe( true );
|
|
976
|
-
} );
|
|
977
|
-
|
|
978
|
-
it( 'returns finite zoom when imageSize is Infinity (aspectRatio NaN guard)', () => {
|
|
979
|
-
// Infinity / Infinity = NaN, which used to leak as `zoom: NaN`
|
|
980
|
-
// through the no-correction early return.
|
|
981
|
-
const rect = { x: 0.1, y: 0.1, width: 0.5, height: 0.5 };
|
|
982
|
-
const out = restrictPanZoom(
|
|
983
|
-
makeState(),
|
|
984
|
-
{ width: Infinity, height: Infinity },
|
|
985
|
-
rect
|
|
986
|
-
);
|
|
987
|
-
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
988
|
-
expect( Number.isFinite( out.pan.x ) ).toBe( true );
|
|
989
|
-
expect( Number.isFinite( out.pan.y ) ).toBe( true );
|
|
990
|
-
} );
|
|
991
|
-
} );
|
|
992
|
-
|
|
993
|
-
describe( 'computeTransformStyle — non-finite input regression', () => {
|
|
994
|
-
it( 'produces a finite matrix string for hostile state', () => {
|
|
995
|
-
// CSS preview path must agree with createCamera under hostile state
|
|
996
|
-
// (both should produce safe output, not divergent NaN vs finite).
|
|
997
|
-
const out = computeTransformStyle( HOSTILE_STATE, IMAGE );
|
|
998
|
-
expect( out ).toMatch( /^matrix\(/ );
|
|
999
|
-
expect( out ).not.toMatch( /NaN/ );
|
|
1000
|
-
expect( out ).not.toMatch( /Infinity/ );
|
|
1001
|
-
} );
|
|
1002
|
-
|
|
1003
|
-
it( 'returns the identity matrix when imageSize is hostile', () => {
|
|
1004
|
-
// State sanitization only covers state fields; imageSize NaN/Infinity
|
|
1005
|
-
// would still emit `matrix(..., NaN, NaN)` in the translate components
|
|
1006
|
-
// without this explicit guard.
|
|
1007
|
-
expect(
|
|
1008
|
-
computeTransformStyle( makeState(), {
|
|
1009
|
-
width: Number.NaN,
|
|
1010
|
-
height: 900,
|
|
1011
|
-
} )
|
|
1012
|
-
).toBe( 'matrix(1, 0, 0, 1, 0, 0)' );
|
|
1013
|
-
expect(
|
|
1014
|
-
computeTransformStyle( makeState(), {
|
|
1015
|
-
width: Infinity,
|
|
1016
|
-
height: Infinity,
|
|
1017
|
-
} )
|
|
1018
|
-
).toBe( 'matrix(1, 0, 0, 1, 0, 0)' );
|
|
1019
|
-
} );
|
|
1020
|
-
} );
|
|
1021
|
-
|
|
1022
|
-
describe( 'getImageCropBounds — non-finite input regression', () => {
|
|
1023
|
-
it( 'returns finite bounds for hostile state', () => {
|
|
1024
|
-
const out = getImageCropBounds(
|
|
1025
|
-
HOSTILE_STATE,
|
|
1026
|
-
{ width: 800, height: 450 },
|
|
1027
|
-
{ width: 800, height: 450 }
|
|
1028
|
-
);
|
|
1029
|
-
expect( Number.isFinite( out.minX ) ).toBe( true );
|
|
1030
|
-
expect( Number.isFinite( out.minY ) ).toBe( true );
|
|
1031
|
-
expect( Number.isFinite( out.maxX ) ).toBe( true );
|
|
1032
|
-
expect( Number.isFinite( out.maxY ) ).toBe( true );
|
|
1033
|
-
expect( out.minX ).toBeLessThanOrEqual( out.maxX );
|
|
1034
|
-
expect( out.minY ).toBeLessThanOrEqual( out.maxY );
|
|
1035
|
-
} );
|
|
1036
|
-
} );
|
|
1037
|
-
|
|
1038
|
-
describe( 'getSourceRegion — non-finite input regression', () => {
|
|
1039
|
-
it( 'returns a finite source region for hostile state', () => {
|
|
1040
|
-
// getSourceRegion feeds the REST /edit endpoint, so NaN here would
|
|
1041
|
-
// reach the server.
|
|
1042
|
-
const out = getSourceRegion( HOSTILE_STATE, IMAGE );
|
|
1043
|
-
expect( Number.isFinite( out.x ) ).toBe( true );
|
|
1044
|
-
expect( Number.isFinite( out.y ) ).toBe( true );
|
|
1045
|
-
expect( Number.isFinite( out.width ) ).toBe( true );
|
|
1046
|
-
expect( Number.isFinite( out.height ) ).toBe( true );
|
|
1047
|
-
} );
|
|
1048
|
-
|
|
1049
|
-
it( 'returns finite metadata on the zero-size early-return path', () => {
|
|
1050
|
-
// When imageSize is zero/invalid, the function returns a zero region
|
|
1051
|
-
// plus rotation/flip/zoom metadata. Hostile state must not leak NaN
|
|
1052
|
-
// out through those metadata fields.
|
|
1053
|
-
const out = getSourceRegion( HOSTILE_STATE, {
|
|
1054
|
-
width: 0,
|
|
1055
|
-
height: 0,
|
|
1056
|
-
} );
|
|
1057
|
-
expect( Number.isFinite( out.rotation ) ).toBe( true );
|
|
1058
|
-
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
1059
|
-
expect( out.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
1060
|
-
} );
|
|
1061
|
-
} );
|
|
1062
|
-
|
|
1063
|
-
describe( 'getViewScale — presentational magnification to fill the canvas', () => {
|
|
1064
|
-
// A tall image (1400x2500) fit into a short, wide canvas (1000x500):
|
|
1065
|
-
// contain fit = min(1000/1400, 500/2500) = 0.2, so the rendered footprint
|
|
1066
|
-
// is 280x500 — it fills the canvas height but only 280 of 1000 px wide.
|
|
1067
|
-
const CANVAS: Size = { width: 1000, height: 500 };
|
|
1068
|
-
const FOOTPRINT: Size = { width: 280, height: 500 };
|
|
1069
|
-
const TARGET = 0.8;
|
|
1070
|
-
const MAX = 8;
|
|
1071
|
-
|
|
1072
|
-
it( 'returns 1 when the crop already fills at least the target on its binding axis', () => {
|
|
1073
|
-
// Full-footprint crop is 280x500 on screen: 500 == canvas height, well
|
|
1074
|
-
// above 0.8 of it. No magnification needed.
|
|
1075
|
-
const full = { x: 0, y: 0, width: 1, height: 1 };
|
|
1076
|
-
expect( getViewScale( full, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
|
|
1077
|
-
1
|
|
1078
|
-
);
|
|
1079
|
-
} );
|
|
1080
|
-
|
|
1081
|
-
it( 'magnifies an under-filling crop so its binding axis reaches target * canvas', () => {
|
|
1082
|
-
// A square crop in the 280x500 footprint: width fills (cropRect.width=1
|
|
1083
|
-
// -> 280px), height is 280/500 = 0.56 (-> 280px). On-screen 280x280.
|
|
1084
|
-
// kW = 0.8*1000/280 = 2.857, kH = 0.8*500/280 = 1.4286, k = min = 1.4286.
|
|
1085
|
-
const square = { x: 0, y: 0.22, width: 1, height: 280 / 500 };
|
|
1086
|
-
const scale = getViewScale( square, CANVAS, FOOTPRINT, TARGET, MAX );
|
|
1087
|
-
expect( scale ).toBeCloseTo( ( TARGET * 500 ) / 280, 5 );
|
|
1088
|
-
// After magnification the binding (height) axis reaches 0.8 * canvas.
|
|
1089
|
-
expect( square.height * FOOTPRINT.height * scale ).toBeCloseTo(
|
|
1090
|
-
TARGET * CANVAS.height,
|
|
1091
|
-
5
|
|
1092
|
-
);
|
|
1093
|
-
} );
|
|
1094
|
-
|
|
1095
|
-
it( 'clamps magnification at maxScale for a near-degenerate crop', () => {
|
|
1096
|
-
// Tiny crop would need an enormous scale to fill; cap it.
|
|
1097
|
-
const tiny = { x: 0.5, y: 0.5, width: 0.02, height: 0.02 };
|
|
1098
|
-
expect( getViewScale( tiny, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
|
|
1099
|
-
MAX
|
|
1100
|
-
);
|
|
1101
|
-
} );
|
|
1102
|
-
|
|
1103
|
-
it( 'never shrinks below 1 even when the crop overfills the target', () => {
|
|
1104
|
-
const full = { x: 0, y: 0, width: 1, height: 1 };
|
|
1105
|
-
expect( getViewScale( full, CANVAS, FOOTPRINT, 0.5, MAX ) ).toBe( 1 );
|
|
1106
|
-
} );
|
|
1107
|
-
|
|
1108
|
-
it( 'returns 1 for zero or degenerate inputs', () => {
|
|
1109
|
-
const full = { x: 0, y: 0, width: 1, height: 1 };
|
|
1110
|
-
const zeroRect = { x: 0, y: 0, width: 0, height: 0 };
|
|
1111
|
-
expect( getViewScale( zeroRect, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
|
|
1112
|
-
1
|
|
1113
|
-
);
|
|
1114
|
-
expect(
|
|
1115
|
-
getViewScale(
|
|
1116
|
-
full,
|
|
1117
|
-
{ width: 0, height: 0 },
|
|
1118
|
-
FOOTPRINT,
|
|
1119
|
-
TARGET,
|
|
1120
|
-
MAX
|
|
1121
|
-
)
|
|
1122
|
-
).toBe( 1 );
|
|
1123
|
-
expect(
|
|
1124
|
-
getViewScale( full, CANVAS, { width: 0, height: 0 }, TARGET, MAX )
|
|
1125
|
-
).toBe( 1 );
|
|
1126
|
-
} );
|
|
1127
|
-
|
|
1128
|
-
it( 'returns 1 (never NaN) for non-finite inputs', () => {
|
|
1129
|
-
const full = { x: 0, y: 0, width: 1, height: 1 };
|
|
1130
|
-
const nanRect = { x: 0, y: 0, width: NaN, height: 1 };
|
|
1131
|
-
expect( getViewScale( nanRect, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
|
|
1132
|
-
1
|
|
1133
|
-
);
|
|
1134
|
-
expect(
|
|
1135
|
-
getViewScale(
|
|
1136
|
-
full,
|
|
1137
|
-
{ width: NaN, height: 500 },
|
|
1138
|
-
FOOTPRINT,
|
|
1139
|
-
TARGET,
|
|
1140
|
-
MAX
|
|
1141
|
-
)
|
|
1142
|
-
).toBe( 1 );
|
|
1143
|
-
expect(
|
|
1144
|
-
getViewScale(
|
|
1145
|
-
full,
|
|
1146
|
-
CANVAS,
|
|
1147
|
-
{ width: Infinity, height: 500 },
|
|
1148
|
-
TARGET,
|
|
1149
|
-
MAX
|
|
1150
|
-
)
|
|
1151
|
-
).toBe( 1 );
|
|
1152
|
-
expect( getViewScale( full, CANVAS, FOOTPRINT, NaN, MAX ) ).toBe( 1 );
|
|
1153
|
-
expect( getViewScale( full, CANVAS, FOOTPRINT, TARGET, NaN ) ).toBe(
|
|
1154
|
-
1
|
|
1155
|
-
);
|
|
1156
|
-
} );
|
|
1157
|
-
} );
|
|
1158
|
-
|
|
1159
|
-
describe( 'getViewScale — property invariants', () => {
|
|
1160
|
-
const ITERATIONS = 100;
|
|
1161
|
-
|
|
1162
|
-
it( `holds across ${ ITERATIONS } random valid inputs`, () => {
|
|
1163
|
-
for ( let i = 0; i < ITERATIONS; i++ ) {
|
|
1164
|
-
const canvasW = 200 + ( ( i * 37 ) % 1800 );
|
|
1165
|
-
const canvasH = 200 + ( ( i * 53 ) % 1200 );
|
|
1166
|
-
const footprintW = 50 + ( ( i * 19 ) % 800 );
|
|
1167
|
-
const footprintH = 50 + ( ( i * 29 ) % 1000 );
|
|
1168
|
-
const cropW = 0.05 + ( ( i * 3 ) % 95 ) / 100;
|
|
1169
|
-
const cropH = 0.05 + ( ( i * 5 ) % 95 ) / 100;
|
|
1170
|
-
const cropX = Math.min( ( ( i * 11 ) % 100 ) / 100, 1 - cropW );
|
|
1171
|
-
const cropY = Math.min( ( ( i * 17 ) % 100 ) / 100, 1 - cropH );
|
|
1172
|
-
const targetFill = 0.1 + ( ( i * 7 ) % 90 ) / 100;
|
|
1173
|
-
const maxScale = 1 + ( ( i * 13 ) % 20 );
|
|
1174
|
-
|
|
1175
|
-
const canvas: Size = { width: canvasW, height: canvasH };
|
|
1176
|
-
const footprint: Size = { width: footprintW, height: footprintH };
|
|
1177
|
-
const cropRect = {
|
|
1178
|
-
x: cropX,
|
|
1179
|
-
y: cropY,
|
|
1180
|
-
width: cropW,
|
|
1181
|
-
height: cropH,
|
|
1182
|
-
};
|
|
1183
|
-
|
|
1184
|
-
const scale = getViewScale(
|
|
1185
|
-
cropRect,
|
|
1186
|
-
canvas,
|
|
1187
|
-
footprint,
|
|
1188
|
-
targetFill,
|
|
1189
|
-
maxScale
|
|
1190
|
-
);
|
|
1191
|
-
|
|
1192
|
-
expect( Number.isFinite( scale ) ).toBe( true );
|
|
1193
|
-
expect( scale ).toBeGreaterThanOrEqual( 1 );
|
|
1194
|
-
expect( scale ).toBeLessThanOrEqual( maxScale );
|
|
1195
|
-
|
|
1196
|
-
const cropScreenW = cropRect.width * footprintW;
|
|
1197
|
-
const cropScreenH = cropRect.height * footprintH;
|
|
1198
|
-
const kW = ( targetFill * canvasW ) / cropScreenW;
|
|
1199
|
-
const kH = ( targetFill * canvasH ) / cropScreenH;
|
|
1200
|
-
const k = Math.min( kW, kH );
|
|
1201
|
-
const unclamped = Math.max( 1, k );
|
|
1202
|
-
|
|
1203
|
-
expect( scale ).toBeCloseTo( Math.min( maxScale, unclamped ), 10 );
|
|
1204
|
-
|
|
1205
|
-
const shouldCheckFill = scale > 1 && scale < maxScale - 1e-10;
|
|
1206
|
-
const bindingIsWidth = kW <= kH + 1e-10;
|
|
1207
|
-
const magnifiedBinding = bindingIsWidth
|
|
1208
|
-
? cropScreenW * scale
|
|
1209
|
-
: cropScreenH * scale;
|
|
1210
|
-
const targetBinding = bindingIsWidth
|
|
1211
|
-
? targetFill * canvasW
|
|
1212
|
-
: targetFill * canvasH;
|
|
1213
|
-
expect(
|
|
1214
|
-
shouldCheckFill ? magnifiedBinding / targetBinding : 1
|
|
1215
|
-
).toBeCloseTo( 1, shouldCheckFill ? 4 : 10 );
|
|
1216
|
-
}
|
|
1217
|
-
} );
|
|
1218
|
-
} );
|