vue实现流程图拖拽连线编排示例代码

代码语言:html

所属分类:拖放

代码描述:vue实现流程图拖拽连线编排示例代码,没有使用插件,vue原生实现,模仿了ai智能体的工作流编排。

代码标签: vue 流程图 拖拽 连线 编排 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue Drag and Drop Flowchart</title>
<style>
  body {
    font-family: Arial, sans-serif;
  }
  .container {
    position: relative;
    width: 100vw;
    height: 100vh;
    background-color: #f0f0f0;
    overflow: hidden;
  }
  .node {
    position: absolute;
    width: 100px;
    height: 100px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 2px 2px 3px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
  }
  .connector {
    width: 10px;
    height: 10px;
    background-color: red;
    position: absolute;
    cursor: pointer;
  }
  .connector-left {
    left: -5px;
    top: 50%;
    transform: translateY(-50%);
  }
  .connector-right {
    right: -5px;
    top: 50%;
    transform: translateY(-50%);
  }
  svg {
    position: absolute;
    top: 0;
    left: 0;
    overflow: visible;
  }
  path {
    stroke: black;
    stroke-width: 2;
    fill: none;
  }
</style>
</head>
<body>
<div id="app">
  <div class="container" ref="container" @mousedown="onCanvasMouseDown($event)">
    <div v-for="node in nodes" :key="node.id" 
         :style="{ left: node.x + 'px', top: node.y + 'px' }" 
         class="node" 
         @mousedown.stop="onNodeMouseDown(node, $event)"
         :data-node-id="node.id">
      {{ node.name }}
      <div class="connector connector-left"></div>
      <div class="connector connector-right" @mousedown.stop="startConnecting(node, 'right', $event)"></div>
    </div>
    <svg v-for="line in lines" :key="line.id">
      <path :d="generatePath(line)"></path>
    </svg>
    <svg v-if="connecting">
      <path :d="generateTempPath()"></path>
    </svg>
  </div>
</div>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue@2.6.1-dev.js"></script>
<script>
  new Vue({
    el: '#app',
    data: {
      nodes: [
        { id: 1, name: 'Node 1', x: 100, y: 100 },
        { id: 2, name: 'Node 2', x: 300, y: 300 },
          { id: 3, name: 'Node 3', x: 300, y: 30 },
      ],
      lines: [],
      draggedNode: null,
      startX: 0,
      startY: 0,
      isDragging: false,
      connecting: false,
      tempLine: { startX: 0, startY: 0, endX: 0, endY: 0 },
      tempNode: null,
      tempSide: '',
    },
    methods: {
      onNodeMouseDown(node, event) {
        this.draggedNode = node;
        this.startX = event.clientX - node.x;
        this.startY = event.clientY - node.y;
        this.isDragging = true;
        window.addEventListener('mousemove', this.onMouseMove);
        window.addEventListener('mouseup', this.onMouseUp);
      },
      onMouseMove(event) {
        const containerRect = this.$refs.container.getBoundingClientRect();
        if (this.isDragging && this.draggedNode) {
          const newX = event.clientX - this.startX;
          const newY = event.clientY - this.st.........完整代码请登录后点击上方下载按钮下载查看

网友评论0