index.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>跨智能流程图功能页面</title>
  7. <style>
  8. #flowchart-container {
  9. width: 100vw;
  10. height: 100vh;
  11. border: 1px solid #ccc;
  12. position: relative;
  13. overflow: hidden;
  14. }
  15. .node {
  16. position: absolute;
  17. width: 120px;
  18. height: 60px;
  19. border: 2px solid #3498db;
  20. background-color: #f0f8ff;
  21. border-radius: 5px;
  22. display: flex;
  23. justify-content: center;
  24. align-items: center;
  25. cursor: move;
  26. user-select: none;
  27. }
  28. .edge {
  29. position: absolute;
  30. height: 2px;
  31. background-color: #7f8c8d;
  32. transform-origin: 0 0;
  33. }
  34. .toolbar {
  35. position: fixed;
  36. top: 10px;
  37. left: 10px;
  38. background-color: #fff;
  39. padding: 10px;
  40. border: 1px solid #ccc;
  41. border-radius: 5px;
  42. z-index: 100;
  43. }
  44. button {
  45. margin: 5px;
  46. padding: 5px 10px;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="toolbar">
  52. <button id="add-node">添加节点</button>
  53. <button id="connect-nodes">连接节点</button>
  54. <button id="save-diagram">保存流程图</button>
  55. </div>
  56. <div id="flowchart-container"></div>
  57. <script>
  58. // 获取DOM元素
  59. const container = document.getElementById('flowchart-container');
  60. const addNodeBtn = document.getElementById('add-node');
  61. const connectNodesBtn = document.getElementById('connect-nodes');
  62. const saveDiagramBtn = document.getElementById('save-diagram');
  63. // 存储节点和边
  64. let nodes = [];
  65. let edges = [];
  66. let isConnecting = false;
  67. let startNode = null;
  68. // 添加节点函数
  69. function addNode(x, y) {
  70. const node = document.createElement('div');
  71. node.className = 'node';
  72. node.textContent = `节点 ${nodes.length + 1}`;
  73. node.style.left = x + 'px';
  74. node.style.top = y + 'px';
  75. container.appendChild(node);
  76. const nodeData = {
  77. element: node,
  78. x: x,
  79. y: y,
  80. id: nodes.length
  81. };
  82. nodes.push(nodeData);
  83. // 添加拖拽功能
  84. let isDragging = false;
  85. let offsetX, offsetY;
  86. node.addEventListener('mousedown', (e) => {
  87. isDragging = true;
  88. offsetX = e.clientX - x;
  89. offsetY = e.clientY - y;
  90. node.style.zIndex = 10;
  91. if (isConnecting && !startNode) {
  92. startNode = nodeData;
  93. }
  94. });
  95. document.addEventListener('mousemove', (e) => {
  96. if (isDragging) {
  97. x = e.clientX - offsetX;
  98. y = e.clientY - offsetY;
  99. node.style.left = x + 'px';
  100. node.style.top = y + 'px';
  101. nodeData.x = x;
  102. nodeData.y = y;
  103. updateEdges();
  104. }
  105. });
  106. document.addEventListener('mouseup', () => {
  107. isDragging = false;
  108. node.style.zIndex = 1;
  109. if (isConnecting && startNode && startNode !== nodeData) {
  110. addEdge(startNode, nodeData);
  111. startNode = null;
  112. isConnecting = false;
  113. }
  114. });
  115. }
  116. // 添加边函数
  117. function addEdge(fromNode, toNode) {
  118. const edge = document.createElement('div');
  119. edge.className = 'edge';
  120. container.appendChild(edge);
  121. const edgeData = {
  122. element: edge,
  123. from: fromNode,
  124. to: toNode
  125. };
  126. edges.push(edgeData);
  127. updateEdgePosition(edgeData);
  128. }
  129. // 更新边的位置
  130. function updateEdgePosition(edgeData) {
  131. const fromX = edgeData.from.x + edgeData.from.element.offsetWidth / 2;
  132. const fromY = edgeData.from.y + edgeData.from.element.offsetHeight / 2;
  133. const toX = edgeData.to.x + edgeData.to.element.offsetWidth / 2;
  134. const toY = edgeData.to.y + edgeData.to.element.offsetHeight / 2;
  135. const dx = toX - fromX;
  136. const dy = toY - fromY;
  137. const length = Math.sqrt(dx * dx + dy * dy);
  138. const angle = Math.atan2(dy, dx) * (180 / Math.PI);
  139. edgeData.element.style.left = fromX + 'px';
  140. edgeData.element.style.top = fromY + 'px';
  141. edgeData.element.style.width = length + 'px';
  142. edgeData.element.style.transform = `rotate(${angle}deg)`;
  143. }
  144. // 更新所有边的位置
  145. function updateEdges() {
  146. edges.forEach(edge => {
  147. updateEdgePosition(edge);
  148. });
  149. }
  150. // 按钮事件监听
  151. addNodeBtn.addEventListener('click', () => {
  152. addNode(100 + nodes.length * 150, 100);
  153. });
  154. connectNodesBtn.addEventListener('click', () => {
  155. isConnecting = true;
  156. });
  157. saveDiagramBtn.addEventListener('click', () => {
  158. // 这里可以实现保存流程图数据的逻辑
  159. console.log('保存流程图数据:', { nodes, edges });
  160. });
  161. // 点击空白处添加节点
  162. container.addEventListener('click', (e) => {
  163. if (e.target === container) {
  164. addNode(e.clientX, e.clientY);
  165. }
  166. });
  167. </script>
  168. </body>
  169. </html>