正好最近用到了VueFlow組件,發現箭頭默認樣式太小,無法體現流程展示,因此翻閱相關資料得出下列方法,有什么更好的方法,大家可以推薦推薦,謝謝。
方法1:通過邊(Edge)的樣式屬性
你可以在定義邊(Edge)的時候,通過style
屬性來指定箭頭的樣式,包括大小。例如,如果你想要改變默認箭頭的大小,可以這樣做:
import ReactFlow, { MiniMap, Controls, Background } from 'reactflow'; ? const edges = [{id: 'e1',source: '0',target: '2',type: 'smoothstep', // 使用不同類型的邊以查看效果data: { label: 'Test' },style: { strokeWidth: 2 }, // 控制邊的寬度markerEnd: {type: 'arrowclosed', // 箭頭類型width: 15, // 箭頭寬度height: 15, // 箭頭高度color: 'black' // 箭頭顏色}} ];
方法2:使用自定義節點和邊
如果你想要更靈活地控制箭頭的外觀,可以考慮使用自定義節點和邊。你可以創建一個自定義的邊組件,然后在其中定義箭頭的樣式。例如:
import React from 'react'; import { getBezierPath, getEdgeCenter } from 'react-flow-renderer'; ? const CustomArrow = ({ sourceX, sourceY, targetX, targetY, color }) => {const path = getBezierPath({ sx: sourceX, sy: sourceY, tx: targetX, ty: targetY });const center = getEdgeCenter({ sx: sourceX, sy: sourceY, tx: targetX, ty: targetY });return (<g><path id="edgePath" stroke={color} strokeWidth={2} fill="none" d={path} /><defs><marker id="arrowhead" markerWidth="10" markerHeight="7" refX="8" refY="4" orient="auto"><path d="M 0 0 L 10 4 L 0 8 z" fill={color} /></marker></defs><line x1={center.x} y1={center.y} x2={targetX} y2={targetY} stroke={color} strokeWidth={2} markerEnd="url(#arrowhead)" /></g>); };
然后在你的邊定義中使用這個自定義組件:
const edges = [{id: 'e1',source: '0',target: '2',type: 'custom', // 使用自定義類型sourcePosition: 'right', // 邊的起點位置targetPosition: 'left', // 邊的終點位置data: { label: 'Test' },style: { strokeWidth: 2 }, // 控制邊的寬度customEdgeComponent: ({ sourceX, sourceY, targetX, targetY }) => (<CustomArrow sourceX={sourceX} sourceY={sourceY} targetX={targetX} targetY={targetY} color="black" />)} ];
方法3:使用CSS或內聯樣式調整SVG元素
如果你只是想簡單地調整箭頭的大小,你也可以直接在SVG元素上使用CSS或內聯樣式。例如,你可以在CustomArrow
組件中直接修改markerWidth
和markerHeight
:
<marker id="arrowhead" markerWidth="20" markerHeight="15" refX="15" refY="7.5" orient="auto"><path d="M 0 0 L 20 7.5 L 0 15 z" fill={color} /> </marker>