@wordpress/media-editor 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (306) hide show
  1. package/build/components/media-editor-canvas/index.cjs +54 -0
  2. package/build/components/media-editor-canvas/index.cjs.map +7 -0
  3. package/build/components/media-editor-crop-panel/index.cjs +116 -0
  4. package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
  5. package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
  6. package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
  7. package/build/components/media-editor-modal/index.cjs +497 -0
  8. package/build/components/media-editor-modal/index.cjs.map +7 -0
  9. package/build/components/media-editor-toolbar/index.cjs +146 -0
  10. package/build/components/media-editor-toolbar/index.cjs.map +7 -0
  11. package/build/components/media-preview/index.cjs +1 -1
  12. package/build/components/media-preview/index.cjs.map +2 -2
  13. package/build/image-editor/core/camera.cjs +194 -0
  14. package/build/image-editor/core/camera.cjs.map +7 -0
  15. package/build/image-editor/core/constants.cjs +81 -0
  16. package/build/image-editor/core/constants.cjs.map +7 -0
  17. package/build/image-editor/core/containment.cjs +245 -0
  18. package/build/image-editor/core/containment.cjs.map +7 -0
  19. package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
  20. package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
  21. package/build/image-editor/core/index.cjs +50 -0
  22. package/build/image-editor/core/index.cjs.map +7 -0
  23. package/build/image-editor/core/interaction-controller.cjs +608 -0
  24. package/build/image-editor/core/interaction-controller.cjs.map +7 -0
  25. package/build/image-editor/core/math/rotation.cjs +41 -0
  26. package/build/image-editor/core/math/rotation.cjs.map +7 -0
  27. package/build/image-editor/core/source-region.cjs +95 -0
  28. package/build/image-editor/core/source-region.cjs.map +7 -0
  29. package/build/image-editor/core/state.cjs +269 -0
  30. package/build/image-editor/core/state.cjs.map +7 -0
  31. package/build/image-editor/core/stencil-math.cjs +197 -0
  32. package/build/image-editor/core/stencil-math.cjs.map +7 -0
  33. package/build/image-editor/core/transform-style.cjs +46 -0
  34. package/build/image-editor/core/transform-style.cjs.map +7 -0
  35. package/build/image-editor/core/transforms/pipeline.cjs +43 -0
  36. package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
  37. package/build/image-editor/core/types.cjs +19 -0
  38. package/build/image-editor/core/types.cjs.map +7 -0
  39. package/build/image-editor/index.cjs +57 -0
  40. package/build/image-editor/index.cjs.map +7 -0
  41. package/build/image-editor/react/components/cropper-provider.cjs +50 -0
  42. package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
  43. package/build/image-editor/react/components/cropper.cjs +365 -0
  44. package/build/image-editor/react/components/cropper.cjs.map +7 -0
  45. package/build/image-editor/react/components/index.cjs +36 -0
  46. package/build/image-editor/react/components/index.cjs.map +7 -0
  47. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
  48. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
  49. package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
  50. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
  51. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
  52. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
  53. package/build/image-editor/react/hooks/index.cjs +31 -0
  54. package/build/image-editor/react/hooks/index.cjs.map +7 -0
  55. package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
  56. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
  57. package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
  58. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
  60. package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
  61. package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
  62. package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
  63. package/build/index.cjs +5 -2
  64. package/build/index.cjs.map +2 -2
  65. package/build/lock-unlock.cjs +37 -0
  66. package/build/lock-unlock.cjs.map +7 -0
  67. package/build/private-apis.cjs +38 -0
  68. package/build/private-apis.cjs.map +7 -0
  69. package/build/store/actions.cjs +45 -0
  70. package/build/store/actions.cjs.map +7 -0
  71. package/build/store/constants.cjs +31 -0
  72. package/build/store/constants.cjs.map +7 -0
  73. package/build/store/index.cjs +51 -0
  74. package/build/store/index.cjs.map +7 -0
  75. package/build/store/reducer.cjs +51 -0
  76. package/build/store/reducer.cjs.map +7 -0
  77. package/build/store/selectors.cjs +43 -0
  78. package/build/store/selectors.cjs.map +7 -0
  79. package/build-module/components/media-editor-canvas/index.mjs +33 -0
  80. package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
  81. package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
  82. package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
  83. package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
  84. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
  85. package/build-module/components/media-editor-modal/index.mjs +483 -0
  86. package/build-module/components/media-editor-modal/index.mjs.map +7 -0
  87. package/build-module/components/media-editor-toolbar/index.mjs +130 -0
  88. package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
  89. package/build-module/components/media-preview/index.mjs +1 -1
  90. package/build-module/components/media-preview/index.mjs.map +2 -2
  91. package/build-module/image-editor/core/camera.mjs +163 -0
  92. package/build-module/image-editor/core/camera.mjs.map +7 -0
  93. package/build-module/image-editor/core/constants.mjs +51 -0
  94. package/build-module/image-editor/core/constants.mjs.map +7 -0
  95. package/build-module/image-editor/core/containment.mjs +218 -0
  96. package/build-module/image-editor/core/containment.mjs.map +7 -0
  97. package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
  98. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
  99. package/build-module/image-editor/core/index.mjs +24 -0
  100. package/build-module/image-editor/core/index.mjs.map +7 -0
  101. package/build-module/image-editor/core/interaction-controller.mjs +583 -0
  102. package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
  103. package/build-module/image-editor/core/math/rotation.mjs +15 -0
  104. package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
  105. package/build-module/image-editor/core/source-region.mjs +69 -0
  106. package/build-module/image-editor/core/source-region.mjs.map +7 -0
  107. package/build-module/image-editor/core/state.mjs +242 -0
  108. package/build-module/image-editor/core/state.mjs.map +7 -0
  109. package/build-module/image-editor/core/stencil-math.mjs +169 -0
  110. package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
  111. package/build-module/image-editor/core/transform-style.mjs +21 -0
  112. package/build-module/image-editor/core/transform-style.mjs.map +7 -0
  113. package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
  114. package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
  115. package/build-module/image-editor/core/types.mjs +1 -0
  116. package/build-module/image-editor/core/types.mjs.map +7 -0
  117. package/build-module/image-editor/index.mjs +30 -0
  118. package/build-module/image-editor/index.mjs.map +7 -0
  119. package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
  120. package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
  121. package/build-module/image-editor/react/components/cropper.mjs +337 -0
  122. package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
  123. package/build-module/image-editor/react/components/index.mjs +9 -0
  124. package/build-module/image-editor/react/components/index.mjs.map +7 -0
  125. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
  126. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
  127. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
  128. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
  129. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
  130. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
  131. package/build-module/image-editor/react/hooks/index.mjs +6 -0
  132. package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
  133. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
  134. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
  135. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
  136. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
  137. package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
  138. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
  139. package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
  140. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
  141. package/build-module/index.mjs +3 -1
  142. package/build-module/index.mjs.map +2 -2
  143. package/build-module/lock-unlock.mjs +11 -0
  144. package/build-module/lock-unlock.mjs.map +7 -0
  145. package/build-module/private-apis.mjs +13 -0
  146. package/build-module/private-apis.mjs.map +7 -0
  147. package/build-module/store/actions.mjs +19 -0
  148. package/build-module/store/actions.mjs.map +7 -0
  149. package/build-module/store/constants.mjs +6 -0
  150. package/build-module/store/constants.mjs.map +7 -0
  151. package/build-module/store/index.mjs +16 -0
  152. package/build-module/store/index.mjs.map +7 -0
  153. package/build-module/store/reducer.mjs +26 -0
  154. package/build-module/store/reducer.mjs.map +7 -0
  155. package/build-module/store/selectors.mjs +16 -0
  156. package/build-module/store/selectors.mjs.map +7 -0
  157. package/build-style/style-rtl.css +315 -0
  158. package/build-style/style.css +315 -0
  159. package/build-types/components/media-editor-canvas/index.d.ts +22 -0
  160. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
  161. package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
  162. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
  163. package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
  164. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
  165. package/build-types/components/media-editor-modal/index.d.ts +20 -0
  166. package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
  167. package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
  168. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  169. package/build-types/components/media-preview/index.d.ts.map +1 -1
  170. package/build-types/image-editor/core/camera.d.ts +107 -0
  171. package/build-types/image-editor/core/camera.d.ts.map +1 -0
  172. package/build-types/image-editor/core/constants.d.ts +46 -0
  173. package/build-types/image-editor/core/constants.d.ts.map +1 -0
  174. package/build-types/image-editor/core/containment.d.ts +61 -0
  175. package/build-types/image-editor/core/containment.d.ts.map +1 -0
  176. package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
  177. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
  178. package/build-types/image-editor/core/index.d.ts +8 -0
  179. package/build-types/image-editor/core/index.d.ts.map +1 -0
  180. package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
  181. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
  182. package/build-types/image-editor/core/math/rotation.d.ts +18 -0
  183. package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
  184. package/build-types/image-editor/core/source-region.d.ts +75 -0
  185. package/build-types/image-editor/core/source-region.d.ts.map +1 -0
  186. package/build-types/image-editor/core/state.d.ts +42 -0
  187. package/build-types/image-editor/core/state.d.ts.map +1 -0
  188. package/build-types/image-editor/core/stencil-math.d.ts +79 -0
  189. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
  190. package/build-types/image-editor/core/transform-style.d.ts +23 -0
  191. package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
  192. package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
  193. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
  194. package/build-types/image-editor/core/types.d.ts +211 -0
  195. package/build-types/image-editor/core/types.d.ts.map +1 -0
  196. package/build-types/image-editor/index.d.ts +16 -0
  197. package/build-types/image-editor/index.d.ts.map +1 -0
  198. package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
  199. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
  200. package/build-types/image-editor/react/components/cropper.d.ts +59 -0
  201. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
  202. package/build-types/image-editor/react/components/index.d.ts +4 -0
  203. package/build-types/image-editor/react/components/index.d.ts.map +1 -0
  204. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
  205. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
  206. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
  207. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
  208. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
  209. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
  210. package/build-types/image-editor/react/hooks/index.d.ts +3 -0
  211. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
  212. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
  213. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
  214. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
  215. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  216. package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
  217. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
  218. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
  219. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
  220. package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
  221. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
  222. package/build-types/index.d.ts +1 -0
  223. package/build-types/index.d.ts.map +1 -1
  224. package/build-types/lock-unlock.d.ts +2 -0
  225. package/build-types/lock-unlock.d.ts.map +1 -0
  226. package/build-types/private-apis.d.ts +2 -0
  227. package/build-types/private-apis.d.ts.map +1 -0
  228. package/build-types/store/actions.d.ts +27 -0
  229. package/build-types/store/actions.d.ts.map +1 -0
  230. package/build-types/store/constants.d.ts +2 -0
  231. package/build-types/store/constants.d.ts.map +1 -0
  232. package/build-types/store/index.d.ts +4 -0
  233. package/build-types/store/index.d.ts.map +1 -0
  234. package/build-types/store/reducer.d.ts +23 -0
  235. package/build-types/store/reducer.d.ts.map +1 -0
  236. package/build-types/store/selectors.d.ts +8 -0
  237. package/build-types/store/selectors.d.ts.map +1 -0
  238. package/package.json +21 -8
  239. package/src/components/media-editor-canvas/index.tsx +56 -0
  240. package/src/components/media-editor-canvas/style.scss +9 -0
  241. package/src/components/media-editor-crop-panel/index.tsx +143 -0
  242. package/src/components/media-editor-modal/build-modifiers.ts +164 -0
  243. package/src/components/media-editor-modal/index.tsx +639 -0
  244. package/src/components/media-editor-modal/style.scss +139 -0
  245. package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
  246. package/src/components/media-editor-toolbar/index.tsx +150 -0
  247. package/src/components/media-editor-toolbar/style.scss +21 -0
  248. package/src/components/media-preview/index.tsx +2 -1
  249. package/src/image-editor/README.md +162 -0
  250. package/src/image-editor/core/camera.ts +352 -0
  251. package/src/image-editor/core/constants.ts +93 -0
  252. package/src/image-editor/core/containment.ts +435 -0
  253. package/src/image-editor/core/export/canvas-renderer.ts +253 -0
  254. package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
  255. package/src/image-editor/core/index.ts +35 -0
  256. package/src/image-editor/core/interaction-controller.ts +889 -0
  257. package/src/image-editor/core/math/rotation.ts +26 -0
  258. package/src/image-editor/core/source-region.ts +166 -0
  259. package/src/image-editor/core/state.ts +428 -0
  260. package/src/image-editor/core/stencil-math.ts +330 -0
  261. package/src/image-editor/core/test/camera.ts +593 -0
  262. package/src/image-editor/core/test/interaction-controller.ts +996 -0
  263. package/src/image-editor/core/test/preview-export-parity.ts +242 -0
  264. package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
  265. package/src/image-editor/core/test/state.ts +762 -0
  266. package/src/image-editor/core/test/stencil-math.ts +321 -0
  267. package/src/image-editor/core/transform-style.ts +44 -0
  268. package/src/image-editor/core/transforms/pipeline.ts +43 -0
  269. package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
  270. package/src/image-editor/core/types.ts +177 -0
  271. package/src/image-editor/docs/architecture.md +96 -0
  272. package/src/image-editor/docs/recipes.md +720 -0
  273. package/src/image-editor/docs/roadmap.md +66 -0
  274. package/src/image-editor/index.ts +43 -0
  275. package/src/image-editor/react/components/cropper-provider.tsx +73 -0
  276. package/src/image-editor/react/components/cropper.scss +195 -0
  277. package/src/image-editor/react/components/cropper.tsx +534 -0
  278. package/src/image-editor/react/components/index.ts +3 -0
  279. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
  280. package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
  281. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
  282. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
  283. package/src/image-editor/react/components/test/cropper.tsx +140 -0
  284. package/src/image-editor/react/hooks/index.ts +2 -0
  285. package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
  286. package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
  287. package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
  288. package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
  289. package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
  290. package/src/image-editor/react/hooks/use-interaction.ts +223 -0
  291. package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
  292. package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
  293. package/src/image-editor/stories/style.css +94 -0
  294. package/src/image-editor/style.scss +1 -0
  295. package/src/index.ts +3 -0
  296. package/src/lock-unlock.ts +10 -0
  297. package/src/private-apis.ts +12 -0
  298. package/src/store/actions.ts +33 -0
  299. package/src/store/constants.ts +1 -0
  300. package/src/store/index.ts +20 -0
  301. package/src/store/reducer.ts +48 -0
  302. package/src/store/selectors.ts +16 -0
  303. package/src/store/test/reducer.ts +57 -0
  304. package/src/style.scss +4 -0
  305. package/tsconfig.json +10 -2
  306. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,435 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { mat2d, vec2 } from 'gl-matrix';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { CropperState, NormalizedRect, Size } from './types';
10
+ import { degreesToRadians } from './math/rotation';
11
+ import { createCamera, getRotatedBBox, getVisibleBounds } from './camera';
12
+
13
+ /** Floating-point epsilon for "close enough to equal" comparisons. */
14
+ const EPSILON = 1e-9;
15
+
16
+ /**
17
+ * Compute the visual (rotated) bounding-box dimensions in pixel-proportional
18
+ * units where the unrotated image is `a × 1` (renderedW = a, renderedH = 1).
19
+ *
20
+ * @param rotation Rotation angle in degrees.
21
+ * @param imageAspectRatio Image width / height ratio.
22
+ * @return Visual dimensions and trig helpers.
23
+ */
24
+ function getVisualDimensions(
25
+ rotation: number,
26
+ imageAspectRatio: number
27
+ ): { visualW: number; visualH: number; absC: number; absS: number } {
28
+ const rad = degreesToRadians( rotation );
29
+ const absC = Math.abs( Math.cos( rad ) );
30
+ const absS = Math.abs( Math.sin( rad ) );
31
+ const bbox = getRotatedBBox( imageAspectRatio, 1, rotation );
32
+ return {
33
+ visualW: bbox.width,
34
+ visualH: bbox.height,
35
+ absC,
36
+ absS,
37
+ };
38
+ }
39
+
40
+ /**
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
46
+ * rotation-dependent visual dimensions before projecting into the image-local
47
+ * frame.
48
+ *
49
+ * @param rotation Rotation angle in degrees.
50
+ * @param imageAspectRatio Image width / height ratio.
51
+ * @param cropRect The crop rectangle in normalized coordinates.
52
+ * @return The minimum zoom factor (always >= 1).
53
+ */
54
+ function getMinZoomForCover(
55
+ rotation: number,
56
+ imageAspectRatio: number,
57
+ cropRect: NormalizedRect
58
+ ): number {
59
+ const aspectRatio = Math.max( imageAspectRatio, Number.EPSILON );
60
+ // The crop rect lives in the SNAP-rotation visual bbox — that's how the
61
+ // stencil and preview position it (see createCamera / getImageFit). Use
62
+ // the snap-rotation dimensions to convert crop fractions to pixels.
63
+ const snapRotation = Math.round( rotation / 90 ) * 90;
64
+ const { visualW: snapVisualW, visualH: snapVisualH } = getVisualDimensions(
65
+ snapRotation,
66
+ aspectRatio
67
+ );
68
+ // Projection into the image-local frame uses the TRUE rotation angle —
69
+ // that's the actual orientation of the image under the stencil.
70
+ const { absC, absS } = getVisualDimensions( rotation, aspectRatio );
71
+
72
+ // Crop half-extents in pixel-proportional space.
73
+ const cropHalfW = ( cropRect.width * snapVisualW ) / 2;
74
+ const cropHalfH = ( cropRect.height * snapVisualH ) / 2;
75
+
76
+ // AABB of the crop rect projected into the image-local (unrotated) frame.
77
+ const spanAlpha = cropHalfW * absC + cropHalfH * absS;
78
+ const spanBeta = cropHalfW * absS + cropHalfH * absC;
79
+
80
+ // Image half-extents at zoom z: (aspectRatio*z/2, z/2).
81
+ // Coverage requires: aspectRatio*z/2 >= spanAlpha AND z/2 >= spanBeta.
82
+ const zoomFromAlpha = ( 2 * spanAlpha ) / aspectRatio;
83
+ const zoomFromBeta = 2 * spanBeta;
84
+
85
+ return Math.max( 1, zoomFromAlpha, zoomFromBeta );
86
+ }
87
+
88
+ /**
89
+ * Compute the crop handle bounds in normalized visual space by finding the
90
+ * axis-aligned bounding box (AABB) of the actual image footprint at the
91
+ * current pan/zoom/rotation, then intersecting with the container viewport.
92
+ *
93
+ * Unlike a static centerline-based calculation, this accounts for the
94
+ * current pan position — if the user pans right, the left bound tightens
95
+ * because the image's left edge has moved right.
96
+ *
97
+ * @param state The current cropper state (crop, zoom, rotation, flip).
98
+ * @param elementSize The fitted (unrotated) image element dimensions in pixels.
99
+ * @param visualSize The visual (rotated) image bounding box in pixels.
100
+ * @param containerSize The container dimensions in pixels.
101
+ * @return The min/max x and y that a crop rect edge can reach in normalized space.
102
+ */
103
+ export function getCropBounds(
104
+ state: CropperState,
105
+ elementSize: Size,
106
+ visualSize: Size,
107
+ containerSize: Size
108
+ ): { minX: number; minY: number; maxX: number; maxY: number } {
109
+ if (
110
+ elementSize.width === 0 ||
111
+ elementSize.height === 0 ||
112
+ visualSize.width === 0 ||
113
+ visualSize.height === 0
114
+ ) {
115
+ return { minX: 0, minY: 0, maxX: 1, maxY: 1 };
116
+ }
117
+
118
+ // Build the same CSS matrix as use-transform-style: flip * rotate * zoom.
119
+ // Flip is outside rotation so it acts in viewport axes (see createCamera).
120
+ const tx = state.pan.x * visualSize.width;
121
+ const ty = state.pan.y * visualSize.height;
122
+ const rad = degreesToRadians( state.rotation );
123
+ const cos = Math.cos( rad );
124
+ const sin = Math.sin( rad );
125
+ const sx = state.flip.horizontal ? -1 : 1;
126
+ const sy = state.flip.vertical ? -1 : 1;
127
+ const z = state.zoom;
128
+ const ma = sx * cos * z;
129
+ const mb = sy * sin * z;
130
+ const mc = -sx * sin * z;
131
+ const md = sy * cos * z;
132
+
133
+ // Image element corners relative to element center.
134
+ const hw = elementSize.width / 2;
135
+ const hh = elementSize.height / 2;
136
+ const corners = [
137
+ [ -hw, -hh ],
138
+ [ hw, -hh ],
139
+ [ hw, hh ],
140
+ [ -hw, hh ],
141
+ ];
142
+
143
+ // Transform corners through CSS matrix → screen offsets from element center.
144
+ // Screen position = element_center + transformed_offset.
145
+ // Element center is at (containerW/2, containerH/2) after CSS centering.
146
+ // But we want normalized coords, so work relative to the visual image origin.
147
+ const offsetX = ( containerSize.width - visualSize.width ) / 2;
148
+ const offsetY = ( containerSize.height - visualSize.height ) / 2;
149
+
150
+ let imgMinX = Infinity;
151
+ let imgMaxX = -Infinity;
152
+ let imgMinY = Infinity;
153
+ let imgMaxY = -Infinity;
154
+
155
+ for ( const [ cx, cy ] of corners ) {
156
+ // Screen offset from element center after CSS matrix.
157
+ const screenX = ma * cx + mc * cy + tx;
158
+ const screenY = mb * cx + md * cy + ty;
159
+ // Convert to normalized visual space.
160
+ // The element center is at the visual image center, which is at
161
+ // normalized (0.5, 0.5). So screen offset / visualSize + 0.5.
162
+ const nx = screenX / visualSize.width + 0.5;
163
+ const ny = screenY / visualSize.height + 0.5;
164
+ imgMinX = Math.min( imgMinX, nx );
165
+ imgMaxX = Math.max( imgMaxX, nx );
166
+ imgMinY = Math.min( imgMinY, ny );
167
+ imgMaxY = Math.max( imgMaxY, ny );
168
+ }
169
+
170
+ // Container bounds in normalized space.
171
+ const containerMinX = -offsetX / visualSize.width;
172
+ const containerMaxX = ( containerSize.width - offsetX ) / visualSize.width;
173
+ const containerMinY = -offsetY / visualSize.height;
174
+ const containerMaxY =
175
+ ( containerSize.height - offsetY ) / visualSize.height;
176
+
177
+ // Crop bounds = intersection of image AABB and container bounds.
178
+ return {
179
+ minX: Math.max( imgMinX, containerMinX ),
180
+ minY: Math.max( imgMinY, containerMinY ),
181
+ maxX: Math.min( imgMaxX, containerMaxX ),
182
+ maxY: Math.min( imgMaxY, containerMaxY ),
183
+ };
184
+ }
185
+
186
+ /**
187
+ * Restricts a crop rectangle so that the rotated, zoomed image can fully cover it.
188
+ * If the crop rect is too large for the current zoom and rotation, it is scaled
189
+ * down proportionally and re-centered.
190
+ *
191
+ * Works in pixel-proportional space where the unrotated image is a×1.
192
+ *
193
+ * @param cropRect The crop rectangle in normalized coordinates.
194
+ * @param zoom The current zoom factor.
195
+ * @param rotation The rotation angle in degrees.
196
+ * @param imageAspectRatio The image width / height ratio.
197
+ * @return The restricted crop rectangle.
198
+ */
199
+ export function restrictCropRect(
200
+ cropRect: NormalizedRect,
201
+ zoom: number,
202
+ rotation: number,
203
+ imageAspectRatio: number
204
+ ): NormalizedRect {
205
+ const aspectRatio = Math.max( imageAspectRatio, Number.EPSILON );
206
+ // Crop rect lives in the SNAP-rotation visual bbox (matching the stencil
207
+ // layout); projection into the image-local frame uses the TRUE rotation.
208
+ const snapRotation = Math.round( rotation / 90 ) * 90;
209
+ const { visualW, visualH } = getVisualDimensions(
210
+ snapRotation,
211
+ aspectRatio
212
+ );
213
+ const { absC, absS } = getVisualDimensions( rotation, aspectRatio );
214
+ const W = cropRect.width;
215
+ const H = cropRect.height;
216
+
217
+ // Crop full-extents in pixel-proportional space, projected to image-local frame.
218
+ const cropWPx = W * visualW;
219
+ const cropHPx = H * visualH;
220
+ const spanAlpha = cropWPx * absC + cropHPx * absS;
221
+ const spanBeta = cropWPx * absS + cropHPx * absC;
222
+
223
+ // Image full-extents at zoom z: (aspectRatio*z, z).
224
+ const limitAlpha = aspectRatio * zoom;
225
+ const limitBeta = zoom;
226
+
227
+ let t = 1;
228
+ if ( spanAlpha > 0 ) {
229
+ t = Math.min( t, limitAlpha / spanAlpha );
230
+ }
231
+ if ( spanBeta > 0 ) {
232
+ t = Math.min( t, limitBeta / spanBeta );
233
+ }
234
+ if ( t >= 1 - EPSILON ) {
235
+ // Crop fits at the current zoom — no size change needed.
236
+ // Position is handled by restrictPanZoom, not here.
237
+ return cropRect;
238
+ }
239
+ const newW = W * t;
240
+ const newH = H * t;
241
+ const centerX = cropRect.x + W / 2;
242
+ const centerY = cropRect.y + H / 2;
243
+ let newX = centerX - newW / 2;
244
+ let newY = centerY - newH / 2;
245
+ newX = Math.max( 0, Math.min( newX, 1 - newW ) );
246
+ newY = Math.max( 0, Math.min( newY, 1 - newH ) );
247
+ return { x: newX, y: newY, width: newW, height: newH };
248
+ }
249
+
250
+ /**
251
+ * Canonical container used internally by restrictPanZoom.
252
+ * Containment is scale-invariant, so the actual size doesn't matter —
253
+ * only the relative geometry between stencil and image matters.
254
+ */
255
+ const CANONICAL_CONTAINER: Size = { width: 1000, height: 1000 };
256
+
257
+ // Pre-allocated scratch buffers for restrictPanZoom's hot path (runs on
258
+ // every pointermove) to avoid Float32Array allocation per call.
259
+ const _scratchMat = mat2d.create();
260
+ const _scratchVec = vec2.create();
261
+
262
+ /**
263
+ * Clamps pan and adjusts zoom so that the zoomed, rotated image fully covers
264
+ * the crop rectangle.
265
+ *
266
+ * Uses the camera matrix to project: builds a camera from the candidate state,
267
+ * maps the stencil corners (axis-aligned in the visual bounding box) to world
268
+ * space via the inverse camera, and checks that all world points lie within
269
+ * [0,1]×[0,1]. If any point is outside, computes the minimal pan correction.
270
+ *
271
+ * @param state The current cropper state.
272
+ * @param imageSize The natural size of the image in pixels.
273
+ * @param cropRect The crop rectangle in normalized coordinates.
274
+ * @return The restricted crop pan and zoom values.
275
+ */
276
+ export function restrictPanZoom(
277
+ state: CropperState,
278
+ imageSize: Size,
279
+ cropRect: NormalizedRect
280
+ ): { pan: { x: number; y: number }; zoom: number } {
281
+ // Algorithm overview:
282
+ // 1. Ensure zoom is high enough for the rotated image to cover the crop.
283
+ // 2. Build a camera (world→screen matrix) with the candidate state.
284
+ // 3. Build a base camera (no pan, zoom=1) to find where the stencil
285
+ // sits in screen space — the stencil is anchored to the container,
286
+ // not the image, so it doesn't move with pan or scale with zoom.
287
+ // 4. Project the 4 stencil corners through the INVERSE of the main
288
+ // camera to get world-space positions. A world point outside [0,1]
289
+ // means that corner of the stencil isn't covered by the image.
290
+ // 5. Compute the minimal translation in world space to push all corners
291
+ // back inside [0,1].
292
+ // 6. Convert the world-space correction to a pan-field correction by
293
+ // mapping it through the camera's linear part back to screen space,
294
+ // then dividing by the visual bounds.
295
+ const aspectRatio =
296
+ imageSize.width > 0 && imageSize.height > 0
297
+ ? imageSize.width / imageSize.height
298
+ : 1;
299
+ const minZoom = getMinZoomForCover( state.rotation, aspectRatio, cropRect );
300
+ const zoom = Math.max( state.zoom, minZoom );
301
+
302
+ // Step 2: build camera with candidate pan and corrected zoom.
303
+ const candidateState = { ...state, zoom };
304
+ const camera = createCamera(
305
+ candidateState,
306
+ CANONICAL_CONTAINER,
307
+ imageSize
308
+ );
309
+
310
+ // Build a base camera (zero pan, zoom=1) to get stencil positions.
311
+ // The stencil is anchored to the container and laid out using the
312
+ // nearest 90° rotation (matching `getImageFit`), so it's stable
313
+ // through fine rotation. CSS zoom only affects the <img> element,
314
+ // not the stencil.
315
+ const snapRotation = Math.round( state.rotation / 90 ) * 90;
316
+ const baseCamera = createCamera(
317
+ {
318
+ ...candidateState,
319
+ pan: { x: 0, y: 0 },
320
+ zoom: 1,
321
+ rotation: snapRotation,
322
+ },
323
+ CANONICAL_CONTAINER,
324
+ imageSize
325
+ );
326
+ const visibleBounds = getVisibleBounds( baseCamera );
327
+
328
+ // Stencil corners in screen space (axis-aligned rect within visual bounds).
329
+ const stencilCorners: [ number, number ][] = [
330
+ [
331
+ visibleBounds.left + cropRect.x * visibleBounds.width,
332
+ visibleBounds.top + cropRect.y * visibleBounds.height,
333
+ ],
334
+ [
335
+ visibleBounds.left +
336
+ ( cropRect.x + cropRect.width ) * visibleBounds.width,
337
+ visibleBounds.top + cropRect.y * visibleBounds.height,
338
+ ],
339
+ [
340
+ visibleBounds.left +
341
+ ( cropRect.x + cropRect.width ) * visibleBounds.width,
342
+ visibleBounds.top +
343
+ ( cropRect.y + cropRect.height ) * visibleBounds.height,
344
+ ],
345
+ [
346
+ visibleBounds.left + cropRect.x * visibleBounds.width,
347
+ visibleBounds.top +
348
+ ( cropRect.y + cropRect.height ) * visibleBounds.height,
349
+ ],
350
+ ];
351
+
352
+ // Map stencil corners to world space via inverse camera.
353
+ // If a world point is outside [0,1], the image doesn't cover that spot.
354
+ // Reuse module-level scratch buffers to avoid allocation per frame.
355
+ mat2d.invert( _scratchMat, camera );
356
+
357
+ let minWx = Infinity;
358
+ let maxWx = -Infinity;
359
+ let minWy = Infinity;
360
+ let maxWy = -Infinity;
361
+
362
+ for ( const corner of stencilCorners ) {
363
+ vec2.transformMat2d( _scratchVec, corner, _scratchMat );
364
+ const w = _scratchVec;
365
+ if ( w[ 0 ] < minWx ) {
366
+ minWx = w[ 0 ];
367
+ }
368
+ if ( w[ 0 ] > maxWx ) {
369
+ maxWx = w[ 0 ];
370
+ }
371
+ if ( w[ 1 ] < minWy ) {
372
+ minWy = w[ 1 ];
373
+ }
374
+ if ( w[ 1 ] > maxWy ) {
375
+ maxWy = w[ 1 ];
376
+ }
377
+ }
378
+
379
+ // If all world points are in [0,1], no correction needed.
380
+ if (
381
+ minWx >= -EPSILON &&
382
+ maxWx <= 1 + EPSILON &&
383
+ minWy >= -EPSILON &&
384
+ maxWy <= 1 + EPSILON
385
+ ) {
386
+ return { pan: state.pan, zoom };
387
+ }
388
+
389
+ // Compute world-space correction needed.
390
+ // If minWx < 0, we need to shift world points right by |minWx|.
391
+ // If maxWx > 1, we need to shift world points left by (maxWx - 1).
392
+ // If both, we're over-constrained (crop too big) — getMinZoomForCover
393
+ // should have prevented this.
394
+ let dwx = 0;
395
+ let dwy = 0;
396
+
397
+ if ( minWx < 0 && maxWx <= 1 + EPSILON ) {
398
+ dwx = -minWx;
399
+ } else if ( maxWx > 1 && minWx >= -EPSILON ) {
400
+ dwx = 1 - maxWx;
401
+ } else if ( minWx < 0 && maxWx > 1 ) {
402
+ // Over-constrained: center it.
403
+ dwx = ( 1 - maxWx - minWx ) / 2;
404
+ }
405
+
406
+ if ( minWy < 0 && maxWy <= 1 + EPSILON ) {
407
+ dwy = -minWy;
408
+ } else if ( maxWy > 1 && minWy >= -EPSILON ) {
409
+ dwy = 1 - maxWy;
410
+ } else if ( minWy < 0 && maxWy > 1 ) {
411
+ dwy = ( 1 - maxWy - minWy ) / 2;
412
+ }
413
+
414
+ // Convert world-space correction to screen-space correction.
415
+ // The camera's 2×2 linear part (indices [0,1,2,3]) maps world deltas
416
+ // to screen deltas: screenDelta = linear * worldDelta.
417
+ const dsx = camera[ 0 ] * dwx + camera[ 2 ] * dwy;
418
+ const dsy = camera[ 1 ] * dwx + camera[ 3 ] * dwy;
419
+
420
+ // Convert screen-space correction to pan-field correction.
421
+ // Pan in screen pixels = pan.x * visualW, pan.y * visualH.
422
+ // The correction is subtractive: a positive world shift (dw > 0) means
423
+ // the image needs to move opposite to pan direction, so pan decreases.
424
+ const newPanX =
425
+ state.pan.x -
426
+ ( visibleBounds.width > 0 ? dsx / visibleBounds.width : 0 );
427
+ const newPanY =
428
+ state.pan.y -
429
+ ( visibleBounds.height > 0 ? dsy / visibleBounds.height : 0 );
430
+
431
+ return {
432
+ pan: { x: newPanX, y: newPanY },
433
+ zoom,
434
+ };
435
+ }
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { CropperState } from '../types';
5
+ import { createExportCamera, getRotatedBBox } from '../camera';
6
+
7
+ /** Default export quality for lossy formats (JPEG, WebP). */
8
+ const DEFAULT_QUALITY = 0.92;
9
+
10
+ /**
11
+ * Load an image from a URL with CORS support.
12
+ *
13
+ * @param src - The image URL to load.
14
+ * @return A promise that resolves to the loaded HTMLImageElement.
15
+ */
16
+ export function loadImage( src: string ): Promise< HTMLImageElement > {
17
+ return new Promise( ( resolve, reject ) => {
18
+ const image = new Image();
19
+ image.addEventListener( 'load', () => resolve( image ) );
20
+ image.addEventListener( 'error', ( error ) => reject( error ) );
21
+ image.crossOrigin = 'anonymous';
22
+ image.src = src;
23
+ } );
24
+ }
25
+
26
+ /**
27
+ * Apply rectangular crop, rotation, and flip transforms to produce a canvas.
28
+ *
29
+ * Uses createExportCamera to compose the full transform matrix, then applies
30
+ * it in a single ctx.setTransform call before drawing the image.
31
+ *
32
+ * @param image - The source image element.
33
+ * @param state - The full cropper state containing crop, rotation, and flip settings.
34
+ * @return A canvas element containing the transformed and cropped image.
35
+ */
36
+ export function renderToCanvas(
37
+ image: HTMLImageElement,
38
+ state: CropperState
39
+ ): HTMLCanvasElement {
40
+ const { naturalWidth, naturalHeight } = image;
41
+ const { rotation, cropRect } = state;
42
+ const imageSize = { width: naturalWidth, height: naturalHeight };
43
+
44
+ // Output size is the crop region in the snap-rotation bbox — matches
45
+ // the preview's reference frame so the exported pixels track exactly
46
+ // what the stencil framed, even at fine rotation angles.
47
+ const snapRotation = Math.round( rotation / 90 ) * 90;
48
+ const rotBBox = getRotatedBBox( naturalWidth, naturalHeight, snapRotation );
49
+ const outW = Math.round( cropRect.width * rotBBox.width );
50
+ const outH = Math.round( cropRect.height * rotBBox.height );
51
+
52
+ const canvas = document.createElement( 'canvas' );
53
+ canvas.width = outW;
54
+ canvas.height = outH;
55
+ const ctx = canvas.getContext( '2d' );
56
+ if ( ! ctx ) {
57
+ throw new Error(
58
+ 'Could not obtain a 2D context for the export canvas.'
59
+ );
60
+ }
61
+
62
+ const camera = createExportCamera( state, imageSize, {
63
+ width: outW,
64
+ height: outH,
65
+ } );
66
+ ctx.setTransform(
67
+ camera[ 0 ],
68
+ camera[ 1 ],
69
+ camera[ 2 ],
70
+ camera[ 3 ],
71
+ camera[ 4 ],
72
+ camera[ 5 ]
73
+ );
74
+ ctx.drawImage( image, 0, 0 );
75
+ return canvas;
76
+ }
77
+
78
+ /**
79
+ * Convert a canvas to a Blob with configurable MIME type and quality.
80
+ *
81
+ * @param canvas - The canvas to export.
82
+ * @param mimeType - The output MIME type. Defaults to 'image/png'.
83
+ * @param quality - The quality parameter for lossy formats (0-1). Defaults to DEFAULT_QUALITY.
84
+ * @return A promise that resolves to the canvas content as a Blob.
85
+ */
86
+ export function canvasToBlob(
87
+ canvas: HTMLCanvasElement,
88
+ mimeType: string = 'image/png',
89
+ quality: number = DEFAULT_QUALITY
90
+ ): Promise< Blob > {
91
+ return new Promise( ( resolve, reject ) => {
92
+ canvas.toBlob(
93
+ ( blob ) => {
94
+ if ( blob ) {
95
+ resolve( blob );
96
+ } else {
97
+ reject( new Error( 'Canvas toBlob returned null.' ) );
98
+ }
99
+ },
100
+ mimeType,
101
+ quality
102
+ );
103
+ } );
104
+ }
105
+
106
+ /**
107
+ * Convert a canvas to a data URL string.
108
+ *
109
+ * @param canvas - The canvas to export.
110
+ * @param mimeType - The output MIME type. Defaults to 'image/png'.
111
+ * @param quality - The quality parameter for lossy formats (0-1). Defaults to DEFAULT_QUALITY.
112
+ * @return The canvas content as a data URL string.
113
+ */
114
+ export function canvasToDataURL(
115
+ canvas: HTMLCanvasElement,
116
+ mimeType: string = 'image/png',
117
+ quality: number = DEFAULT_QUALITY
118
+ ): string {
119
+ return canvas.toDataURL( mimeType, quality );
120
+ }
121
+
122
+ /**
123
+ * Load an image, render with transforms, and export as a Blob.
124
+ *
125
+ * Throws on failure so callers can distinguish error cases:
126
+ * - Image load failures (network errors, 404s) throw the native
127
+ * load error from the Image element.
128
+ * - Cross-origin failures manifest as tainted-canvas errors from
129
+ * `canvas.toBlob()` — the browser rejects reading pixel data
130
+ * from a canvas that drew an image without valid CORS headers.
131
+ * Ensure the source server sets `Access-Control-Allow-Origin`.
132
+ * - Canvas context creation failures throw a descriptive Error.
133
+ *
134
+ * Only works in browser environments (needs DOM / HTMLCanvasElement).
135
+ *
136
+ * @param src - The image URL to load.
137
+ * @param state - The cropper state with all transform settings.
138
+ * @param mimeType - The output MIME type. Defaults to 'image/png'.
139
+ * @param quality - The quality parameter for lossy formats (0-1). Defaults to DEFAULT_QUALITY.
140
+ * @return A promise that resolves to the exported Blob.
141
+ * @throws If the image fails to load, canvas creation fails, or
142
+ * the resulting canvas is tainted by CORS restrictions.
143
+ */
144
+ export async function exportCroppedImage(
145
+ src: string,
146
+ state: CropperState,
147
+ mimeType: string = 'image/png',
148
+ quality: number = DEFAULT_QUALITY
149
+ ): Promise< Blob > {
150
+ const image = await loadImage( src );
151
+ const canvas = renderToCanvas( image, state );
152
+ return canvasToBlob( canvas, mimeType, quality );
153
+ }
154
+
155
+ /**
156
+ * Apply the cropper's transform (crop, rotation, flip, zoom) to an existing
157
+ * canvas or image source. This is the bridge for multi-step editing pipelines
158
+ * where an upstream tool (e.g., brightness/color adjustment) has already
159
+ * processed the image and you want to apply the crop on top.
160
+ *
161
+ * Accepts any CanvasImageSource: HTMLImageElement, HTMLCanvasElement,
162
+ * OffscreenCanvas, ImageBitmap, HTMLVideoElement, etc.
163
+ *
164
+ * @param source - The image source to crop/transform.
165
+ * @param sourceSize - The dimensions of the source (natural width/height).
166
+ * @param sourceSize.width - The width of the source in pixels.
167
+ * @param sourceSize.height - The height of the source in pixels.
168
+ * @param state - The cropper state with all transform settings.
169
+ * @param outputSize - Optional output size. Defaults to the crop region's natural pixel size.
170
+ * @param outputSize.width - The width of the output in pixels.
171
+ * @param outputSize.height - The height of the output in pixels.
172
+ * @return A canvas with the transforms applied.
173
+ */
174
+ export function applyToCanvas(
175
+ source: CanvasImageSource,
176
+ sourceSize: { width: number; height: number },
177
+ state: CropperState,
178
+ outputSize?: { width: number; height: number }
179
+ ): HTMLCanvasElement {
180
+ // Use snap-rotation bbox for default sizing so the default output
181
+ // matches the preview's reference frame at fine rotation angles
182
+ // (see renderToCanvas / createExportCamera).
183
+ const snapRotation = Math.round( state.rotation / 90 ) * 90;
184
+ const rotBBox = getRotatedBBox(
185
+ sourceSize.width,
186
+ sourceSize.height,
187
+ snapRotation
188
+ );
189
+
190
+ // Default output size: the crop region in natural pixels.
191
+ const outW =
192
+ outputSize?.width ?? Math.round( state.cropRect.width * rotBBox.width );
193
+ const outH =
194
+ outputSize?.height ??
195
+ Math.round( state.cropRect.height * rotBBox.height );
196
+
197
+ const canvas = document.createElement( 'canvas' );
198
+ canvas.width = outW;
199
+ canvas.height = outH;
200
+ const ctx = canvas.getContext( '2d' );
201
+ if ( ! ctx ) {
202
+ throw new Error(
203
+ 'Could not obtain a 2D context for the export canvas.'
204
+ );
205
+ }
206
+
207
+ const camera = createExportCamera( state, sourceSize, {
208
+ width: outW,
209
+ height: outH,
210
+ } );
211
+ ctx.setTransform(
212
+ camera[ 0 ],
213
+ camera[ 1 ],
214
+ camera[ 2 ],
215
+ camera[ 3 ],
216
+ camera[ 4 ],
217
+ camera[ 5 ]
218
+ );
219
+ ctx.drawImage( source, 0, 0 );
220
+ return canvas;
221
+ }
222
+
223
+ /**
224
+ * Download the cropped image as a file.
225
+ *
226
+ * Loads the source image, applies all transforms, and triggers a
227
+ * browser download via exportCroppedImage() + object URL + anchor click.
228
+ *
229
+ * Throws on export failure (see `exportCroppedImage`).
230
+ *
231
+ * @param src - The image URL to load.
232
+ * @param state - The cropper state with all transform settings.
233
+ * @param filename - The download filename. Defaults to 'cropped-image'.
234
+ * @param mimeType - The output MIME type. Defaults to 'image/png'.
235
+ * @param quality - The quality parameter for lossy formats (0-1). Defaults to DEFAULT_QUALITY.
236
+ * @return A promise that resolves when the download is triggered.
237
+ */
238
+ export async function downloadCroppedImage(
239
+ src: string,
240
+ state: CropperState,
241
+ filename: string = 'cropped-image',
242
+ mimeType: string = 'image/png',
243
+ quality: number = DEFAULT_QUALITY
244
+ ): Promise< void > {
245
+ const blob = await exportCroppedImage( src, state, mimeType, quality );
246
+ const ext = mimeType.split( '/' )[ 1 ] ?? 'png';
247
+ const url = URL.createObjectURL( blob );
248
+ const a = document.createElement( 'a' );
249
+ a.href = url;
250
+ a.download = `${ filename }.${ ext }`;
251
+ a.click();
252
+ URL.revokeObjectURL( url );
253
+ }