@wordpress/media-editor 0.21.0 → 0.22.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 +12 -0
- package/build/components/media-editor/index.cjs +533 -686
- package/build/components/media-editor/index.cjs.map +1 -7
- package/build/components/media-editor/use-crop-options.cjs +43 -57
- package/build/components/media-editor/use-crop-options.cjs.map +1 -7
- package/build/components/media-editor/use-save-media-editor.cjs +111 -185
- package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
- package/build/components/media-editor-canvas/index.cjs +122 -124
- package/build/components/media-editor-canvas/index.cjs.map +1 -7
- package/build/components/media-editor-crop-panel/index.cjs +65 -72
- package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
- package/build/components/media-editor-fine-rotation/index.cjs +62 -72
- package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
- package/build/components/media-editor-image-controls/index.cjs +183 -257
- package/build/components/media-editor-image-controls/index.cjs.map +1 -7
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +119 -157
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -7
- package/build/components/media-editor-modal/build-modifiers.cjs +107 -88
- package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -7
- package/build/components/media-editor-modal/index.cjs +112 -155
- package/build/components/media-editor-modal/index.cjs.map +1 -7
- package/build/components/media-editor-provider/index.cjs +32 -55
- package/build/components/media-editor-provider/index.cjs.map +1 -7
- package/build/components/media-form/index.cjs +76 -77
- package/build/components/media-form/index.cjs.map +1 -7
- package/build/components/media-preview/index.cjs +114 -114
- package/build/components/media-preview/index.cjs.map +1 -7
- package/build/components/rotation-ruler/index.cjs +181 -188
- package/build/components/rotation-ruler/index.cjs.map +1 -7
- package/build/components/rotation-ruler/use-ruler-drag.cjs +132 -149
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -7
- package/build/hooks/use-crop-gesture-handlers.cjs +82 -76
- package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -7
- package/build/image-editor/core/camera.cjs +257 -205
- package/build/image-editor/core/camera.cjs.map +1 -7
- package/build/image-editor/core/constants.cjs +127 -102
- package/build/image-editor/core/constants.cjs.map +1 -7
- package/build/image-editor/core/containment.cjs +275 -262
- package/build/image-editor/core/containment.cjs.map +1 -7
- package/build/image-editor/core/crop-rect.cjs +33 -44
- package/build/image-editor/core/crop-rect.cjs.map +1 -7
- package/build/image-editor/core/export/canvas-renderer.cjs +165 -135
- package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -7
- package/build/image-editor/core/index.cjs +88 -61
- package/build/image-editor/core/interaction-controller.cjs +583 -646
- package/build/image-editor/core/interaction-controller.cjs.map +1 -7
- package/build/image-editor/core/math/rotation.cjs +26 -35
- package/build/image-editor/core/math/rotation.cjs.map +1 -7
- package/build/image-editor/core/math/sanitize.cjs +124 -80
- package/build/image-editor/core/math/sanitize.cjs.map +1 -7
- package/build/image-editor/core/setter-helpers.cjs +57 -60
- package/build/image-editor/core/setter-helpers.cjs.map +1 -7
- package/build/image-editor/core/source-region.cjs +220 -190
- package/build/image-editor/core/source-region.cjs.map +1 -7
- package/build/image-editor/core/state.cjs +278 -265
- package/build/image-editor/core/state.cjs.map +1 -7
- package/build/image-editor/core/stencil-math.cjs +193 -206
- package/build/image-editor/core/stencil-math.cjs.map +1 -7
- package/build/image-editor/core/transform-style.cjs +39 -48
- package/build/image-editor/core/transform-style.cjs.map +1 -7
- package/build/image-editor/core/transforms/pipeline.cjs +36 -38
- package/build/image-editor/core/transforms/pipeline.cjs.map +1 -7
- package/build/image-editor/core/types.cjs +4 -18
- package/build/image-editor/core/viewport-state.cjs +31 -45
- package/build/image-editor/core/viewport-state.cjs.map +1 -7
- package/build/image-editor/index.cjs +65 -64
- package/build/image-editor/react/components/cropper-provider.cjs +44 -45
- package/build/image-editor/react/components/cropper-provider.cjs.map +1 -7
- package/build/image-editor/react/components/cropper.cjs +583 -710
- package/build/image-editor/react/components/cropper.cjs.map +1 -7
- package/build/image-editor/react/components/index.cjs +22 -33
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +135 -160
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -7
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +28 -59
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +1 -7
- package/build/image-editor/react/components/overlays/grid-overlay.cjs +58 -88
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +1 -7
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +361 -420
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +1 -7
- package/build/image-editor/react/components/viewport-provider.cjs +21 -46
- package/build/image-editor/react/components/viewport-provider.cjs.map +1 -7
- package/build/image-editor/react/hooks/build-cropper-setters.cjs +76 -67
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -7
- package/build/image-editor/react/hooks/index.cjs +16 -31
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +90 -146
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +1 -7
- package/build/image-editor/react/hooks/use-cropper-controller.cjs +80 -0
- package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +1 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +179 -205
- package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -7
- package/build/image-editor/react/hooks/use-transform-style.cjs +20 -34
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +1 -7
- package/build/image-editor/react/hooks/use-viewport-state.cjs +36 -47
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -7
- package/build/image-editor/react/visually-hidden-style.cjs +21 -38
- package/build/image-editor/react/visually-hidden-style.cjs.map +1 -7
- package/build/index.cjs +45 -39
- package/build/lock-unlock.cjs +9 -35
- package/build/lock-unlock.cjs.map +1 -7
- package/build/private-apis.cjs +16 -37
- package/build/private-apis.cjs.map +1 -7
- package/build/state/aspect-ratio-presets.cjs +53 -0
- package/build/state/aspect-ratio-presets.cjs.map +1 -0
- package/build/state/composite-reducer.cjs +134 -112
- package/build/state/composite-reducer.cjs.map +1 -7
- package/build/state/index.cjs +63 -47
- package/build/state/media-editor-state-provider.cjs +35 -48
- package/build/state/media-editor-state-provider.cjs.map +1 -7
- package/build/state/types.cjs +12 -31
- package/build/state/types.cjs.map +1 -7
- package/build/state/use-media-editor-state.cjs +261 -294
- package/build/state/use-media-editor-state.cjs.map +1 -7
- package/build/store/actions.cjs +17 -44
- package/build/store/actions.cjs.map +1 -7
- package/build/store/constants.cjs +8 -29
- package/build/store/constants.cjs.map +1 -7
- package/build/store/index.cjs +33 -40
- package/build/store/index.cjs.map +1 -7
- package/build/store/reducer.cjs +28 -50
- package/build/store/reducer.cjs.map +1 -7
- package/build/store/selectors.cjs +16 -41
- package/build/store/selectors.cjs.map +1 -7
- package/build/utils/get-media-type.cjs +18 -41
- package/build/utils/get-media-type.cjs.map +1 -7
- package/build/utils/index.cjs +9 -28
- package/build-module/components/media-editor/index.mjs +490 -673
- package/build-module/components/media-editor/index.mjs.map +1 -7
- package/build-module/components/media-editor/use-crop-options.mjs +38 -32
- package/build-module/components/media-editor/use-crop-options.mjs.map +1 -7
- package/build-module/components/media-editor/use-save-media-editor.mjs +89 -157
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
- package/build-module/components/media-editor-canvas/index.mjs +98 -96
- package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
- package/build-module/components/media-editor-crop-panel/index.mjs +43 -46
- package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -7
- package/build-module/components/media-editor-fine-rotation/index.mjs +39 -45
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
- package/build-module/components/media-editor-image-controls/index.mjs +172 -237
- package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +112 -137
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -7
- package/build-module/components/media-editor-modal/build-modifiers.mjs +103 -64
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -7
- package/build-module/components/media-editor-modal/index.mjs +88 -124
- package/build-module/components/media-editor-modal/index.mjs.map +1 -7
- package/build-module/components/media-editor-provider/index.mjs +26 -29
- package/build-module/components/media-editor-provider/index.mjs.map +1 -7
- package/build-module/components/media-form/index.mjs +68 -56
- package/build-module/components/media-form/index.mjs.map +1 -7
- package/build-module/components/media-preview/index.mjs +106 -90
- package/build-module/components/media-preview/index.mjs.map +1 -7
- package/build-module/components/rotation-ruler/index.mjs +161 -164
- package/build-module/components/rotation-ruler/index.mjs.map +1 -7
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +125 -122
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -7
- package/build-module/hooks/use-crop-gesture-handlers.mjs +76 -50
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +1 -7
- package/build-module/image-editor/core/camera.mjs +245 -178
- package/build-module/image-editor/core/camera.mjs.map +1 -7
- package/build-module/image-editor/core/constants.mjs +110 -64
- package/build-module/image-editor/core/constants.mjs.map +1 -7
- package/build-module/image-editor/core/containment.mjs +265 -237
- package/build-module/image-editor/core/containment.mjs.map +1 -7
- package/build-module/image-editor/core/crop-rect.mjs +29 -20
- package/build-module/image-editor/core/crop-rect.mjs.map +1 -7
- package/build-module/image-editor/core/export/canvas-renderer.mjs +155 -105
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -7
- package/build-module/image-editor/core/index.mjs +5 -34
- package/build-module/image-editor/core/interaction-controller.mjs +579 -628
- package/build-module/image-editor/core/interaction-controller.mjs.map +1 -7
- package/build-module/image-editor/core/math/rotation.mjs +21 -10
- package/build-module/image-editor/core/math/rotation.mjs.map +1 -7
- package/build-module/image-editor/core/math/sanitize.mjs +116 -52
- package/build-module/image-editor/core/math/sanitize.mjs.map +1 -7
- package/build-module/image-editor/core/setter-helpers.mjs +52 -35
- package/build-module/image-editor/core/setter-helpers.mjs.map +1 -7
- package/build-module/image-editor/core/source-region.mjs +210 -160
- package/build-module/image-editor/core/source-region.mjs.map +1 -7
- package/build-module/image-editor/core/state.mjs +271 -243
- package/build-module/image-editor/core/state.mjs.map +1 -7
- package/build-module/image-editor/core/stencil-math.mjs +185 -178
- package/build-module/image-editor/core/stencil-math.mjs.map +1 -7
- package/build-module/image-editor/core/transform-style.mjs +34 -23
- package/build-module/image-editor/core/transform-style.mjs.map +1 -7
- package/build-module/image-editor/core/transforms/pipeline.mjs +29 -11
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +1 -7
- package/build-module/image-editor/core/types.mjs +0 -1
- package/build-module/image-editor/core/viewport-state.mjs +26 -20
- package/build-module/image-editor/core/viewport-state.mjs.map +1 -7
- package/build-module/image-editor/index.mjs +3 -38
- package/build-module/image-editor/react/components/cropper-provider.mjs +38 -21
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +1 -7
- package/build-module/image-editor/react/components/cropper.mjs +550 -686
- package/build-module/image-editor/react/components/cropper.mjs.map +1 -7
- package/build-module/image-editor/react/components/index.mjs +1 -7
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +130 -135
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -7
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +24 -35
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +1 -7
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +54 -64
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +1 -7
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +355 -407
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +1 -7
- package/build-module/image-editor/react/components/viewport-provider.mjs +15 -22
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +72 -43
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/index.mjs +2 -7
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +83 -119
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +75 -0
- package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +1 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +175 -183
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/use-transform-style.mjs +15 -9
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs +32 -26
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -7
- package/build-module/image-editor/react/visually-hidden-style.mjs +17 -14
- package/build-module/image-editor/react/visually-hidden-style.mjs.map +1 -7
- package/build-module/index.mjs +3 -10
- package/build-module/lock-unlock.mjs +5 -10
- package/build-module/lock-unlock.mjs.map +1 -7
- package/build-module/private-apis.mjs +8 -9
- package/build-module/private-apis.mjs.map +1 -7
- package/build-module/state/aspect-ratio-presets.mjs +48 -0
- package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
- package/build-module/state/composite-reducer.mjs +114 -90
- package/build-module/state/composite-reducer.mjs.map +1 -7
- package/build-module/state/index.mjs +5 -25
- package/build-module/state/media-editor-state-provider.mjs +29 -24
- package/build-module/state/media-editor-state-provider.mjs.map +1 -7
- package/build-module/state/types.mjs +8 -7
- package/build-module/state/types.mjs.map +1 -7
- package/build-module/state/use-media-editor-state.mjs +256 -286
- package/build-module/state/use-media-editor-state.mjs.map +1 -7
- package/build-module/store/actions.mjs +12 -19
- package/build-module/store/actions.mjs.map +1 -7
- package/build-module/store/constants.mjs +4 -5
- package/build-module/store/constants.mjs.map +1 -7
- package/build-module/store/index.mjs +8 -9
- package/build-module/store/index.mjs.map +1 -7
- package/build-module/store/reducer.mjs +23 -26
- package/build-module/store/reducer.mjs.map +1 -7
- package/build-module/store/selectors.mjs +8 -13
- package/build-module/store/selectors.mjs.map +1 -7
- package/build-module/utils/get-media-type.mjs +14 -17
- package/build-module/utils/get-media-type.mjs.map +1 -7
- package/build-module/utils/index.mjs +1 -6
- package/build-style/style-rtl.css +41 -4
- package/build-style/style.css +41 -4
- package/build-types/components/media-editor/index.d.ts +4 -0
- package/build-types/components/media-editor/index.d.ts.map +1 -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.map +1 -1
- package/build-types/components/media-editor-canvas/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/build-modifiers.d.ts +6 -5
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -1
- package/build-types/components/media-preview/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +0 -1
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/index.d.ts +1 -1
- package/build-types/image-editor/core/index.d.ts.map +1 -1
- package/build-types/image-editor/core/setter-helpers.d.ts +1 -1
- package/build-types/image-editor/core/types.d.ts +1 -1
- package/build-types/image-editor/index.d.ts +7 -6
- package/build-types/image-editor/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +2 -2
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +5 -14
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +2 -2
- package/build-types/image-editor/react/hooks/index.d.ts +2 -2
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/{use-cropper-reducer.d.ts → use-cropper-controller.d.ts} +12 -12
- package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +1 -0
- package/build-types/state/aspect-ratio-presets.d.ts +7 -0
- package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
- package/build-types/state/composite-reducer.d.ts +1 -1
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +1 -0
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/types.d.ts +2 -1
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/package.json +21 -21
- package/src/components/media-editor/index.tsx +215 -162
- package/src/components/media-editor/style.scss +19 -0
- package/src/components/media-editor/use-crop-options.ts +5 -2
- package/src/components/media-editor/use-save-media-editor.ts +2 -6
- package/src/components/media-editor-canvas/index.tsx +16 -26
- package/src/components/media-editor-image-controls/index.tsx +4 -2
- package/src/components/media-editor-modal/build-modifiers.ts +13 -7
- package/src/components/media-editor-modal/style.scss +1 -1
- package/src/components/media-preview/index.tsx +66 -23
- package/src/components/media-preview/style.scss +16 -2
- package/src/components/rotation-ruler/style.scss +6 -2
- package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
- package/src/image-editor/README.md +8 -8
- package/src/image-editor/core/constants.ts +0 -13
- package/src/image-editor/core/index.ts +1 -5
- package/src/image-editor/core/setter-helpers.ts +1 -1
- package/src/image-editor/core/types.ts +1 -1
- package/src/image-editor/docs/architecture.md +1 -1
- package/src/image-editor/docs/recipes.md +3 -3
- package/src/image-editor/index.ts +7 -16
- package/src/image-editor/react/components/cropper-provider.tsx +5 -5
- package/src/image-editor/react/components/cropper.tsx +19 -27
- package/src/image-editor/react/hooks/build-cropper-setters.ts +2 -2
- package/src/image-editor/react/hooks/index.ts +2 -2
- package/src/image-editor/react/hooks/{use-cropper-reducer.ts → use-cropper-controller.ts} +11 -11
- package/src/state/aspect-ratio-presets.ts +18 -0
- package/src/state/composite-reducer.ts +2 -2
- package/src/state/index.ts +1 -0
- package/src/state/types.ts +2 -6
- package/src/state/use-media-editor-state.ts +7 -5
- package/build/image-editor/core/index.cjs.map +0 -7
- package/build/image-editor/core/types.cjs.map +0 -7
- package/build/image-editor/index.cjs.map +0 -7
- package/build/image-editor/react/components/index.cjs.map +0 -7
- package/build/image-editor/react/hooks/index.cjs.map +0 -7
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +0 -113
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +0 -7
- package/build/index.cjs.map +0 -7
- package/build/state/index.cjs.map +0 -7
- package/build/utils/index.cjs.map +0 -7
- package/build-module/image-editor/core/index.mjs.map +0 -7
- package/build-module/image-editor/core/types.mjs.map +0 -7
- package/build-module/image-editor/index.mjs.map +0 -7
- package/build-module/image-editor/react/components/index.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +0 -100
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +0 -7
- package/build-module/index.mjs.map +0 -7
- package/build-module/state/index.mjs.map +0 -7
- package/build-module/utils/index.mjs.map +0 -7
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
|
@@ -1,253 +1,281 @@
|
|
|
1
|
-
// packages/media-editor/src/image-editor/core/containment.ts
|
|
2
1
|
import { mat2d, vec2 } from "gl-matrix";
|
|
3
2
|
import { ABSOLUTE_MIN_ZOOM, MAX_ZOOM, MIN_ZOOM } from "./constants.mjs";
|
|
4
3
|
import { degreesToRadians } from "./math/rotation.mjs";
|
|
5
|
-
import {
|
|
6
|
-
isValidSize,
|
|
7
|
-
safeBoundedNumber,
|
|
8
|
-
sanitizeCropperState,
|
|
9
|
-
sanitizeRect
|
|
10
|
-
} from "./math/sanitize.mjs";
|
|
4
|
+
import { isValidSize, safeBoundedNumber, sanitizeCropperState, sanitizeRect } from "./math/sanitize.mjs";
|
|
11
5
|
import { createCamera, getRotatedBBox, getVisibleBounds } from "./camera.mjs";
|
|
12
|
-
|
|
6
|
+
// packages/media-editor/src/image-editor/core/containment.ts
|
|
7
|
+
/**
|
|
8
|
+
* Compute the visual (rotated) bounding-box dimensions in pixel-proportional
|
|
9
|
+
* units where the unrotated image is `a × 1` (renderedW = a, renderedH = 1).
|
|
10
|
+
*
|
|
11
|
+
* @param rotation Rotation angle in degrees.
|
|
12
|
+
* @param imageAspectRatio Image width / height ratio.
|
|
13
|
+
* @return Visual dimensions and trig helpers.
|
|
14
|
+
*/
|
|
13
15
|
function getVisualDimensions(rotation, imageAspectRatio) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
const rad = degreesToRadians(rotation);
|
|
17
|
+
const absC = Math.abs(Math.cos(rad));
|
|
18
|
+
const absS = Math.abs(Math.sin(rad));
|
|
19
|
+
const bbox = getRotatedBBox(imageAspectRatio, 1, rotation);
|
|
20
|
+
return {
|
|
21
|
+
visualW: bbox.width,
|
|
22
|
+
visualH: bbox.height,
|
|
23
|
+
absC,
|
|
24
|
+
absS
|
|
25
|
+
};
|
|
24
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* The geometric minimum zoom required for the rotated image to fully cover the
|
|
29
|
+
* crop rectangle. Works in pixel-proportional space where the unrotated image
|
|
30
|
+
* is a×1; the crop rect lives in visual-normalized space and is scaled by the
|
|
31
|
+
* rotation-dependent visual dimensions before projecting into the image-local
|
|
32
|
+
* frame.
|
|
33
|
+
*
|
|
34
|
+
* Returns 0 for a degenerate (zero-area) crop — callers compose the result
|
|
35
|
+
* with their own floor (typically the current zoom or `MIN_ZOOM`).
|
|
36
|
+
*
|
|
37
|
+
* @param rotation Rotation angle in degrees.
|
|
38
|
+
* @param imageAspectRatio Image width / height ratio.
|
|
39
|
+
* @param cropRect The crop rectangle in normalized coordinates.
|
|
40
|
+
* @return The minimum zoom factor needed for coverage.
|
|
41
|
+
*/
|
|
25
42
|
function getMinZoomForCover(rotation, imageAspectRatio, cropRect) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const spanBeta = cropHalfW * absS + cropHalfH * absC;
|
|
37
|
-
const zoomFromAlpha = 2 * spanAlpha / aspectRatio;
|
|
38
|
-
const zoomFromBeta = 2 * spanBeta;
|
|
39
|
-
return Math.max(zoomFromAlpha, zoomFromBeta);
|
|
43
|
+
const aspectRatio = Math.max(imageAspectRatio, Number.EPSILON);
|
|
44
|
+
const { visualW: snapVisualW, visualH: snapVisualH } = getVisualDimensions(Math.round(rotation / 90) * 90, aspectRatio);
|
|
45
|
+
const { absC, absS } = getVisualDimensions(rotation, aspectRatio);
|
|
46
|
+
const cropHalfW = cropRect.width * snapVisualW / 2;
|
|
47
|
+
const cropHalfH = cropRect.height * snapVisualH / 2;
|
|
48
|
+
const spanAlpha = cropHalfW * absC + cropHalfH * absS;
|
|
49
|
+
const spanBeta = cropHalfW * absS + cropHalfH * absC;
|
|
50
|
+
const zoomFromAlpha = 2 * spanAlpha / aspectRatio;
|
|
51
|
+
const zoomFromBeta = 2 * spanBeta;
|
|
52
|
+
return Math.max(zoomFromAlpha, zoomFromBeta);
|
|
40
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Resolves the effective minimum zoom for the current state — the coverage-
|
|
56
|
+
* aware floor when an image is loaded, falling back to `MIN_ZOOM` otherwise.
|
|
57
|
+
* Used by both the cropper's interaction layer and the zoom slider so the
|
|
58
|
+
* floor stays consistent across surfaces.
|
|
59
|
+
*
|
|
60
|
+
* @param state The current cropper state.
|
|
61
|
+
* @return The minimum zoom factor.
|
|
62
|
+
*/
|
|
41
63
|
function getMinZoom(state) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
const safeState = sanitizeCropperState(state);
|
|
53
|
-
const aspectRatio = imageSize.width / imageSize.height;
|
|
54
|
-
return Math.max(
|
|
55
|
-
ABSOLUTE_MIN_ZOOM,
|
|
56
|
-
getMinZoomForCover(
|
|
57
|
-
safeState.rotation,
|
|
58
|
-
aspectRatio,
|
|
59
|
-
safeState.cropRect
|
|
60
|
-
)
|
|
61
|
-
);
|
|
64
|
+
if (!state.image) return MIN_ZOOM;
|
|
65
|
+
const imageSize = {
|
|
66
|
+
width: state.image.naturalWidth,
|
|
67
|
+
height: state.image.naturalHeight
|
|
68
|
+
};
|
|
69
|
+
if (!isValidSize(imageSize)) return MIN_ZOOM;
|
|
70
|
+
const safeState = sanitizeCropperState(state);
|
|
71
|
+
const aspectRatio = imageSize.width / imageSize.height;
|
|
72
|
+
return Math.max(ABSOLUTE_MIN_ZOOM, getMinZoomForCover(safeState.rotation, aspectRatio, safeState.cropRect));
|
|
62
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Compute the crop handle bounds in normalized visual space — the
|
|
76
|
+
* axis-aligned bounding box (AABB) of the actual image footprint at the
|
|
77
|
+
* current pan/zoom/rotation.
|
|
78
|
+
*
|
|
79
|
+
* Returns the full image AABB without intersecting with the container
|
|
80
|
+
* viewport. The viewport pan mechanism in the Cropper component allows
|
|
81
|
+
* the canvas to scroll so handles can reach the image edge even when it
|
|
82
|
+
* lies outside the visible canvas area.
|
|
83
|
+
*
|
|
84
|
+
* Unlike a static centerline-based calculation, this accounts for the
|
|
85
|
+
* current pan position — if the user pans right, the left bound tightens
|
|
86
|
+
* because the image's left edge has moved right.
|
|
87
|
+
*
|
|
88
|
+
* @param state The current cropper state (crop, zoom, rotation, flip).
|
|
89
|
+
* @param elementSize The fitted (unrotated) image element dimensions in pixels.
|
|
90
|
+
* @param visualSize The visual (rotated) image bounding box in pixels.
|
|
91
|
+
* @return The min/max x and y that a crop rect edge can reach in normalized space.
|
|
92
|
+
*/
|
|
63
93
|
function getImageCropBounds(state, elementSize, visualSize) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
94
|
+
if (!isValidSize(elementSize) || !isValidSize(visualSize)) return {
|
|
95
|
+
minX: 0,
|
|
96
|
+
minY: 0,
|
|
97
|
+
maxX: 1,
|
|
98
|
+
maxY: 1
|
|
99
|
+
};
|
|
100
|
+
const safeState = sanitizeCropperState(state);
|
|
101
|
+
const tx = safeState.pan.x * visualSize.width;
|
|
102
|
+
const ty = safeState.pan.y * visualSize.height;
|
|
103
|
+
const rad = degreesToRadians(safeState.rotation);
|
|
104
|
+
const cos = Math.cos(rad);
|
|
105
|
+
const sin = Math.sin(rad);
|
|
106
|
+
const sx = safeState.flip.horizontal ? -1 : 1;
|
|
107
|
+
const sy = safeState.flip.vertical ? -1 : 1;
|
|
108
|
+
const z = safeState.zoom;
|
|
109
|
+
const ma = sx * cos * z;
|
|
110
|
+
const mb = sy * sin * z;
|
|
111
|
+
const mc = -sx * sin * z;
|
|
112
|
+
const md = sy * cos * z;
|
|
113
|
+
const hw = elementSize.width / 2;
|
|
114
|
+
const hh = elementSize.height / 2;
|
|
115
|
+
const corners = [
|
|
116
|
+
[-hw, -hh],
|
|
117
|
+
[hw, -hh],
|
|
118
|
+
[hw, hh],
|
|
119
|
+
[-hw, hh]
|
|
120
|
+
];
|
|
121
|
+
let imgMinX = Infinity;
|
|
122
|
+
let imgMaxX = -Infinity;
|
|
123
|
+
let imgMinY = Infinity;
|
|
124
|
+
let imgMaxY = -Infinity;
|
|
125
|
+
for (const [cx, cy] of corners) {
|
|
126
|
+
const screenX = ma * cx + mc * cy + tx;
|
|
127
|
+
const screenY = mb * cx + md * cy + ty;
|
|
128
|
+
const nx = screenX / visualSize.width + .5;
|
|
129
|
+
const ny = screenY / visualSize.height + .5;
|
|
130
|
+
imgMinX = Math.min(imgMinX, nx);
|
|
131
|
+
imgMaxX = Math.max(imgMaxX, nx);
|
|
132
|
+
imgMinY = Math.min(imgMinY, ny);
|
|
133
|
+
imgMaxY = Math.max(imgMaxY, ny);
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
minX: imgMinX,
|
|
137
|
+
minY: imgMinY,
|
|
138
|
+
maxX: imgMaxX,
|
|
139
|
+
maxY: imgMaxY
|
|
140
|
+
};
|
|
108
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Restricts a crop rectangle so that the rotated, zoomed image can fully cover it.
|
|
144
|
+
* If the crop rect is too large for the current zoom and rotation, it is scaled
|
|
145
|
+
* down proportionally and re-centered.
|
|
146
|
+
*
|
|
147
|
+
* Works in pixel-proportional space where the unrotated image is a×1.
|
|
148
|
+
*
|
|
149
|
+
* @param cropRect The crop rectangle in normalized coordinates.
|
|
150
|
+
* @param zoom The current zoom factor.
|
|
151
|
+
* @param rotation The rotation angle in degrees.
|
|
152
|
+
* @param imageAspectRatio The image width / height ratio.
|
|
153
|
+
* @return The restricted crop rectangle.
|
|
154
|
+
*/
|
|
109
155
|
function restrictCropRect(cropRect, zoom, rotation, imageAspectRatio) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
let newX = centerX - newW / 2;
|
|
145
|
-
let newY = centerY - newH / 2;
|
|
146
|
-
newX = Math.max(0, Math.min(newX, 1 - newW));
|
|
147
|
-
newY = Math.max(0, Math.min(newY, 1 - newH));
|
|
148
|
-
return { x: newX, y: newY, width: newW, height: newH };
|
|
156
|
+
const safeRect = sanitizeRect(cropRect);
|
|
157
|
+
const zoomCandidate = safeBoundedNumber(zoom, 1);
|
|
158
|
+
const safeZoom = zoomCandidate >= Number.EPSILON ? zoomCandidate : 1;
|
|
159
|
+
const safeRotation = safeBoundedNumber(rotation, 0);
|
|
160
|
+
const safeAspect = safeBoundedNumber(imageAspectRatio, 1);
|
|
161
|
+
const aspectRatio = Math.max(safeAspect, Number.EPSILON);
|
|
162
|
+
const { visualW, visualH } = getVisualDimensions(Math.round(safeRotation / 90) * 90, aspectRatio);
|
|
163
|
+
const { absC, absS } = getVisualDimensions(safeRotation, aspectRatio);
|
|
164
|
+
const W = safeRect.width;
|
|
165
|
+
const H = safeRect.height;
|
|
166
|
+
const cropWPx = W * visualW;
|
|
167
|
+
const cropHPx = H * visualH;
|
|
168
|
+
const spanAlpha = cropWPx * absC + cropHPx * absS;
|
|
169
|
+
const spanBeta = cropWPx * absS + cropHPx * absC;
|
|
170
|
+
const limitAlpha = aspectRatio * safeZoom;
|
|
171
|
+
const limitBeta = safeZoom;
|
|
172
|
+
let t = 1;
|
|
173
|
+
if (spanAlpha > 0) t = Math.min(t, limitAlpha / spanAlpha);
|
|
174
|
+
if (spanBeta > 0) t = Math.min(t, limitBeta / spanBeta);
|
|
175
|
+
if (t >= .999999999) return safeRect;
|
|
176
|
+
const newW = W * t;
|
|
177
|
+
const newH = H * t;
|
|
178
|
+
const centerX = safeRect.x + W / 2;
|
|
179
|
+
const centerY = safeRect.y + H / 2;
|
|
180
|
+
let newX = centerX - newW / 2;
|
|
181
|
+
let newY = centerY - newH / 2;
|
|
182
|
+
newX = Math.max(0, Math.min(newX, 1 - newW));
|
|
183
|
+
newY = Math.max(0, Math.min(newY, 1 - newH));
|
|
184
|
+
return {
|
|
185
|
+
x: newX,
|
|
186
|
+
y: newY,
|
|
187
|
+
width: newW,
|
|
188
|
+
height: newH
|
|
189
|
+
};
|
|
149
190
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
191
|
+
/**
|
|
192
|
+
* Canonical container used internally by restrictPanZoom.
|
|
193
|
+
* Containment is scale-invariant, so the actual size doesn't matter —
|
|
194
|
+
* only the relative geometry between stencil and image matters.
|
|
195
|
+
*/
|
|
196
|
+
const CANONICAL_CONTAINER = {
|
|
197
|
+
width: 1e3,
|
|
198
|
+
height: 1e3
|
|
199
|
+
};
|
|
200
|
+
const _scratchMat = mat2d.create();
|
|
201
|
+
const _scratchVec = vec2.create();
|
|
202
|
+
/**
|
|
203
|
+
* Clamps pan and adjusts zoom so that the zoomed, rotated image fully covers
|
|
204
|
+
* the crop rectangle.
|
|
205
|
+
*
|
|
206
|
+
* Uses the camera matrix to project: builds a camera from the candidate state,
|
|
207
|
+
* maps the stencil corners (axis-aligned in the visual bounding box) to world
|
|
208
|
+
* space via the inverse camera, and checks that all world points lie within
|
|
209
|
+
* [0,1]×[0,1]. If any point is outside, computes the minimal pan correction.
|
|
210
|
+
*
|
|
211
|
+
* @param state The current cropper state.
|
|
212
|
+
* @param imageSize The natural size of the image in pixels.
|
|
213
|
+
* @param cropRect The crop rectangle in normalized coordinates.
|
|
214
|
+
* @return The restricted crop pan and zoom values.
|
|
215
|
+
*/
|
|
153
216
|
function restrictPanZoom(state, imageSize, cropRect) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (w[1] > maxWy) {
|
|
216
|
-
maxWy = w[1];
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
if (minWx >= -EPSILON && maxWx <= 1 + EPSILON && minWy >= -EPSILON && maxWy <= 1 + EPSILON) {
|
|
220
|
-
return { pan: safeState.pan, zoom };
|
|
221
|
-
}
|
|
222
|
-
let dwx = 0;
|
|
223
|
-
let dwy = 0;
|
|
224
|
-
if (minWx < 0 && maxWx <= 1 + EPSILON) {
|
|
225
|
-
dwx = -minWx;
|
|
226
|
-
} else if (maxWx > 1 && minWx >= -EPSILON) {
|
|
227
|
-
dwx = 1 - maxWx;
|
|
228
|
-
} else if (minWx < 0 && maxWx > 1) {
|
|
229
|
-
dwx = (1 - maxWx - minWx) / 2;
|
|
230
|
-
}
|
|
231
|
-
if (minWy < 0 && maxWy <= 1 + EPSILON) {
|
|
232
|
-
dwy = -minWy;
|
|
233
|
-
} else if (maxWy > 1 && minWy >= -EPSILON) {
|
|
234
|
-
dwy = 1 - maxWy;
|
|
235
|
-
} else if (minWy < 0 && maxWy > 1) {
|
|
236
|
-
dwy = (1 - maxWy - minWy) / 2;
|
|
237
|
-
}
|
|
238
|
-
const dsx = camera[0] * dwx + camera[2] * dwy;
|
|
239
|
-
const dsy = camera[1] * dwx + camera[3] * dwy;
|
|
240
|
-
const newPanX = safeState.pan.x - (visibleBounds.width > 0 ? dsx / visibleBounds.width : 0);
|
|
241
|
-
const newPanY = safeState.pan.y - (visibleBounds.height > 0 ? dsy / visibleBounds.height : 0);
|
|
242
|
-
return {
|
|
243
|
-
pan: { x: newPanX, y: newPanY },
|
|
244
|
-
zoom
|
|
245
|
-
};
|
|
217
|
+
const safeState = sanitizeCropperState(state);
|
|
218
|
+
const safeCropRect = sanitizeRect(cropRect);
|
|
219
|
+
const aspectRatio = isValidSize(imageSize) ? imageSize.width / imageSize.height : 1;
|
|
220
|
+
const minZoom = Math.max(ABSOLUTE_MIN_ZOOM, getMinZoomForCover(safeState.rotation, aspectRatio, safeCropRect));
|
|
221
|
+
const zoom = Math.min(MAX_ZOOM, Math.max(safeState.zoom, minZoom));
|
|
222
|
+
const candidateState = {
|
|
223
|
+
...safeState,
|
|
224
|
+
zoom
|
|
225
|
+
};
|
|
226
|
+
const camera = createCamera(candidateState, CANONICAL_CONTAINER, imageSize);
|
|
227
|
+
const snapRotation = Math.round(safeState.rotation / 90) * 90;
|
|
228
|
+
const baseCamera = createCamera({
|
|
229
|
+
...candidateState,
|
|
230
|
+
pan: {
|
|
231
|
+
x: 0,
|
|
232
|
+
y: 0
|
|
233
|
+
},
|
|
234
|
+
zoom: 1,
|
|
235
|
+
rotation: snapRotation
|
|
236
|
+
}, CANONICAL_CONTAINER, imageSize);
|
|
237
|
+
const visibleBounds = getVisibleBounds(baseCamera);
|
|
238
|
+
const stencilCorners = [
|
|
239
|
+
[visibleBounds.left + safeCropRect.x * visibleBounds.width, visibleBounds.top + safeCropRect.y * visibleBounds.height],
|
|
240
|
+
[visibleBounds.left + (safeCropRect.x + safeCropRect.width) * visibleBounds.width, visibleBounds.top + safeCropRect.y * visibleBounds.height],
|
|
241
|
+
[visibleBounds.left + (safeCropRect.x + safeCropRect.width) * visibleBounds.width, visibleBounds.top + (safeCropRect.y + safeCropRect.height) * visibleBounds.height],
|
|
242
|
+
[visibleBounds.left + safeCropRect.x * visibleBounds.width, visibleBounds.top + (safeCropRect.y + safeCropRect.height) * visibleBounds.height]
|
|
243
|
+
];
|
|
244
|
+
mat2d.invert(_scratchMat, camera);
|
|
245
|
+
let minWx = Infinity;
|
|
246
|
+
let maxWx = -Infinity;
|
|
247
|
+
let minWy = Infinity;
|
|
248
|
+
let maxWy = -Infinity;
|
|
249
|
+
for (const corner of stencilCorners) {
|
|
250
|
+
vec2.transformMat2d(_scratchVec, corner, _scratchMat);
|
|
251
|
+
const w = _scratchVec;
|
|
252
|
+
if (w[0] < minWx) minWx = w[0];
|
|
253
|
+
if (w[0] > maxWx) maxWx = w[0];
|
|
254
|
+
if (w[1] < minWy) minWy = w[1];
|
|
255
|
+
if (w[1] > maxWy) maxWy = w[1];
|
|
256
|
+
}
|
|
257
|
+
if (minWx >= -1e-9 && maxWx <= 1.000000001 && minWy >= -1e-9 && maxWy <= 1.000000001) return {
|
|
258
|
+
pan: safeState.pan,
|
|
259
|
+
zoom
|
|
260
|
+
};
|
|
261
|
+
let dwx = 0;
|
|
262
|
+
let dwy = 0;
|
|
263
|
+
if (minWx < 0 && maxWx <= 1.000000001) dwx = -minWx;
|
|
264
|
+
else if (maxWx > 1 && minWx >= -1e-9) dwx = 1 - maxWx;
|
|
265
|
+
else if (minWx < 0 && maxWx > 1) dwx = (1 - maxWx - minWx) / 2;
|
|
266
|
+
if (minWy < 0 && maxWy <= 1.000000001) dwy = -minWy;
|
|
267
|
+
else if (maxWy > 1 && minWy >= -1e-9) dwy = 1 - maxWy;
|
|
268
|
+
else if (minWy < 0 && maxWy > 1) dwy = (1 - maxWy - minWy) / 2;
|
|
269
|
+
const dsx = camera[0] * dwx + camera[2] * dwy;
|
|
270
|
+
const dsy = camera[1] * dwx + camera[3] * dwy;
|
|
271
|
+
return {
|
|
272
|
+
pan: {
|
|
273
|
+
x: safeState.pan.x - (visibleBounds.width > 0 ? dsx / visibleBounds.width : 0),
|
|
274
|
+
y: safeState.pan.y - (visibleBounds.height > 0 ? dsy / visibleBounds.height : 0)
|
|
275
|
+
},
|
|
276
|
+
zoom
|
|
277
|
+
};
|
|
246
278
|
}
|
|
247
|
-
export {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
restrictCropRect,
|
|
251
|
-
restrictPanZoom
|
|
252
|
-
};
|
|
253
|
-
//# sourceMappingURL=containment.mjs.map
|
|
279
|
+
export { getImageCropBounds, getMinZoom, restrictCropRect, restrictPanZoom };
|
|
280
|
+
|
|
281
|
+
//# sourceMappingURL=containment.mjs.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/image-editor/core/containment.ts"],
|
|
4
|
-
"sourcesContent": ["import { mat2d, vec2 } from 'gl-matrix';\nimport type { CropperState, NormalizedRect, Size } from './types';\nimport { ABSOLUTE_MIN_ZOOM, MAX_ZOOM, MIN_ZOOM } from './constants';\nimport { degreesToRadians } from './math/rotation';\nimport {\n\tisValidSize,\n\tsafeBoundedNumber,\n\tsanitizeCropperState,\n\tsanitizeRect,\n} from './math/sanitize';\nimport { createCamera, getRotatedBBox, getVisibleBounds } from './camera';\n\n/** Floating-point epsilon for \"close enough to equal\" comparisons. */\nconst EPSILON = 1e-9;\n\n/**\n * Compute the visual (rotated) bounding-box dimensions in pixel-proportional\n * units where the unrotated image is `a × 1` (renderedW = a, renderedH = 1).\n *\n * @param rotation Rotation angle in degrees.\n * @param imageAspectRatio Image width / height ratio.\n * @return Visual dimensions and trig helpers.\n */\nfunction getVisualDimensions(\n\trotation: number,\n\timageAspectRatio: number\n): { visualW: number; visualH: number; absC: number; absS: number } {\n\tconst rad = degreesToRadians( rotation );\n\tconst absC = Math.abs( Math.cos( rad ) );\n\tconst absS = Math.abs( Math.sin( rad ) );\n\tconst bbox = getRotatedBBox( imageAspectRatio, 1, rotation );\n\treturn {\n\t\tvisualW: bbox.width,\n\t\tvisualH: bbox.height,\n\t\tabsC,\n\t\tabsS,\n\t};\n}\n\n/**\n * The geometric minimum zoom required for the rotated image to fully cover the\n * crop rectangle. Works in pixel-proportional space where the unrotated image\n * is a×1; the crop rect lives in visual-normalized space and is scaled by the\n * rotation-dependent visual dimensions before projecting into the image-local\n * frame.\n *\n * Returns 0 for a degenerate (zero-area) crop — callers compose the result\n * with their own floor (typically the current zoom or `MIN_ZOOM`).\n *\n * @param rotation Rotation angle in degrees.\n * @param imageAspectRatio Image width / height ratio.\n * @param cropRect The crop rectangle in normalized coordinates.\n * @return The minimum zoom factor needed for coverage.\n */\nfunction getMinZoomForCover(\n\trotation: number,\n\timageAspectRatio: number,\n\tcropRect: NormalizedRect\n): number {\n\tconst aspectRatio = Math.max( imageAspectRatio, Number.EPSILON );\n\t// The crop rect lives in the SNAP-rotation visual bbox — that's how the\n\t// stencil and preview position it (see createCamera / getImageFit). Use\n\t// the snap-rotation dimensions to convert crop fractions to pixels.\n\tconst snapRotation = Math.round( rotation / 90 ) * 90;\n\tconst { visualW: snapVisualW, visualH: snapVisualH } = getVisualDimensions(\n\t\tsnapRotation,\n\t\taspectRatio\n\t);\n\t// Projection into the image-local frame uses the TRUE rotation angle —\n\t// that's the actual orientation of the image under the stencil.\n\tconst { absC, absS } = getVisualDimensions( rotation, aspectRatio );\n\n\t// Crop half-extents in pixel-proportional space.\n\tconst cropHalfW = ( cropRect.width * snapVisualW ) / 2;\n\tconst cropHalfH = ( cropRect.height * snapVisualH ) / 2;\n\n\t// AABB of the crop rect projected into the image-local (unrotated) frame.\n\tconst spanAlpha = cropHalfW * absC + cropHalfH * absS;\n\tconst spanBeta = cropHalfW * absS + cropHalfH * absC;\n\n\t// Image half-extents at zoom z: (aspectRatio*z/2, z/2).\n\t// Coverage requires: aspectRatio*z/2 >= spanAlpha AND z/2 >= spanBeta.\n\tconst zoomFromAlpha = ( 2 * spanAlpha ) / aspectRatio;\n\tconst zoomFromBeta = 2 * spanBeta;\n\n\treturn Math.max( zoomFromAlpha, zoomFromBeta );\n}\n\n/**\n * Resolves the effective minimum zoom for the current state — the coverage-\n * aware floor when an image is loaded, falling back to `MIN_ZOOM` otherwise.\n * Used by both the cropper's interaction layer and the zoom slider so the\n * floor stays consistent across surfaces.\n *\n * @param state The current cropper state.\n * @return The minimum zoom factor.\n */\nexport function getMinZoom( state: CropperState ): number {\n\tif ( ! state.image ) {\n\t\treturn MIN_ZOOM;\n\t}\n\tconst imageSize = {\n\t\twidth: state.image.naturalWidth,\n\t\theight: state.image.naturalHeight,\n\t};\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn MIN_ZOOM;\n\t}\n\tconst safeState = sanitizeCropperState( state );\n\tconst aspectRatio = imageSize.width / imageSize.height;\n\treturn Math.max(\n\t\tABSOLUTE_MIN_ZOOM,\n\t\tgetMinZoomForCover(\n\t\t\tsafeState.rotation,\n\t\t\taspectRatio,\n\t\t\tsafeState.cropRect\n\t\t)\n\t);\n}\n\n/**\n * Compute the crop handle bounds in normalized visual space — the\n * axis-aligned bounding box (AABB) of the actual image footprint at the\n * current pan/zoom/rotation.\n *\n * Returns the full image AABB without intersecting with the container\n * viewport. The viewport pan mechanism in the Cropper component allows\n * the canvas to scroll so handles can reach the image edge even when it\n * lies outside the visible canvas area.\n *\n * Unlike a static centerline-based calculation, this accounts for the\n * current pan position — if the user pans right, the left bound tightens\n * because the image's left edge has moved right.\n *\n * @param state The current cropper state (crop, zoom, rotation, flip).\n * @param elementSize The fitted (unrotated) image element dimensions in pixels.\n * @param visualSize The visual (rotated) image bounding box in pixels.\n * @return The min/max x and y that a crop rect edge can reach in normalized space.\n */\nexport function getImageCropBounds(\n\tstate: CropperState,\n\telementSize: Size,\n\tvisualSize: Size\n): { minX: number; minY: number; maxX: number; maxY: number } {\n\tif ( ! isValidSize( elementSize ) || ! isValidSize( visualSize ) ) {\n\t\treturn { minX: 0, minY: 0, maxX: 1, maxY: 1 };\n\t}\n\n\tconst safeState = sanitizeCropperState( state );\n\n\t// Build the same CSS matrix as use-transform-style: flip * rotate * zoom.\n\t// Flip is outside rotation so it acts in viewport axes (see createCamera).\n\tconst tx = safeState.pan.x * visualSize.width;\n\tconst ty = safeState.pan.y * visualSize.height;\n\tconst rad = degreesToRadians( safeState.rotation );\n\tconst cos = Math.cos( rad );\n\tconst sin = Math.sin( rad );\n\tconst sx = safeState.flip.horizontal ? -1 : 1;\n\tconst sy = safeState.flip.vertical ? -1 : 1;\n\tconst z = safeState.zoom;\n\tconst ma = sx * cos * z;\n\tconst mb = sy * sin * z;\n\tconst mc = -sx * sin * z;\n\tconst md = sy * cos * z;\n\n\t// Image element corners relative to element center.\n\tconst hw = elementSize.width / 2;\n\tconst hh = elementSize.height / 2;\n\tconst corners = [\n\t\t[ -hw, -hh ],\n\t\t[ hw, -hh ],\n\t\t[ hw, hh ],\n\t\t[ -hw, hh ],\n\t];\n\n\tlet imgMinX = Infinity;\n\tlet imgMaxX = -Infinity;\n\tlet imgMinY = Infinity;\n\tlet imgMaxY = -Infinity;\n\n\tfor ( const [ cx, cy ] of corners ) {\n\t\t// Screen offset from element center after CSS matrix.\n\t\tconst screenX = ma * cx + mc * cy + tx;\n\t\tconst screenY = mb * cx + md * cy + ty;\n\t\t// Convert to normalized visual space.\n\t\t// The element center is at the visual image center, which is at\n\t\t// normalized (0.5, 0.5). So screen offset / visualSize + 0.5.\n\t\tconst nx = screenX / visualSize.width + 0.5;\n\t\tconst ny = screenY / visualSize.height + 0.5;\n\t\timgMinX = Math.min( imgMinX, nx );\n\t\timgMaxX = Math.max( imgMaxX, nx );\n\t\timgMinY = Math.min( imgMinY, ny );\n\t\timgMaxY = Math.max( imgMaxY, ny );\n\t}\n\n\treturn {\n\t\tminX: imgMinX,\n\t\tminY: imgMinY,\n\t\tmaxX: imgMaxX,\n\t\tmaxY: imgMaxY,\n\t};\n}\n\n/**\n * Restricts a crop rectangle so that the rotated, zoomed image can fully cover it.\n * If the crop rect is too large for the current zoom and rotation, it is scaled\n * down proportionally and re-centered.\n *\n * Works in pixel-proportional space where the unrotated image is a×1.\n *\n * @param cropRect The crop rectangle in normalized coordinates.\n * @param zoom The current zoom factor.\n * @param rotation The rotation angle in degrees.\n * @param imageAspectRatio The image width / height ratio.\n * @return The restricted crop rectangle.\n */\nexport function restrictCropRect(\n\tcropRect: NormalizedRect,\n\tzoom: number,\n\trotation: number,\n\timageAspectRatio: number\n): NormalizedRect {\n\t// Guard against non-finite inputs. The reducer normalizes these, but\n\t// programmatic callers or deserialized state could still send NaN/±Infinity\n\t// through here, where it would silently propagate into the crop rect.\n\tconst safeRect = sanitizeRect( cropRect );\n\tconst zoomCandidate = safeBoundedNumber( zoom, 1 );\n\tconst safeZoom = zoomCandidate >= Number.EPSILON ? zoomCandidate : 1;\n\tconst safeRotation = safeBoundedNumber( rotation, 0 );\n\tconst safeAspect = safeBoundedNumber( imageAspectRatio, 1 );\n\tconst aspectRatio = Math.max( safeAspect, Number.EPSILON );\n\t// Crop rect lives in the SNAP-rotation visual bbox (matching the stencil\n\t// layout); projection into the image-local frame uses the TRUE rotation.\n\tconst snapRotation = Math.round( safeRotation / 90 ) * 90;\n\tconst { visualW, visualH } = getVisualDimensions(\n\t\tsnapRotation,\n\t\taspectRatio\n\t);\n\tconst { absC, absS } = getVisualDimensions( safeRotation, aspectRatio );\n\tconst W = safeRect.width;\n\tconst H = safeRect.height;\n\n\t// Crop full-extents in pixel-proportional space, projected to image-local frame.\n\tconst cropWPx = W * visualW;\n\tconst cropHPx = H * visualH;\n\tconst spanAlpha = cropWPx * absC + cropHPx * absS;\n\tconst spanBeta = cropWPx * absS + cropHPx * absC;\n\n\t// Image full-extents at zoom z: (aspectRatio*z, z).\n\tconst limitAlpha = aspectRatio * safeZoom;\n\tconst limitBeta = safeZoom;\n\n\tlet t = 1;\n\tif ( spanAlpha > 0 ) {\n\t\tt = Math.min( t, limitAlpha / spanAlpha );\n\t}\n\tif ( spanBeta > 0 ) {\n\t\tt = Math.min( t, limitBeta / spanBeta );\n\t}\n\tif ( t >= 1 - EPSILON ) {\n\t\t// Crop fits at the current zoom — no size change needed.\n\t\t// Return the sanitized rect (not the raw arg) so non-finite fields\n\t\t// don't slip through this path. Position is handled by restrictPanZoom.\n\t\treturn safeRect;\n\t}\n\tconst newW = W * t;\n\tconst newH = H * t;\n\tconst centerX = safeRect.x + W / 2;\n\tconst centerY = safeRect.y + H / 2;\n\tlet newX = centerX - newW / 2;\n\tlet newY = centerY - newH / 2;\n\tnewX = Math.max( 0, Math.min( newX, 1 - newW ) );\n\tnewY = Math.max( 0, Math.min( newY, 1 - newH ) );\n\treturn { x: newX, y: newY, width: newW, height: newH };\n}\n\n/**\n * Canonical container used internally by restrictPanZoom.\n * Containment is scale-invariant, so the actual size doesn't matter —\n * only the relative geometry between stencil and image matters.\n */\nconst CANONICAL_CONTAINER: Size = { width: 1000, height: 1000 };\n\n// Pre-allocated scratch buffers for restrictPanZoom's hot path (runs on\n// every pointermove) to avoid Float32Array allocation per call.\nconst _scratchMat = mat2d.create();\nconst _scratchVec = vec2.create();\n\n/**\n * Clamps pan and adjusts zoom so that the zoomed, rotated image fully covers\n * the crop rectangle.\n *\n * Uses the camera matrix to project: builds a camera from the candidate state,\n * maps the stencil corners (axis-aligned in the visual bounding box) to world\n * space via the inverse camera, and checks that all world points lie within\n * [0,1]×[0,1]. If any point is outside, computes the minimal pan correction.\n *\n * @param state The current cropper state.\n * @param imageSize The natural size of the image in pixels.\n * @param cropRect The crop rectangle in normalized coordinates.\n * @return The restricted crop pan and zoom values.\n */\nexport function restrictPanZoom(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect\n): { pan: { x: number; y: number }; zoom: number } {\n\t// Algorithm overview:\n\t// 1. Ensure zoom is high enough for the rotated image to cover the crop.\n\t// 2. Build a camera (world→screen matrix) with the candidate state.\n\t// 3. Build a base camera (no pan, zoom=1) to find where the stencil\n\t// sits in screen space — the stencil is anchored to the container,\n\t// not the image, so it doesn't move with pan or scale with zoom.\n\t// 4. Project the 4 stencil corners through the INVERSE of the main\n\t// camera to get world-space positions. A world point outside [0,1]\n\t// means that corner of the stencil isn't covered by the image.\n\t// 5. Compute the minimal translation in world space to push all corners\n\t// back inside [0,1].\n\t// 6. Convert the world-space correction to a pan-field correction by\n\t// mapping it through the camera's linear part back to screen space,\n\t// then dividing by the visual bounds.\n\tconst safeState = sanitizeCropperState( state );\n\tconst safeCropRect = sanitizeRect( cropRect );\n\t// Use `isValidSize` rather than a bare `> 0` check so Infinity dims\n\t// don't drive `Infinity / Infinity = NaN` into `getMinZoomForCover`\n\t// and leak as `zoom: NaN` through the no-correction early return.\n\tconst aspectRatio = isValidSize( imageSize )\n\t\t? imageSize.width / imageSize.height\n\t\t: 1;\n\tconst minZoom = Math.max(\n\t\tABSOLUTE_MIN_ZOOM,\n\t\tgetMinZoomForCover( safeState.rotation, aspectRatio, safeCropRect )\n\t);\n\tconst zoom = Math.min( MAX_ZOOM, Math.max( safeState.zoom, minZoom ) );\n\n\t// Step 2: build camera with candidate pan and corrected zoom.\n\tconst candidateState = { ...safeState, zoom };\n\tconst camera = createCamera(\n\t\tcandidateState,\n\t\tCANONICAL_CONTAINER,\n\t\timageSize\n\t);\n\n\t// Build a base camera (zero pan, zoom=1) to get stencil positions.\n\t// The stencil is anchored to the container and laid out using the\n\t// nearest 90° rotation (matching `getImageFit`), so it's stable\n\t// through fine rotation. CSS zoom only affects the <img> element,\n\t// not the stencil.\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst baseCamera = createCamera(\n\t\t{\n\t\t\t...candidateState,\n\t\t\tpan: { x: 0, y: 0 },\n\t\t\tzoom: 1,\n\t\t\trotation: snapRotation,\n\t\t},\n\t\tCANONICAL_CONTAINER,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\n\t// Stencil corners in screen space (axis-aligned rect within visual bounds).\n\tconst stencilCorners: [ number, number ][] = [\n\t\t[\n\t\t\tvisibleBounds.left + safeCropRect.x * visibleBounds.width,\n\t\t\tvisibleBounds.top + safeCropRect.y * visibleBounds.height,\n\t\t],\n\t\t[\n\t\t\tvisibleBounds.left +\n\t\t\t\t( safeCropRect.x + safeCropRect.width ) * visibleBounds.width,\n\t\t\tvisibleBounds.top + safeCropRect.y * visibleBounds.height,\n\t\t],\n\t\t[\n\t\t\tvisibleBounds.left +\n\t\t\t\t( safeCropRect.x + safeCropRect.width ) * visibleBounds.width,\n\t\t\tvisibleBounds.top +\n\t\t\t\t( safeCropRect.y + safeCropRect.height ) * visibleBounds.height,\n\t\t],\n\t\t[\n\t\t\tvisibleBounds.left + safeCropRect.x * visibleBounds.width,\n\t\t\tvisibleBounds.top +\n\t\t\t\t( safeCropRect.y + safeCropRect.height ) * visibleBounds.height,\n\t\t],\n\t];\n\n\t// Map stencil corners to world space via inverse camera.\n\t// If a world point is outside [0,1], the image doesn't cover that spot.\n\t// Reuse module-level scratch buffers to avoid allocation per frame.\n\tmat2d.invert( _scratchMat, camera );\n\n\tlet minWx = Infinity;\n\tlet maxWx = -Infinity;\n\tlet minWy = Infinity;\n\tlet maxWy = -Infinity;\n\n\tfor ( const corner of stencilCorners ) {\n\t\tvec2.transformMat2d( _scratchVec, corner, _scratchMat );\n\t\tconst w = _scratchVec;\n\t\tif ( w[ 0 ] < minWx ) {\n\t\t\tminWx = w[ 0 ];\n\t\t}\n\t\tif ( w[ 0 ] > maxWx ) {\n\t\t\tmaxWx = w[ 0 ];\n\t\t}\n\t\tif ( w[ 1 ] < minWy ) {\n\t\t\tminWy = w[ 1 ];\n\t\t}\n\t\tif ( w[ 1 ] > maxWy ) {\n\t\t\tmaxWy = w[ 1 ];\n\t\t}\n\t}\n\n\t// If all world points are in [0,1], no correction needed.\n\tif (\n\t\tminWx >= -EPSILON &&\n\t\tmaxWx <= 1 + EPSILON &&\n\t\tminWy >= -EPSILON &&\n\t\tmaxWy <= 1 + EPSILON\n\t) {\n\t\treturn { pan: safeState.pan, zoom };\n\t}\n\n\t// Compute world-space correction needed.\n\t// If minWx < 0, we need to shift world points right by |minWx|.\n\t// If maxWx > 1, we need to shift world points left by (maxWx - 1).\n\t// If both, we're over-constrained (crop too big) — getMinZoomForCover\n\t// should have prevented this.\n\tlet dwx = 0;\n\tlet dwy = 0;\n\n\tif ( minWx < 0 && maxWx <= 1 + EPSILON ) {\n\t\tdwx = -minWx;\n\t} else if ( maxWx > 1 && minWx >= -EPSILON ) {\n\t\tdwx = 1 - maxWx;\n\t} else if ( minWx < 0 && maxWx > 1 ) {\n\t\t// Over-constrained: center it.\n\t\tdwx = ( 1 - maxWx - minWx ) / 2;\n\t}\n\n\tif ( minWy < 0 && maxWy <= 1 + EPSILON ) {\n\t\tdwy = -minWy;\n\t} else if ( maxWy > 1 && minWy >= -EPSILON ) {\n\t\tdwy = 1 - maxWy;\n\t} else if ( minWy < 0 && maxWy > 1 ) {\n\t\tdwy = ( 1 - maxWy - minWy ) / 2;\n\t}\n\n\t// Convert world-space correction to screen-space correction.\n\t// The camera's 2×2 linear part (indices [0,1,2,3]) maps world deltas\n\t// to screen deltas: screenDelta = linear * worldDelta.\n\tconst dsx = camera[ 0 ] * dwx + camera[ 2 ] * dwy;\n\tconst dsy = camera[ 1 ] * dwx + camera[ 3 ] * dwy;\n\n\t// Convert screen-space correction to pan-field correction.\n\t// Pan in screen pixels = pan.x * visualW, pan.y * visualH.\n\t// The correction is subtractive: a positive world shift (dw > 0) means\n\t// the image needs to move opposite to pan direction, so pan decreases.\n\tconst newPanX =\n\t\tsafeState.pan.x -\n\t\t( visibleBounds.width > 0 ? dsx / visibleBounds.width : 0 );\n\tconst newPanY =\n\t\tsafeState.pan.y -\n\t\t( visibleBounds.height > 0 ? dsy / visibleBounds.height : 0 );\n\n\treturn {\n\t\tpan: { x: newPanX, y: newPanY },\n\t\tzoom,\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,OAAO,YAAY;AAE5B,SAAS,mBAAmB,UAAU,gBAAgB;AACtD,SAAS,wBAAwB;AACjC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,cAAc,gBAAgB,wBAAwB;AAG/D,IAAM,UAAU;AAUhB,SAAS,oBACR,UACA,kBACmE;AACnE,QAAM,MAAM,iBAAkB,QAAS;AACvC,QAAM,OAAO,KAAK,IAAK,KAAK,IAAK,GAAI,CAAE;AACvC,QAAM,OAAO,KAAK,IAAK,KAAK,IAAK,GAAI,CAAE;AACvC,QAAM,OAAO,eAAgB,kBAAkB,GAAG,QAAS;AAC3D,SAAO;AAAA,IACN,SAAS,KAAK;AAAA,IACd,SAAS,KAAK;AAAA,IACd;AAAA,IACA;AAAA,EACD;AACD;AAiBA,SAAS,mBACR,UACA,kBACA,UACS;AACT,QAAM,cAAc,KAAK,IAAK,kBAAkB,OAAO,OAAQ;AAI/D,QAAM,eAAe,KAAK,MAAO,WAAW,EAAG,IAAI;AACnD,QAAM,EAAE,SAAS,aAAa,SAAS,YAAY,IAAI;AAAA,IACtD;AAAA,IACA;AAAA,EACD;AAGA,QAAM,EAAE,MAAM,KAAK,IAAI,oBAAqB,UAAU,WAAY;AAGlE,QAAM,YAAc,SAAS,QAAQ,cAAgB;AACrD,QAAM,YAAc,SAAS,SAAS,cAAgB;AAGtD,QAAM,YAAY,YAAY,OAAO,YAAY;AACjD,QAAM,WAAW,YAAY,OAAO,YAAY;AAIhD,QAAM,gBAAkB,IAAI,YAAc;AAC1C,QAAM,eAAe,IAAI;AAEzB,SAAO,KAAK,IAAK,eAAe,YAAa;AAC9C;AAWO,SAAS,WAAY,OAA8B;AACzD,M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C,WACV,SAAS,IAAI,SACZ;AACD,WAAO,EAAE,KAAK,UAAU,KAAK,KAAK;AAAA,EACnC;AAOA,MAAI,MAAM;AACV,MAAI,MAAM;AAEV,MAAK,QAAQ,KAAK,SAAS,IAAI,SAAU;AACxC,UAAM,CAAC;AAAA,EACR,WAAY,QAAQ,KAAK,SAAS,CAAC,SAAU;AAC5C,UAAM,IAAI;AAAA,EACX,WAAY,QAAQ,KAAK,QAAQ,GAAI;AAEpC,WAAQ,IAAI,QAAQ,SAAU;AAAA,EAC/B;AAEA,MAAK,QAAQ,KAAK,SAAS,IAAI,SAAU;AACxC,UAAM,CAAC;AAAA,EACR,WAAY,QAAQ,KAAK,SAAS,CAAC,SAAU;AAC5C,UAAM,IAAI;AAAA,EACX,WAAY,QAAQ,KAAK,QAAQ,GAAI;AACpC,WAAQ,IAAI,QAAQ,SAAU;AAAA,EAC/B;AAKA,QAAM,MAAM,OAAQ,CAAE,IAAI,MAAM,OAAQ,CAAE,IAAI;AAC9C,QAAM,MAAM,OAAQ,CAAE,IAAI,MAAM,OAAQ,CAAE,IAAI;AAM9C,QAAM,UACL,UAAU,IAAI,KACZ,cAAc,QAAQ,IAAI,MAAM,cAAc,QAAQ;AACzD,QAAM,UACL,UAAU,IAAI,KACZ,cAAc,SAAS,IAAI,MAAM,cAAc,SAAS;AAE3D,SAAO;AAAA,IACN,KAAK,EAAE,GAAG,SAAS,GAAG,QAAQ;AAAA,IAC9B;AAAA,EACD;AACD;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"containment.mjs","names":[],"sources":["../../../src/image-editor/core/containment.ts"],"sourcesContent":["import { mat2d, vec2 } from 'gl-matrix';\nimport type { CropperState, NormalizedRect, Size } from './types';\nimport { ABSOLUTE_MIN_ZOOM, MAX_ZOOM, MIN_ZOOM } from './constants';\nimport { degreesToRadians } from './math/rotation';\nimport {\n\tisValidSize,\n\tsafeBoundedNumber,\n\tsanitizeCropperState,\n\tsanitizeRect,\n} from './math/sanitize';\nimport { createCamera, getRotatedBBox, getVisibleBounds } from './camera';\n\n/** Floating-point epsilon for \"close enough to equal\" comparisons. */\nconst EPSILON = 1e-9;\n\n/**\n * Compute the visual (rotated) bounding-box dimensions in pixel-proportional\n * units where the unrotated image is `a × 1` (renderedW = a, renderedH = 1).\n *\n * @param rotation Rotation angle in degrees.\n * @param imageAspectRatio Image width / height ratio.\n * @return Visual dimensions and trig helpers.\n */\nfunction getVisualDimensions(\n\trotation: number,\n\timageAspectRatio: number\n): { visualW: number; visualH: number; absC: number; absS: number } {\n\tconst rad = degreesToRadians( rotation );\n\tconst absC = Math.abs( Math.cos( rad ) );\n\tconst absS = Math.abs( Math.sin( rad ) );\n\tconst bbox = getRotatedBBox( imageAspectRatio, 1, rotation );\n\treturn {\n\t\tvisualW: bbox.width,\n\t\tvisualH: bbox.height,\n\t\tabsC,\n\t\tabsS,\n\t};\n}\n\n/**\n * The geometric minimum zoom required for the rotated image to fully cover the\n * crop rectangle. Works in pixel-proportional space where the unrotated image\n * is a×1; the crop rect lives in visual-normalized space and is scaled by the\n * rotation-dependent visual dimensions before projecting into the image-local\n * frame.\n *\n * Returns 0 for a degenerate (zero-area) crop — callers compose the result\n * with their own floor (typically the current zoom or `MIN_ZOOM`).\n *\n * @param rotation Rotation angle in degrees.\n * @param imageAspectRatio Image width / height ratio.\n * @param cropRect The crop rectangle in normalized coordinates.\n * @return The minimum zoom factor needed for coverage.\n */\nfunction getMinZoomForCover(\n\trotation: number,\n\timageAspectRatio: number,\n\tcropRect: NormalizedRect\n): number {\n\tconst aspectRatio = Math.max( imageAspectRatio, Number.EPSILON );\n\t// The crop rect lives in the SNAP-rotation visual bbox — that's how the\n\t// stencil and preview position it (see createCamera / getImageFit). Use\n\t// the snap-rotation dimensions to convert crop fractions to pixels.\n\tconst snapRotation = Math.round( rotation / 90 ) * 90;\n\tconst { visualW: snapVisualW, visualH: snapVisualH } = getVisualDimensions(\n\t\tsnapRotation,\n\t\taspectRatio\n\t);\n\t// Projection into the image-local frame uses the TRUE rotation angle —\n\t// that's the actual orientation of the image under the stencil.\n\tconst { absC, absS } = getVisualDimensions( rotation, aspectRatio );\n\n\t// Crop half-extents in pixel-proportional space.\n\tconst cropHalfW = ( cropRect.width * snapVisualW ) / 2;\n\tconst cropHalfH = ( cropRect.height * snapVisualH ) / 2;\n\n\t// AABB of the crop rect projected into the image-local (unrotated) frame.\n\tconst spanAlpha = cropHalfW * absC + cropHalfH * absS;\n\tconst spanBeta = cropHalfW * absS + cropHalfH * absC;\n\n\t// Image half-extents at zoom z: (aspectRatio*z/2, z/2).\n\t// Coverage requires: aspectRatio*z/2 >= spanAlpha AND z/2 >= spanBeta.\n\tconst zoomFromAlpha = ( 2 * spanAlpha ) / aspectRatio;\n\tconst zoomFromBeta = 2 * spanBeta;\n\n\treturn Math.max( zoomFromAlpha, zoomFromBeta );\n}\n\n/**\n * Resolves the effective minimum zoom for the current state — the coverage-\n * aware floor when an image is loaded, falling back to `MIN_ZOOM` otherwise.\n * Used by both the cropper's interaction layer and the zoom slider so the\n * floor stays consistent across surfaces.\n *\n * @param state The current cropper state.\n * @return The minimum zoom factor.\n */\nexport function getMinZoom( state: CropperState ): number {\n\tif ( ! state.image ) {\n\t\treturn MIN_ZOOM;\n\t}\n\tconst imageSize = {\n\t\twidth: state.image.naturalWidth,\n\t\theight: state.image.naturalHeight,\n\t};\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn MIN_ZOOM;\n\t}\n\tconst safeState = sanitizeCropperState( state );\n\tconst aspectRatio = imageSize.width / imageSize.height;\n\treturn Math.max(\n\t\tABSOLUTE_MIN_ZOOM,\n\t\tgetMinZoomForCover(\n\t\t\tsafeState.rotation,\n\t\t\taspectRatio,\n\t\t\tsafeState.cropRect\n\t\t)\n\t);\n}\n\n/**\n * Compute the crop handle bounds in normalized visual space — the\n * axis-aligned bounding box (AABB) of the actual image footprint at the\n * current pan/zoom/rotation.\n *\n * Returns the full image AABB without intersecting with the container\n * viewport. The viewport pan mechanism in the Cropper component allows\n * the canvas to scroll so handles can reach the image edge even when it\n * lies outside the visible canvas area.\n *\n * Unlike a static centerline-based calculation, this accounts for the\n * current pan position — if the user pans right, the left bound tightens\n * because the image's left edge has moved right.\n *\n * @param state The current cropper state (crop, zoom, rotation, flip).\n * @param elementSize The fitted (unrotated) image element dimensions in pixels.\n * @param visualSize The visual (rotated) image bounding box in pixels.\n * @return The min/max x and y that a crop rect edge can reach in normalized space.\n */\nexport function getImageCropBounds(\n\tstate: CropperState,\n\telementSize: Size,\n\tvisualSize: Size\n): { minX: number; minY: number; maxX: number; maxY: number } {\n\tif ( ! isValidSize( elementSize ) || ! isValidSize( visualSize ) ) {\n\t\treturn { minX: 0, minY: 0, maxX: 1, maxY: 1 };\n\t}\n\n\tconst safeState = sanitizeCropperState( state );\n\n\t// Build the same CSS matrix as use-transform-style: flip * rotate * zoom.\n\t// Flip is outside rotation so it acts in viewport axes (see createCamera).\n\tconst tx = safeState.pan.x * visualSize.width;\n\tconst ty = safeState.pan.y * visualSize.height;\n\tconst rad = degreesToRadians( safeState.rotation );\n\tconst cos = Math.cos( rad );\n\tconst sin = Math.sin( rad );\n\tconst sx = safeState.flip.horizontal ? -1 : 1;\n\tconst sy = safeState.flip.vertical ? -1 : 1;\n\tconst z = safeState.zoom;\n\tconst ma = sx * cos * z;\n\tconst mb = sy * sin * z;\n\tconst mc = -sx * sin * z;\n\tconst md = sy * cos * z;\n\n\t// Image element corners relative to element center.\n\tconst hw = elementSize.width / 2;\n\tconst hh = elementSize.height / 2;\n\tconst corners = [\n\t\t[ -hw, -hh ],\n\t\t[ hw, -hh ],\n\t\t[ hw, hh ],\n\t\t[ -hw, hh ],\n\t];\n\n\tlet imgMinX = Infinity;\n\tlet imgMaxX = -Infinity;\n\tlet imgMinY = Infinity;\n\tlet imgMaxY = -Infinity;\n\n\tfor ( const [ cx, cy ] of corners ) {\n\t\t// Screen offset from element center after CSS matrix.\n\t\tconst screenX = ma * cx + mc * cy + tx;\n\t\tconst screenY = mb * cx + md * cy + ty;\n\t\t// Convert to normalized visual space.\n\t\t// The element center is at the visual image center, which is at\n\t\t// normalized (0.5, 0.5). So screen offset / visualSize + 0.5.\n\t\tconst nx = screenX / visualSize.width + 0.5;\n\t\tconst ny = screenY / visualSize.height + 0.5;\n\t\timgMinX = Math.min( imgMinX, nx );\n\t\timgMaxX = Math.max( imgMaxX, nx );\n\t\timgMinY = Math.min( imgMinY, ny );\n\t\timgMaxY = Math.max( imgMaxY, ny );\n\t}\n\n\treturn {\n\t\tminX: imgMinX,\n\t\tminY: imgMinY,\n\t\tmaxX: imgMaxX,\n\t\tmaxY: imgMaxY,\n\t};\n}\n\n/**\n * Restricts a crop rectangle so that the rotated, zoomed image can fully cover it.\n * If the crop rect is too large for the current zoom and rotation, it is scaled\n * down proportionally and re-centered.\n *\n * Works in pixel-proportional space where the unrotated image is a×1.\n *\n * @param cropRect The crop rectangle in normalized coordinates.\n * @param zoom The current zoom factor.\n * @param rotation The rotation angle in degrees.\n * @param imageAspectRatio The image width / height ratio.\n * @return The restricted crop rectangle.\n */\nexport function restrictCropRect(\n\tcropRect: NormalizedRect,\n\tzoom: number,\n\trotation: number,\n\timageAspectRatio: number\n): NormalizedRect {\n\t// Guard against non-finite inputs. The reducer normalizes these, but\n\t// programmatic callers or deserialized state could still send NaN/±Infinity\n\t// through here, where it would silently propagate into the crop rect.\n\tconst safeRect = sanitizeRect( cropRect );\n\tconst zoomCandidate = safeBoundedNumber( zoom, 1 );\n\tconst safeZoom = zoomCandidate >= Number.EPSILON ? zoomCandidate : 1;\n\tconst safeRotation = safeBoundedNumber( rotation, 0 );\n\tconst safeAspect = safeBoundedNumber( imageAspectRatio, 1 );\n\tconst aspectRatio = Math.max( safeAspect, Number.EPSILON );\n\t// Crop rect lives in the SNAP-rotation visual bbox (matching the stencil\n\t// layout); projection into the image-local frame uses the TRUE rotation.\n\tconst snapRotation = Math.round( safeRotation / 90 ) * 90;\n\tconst { visualW, visualH } = getVisualDimensions(\n\t\tsnapRotation,\n\t\taspectRatio\n\t);\n\tconst { absC, absS } = getVisualDimensions( safeRotation, aspectRatio );\n\tconst W = safeRect.width;\n\tconst H = safeRect.height;\n\n\t// Crop full-extents in pixel-proportional space, projected to image-local frame.\n\tconst cropWPx = W * visualW;\n\tconst cropHPx = H * visualH;\n\tconst spanAlpha = cropWPx * absC + cropHPx * absS;\n\tconst spanBeta = cropWPx * absS + cropHPx * absC;\n\n\t// Image full-extents at zoom z: (aspectRatio*z, z).\n\tconst limitAlpha = aspectRatio * safeZoom;\n\tconst limitBeta = safeZoom;\n\n\tlet t = 1;\n\tif ( spanAlpha > 0 ) {\n\t\tt = Math.min( t, limitAlpha / spanAlpha );\n\t}\n\tif ( spanBeta > 0 ) {\n\t\tt = Math.min( t, limitBeta / spanBeta );\n\t}\n\tif ( t >= 1 - EPSILON ) {\n\t\t// Crop fits at the current zoom — no size change needed.\n\t\t// Return the sanitized rect (not the raw arg) so non-finite fields\n\t\t// don't slip through this path. Position is handled by restrictPanZoom.\n\t\treturn safeRect;\n\t}\n\tconst newW = W * t;\n\tconst newH = H * t;\n\tconst centerX = safeRect.x + W / 2;\n\tconst centerY = safeRect.y + H / 2;\n\tlet newX = centerX - newW / 2;\n\tlet newY = centerY - newH / 2;\n\tnewX = Math.max( 0, Math.min( newX, 1 - newW ) );\n\tnewY = Math.max( 0, Math.min( newY, 1 - newH ) );\n\treturn { x: newX, y: newY, width: newW, height: newH };\n}\n\n/**\n * Canonical container used internally by restrictPanZoom.\n * Containment is scale-invariant, so the actual size doesn't matter —\n * only the relative geometry between stencil and image matters.\n */\nconst CANONICAL_CONTAINER: Size = { width: 1000, height: 1000 };\n\n// Pre-allocated scratch buffers for restrictPanZoom's hot path (runs on\n// every pointermove) to avoid Float32Array allocation per call.\nconst _scratchMat = mat2d.create();\nconst _scratchVec = vec2.create();\n\n/**\n * Clamps pan and adjusts zoom so that the zoomed, rotated image fully covers\n * the crop rectangle.\n *\n * Uses the camera matrix to project: builds a camera from the candidate state,\n * maps the stencil corners (axis-aligned in the visual bounding box) to world\n * space via the inverse camera, and checks that all world points lie within\n * [0,1]×[0,1]. If any point is outside, computes the minimal pan correction.\n *\n * @param state The current cropper state.\n * @param imageSize The natural size of the image in pixels.\n * @param cropRect The crop rectangle in normalized coordinates.\n * @return The restricted crop pan and zoom values.\n */\nexport function restrictPanZoom(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect\n): { pan: { x: number; y: number }; zoom: number } {\n\t// Algorithm overview:\n\t// 1. Ensure zoom is high enough for the rotated image to cover the crop.\n\t// 2. Build a camera (world→screen matrix) with the candidate state.\n\t// 3. Build a base camera (no pan, zoom=1) to find where the stencil\n\t// sits in screen space — the stencil is anchored to the container,\n\t// not the image, so it doesn't move with pan or scale with zoom.\n\t// 4. Project the 4 stencil corners through the INVERSE of the main\n\t// camera to get world-space positions. A world point outside [0,1]\n\t// means that corner of the stencil isn't covered by the image.\n\t// 5. Compute the minimal translation in world space to push all corners\n\t// back inside [0,1].\n\t// 6. Convert the world-space correction to a pan-field correction by\n\t// mapping it through the camera's linear part back to screen space,\n\t// then dividing by the visual bounds.\n\tconst safeState = sanitizeCropperState( state );\n\tconst safeCropRect = sanitizeRect( cropRect );\n\t// Use `isValidSize` rather than a bare `> 0` check so Infinity dims\n\t// don't drive `Infinity / Infinity = NaN` into `getMinZoomForCover`\n\t// and leak as `zoom: NaN` through the no-correction early return.\n\tconst aspectRatio = isValidSize( imageSize )\n\t\t? imageSize.width / imageSize.height\n\t\t: 1;\n\tconst minZoom = Math.max(\n\t\tABSOLUTE_MIN_ZOOM,\n\t\tgetMinZoomForCover( safeState.rotation, aspectRatio, safeCropRect )\n\t);\n\tconst zoom = Math.min( MAX_ZOOM, Math.max( safeState.zoom, minZoom ) );\n\n\t// Step 2: build camera with candidate pan and corrected zoom.\n\tconst candidateState = { ...safeState, zoom };\n\tconst camera = createCamera(\n\t\tcandidateState,\n\t\tCANONICAL_CONTAINER,\n\t\timageSize\n\t);\n\n\t// Build a base camera (zero pan, zoom=1) to get stencil positions.\n\t// The stencil is anchored to the container and laid out using the\n\t// nearest 90° rotation (matching `getImageFit`), so it's stable\n\t// through fine rotation. CSS zoom only affects the <img> element,\n\t// not the stencil.\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst baseCamera = createCamera(\n\t\t{\n\t\t\t...candidateState,\n\t\t\tpan: { x: 0, y: 0 },\n\t\t\tzoom: 1,\n\t\t\trotation: snapRotation,\n\t\t},\n\t\tCANONICAL_CONTAINER,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\n\t// Stencil corners in screen space (axis-aligned rect within visual bounds).\n\tconst stencilCorners: [ number, number ][] = [\n\t\t[\n\t\t\tvisibleBounds.left + safeCropRect.x * visibleBounds.width,\n\t\t\tvisibleBounds.top + safeCropRect.y * visibleBounds.height,\n\t\t],\n\t\t[\n\t\t\tvisibleBounds.left +\n\t\t\t\t( safeCropRect.x + safeCropRect.width ) * visibleBounds.width,\n\t\t\tvisibleBounds.top + safeCropRect.y * visibleBounds.height,\n\t\t],\n\t\t[\n\t\t\tvisibleBounds.left +\n\t\t\t\t( safeCropRect.x + safeCropRect.width ) * visibleBounds.width,\n\t\t\tvisibleBounds.top +\n\t\t\t\t( safeCropRect.y + safeCropRect.height ) * visibleBounds.height,\n\t\t],\n\t\t[\n\t\t\tvisibleBounds.left + safeCropRect.x * visibleBounds.width,\n\t\t\tvisibleBounds.top +\n\t\t\t\t( safeCropRect.y + safeCropRect.height ) * visibleBounds.height,\n\t\t],\n\t];\n\n\t// Map stencil corners to world space via inverse camera.\n\t// If a world point is outside [0,1], the image doesn't cover that spot.\n\t// Reuse module-level scratch buffers to avoid allocation per frame.\n\tmat2d.invert( _scratchMat, camera );\n\n\tlet minWx = Infinity;\n\tlet maxWx = -Infinity;\n\tlet minWy = Infinity;\n\tlet maxWy = -Infinity;\n\n\tfor ( const corner of stencilCorners ) {\n\t\tvec2.transformMat2d( _scratchVec, corner, _scratchMat );\n\t\tconst w = _scratchVec;\n\t\tif ( w[ 0 ] < minWx ) {\n\t\t\tminWx = w[ 0 ];\n\t\t}\n\t\tif ( w[ 0 ] > maxWx ) {\n\t\t\tmaxWx = w[ 0 ];\n\t\t}\n\t\tif ( w[ 1 ] < minWy ) {\n\t\t\tminWy = w[ 1 ];\n\t\t}\n\t\tif ( w[ 1 ] > maxWy ) {\n\t\t\tmaxWy = w[ 1 ];\n\t\t}\n\t}\n\n\t// If all world points are in [0,1], no correction needed.\n\tif (\n\t\tminWx >= -EPSILON &&\n\t\tmaxWx <= 1 + EPSILON &&\n\t\tminWy >= -EPSILON &&\n\t\tmaxWy <= 1 + EPSILON\n\t) {\n\t\treturn { pan: safeState.pan, zoom };\n\t}\n\n\t// Compute world-space correction needed.\n\t// If minWx < 0, we need to shift world points right by |minWx|.\n\t// If maxWx > 1, we need to shift world points left by (maxWx - 1).\n\t// If both, we're over-constrained (crop too big) — getMinZoomForCover\n\t// should have prevented this.\n\tlet dwx = 0;\n\tlet dwy = 0;\n\n\tif ( minWx < 0 && maxWx <= 1 + EPSILON ) {\n\t\tdwx = -minWx;\n\t} else if ( maxWx > 1 && minWx >= -EPSILON ) {\n\t\tdwx = 1 - maxWx;\n\t} else if ( minWx < 0 && maxWx > 1 ) {\n\t\t// Over-constrained: center it.\n\t\tdwx = ( 1 - maxWx - minWx ) / 2;\n\t}\n\n\tif ( minWy < 0 && maxWy <= 1 + EPSILON ) {\n\t\tdwy = -minWy;\n\t} else if ( maxWy > 1 && minWy >= -EPSILON ) {\n\t\tdwy = 1 - maxWy;\n\t} else if ( minWy < 0 && maxWy > 1 ) {\n\t\tdwy = ( 1 - maxWy - minWy ) / 2;\n\t}\n\n\t// Convert world-space correction to screen-space correction.\n\t// The camera's 2×2 linear part (indices [0,1,2,3]) maps world deltas\n\t// to screen deltas: screenDelta = linear * worldDelta.\n\tconst dsx = camera[ 0 ] * dwx + camera[ 2 ] * dwy;\n\tconst dsy = camera[ 1 ] * dwx + camera[ 3 ] * dwy;\n\n\t// Convert screen-space correction to pan-field correction.\n\t// Pan in screen pixels = pan.x * visualW, pan.y * visualH.\n\t// The correction is subtractive: a positive world shift (dw > 0) means\n\t// the image needs to move opposite to pan direction, so pan decreases.\n\tconst newPanX =\n\t\tsafeState.pan.x -\n\t\t( visibleBounds.width > 0 ? dsx / visibleBounds.width : 0 );\n\tconst newPanY =\n\t\tsafeState.pan.y -\n\t\t( visibleBounds.height > 0 ? dsy / visibleBounds.height : 0 );\n\n\treturn {\n\t\tpan: { x: newPanX, y: newPanY },\n\t\tzoom,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;AAuBA,SAAS,oBACR,UACA,kBACmE;CACnE,MAAM,MAAM,iBAAkB,QAAS;CACvC,MAAM,OAAO,KAAK,IAAK,KAAK,IAAK,GAAI,CAAE;CACvC,MAAM,OAAO,KAAK,IAAK,KAAK,IAAK,GAAI,CAAE;CACvC,MAAM,OAAO,eAAgB,kBAAkB,GAAG,QAAS;CAC3D,OAAO;EACN,SAAS,KAAK;EACd,SAAS,KAAK;EACd;EACA;CACD;AACD;;;;;;;;;;;;;;;;AAiBA,SAAS,mBACR,UACA,kBACA,UACS;CACT,MAAM,cAAc,KAAK,IAAK,kBAAkB,OAAO,OAAQ;CAK/D,MAAM,EAAE,SAAS,aAAa,SAAS,gBAAgB,oBADlC,KAAK,MAAO,WAAW,EAAG,IAAI,IAGlD,WACD;CAGA,MAAM,EAAE,MAAM,SAAS,oBAAqB,UAAU,WAAY;CAGlE,MAAM,YAAc,SAAS,QAAQ,cAAgB;CACrD,MAAM,YAAc,SAAS,SAAS,cAAgB;CAGtD,MAAM,YAAY,YAAY,OAAO,YAAY;CACjD,MAAM,WAAW,YAAY,OAAO,YAAY;CAIhD,MAAM,gBAAkB,IAAI,YAAc;CAC1C,MAAM,eAAe,IAAI;CAEzB,OAAO,KAAK,IAAK,eAAe,YAAa;AAC9C;;;;;;;;;;AAWA,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