
LogicFlow 边进阶完全指南React 自定义边、锚点数据与 SVG 动画【免费下载链接】LogicFlowA flow chart editing framework focus on business customization. 专注于业务自定义的流程图编辑框架支持实现脑图、ER图、UML、工作流等各种图编辑场景。项目地址: https://gitcode.com/GitHub_Trending/lo/LogicFlow本文基于 sites/docs/docs/tutorial/advanced/edge.zh.md 编写面向已完成基础自定义边、希望继续深入边上的 React 内容、锚点信息与动画效果的开发者。读完本文你将掌握如何用 React 组件重绘整条边、如何在保存数据时记录锚点信息、以及如何用一行配置让边动起来并自定义动画细节。阅读前提与本页边界本页是 LogicFlow 边主题的进阶篇请先确保你已经掌握边基础篇内置边line直线、polyline直角折线、bezier贝塞尔曲线、基于继承的自定义边、register注册机制、主题与getEdgeStyle样式覆盖节点、边、graphData与render的基本用法以及 进阶节点 中关于节点规则的说明。本页不再展开节点规则与框架节点注册。相关 API 请对照查阅edgeModelAPI事件API主题APIReact 边在边上渲染任意 React 组件使用以下方法可以基于 React 组件自定义边。你可以在边上添加任何想要的 React 组件甚至将原有的边通过样式隐藏使用 React 重新绘制。完整实现拆解完整的可运行示例位于 reactEdge/index.tsx 与 reactEdge/customEdge.tsx其核心思路分为三步第一步自定义 Model控制原有边的显示class CustomEdgeModel extends BaseEdgeModel { getEdgeStyle() { const edgeStyle super.getEdgeStyle(); // 可以自己设置线的显示样式甚至隐藏掉原本的线自己用 react 绘制 edgeStyle.strokeDasharray 4 4; edgeStyle.stroke #DDDFE3; return edgeStyle; } }通过重写getEdgeStyle你可以把内置的线改成虚线、改色或将其stroke设为none彻底隐藏——此时视觉上看到的将完全是你自己的 React 内容。第二步自定义 View绘制 SVG 线 foreignObject 容器class CustomEdgeView extends LineEdge { getEdge() { const { model } this.props; const { customWidth 48, customHeight 32 } model.getProperties(); const id model.id; const edgeStyle model.getEdgeStyle(); const { startPoint, endPoint, arrowConfig } model; const lineData { x1: startPoint.x, y1: startPoint.y, x2: endPoint.x, y2: endPoint.y, }; const positionData { x: (startPoint.x endPoint.x - customWidth) / 2, y: (startPoint.y endPoint.y - customHeight) / 2, width: customWidth, height: customHeight, }; setTimeout(() { const dom document.querySelector(#${id}); if (dom) { ReactDOM.createRoot(dom).render(CustomLine /); } }, 0); return h(g, {}, [ h(line, { ...lineData, ...edgeStyle, ...arrowConfig }), h(foreignObject, { ...positionData }, [ h(div, { id, style: { width: customWidth, height: customHeight }, className: lf-custom-edge-wrapper, }), ]), ]); } // 清空追加内容避免多余图形 getAppend() { return h(g, {}, []); } }关键点说明getEdge()方法返回由h()创建的 SVG 元素树h是 LogicFlow 基于 hyperscript 风格的创建函数import { h } from logicflow/core原生line使用起点startPoint、终点endPoint绘制配合arrowConfig可保持箭头foreignObject是 SVG 中承载 HTML 内容的容器这里用它把 React 组件的挂载点div放进 SVG 坐标系并通过positionData计算居中位置组件宽度customWidth/customHeight从model.getProperties()读取默认 48×32这样每条边可以通过properties配置不同的尺寸setTimeout(..., 0)确保foreignObject内的 DOM 挂载完成后再调用ReactDOM.createRoot(dom).render(...)注入 React 内容样式文件 reactEdge/index.less 中定义了.lf-custom-edge-wrapper与.custom-edge实现容器内居中与卡片外观。第三步注册并渲染export default { type: CustomEdge, view: CustomEdgeView, model: CustomEdgeModel, };在使用侧reactEdge/index.tsxconst lf new LogicFlow({ container: containerRef.current, height: 400, }); lf.register(CustomEdge); lf.render({ nodes: [ { type: rect, x: 100, y: 100, text: 节点1, id: node_id_1 }, { type: rect, text: 节点2, x: 100, y: 300, id: node_id_2 }, ], edges: [ { id: edge_id_1, type: CustomEdge, sourceNodeId: node_id_1, targetNodeId: node_id_2, properties: {}, }, ], }); lf.translateCenter();这种原生 SVG 线 foreignObject 内嵌 React的组合非常适合需要在边上展示按钮、标签徽章、进度条等复杂交互内容的业务场景。锚点让边保存起点与终点的锚点信息默认情况下LogicFlow 只记录节点与节点的连接信息。但在一些业务场景下需要关注到锚点UML 类图中的关联关系关联的是类的某个成员而非类整体锚点表示节点的入口和出口如流程节点上的 in/out 端口保存与回显时需要精确还原连到了哪个锚点。此时需要重写连线的保存方法getData()将锚点信息一并写入图数据class CustomEdgeModel2 extends LineEdgeModel { // 重写此方法使保存数据时能带上锚点数据。 getData() { const data super.getData(); data.sourceAnchorId this.sourceAnchorId; data.targetAnchorId this.targetAnchorId; return data; } }从源码看锚点如何参与边的计算在核心包中sourceAnchorId/targetAnchorId是BaseEdgeModel的标准字段见 packages/core/src/model/edge/BaseEdgeModel.ts#L47-L48。当前仓库版本的getData()已内置透出这两个字段// packages/core/src/model/edge/BaseEdgeModel.ts#L400-L407 const data: EdgeData { id: this.id, type: this.type, properties, sourceNodeId: this.sourceNodeId, targetNodeId: this.targetNodeId, sourceAnchorId: this.sourceAnchorId, targetAnchorId: this.targetAnchorId, startPoint: assign({}, this.startPoint), endPoint: assign({}, this.endPoint), };因此文档中重写getData()追加锚点的做法本质上是把锚点字段从模型层透出到graphData的标准扩展点——在旧版本或自定义字段较多的场景下你完全可以沿用这个模板在super.getData()结果上追加任何自定义字段。锚点的解析发生在getBeginAnchor/getEndAnchorBaseEdgeModel.ts#L620-L663当指定了sourceAnchorId/targetAnchorId时会按 id 在节点的锚点列表中查找并定位起止点如果找不到则使用节点的默认锚点。注意当节点自定义了getDefaultAnchor(){ return [] }时表示不显示锚点、也不允许连接此时会抛出无法获取beginAnchor/endAnchor错误——这是需要留意的边界行为。在图上通过getGraphData()拿到带锚点信息的边数据后重新render时即可精确还原每条边与锚点的连接关系实现 UML 关联线、端口连线等业务语义。动画基于 SVG 让边动起来LogicFlow 是基于SVG的流程图编辑框架因此可以借助给 SVG 元素添加 CSS 动画的方式为流程图增加动态效果。为了方便使用框架也内置了基础的动画能力。两种开启动画的方式方式一定义边时通过isAnimation开启class CustomEdgeModel extends PolylineEdgeModel { setAttributes() { this.isAnimation true; } getEdgeAnimationStyle() { const style super.getEdgeAnimationStyle(); style.strokeDasharray 5 5; style.animationDuration 10s; return style; } }setAttributes在模型初始化阶段被调用这里把isAnimation置为true该边从渲染开始就带动画。方式二运行时通过 API 动态开启// 开启指定边的默认动画 lf.openEdgeAnimation(edgeId);lf.openEdgeAnimation(edgeId)的实现链路为LogicFlow实例 →graphModel.openEdgeAnimation(edgeId)→ 找到该边模型并调用其openEdgeAnimation()将isAnimation置为true对应源码packages/core/src/LogicFlow.tsx#L1281-L1282packages/core/src/model/GraphModel.ts#L1756-L1758packages/core/src/model/edge/BaseEdgeModel.ts#L687-L694模型层还提供了对称的closeEdgeAnimation()方法用于关闭动画。默认动画样式与主题isAnimation置为true时边的描边会使用getEdgeAnimationStyle()的返回值而该方法的默认实现读取主题中的edgeAnimation配置BaseEdgeModel.ts#L222-L225。内置默认主题的配置如下packages/core/src/constant/theme.ts#L125-L134edgeAnimation: { stroke: #4271DF, // 动画描边颜色 strokeDasharray: 12,4,6,4, // 虚线节奏 strokeDashoffset: 100%, // 偏移起点 animationName: lf_animate_dash, animationDuration: 20s, // 动画周期 animationIterationCount: infinite, animationTimingFunction: linear, animationDirection: normal, }你可以像其他主题一样通过lf.setTheme({ edgeAnimation: {...} })全局覆盖。三种内置边视图LineEdge、PolylineEdge、BezierEdge在渲染时都会读取isAnimation并把动画样式并入 SVG 属性见 LineEdge.tsx、PolylineEdge.tsx、BezierEdge.tsx因此动画能力对内置边是开箱即用的。自定义动画细节流动感与时长控制在getEdgeAnimationStyle()中重写返回值即可精细控制动画getEdgeAnimationStyle() { const style super.getEdgeAnimationStyle(); style.strokeDasharray 5 5; // 虚线长短 style.strokeDashoffset 100%; // 偏移量配合动画形成流动效果 style.animationDuration 10s; // 动画周期越大流动越慢 return style; }strokeDasharray与strokeDashoffset的组合是经典的虚线流动实现虚线沿边路径平移视觉上就像电流或水流动。与 hover 交互结合悬停点亮动画进阶示例 animation/customEdge.ts 展示了把动画与 hover 状态结合的技巧——通过重写setHovered让边在鼠标悬停时播放动画、移出时停止class CustomEdgeModel extends LineEdgeModel { setAttributes() { this.isAnimation true; } setHovered(isHovered: boolean) { super.setHovered(isHovered); this.isAnimation isHovered; } getEdgeAnimationStyle() { const style super.getEdgeAnimationStyle(); style.strokeDasharray 5 5; style.strokeDashoffset 100%; style.animationDuration 10s; return style; } }由于箭头颜色在getArrowStyle()中会跟随isAnimation切换描边色BaseEdgeModel.ts#L238-L251悬停时箭头也会同步点亮为动画色交互反馈非常完整。完整可运行示例动画 自定义箭头 文本定位的组合拳animation/index.tsx 与 animation/data.ts 提供了一个整合示例注册custom-edge边、通过edgeGenerator按源节点类型决定边的类型、用按钮批量开启动画const lf new LogicFlow({ container: this.container, grid: true, ...SilentConfig, // 示例中禁用滚动/拖拽/缩放便于演示 edgeGenerator: (sourceNode) { if (sourceNode.type circle) { return line; } }, }); lf.register(customEdge); lf.setDefaultEdgeType(custom-edge); lf.render(data); // 按钮点击让所有边动起来 const { edges } lf?.getGraphRawData() ?? {}; edges?.forEach(({ id }) { lf?.openEdgeAnimation(id); });该示例中的CustomEdge还同时演示了两个进阶技巧自定义箭头重写getEndArrow()返回一个path图形替换默认三角箭头自定义文本位置在initEdgeData中设置customTextPosition true并重写getTextPosition()根据边首段走向把文本偏移到起点附近——points字符串解析成坐标数组后判断首段的水平/垂直方向再按 ±50 的偏移放置文本。getTextPosition() { const position super.getTextPosition(); const currentPositionList this.points.split( ); // ... 解析 points 为坐标数组判断首段方向后调整 position.x / position.y return position; }这个组合示例基本覆盖了进阶边的全部能力动画、箭头、文本定位、按源节点选边型是值得完整运行一遍的参考实现。进阶边实践小结能力关键 API / 属性作用React 边getEdge()foreignObjectReactDOM.createRoot在边上渲染任意 React 组件可隐藏原生线锚点数据重写getData()透出sourceAnchorId/targetAnchorId保存并还原边连接了哪个锚点默认动画isAnimation truesetAttributes中设置边渲染即播放动画动态动画lf.openEdgeAnimation(edgeId)/closeEdgeAnimation()运行时按需开/关动画动画细节重写getEdgeAnimationStyle()控制虚线、偏移、时长等 SVG 动画参数动画主题lf.setTheme({ edgeAnimation: {...} })全局统一动画外观交互联动重写setHovered()同步isAnimation悬停点亮动画进阶边能力的完整参考实现可以继续阅读边基础篇内置边类型、样式、文本位置与箭头自定义animation 示例源码动画 箭头 文本定位组合实现reactEdge 示例源码React 边完整实现edgeModelAPIisAnimation、getEdgeAnimationStyle、getData等全部可覆盖方法主题APIedgeAnimation主题项完整参数【免费下载链接】LogicFlowA flow chart editing framework focus on business customization. 专注于业务自定义的流程图编辑框架支持实现脑图、ER图、UML、工作流等各种图编辑场景。项目地址: https://gitcode.com/GitHub_Trending/lo/LogicFlow创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考