Skip to Content
教程高级使用白板功能

白板功能

🌐 Whiteboard Features

React Flow 是为构建基于节点的用户界面而设计的,例如工作流编辑器、流程图和图表。即使 React Flow 并非用于创建白板应用,你可能仍然希望集成常见的白板功能。这些示例展示了在需要在节点和边旁边进行注释或绘图时,如何向你的应用添加绘图功能。

🌐 React Flow is designed for building node-based UIs like workflow editors, flowcharts and diagrams. Even if React Flow is not made for creating whiteboard applications, you might want to integrate common whiteboard features. These examples show how to add drawing capabilities to your applications when you need to annotate or sketch alongside your nodes and edges.

示例

🌐 Examples

✏️ 自由手绘(专业版)

🌐 ✏️ Freehand draw (Pro)

在你的 React Flow 面板上绘制平滑曲线。适用于注释或在现有节点周围绘图。

🌐 Draw smooth curves on your React Flow pane. Useful for annotations or sketching around existing nodes.

特点:

  • 鼠标/触摸绘图
  • 可调节画笔大小和颜色
  • 将绘制的路径转换为自定义节点

常见用途:

  • 为流程图添加注释
  • 为图表添加注释
  • 围绕节点勾勒创意

This is a Pro example. Get all pro examples, templates, 1:1 support from the xyflow team and prioritized Github issues with a React Flow Pro subscription.

🎯 套索选择

🌐 🎯 Lasso selection

通过绘制自由形式的选择区域来选择多个元素,并可以选择包括部分被选中的元素。

🌐 Select multiple elements by drawing a freeform selection area with an option to include partially selected elements.

特点:

  • 自由选择形状
  • 元素的部分选择

常见用途:

  • 同时选择节点和注释
  • 混合内容中的复杂选择
import { useCallback, useState } from 'react'; import { ReactFlow, useNodesState, useEdgesState, addEdge, Controls, Background, Panel, } from '@xyflow/react'; import { Lasso } from './Lasso'; import '@xyflow/react/dist/style.css'; const initialNodes = [ { id: '1', position: { x: 0, y: 0 }, data: { label: 'Hello' }, }, { id: '2', position: { x: 300, y: 0 }, data: { label: 'World' }, }, ]; const initialEdges = []; export default function LassoSelectionFlow() { const [nodes, _, onNodesChange] = useNodesState(initialNodes); const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); const onConnect = useCallback((params) => setEdges((els) => addEdge(params, els)), []); const [partial, setPartial] = useState(false); const [isLassoActive, setIsLassoActive] = useState(true); return ( <ReactFlow nodes={nodes} edges={edges} onNodesChange={onNodesChange} onEdgesChange={onEdgesChange} onConnect={onConnect} fitView > <Controls /> <Background /> {isLassoActive && <Lasso partial={partial} />} <Panel position="top-left" className="lasso-controls"> <div className="xy-theme__button-group"> <button className={`xy-theme__button ${isLassoActive ? 'active' : ''}`} onClick={() => setIsLassoActive(true)} > Lasso Mode </button> <button className={`xy-theme__button ${!isLassoActive ? 'active' : ''}`} onClick={() => setIsLassoActive(false)} > Selection Mode </button> </div> <label> <input type="checkbox" checked={partial} onChange={() => setPartial((p) => !p)} className="xy-theme__checkbox" /> Partial selection </label> </Panel> </ReactFlow> ); }

🧹 橡皮

🌐 🧹 Eraser

通过在对象上“擦除”来移除它们。使用碰撞检测来确定要删除的内容。

🌐 Remove items by “erasing” over them. Uses collision detection to determine what to delete.

特点:

  • 基于碰撞的擦除
  • 可视化橡皮擦光标

常见用途:

  • 移除流程的一部分
import { useCallback, useState } from 'react'; import { ReactFlow, useNodesState, useEdgesState, addEdge, Controls, Background, Panel, } from '@xyflow/react'; import { ErasableNode } from './ErasableNode'; import { ErasableEdge } from './ErasableEdge'; import { Eraser } from './Eraser'; import '@xyflow/react/dist/style.css'; const initialNodes = [ { id: '1', type: 'erasable-node', position: { x: 0, y: 0 }, data: { label: 'Hello' }, }, { id: '2', type: 'erasable-node', position: { x: 300, y: 0 }, data: { label: 'World' }, }, ]; const initialEdges = [ { id: '1->2', type: 'erasable-edge', source: '1', target: '2', }, ]; const nodeTypes = { 'erasable-node': ErasableNode, }; const edgeTypes = { 'erasable-edge': ErasableEdge, }; const defaultEdgeOptions = { type: 'erasable-edge', }; export default function EraserFlow() { const [nodes, _, onNodesChange] = useNodesState(initialNodes); const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); const onConnect = useCallback((params) => setEdges((els) => addEdge(params, els)), []); const [isEraserActive, setIsEraserActive] = useState(true); return ( <ReactFlow nodes={nodes} nodeTypes={nodeTypes} edges={edges} edgeTypes={edgeTypes} onNodesChange={onNodesChange} onEdgesChange={onEdgesChange} onConnect={onConnect} fitView defaultEdgeOptions={defaultEdgeOptions} > <Controls /> <Background /> {isEraserActive && <Eraser />} <Panel position="top-left"> <div className="xy-theme__button-group"> <button className={`xy-theme__button ${isEraserActive ? 'active' : ''}`} onClick={() => setIsEraserActive(true)} > Eraser Mode </button> <button className={`xy-theme__button ${!isEraserActive ? 'active' : ''}`} onClick={() => setIsEraserActive(false)} > Selection Mode </button> </div> </Panel> </ReactFlow> ); }

📐 绘制矩形

🌐 📐 Rectangle draw

通过点击和拖动来创建矩形形状。适合高亮区域或为节点组创建背景。

🌐 Create rectangular shapes by clicking and dragging. Good for highlighting areas or creating backgrounds for node groups.

特点:

  • 点击并拖动创建矩形
  • 可自定义颜色

常见用途:

  • 创建背景容器
  • 可视化地分组相关节点
  • 高亮显示部分图表
import { useCallback, useState } from 'react'; import { ReactFlow, useNodesState, useEdgesState, addEdge, Controls, Background, Panel, } from '@xyflow/react'; import { RectangleNode } from './RectangleNode'; import { RectangleTool } from './RectangleTool'; import '@xyflow/react/dist/style.css'; const initialNodes = [ { id: '1', type: 'rectangle', position: { x: 250, y: 5 }, data: { color: '#ff7000' }, width: 150, height: 100, }, ]; const initialEdges = []; const nodeTypes = { rectangle: RectangleNode, }; export default function RectangleFlow() { const [nodes, _, onNodesChange] = useNodesState(initialNodes); const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); const onConnect = useCallback((params) => setEdges((els) => addEdge(params, els)), []); const [isRectangleActive, setIsRectangleActive] = useState(true); return ( <ReactFlow nodes={nodes} nodeTypes={nodeTypes} edges={edges} onNodesChange={onNodesChange} onEdgesChange={onEdgesChange} onConnect={onConnect} fitView > <Controls /> <Background /> {isRectangleActive && <RectangleTool />} <Panel position="top-left"> <div className="xy-theme__button-group"> <button className={`xy-theme__button ${isRectangleActive ? 'active' : ''}`} onClick={() => setIsRectangleActive(true)} > Rectangle Mode </button> <button className={`xy-theme__button ${!isRectangleActive ? 'active' : ''}`} onClick={() => setIsRectangleActive(false)} > Selection Mode </button> </div> </Panel> </ReactFlow> ); }

白板库

🌐 Whiteboard libraries

如果你正在寻找更完整的白板解决方案,可以考虑使用专门为白板应用设计的库,如 tldraw Excalidraw 。这些库提供了用于协作绘图、图形、文本等的完整功能集。

🌐 If you are looking for a more complete whiteboard solution, consider using libraries that are specifically designed for whiteboard applications like tldraw  or Excalidraw . These libraries provide a full set of features for collaborative drawing, shapes, text, and more.

Last updated on