vue原生实现拖拽可视化流程图连线代码

代码语言:html

所属分类:拖放

代码描述:vue原生实现拖拽可视化流程图连线代码,可导出json格式,并且可加载。

代码标签: 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;
     -webkit-user-select: none; /* Safari */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none; /* IE/Edge */
    user-select: none; /* 标准语法 */

  }
  .container {
    position: relative;
    width: 200vw;
    height: 80vh;
    background-color: #f0f0f0;
    overflow: hidden;
  }
  .parat{
      font-size: 10px;
      padding: 6px;
  }
  .node {
    position: absolute;
    width: 200px;
    height: auto;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 2px 2px 3px rgba(0,0,0,0.1);
   
   
  
    
  }
  .node .title{
      padding: 10px;
      background: #2596cc;
      cursor: grab;
      text-align: center;
      color: white;
  }
  .connector {
    width: 20px;
    height: 20px;
    background-color: red;
    position: absolute;
    cursor: pointer;
  }
  .connector-left {
    left: -5px;
    top: 20px;
    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;
  }
  .tool-bar {
    height: 20vh;
    padding: 10px;
    background-color: #EEE;
  }
</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" 
       >
    <div class="title"   @mousedown.stop="onNodeMouseDown(node, $event)"
         :data-node-id="node.id">  {{ node.name }}</div>
         
         <div class="cont"> 
         <div class="parat">入参</div>
           <ul >
               <li v-for="para in node.inpara">{{para}}</li>
           </ul>
            <div class="parat">出参</div>
         <ul >
               <li v-for="para in node.outpara">{{para}}</li>
           </ul>
         </div>
      <div class="connector connector-left" v-if="node.type!='start'"></div>
      <div class="connector connector-right"  v-if="node.type!='end'" @mousedown.stop="startConnecting(node, 'right', $event)"></div>
    </div>
    <svg v-for="line in lines" :key="line.id">
      <path :d="generatePath(line)" @contextmenu.prevent="deleteLine(line.id)"></path>
    </svg>
    <svg v-if="connecting">
      <path :d="generateTempPath()"></path>
    </svg>
  </div>
  
  <div class="tool-bar">
    <button @click="addNode">添加节点</button>
    <button @click="exportToJson">导出JSON</button>
    <input type="file" @change="importFromJson" accept=".json">
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script>
  new Vue({
    el: '#app',
    data: {
      nodes: [
        { id: 1, name: '开始', x: 30, y: 100,type:"start" ,inpara:[],outpara:["t","z"]},
          { id: 3, name: 'ifelse', x: 280, y: 70,type:"http",inpara:["x","y"],outpara:["t","z"] },
          { id: 5, name: 'http', x: 280, y: 200,type:"ifelse",inpara:["x","y"],outpara:["t","z"]},
        { id: 6, name: '结束', x: 500, y: 100,type:"end",inpara:["y"],outpara:[] },
      ],
      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);
      },
       deleteLine(lineId) {
          this.lines = this.lines.filter(line => line.id !== lineId);
        },
      onMouseMove(ev.........完整代码请登录后点击上方下载按钮下载查看

网友评论0