@wordpress/media-editor 0.7.1-next.v.202604201441.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (300) 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/image-editor/core/camera.cjs +194 -0
  12. package/build/image-editor/core/camera.cjs.map +7 -0
  13. package/build/image-editor/core/constants.cjs +81 -0
  14. package/build/image-editor/core/constants.cjs.map +7 -0
  15. package/build/image-editor/core/containment.cjs +245 -0
  16. package/build/image-editor/core/containment.cjs.map +7 -0
  17. package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
  18. package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
  19. package/build/image-editor/core/index.cjs +50 -0
  20. package/build/image-editor/core/index.cjs.map +7 -0
  21. package/build/image-editor/core/interaction-controller.cjs +608 -0
  22. package/build/image-editor/core/interaction-controller.cjs.map +7 -0
  23. package/build/image-editor/core/math/rotation.cjs +41 -0
  24. package/build/image-editor/core/math/rotation.cjs.map +7 -0
  25. package/build/image-editor/core/source-region.cjs +95 -0
  26. package/build/image-editor/core/source-region.cjs.map +7 -0
  27. package/build/image-editor/core/state.cjs +269 -0
  28. package/build/image-editor/core/state.cjs.map +7 -0
  29. package/build/image-editor/core/stencil-math.cjs +197 -0
  30. package/build/image-editor/core/stencil-math.cjs.map +7 -0
  31. package/build/image-editor/core/transform-style.cjs +46 -0
  32. package/build/image-editor/core/transform-style.cjs.map +7 -0
  33. package/build/image-editor/core/transforms/pipeline.cjs +43 -0
  34. package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
  35. package/build/image-editor/core/types.cjs +19 -0
  36. package/build/image-editor/core/types.cjs.map +7 -0
  37. package/build/image-editor/index.cjs +57 -0
  38. package/build/image-editor/index.cjs.map +7 -0
  39. package/build/image-editor/react/components/cropper-provider.cjs +50 -0
  40. package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
  41. package/build/image-editor/react/components/cropper.cjs +365 -0
  42. package/build/image-editor/react/components/cropper.cjs.map +7 -0
  43. package/build/image-editor/react/components/index.cjs +36 -0
  44. package/build/image-editor/react/components/index.cjs.map +7 -0
  45. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
  46. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
  47. package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
  48. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
  49. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
  50. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
  51. package/build/image-editor/react/hooks/index.cjs +31 -0
  52. package/build/image-editor/react/hooks/index.cjs.map +7 -0
  53. package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
  54. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
  55. package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
  56. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
  57. package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
  58. package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
  60. package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
  61. package/build/index.cjs +5 -2
  62. package/build/index.cjs.map +2 -2
  63. package/build/lock-unlock.cjs +37 -0
  64. package/build/lock-unlock.cjs.map +7 -0
  65. package/build/private-apis.cjs +38 -0
  66. package/build/private-apis.cjs.map +7 -0
  67. package/build/store/actions.cjs +45 -0
  68. package/build/store/actions.cjs.map +7 -0
  69. package/build/store/constants.cjs +31 -0
  70. package/build/store/constants.cjs.map +7 -0
  71. package/build/store/index.cjs +51 -0
  72. package/build/store/index.cjs.map +7 -0
  73. package/build/store/reducer.cjs +51 -0
  74. package/build/store/reducer.cjs.map +7 -0
  75. package/build/store/selectors.cjs +43 -0
  76. package/build/store/selectors.cjs.map +7 -0
  77. package/build-module/components/media-editor-canvas/index.mjs +33 -0
  78. package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
  79. package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
  80. package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
  81. package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
  82. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
  83. package/build-module/components/media-editor-modal/index.mjs +483 -0
  84. package/build-module/components/media-editor-modal/index.mjs.map +7 -0
  85. package/build-module/components/media-editor-toolbar/index.mjs +130 -0
  86. package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
  87. package/build-module/image-editor/core/camera.mjs +163 -0
  88. package/build-module/image-editor/core/camera.mjs.map +7 -0
  89. package/build-module/image-editor/core/constants.mjs +51 -0
  90. package/build-module/image-editor/core/constants.mjs.map +7 -0
  91. package/build-module/image-editor/core/containment.mjs +218 -0
  92. package/build-module/image-editor/core/containment.mjs.map +7 -0
  93. package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
  94. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
  95. package/build-module/image-editor/core/index.mjs +24 -0
  96. package/build-module/image-editor/core/index.mjs.map +7 -0
  97. package/build-module/image-editor/core/interaction-controller.mjs +583 -0
  98. package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
  99. package/build-module/image-editor/core/math/rotation.mjs +15 -0
  100. package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
  101. package/build-module/image-editor/core/source-region.mjs +69 -0
  102. package/build-module/image-editor/core/source-region.mjs.map +7 -0
  103. package/build-module/image-editor/core/state.mjs +242 -0
  104. package/build-module/image-editor/core/state.mjs.map +7 -0
  105. package/build-module/image-editor/core/stencil-math.mjs +169 -0
  106. package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
  107. package/build-module/image-editor/core/transform-style.mjs +21 -0
  108. package/build-module/image-editor/core/transform-style.mjs.map +7 -0
  109. package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
  110. package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
  111. package/build-module/image-editor/core/types.mjs +1 -0
  112. package/build-module/image-editor/core/types.mjs.map +7 -0
  113. package/build-module/image-editor/index.mjs +30 -0
  114. package/build-module/image-editor/index.mjs.map +7 -0
  115. package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
  116. package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
  117. package/build-module/image-editor/react/components/cropper.mjs +337 -0
  118. package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
  119. package/build-module/image-editor/react/components/index.mjs +9 -0
  120. package/build-module/image-editor/react/components/index.mjs.map +7 -0
  121. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
  122. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
  123. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
  124. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
  125. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
  126. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
  127. package/build-module/image-editor/react/hooks/index.mjs +6 -0
  128. package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
  129. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
  130. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
  131. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
  132. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
  133. package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
  134. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
  135. package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
  136. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
  137. package/build-module/index.mjs +3 -1
  138. package/build-module/index.mjs.map +2 -2
  139. package/build-module/lock-unlock.mjs +11 -0
  140. package/build-module/lock-unlock.mjs.map +7 -0
  141. package/build-module/private-apis.mjs +13 -0
  142. package/build-module/private-apis.mjs.map +7 -0
  143. package/build-module/store/actions.mjs +19 -0
  144. package/build-module/store/actions.mjs.map +7 -0
  145. package/build-module/store/constants.mjs +6 -0
  146. package/build-module/store/constants.mjs.map +7 -0
  147. package/build-module/store/index.mjs +16 -0
  148. package/build-module/store/index.mjs.map +7 -0
  149. package/build-module/store/reducer.mjs +26 -0
  150. package/build-module/store/reducer.mjs.map +7 -0
  151. package/build-module/store/selectors.mjs +16 -0
  152. package/build-module/store/selectors.mjs.map +7 -0
  153. package/build-style/style-rtl.css +315 -0
  154. package/build-style/style.css +315 -0
  155. package/build-types/components/media-editor-canvas/index.d.ts +22 -0
  156. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
  157. package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
  158. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
  159. package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
  160. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
  161. package/build-types/components/media-editor-modal/index.d.ts +20 -0
  162. package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
  163. package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
  164. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  165. package/build-types/image-editor/core/camera.d.ts +107 -0
  166. package/build-types/image-editor/core/camera.d.ts.map +1 -0
  167. package/build-types/image-editor/core/constants.d.ts +46 -0
  168. package/build-types/image-editor/core/constants.d.ts.map +1 -0
  169. package/build-types/image-editor/core/containment.d.ts +61 -0
  170. package/build-types/image-editor/core/containment.d.ts.map +1 -0
  171. package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
  172. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
  173. package/build-types/image-editor/core/index.d.ts +8 -0
  174. package/build-types/image-editor/core/index.d.ts.map +1 -0
  175. package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
  176. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
  177. package/build-types/image-editor/core/math/rotation.d.ts +18 -0
  178. package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
  179. package/build-types/image-editor/core/source-region.d.ts +75 -0
  180. package/build-types/image-editor/core/source-region.d.ts.map +1 -0
  181. package/build-types/image-editor/core/state.d.ts +42 -0
  182. package/build-types/image-editor/core/state.d.ts.map +1 -0
  183. package/build-types/image-editor/core/stencil-math.d.ts +79 -0
  184. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
  185. package/build-types/image-editor/core/transform-style.d.ts +23 -0
  186. package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
  187. package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
  188. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
  189. package/build-types/image-editor/core/types.d.ts +211 -0
  190. package/build-types/image-editor/core/types.d.ts.map +1 -0
  191. package/build-types/image-editor/index.d.ts +16 -0
  192. package/build-types/image-editor/index.d.ts.map +1 -0
  193. package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
  194. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
  195. package/build-types/image-editor/react/components/cropper.d.ts +59 -0
  196. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
  197. package/build-types/image-editor/react/components/index.d.ts +4 -0
  198. package/build-types/image-editor/react/components/index.d.ts.map +1 -0
  199. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
  200. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
  201. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
  202. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
  203. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
  204. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
  205. package/build-types/image-editor/react/hooks/index.d.ts +3 -0
  206. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
  207. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
  208. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
  209. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
  210. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  211. package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
  212. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
  213. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
  214. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
  215. package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
  216. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
  217. package/build-types/index.d.ts +1 -0
  218. package/build-types/index.d.ts.map +1 -1
  219. package/build-types/lock-unlock.d.ts +2 -0
  220. package/build-types/lock-unlock.d.ts.map +1 -0
  221. package/build-types/private-apis.d.ts +2 -0
  222. package/build-types/private-apis.d.ts.map +1 -0
  223. package/build-types/store/actions.d.ts +27 -0
  224. package/build-types/store/actions.d.ts.map +1 -0
  225. package/build-types/store/constants.d.ts +2 -0
  226. package/build-types/store/constants.d.ts.map +1 -0
  227. package/build-types/store/index.d.ts +4 -0
  228. package/build-types/store/index.d.ts.map +1 -0
  229. package/build-types/store/reducer.d.ts +23 -0
  230. package/build-types/store/reducer.d.ts.map +1 -0
  231. package/build-types/store/selectors.d.ts +8 -0
  232. package/build-types/store/selectors.d.ts.map +1 -0
  233. package/package.json +21 -7
  234. package/src/components/media-editor-canvas/index.tsx +56 -0
  235. package/src/components/media-editor-canvas/style.scss +9 -0
  236. package/src/components/media-editor-crop-panel/index.tsx +143 -0
  237. package/src/components/media-editor-modal/build-modifiers.ts +164 -0
  238. package/src/components/media-editor-modal/index.tsx +639 -0
  239. package/src/components/media-editor-modal/style.scss +139 -0
  240. package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
  241. package/src/components/media-editor-toolbar/index.tsx +150 -0
  242. package/src/components/media-editor-toolbar/style.scss +21 -0
  243. package/src/image-editor/README.md +162 -0
  244. package/src/image-editor/core/camera.ts +352 -0
  245. package/src/image-editor/core/constants.ts +93 -0
  246. package/src/image-editor/core/containment.ts +435 -0
  247. package/src/image-editor/core/export/canvas-renderer.ts +253 -0
  248. package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
  249. package/src/image-editor/core/index.ts +35 -0
  250. package/src/image-editor/core/interaction-controller.ts +889 -0
  251. package/src/image-editor/core/math/rotation.ts +26 -0
  252. package/src/image-editor/core/source-region.ts +166 -0
  253. package/src/image-editor/core/state.ts +428 -0
  254. package/src/image-editor/core/stencil-math.ts +330 -0
  255. package/src/image-editor/core/test/camera.ts +593 -0
  256. package/src/image-editor/core/test/interaction-controller.ts +996 -0
  257. package/src/image-editor/core/test/preview-export-parity.ts +242 -0
  258. package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
  259. package/src/image-editor/core/test/state.ts +762 -0
  260. package/src/image-editor/core/test/stencil-math.ts +321 -0
  261. package/src/image-editor/core/transform-style.ts +44 -0
  262. package/src/image-editor/core/transforms/pipeline.ts +43 -0
  263. package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
  264. package/src/image-editor/core/types.ts +177 -0
  265. package/src/image-editor/docs/architecture.md +96 -0
  266. package/src/image-editor/docs/recipes.md +720 -0
  267. package/src/image-editor/docs/roadmap.md +66 -0
  268. package/src/image-editor/index.ts +43 -0
  269. package/src/image-editor/react/components/cropper-provider.tsx +73 -0
  270. package/src/image-editor/react/components/cropper.scss +195 -0
  271. package/src/image-editor/react/components/cropper.tsx +534 -0
  272. package/src/image-editor/react/components/index.ts +3 -0
  273. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
  274. package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
  275. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
  276. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
  277. package/src/image-editor/react/components/test/cropper.tsx +140 -0
  278. package/src/image-editor/react/hooks/index.ts +2 -0
  279. package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
  280. package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
  281. package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
  282. package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
  283. package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
  284. package/src/image-editor/react/hooks/use-interaction.ts +223 -0
  285. package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
  286. package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
  287. package/src/image-editor/stories/style.css +94 -0
  288. package/src/image-editor/style.scss +1 -0
  289. package/src/index.ts +3 -0
  290. package/src/lock-unlock.ts +10 -0
  291. package/src/private-apis.ts +12 -0
  292. package/src/store/actions.ts +33 -0
  293. package/src/store/constants.ts +1 -0
  294. package/src/store/index.ts +20 -0
  295. package/src/store/reducer.ts +48 -0
  296. package/src/store/selectors.ts +16 -0
  297. package/src/store/test/reducer.ts +57 -0
  298. package/src/style.scss +4 -0
  299. package/tsconfig.json +10 -2
  300. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Preview ↔ Export parity — the load-bearing invariant of an image cropper.
3
+ *
4
+ * "What you see in the stencil is what you get in the exported file."
5
+ * If this ever breaks silently, users can't trust the tool.
6
+ *
7
+ * How the test works (read this before changing anything):
8
+ *
9
+ * 1. Pick a state (rotation, zoom, pan, flip, cropRect).
10
+ * 2. Pick a position (u, v) inside the crop rect, where (0,0) is the
11
+ * stencil's top-left and (1,1) is its bottom-right.
12
+ * 3. Compute the source-image pixel the PREVIEW shows at (u, v):
13
+ * - Screen point = stencil origin + (u, v) * stencil size.
14
+ * - Inverse of createCamera → world → × imageSize → source pixel.
15
+ * 4. Compute the source-image pixel the EXPORT picks at the SAME (u, v):
16
+ * - Output canvas point = (u * outW, v * outH).
17
+ * - Inverse of createExportCamera → source pixel.
18
+ * 5. Assert they agree.
19
+ *
20
+ * The (u, v) parametrization is deliberate: both the preview and the
21
+ * export map the same (u, v) back to a source pixel through entirely
22
+ * different code paths. If the two paths disagree anywhere in the
23
+ * rotation / zoom / pan / flip / crop / output-size space, the
24
+ * invariant has been broken and users see something different from
25
+ * what they download.
26
+ *
27
+ * Why this is load-bearing: the crop regression that shifted exports
28
+ * sideways during fine rotation would have been caught by this test —
29
+ * the center would agree (symmetric bug) but the corners would drift.
30
+ */
31
+ import { mat2d, vec2 } from 'gl-matrix';
32
+
33
+ import {
34
+ createCamera,
35
+ createExportCamera,
36
+ screenToWorld,
37
+ getVisibleBounds,
38
+ } from '../camera';
39
+ import { DEFAULT_STATE } from '../constants';
40
+ import type { CropperState, Size } from '../types';
41
+
42
+ const CONTAINER: Size = { width: 800, height: 600 };
43
+ const IMAGE: Size = { width: 1600, height: 900 };
44
+
45
+ function makeState( overrides: Partial< CropperState > = {} ): CropperState {
46
+ return {
47
+ ...DEFAULT_STATE,
48
+ image: {
49
+ src: 'test.jpg',
50
+ naturalWidth: IMAGE.width,
51
+ naturalHeight: IMAGE.height,
52
+ },
53
+ ...overrides,
54
+ };
55
+ }
56
+
57
+ // (u, v) = (0, 0) is the stencil's top-left, (1, 1) is bottom-right.
58
+ function stencilScreenPoint(
59
+ state: CropperState,
60
+ container: Size,
61
+ imageSize: Size,
62
+ u: number,
63
+ v: number
64
+ ): { x: number; y: number } {
65
+ // The stencil lives in the snap-rotation visible bounds (that's
66
+ // how the render path positions it — see rectangle-stencil.tsx
67
+ // and the snap rotation logic in createCamera).
68
+ const snapRotation = Math.round( state.rotation / 90 ) * 90;
69
+ const baseCamera = createCamera(
70
+ {
71
+ ...state,
72
+ pan: { x: 0, y: 0 },
73
+ zoom: 1,
74
+ rotation: snapRotation,
75
+ },
76
+ container,
77
+ imageSize
78
+ );
79
+ const vb = getVisibleBounds( baseCamera );
80
+ const cr = state.cropRect;
81
+ return {
82
+ x: vb.left + ( cr.x + u * cr.width ) * vb.width,
83
+ y: vb.top + ( cr.y + v * cr.height ) * vb.height,
84
+ };
85
+ }
86
+
87
+ function previewSourcePixel(
88
+ state: CropperState,
89
+ container: Size,
90
+ imageSize: Size,
91
+ screen: { x: number; y: number }
92
+ ): { x: number; y: number } {
93
+ const camera = createCamera( state, container, imageSize );
94
+ const world = screenToWorld( camera, screen );
95
+ return {
96
+ x: world.x * imageSize.width,
97
+ y: world.y * imageSize.height,
98
+ };
99
+ }
100
+
101
+ function exportSourcePixel(
102
+ state: CropperState,
103
+ imageSize: Size,
104
+ outputSize: Size,
105
+ outputPoint: { x: number; y: number }
106
+ ): { x: number; y: number } {
107
+ const camera = createExportCamera( state, imageSize, outputSize );
108
+ const inv = mat2d.create();
109
+ mat2d.invert( inv, camera );
110
+ const out = vec2.create();
111
+ vec2.transformMat2d( out, [ outputPoint.x, outputPoint.y ], inv );
112
+ return { x: out[ 0 ], y: out[ 1 ] };
113
+ }
114
+
115
+ // Five probe points: 4 corners + center. Corners catch offset/scale
116
+ // drift, center catches uniform bugs, and the asymmetric "interior"
117
+ // cases below add two more via the test matrix.
118
+ const PROBES: { label: string; u: number; v: number }[] = [
119
+ { label: 'top-left', u: 0, v: 0 },
120
+ { label: 'top-right', u: 1, v: 0 },
121
+ { label: 'bottom-right', u: 1, v: 1 },
122
+ { label: 'bottom-left', u: 0, v: 1 },
123
+ { label: 'center', u: 0.5, v: 0.5 },
124
+ { label: 'interior-1', u: 0.3, v: 0.7 },
125
+ { label: 'interior-2', u: 0.8, v: 0.25 },
126
+ ];
127
+
128
+ // All (state × output-size × probe) combinations flattened into rows
129
+ // for `it.each`. Each row is self-describing; on failure, the label
130
+ // pinpoints the exact state and probe point where preview / export
131
+ // disagreed.
132
+ interface ParityRow {
133
+ label: string;
134
+ state: CropperState;
135
+ outputSize: Size;
136
+ probeLabel: string;
137
+ u: number;
138
+ v: number;
139
+ }
140
+
141
+ function buildRows(): ParityRow[] {
142
+ const rotations = [ 0, 15, 45, 60, 90, 135, 200, 270 ];
143
+ const zooms = [ 1, 1.5, 2.5 ];
144
+ const pans = [
145
+ { x: 0, y: 0 },
146
+ { x: 0.08, y: -0.05 },
147
+ { x: -0.1, y: 0.12 },
148
+ ];
149
+ const flips = [
150
+ { horizontal: false, vertical: false },
151
+ { horizontal: true, vertical: false },
152
+ { horizontal: false, vertical: true },
153
+ { horizontal: true, vertical: true },
154
+ ];
155
+ const cropRects = [
156
+ { label: 'full', rect: { x: 0, y: 0, width: 1, height: 1 } },
157
+ {
158
+ label: 'centered',
159
+ rect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
160
+ },
161
+ {
162
+ label: 'off-center',
163
+ rect: { x: 0.1, y: 0.2, width: 0.45, height: 0.55 },
164
+ },
165
+ ];
166
+ const outputSizes: Size[] = [
167
+ { width: 400, height: 300 },
168
+ { width: 1024, height: 768 },
169
+ ];
170
+
171
+ const rows: ParityRow[] = [];
172
+ for ( const rotation of rotations ) {
173
+ for ( const zoom of zooms ) {
174
+ for ( const pan of pans ) {
175
+ for ( const flip of flips ) {
176
+ for ( const { label: cropLabel, rect } of cropRects ) {
177
+ for ( const outputSize of outputSizes ) {
178
+ const flipLabel = `${
179
+ flip.horizontal ? 'H' : '-'
180
+ }${ flip.vertical ? 'V' : '-' }`;
181
+ const stateLabel = `rot=${ rotation } zoom=${ zoom } pan=(${ pan.x },${ pan.y }) flip=${ flipLabel } crop=${ cropLabel } out=${ outputSize.width }x${ outputSize.height }`;
182
+ const state = makeState( {
183
+ rotation,
184
+ zoom,
185
+ pan,
186
+ flip,
187
+ cropRect: rect,
188
+ } );
189
+ for ( const {
190
+ label: probeLabel,
191
+ u,
192
+ v,
193
+ } of PROBES ) {
194
+ rows.push( {
195
+ label: `${ stateLabel } probe=${ probeLabel }`,
196
+ state,
197
+ outputSize,
198
+ probeLabel,
199
+ u,
200
+ v,
201
+ } );
202
+ }
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
209
+ return rows;
210
+ }
211
+
212
+ describe( 'Preview ↔ Export parity', () => {
213
+ // Parametric sweep: same (u, v) inside the stencil → same source
214
+ // pixel, across rotations × zooms × pans × flips × crop rects ×
215
+ // output sizes. 5 + 2 probes per state (corners, center, two
216
+ // asymmetric interior points) catch drift the center alone would
217
+ // miss.
218
+ const rows = buildRows();
219
+
220
+ it.each( rows )( '$label', ( { state, outputSize, u, v } ) => {
221
+ const screen = stencilScreenPoint( state, CONTAINER, IMAGE, u, v );
222
+ const outputPoint = {
223
+ x: u * outputSize.width,
224
+ y: v * outputSize.height,
225
+ };
226
+ const preview = previewSourcePixel( state, CONTAINER, IMAGE, screen );
227
+ const exported = exportSourcePixel(
228
+ state,
229
+ IMAGE,
230
+ outputSize,
231
+ outputPoint
232
+ );
233
+
234
+ // Tolerance of 1 source pixel. Intermediate scale factors differ
235
+ // between the preview (container pixels) and export (output
236
+ // pixels) paths, so sub-pixel agreement is unrealistic. A drift
237
+ // of ~1px is still visibly correct; anything larger is a real
238
+ // coordinate-system mismatch.
239
+ expect( preview.x ).toBeCloseTo( exported.x, 0 );
240
+ expect( preview.y ).toBeCloseTo( exported.y, 0 );
241
+ } );
242
+ } );
@@ -0,0 +1,348 @@
1
+ /**
2
+ * Render–Camera Bridge Tests
3
+ *
4
+ * The image cropper has two parallel code paths that must agree:
5
+ *
6
+ * 1. **Render path** — manual cos/sin math in `computeTransformStyle`
7
+ * (CSS matrix) and stencil positioning (offset + cropRect × visualSize).
8
+ * This is what the user sees on screen.
9
+ *
10
+ * 2. **Camera path** — `createCamera` builds a `mat2d` matrix used by
11
+ * `restrictPanZoom` and `getSourceRegion` to enforce containment and
12
+ * compute export coordinates.
13
+ *
14
+ * If either path changes without updating the other, the image will
15
+ * appear to cover the crop area on screen but the restriction says it
16
+ * doesn't (or vice versa). These tests catch that drift by computing
17
+ * the same positions through both paths and asserting they agree.
18
+ *
19
+ * If you change:
20
+ * - `computeTransformStyle` (transform-style.ts)
21
+ * - `createCamera` (camera.ts)
22
+ * - Stencil positioning formula (rectangle-stencil.tsx)
23
+ * - `getImageFit` (camera.ts)
24
+ *
25
+ * ...run these tests to verify the two paths still agree.
26
+ */
27
+ import {
28
+ createCamera,
29
+ worldToScreen,
30
+ getImageFit,
31
+ getVisibleBounds,
32
+ } from '../camera';
33
+ import { computeTransformStyle } from '../transform-style';
34
+ import { DEFAULT_STATE } from '../constants';
35
+ import type { CropperState, Size } from '../types';
36
+
37
+ const CONTAINER: Size = { width: 800, height: 600 };
38
+ const IMAGE: Size = { width: 1600, height: 900 };
39
+
40
+ /**
41
+ * Create a test state with an image loaded.
42
+ *
43
+ * @param overrides Partial state overrides.
44
+ * @return A complete CropperState with image set.
45
+ */
46
+ function makeState( overrides: Partial< CropperState > = {} ): CropperState {
47
+ return {
48
+ ...DEFAULT_STATE,
49
+ image: {
50
+ src: 'test.jpg',
51
+ naturalWidth: IMAGE.width,
52
+ naturalHeight: IMAGE.height,
53
+ },
54
+ ...overrides,
55
+ };
56
+ }
57
+
58
+ /**
59
+ * Compute the stencil corner positions using the render path.
60
+ *
61
+ * This replicates the exact math from rectangle-stencil.tsx:
62
+ * offsetX = (containerW - visualW) / 2
63
+ * left = offsetX + cropRect.x * visualW
64
+ * top = offsetY + cropRect.y * visualH
65
+ *
66
+ * @param state The cropper state.
67
+ * @param containerSize The container dimensions.
68
+ * @param imageSize The natural image dimensions.
69
+ * @return The four stencil corners in screen pixels.
70
+ */
71
+ function stencilCornersViaCssPath(
72
+ state: CropperState,
73
+ containerSize: Size,
74
+ imageSize: Size
75
+ ): { x: number; y: number }[] {
76
+ const { visualSize } = getImageFit(
77
+ containerSize,
78
+ imageSize,
79
+ state.rotation
80
+ );
81
+ const offsetX = ( containerSize.width - visualSize.width ) / 2;
82
+ const offsetY = ( containerSize.height - visualSize.height ) / 2;
83
+ const cr = state.cropRect;
84
+
85
+ return [
86
+ {
87
+ x: offsetX + cr.x * visualSize.width,
88
+ y: offsetY + cr.y * visualSize.height,
89
+ },
90
+ {
91
+ x: offsetX + ( cr.x + cr.width ) * visualSize.width,
92
+ y: offsetY + cr.y * visualSize.height,
93
+ },
94
+ {
95
+ x: offsetX + ( cr.x + cr.width ) * visualSize.width,
96
+ y: offsetY + ( cr.y + cr.height ) * visualSize.height,
97
+ },
98
+ {
99
+ x: offsetX + cr.x * visualSize.width,
100
+ y: offsetY + ( cr.y + cr.height ) * visualSize.height,
101
+ },
102
+ ];
103
+ }
104
+
105
+ /**
106
+ * Compute the stencil corner positions using the camera path.
107
+ *
108
+ * This replicates the exact math from restrictPanZoom in containment.ts:
109
+ * baseCamera = createCamera(state with zoom=1, pan=0)
110
+ * visibleBounds = getVisibleBounds(baseCamera)
111
+ * stencilX = visibleBounds.left + cropRect.x * visibleBounds.width
112
+ *
113
+ * @param state The cropper state.
114
+ * @param containerSize The container dimensions.
115
+ * @param imageSize The natural image dimensions.
116
+ * @return The four stencil corners in screen pixels.
117
+ */
118
+ function stencilCornersViaCameraPath(
119
+ state: CropperState,
120
+ containerSize: Size,
121
+ imageSize: Size
122
+ ): { x: number; y: number }[] {
123
+ const baseCamera = createCamera(
124
+ { ...state, pan: { x: 0, y: 0 }, zoom: 1 },
125
+ containerSize,
126
+ imageSize
127
+ );
128
+ const vb = getVisibleBounds( baseCamera );
129
+ const cr = state.cropRect;
130
+
131
+ return [
132
+ {
133
+ x: vb.left + cr.x * vb.width,
134
+ y: vb.top + cr.y * vb.height,
135
+ },
136
+ {
137
+ x: vb.left + ( cr.x + cr.width ) * vb.width,
138
+ y: vb.top + cr.y * vb.height,
139
+ },
140
+ {
141
+ x: vb.left + ( cr.x + cr.width ) * vb.width,
142
+ y: vb.top + ( cr.y + cr.height ) * vb.height,
143
+ },
144
+ {
145
+ x: vb.left + cr.x * vb.width,
146
+ y: vb.top + ( cr.y + cr.height ) * vb.height,
147
+ },
148
+ ];
149
+ }
150
+
151
+ /**
152
+ * Compute where an image-center point ends up on screen using the
153
+ * render path (the CSS matrix from computeTransformStyle).
154
+ *
155
+ * The CSS matrix is applied with transform-origin: center center,
156
+ * relative to the element. The element is centered in the container.
157
+ * So screen position = containerCenter + matrix × (point - elementCenter).
158
+ *
159
+ * @param state The cropper state.
160
+ * @param containerSize The container dimensions.
161
+ * @param imageSize The natural image dimensions.
162
+ * @param normPoint The point in normalized [0,1] image space.
163
+ * @param normPoint.x Horizontal coordinate (0 = left, 1 = right).
164
+ * @param normPoint.y Vertical coordinate (0 = top, 1 = bottom).
165
+ * @return The screen pixel position.
166
+ */
167
+ function pointViaCssPath(
168
+ state: CropperState,
169
+ containerSize: Size,
170
+ imageSize: Size,
171
+ normPoint: { x: number; y: number }
172
+ ): { x: number; y: number } {
173
+ const { elementSize, visualSize } = getImageFit(
174
+ containerSize,
175
+ imageSize,
176
+ state.rotation
177
+ );
178
+
179
+ // computeTransformStyle receives visualSize (the rotated bounding box),
180
+ // matching what cropper.tsx passes in production.
181
+ const matrixStr = computeTransformStyle( state, visualSize );
182
+ const match = matrixStr.match(
183
+ /matrix\(([^,]+),\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^)]+)\)/
184
+ );
185
+ const [ , a, b, c, d, tx, ty ] = match!.map( Number );
186
+
187
+ // The point in rendered-pixel space (relative to element origin).
188
+ const px = normPoint.x * elementSize.width;
189
+ const py = normPoint.y * elementSize.height;
190
+
191
+ // CSS transform-origin is element center.
192
+ const cx = elementSize.width / 2;
193
+ const cy = elementSize.height / 2;
194
+
195
+ // Apply the matrix around the center origin, then translate
196
+ // to container center (where the element is positioned).
197
+ const dx = px - cx;
198
+ const dy = py - cy;
199
+ return {
200
+ x: containerSize.width / 2 + a * dx + c * dy + tx,
201
+ y: containerSize.height / 2 + b * dx + d * dy + ty,
202
+ };
203
+ }
204
+
205
+ /**
206
+ * Compute where an image point ends up on screen using the camera path.
207
+ *
208
+ * @param state The cropper state.
209
+ * @param containerSize The container dimensions.
210
+ * @param imageSize The natural image dimensions.
211
+ * @param normPoint The point in normalized [0,1] image space.
212
+ * @param normPoint.x Horizontal coordinate (0 = left, 1 = right).
213
+ * @param normPoint.y Vertical coordinate (0 = top, 1 = bottom).
214
+ * @return The screen pixel position.
215
+ */
216
+ function pointViaCameraPath(
217
+ state: CropperState,
218
+ containerSize: Size,
219
+ imageSize: Size,
220
+ normPoint: { x: number; y: number }
221
+ ): { x: number; y: number } {
222
+ const camera = createCamera( state, containerSize, imageSize );
223
+ return worldToScreen( camera, normPoint );
224
+ }
225
+
226
+ describe( 'Render–Camera bridge: stencil positioning', () => {
227
+ // Stencil positioning uses the snapped rotation for layout (see
228
+ // getImageFit), which matches the camera path at 90° multiples but
229
+ // diverges during fine rotation (±15°, ±30°, etc.). The CSS stencil
230
+ // stays put while the image visibly rotates behind it — this is the
231
+ // fixed-stencil layout. Only the snap-multiple angles are expected
232
+ // to have exact agreement between the two paths.
233
+ const SNAP_ROTATIONS = [ 0, 90, 180, 270 ];
234
+ const CROP_RECTS = [
235
+ { x: 0, y: 0, width: 1, height: 1 },
236
+ { x: 0.1, y: 0.1, width: 0.8, height: 0.8 },
237
+ { x: 0.25, y: 0.3, width: 0.5, height: 0.4 },
238
+ ];
239
+
240
+ for ( const rotation of SNAP_ROTATIONS ) {
241
+ for ( const cropRect of CROP_RECTS ) {
242
+ const label = `rotation=${ rotation }° crop=${ cropRect.width }×${ cropRect.height }`;
243
+ it( `stencil corners agree at ${ label }`, () => {
244
+ const state = makeState( { rotation, cropRect } );
245
+ const stencilCorners = stencilCornersViaCssPath(
246
+ state,
247
+ CONTAINER,
248
+ IMAGE
249
+ );
250
+ const cameraCorners = stencilCornersViaCameraPath(
251
+ state,
252
+ CONTAINER,
253
+ IMAGE
254
+ );
255
+
256
+ for ( let i = 0; i < 4; i++ ) {
257
+ expect( stencilCorners[ i ].x ).toBeCloseTo(
258
+ cameraCorners[ i ].x,
259
+ 1
260
+ );
261
+ expect( stencilCorners[ i ].y ).toBeCloseTo(
262
+ cameraCorners[ i ].y,
263
+ 1
264
+ );
265
+ }
266
+ } );
267
+ }
268
+ }
269
+ } );
270
+
271
+ describe( 'Render–Camera bridge: image point projection', () => {
272
+ const TEST_POINTS = [
273
+ { x: 0, y: 0 },
274
+ { x: 0.5, y: 0.5 },
275
+ { x: 1, y: 1 },
276
+ { x: 0.25, y: 0.75 },
277
+ ];
278
+ const TEST_STATES = [
279
+ { label: 'identity', overrides: {} },
280
+ { label: 'zoom=2', overrides: { zoom: 2 } },
281
+ {
282
+ label: 'rotation=30 zoom=1.5',
283
+ overrides: { rotation: 30, zoom: 1.5 },
284
+ },
285
+ {
286
+ label: 'flip-h rotation=45',
287
+ overrides: {
288
+ flip: { horizontal: true, vertical: false },
289
+ rotation: 45,
290
+ },
291
+ },
292
+ {
293
+ label: 'pan + zoom + rotation',
294
+ overrides: {
295
+ pan: { x: 0.05, y: -0.03 },
296
+ zoom: 2,
297
+ rotation: 15,
298
+ },
299
+ },
300
+ ];
301
+
302
+ for ( const { label, overrides } of TEST_STATES ) {
303
+ for ( const point of TEST_POINTS ) {
304
+ it( `(${ point.x },${ point.y }) agrees at ${ label }`, () => {
305
+ const state = makeState( overrides );
306
+ const cssPos = pointViaCssPath(
307
+ state,
308
+ CONTAINER,
309
+ IMAGE,
310
+ point
311
+ );
312
+ const cameraPos = pointViaCameraPath(
313
+ state,
314
+ CONTAINER,
315
+ IMAGE,
316
+ point
317
+ );
318
+
319
+ expect( cssPos.x ).toBeCloseTo( cameraPos.x, 0 );
320
+ expect( cssPos.y ).toBeCloseTo( cameraPos.y, 0 );
321
+ } );
322
+ }
323
+ }
324
+ } );
325
+
326
+ describe( 'Render–Camera bridge: visual size consistency', () => {
327
+ it( 'getImageFit visualSize matches getVisibleBounds dimensions at snap angles', () => {
328
+ // At fine rotation (15°, 30°, etc.) the image footprint AABB
329
+ // inflates beyond the snapped visual box, so visualSize (which
330
+ // snaps) and getVisibleBounds (which sees actual rotated corners)
331
+ // diverge by design. They must still agree at snap multiples.
332
+ const SNAP_ROTATIONS = [ 0, 90, 180, 270 ];
333
+
334
+ for ( const rotation of SNAP_ROTATIONS ) {
335
+ const state = makeState( { rotation } );
336
+ const { visualSize } = getImageFit( CONTAINER, IMAGE, rotation );
337
+ const baseCamera = createCamera(
338
+ { ...state, pan: { x: 0, y: 0 }, zoom: 1 },
339
+ CONTAINER,
340
+ IMAGE
341
+ );
342
+ const vb = getVisibleBounds( baseCamera );
343
+
344
+ expect( visualSize.width ).toBeCloseTo( vb.width, 0 );
345
+ expect( visualSize.height ).toBeCloseTo( vb.height, 0 );
346
+ }
347
+ } );
348
+ } );