Cytoscape.js 使用edgehandles 插件,动态创建节点之间的连线

在 Cytoscape.js 中,使用edgehandles 插件,用于通过拖拽交互创建边(连线)。它允许用户从一个节点拖拽到另一个节点来创建边。以下是 edgehandles 插件的使用方法和详细说明。

  1. 安装 edgehandles 插件
    首先,确保你已经安装了 edgehandles 插件。如果未安装,可以通过以下方式安装:

使用 npm 安装

javascript 复制代码
bash 
npm install cytoscape-edgehandles

通过 CDN 引入

javascript 复制代码
html 
<script src="https://cdn.jsdelivr.net/npm/cytoscape-edgehandles@3.5.3/cytoscape-edgehandles.min.js"></script>

运行 HTML

  1. 注册插件

在使用 edgehandles 插件之前,需要将其注册到 Cytoscape.js 中。

示例代码

javascript 复制代码
javascript 
import cytoscape from 'cytoscape';
import edgehandles from 'cytoscape-edgehandles';

// 注册插件
cytoscape.use(edgehandles);
  1. 初始化 edgehandles
    在 Cytoscape 实例初始化后,可以通过 cy.edgehandles() 方法启用 edgehandles 插件。
javascript 复制代码
示例代码
javascript 
const cy = cytoscape({
  container: document.getElementById('cy'),
  elements: [
    { data: { id: 'a' } },
    { data: { id: 'b' } },
    { data: { id: 'c' } },
  ],
  style: [
    {
      selector: 'node',
      style: {
        'label': 'data(id)',
        'width': 50,
        'height': 50,
        'background-color': '#666',
      },
    },
    {
      selector: 'edge',
      style: {
        'width': 3,
        'line-color': '#999',
      },
    },
  ],
});

// 初始化 edgehandles
const eh = cy.edgehandles({
  snap: false, // 禁用自动吸附
});
  1. edgehandles 的配置选项
    edgehandles 插件支持多种配置选项,以下是一些常用选项:
选项名 类型 默认值 说明
snap boolean true 是否启用自动吸附到节点
snapFrequency number 15 自动吸附的灵敏度(像素)
snapThreshold number 10 自动吸附的阈值(像素)
handleSize number 10 拖拽手柄的大小(像素)
handleColor string #ff0000 拖拽手柄的颜色
handleLineWidth number 1 拖拽手柄的线宽(像素)
handleOutlineColor string #000000 拖拽手柄的轮廓颜色
handleOutlineWidth number 1 拖拽手柄的轮廓线宽(像素)
edgeType string 'flat' 边的类型('flat' 或 'node')
loopAllowed boolean true 自环边的偏移量(像素)
nodeLoopOffset number -50 是否启用自动吸附到节点
edgeParams function null 自定义边参数的函数
complete function null 拖拽完成时的回调函数
stop function null 拖拽停止时的回调函数
  1. 启用和禁用 edgehandles
    你可以通过以下方法动态启用或禁用 edgehandles:

启用

javascript 复制代码
javascript
eh.enable();

禁用

javascript 复制代码
javascript
eh.disable();
  1. 事件回调
    edgehandles 插件提供了多个事件回调函数,可以在不同阶段执行自定义逻辑。

示例代码

javascript 复制代码
javascript
const eh = cy.edgehandles({
  snap: false,
  complete: (sourceNode, targetNode, addedEdge) => {
    console.log('连线完成', sourceNode.id(), targetNode.id(), addedEdge.id());
  },
  stop: () => {
    console.log('连线停止');
  },
});
  1. 完整示例
    以下是一个完整的示例,展示如何使用 edgehandles 插件:
javascript 复制代码
html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cytoscape.js Edgehandles 示例</title>
  <style>
    #cy {
      width: 100%;
      height: 100vh;
      border: 1px solid #ccc;
    }
  </style>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.23.0/cytoscape.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/cytoscape-edgehandles@3.5.3/cytoscape-edgehandles.min.js"></script>
</head>
<body>
  <div id="cy"></div>
  <script>
    // 注册插件
    cytoscape.use(edgehandles);

    const cy = cytoscape({
      container: document.getElementById('cy'),
      elements: [
        { data: { id: 'a' } },
        { data: { id: 'b' } },
        { data: { id: 'c' } },
      ],
      style: [
        {
          selector: 'node',
          style: {
            'label': 'data(id)',
            'width': 50,
            'height': 50,
            'background-color': '#666',
          },
        },
        {
          selector: 'edge',
          style: {
            'width': 3,
            'line-color': '#999',
          },
        },
      ],
    });

    // 初始化 edgehandles
    const eh = cy.edgehandles({
      snap: false, // 禁用自动吸附
      complete: (sourceNode, targetNode, addedEdge) => {
        console.log('连线完成', sourceNode.id(), targetNode.id(), addedEdge.id());
      },
      stop: () => {
        console.log('连线停止');
      },
    });

    // 启用 edgehandles
    eh.enable();
  </script>
</body>
</html>
运行 HTML
  1. 总结
    安装插件:通过 npm 或 CDN 引入 edgehandles 插件。

注册插件:使用 cytoscape.use(edgehandles) 注册插件。

初始化插件:通过 cy.edgehandles() 启用插件。

配置选项:根据需求配置 edgehandles 的行为。

事件回调:使用 complete 和 stop 回调函数处理连线完成和停止事件。

通过以上步骤,你可以在 Cytoscape.js 中使用 edgehandles 插件实现拖拽创建连线的功能。

以上就是文章全部内容了,如果喜欢这篇文章的话,还希望三连支持一下,感谢!

相关推荐
wen's26 分钟前
React Native 0.79.4 中 [RCTView setColor:] 崩溃问题完整解决方案
javascript·react native·react.js
vvilkim1 小时前
Electron 自动更新机制详解:实现无缝应用升级
前端·javascript·electron
vvilkim1 小时前
Electron 应用中的内容安全策略 (CSP) 全面指南
前端·javascript·electron
aha-凯心1 小时前
vben 之 axios 封装
前端·javascript·学习
漫谈网络1 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
失落的多巴胺3 小时前
使用deepseek制作“喝什么奶茶”随机抽签小网页
javascript·css·css3·html5
DataGear3 小时前
如何在DataGear 5.4.1 中快速制作SQL服务端分页的数据表格看板
javascript·数据库·sql·信息可视化·数据分析·echarts·数据可视化
影子信息3 小时前
vue 前端动态导入文件 import.meta.glob
前端·javascript·vue.js
样子20183 小时前
Vue3 之dialog弹框简单制作
前端·javascript·vue.js·前端框架·ecmascript
kevin_水滴石穿3 小时前
Vue 中报错 TypeError: crypto$2.getRandomValues is not a function
前端·javascript·vue.js