@yft-design/engine 0.0.17 → 0.0.19

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 (279) hide show
  1. package/README.md +23 -11
  2. package/dist/EngineCanvas.d.ts +0 -255
  3. package/dist/core/color.d.ts +0 -11
  4. package/dist/core/distanceField.d.ts +0 -13
  5. package/dist/core/events.d.ts +0 -25
  6. package/dist/core/gradient.d.ts +0 -47
  7. package/dist/core/homography.d.ts +0 -20
  8. package/dist/core/imageFilters.d.ts +0 -31
  9. package/dist/core/mat2d.d.ts +0 -62
  10. package/dist/core/pathData.d.ts +0 -36
  11. package/dist/core/pattern.d.ts +0 -14
  12. package/dist/core/point.d.ts +0 -14
  13. package/dist/core/psdEffects.d.ts +0 -48
  14. package/dist/core/roundedRect.d.ts +0 -48
  15. package/dist/core/shadow.d.ts +0 -23
  16. package/dist/core/snap.d.ts +0 -103
  17. package/dist/core/stroke.d.ts +0 -13
  18. package/dist/core/triangulate.d.ts +0 -31
  19. package/dist/core/warpMesh.d.ts +0 -38
  20. package/dist/debug/canvasInspection.d.ts +0 -20
  21. package/dist/debug/pixelDiff.d.ts +0 -7
  22. package/dist/debug/pixelDiff.js +1 -64
  23. package/dist/debug/seed.d.ts +0 -426
  24. package/dist/debug/seed.js +28 -3806
  25. package/dist/decoders/webp2sprite.d.ts +0 -16
  26. package/dist/engineFlag.d.ts +0 -76
  27. package/dist/export/SceneExporter.d.ts +0 -58
  28. package/dist/export/glExport.d.ts +0 -41
  29. package/dist/export/svgExport.d.ts +0 -181
  30. package/dist/index.d.ts +1 -14
  31. package/dist/index.js +1060 -64
  32. package/dist/interact/animationPlayback.d.ts +0 -62
  33. package/dist/interact/canvasInteractions.d.ts +1 -38
  34. package/dist/interact/cropSession.d.ts +0 -68
  35. package/dist/interact/dblClick.d.ts +0 -32
  36. package/dist/interact/expandSession.d.ts +0 -46
  37. package/dist/interact/hotkeyActions.d.ts +0 -12
  38. package/dist/interact/pageTransfer.d.ts +0 -2
  39. package/dist/interact/pathSession.d.ts +0 -42
  40. package/dist/interact/pointer.d.ts +0 -87
  41. package/dist/interact/pointerTablePuzzle.d.ts +0 -60
  42. package/dist/interact/pointerTransform.d.ts +0 -48
  43. package/dist/interact/pointerTypes.d.ts +6 -148
  44. package/dist/interact/snapGuide.d.ts +0 -68
  45. package/dist/interact/tableCellEditing.d.ts +0 -34
  46. package/dist/interact/tableInteraction.d.ts +0 -31
  47. package/dist/interact/textEditing.d.ts +0 -44
  48. package/dist/interact/warpFacade.d.ts +0 -5
  49. package/dist/interact/warpSession.d.ts +0 -55
  50. package/dist/interact/wheelScroll.d.ts +0 -42
  51. package/dist/model/alphaHit.d.ts +0 -51
  52. package/dist/model/animation.d.ts +0 -99
  53. package/dist/model/arcLayout.d.ts +0 -33
  54. package/dist/model/arcTextTransform.d.ts +0 -4
  55. package/dist/model/compatEvents.d.ts +0 -72
  56. package/dist/model/compatObject.d.ts +0 -360
  57. package/dist/model/cropEdit.d.ts +0 -60
  58. package/dist/model/document.d.ts +0 -167
  59. package/dist/model/elementTypes.d.ts +0 -47
  60. package/dist/model/expandEdit.d.ts +0 -36
  61. package/dist/model/fabricPrimitives.d.ts +0 -24
  62. package/dist/model/filler.d.ts +0 -12
  63. package/dist/model/groupAssemble.d.ts +0 -5
  64. package/dist/model/groupChildMove.d.ts +0 -37
  65. package/dist/model/groupResize.d.ts +0 -42
  66. package/dist/model/handles.d.ts +0 -24
  67. package/dist/model/imageMaskBake.d.ts +0 -50
  68. package/dist/model/lineConstruct.d.ts +0 -8
  69. package/dist/model/maskCropEdit.d.ts +0 -34
  70. package/dist/model/pathBackground.d.ts +0 -33
  71. package/dist/model/pathConstruct.d.ts +0 -24
  72. package/dist/model/pathEdit.d.ts +0 -55
  73. package/dist/model/puzzleEdit.d.ts +0 -66
  74. package/dist/model/puzzleLayout.d.ts +0 -31
  75. package/dist/model/scene.d.ts +0 -603
  76. package/dist/model/schema.d.ts +0 -48
  77. package/dist/model/shapePath.d.ts +0 -5
  78. package/dist/model/smartObjectBake.d.ts +0 -6
  79. package/dist/model/tableEdit.d.ts +0 -90
  80. package/dist/model/tableFacade.d.ts +0 -5
  81. package/dist/model/tableLayout.d.ts +0 -40
  82. package/dist/model/textAlign.d.ts +0 -22
  83. package/dist/model/textLayout.d.ts +0 -213
  84. package/dist/render/GLBlend.d.ts +0 -24
  85. package/dist/render/GLContext.d.ts +0 -11
  86. package/dist/render/GLIsolate.d.ts +0 -25
  87. package/dist/render/GLTextureCache.d.ts +0 -72
  88. package/dist/render/GlyphAtlas.d.ts +0 -98
  89. package/dist/render/MeshBatch.d.ts +0 -71
  90. package/dist/render/QuadBatch.d.ts +0 -87
  91. package/dist/render/SceneRenderer.d.ts +0 -137
  92. package/dist/render/SourceManager.d.ts +0 -82
  93. package/dist/render/TextRenderer.d.ts +0 -120
  94. package/dist/render/arcText.d.ts +0 -28
  95. package/dist/render/bakeGate.d.ts +0 -35
  96. package/dist/render/batchOrder.d.ts +0 -60
  97. package/dist/render/blendMode.d.ts +0 -27
  98. package/dist/render/clipMaskPainter.d.ts +0 -42
  99. package/dist/render/fontInvalidation.d.ts +0 -18
  100. package/dist/render/fontInvalidationController.d.ts +0 -52
  101. package/dist/render/frameOverlay.d.ts +0 -32
  102. package/dist/render/framePipeline.d.ts +0 -27
  103. package/dist/render/gpuFilterPasses.d.ts +0 -15
  104. package/dist/render/gpuFx.d.ts +0 -82
  105. package/dist/render/gpuImageFilters.d.ts +0 -41
  106. package/dist/render/gpuShadow.d.ts +0 -38
  107. package/dist/render/gradientRamp.d.ts +0 -14
  108. package/dist/render/imageEffects.d.ts +0 -99
  109. package/dist/render/lineDecorationSdf.d.ts +0 -32
  110. package/dist/render/maskRender.d.ts +0 -60
  111. package/dist/render/multiPage.d.ts +0 -91
  112. package/dist/render/progressiveLoad.d.ts +0 -27
  113. package/dist/render/psdEffectPainter.d.ts +0 -25
  114. package/dist/render/psdWarpText.d.ts +0 -19
  115. package/dist/render/puzzleClamp.d.ts +0 -15
  116. package/dist/render/renderEpoch.d.ts +0 -21
  117. package/dist/render/retainedFrame.d.ts +0 -36
  118. package/dist/render/ruler.d.ts +0 -36
  119. package/dist/render/screenClip.d.ts +0 -31
  120. package/dist/render/scrollText.d.ts +0 -46
  121. package/dist/render/segmentTable.d.ts +0 -30
  122. package/dist/render/selectionOverlay.d.ts +0 -203
  123. package/dist/render/settleScheduler.d.ts +0 -24
  124. package/dist/render/shaders.d.ts +4 -24
  125. package/dist/render/shadowBake.d.ts +0 -28
  126. package/dist/render/shadowPainter.d.ts +0 -8
  127. package/dist/render/sharedGpuPainters.d.ts +0 -13
  128. package/dist/render/sourceHydration.d.ts +0 -8
  129. package/dist/render/strokeBake.d.ts +0 -59
  130. package/dist/render/strokeMiterOutline.d.ts +0 -29
  131. package/dist/render/strokePainter.d.ts +0 -31
  132. package/dist/render/strokeSdf.d.ts +0 -52
  133. package/dist/render/svgRaster.d.ts +0 -34
  134. package/dist/render/tableOverlay.d.ts +0 -35
  135. package/dist/render/textHighlight.d.ts +0 -67
  136. package/dist/render/textureUpload.d.ts +0 -13
  137. package/dist/render/verticalText.d.ts +0 -21
  138. package/dist/render/warpText.d.ts +0 -48
  139. package/dist/render/workspaceBackdrop.d.ts +0 -22
  140. package/dist/render/workspaceChecker.d.ts +0 -34
  141. package/dist/render/workspaceGlow.d.ts +0 -38
  142. package/package.json +4 -3
  143. package/dist/EngineCanvas.js +0 -1202
  144. package/dist/core/canvasPerf.js +0 -170
  145. package/dist/core/color.js +0 -107
  146. package/dist/core/distanceField.js +0 -92
  147. package/dist/core/events.js +0 -80
  148. package/dist/core/gradient.js +0 -175
  149. package/dist/core/homography.js +0 -123
  150. package/dist/core/imageFilters.js +0 -330
  151. package/dist/core/mat2d.js +0 -241
  152. package/dist/core/pathData.js +0 -926
  153. package/dist/core/pattern.js +0 -40
  154. package/dist/core/point.js +0 -119
  155. package/dist/core/psdEffects.js +0 -158
  156. package/dist/core/roundedRect.js +0 -164
  157. package/dist/core/shadow.js +0 -47
  158. package/dist/core/snap.js +0 -793
  159. package/dist/core/stroke.js +0 -45
  160. package/dist/core/triangulate.js +0 -194
  161. package/dist/core/warpMesh.js +0 -469
  162. package/dist/debug/canvasInspection.js +0 -113
  163. package/dist/decoders/gif2sprite.js +0 -84
  164. package/dist/decoders/webp2sprite.js +0 -133
  165. package/dist/engineFlag.js +0 -104
  166. package/dist/export/SceneExporter.js +0 -286
  167. package/dist/export/glExport.js +0 -79
  168. package/dist/export/svgExport.js +0 -1803
  169. package/dist/interact/animationPlayback.js +0 -358
  170. package/dist/interact/canvasInteractions.js +0 -174
  171. package/dist/interact/cropSession.js +0 -527
  172. package/dist/interact/dblClick.js +0 -120
  173. package/dist/interact/expandSession.js +0 -282
  174. package/dist/interact/hotkeyActions.js +0 -42
  175. package/dist/interact/pageTransfer.js +0 -56
  176. package/dist/interact/pathSession.js +0 -480
  177. package/dist/interact/pointer.js +0 -1692
  178. package/dist/interact/pointerTablePuzzle.js +0 -358
  179. package/dist/interact/pointerTransform.js +0 -441
  180. package/dist/interact/pointerTypes.js +0 -96
  181. package/dist/interact/snapGuide.js +0 -377
  182. package/dist/interact/tableCellEditing.js +0 -207
  183. package/dist/interact/tableInteraction.js +0 -219
  184. package/dist/interact/textEditing.js +0 -423
  185. package/dist/interact/warpFacade.js +0 -51
  186. package/dist/interact/warpSession.js +0 -583
  187. package/dist/interact/wheelScroll.js +0 -100
  188. package/dist/model/alphaHit.js +0 -267
  189. package/dist/model/animation.js +0 -510
  190. package/dist/model/arcLayout.js +0 -308
  191. package/dist/model/arcTextTransform.js +0 -87
  192. package/dist/model/compatEvents.js +0 -96
  193. package/dist/model/compatObject.js +0 -1583
  194. package/dist/model/cropEdit.js +0 -497
  195. package/dist/model/document.js +0 -590
  196. package/dist/model/elementTypes.js +0 -119
  197. package/dist/model/expandEdit.js +0 -161
  198. package/dist/model/fabricPrimitives.js +0 -9
  199. package/dist/model/filler.js +0 -1
  200. package/dist/model/groupAssemble.js +0 -98
  201. package/dist/model/groupChildMove.js +0 -145
  202. package/dist/model/groupResize.js +0 -109
  203. package/dist/model/handles.js +0 -29
  204. package/dist/model/imageMaskBake.js +0 -136
  205. package/dist/model/lineConstruct.js +0 -79
  206. package/dist/model/maskCropEdit.js +0 -90
  207. package/dist/model/pathBackground.js +0 -196
  208. package/dist/model/pathConstruct.js +0 -55
  209. package/dist/model/pathEdit.js +0 -378
  210. package/dist/model/puzzleEdit.js +0 -742
  211. package/dist/model/puzzleLayout.js +0 -143
  212. package/dist/model/scene.js +0 -2930
  213. package/dist/model/schema.js +0 -13
  214. package/dist/model/shapePath.js +0 -137
  215. package/dist/model/smartObjectBake.js +0 -295
  216. package/dist/model/tableEdit.js +0 -751
  217. package/dist/model/tableFacade.js +0 -808
  218. package/dist/model/tableLayout.js +0 -194
  219. package/dist/model/textAlign.js +0 -36
  220. package/dist/model/textLayout.js +0 -842
  221. package/dist/render/GLBlend.js +0 -270
  222. package/dist/render/GLContext.js +0 -48
  223. package/dist/render/GLIsolate.js +0 -188
  224. package/dist/render/GLTextureCache.js +0 -224
  225. package/dist/render/GlyphAtlas.js +0 -412
  226. package/dist/render/MeshBatch.js +0 -416
  227. package/dist/render/QuadBatch.js +0 -561
  228. package/dist/render/SceneRenderer.js +0 -753
  229. package/dist/render/SourceManager.js +0 -523
  230. package/dist/render/TextRenderer.js +0 -854
  231. package/dist/render/arcText.js +0 -187
  232. package/dist/render/bakeGate.js +0 -32
  233. package/dist/render/batchOrder.js +0 -168
  234. package/dist/render/blendMode.js +0 -142
  235. package/dist/render/clipMaskPainter.js +0 -244
  236. package/dist/render/fontInvalidation.js +0 -77
  237. package/dist/render/fontInvalidationController.js +0 -119
  238. package/dist/render/frameOverlay.js +0 -124
  239. package/dist/render/framePipeline.js +0 -96
  240. package/dist/render/gpuFilterPasses.js +0 -883
  241. package/dist/render/gpuFx.js +0 -673
  242. package/dist/render/gpuImageFilters.js +0 -295
  243. package/dist/render/gpuShadow.js +0 -327
  244. package/dist/render/gradientRamp.js +0 -74
  245. package/dist/render/imageEffects.js +0 -415
  246. package/dist/render/lineDecorationSdf.js +0 -189
  247. package/dist/render/maskRender.js +0 -217
  248. package/dist/render/multiPage.js +0 -420
  249. package/dist/render/progressiveLoad.js +0 -86
  250. package/dist/render/psdEffectPainter.js +0 -333
  251. package/dist/render/psdWarpText.js +0 -212
  252. package/dist/render/puzzleClamp.js +0 -30
  253. package/dist/render/renderEpoch.js +0 -35
  254. package/dist/render/renderViewport.js +0 -18
  255. package/dist/render/retainedFrame.js +0 -192
  256. package/dist/render/ruler.js +0 -287
  257. package/dist/render/screenClip.js +0 -67
  258. package/dist/render/scrollText.js +0 -266
  259. package/dist/render/segmentTable.js +0 -117
  260. package/dist/render/selectionOverlay.js +0 -1008
  261. package/dist/render/settleScheduler.js +0 -51
  262. package/dist/render/shaders.js +0 -450
  263. package/dist/render/shadowBake.js +0 -203
  264. package/dist/render/shadowPainter.js +0 -93
  265. package/dist/render/sharedGpuPainters.js +0 -42
  266. package/dist/render/sourceHydration.js +0 -48
  267. package/dist/render/strokeBake.js +0 -516
  268. package/dist/render/strokeMiterOutline.js +0 -224
  269. package/dist/render/strokePainter.js +0 -207
  270. package/dist/render/strokeSdf.js +0 -262
  271. package/dist/render/svgRaster.js +0 -71
  272. package/dist/render/tableOverlay.js +0 -233
  273. package/dist/render/textHighlight.js +0 -380
  274. package/dist/render/textureUpload.js +0 -26
  275. package/dist/render/verticalText.js +0 -63
  276. package/dist/render/warpText.js +0 -384
  277. package/dist/render/workspaceBackdrop.js +0 -60
  278. package/dist/render/workspaceChecker.js +0 -170
  279. package/dist/render/workspaceGlow.js +0 -198
@@ -1,480 +0,0 @@
1
- /**
2
- * 路径族锚点编辑会话(P4-7):应用 FabricPath/Polygon/Polyline/Line 的
3
- * changeControls 编辑态引擎面(crop/expand 会话同模式)。
4
- *
5
- * 会话语义(应用口径):
6
- * - 进入:单选路径族节点(Path=命令锚点+贝塞尔手柄 / polygon·polyline=顶点 /
7
- * line=两端点);进入即吞没常规交互(选区 UI 换锚点覆盖层)
8
- * - 命中序(应用 mouse:down:before → down 两段判定同序):锚点/手柄(12px 屏幕
9
- * 半径)> 段上插点候选(8px,端点 6px 让位——悬停出预览点、点击插入)> 本体
10
- * (拖动=移动对象)> 框外退出。**点外退出不吞没本次点击**——同击继续常规
11
- * 命中/选区(应用 fabricCanvas._maybeExitEditingPath 同击换选语义,与 crop
12
- * 会话整击吞没不同)
13
- * - 拖锚点/手柄:逐帧绝对落点(幂等)——指针经逆矩阵入本地系写指令坐标位,
14
- * 随后按 fabric movePathPoint 同式补偿 left/top(pathOffset 位移经线性部映
15
- * 射进场景,未拖动点场景位恒定)
16
- * - 插点(de Casteljau 拆段)与 L↔C 类型转换立即提交(fabric 在操作点即发
17
- * object:modified:modifyPathInsert / modifyPathAnchorType);拖拽提交在 up
18
- * - 退出(Esc/点外/程序化)不发 object:modified(应用 changeControls(false)
19
- * 同口径——历史条目已由各操作提交)
20
- * - 选中锚点变化发 document 事件 path:active-anchor-change(target, key)——
21
- * 面板锚点类型切换消费(应用 canvas 事件同名透传)
22
- */
23
- import { invert, objectOriginFromCenter, transformPoint } from '../core/mat2d';
24
- import { getBoundsOfPath, parsePathData } from '../core/pathData';
25
- import { parseColor } from '../core/color';
26
- import { LineNode } from '../model/scene';
27
- import { ElementTypes } from '../model/elementTypes';
28
- import { describePathEdit, describePolyEdit, findInsertCandidate, insertAnchorAt, pathBBoxCenter, polyBBoxCenter, setAnchorType as setAnchorTypeOp, shiftedCenter, writePathCoordinate, writePolyPoint } from '../model/pathEdit';
29
- import { buildNodeBorderOverlay, lineQuad } from '../render/selectionOverlay';
30
- /** 锚点/手柄命中半径(屏幕 px;引擎手柄命中惯例同 selectionOverlay.HANDLE_HIT_PX) */
31
- const ANCHOR_HIT_PX = 12;
32
- /** 应用 FabricPath 的控件配色(PATH_CONTROL_STROKE) */
33
- const PATH_CONTROL_COLOR = parseColor('#2254f4');
34
- const CONTROL_WHITE = parseColor('#ffffff');
35
- /** 控件视觉常量(屏幕 px):锚点方点 7、手柄圆点 5、连接线 1 */
36
- const ANCHOR_SIZE_PX = 7;
37
- const HANDLE_R_PX = 5;
38
- const CONNECTION_PX = 1;
39
- /** 插点预览与手柄共用的圆点 sprite(白填 + 蓝环;模块级单例,经 GLTextureCache 上传) */
40
- let circleSprite = null;
41
- const getCircleSprite = () => {
42
- if (circleSprite)
43
- return circleSprite;
44
- const size = Math.ceil((HANDLE_R_PX + 2) * 2);
45
- const canvas = document.createElement('canvas');
46
- canvas.width = size;
47
- canvas.height = size;
48
- const ctx = canvas.getContext('2d');
49
- ctx.beginPath();
50
- ctx.arc(size / 2, size / 2, HANDLE_R_PX, 0, Math.PI * 2);
51
- ctx.fillStyle = '#ffffff';
52
- ctx.fill();
53
- ctx.lineWidth = 1.5;
54
- ctx.strokeStyle = '#2254f4';
55
- ctx.stroke();
56
- circleSprite = canvas;
57
- return canvas;
58
- };
59
- /** 编辑族判定:Path(命令)/ polygon·polyline(points)/ Line(端点);null = 不可编辑 */
60
- export const pathEditKindOf = (node) => {
61
- const t = node.typeKey;
62
- if (t === ElementTypes.PATH)
63
- return 'path';
64
- if (t === ElementTypes.POLYGON || t === ElementTypes.POLYLINE)
65
- return 'poly';
66
- if (t === ElementTypes.LINE)
67
- return 'line';
68
- return null;
69
- };
70
- export class PathSessionController {
71
- host;
72
- node = null;
73
- kind = null;
74
- activeAnchorKey;
75
- insertCandidate;
76
- detachKey = null;
77
- constructor(host) {
78
- this.host = host;
79
- }
80
- get active() {
81
- return this.node !== null;
82
- }
83
- get target() {
84
- return this.node;
85
- }
86
- get info() {
87
- const node = this.node;
88
- const kind = this.kind;
89
- if (!node || !kind)
90
- return null;
91
- const matrix = node.matrix;
92
- const anchors = this.currentHandles().map((h) => {
93
- const scene = transformPoint(matrix, h.local.x, h.local.y);
94
- return { key: h.key, kind: h.kind, x: scene.x, y: scene.y };
95
- });
96
- return {
97
- nodeId: node.id,
98
- kind,
99
- anchors,
100
- activeAnchorKey: this.activeAnchorKey,
101
- activeAnchorIndex: this.activeAnchorIndex(),
102
- insertCandidate: this.insertCandidate
103
- ? { commandIndex: this.insertCandidate.commandIndex, commandType: this.insertCandidate.commandType, x: this.insertCandidate.scene.x, y: this.insertCandidate.scene.y }
104
- : undefined
105
- };
106
- }
107
- /** 进入编辑(应用 changeControls(true)):吞没常规交互,选区 UI 换锚点覆盖层 */
108
- enter(node) {
109
- if (this.node)
110
- this.exit();
111
- const kind = pathEditKindOf(node);
112
- if (!kind)
113
- return false;
114
- if (kind === 'path' && !parsePathData(node.raw.path))
115
- return false;
116
- if (kind === 'poly' && !Array.isArray(node.raw.points))
117
- return false;
118
- if (kind === 'line' && !(node instanceof LineNode))
119
- return false;
120
- this.node = node;
121
- this.kind = kind;
122
- this.activeAnchorKey = undefined;
123
- this.insertCandidate = undefined;
124
- // 编辑态让位(文本编辑退出;选中保持单选本节点)
125
- this.host.document.exitEditing();
126
- this.host.document.setSelection([node], 'update');
127
- const onKeyDown = (e) => {
128
- if (e.key === 'Escape' && this.node)
129
- this.exit();
130
- };
131
- window.addEventListener('keydown', onKeyDown);
132
- this.detachKey = () => window.removeEventListener('keydown', onKeyDown);
133
- this.host.requestRenderAll();
134
- return true;
135
- }
136
- /** 退出(Esc/点外/程序化同径):不发 object:modified——差量提交已在各操作点 */
137
- exit() {
138
- if (!this.node)
139
- return;
140
- this.node = null;
141
- this.kind = null;
142
- this.activeAnchorKey = undefined;
143
- this.insertCandidate = undefined;
144
- this.detachKey?.();
145
- this.detachKey = null;
146
- this.host.requestRenderAll();
147
- }
148
- exitByOutsideClick() {
149
- this.exit();
150
- }
151
- // ── 命中(渲染/命中同源 currentHandles) ──
152
- hitAnchor(cssX, cssY) {
153
- const node = this.node;
154
- if (!node)
155
- return null;
156
- const matrix = node.matrix;
157
- let best = null;
158
- let bestDist = ANCHOR_HIT_PX;
159
- for (const h of this.currentHandles()) {
160
- const scene = transformPoint(matrix, h.local.x, h.local.y);
161
- const screen = this.host.toScreen(scene.x, scene.y);
162
- const d = Math.hypot(screen.x - cssX, screen.y - cssY);
163
- if (d <= bestDist) {
164
- bestDist = d;
165
- best = h.key;
166
- }
167
- }
168
- return best;
169
- }
170
- hitBody(sceneX, sceneY) {
171
- const node = this.node;
172
- if (!node)
173
- return false;
174
- // path/poly 按轮廓 fill 规则、line 按线段距离(containsPoint 多态)——
175
- // 描边-only 开路径按轮廓内部的实用近似(fabric 编辑态点本体即可拖动)
176
- return node.containsPoint(sceneX, sceneY);
177
- }
178
- findInsertCandidate(sceneX, sceneY) {
179
- const node = this.node;
180
- if (!node || this.kind !== 'path')
181
- return null;
182
- const commands = parsePathData(node.raw.path);
183
- const center = pathBBoxCenter(commands);
184
- if (!commands || !center)
185
- return null;
186
- return findInsertCandidate(commands, center, { x: sceneX, y: sceneY }, node.matrix, this.host.zoom) ?? null;
187
- }
188
- // ── 拖拽(逐帧绝对落点——幂等,同引擎其它手势的快照惯例) ──
189
- startAnchorDrag(key) {
190
- if (!this.node)
191
- return false;
192
- const exists = this.currentHandles().some((h) => h.key === key);
193
- if (!exists)
194
- return false;
195
- this.captureDragBefore();
196
- this.setActiveAnchor(key);
197
- return true;
198
- }
199
- applyAnchorDrag(key, scenePoint) {
200
- const node = this.node;
201
- const kind = this.kind;
202
- if (!node || !kind)
203
- return;
204
- const handle = this.currentHandles().find((h) => h.key === key);
205
- if (!handle)
206
- return;
207
- const matrix = node.matrix;
208
- const local = transformPoint(invert(matrix), scenePoint.x, scenePoint.y);
209
- if (kind === 'path') {
210
- const commands = parsePathData(node.raw.path);
211
- const center = pathBBoxCenter(commands);
212
- if (!commands || !center)
213
- return;
214
- const next = writePathCoordinate(commands, handle.commandIndex, handle.pointIndex, local, center);
215
- this.writeBackWithCompensation(node, next, center, pathBBoxCenter(next), getBoundsOfPath(next));
216
- }
217
- else if (kind === 'poly') {
218
- const points = node.raw.points;
219
- const center = polyBBoxCenter(points);
220
- if (!center)
221
- return;
222
- const next = writePolyPoint(points, handle.commandIndex, local, center);
223
- let minX = Infinity;
224
- let minY = Infinity;
225
- let maxX = -Infinity;
226
- let maxY = -Infinity;
227
- for (const p of next) {
228
- if (minX > p.x)
229
- minX = p.x;
230
- if (minY > p.y)
231
- minY = p.y;
232
- if (maxX < p.x)
233
- maxX = p.x;
234
- if (maxY < p.y)
235
- maxY = p.y;
236
- }
237
- this.writeBackWithCompensation(node, next, center, polyBBoxCenter(next), { left: minX, top: minY, right: maxX, bottom: maxY });
238
- }
239
- else {
240
- // line:x1..y2 即中心系本地位(无 pathOffset)——中心保持不动,left/top 按新 dims 反解
241
- const props = handle.commandIndex === 0 ? { x1: local.x, y1: local.y } : { x2: local.x, y2: local.y };
242
- const x1 = Number(props.x1 ?? node.raw.x1);
243
- const y1 = Number(props.y1 ?? node.raw.y1);
244
- const x2 = Number(props.x2 ?? node.raw.x2);
245
- const y2 = Number(props.y2 ?? node.raw.y2);
246
- const origin = objectOriginFromCenter({ ...node.raw, width: Math.abs(x2 - x1), height: Math.abs(y2 - y1) }, node.center);
247
- this.host.document.setProps(node, { ...props, left: origin.x, top: origin.y }, { deferIndexRebuild: true });
248
- }
249
- this.host.requestRenderAll();
250
- }
251
- /** 本体拖动(fabric 编辑态拖对象体 = 移动对象) */
252
- moveBodyBy(delta) {
253
- const node = this.node;
254
- if (!node)
255
- return;
256
- this.captureDragBefore();
257
- this.host.document.setProps(node, { left: (node.raw.left ?? 0) + delta.x, top: (node.raw.top ?? 0) + delta.y }, { deferIndexRebuild: true });
258
- this.host.requestRenderAll();
259
- }
260
- /** P5-4:拖拽起点的整对象快照(首次写入前一击捕获;提交后清空) */
261
- captureDragBefore() {
262
- this._dragBefore ??= this.node ? this.node.toObject() : null;
263
- }
264
- _dragBefore = null;
265
- commit() {
266
- const node = this.node;
267
- if (!node)
268
- return;
269
- const before = this._dragBefore;
270
- this._dragBefore = null;
271
- this.host.document.applyTransform([node], () => ({}), 'modified', before ? { before: new Map([[node, before]]) } : undefined);
272
- }
273
- // ── 面板 API(compat changeControls 面消费) ──
274
- /** 插点:立即提交(fabric modifyPathInsert);返回新锚点 key(随即可转拖拽) */
275
- insertAnchor(candidate) {
276
- const node = this.node;
277
- if (!node || this.kind !== 'path')
278
- return null;
279
- const commands = parsePathData(node.raw.path);
280
- const center = pathBBoxCenter(commands);
281
- if (!commands || !center)
282
- return null;
283
- const next = insertAnchorAt(commands, center, candidate);
284
- if (!next)
285
- return null;
286
- // 插点在段上(形状不变,bbox 不动)——无 left/top 补偿(fabric 同口径)
287
- const before = node.toObject();
288
- this.host.document.setProps(node, { path: next }, { deferIndexRebuild: true });
289
- this.host.document.applyTransform([node], () => ({}), 'modified', { before: new Map([[node, before]]) });
290
- const newCommand = candidate.commandType === 'Z' ? 'L' : candidate.commandType;
291
- const key = `c_${candidate.commandIndex}_${newCommand}`;
292
- this.insertCandidate = undefined;
293
- this.setActiveAnchor(key);
294
- this.host.requestRenderAll();
295
- return key;
296
- }
297
- /** L↔C 锚点类型切换(fabric setAnchorPointType):changed 才写盘+提交;重高亮恒做 */
298
- setAnchorType(index, type) {
299
- const node = this.node;
300
- if (!node || this.kind !== 'path')
301
- return false;
302
- const commands = parsePathData(node.raw.path);
303
- if (!commands)
304
- return false;
305
- const r = setAnchorTypeOp(commands, index, type);
306
- if (!r)
307
- return false;
308
- if (r.changed) {
309
- const before = node.toObject();
310
- this.host.document.setProps(node, { path: r.commands }, { deferIndexRebuild: true });
311
- this.host.document.applyTransform([node], () => ({}), 'modified', { before: new Map([[node, before]]) });
312
- }
313
- this.setActiveAnchor(`c_${index}_${String(r.commands[index][0])}`);
314
- this.host.requestRenderAll();
315
- return r.changed;
316
- }
317
- /** 选中锚点(点击/插入/转换时):面板读位 + 覆盖层高亮 */
318
- setActiveAnchor(key) {
319
- if (this.activeAnchorKey === key)
320
- return;
321
- this.activeAnchorKey = key;
322
- const node = this.node;
323
- if (node)
324
- this.host.document.events.fire('path:active-anchor-change', node, key);
325
- this.host.requestRenderAll();
326
- }
327
- /** 应用 fabricCanvas 退出链的 resetAnchorStyles:清高亮(不发事件) */
328
- resetAnchorStyles() {
329
- if (this.activeAnchorKey === undefined)
330
- return;
331
- this.activeAnchorKey = undefined;
332
- this.host.requestRenderAll();
333
- }
334
- activeAnchorIndex() {
335
- const key = this.activeAnchorKey;
336
- if (!key)
337
- return undefined;
338
- if (key.startsWith('c_')) {
339
- const m = key.match(/^c_(\d+)_/);
340
- const idx = m ? Number(m[1]) : NaN;
341
- return Number.isFinite(idx) ? idx : undefined;
342
- }
343
- if (key.startsWith('p_'))
344
- return Number(key.slice(2));
345
- if (key === 'e0' || key === 'e1')
346
- return Number(key[1]);
347
- return undefined;
348
- }
349
- // ── 悬停(插点候选预览 + 光标) ──
350
- hoverCursor(cssX, cssY, scenePoint) {
351
- const anchor = this.hitAnchor(cssX, cssY);
352
- if (anchor)
353
- return anchor.includes('_CP_') ? 'crosshair' : 'default';
354
- const candidate = this.findInsertCandidate(scenePoint.x, scenePoint.y);
355
- if (candidate) {
356
- this.updateInsertCandidate(candidate);
357
- return 'copy';
358
- }
359
- this.updateInsertCandidate(undefined);
360
- return this.hitBody(scenePoint.x, scenePoint.y) ? 'move' : '';
361
- }
362
- updateInsertCandidate(candidate) {
363
- const prev = this.insertCandidate;
364
- const same = !!prev &&
365
- !!candidate &&
366
- prev.commandIndex === candidate.commandIndex &&
367
- prev.commandType === candidate.commandType &&
368
- Math.abs(prev.scene.x - candidate.scene.x) < 0.1 &&
369
- Math.abs(prev.scene.y - candidate.scene.y) < 0.1;
370
- if (same || (!prev && !candidate))
371
- return;
372
- this.insertCandidate = candidate;
373
- this.host.requestRenderAll();
374
- }
375
- // ── 覆盖层(两遍绘制:连接线在下、控制点在上——应用 _drawPathEditControls 同序) ──
376
- buildOverlay(zoom) {
377
- const node = this.node;
378
- const kind = this.kind;
379
- if (!node || !kind)
380
- return [];
381
- const matrix = node.matrix;
382
- const quads = [...buildNodeBorderOverlay(node.aCoords, zoom)];
383
- const sceneOf = (local) => transformPoint(matrix, local.x, local.y);
384
- if (kind === 'path') {
385
- const commands = parsePathData(node.raw.path);
386
- const center = pathBBoxCenter(commands);
387
- if (!commands || !center)
388
- return quads;
389
- const { handles, connections } = describePathEdit(commands, center);
390
- // 第一遍:手柄↔锚点连接线
391
- const lineW = CONNECTION_PX / zoom;
392
- for (const c of connections)
393
- quads.push(lineQuad(sceneOf(c.from), sceneOf(c.to), lineW, PATH_CONTROL_COLOR));
394
- // 第二遍:控制点(锚点方点双层 / 手柄圆点 sprite / 选中锚点蓝填)
395
- const anchorSize = ANCHOR_SIZE_PX / zoom;
396
- const sprite = getCircleSprite();
397
- const spriteSize = (sprite.width / 2) * 2 * (1 / zoom);
398
- for (const h of handles) {
399
- const scene = sceneOf(h.local);
400
- if (h.kind === 'handle') {
401
- quads.push({ matrix: [1, 0, 0, 1, scene.x, scene.y], dims: { x: spriteSize, y: spriteSize }, color: [1, 1, 1, 1], texture: sprite });
402
- }
403
- else {
404
- const active = h.key === this.activeAnchorKey;
405
- quads.push({ matrix: [1, 0, 0, 1, scene.x, scene.y], dims: { x: anchorSize, y: anchorSize }, color: PATH_CONTROL_COLOR });
406
- quads.push({ matrix: [1, 0, 0, 1, scene.x, scene.y], dims: { x: anchorSize - 2 / zoom, y: anchorSize - 2 / zoom }, color: active ? PATH_CONTROL_COLOR : CONTROL_WHITE });
407
- }
408
- }
409
- // 插点预览(白填蓝环圆点)
410
- if (this.insertCandidate) {
411
- quads.push({ matrix: [1, 0, 0, 1, this.insertCandidate.scene.x, this.insertCandidate.scene.y], dims: { x: spriteSize, y: spriteSize }, color: [1, 1, 1, 1], texture: sprite });
412
- }
413
- }
414
- else {
415
- // poly/line:方点(双层蓝白;选中蓝填)
416
- const anchorSize = ANCHOR_SIZE_PX / zoom;
417
- for (const h of this.currentHandles()) {
418
- const scene = sceneOf(h.local);
419
- const active = h.key === this.activeAnchorKey;
420
- quads.push({ matrix: [1, 0, 0, 1, scene.x, scene.y], dims: { x: anchorSize, y: anchorSize }, color: PATH_CONTROL_COLOR });
421
- quads.push({ matrix: [1, 0, 0, 1, scene.x, scene.y], dims: { x: anchorSize - 2 / zoom, y: anchorSize - 2 / zoom }, color: active ? PATH_CONTROL_COLOR : CONTROL_WHITE });
422
- }
423
- }
424
- return quads;
425
- }
426
- dispose() {
427
- this.detachKey?.();
428
- this.detachKey = null;
429
- this.node = null;
430
- this.kind = null;
431
- }
432
- // ── 内部 ──
433
- currentHandles() {
434
- const node = this.node;
435
- const kind = this.kind;
436
- if (!node || !kind)
437
- return [];
438
- if (kind === 'path') {
439
- const commands = parsePathData(node.raw.path);
440
- const center = pathBBoxCenter(commands);
441
- if (!commands || !center)
442
- return [];
443
- return describePathEdit(commands, center).handles;
444
- }
445
- if (kind === 'poly') {
446
- const points = node.raw.points;
447
- const center = polyBBoxCenter(points);
448
- if (!Array.isArray(points) || !center)
449
- return [];
450
- return describePolyEdit(points, center);
451
- }
452
- // line:两端点即中心系本地位(x1..y2 恒等映射)
453
- const x1 = Number(node.raw.x1);
454
- const y1 = Number(node.raw.y1);
455
- const x2 = Number(node.raw.x2);
456
- const y2 = Number(node.raw.y2);
457
- if (![x1, y1, x2, y2].every(Number.isFinite))
458
- return [];
459
- return [
460
- { key: 'e0', kind: 'endpoint', commandIndex: 0, pointIndex: 0, local: { x: x1, y: y1 } },
461
- { key: 'e1', kind: 'endpoint', commandIndex: 1, pointIndex: 1, local: { x: x2, y: y2 } }
462
- ];
463
- }
464
- /**
465
- * 拖点写盘 + left/top 补偿(fabric movePathPoint 参考点平移补偿的等价式):
466
- * pathOffset 从 oldC 移到 newC 时中心平移 linear·(newC−oldC),未拖动点场景位
467
- * 不动;left/top 由新 dims(新 bbox)反解。
468
- */
469
- writeBackWithCompensation(node, nextPath, oldCenter, newC, bounds) {
470
- if (!bounds)
471
- return;
472
- // 补偿系 = 父坐标系(center/left/top 同系):线性部取自身矩阵(fabric movePathPoint
473
- // 的 calcOwnMatrix 口径——组内子节点用全矩阵会把父组缩放乘进平移量)
474
- const own = node.ownMatrix;
475
- const target = shiftedCenter(node.center, oldCenter, newC, [own[0], own[1], own[2], own[3]]);
476
- const origin = objectOriginFromCenter({ ...node.raw, width: bounds.right - bounds.left, height: bounds.bottom - bounds.top }, target);
477
- const props = this.kind === 'path' ? { path: nextPath } : { points: nextPath };
478
- this.host.document.setProps(node, { ...props, left: origin.x, top: origin.y }, { deferIndexRebuild: true });
479
- }
480
- }