@wordpress/media-editor 0.8.1-next.v.202605131032.0 → 0.10.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.
Files changed (292) hide show
  1. package/build/components/media-editor/index.cjs +34 -135
  2. package/build/components/media-editor/index.cjs.map +3 -3
  3. package/build/components/media-editor/use-crop-options.cjs +87 -0
  4. package/build/components/media-editor/use-crop-options.cjs.map +7 -0
  5. package/build/components/media-editor/use-save-media-editor.cjs +185 -0
  6. package/build/components/media-editor/use-save-media-editor.cjs.map +7 -0
  7. package/build/components/media-editor-canvas/index.cjs +32 -11
  8. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  9. package/build/components/media-editor-crop-panel/index.cjs +66 -83
  10. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  11. package/build/components/media-editor-modal/index.cjs +40 -11
  12. package/build/components/media-editor-modal/index.cjs.map +3 -3
  13. package/build/components/media-editor-toolbar/index.cjs +7 -3
  14. package/build/components/media-editor-toolbar/index.cjs.map +2 -2
  15. package/build/components/media-form/index.cjs +19 -1
  16. package/build/components/media-form/index.cjs.map +3 -3
  17. package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
  18. package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
  19. package/build/components/rotation-ruler/index.cjs +8 -2
  20. package/build/components/rotation-ruler/index.cjs.map +2 -2
  21. package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
  22. package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
  23. package/build/image-editor/core/camera.cjs +29 -11
  24. package/build/image-editor/core/camera.cjs.map +2 -2
  25. package/build/image-editor/core/constants.cjs +6 -0
  26. package/build/image-editor/core/constants.cjs.map +2 -2
  27. package/build/image-editor/core/containment.cjs +71 -34
  28. package/build/image-editor/core/containment.cjs.map +2 -2
  29. package/build/image-editor/core/crop-rect.cjs +48 -0
  30. package/build/image-editor/core/crop-rect.cjs.map +7 -0
  31. package/build/image-editor/core/index.cjs +12 -0
  32. package/build/image-editor/core/index.cjs.map +2 -2
  33. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  34. package/build/image-editor/core/math/sanitize.cjs +92 -0
  35. package/build/image-editor/core/math/sanitize.cjs.map +7 -0
  36. package/build/image-editor/core/setter-helpers.cjs +64 -0
  37. package/build/image-editor/core/setter-helpers.cjs.map +7 -0
  38. package/build/image-editor/core/source-region.cjs +16 -14
  39. package/build/image-editor/core/source-region.cjs.map +2 -2
  40. package/build/image-editor/core/state.cjs +20 -10
  41. package/build/image-editor/core/state.cjs.map +2 -2
  42. package/build/image-editor/core/stencil-math.cjs +31 -20
  43. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  44. package/build/image-editor/core/transform-style.cjs +12 -6
  45. package/build/image-editor/core/transform-style.cjs.map +2 -2
  46. package/build/image-editor/core/types.cjs.map +1 -1
  47. package/build/image-editor/index.cjs +12 -2
  48. package/build/image-editor/index.cjs.map +2 -2
  49. package/build/image-editor/react/components/cropper-provider.cjs +2 -2
  50. package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
  51. package/build/image-editor/react/components/cropper.cjs +121 -48
  52. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  53. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
  54. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
  55. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +20 -10
  56. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  57. package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
  58. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/index.cjs +6 -3
  60. package/build/image-editor/react/hooks/index.cjs.map +2 -2
  61. package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
  62. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  63. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +113 -0
  64. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
  65. package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
  66. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  67. package/build/state/composite-reducer.cjs +115 -0
  68. package/build/state/composite-reducer.cjs.map +7 -0
  69. package/build/state/index.cjs +48 -0
  70. package/build/state/index.cjs.map +7 -0
  71. package/build/state/media-editor-state-provider.cjs +58 -0
  72. package/build/state/media-editor-state-provider.cjs.map +7 -0
  73. package/build/state/types.cjs +34 -0
  74. package/build/state/types.cjs.map +7 -0
  75. package/build/state/use-media-editor-state.cjs +298 -0
  76. package/build/state/use-media-editor-state.cjs.map +7 -0
  77. package/build-module/components/media-editor/index.mjs +40 -140
  78. package/build-module/components/media-editor/index.mjs.map +2 -2
  79. package/build-module/components/media-editor/use-crop-options.mjs +61 -0
  80. package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
  81. package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
  82. package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
  83. package/build-module/components/media-editor-canvas/index.mjs +33 -12
  84. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  85. package/build-module/components/media-editor-crop-panel/index.mjs +68 -83
  86. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  87. package/build-module/components/media-editor-modal/index.mjs +40 -11
  88. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  89. package/build-module/components/media-editor-toolbar/index.mjs +7 -3
  90. package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
  91. package/build-module/components/media-form/index.mjs +9 -1
  92. package/build-module/components/media-form/index.mjs.map +2 -2
  93. package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
  94. package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
  95. package/build-module/components/rotation-ruler/index.mjs +8 -2
  96. package/build-module/components/rotation-ruler/index.mjs.map +2 -2
  97. package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
  98. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
  99. package/build-module/image-editor/core/camera.mjs +34 -11
  100. package/build-module/image-editor/core/camera.mjs.map +2 -2
  101. package/build-module/image-editor/core/constants.mjs +4 -0
  102. package/build-module/image-editor/core/constants.mjs.map +2 -2
  103. package/build-module/image-editor/core/containment.mjs +75 -34
  104. package/build-module/image-editor/core/containment.mjs.map +2 -2
  105. package/build-module/image-editor/core/crop-rect.mjs +23 -0
  106. package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
  107. package/build-module/image-editor/core/index.mjs +12 -0
  108. package/build-module/image-editor/core/index.mjs.map +2 -2
  109. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  110. package/build-module/image-editor/core/math/sanitize.mjs +63 -0
  111. package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
  112. package/build-module/image-editor/core/setter-helpers.mjs +39 -0
  113. package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
  114. package/build-module/image-editor/core/source-region.mjs +16 -14
  115. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  116. package/build-module/image-editor/core/state.mjs +25 -11
  117. package/build-module/image-editor/core/state.mjs.map +2 -2
  118. package/build-module/image-editor/core/stencil-math.mjs +30 -19
  119. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  120. package/build-module/image-editor/core/transform-style.mjs +12 -6
  121. package/build-module/image-editor/core/transform-style.mjs.map +2 -2
  122. package/build-module/image-editor/index.mjs +11 -2
  123. package/build-module/image-editor/index.mjs.map +2 -2
  124. package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
  125. package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
  126. package/build-module/image-editor/react/components/cropper.mjs +122 -49
  127. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  128. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
  129. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
  130. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +20 -10
  131. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  132. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
  133. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
  134. package/build-module/image-editor/react/hooks/index.mjs +4 -2
  135. package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
  136. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
  137. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  138. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
  139. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
  140. package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
  141. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  142. package/build-module/state/composite-reducer.mjs +94 -0
  143. package/build-module/state/composite-reducer.mjs.map +7 -0
  144. package/build-module/state/index.mjs +26 -0
  145. package/build-module/state/index.mjs.map +7 -0
  146. package/build-module/state/media-editor-state-provider.mjs +34 -0
  147. package/build-module/state/media-editor-state-provider.mjs.map +7 -0
  148. package/build-module/state/types.mjs +9 -0
  149. package/build-module/state/types.mjs.map +7 -0
  150. package/build-module/state/use-media-editor-state.mjs +289 -0
  151. package/build-module/state/use-media-editor-state.mjs.map +7 -0
  152. package/build-style/style-rtl.css +46 -9
  153. package/build-style/style.css +46 -9
  154. package/build-types/components/media-editor/index.d.ts +4 -7
  155. package/build-types/components/media-editor/index.d.ts.map +1 -1
  156. package/build-types/components/media-editor/use-crop-options.d.ts +37 -0
  157. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
  158. package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
  159. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
  160. package/build-types/components/media-editor-canvas/index.d.ts +5 -10
  161. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  162. package/build-types/components/media-editor-crop-panel/index.d.ts +4 -16
  163. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  164. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  165. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
  166. package/build-types/components/media-form/index.d.ts.map +1 -1
  167. package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
  168. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
  169. package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
  170. package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
  171. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
  172. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  173. package/build-types/image-editor/core/constants.d.ts +18 -0
  174. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  175. package/build-types/image-editor/core/containment.d.ts +10 -0
  176. package/build-types/image-editor/core/containment.d.ts.map +1 -1
  177. package/build-types/image-editor/core/crop-rect.d.ts +19 -0
  178. package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
  179. package/build-types/image-editor/core/index.d.ts +3 -1
  180. package/build-types/image-editor/core/index.d.ts.map +1 -1
  181. package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
  182. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  183. package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
  184. package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
  185. package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
  186. package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
  187. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  188. package/build-types/image-editor/core/state.d.ts +12 -0
  189. package/build-types/image-editor/core/state.d.ts.map +1 -1
  190. package/build-types/image-editor/core/stencil-math.d.ts +23 -18
  191. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  192. package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
  193. package/build-types/image-editor/core/types.d.ts +23 -4
  194. package/build-types/image-editor/core/types.d.ts.map +1 -1
  195. package/build-types/image-editor/index.d.ts +4 -4
  196. package/build-types/image-editor/index.d.ts.map +1 -1
  197. package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
  198. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  199. package/build-types/image-editor/react/components/cropper.d.ts +6 -4
  200. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  201. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
  202. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
  203. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
  204. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  205. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
  206. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
  207. package/build-types/image-editor/react/hooks/index.d.ts +4 -2
  208. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  209. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  210. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
  211. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
  212. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  213. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
  214. package/build-types/state/composite-reducer.d.ts +39 -0
  215. package/build-types/state/composite-reducer.d.ts.map +1 -0
  216. package/build-types/state/index.d.ts +7 -0
  217. package/build-types/state/index.d.ts.map +1 -0
  218. package/build-types/state/media-editor-state-provider.d.ts +34 -0
  219. package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
  220. package/build-types/state/types.d.ts +104 -0
  221. package/build-types/state/types.d.ts.map +1 -0
  222. package/build-types/state/use-media-editor-state.d.ts +104 -0
  223. package/build-types/state/use-media-editor-state.d.ts.map +1 -0
  224. package/package.json +19 -17
  225. package/src/components/media-editor/index.tsx +52 -188
  226. package/src/components/media-editor/test/use-crop-options.tsx +164 -0
  227. package/src/components/media-editor/use-crop-options.ts +115 -0
  228. package/src/components/media-editor/use-save-media-editor.ts +214 -0
  229. package/src/components/media-editor-canvas/index.tsx +56 -24
  230. package/src/components/media-editor-crop-panel/index.tsx +25 -61
  231. package/src/components/media-editor-crop-panel/test/index.tsx +17 -43
  232. package/src/components/media-editor-modal/index.tsx +50 -12
  233. package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
  234. package/src/components/media-editor-modal/test/index.tsx +143 -0
  235. package/src/components/media-editor-toolbar/index.tsx +9 -5
  236. package/src/components/media-form/index.tsx +15 -1
  237. package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
  238. package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
  239. package/src/components/rotation-ruler/README.md +11 -25
  240. package/src/components/rotation-ruler/index.tsx +17 -2
  241. package/src/components/rotation-ruler/style.scss +49 -13
  242. package/src/hooks/use-crop-gesture-handlers.ts +66 -17
  243. package/src/image-editor/README.md +28 -129
  244. package/src/image-editor/core/camera.ts +51 -26
  245. package/src/image-editor/core/constants.ts +19 -0
  246. package/src/image-editor/core/containment.ts +102 -49
  247. package/src/image-editor/core/crop-rect.ts +45 -0
  248. package/src/image-editor/core/index.ts +16 -4
  249. package/src/image-editor/core/interaction-controller.ts +5 -1
  250. package/src/image-editor/core/math/sanitize.ts +158 -0
  251. package/src/image-editor/core/math/test/sanitize.ts +244 -0
  252. package/src/image-editor/core/setter-helpers.ts +66 -0
  253. package/src/image-editor/core/source-region.ts +21 -14
  254. package/src/image-editor/core/state.ts +67 -15
  255. package/src/image-editor/core/stencil-math.ts +55 -35
  256. package/src/image-editor/core/test/camera.ts +367 -1
  257. package/src/image-editor/core/test/state.ts +101 -8
  258. package/src/image-editor/core/test/stencil-math.ts +193 -0
  259. package/src/image-editor/core/transform-style.ts +21 -6
  260. package/src/image-editor/core/types.ts +24 -4
  261. package/src/image-editor/docs/architecture.md +48 -89
  262. package/src/image-editor/docs/recipes.md +123 -659
  263. package/src/image-editor/index.ts +12 -3
  264. package/src/image-editor/react/components/cropper-provider.tsx +16 -12
  265. package/src/image-editor/react/components/cropper.scss +23 -4
  266. package/src/image-editor/react/components/cropper.tsx +197 -90
  267. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
  268. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -9
  269. package/src/image-editor/react/components/test/cropper.tsx +55 -13
  270. package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
  271. package/src/image-editor/react/hooks/index.ts +4 -2
  272. package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
  273. package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
  274. package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
  275. package/src/image-editor/react/hooks/use-interaction.ts +3 -2
  276. package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
  277. package/src/state/composite-reducer.ts +152 -0
  278. package/src/state/index.ts +20 -0
  279. package/src/state/media-editor-state-provider.tsx +70 -0
  280. package/src/state/test/use-media-editor-state.ts +402 -0
  281. package/src/state/types.ts +107 -0
  282. package/src/state/use-media-editor-state.ts +488 -0
  283. package/tsconfig.json +2 -0
  284. package/tsconfig.tsbuildinfo +1 -1
  285. package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
  286. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
  287. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
  288. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
  289. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
  290. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
  291. package/src/image-editor/docs/roadmap.md +0 -66
  292. package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
@@ -1,162 +1,61 @@
1
1
  # Image Editor
2
2
 
3
- > **Status: internal.** This module is not exported from `@wordpress/media-editor`'s public API. Only code inside `@wordpress/media-editor` should import it, via relative paths (e.g. `../image-editor`). When the module is promoted to the package's public surface, the `@wordpress/media-editor` import paths used in these docs will work.
3
+ > **Status: internal.** This module is not part of `@wordpress/media-editor`'s public API. Import it only from inside the media-editor package, through the local `../image-editor` barrel.
4
4
 
5
- A modular image editor inside `@wordpress/media-editor`. Two layers:
5
+ The image editor is the cropper engine used by the media editor:
6
6
 
7
- - **Core** — framework-agnostic state, math, and interaction logic. Pure TypeScript + `gl-matrix`. The export helpers in `core/export/` are browser-only (they use `HTMLCanvasElement` and `Image`), but everything else — reducer, camera math, interaction controller — has no DOM or React dependency. A non-React UI layer (Vue, Svelte, vanilla) can reuse core directly.
8
- - **React adapter** — thin wrappers over core. The public surface is `<Cropper>`, `useCropperState`, plus the optional `CropperProvider` / `useCropper` context pair. `useInteraction` and `useTransformStyle` exist internally but are not exported.
7
+ - **Core:** state, camera math, containment, source-region, transforms, and canvas export.
8
+ - **React:** `<Cropper>`, `useCropperReducer`, and the optional `CropperProvider` / `useCropper` context wrapper.
9
9
 
10
- ## Quick start
10
+ `useCropperReducer` does not provide undo/redo. The media editor's composite state layer adds history and sidebar crop options on top of the cropper reducer.
11
+
12
+ ## Quick Start
11
13
 
12
14
  ```tsx
13
- import { Cropper, useCropperState } from '../image-editor';
15
+ import { Cropper, useCropperReducer } from '../image-editor';
16
+
17
+ function ImageCropper() {
18
+ const controller = useCropperReducer();
14
19
 
15
- function ImageEditor() {
16
- const controller = useCropperState();
17
20
  return (
18
21
  <div style={ { width: 600, height: 400 } }>
19
22
  <Cropper
20
23
  src="https://example.com/photo.jpg"
21
24
  controller={ controller }
22
- showDimming
23
- showGrid
24
25
  freeformCrop
26
+ showDimming
27
+ showGrid="interactive"
25
28
  />
26
29
  </div>
27
30
  );
28
31
  }
29
32
  ```
30
33
 
31
- `useCropperState` returns a single `controller` object that bundles the current state and every setter. Pass it to `<Cropper>` as a single prop, and call setters like `controller.setZoom( 2 )` or `controller.snapRotate90( 1 )` from your own toolbars.
32
-
33
- ## Styles
34
-
35
- The cropper's styles are compiled as part of `@wordpress/media-editor`'s SCSS build. Internal callers that render `<Cropper>` inherit them automatically when the package's stylesheet is loaded. All CSS classes use the `wp-media-editor-image-editor` prefix.
36
-
37
- ## Docs
38
-
39
- - [docs/architecture.md](docs/architecture.md) — Data flow, coordinate spaces, and design decisions
40
- - [docs/recipes.md](docs/recipes.md) — Getting started walkthrough, extension points, and integration patterns
41
- - [docs/roadmap.md](docs/roadmap.md) — Planned follow-up work and phases
42
-
43
- ## API Reference
44
-
45
- ### React components
46
-
47
- #### `Cropper`
48
-
49
- Main cropper component. Fills its parent container.
50
-
51
- | Prop | Type | Default | Description |
52
- | ---------------- | ------------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
53
- | `src` | `string` | **required** | Image source URL |
54
- | `controller` | `UseCropperStateReturn` | **required** | The full object returned by `useCropperState()` |
55
- | `stencil` | `ComponentType<StencilProps>` | `RectangleStencil` | Custom crop area UI |
56
- | `showGrid` | `boolean` | `false` | Rule-of-thirds grid overlay |
57
- | `showDimming` | `boolean` | `true` | Dimming overlay outside crop |
58
- | `minZoom` | `number` | `1` | Minimum zoom for interactive gestures (wheel, pinch, double-tap). The reducer itself clamps to `[1, 10]` regardless, so a larger `minZoom` only narrows what the user can reach through gestures. |
59
- | `maxZoom` | `number` | `10` | Maximum zoom for interactive gestures. Same caveat as `minZoom`. |
60
- | `aspectRatio` | `number` | — | Fixed aspect ratio (width/height) |
61
- | `freeformCrop` | `boolean` | `false` | Enable resize handles |
62
- | `onImageLoaded` | `(size: Size) => void` | — | Image load callback |
63
- | `onStateChange` | `(state: CropperState) => void` | — | Fires on every state change (high frequency — at pointermove rate during drags). Avoid heavy work in the handler; for commit-style events use `onGestureEnd` instead. |
64
- | `onGestureStart` | `() => void` | — | Gesture boundary start |
65
- | `onGestureEnd` | `() => void` | — | Gesture boundary end |
66
- | `className` | `string` | — | Additional CSS class |
67
-
68
- #### `CropperProvider` / `useCropper()`
69
-
70
- Context wrapper for deep component trees. Wraps `useCropperState` and provides it to descendants via `useCropper()`.
71
-
72
- ### React hooks
34
+ `useCropperReducer` returns a controller object with the current `state`, named setters, `reset`, `isDirty`, and `getCroppedImage`.
73
35
 
74
- #### `useCropperState( initialState?: Partial<CropperState> ): UseCropperStateReturn`
36
+ ## Internal APIs
75
37
 
76
- State management hook. Returns a `controller` object with the current state and a named setter for each supported transition.
38
+ ### `Cropper`
77
39
 
78
- > **Pan vs. crop rectangle**: `setPan` / `state.pan` sets the image _pan offset_ (how the image is translated inside the viewport). `setCropRect` / `state.cropRect` sets the _crop rectangle_ (the region being selected).
40
+ Fills its parent container. Required props are `src` and `controller`. Common options include `freeformCrop`, `aspectRatio`, `showGrid`, `showDimming`, `showDimensions`, `onGestureStart`, and `onGestureEnd`.
79
41
 
80
- | Field | Type | Description |
81
- | ----------------- | ---------------------------------------------------- | ----------------------------------------------------------------------------------- |
82
- | `state` | `CropperState` | Current state (read-only) |
83
- | `setImage` | `(image: CropperState['image']) => void` | Set the loaded image (src + natural size) |
84
- | `setPan` | `(pan: NormalizedPoint) => void` | Set image pan offset |
85
- | `setZoom` | `(zoom: number) => void` | Set zoom (clamped 1–10) |
86
- | `setZoomAtPoint` | `(zoom: number, pan: NormalizedPoint) => void` | Set zoom and pan together for focal-point zoom |
87
- | `setRotation` | `(degrees: number) => void` | Set rotation (normalized 0–360) |
88
- | `setFlip` | `(flip: Flip) => void` | Set flip state |
89
- | `snapRotate90` | `(direction: 1 \| -1) => void` | 90° snap rotation |
90
- | `setCropRect` | `(rect: NormalizedRect) => void` | Set crop rectangle |
91
- | `settleCrop` | `() => void` | Settle the crop rect after a resize drag (typically from `onResizeEnd`) |
92
- | `applyOperation` | `(op: TransformOperation) => void` | Apply a pipeline operation |
93
- | `reset` | `(state?: Partial<CropperState>) => void` | Reset to initial or given state |
94
- | `isDirty` | `boolean` | Whether state differs from initial |
95
- | `getCroppedImage` | `(mime?: string, quality?: number) => Promise<Blob>` | Export as Blob. Throws on load/CORS/context failure — wrap in try/catch to recover. |
42
+ ### `useCropperReducer( initialState? )`
96
43
 
97
- The controller does not expose the reducer dispatch. Use the named setters or `applyOperation()` to change state.
44
+ Pure React state hook for the cropper reducer. Returns a `CropperController`: current state, named setters, `reset`, `isDirty`, and `getCroppedImage`.
98
45
 
99
- ### Source region
46
+ ### `CropperProvider` / `useCropper`
100
47
 
101
- #### `getSourceRegion( state, imageSize ): SourceRegion`
48
+ Convenience context wrapper around `useCropperReducer` for component trees that need shared cropper state.
102
49
 
103
- Converts crop state to source-pixel coordinates: `{ x, y, width, height, rotation, flip, zoom }`. For server-side processing (FFmpeg, ImageMagick, etc.).
50
+ ## Core Helpers
104
51
 
105
- #### `getSourceRegionPercent( state, imageSize ): SourceRegionPercent`
52
+ The local barrel also exposes helpers for reducer composition, source-region calculation, pipeline replay, and canvas export. Use named setters or `TransformOperation` values for normal state changes. Use reducer actions only when composing a controller such as `useMediaEditorState()`.
106
53
 
107
- Same as `getSourceRegion` but returns percentages (0–100): `{ x, y, width, height }`. Compatible with the WordPress REST API attachments `/edit` endpoint.
108
-
109
- ### Export
110
-
111
- #### `exportCroppedImage( src, state, mimeType?, quality? ): Promise<Blob>`
112
-
113
- End-to-end: load image, render with transforms, export as Blob. Browser-only (needs `HTMLCanvasElement`).
114
-
115
- Rejects on:
116
-
117
- - **Image load failures** (network error, 404) — the native load error is propagated.
118
- - **CORS / tainted canvas** — if the source doesn't set `Access-Control-Allow-Origin`, `canvas.toBlob()` rejects because the canvas is tainted. Fix at the server: send permissive CORS headers.
119
- - **Missing canvas context** — throws with a descriptive `Error`.
120
-
121
- Wrap in `try/catch` to distinguish failure modes.
122
-
123
- #### `applyToCanvas( source: CanvasImageSource, imageSize, state ): HTMLCanvasElement`
124
-
125
- Applies transforms to any `CanvasImageSource` (image, canvas, video frame, offscreen canvas). For multi-step editing pipelines.
126
-
127
- ### Pipeline
128
-
129
- #### `stateFromPipeline( operations: TransformOperation[] ): CropperState`
130
-
131
- Replays a sequence of operations from default state. Pure function, no DOM needed. For headless/server-side processing.
132
-
133
- #### `applyOperationToState( state, operation ): CropperState`
134
-
135
- Applies a single operation to an existing state.
136
-
137
- ### Types
138
-
139
- | Type | Description |
140
- | ----------------------- | --------------------------------------------------------------------------------- |
141
- | `CropperState` | `{ image, pan, zoom, rotation, flip, cropRect, basePan, baseZoom, baseRotation }` |
142
- | `UseCropperStateReturn` | The full shape returned by `useCropperState()`: state + setters |
143
- | `CropperProps` | Props for the `<Cropper>` component |
144
- | `StencilProps` | Contract for pluggable stencil components |
145
- | `TransformOperation` | `{ type: 'crop' \| 'rotate' \| 'flip' \| 'zoom', ... }` |
146
- | `NormalizedPoint` | `{ x: number, y: number }` in [0,1] space |
147
- | `NormalizedRect` | `{ x, y, width, height }` in [0,1] space |
148
- | `Size` | `{ width: number, height: number }` |
149
- | `Flip` | `{ horizontal: boolean, vertical: boolean }` |
150
- | `SourceRegion` | `{ x, y, width, height, rotation, flip, zoom }` in source pixels |
151
- | `SourceRegionPercent` | `{ x, y, width, height }` as percentages (0–100) |
152
- | `AspectRatioPreset` | `{ label: string, value: number }` |
54
+ ## Styles
153
55
 
154
- `CropperAction` (the reducer's action union) is internal. Drive state through the named setters on the controller object.
56
+ Styles are bundled through the media-editor stylesheet. Classes use the `wp-media-editor-image-editor` prefix.
155
57
 
156
- ### Constants
58
+ ## More
157
59
 
158
- | Constant | Value | Description |
159
- | ----------------------- | ----- | ------------------------------------------------------ |
160
- | `DEFAULT_STATE` | — | Default `CropperState` |
161
- | `DEFAULT_ASPECT_RATIOS` | Array | Preset aspect ratios (Free, Original, 1:1, 16:9, etc.) |
162
- | `ORIGINAL_ASPECT_RATIO` | `-1` | Sentinel value for "use image's original ratio" |
60
+ - [docs/architecture.md](docs/architecture.md)
61
+ - [docs/recipes.md](docs/recipes.md)
@@ -8,6 +8,12 @@ import { mat2d, vec2 } from 'gl-matrix';
8
8
  */
9
9
  import type { CropperState, NormalizedPoint, Size, Camera } from './types';
10
10
  import { degreesToRadians } from './math/rotation';
11
+ import {
12
+ isSafeNumber,
13
+ isValidSize,
14
+ safeBoundedNumber,
15
+ sanitizeCropperState,
16
+ } from './math/sanitize';
11
17
 
12
18
  // Pre-allocated scratch buffers for `screenToWorld`, which is called per
13
19
  // pointermove. Module-level singletons — safe because usage is synchronous.
@@ -27,6 +33,19 @@ export function getRotatedBBox(
27
33
  height: number,
28
34
  rotation: number
29
35
  ): Size {
36
+ // Short-circuit on any unsafe input. `isSafeNumber` is stricter than
37
+ // `Number.isFinite`: very large but technically-finite rotations (e.g.
38
+ // `Number.MAX_VALUE`) overflow inside `degreesToRadians` and then
39
+ // `Math.cos`/`Math.sin` return `NaN`, so the magnitude bound matters.
40
+ if (
41
+ ! isSafeNumber( width ) ||
42
+ ! isSafeNumber( height ) ||
43
+ ! isSafeNumber( rotation ) ||
44
+ width <= 0 ||
45
+ height <= 0
46
+ ) {
47
+ return { width: 0, height: 0 };
48
+ }
30
49
  const rad = degreesToRadians( rotation );
31
50
  const cosR = Math.abs( Math.cos( rad ) );
32
51
  const sinR = Math.abs( Math.sin( rad ) );
@@ -54,27 +73,31 @@ export function getImageFit(
54
73
  imageSize: Size,
55
74
  rotation: number
56
75
  ): { elementSize: Size; visualSize: Size } {
57
- if (
58
- containerSize.width === 0 ||
59
- containerSize.height === 0 ||
60
- imageSize.width === 0 ||
61
- imageSize.height === 0
62
- ) {
76
+ if ( ! isValidSize( containerSize ) || ! isValidSize( imageSize ) ) {
63
77
  return {
64
78
  elementSize: { width: 0, height: 0 },
65
79
  visualSize: { width: 0, height: 0 },
66
80
  };
67
81
  }
82
+ const safeRotation = safeBoundedNumber( rotation, 0 );
68
83
  // Snap rotation to the nearest 90° multiple for layout sizing.
69
84
  // This keeps the stencil a stable size through fine ±45° rotation
70
85
  // (no inflation at 15°/30° etc.) while still swapping aspect at
71
86
  // 90°/180°/270° so the snap rotate preserves the framed content.
72
- const snapRotation = Math.round( rotation / 90 ) * 90;
87
+ const snapRotation = Math.round( safeRotation / 90 ) * 90;
73
88
  const naturalBBox = getRotatedBBox(
74
89
  imageSize.width,
75
90
  imageSize.height,
76
91
  snapRotation
77
92
  );
93
+ // Defensive: getRotatedBBox returns zero dims for hostile inputs, which
94
+ // would make fitScale infinite. Mirror the zero-container short-circuit.
95
+ if ( naturalBBox.width === 0 || naturalBBox.height === 0 ) {
96
+ return {
97
+ elementSize: { width: 0, height: 0 },
98
+ visualSize: { width: 0, height: 0 },
99
+ };
100
+ }
78
101
  const fitScale = Math.min(
79
102
  containerSize.width / naturalBBox.width,
80
103
  containerSize.height / naturalBBox.height
@@ -113,20 +136,17 @@ export function createCamera(
113
136
  ): Camera {
114
137
  const m = mat2d.create();
115
138
 
116
- if (
117
- containerSize.width === 0 ||
118
- containerSize.height === 0 ||
119
- imageSize.width === 0 ||
120
- imageSize.height === 0
121
- ) {
139
+ if ( ! isValidSize( containerSize ) || ! isValidSize( imageSize ) ) {
122
140
  return m;
123
141
  }
124
142
 
143
+ const safeState = sanitizeCropperState( state );
144
+
125
145
  // Use the nearest 90° multiple for layout sizing so the stencil
126
146
  // and visual bounds are stable through fine rotation. The actual
127
147
  // `state.rotation` is still used for the rotation component of
128
148
  // the matrix below.
129
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
149
+ const snapRotation = Math.round( safeState.rotation / 90 ) * 90;
130
150
 
131
151
  // Rotated bounding box of the natural image (at snap angle).
132
152
  const naturalBBox = getRotatedBBox(
@@ -135,6 +155,12 @@ export function createCamera(
135
155
  snapRotation
136
156
  );
137
157
 
158
+ // Defensive: getRotatedBBox returns zero dims for unsafe inputs. Mirror
159
+ // the zero-size short-circuit so `fitScale` doesn't become Infinity.
160
+ if ( naturalBBox.width === 0 || naturalBBox.height === 0 ) {
161
+ return m;
162
+ }
163
+
138
164
  // "Contain" fit: scale rotated bounding box to fit within container.
139
165
  const fitScale = Math.min(
140
166
  containerSize.width / naturalBBox.width,
@@ -162,20 +188,23 @@ export function createCamera(
162
188
  ] );
163
189
 
164
190
  // Pan offset in visual-space pixels.
165
- mat2d.translate( m, m, [ state.pan.x * visualW, state.pan.y * visualH ] );
191
+ mat2d.translate( m, m, [
192
+ safeState.pan.x * visualW,
193
+ safeState.pan.y * visualH,
194
+ ] );
166
195
 
167
196
  // Flip (viewport-relative — composed outside rotation so horizontal
168
197
  // flip always mirrors across the viewport's vertical axis).
169
198
  mat2d.scale( m, m, [
170
- state.flip.horizontal ? -1 : 1,
171
- state.flip.vertical ? -1 : 1,
199
+ safeState.flip.horizontal ? -1 : 1,
200
+ safeState.flip.vertical ? -1 : 1,
172
201
  ] );
173
202
 
174
203
  // Rotate.
175
- mat2d.rotate( m, m, degreesToRadians( state.rotation ) );
204
+ mat2d.rotate( m, m, degreesToRadians( safeState.rotation ) );
176
205
 
177
206
  // Zoom.
178
- mat2d.scale( m, m, [ state.zoom, state.zoom ] );
207
+ mat2d.scale( m, m, [ safeState.zoom, safeState.zoom ] );
179
208
 
180
209
  // Center origin (shift so 0.5,0.5 in rendered-pixel space = origin).
181
210
  mat2d.translate( m, m, [ -renderedW / 2, -renderedH / 2 ] );
@@ -304,15 +333,11 @@ export function createExportCamera(
304
333
  outputSize: Size
305
334
  ): Camera {
306
335
  const m = mat2d.create();
307
- const { rotation, flip, cropRect, zoom, pan } = state;
308
- if (
309
- imageSize.width === 0 ||
310
- imageSize.height === 0 ||
311
- outputSize.width === 0 ||
312
- outputSize.height === 0
313
- ) {
336
+ if ( ! isValidSize( imageSize ) || ! isValidSize( outputSize ) ) {
314
337
  return m;
315
338
  }
339
+ const { rotation, flip, cropRect, zoom, pan } =
340
+ sanitizeCropperState( state );
316
341
  // Reference frame for cropRect/pan is the snap-rotation bbox — that's
317
342
  // what the stencil and CSS matrix use in the preview (see createCamera
318
343
  // and getImageFit). Using the true rotation here would position the
@@ -8,9 +8,28 @@ import { __ } from '@wordpress/i18n';
8
8
  */
9
9
  import type { NormalizedRect, Flip, CropperState } from './types';
10
10
 
11
+ /**
12
+ * Default/resting zoom. A full, unrotated crop needs at least 1x to cover the
13
+ * crop area, but smaller/fine-rotated crops can be valid below 1x — the true
14
+ * floor for those comes from `getMinZoom`.
15
+ */
11
16
  export const MIN_ZOOM = 1;
17
+ /**
18
+ * Safety floor for coverage-aware zoom. Degenerate crops can have a geometric
19
+ * coverage minimum of 0, but camera and inverse-matrix math need a stable,
20
+ * positive scale.
21
+ */
22
+ export const ABSOLUTE_MIN_ZOOM = 0.1;
12
23
  export const MAX_ZOOM = 10;
13
24
 
25
+ /**
26
+ * Minimum crop rect dimension in source-image pixels, enforced per axis
27
+ * during resize. Prevents accidental sub-pixel crops while staying small
28
+ * enough to allow tight crops (favicons, icons). Adjust here to tune the
29
+ * floor globally.
30
+ */
31
+ export const MIN_CROP_PIXELS = 24;
32
+
14
33
  /**
15
34
  * Wheel zoom sensitivity. A deltaY of 100 changes zoom by 0.25.
16
35
  * This could be made configurable as a prop to the Cropper component.
@@ -7,7 +7,14 @@ import { mat2d, vec2 } from 'gl-matrix';
7
7
  * Internal dependencies
8
8
  */
9
9
  import type { CropperState, NormalizedRect, Size } from './types';
10
+ import { ABSOLUTE_MIN_ZOOM, MAX_ZOOM, MIN_ZOOM } from './constants';
10
11
  import { degreesToRadians } from './math/rotation';
12
+ import {
13
+ isValidSize,
14
+ safeBoundedNumber,
15
+ sanitizeCropperState,
16
+ sanitizeRect,
17
+ } from './math/sanitize';
11
18
  import { createCamera, getRotatedBBox, getVisibleBounds } from './camera';
12
19
 
13
20
  /** Floating-point epsilon for "close enough to equal" comparisons. */
@@ -38,18 +45,19 @@ function getVisualDimensions(
38
45
  }
39
46
 
40
47
  /**
41
- * Calculates the minimum zoom factor needed for the rotated image to fully cover
42
- * the crop rectangle, using normalized coordinates and imageAspectRatio.
43
- *
44
- * Works in pixel-proportional space where the unrotated image is a×1.
45
- * The crop rect is in visual-normalized space and must be scaled by the
48
+ * The geometric minimum zoom required for the rotated image to fully cover the
49
+ * crop rectangle. Works in pixel-proportional space where the unrotated image
50
+ * is a×1; the crop rect lives in visual-normalized space and is scaled by the
46
51
  * rotation-dependent visual dimensions before projecting into the image-local
47
52
  * frame.
48
53
  *
54
+ * Returns 0 for a degenerate (zero-area) crop — callers compose the result
55
+ * with their own floor (typically the current zoom or `MIN_ZOOM`).
56
+ *
49
57
  * @param rotation Rotation angle in degrees.
50
58
  * @param imageAspectRatio Image width / height ratio.
51
59
  * @param cropRect The crop rectangle in normalized coordinates.
52
- * @return The minimum zoom factor (always >= 1).
60
+ * @return The minimum zoom factor needed for coverage.
53
61
  */
54
62
  function getMinZoomForCover(
55
63
  rotation: number,
@@ -82,7 +90,39 @@ function getMinZoomForCover(
82
90
  const zoomFromAlpha = ( 2 * spanAlpha ) / aspectRatio;
83
91
  const zoomFromBeta = 2 * spanBeta;
84
92
 
85
- return Math.max( 1, zoomFromAlpha, zoomFromBeta );
93
+ return Math.max( zoomFromAlpha, zoomFromBeta );
94
+ }
95
+
96
+ /**
97
+ * Resolves the effective minimum zoom for the current state — the coverage-
98
+ * aware floor when an image is loaded, falling back to `MIN_ZOOM` otherwise.
99
+ * Used by both the cropper's interaction layer and the zoom slider so the
100
+ * floor stays consistent across surfaces.
101
+ *
102
+ * @param state The current cropper state.
103
+ * @return The minimum zoom factor.
104
+ */
105
+ export function getMinZoom( state: CropperState ): number {
106
+ if ( ! state.image ) {
107
+ return MIN_ZOOM;
108
+ }
109
+ const imageSize = {
110
+ width: state.image.naturalWidth,
111
+ height: state.image.naturalHeight,
112
+ };
113
+ if ( ! isValidSize( imageSize ) ) {
114
+ return MIN_ZOOM;
115
+ }
116
+ const safeState = sanitizeCropperState( state );
117
+ const aspectRatio = imageSize.width / imageSize.height;
118
+ return Math.max(
119
+ ABSOLUTE_MIN_ZOOM,
120
+ getMinZoomForCover(
121
+ safeState.rotation,
122
+ aspectRatio,
123
+ safeState.cropRect
124
+ )
125
+ );
86
126
  }
87
127
 
88
128
  /**
@@ -109,25 +149,22 @@ export function getImageCropBounds(
109
149
  elementSize: Size,
110
150
  visualSize: Size
111
151
  ): { minX: number; minY: number; maxX: number; maxY: number } {
112
- if (
113
- elementSize.width === 0 ||
114
- elementSize.height === 0 ||
115
- visualSize.width === 0 ||
116
- visualSize.height === 0
117
- ) {
152
+ if ( ! isValidSize( elementSize ) || ! isValidSize( visualSize ) ) {
118
153
  return { minX: 0, minY: 0, maxX: 1, maxY: 1 };
119
154
  }
120
155
 
156
+ const safeState = sanitizeCropperState( state );
157
+
121
158
  // Build the same CSS matrix as use-transform-style: flip * rotate * zoom.
122
159
  // Flip is outside rotation so it acts in viewport axes (see createCamera).
123
- const tx = state.pan.x * visualSize.width;
124
- const ty = state.pan.y * visualSize.height;
125
- const rad = degreesToRadians( state.rotation );
160
+ const tx = safeState.pan.x * visualSize.width;
161
+ const ty = safeState.pan.y * visualSize.height;
162
+ const rad = degreesToRadians( safeState.rotation );
126
163
  const cos = Math.cos( rad );
127
164
  const sin = Math.sin( rad );
128
- const sx = state.flip.horizontal ? -1 : 1;
129
- const sy = state.flip.vertical ? -1 : 1;
130
- const z = state.zoom;
165
+ const sx = safeState.flip.horizontal ? -1 : 1;
166
+ const sy = safeState.flip.vertical ? -1 : 1;
167
+ const z = safeState.zoom;
131
168
  const ma = sx * cos * z;
132
169
  const mb = sy * sin * z;
133
170
  const mc = -sx * sin * z;
@@ -190,17 +227,25 @@ export function restrictCropRect(
190
227
  rotation: number,
191
228
  imageAspectRatio: number
192
229
  ): NormalizedRect {
193
- const aspectRatio = Math.max( imageAspectRatio, Number.EPSILON );
230
+ // Guard against non-finite inputs. The reducer normalizes these, but
231
+ // programmatic callers or deserialized state could still send NaN/±Infinity
232
+ // through here, where it would silently propagate into the crop rect.
233
+ const safeRect = sanitizeRect( cropRect );
234
+ const zoomCandidate = safeBoundedNumber( zoom, 1 );
235
+ const safeZoom = zoomCandidate >= Number.EPSILON ? zoomCandidate : 1;
236
+ const safeRotation = safeBoundedNumber( rotation, 0 );
237
+ const safeAspect = safeBoundedNumber( imageAspectRatio, 1 );
238
+ const aspectRatio = Math.max( safeAspect, Number.EPSILON );
194
239
  // Crop rect lives in the SNAP-rotation visual bbox (matching the stencil
195
240
  // layout); projection into the image-local frame uses the TRUE rotation.
196
- const snapRotation = Math.round( rotation / 90 ) * 90;
241
+ const snapRotation = Math.round( safeRotation / 90 ) * 90;
197
242
  const { visualW, visualH } = getVisualDimensions(
198
243
  snapRotation,
199
244
  aspectRatio
200
245
  );
201
- const { absC, absS } = getVisualDimensions( rotation, aspectRatio );
202
- const W = cropRect.width;
203
- const H = cropRect.height;
246
+ const { absC, absS } = getVisualDimensions( safeRotation, aspectRatio );
247
+ const W = safeRect.width;
248
+ const H = safeRect.height;
204
249
 
205
250
  // Crop full-extents in pixel-proportional space, projected to image-local frame.
206
251
  const cropWPx = W * visualW;
@@ -209,8 +254,8 @@ export function restrictCropRect(
209
254
  const spanBeta = cropWPx * absS + cropHPx * absC;
210
255
 
211
256
  // Image full-extents at zoom z: (aspectRatio*z, z).
212
- const limitAlpha = aspectRatio * zoom;
213
- const limitBeta = zoom;
257
+ const limitAlpha = aspectRatio * safeZoom;
258
+ const limitBeta = safeZoom;
214
259
 
215
260
  let t = 1;
216
261
  if ( spanAlpha > 0 ) {
@@ -221,13 +266,14 @@ export function restrictCropRect(
221
266
  }
222
267
  if ( t >= 1 - EPSILON ) {
223
268
  // Crop fits at the current zoom — no size change needed.
224
- // Position is handled by restrictPanZoom, not here.
225
- return cropRect;
269
+ // Return the sanitized rect (not the raw arg) so non-finite fields
270
+ // don't slip through this path. Position is handled by restrictPanZoom.
271
+ return safeRect;
226
272
  }
227
273
  const newW = W * t;
228
274
  const newH = H * t;
229
- const centerX = cropRect.x + W / 2;
230
- const centerY = cropRect.y + H / 2;
275
+ const centerX = safeRect.x + W / 2;
276
+ const centerY = safeRect.y + H / 2;
231
277
  let newX = centerX - newW / 2;
232
278
  let newY = centerY - newH / 2;
233
279
  newX = Math.max( 0, Math.min( newX, 1 - newW ) );
@@ -280,15 +326,22 @@ export function restrictPanZoom(
280
326
  // 6. Convert the world-space correction to a pan-field correction by
281
327
  // mapping it through the camera's linear part back to screen space,
282
328
  // then dividing by the visual bounds.
283
- const aspectRatio =
284
- imageSize.width > 0 && imageSize.height > 0
285
- ? imageSize.width / imageSize.height
286
- : 1;
287
- const minZoom = getMinZoomForCover( state.rotation, aspectRatio, cropRect );
288
- const zoom = Math.max( state.zoom, minZoom );
329
+ const safeState = sanitizeCropperState( state );
330
+ const safeCropRect = sanitizeRect( cropRect );
331
+ // Use `isValidSize` rather than a bare `> 0` check so Infinity dims
332
+ // don't drive `Infinity / Infinity = NaN` into `getMinZoomForCover`
333
+ // and leak as `zoom: NaN` through the no-correction early return.
334
+ const aspectRatio = isValidSize( imageSize )
335
+ ? imageSize.width / imageSize.height
336
+ : 1;
337
+ const minZoom = Math.max(
338
+ ABSOLUTE_MIN_ZOOM,
339
+ getMinZoomForCover( safeState.rotation, aspectRatio, safeCropRect )
340
+ );
341
+ const zoom = Math.min( MAX_ZOOM, Math.max( safeState.zoom, minZoom ) );
289
342
 
290
343
  // Step 2: build camera with candidate pan and corrected zoom.
291
- const candidateState = { ...state, zoom };
344
+ const candidateState = { ...safeState, zoom };
292
345
  const camera = createCamera(
293
346
  candidateState,
294
347
  CANONICAL_CONTAINER,
@@ -300,7 +353,7 @@ export function restrictPanZoom(
300
353
  // nearest 90° rotation (matching `getImageFit`), so it's stable
301
354
  // through fine rotation. CSS zoom only affects the <img> element,
302
355
  // not the stencil.
303
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
356
+ const snapRotation = Math.round( safeState.rotation / 90 ) * 90;
304
357
  const baseCamera = createCamera(
305
358
  {
306
359
  ...candidateState,
@@ -316,24 +369,24 @@ export function restrictPanZoom(
316
369
  // Stencil corners in screen space (axis-aligned rect within visual bounds).
317
370
  const stencilCorners: [ number, number ][] = [
318
371
  [
319
- visibleBounds.left + cropRect.x * visibleBounds.width,
320
- visibleBounds.top + cropRect.y * visibleBounds.height,
372
+ visibleBounds.left + safeCropRect.x * visibleBounds.width,
373
+ visibleBounds.top + safeCropRect.y * visibleBounds.height,
321
374
  ],
322
375
  [
323
376
  visibleBounds.left +
324
- ( cropRect.x + cropRect.width ) * visibleBounds.width,
325
- visibleBounds.top + cropRect.y * visibleBounds.height,
377
+ ( safeCropRect.x + safeCropRect.width ) * visibleBounds.width,
378
+ visibleBounds.top + safeCropRect.y * visibleBounds.height,
326
379
  ],
327
380
  [
328
381
  visibleBounds.left +
329
- ( cropRect.x + cropRect.width ) * visibleBounds.width,
382
+ ( safeCropRect.x + safeCropRect.width ) * visibleBounds.width,
330
383
  visibleBounds.top +
331
- ( cropRect.y + cropRect.height ) * visibleBounds.height,
384
+ ( safeCropRect.y + safeCropRect.height ) * visibleBounds.height,
332
385
  ],
333
386
  [
334
- visibleBounds.left + cropRect.x * visibleBounds.width,
387
+ visibleBounds.left + safeCropRect.x * visibleBounds.width,
335
388
  visibleBounds.top +
336
- ( cropRect.y + cropRect.height ) * visibleBounds.height,
389
+ ( safeCropRect.y + safeCropRect.height ) * visibleBounds.height,
337
390
  ],
338
391
  ];
339
392
 
@@ -371,7 +424,7 @@ export function restrictPanZoom(
371
424
  minWy >= -EPSILON &&
372
425
  maxWy <= 1 + EPSILON
373
426
  ) {
374
- return { pan: state.pan, zoom };
427
+ return { pan: safeState.pan, zoom };
375
428
  }
376
429
 
377
430
  // Compute world-space correction needed.
@@ -410,10 +463,10 @@ export function restrictPanZoom(
410
463
  // The correction is subtractive: a positive world shift (dw > 0) means
411
464
  // the image needs to move opposite to pan direction, so pan decreases.
412
465
  const newPanX =
413
- state.pan.x -
466
+ safeState.pan.x -
414
467
  ( visibleBounds.width > 0 ? dsx / visibleBounds.width : 0 );
415
468
  const newPanY =
416
- state.pan.y -
469
+ safeState.pan.y -
417
470
  ( visibleBounds.height > 0 ? dsy / visibleBounds.height : 0 );
418
471
 
419
472
  return {