@yft-design/engine 0.0.0-stage → 0.0.1

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 (250) hide show
  1. package/README.md +39 -2
  2. package/dist/EngineCanvas.d.ts +500 -0
  3. package/dist/EngineCanvas.js +927 -0
  4. package/dist/core/canvasPerf.d.ts +23 -0
  5. package/dist/core/canvasPerf.js +170 -0
  6. package/dist/core/color.d.ts +52 -0
  7. package/dist/core/color.js +107 -0
  8. package/dist/core/distanceField.d.ts +15 -0
  9. package/dist/core/distanceField.js +92 -0
  10. package/dist/core/events.d.ts +34 -0
  11. package/dist/core/events.js +52 -0
  12. package/dist/core/gradient.d.ts +97 -0
  13. package/dist/core/gradient.js +175 -0
  14. package/dist/core/imageFilters.d.ts +150 -0
  15. package/dist/core/imageFilters.js +330 -0
  16. package/dist/core/mat2d.d.ts +151 -0
  17. package/dist/core/mat2d.js +241 -0
  18. package/dist/core/pathData.d.ts +53 -0
  19. package/dist/core/pathData.js +926 -0
  20. package/dist/core/pattern.d.ts +38 -0
  21. package/dist/core/pattern.js +40 -0
  22. package/dist/core/point.d.ts +59 -0
  23. package/dist/core/point.js +119 -0
  24. package/dist/core/psdEffects.d.ts +126 -0
  25. package/dist/core/psdEffects.js +158 -0
  26. package/dist/core/roundedRect.d.ts +62 -0
  27. package/dist/core/roundedRect.js +163 -0
  28. package/dist/core/shadow.d.ts +51 -0
  29. package/dist/core/shadow.js +47 -0
  30. package/dist/core/snap.d.ts +232 -0
  31. package/dist/core/snap.js +793 -0
  32. package/dist/core/stroke.d.ts +41 -0
  33. package/dist/core/stroke.js +44 -0
  34. package/dist/core/triangulate.d.ts +24 -0
  35. package/dist/core/triangulate.js +118 -0
  36. package/dist/debug/canvasInspection.d.ts +92 -0
  37. package/dist/debug/canvasInspection.js +113 -0
  38. package/dist/debug/pixelDiff.d.ts +22 -0
  39. package/dist/debug/pixelDiff.js +64 -0
  40. package/dist/debug/seed.d.ts +473 -0
  41. package/dist/debug/seed.js +3803 -0
  42. package/dist/decoders/gif2sprite.d.ts +9 -0
  43. package/dist/decoders/gif2sprite.js +84 -0
  44. package/dist/decoders/webp2sprite.d.ts +22 -0
  45. package/dist/decoders/webp2sprite.js +133 -0
  46. package/dist/engineFlag.d.ts +72 -0
  47. package/dist/engineFlag.js +86 -0
  48. package/dist/export/SceneExporter.d.ts +92 -0
  49. package/dist/export/SceneExporter.js +280 -0
  50. package/dist/export/glExport.d.ts +66 -0
  51. package/dist/export/glExport.js +79 -0
  52. package/dist/export/svgExport.d.ts +277 -0
  53. package/dist/export/svgExport.js +1790 -0
  54. package/dist/index.d.ts +52 -0
  55. package/dist/index.js +48 -0
  56. package/dist/interact/animationPlayback.d.ts +125 -0
  57. package/dist/interact/animationPlayback.js +344 -0
  58. package/dist/interact/canvasInteractions.d.ts +50 -0
  59. package/dist/interact/canvasInteractions.js +150 -0
  60. package/dist/interact/cropSession.d.ts +123 -0
  61. package/dist/interact/cropSession.js +526 -0
  62. package/dist/interact/dblClick.d.ts +51 -0
  63. package/dist/interact/dblClick.js +106 -0
  64. package/dist/interact/expandSession.d.ts +107 -0
  65. package/dist/interact/expandSession.js +281 -0
  66. package/dist/interact/hotkeyActions.d.ts +16 -0
  67. package/dist/interact/hotkeyActions.js +41 -0
  68. package/dist/interact/pathSession.d.ts +141 -0
  69. package/dist/interact/pathSession.js +480 -0
  70. package/dist/interact/pointer.d.ts +106 -0
  71. package/dist/interact/pointer.js +1594 -0
  72. package/dist/interact/pointerTablePuzzle.d.ts +100 -0
  73. package/dist/interact/pointerTablePuzzle.js +358 -0
  74. package/dist/interact/pointerTransform.d.ts +86 -0
  75. package/dist/interact/pointerTransform.js +440 -0
  76. package/dist/interact/pointerTypes.d.ts +577 -0
  77. package/dist/interact/pointerTypes.js +96 -0
  78. package/dist/interact/snapGuide.d.ts +127 -0
  79. package/dist/interact/snapGuide.js +376 -0
  80. package/dist/interact/tableCellEditing.d.ts +61 -0
  81. package/dist/interact/tableCellEditing.js +198 -0
  82. package/dist/interact/tableInteraction.d.ts +55 -0
  83. package/dist/interact/tableInteraction.js +212 -0
  84. package/dist/interact/textEditing.d.ts +77 -0
  85. package/dist/interact/textEditing.js +410 -0
  86. package/dist/interact/warpFacade.d.ts +36 -0
  87. package/dist/interact/warpFacade.js +51 -0
  88. package/dist/interact/warpSession.d.ts +191 -0
  89. package/dist/interact/warpSession.js +582 -0
  90. package/dist/interact/wheelScroll.d.ts +62 -0
  91. package/dist/interact/wheelScroll.js +72 -0
  92. package/dist/model/alphaHit.d.ts +59 -0
  93. package/dist/model/alphaHit.js +262 -0
  94. package/dist/model/animation.d.ts +228 -0
  95. package/dist/model/animation.js +510 -0
  96. package/dist/model/arcLayout.d.ts +54 -0
  97. package/dist/model/arcLayout.js +307 -0
  98. package/dist/model/arcTextTransform.d.ts +7 -0
  99. package/dist/model/arcTextTransform.js +87 -0
  100. package/dist/model/compatEvents.d.ts +135 -0
  101. package/dist/model/compatEvents.js +73 -0
  102. package/dist/model/compatObject.d.ts +716 -0
  103. package/dist/model/compatObject.js +1579 -0
  104. package/dist/model/cropEdit.d.ts +118 -0
  105. package/dist/model/cropEdit.js +497 -0
  106. package/dist/model/document.d.ts +294 -0
  107. package/dist/model/document.js +572 -0
  108. package/dist/model/expandEdit.d.ts +93 -0
  109. package/dist/model/expandEdit.js +161 -0
  110. package/dist/model/fabricPrimitives.d.ts +156 -0
  111. package/dist/model/fabricPrimitives.js +9 -0
  112. package/dist/model/filler.d.ts +53 -0
  113. package/dist/model/filler.js +1 -0
  114. package/dist/model/groupAssemble.d.ts +7 -0
  115. package/dist/model/groupAssemble.js +98 -0
  116. package/dist/model/groupChildMove.d.ts +46 -0
  117. package/dist/model/groupChildMove.js +145 -0
  118. package/dist/model/groupResize.d.ts +78 -0
  119. package/dist/model/groupResize.js +108 -0
  120. package/dist/model/handles.d.ts +40 -0
  121. package/dist/model/handles.js +29 -0
  122. package/dist/model/imageMaskBake.d.ts +75 -0
  123. package/dist/model/imageMaskBake.js +136 -0
  124. package/dist/model/lineConstruct.d.ts +13 -0
  125. package/dist/model/lineConstruct.js +79 -0
  126. package/dist/model/maskCropEdit.d.ts +64 -0
  127. package/dist/model/maskCropEdit.js +90 -0
  128. package/dist/model/pathBackground.d.ts +51 -0
  129. package/dist/model/pathBackground.js +196 -0
  130. package/dist/model/pathConstruct.d.ts +27 -0
  131. package/dist/model/pathConstruct.js +55 -0
  132. package/dist/model/pathEdit.d.ts +114 -0
  133. package/dist/model/pathEdit.js +378 -0
  134. package/dist/model/puzzleEdit.d.ts +136 -0
  135. package/dist/model/puzzleEdit.js +742 -0
  136. package/dist/model/puzzleLayout.d.ts +91 -0
  137. package/dist/model/puzzleLayout.js +143 -0
  138. package/dist/model/scene.d.ts +1004 -0
  139. package/dist/model/scene.js +2841 -0
  140. package/dist/model/schema.d.ts +290 -0
  141. package/dist/model/schema.js +13 -0
  142. package/dist/model/shapePath.d.ts +14 -0
  143. package/dist/model/shapePath.js +136 -0
  144. package/dist/model/tableEdit.d.ts +196 -0
  145. package/dist/model/tableEdit.js +751 -0
  146. package/dist/model/tableFacade.d.ts +6 -0
  147. package/dist/model/tableFacade.js +808 -0
  148. package/dist/model/tableLayout.d.ts +85 -0
  149. package/dist/model/tableLayout.js +193 -0
  150. package/dist/model/textLayout.d.ts +357 -0
  151. package/dist/model/textLayout.js +822 -0
  152. package/dist/render/GLBlend.d.ts +40 -0
  153. package/dist/render/GLBlend.js +270 -0
  154. package/dist/render/GLContext.d.ts +25 -0
  155. package/dist/render/GLContext.js +48 -0
  156. package/dist/render/GLIsolate.d.ts +46 -0
  157. package/dist/render/GLIsolate.js +188 -0
  158. package/dist/render/GLTextureCache.d.ts +86 -0
  159. package/dist/render/GLTextureCache.js +187 -0
  160. package/dist/render/GlyphAtlas.d.ts +151 -0
  161. package/dist/render/GlyphAtlas.js +404 -0
  162. package/dist/render/MeshBatch.d.ts +123 -0
  163. package/dist/render/MeshBatch.js +416 -0
  164. package/dist/render/QuadBatch.d.ts +138 -0
  165. package/dist/render/QuadBatch.js +490 -0
  166. package/dist/render/SceneRenderer.d.ts +186 -0
  167. package/dist/render/SceneRenderer.js +700 -0
  168. package/dist/render/SourceManager.d.ts +121 -0
  169. package/dist/render/SourceManager.js +505 -0
  170. package/dist/render/TextRenderer.d.ts +140 -0
  171. package/dist/render/TextRenderer.js +795 -0
  172. package/dist/render/arcText.d.ts +40 -0
  173. package/dist/render/arcText.js +187 -0
  174. package/dist/render/batchOrder.d.ts +105 -0
  175. package/dist/render/batchOrder.js +168 -0
  176. package/dist/render/blendMode.d.ts +35 -0
  177. package/dist/render/blendMode.js +142 -0
  178. package/dist/render/clipMaskPainter.d.ts +17 -0
  179. package/dist/render/clipMaskPainter.js +121 -0
  180. package/dist/render/fontInvalidation.d.ts +25 -0
  181. package/dist/render/fontInvalidation.js +77 -0
  182. package/dist/render/fontInvalidationController.d.ts +76 -0
  183. package/dist/render/fontInvalidationController.js +114 -0
  184. package/dist/render/frameOverlay.d.ts +62 -0
  185. package/dist/render/frameOverlay.js +124 -0
  186. package/dist/render/framePipeline.d.ts +99 -0
  187. package/dist/render/framePipeline.js +95 -0
  188. package/dist/render/gpuFilterPasses.d.ts +25 -0
  189. package/dist/render/gpuFilterPasses.js +883 -0
  190. package/dist/render/gpuFx.d.ts +136 -0
  191. package/dist/render/gpuFx.js +614 -0
  192. package/dist/render/gpuImageFilters.d.ts +59 -0
  193. package/dist/render/gpuImageFilters.js +289 -0
  194. package/dist/render/gpuShadow.d.ts +52 -0
  195. package/dist/render/gpuShadow.js +289 -0
  196. package/dist/render/gradientRamp.d.ts +45 -0
  197. package/dist/render/gradientRamp.js +74 -0
  198. package/dist/render/imageEffects.d.ts +123 -0
  199. package/dist/render/imageEffects.js +390 -0
  200. package/dist/render/maskRender.d.ts +92 -0
  201. package/dist/render/maskRender.js +224 -0
  202. package/dist/render/progressiveLoad.d.ts +45 -0
  203. package/dist/render/progressiveLoad.js +86 -0
  204. package/dist/render/psdEffectPainter.d.ts +34 -0
  205. package/dist/render/psdEffectPainter.js +309 -0
  206. package/dist/render/puzzleClamp.d.ts +28 -0
  207. package/dist/render/puzzleClamp.js +30 -0
  208. package/dist/render/retainedFrame.d.ts +85 -0
  209. package/dist/render/retainedFrame.js +193 -0
  210. package/dist/render/ruler.d.ts +90 -0
  211. package/dist/render/ruler.js +287 -0
  212. package/dist/render/scrollText.d.ts +65 -0
  213. package/dist/render/scrollText.js +261 -0
  214. package/dist/render/selectionOverlay.d.ts +318 -0
  215. package/dist/render/selectionOverlay.js +921 -0
  216. package/dist/render/shaders.d.ts +38 -0
  217. package/dist/render/shaders.js +314 -0
  218. package/dist/render/shadowBake.d.ts +43 -0
  219. package/dist/render/shadowBake.js +202 -0
  220. package/dist/render/shadowPainter.d.ts +13 -0
  221. package/dist/render/shadowPainter.js +80 -0
  222. package/dist/render/sharedGpuPainters.d.ts +25 -0
  223. package/dist/render/sharedGpuPainters.js +40 -0
  224. package/dist/render/sourceHydration.d.ts +12 -0
  225. package/dist/render/sourceHydration.js +48 -0
  226. package/dist/render/strokeBake.d.ts +85 -0
  227. package/dist/render/strokeBake.js +515 -0
  228. package/dist/render/strokePainter.d.ts +28 -0
  229. package/dist/render/strokePainter.js +119 -0
  230. package/dist/render/strokeSdf.d.ts +35 -0
  231. package/dist/render/strokeSdf.js +74 -0
  232. package/dist/render/svgRaster.d.ts +46 -0
  233. package/dist/render/svgRaster.js +71 -0
  234. package/dist/render/tableOverlay.d.ts +38 -0
  235. package/dist/render/tableOverlay.js +233 -0
  236. package/dist/render/textHighlight.d.ts +117 -0
  237. package/dist/render/textHighlight.js +380 -0
  238. package/dist/render/textureUpload.d.ts +18 -0
  239. package/dist/render/textureUpload.js +26 -0
  240. package/dist/render/verticalText.d.ts +38 -0
  241. package/dist/render/verticalText.js +63 -0
  242. package/dist/render/warpText.d.ts +58 -0
  243. package/dist/render/warpText.js +380 -0
  244. package/dist/render/workspaceBackdrop.d.ts +35 -0
  245. package/dist/render/workspaceBackdrop.js +71 -0
  246. package/dist/render/workspaceChecker.d.ts +54 -0
  247. package/dist/render/workspaceChecker.js +170 -0
  248. package/dist/render/workspaceGlow.d.ts +51 -0
  249. package/dist/render/workspaceGlow.js +171 -0
  250. package/package.json +39 -3
@@ -0,0 +1,118 @@
1
+ /**
2
+ * 图片裁剪模式纯函数层(B2):应用 cropping.controls.handlers / cropping.mixin /
3
+ * ImageClip 预设的逐式移植。只算不写——入参出参都是普通对象,节点落盘由
4
+ * 调用方(interact/cropSession)执行。
5
+ *
6
+ * 数据模型(与应用 live 对象同构,区别于盘上序列化形态):
7
+ * - `width/height` = 裁剪窗尺寸(源像素);`scaleX/scaleY` 把窗映射到画布
8
+ * - `cropX/cropY` = 窗左上角在源图内的偏移(源像素);整图 = naturalWidth/Height
9
+ * - 盘上 legacy 口径 raw.width = width + cropX(右缘坐标语义)、visible 口径
10
+ * raw.width = width——`liveStateFromRaw/applyLiveState` 做双向映射(B1 契约:
11
+ * raw 不改写语义、toObject 原样回写)
12
+ *
13
+ * 几何原语与 fabric 6.9 逐位同源(口径实测锚定):
14
+ * - 数值型 origin 分数 resolve(v) = v − 0.5(字符串 left/top/center/right/bottom
15
+ * = −0.5/−0.5/0/0.5/0.5——应用 utils/resolveOrigin 与 fabric 同式)
16
+ * - `translateToGivenOrigin` 底层是**轴对齐**偏移(offset = fracDelta·缩放后尺寸,
17
+ * 不含旋转);旋转在包装层:translateToOriginPoint 绕 center 正转、
18
+ * translateToCenterPoint 绕 pos 正转
19
+ * - normalizePoint = 指针绕 center 反转(去旋转、不去缩放)− origin 偏移
20
+ * (origin 偏移同样轴对齐)——边手柄公式里的 `/scaleX` 即由此而来
21
+ */
22
+ import type { CornerHandle } from './handles';
23
+ import type { SerializedNode } from './scene';
24
+ /** 裁剪角手柄(词汇表同源:model/handles CornerHandle) */
25
+ export type CropCorner = CornerHandle;
26
+ export type CropEdge = 'left' | 'right' | 'top' | 'bottom';
27
+ /** 裁剪会话的 live 状态(应用 FabricImage 的裁剪相关字段子集) */
28
+ export interface CropLiveState {
29
+ left: number;
30
+ top: number;
31
+ angle: number;
32
+ originX: string;
33
+ originY: string;
34
+ flipX: boolean;
35
+ flipY: boolean;
36
+ /** 裁剪窗尺寸(源像素) */
37
+ width: number;
38
+ height: number;
39
+ cropX: number;
40
+ cropY: number;
41
+ scaleX: number;
42
+ scaleY: number;
43
+ naturalWidth: number;
44
+ naturalHeight: number;
45
+ /** 盘上口径('visible' | 'legacy' 缺省)——raw.width 写回映射用 */
46
+ cropSizeMode: string;
47
+ }
48
+ /** fabric resolveOrigin 同式:字符串查表、数值 v → v − 0.5 */
49
+ export declare const resolveCropOrigin: (v: string | number) => number;
50
+ type Pt = {
51
+ x: number;
52
+ y: number;
53
+ };
54
+ /** 缩放后(未旋转)尺寸 = width·scaleX——fabric _getTransformedDimensions(stroke=0 图片族) */
55
+ export declare const scaledDimsOf: (s: Pick<CropLiveState, "width" | "height" | "scaleX" | "scaleY">) => Pt;
56
+ /** 源空间局部点 (fx·W, fy·H) 的场景位(含旋转)。flip 参数=锚定几何的翻转
57
+ * 感知(scene = center + R·S·F·local,有意偏差侧——见 cropLocalPoint 注;
58
+ * 无 flip 时与 fabric translateToOriginPoint 逐位同源) */
59
+ export declare const originFromCenter: (center: Pt, fx: number, fy: number, dims: Pt, angleDeg: number, flipX?: boolean, flipY?: boolean) => Pt;
60
+ /** originFromCenter 的逆(锚定回钉;flip 感知同上) */
61
+ export declare const centerFromOrigin: (pos: Pt, fx: number, fy: number, dims: Pt, angleDeg: number, flipX?: boolean, flipY?: boolean) => Pt;
62
+ /** 自身 origin 位置(left/top 写入口径) */
63
+ export declare const positionFromCenter: (center: Pt, ownOX: string, ownOY: string, dims: Pt, angleDeg: number) => Pt;
64
+ /** 自身 origin 位置 → center(fabric getRelativeCenterPoint) */
65
+ export declare const centerFromPosition: (left: number, top: number, ownOX: string, ownOY: string, dims: Pt, angleDeg: number) => Pt;
66
+ /** fabric normalizePoint:指针去旋转(不去缩放)再减 origin 偏移(轴对齐)。
67
+ * **有意偏差(应用 bug 修正,2026-09-20 用户反馈「对角点一拖就合到一起」)**:
68
+ * fabric 的 local 坐标不去翻转(实测实证:flipX 图边/角手柄外拖 → 宽 0 塌缩),
69
+ * 引擎补上翻转逆变换(local = S⁻¹·F⁻¹·R⁻¹·δ)后恒走非 Flig 手柄——翻转镜像
70
+ * 下手柄语义自然正确;应用的 *Flig 变体是错位补偿(保留导出仅作语义参考,不再被分发),
71
+ * 应用侧修复后同步回归(竖排 right/center 先例) */
72
+ export declare const cropLocalPoint: (s: CropLiveState, pointer: Pt, fx?: number, fy?: number) => Pt;
73
+ export declare const cropFromLeftFlig: (s: CropLiveState, pointer: Pt) => CropLiveState;
74
+ export declare const cropFromRightFlig: (s: CropLiveState, pointer: Pt) => CropLiveState;
75
+ export declare const cropFromTopFlig: (s: CropLiveState, pointer: Pt) => CropLiveState;
76
+ export declare const cropFromBottomFlig: (s: CropLiveState, pointer: Pt) => CropLiveState;
77
+ /** 边手柄分发(有意偏差:恒走非 Flig——见 cropLocalPoint 的翻转修正注;
78
+ * 手柄 id = 源空间侧(offset 经翻转矩阵渲染到镜像侧),un-flip 后语义自然对齐) */
79
+ export declare const applyCropEdge: (s: CropLiveState, edge: CropEdge, pointer: Pt) => CropLiveState;
80
+ /**
81
+ * 角括号手柄 = X/Y 两轴独立裁剪(应用 cropFromTopLeft 组合:left 先 top 后,
82
+ * 后者读前者已改的 width/height——Y 轴锚定用更新后的 dims,逐步组合同源)。
83
+ */
84
+ export declare const applyCropCorner: (s: CropLiveState, corner: CropCorner, pointer: Pt) => CropLiveState;
85
+ export declare const constrainToRatio: (rawWidth: number, rawHeight: number, ratio: number) => {
86
+ width: number;
87
+ height: number;
88
+ };
89
+ export declare const applyCropRatioCorner: (s: CropLiveState, corner: CropCorner, pointer: Pt, ratio: number) => CropLiveState | null;
90
+ export declare const applyScaleEquallyCrop: (s: CropLiveState, corner: CropCorner, pointer: Pt) => CropLiveState | null;
91
+ /** 拖图增量(画布系)→ 新 cropX/cropY;增量先过「去平移逆变换」(fabric
92
+ * moveTransformationMatrix = invert(calcTransformMatrix) 去平移——旋转/缩放/
93
+ * 翻转全随图),再取负(拖图视觉方向 = 内容滚动反向)。
94
+ * 旋转方向修正(2026-09-22 用户反馈「有旋转角时拖底图发飘」):去旋转应乘
95
+ * R(−θ)——rx/ry 两式即「按 −φ 展开」,φ 必须传 +θ;首版传了 −θ 双重取负,
96
+ * 等效乘 R(+θ),指针与内容运动方向差 2θ(45° 垂直、90° 反向) */
97
+ export declare const moveCropImage: (s: CropLiveState, deltaScene: Pt) => {
98
+ cropX: number;
99
+ cropY: number;
100
+ };
101
+ export declare const applyCropRatioPreset: (s: CropLiveState, ratio: number) => CropLiveState;
102
+ /** raw(盘上形态)→ live 状态。naturalW/H 由调用方给(imageEl 已加载)。
103
+ * 越界防御(批次 319):换源后未重排几何的病态存盘(旧窗/旧偏移溢出新自然
104
+ * 尺寸)会让会话几何发散——整图框/等比手柄锚定分数进负数域、moveCropImage
105
+ * 的右缘钳制产出负 cropX。窗优先保(窗是可视内容盒)钳进 [0, natural],再把
106
+ * 偏移收进余量;sane 数据(cropX+width ≤ natural 恒成立)零改写,natural
107
+ * 未知(0,无内禀尺寸 SVG 等)不钳保持原语义 */
108
+ export declare const liveStateFromRaw: (raw: SerializedNode, naturalWidth: number, naturalHeight: number) => CropLiveState;
109
+ /** live 状态 → raw 写回字段(left/top/width/height/cropX/cropY/scale;cropSizeMode 保持盘上原值) */
110
+ export declare const liveStateToRawProps: (s: CropLiveState) => Partial<SerializedNode>;
111
+ /** 手柄在「窗本地系」的归一化位置(渲染/命中共用) */
112
+ export declare const CROP_HANDLE_OFFSETS: Record<string, {
113
+ x: number;
114
+ y: number;
115
+ }>;
116
+ /** 整图四角(tlS 等)在窗本地系的坐标(应用 imageCorner* 同式) */
117
+ export declare const fullImageCornerLocal: (s: CropLiveState, corner: CropCorner) => Pt;
118
+ export {};
@@ -0,0 +1,497 @@
1
+ /**
2
+ * 图片裁剪模式纯函数层(B2):应用 cropping.controls.handlers / cropping.mixin /
3
+ * ImageClip 预设的逐式移植。只算不写——入参出参都是普通对象,节点落盘由
4
+ * 调用方(interact/cropSession)执行。
5
+ *
6
+ * 数据模型(与应用 live 对象同构,区别于盘上序列化形态):
7
+ * - `width/height` = 裁剪窗尺寸(源像素);`scaleX/scaleY` 把窗映射到画布
8
+ * - `cropX/cropY` = 窗左上角在源图内的偏移(源像素);整图 = naturalWidth/Height
9
+ * - 盘上 legacy 口径 raw.width = width + cropX(右缘坐标语义)、visible 口径
10
+ * raw.width = width——`liveStateFromRaw/applyLiveState` 做双向映射(B1 契约:
11
+ * raw 不改写语义、toObject 原样回写)
12
+ *
13
+ * 几何原语与 fabric 6.9 逐位同源(口径实测锚定):
14
+ * - 数值型 origin 分数 resolve(v) = v − 0.5(字符串 left/top/center/right/bottom
15
+ * = −0.5/−0.5/0/0.5/0.5——应用 utils/resolveOrigin 与 fabric 同式)
16
+ * - `translateToGivenOrigin` 底层是**轴对齐**偏移(offset = fracDelta·缩放后尺寸,
17
+ * 不含旋转);旋转在包装层:translateToOriginPoint 绕 center 正转、
18
+ * translateToCenterPoint 绕 pos 正转
19
+ * - normalizePoint = 指针绕 center 反转(去旋转、不去缩放)− origin 偏移
20
+ * (origin 偏移同样轴对齐)——边手柄公式里的 `/scaleX` 即由此而来
21
+ */
22
+ const ORIGIN_OFFSET = { left: -0.5, top: -0.5, center: 0, bottom: 0.5, right: 0.5 };
23
+ /** fabric resolveOrigin 同式:字符串查表、数值 v → v − 0.5 */
24
+ export const resolveCropOrigin = (v) => (typeof v === 'string' ? ORIGIN_OFFSET[v] ?? 0 : v - 0.5);
25
+ const rotateAbout = (p, byRad, origin) => {
26
+ const c = Math.cos(byRad);
27
+ const s = Math.sin(byRad);
28
+ const dx = p.x - origin.x;
29
+ const dy = p.y - origin.y;
30
+ return { x: origin.x + c * dx - s * dy, y: origin.y + s * dx + c * dy };
31
+ };
32
+ /** 缩放后(未旋转)尺寸 = width·scaleX——fabric _getTransformedDimensions(stroke=0 图片族) */
33
+ export const scaledDimsOf = (s) => ({ x: s.width * s.scaleX, y: s.height * s.scaleY });
34
+ const scaledDims = scaledDimsOf;
35
+ const rad = (d) => (d * Math.PI) / 180;
36
+ /** 源空间局部点 (fx·W, fy·H) 的场景位(含旋转)。flip 参数=锚定几何的翻转
37
+ * 感知(scene = center + R·S·F·local,有意偏差侧——见 cropLocalPoint 注;
38
+ * 无 flip 时与 fabric translateToOriginPoint 逐位同源) */
39
+ export const originFromCenter = (center, fx, fy, dims, angleDeg, flipX = false, flipY = false) => {
40
+ const ux = flipX ? -1 : 1;
41
+ const uy = flipY ? -1 : 1;
42
+ let p = { x: center.x + fx * dims.x * ux, y: center.y + fy * dims.y * uy };
43
+ if (angleDeg)
44
+ p = rotateAbout(p, rad(angleDeg), center);
45
+ return p;
46
+ };
47
+ /** originFromCenter 的逆(锚定回钉;flip 感知同上) */
48
+ export const centerFromOrigin = (pos, fx, fy, dims, angleDeg, flipX = false, flipY = false) => {
49
+ const ux = flipX ? -1 : 1;
50
+ const uy = flipY ? -1 : 1;
51
+ let p = { x: pos.x - fx * dims.x * ux, y: pos.y - fy * dims.y * uy };
52
+ if (angleDeg)
53
+ p = rotateAbout(p, rad(angleDeg), pos);
54
+ return p;
55
+ };
56
+ /** 自身 origin 位置(left/top 写入口径) */
57
+ export const positionFromCenter = (center, ownOX, ownOY, dims, angleDeg) => originFromCenter(center, resolveCropOrigin(ownOX), resolveCropOrigin(ownOY), dims, angleDeg);
58
+ /** 自身 origin 位置 → center(fabric getRelativeCenterPoint) */
59
+ export const centerFromPosition = (left, top, ownOX, ownOY, dims, angleDeg) => centerFromOrigin({ x: left, y: top }, resolveCropOrigin(ownOX), resolveCropOrigin(ownOY), dims, angleDeg);
60
+ /** fabric normalizePoint:指针去旋转(不去缩放)再减 origin 偏移(轴对齐)。
61
+ * **有意偏差(应用 bug 修正,2026-09-20 用户反馈「对角点一拖就合到一起」)**:
62
+ * fabric 的 local 坐标不去翻转(实测实证:flipX 图边/角手柄外拖 → 宽 0 塌缩),
63
+ * 引擎补上翻转逆变换(local = S⁻¹·F⁻¹·R⁻¹·δ)后恒走非 Flig 手柄——翻转镜像
64
+ * 下手柄语义自然正确;应用的 *Flig 变体是错位补偿(保留导出仅作语义参考,不再被分发),
65
+ * 应用侧修复后同步回归(竖排 right/center 先例) */
66
+ export const cropLocalPoint = (s, pointer, fx = 0, fy = 0) => {
67
+ const center = centerFromPosition(s.left, s.top, s.originX, s.originY, scaledDims(s), s.angle);
68
+ let p = pointer;
69
+ if (s.angle)
70
+ p = rotateAbout(p, -rad(s.angle), center);
71
+ const dims = scaledDims(s);
72
+ const unflipX = s.flipX ? -1 : 1;
73
+ const unflipY = s.flipY ? -1 : 1;
74
+ return { x: (p.x - center.x) * unflipX - fx * dims.x, y: (p.y - center.y) * unflipY - fy * dims.y };
75
+ };
76
+ /** 各 handler 共用的「锚定→改窗→回钉」收口:返回新 left/top */
77
+ const setByAnchor = (s, anchorFx, anchorFy, next) => {
78
+ const oldDims = scaledDims(s);
79
+ const center = centerFromPosition(s.left, s.top, s.originX, s.originY, oldDims, s.angle);
80
+ const constraint = originFromCenter(center, anchorFx, anchorFy, oldDims, s.angle, s.flipX, s.flipY);
81
+ const scaleX = next.scaleX ?? s.scaleX;
82
+ const scaleY = next.scaleY ?? s.scaleY;
83
+ const newDims = { x: next.width * scaleX, y: next.height * scaleY };
84
+ const newCenter = centerFromOrigin(constraint, anchorFx, anchorFy, newDims, s.angle, s.flipX, s.flipY);
85
+ const pos = positionFromCenter(newCenter, s.originX, s.originY, newDims, s.angle);
86
+ return { ...s, ...next, scaleX, scaleY, left: pos.x, top: pos.y };
87
+ };
88
+ // ── 边手柄(窗随指针走、对边锚定;cropX/cropY 吸收位移)──────────────────────
89
+ // anchor 分数 = fabric transform origin(cropLeft 控件 x:-0.5,y:0 → originX 'left'),
90
+ // 窗口「左/右/上/下」边移动时锚定对边('right'/'left'/'bottom'/'top'),Y 锚 originY。
91
+ const cropFromLeft = (s, pointer) => {
92
+ const { width, cropX } = s;
93
+ const local = cropLocalPoint(s, pointer);
94
+ let changeX = local.x / s.scaleX - -(width / 2);
95
+ let newWidth = width - changeX;
96
+ if (cropX + changeX < 0) {
97
+ changeX = -cropX;
98
+ newWidth = width + cropX;
99
+ }
100
+ if (newWidth <= 0) {
101
+ changeX += newWidth;
102
+ newWidth = 0;
103
+ }
104
+ return setByAnchor(s, 0.5, 0, { width: newWidth, height: s.height, cropX: cropX + changeX, cropY: s.cropY });
105
+ };
106
+ export const cropFromLeftFlig = (s, pointer) => {
107
+ const { width, cropX } = s;
108
+ const local = cropLocalPoint(s, pointer);
109
+ let newWidth = width - (local.x / s.scaleX - -(width / 2));
110
+ if (newWidth + cropX > s.naturalWidth)
111
+ newWidth = s.naturalWidth - cropX;
112
+ if (newWidth < 0)
113
+ newWidth = 0;
114
+ return setByAnchor(s, 0.5, 0, { width: newWidth, height: s.height, cropX, cropY: s.cropY });
115
+ };
116
+ const cropFromRight = (s, pointer) => {
117
+ const { width, cropX } = s;
118
+ const local = cropLocalPoint(s, pointer);
119
+ let newWidth = width - (width / 2 - local.x / s.scaleX);
120
+ if (newWidth + cropX > s.naturalWidth)
121
+ newWidth = s.naturalWidth - cropX;
122
+ if (newWidth < 0)
123
+ newWidth = 0;
124
+ return setByAnchor(s, -0.5, 0, { width: newWidth, height: s.height, cropX, cropY: s.cropY });
125
+ };
126
+ export const cropFromRightFlig = (s, pointer) => {
127
+ const { width, cropX } = s;
128
+ const local = cropLocalPoint(s, pointer);
129
+ let changeX = width / 2 - local.x / s.scaleX;
130
+ let newWidth = width - changeX;
131
+ if (cropX + changeX < 0) {
132
+ changeX = -cropX;
133
+ newWidth = width + cropX;
134
+ }
135
+ if (newWidth <= 0) {
136
+ changeX += newWidth;
137
+ newWidth = 0;
138
+ }
139
+ return setByAnchor(s, -0.5, 0, { width: newWidth, height: s.height, cropX: cropX + changeX, cropY: s.cropY });
140
+ };
141
+ const cropFromTop = (s, pointer) => {
142
+ const { height, cropY } = s;
143
+ const local = cropLocalPoint(s, pointer);
144
+ let changeY = local.y / s.scaleY - -(height / 2);
145
+ let newHeight = height - changeY;
146
+ if (cropY + changeY < 0) {
147
+ changeY = -cropY;
148
+ newHeight = height + cropY;
149
+ }
150
+ if (newHeight <= 0) {
151
+ changeY += newHeight;
152
+ newHeight = 0;
153
+ }
154
+ return setByAnchor(s, 0, 0.5, { width: s.width, height: newHeight, cropX: s.cropX, cropY: cropY + changeY });
155
+ };
156
+ export const cropFromTopFlig = (s, pointer) => {
157
+ const { height, cropY } = s;
158
+ const local = cropLocalPoint(s, pointer);
159
+ let newHeight = height - (local.y / s.scaleY - -(height / 2));
160
+ if (newHeight + cropY > s.naturalHeight)
161
+ newHeight = s.naturalHeight - cropY;
162
+ if (newHeight < 0)
163
+ newHeight = 0;
164
+ return setByAnchor(s, 0, 0.5, { width: s.width, height: newHeight, cropX: s.cropX, cropY });
165
+ };
166
+ const cropFromBottom = (s, pointer) => {
167
+ const { height, cropY } = s;
168
+ const local = cropLocalPoint(s, pointer);
169
+ let newHeight = height - (height / 2 - local.y / s.scaleY);
170
+ if (newHeight + cropY > s.naturalHeight)
171
+ newHeight = s.naturalHeight - cropY;
172
+ if (newHeight < 0)
173
+ newHeight = 0;
174
+ return setByAnchor(s, 0, -0.5, { width: s.width, height: newHeight, cropX: s.cropX, cropY });
175
+ };
176
+ export const cropFromBottomFlig = (s, pointer) => {
177
+ const { height, cropY } = s;
178
+ const local = cropLocalPoint(s, pointer);
179
+ let changeY = height / 2 - local.y / s.scaleY;
180
+ let newHeight = height - changeY;
181
+ if (cropY + changeY < 0) {
182
+ changeY = -cropY;
183
+ newHeight = height + cropY;
184
+ }
185
+ if (newHeight <= 0) {
186
+ changeY += newHeight;
187
+ newHeight = 0;
188
+ }
189
+ return setByAnchor(s, 0, -0.5, { width: s.width, height: newHeight, cropX: s.cropX, cropY: cropY + changeY });
190
+ };
191
+ /** 边手柄分发(有意偏差:恒走非 Flig——见 cropLocalPoint 的翻转修正注;
192
+ * 手柄 id = 源空间侧(offset 经翻转矩阵渲染到镜像侧),un-flip 后语义自然对齐) */
193
+ export const applyCropEdge = (s, edge, pointer) => {
194
+ if (edge === 'left' || edge === 'right') {
195
+ return edge === 'left' ? cropFromLeft(s, pointer) : cropFromRight(s, pointer);
196
+ }
197
+ return edge === 'top' ? cropFromTop(s, pointer) : cropFromBottom(s, pointer);
198
+ };
199
+ /**
200
+ * 角括号手柄 = X/Y 两轴独立裁剪(应用 cropFromTopLeft 组合:left 先 top 后,
201
+ * 后者读前者已改的 width/height——Y 轴锚定用更新后的 dims,逐步组合同源)。
202
+ */
203
+ export const applyCropCorner = (s, corner, pointer) => {
204
+ const horizontal = corner === 'tl' || corner === 'bl' ? 'left' : 'right';
205
+ const vertical = corner === 'tl' || corner === 'tr' ? 'top' : 'bottom';
206
+ const afterX = applyCropEdge(s, horizontal, pointer);
207
+ return applyCropEdge(afterX, vertical, pointer);
208
+ };
209
+ // ── 比例约束角手柄(cropAspectRatio = height/width;锚定对角)────────────────
210
+ export const constrainToRatio = (rawWidth, rawHeight, ratio) => {
211
+ const widthByHeight = rawHeight / ratio;
212
+ if (rawWidth <= widthByHeight) {
213
+ return { width: rawWidth, height: rawWidth * ratio };
214
+ }
215
+ return { width: rawHeight / ratio, height: rawHeight };
216
+ };
217
+ export const applyCropRatioCorner = (s, corner, pointer, ratio) => {
218
+ const local = cropLocalPoint(s, pointer);
219
+ const px = local.x / s.scaleX;
220
+ const py = local.y / s.scaleY;
221
+ if (corner === 'br') {
222
+ // 锚定左上:从左上到指针的可用空间
223
+ let rawWidth = px - -s.width / 2;
224
+ let rawHeight = py - -s.height / 2;
225
+ rawWidth = Math.min(rawWidth, s.naturalWidth - s.cropX);
226
+ rawHeight = Math.min(rawHeight, s.naturalHeight - s.cropY);
227
+ if (rawWidth <= 0 || rawHeight <= 0)
228
+ return null;
229
+ const { width, height } = constrainToRatio(rawWidth, rawHeight, ratio);
230
+ return setByAnchor(s, -0.5, -0.5, { width, height, cropX: s.cropX, cropY: s.cropY });
231
+ }
232
+ if (corner === 'tl') {
233
+ // 锚定右下:指针到右/下边缘的可用空间;cropX/Y 随窗缩左移
234
+ let rawWidth = s.width / 2 - px;
235
+ let rawHeight = s.height / 2 - py;
236
+ rawWidth = Math.min(rawWidth, s.width + s.cropX);
237
+ rawHeight = Math.min(rawHeight, s.height + s.cropY);
238
+ if (rawWidth <= 0 || rawHeight <= 0)
239
+ return null;
240
+ const { width, height } = constrainToRatio(rawWidth, rawHeight, ratio);
241
+ const changeX = s.width - width;
242
+ const changeY = s.height - height;
243
+ if (s.cropX + changeX < 0 || s.cropY + changeY < 0)
244
+ return null;
245
+ return setByAnchor(s, 0.5, 0.5, { width, height, cropX: s.cropX + changeX, cropY: s.cropY + changeY });
246
+ }
247
+ if (corner === 'tr') {
248
+ // 锚定左下:水平从左缘到指针(扩展)、垂直从指针到下缘(收缩)
249
+ let rawWidth = px - -s.width / 2;
250
+ let rawHeight = s.height / 2 - py;
251
+ rawWidth = Math.min(rawWidth, s.naturalWidth - s.cropX);
252
+ rawHeight = Math.min(rawHeight, s.height + s.cropY);
253
+ if (rawWidth <= 0 || rawHeight <= 0)
254
+ return null;
255
+ const { width, height } = constrainToRatio(rawWidth, rawHeight, ratio);
256
+ const changeY = s.height - height;
257
+ if (s.cropY + changeY < 0)
258
+ return null;
259
+ return setByAnchor(s, -0.5, 0.5, { width, height, cropX: s.cropX, cropY: s.cropY + changeY });
260
+ }
261
+ // bl:锚定右上:水平从指针到右缘(收缩)、垂直从上缘到指针(扩展)
262
+ let rawWidth = s.width / 2 - px;
263
+ let rawHeight = py - -s.height / 2;
264
+ rawWidth = Math.min(rawWidth, s.width + s.cropX);
265
+ rawHeight = Math.min(rawHeight, s.naturalHeight - s.cropY);
266
+ if (rawWidth <= 0 || rawHeight <= 0)
267
+ return null;
268
+ const { width, height } = constrainToRatio(rawWidth, rawHeight, ratio);
269
+ const changeX = s.width - width;
270
+ if (s.cropX + changeX < 0)
271
+ return null;
272
+ return setByAnchor(s, 0.5, -0.5, { width, height, cropX: s.cropX + changeX, cropY: s.cropY });
273
+ };
274
+ // ── 整图四角圆手柄(等比缩放;锚定对侧整图角,溢出拒绝)────────────────────
275
+ /** 等比 scale(有意偏差:恒取 min——local 已 un-flip,应用的 flip 取 max 是
276
+ * 同一未补偿坐标系的另一半错位补偿) */
277
+ const calcScaleOf = (p, fullHeight, fullWidth, flipX, flipY) => {
278
+ const sx = Math.abs(p.x / fullWidth);
279
+ const sy = Math.abs(p.y / fullHeight);
280
+ void flipX;
281
+ void flipY;
282
+ return Math.min(sx, sy);
283
+ };
284
+ export const applyScaleEquallyCrop = (s, corner, pointer) => {
285
+ if (s.width <= 0 || s.height <= 0)
286
+ return null;
287
+ const { naturalWidth: fullWidth, naturalHeight: fullHeight } = s;
288
+ const remainderX = fullWidth - s.width - s.cropX;
289
+ const remainderY = fullHeight - s.height - s.cropY;
290
+ // 各角的锚定 origin 原值(应用的 anchorOrigin 组合;resolve 后进 origin 系)
291
+ let anchorRawX;
292
+ let anchorRawY;
293
+ if (corner === 'br') {
294
+ anchorRawX = -s.cropX / s.width;
295
+ anchorRawY = -s.cropY / s.height;
296
+ }
297
+ else if (corner === 'tl') {
298
+ anchorRawX = 1 + remainderX / s.width;
299
+ anchorRawY = 1 + remainderY / s.height;
300
+ }
301
+ else if (corner === 'tr') {
302
+ anchorRawX = -s.cropX / s.width;
303
+ anchorRawY = 1 + remainderY / s.height;
304
+ }
305
+ else {
306
+ anchorRawX = 1 + remainderX / s.width;
307
+ anchorRawY = -s.cropY / s.height;
308
+ }
309
+ // 指针相对锚定 origin 的本地量(getLocalPoint:去旋转 + 轴对齐 origin 偏移)
310
+ const local = cropLocalPoint(s, pointer, resolveCropOrigin(anchorRawX), resolveCropOrigin(anchorRawY));
311
+ const scale = calcScaleOf(local, fullHeight, fullWidth, s.flipX, s.flipY);
312
+ const scaleChangeX = scale / s.scaleX;
313
+ const scaleChangeY = scale / s.scaleY;
314
+ const newWidth = s.width / scaleChangeX;
315
+ const newHeight = s.height / scaleChangeY;
316
+ const scaledRemainderX = remainderX / scaleChangeX;
317
+ const scaledRemainderY = remainderY / scaleChangeY;
318
+ let newCropX;
319
+ let newCropY;
320
+ if (corner === 'br') {
321
+ newCropX = s.cropX / scaleChangeX;
322
+ newCropY = s.cropY / scaleChangeY;
323
+ if (newCropX + newWidth > fullWidth || newCropY + newHeight > fullHeight)
324
+ return null;
325
+ }
326
+ else if (corner === 'tl') {
327
+ newCropX = fullWidth - newWidth - scaledRemainderX;
328
+ newCropY = fullHeight - newHeight - scaledRemainderY;
329
+ if (newWidth + scaledRemainderX > fullWidth || newHeight + scaledRemainderY > fullHeight)
330
+ return null;
331
+ }
332
+ else if (corner === 'tr') {
333
+ newCropX = s.cropX / scaleChangeX;
334
+ newCropY = fullHeight - newHeight - scaledRemainderY;
335
+ if (newCropX + newWidth > fullWidth || scaledRemainderY + newHeight > fullHeight)
336
+ return null;
337
+ }
338
+ else {
339
+ newCropX = fullWidth - newWidth - scaledRemainderX;
340
+ newCropY = s.cropY / scaleChangeY;
341
+ if (newWidth + scaledRemainderX > fullWidth || newHeight + newCropY > fullHeight)
342
+ return null;
343
+ }
344
+ // 回钉:锚定整图角冻结(new 锚定分数按新窗取——应用的 newExtraAnchor 组合)
345
+ let newAnchorRawX;
346
+ let newAnchorRawY;
347
+ if (corner === 'br') {
348
+ newAnchorRawX = -newCropX / newWidth;
349
+ newAnchorRawY = -newCropY / newHeight;
350
+ }
351
+ else if (corner === 'tl') {
352
+ newAnchorRawX = 1 + scaledRemainderX / newWidth;
353
+ newAnchorRawY = 1 + scaledRemainderY / newHeight;
354
+ }
355
+ else if (corner === 'tr') {
356
+ newAnchorRawX = -newCropX / newWidth;
357
+ newAnchorRawY = 1 + scaledRemainderY / newHeight;
358
+ }
359
+ else {
360
+ newAnchorRawX = 1 + scaledRemainderX / newWidth;
361
+ newAnchorRawY = -newCropY / newHeight;
362
+ }
363
+ const oldDims = scaledDims(s);
364
+ const center = centerFromPosition(s.left, s.top, s.originX, s.originY, oldDims, s.angle);
365
+ const constraint = originFromCenter(center, resolveCropOrigin(anchorRawX), resolveCropOrigin(anchorRawY), oldDims, s.angle, s.flipX, s.flipY);
366
+ const newDims = { x: newWidth * scale, y: newHeight * scale };
367
+ const newCenter = centerFromOrigin(constraint, resolveCropOrigin(newAnchorRawX), resolveCropOrigin(newAnchorRawY), newDims, s.angle, s.flipX, s.flipY);
368
+ const pos = positionFromCenter(newCenter, s.originX, s.originY, newDims, s.angle);
369
+ return { ...s, width: newWidth, height: newHeight, cropX: newCropX, cropY: newCropY, scaleX: scale, scaleY: scale, left: pos.x, top: pos.y };
370
+ };
371
+ // ── 框内拖图(应用 cropModeHandlerMoveImage / canvasMouseMove 同一套钳制)────
372
+ /** 拖图增量(画布系)→ 新 cropX/cropY;增量先过「去平移逆变换」(fabric
373
+ * moveTransformationMatrix = invert(calcTransformMatrix) 去平移——旋转/缩放/
374
+ * 翻转全随图),再取负(拖图视觉方向 = 内容滚动反向)。
375
+ * 旋转方向修正(2026-09-22 用户反馈「有旋转角时拖底图发飘」):去旋转应乘
376
+ * R(−θ)——rx/ry 两式即「按 −φ 展开」,φ 必须传 +θ;首版传了 −θ 双重取负,
377
+ * 等效乘 R(+θ),指针与内容运动方向差 2θ(45° 垂直、90° 反向) */
378
+ export const moveCropImage = (s, deltaScene) => {
379
+ const c = Math.cos(rad(s.angle));
380
+ const sn = Math.sin(rad(s.angle));
381
+ // 逆线性 = S⁻¹·F⁻¹·R(−θ):先去旋转,再除 scale(flip 并入除子)
382
+ const fx = s.flipX ? -1 : 1;
383
+ const fy = s.flipY ? -1 : 1;
384
+ const rx = c * deltaScene.x + sn * deltaScene.y;
385
+ const ry = -sn * deltaScene.x + c * deltaScene.y;
386
+ const changeX = -rx / (fx * s.scaleX);
387
+ const changeY = -ry / (fy * s.scaleY);
388
+ let cropX = s.cropX + changeX;
389
+ let cropY = s.cropY + changeY;
390
+ if (cropX < 0)
391
+ cropX = 0;
392
+ else if (cropX + s.width > s.naturalWidth)
393
+ cropX = s.naturalWidth - s.width;
394
+ if (cropY < 0)
395
+ cropY = 0;
396
+ else if (cropY + s.height > s.naturalHeight)
397
+ cropY = s.naturalHeight - s.height;
398
+ return { cropX, cropY };
399
+ };
400
+ // ── 比例预设(ImageClip presetImageClip 的 ratio 分支:整图内居中最大比例矩形)──
401
+ export const applyCropRatioPreset = (s, ratio) => {
402
+ const imageRatio = s.naturalHeight / s.naturalWidth;
403
+ let newWidth;
404
+ let newHeight;
405
+ if (imageRatio > ratio) {
406
+ newWidth = s.naturalWidth;
407
+ newHeight = s.naturalWidth * ratio;
408
+ }
409
+ else {
410
+ newHeight = s.naturalHeight;
411
+ newWidth = s.naturalHeight / ratio;
412
+ }
413
+ const newCropX = (s.naturalWidth - newWidth) / 2;
414
+ const newCropY = (s.naturalHeight - newHeight) / 2;
415
+ // 预设直写 width/cropX 且 setCoords+setupDragMatrix——left/top 不动(窗锚点跟随
416
+ // origin,尺寸变化后中心平移由引擎 position 语义自动呈现);应用不重钉位置
417
+ return { ...s, width: newWidth, height: newHeight, cropX: newCropX, cropY: newCropY };
418
+ };
419
+ // ── live ↔ raw(盘上序列化双口径映射;B1 契约 raw 派生层换算)─────────────────
420
+ /** raw(盘上形态)→ live 状态。naturalW/H 由调用方给(imageEl 已加载)。
421
+ * 越界防御(批次 319):换源后未重排几何的病态存盘(旧窗/旧偏移溢出新自然
422
+ * 尺寸)会让会话几何发散——整图框/等比手柄锚定分数进负数域、moveCropImage
423
+ * 的右缘钳制产出负 cropX。窗优先保(窗是可视内容盒)钳进 [0, natural],再把
424
+ * 偏移收进余量;sane 数据(cropX+width ≤ natural 恒成立)零改写,natural
425
+ * 未知(0,无内禀尺寸 SVG 等)不钳保持原语义 */
426
+ export const liveStateFromRaw = (raw, naturalWidth, naturalHeight) => {
427
+ const visible = raw.cropSizeMode === 'visible';
428
+ let cropX = raw.cropX ?? 0;
429
+ let cropY = raw.cropY ?? 0;
430
+ const rawW = raw.width ?? naturalWidth;
431
+ const rawH = raw.height ?? naturalHeight;
432
+ let width = visible ? rawW : rawW - cropX;
433
+ let height = visible ? rawH : rawH - cropY;
434
+ if (naturalWidth > 0 && naturalHeight > 0) {
435
+ width = Math.min(Math.max(width, 0), naturalWidth);
436
+ height = Math.min(Math.max(height, 0), naturalHeight);
437
+ cropX = Math.min(Math.max(cropX, 0), naturalWidth - width);
438
+ cropY = Math.min(Math.max(cropY, 0), naturalHeight - height);
439
+ }
440
+ return {
441
+ left: raw.left ?? 0,
442
+ top: raw.top ?? 0,
443
+ angle: raw.angle ?? 0,
444
+ originX: raw.originX ?? 'left',
445
+ originY: raw.originY ?? 'top',
446
+ flipX: raw.flipX === true,
447
+ flipY: raw.flipY === true,
448
+ width,
449
+ height,
450
+ cropX,
451
+ cropY,
452
+ scaleX: raw.scaleX ?? 1,
453
+ scaleY: raw.scaleY ?? 1,
454
+ naturalWidth,
455
+ naturalHeight,
456
+ cropSizeMode: raw.cropSizeMode ?? 'legacy'
457
+ };
458
+ };
459
+ /** live 状态 → raw 写回字段(left/top/width/height/cropX/cropY/scale;cropSizeMode 保持盘上原值) */
460
+ export const liveStateToRawProps = (s) => {
461
+ const visible = s.cropSizeMode === 'visible';
462
+ return {
463
+ left: s.left,
464
+ top: s.top,
465
+ width: visible ? s.width : s.width + s.cropX,
466
+ height: visible ? s.height : s.height + s.cropY,
467
+ cropX: s.cropX,
468
+ cropY: s.cropY,
469
+ scaleX: s.scaleX,
470
+ scaleY: s.scaleY
471
+ };
472
+ };
473
+ /** 手柄在「窗本地系」的归一化位置(渲染/命中共用) */
474
+ export const CROP_HANDLE_OFFSETS = {
475
+ tl: { x: -0.5, y: -0.5 },
476
+ tr: { x: 0.5, y: -0.5 },
477
+ bl: { x: -0.5, y: 0.5 },
478
+ br: { x: 0.5, y: 0.5 },
479
+ left: { x: -0.5, y: 0 },
480
+ right: { x: 0.5, y: 0 },
481
+ top: { x: 0, y: -0.5 },
482
+ bottom: { x: 0, y: 0.5 }
483
+ };
484
+ /** 整图四角(tlS 等)在窗本地系的坐标(应用 imageCorner* 同式) */
485
+ export const fullImageCornerLocal = (s, corner) => {
486
+ const xLeft = -s.width / 2 - s.cropX;
487
+ const yTop = -s.height / 2 - s.cropY;
488
+ const xRight = s.naturalWidth - s.width / 2 - s.cropX;
489
+ const yBottom = s.naturalHeight - s.height / 2 - s.cropY;
490
+ if (corner === 'tl')
491
+ return { x: xLeft, y: yTop };
492
+ if (corner === 'tr')
493
+ return { x: xRight, y: yTop };
494
+ if (corner === 'bl')
495
+ return { x: xLeft, y: yBottom };
496
+ return { x: xRight, y: yBottom };
497
+ };