transformModel.vue
4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!--
* @Date: 2025-03-10 16:52:35
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-03-19 00:57:55
* @FilePath: /logic-flow2/src/views/api/transformModel.vue
* @Description: 拖拽面板
-->
<template>
<div class="container">
<div ref="container" class="flow-container"></div>
<div class="control-panel">
<button @click="zoomIn">放大</button>
<button @click="zoomOut">缩小</button>
<button @click="moveLeft">左移</button>
<button @click="moveRight">右移</button>
<button @click="centerView">居中</button>
</div>
</div>
</template>
<script setup>
import LogicFlow from "@logicflow/core";
const container = ref(null);
let lf = null;
// 缩放画布
const zoomIn = () => {
const { transformModel } = lf.graphModel;
const currentZoom = transformModel.ZOOM;
transformModel.zoom(currentZoom + 0.1);
};
const zoomOut = () => {
const { transformModel } = lf.graphModel;
const currentZoom = transformModel.ZOOM;
transformModel.zoom(currentZoom - 0.1);
};
// 平移画布
const moveLeft = () => {
const { transformModel } = lf.graphModel;
const [x, y] = transformModel.getTranslate();
transformModel.translate(x - 50, y);
};
const moveRight = () => {
const { transformModel } = lf.graphModel;
const [x, y] = transformModel.getTranslate();
transformModel.translate(x + 50, y);
};
// 居中显示
const centerView = () => {
const { transformModel } = lf;
// 直接从 lf 实例获取节点
const nodes = lf.graphModel.nodes;
if (nodes.length === 0) return;
// 计算所有节点的边界框
const bounds = nodes.reduce((acc, node) => {
const { x, y } = node;
acc.minX = Math.min(acc.minX, x);
acc.maxX = Math.max(acc.maxX, x);
acc.minY = Math.min(acc.minY, y);
acc.maxY = Math.max(acc.maxY, y);
return acc;
}, { minX: Infinity, maxX: -Infinity, minY: Infinity, maxY: -Infinity });
// 计算中心点和范围
const centerX = (bounds.minX + bounds.maxX) / 2;
const centerY = (bounds.minY + bounds.maxY) / 2;
const width = bounds.maxX - bounds.minX + 200; // 添加边距
const height = bounds.maxY - bounds.minY + 100;
// 居中显示
transformModel.focusOn(centerX, centerY, width, height);
};
onMounted(() => {
lf = new LogicFlow({
container: container.value,
grid: true,
});
// 监听点击画布功能
lf.on("canvas:click", (e) => {
console.log("Canvas clicked at:", e.x, e.y);
});
lf.on('node:click', (e) => {
console.log(e);
// 示例:HTML坐标转换为画布坐标
console.warn(e);
const htmlPoint = { x: 100, y: 100 };
const { transformModel } = lf.graphModel;
const canvasPoint = transformModel.HtmlPointToCanvasPoint(htmlPoint);
console.log("Canvas coordinates:", canvasPoint);
});
lf.render({
nodes: [
{ id: "node1", type: "rect", x: 200, y: 100 },
{ id: "node2", type: "circle", x: 400, y: 100 },
],
edges: [{ id: "edge1", sourceNodeId: "node1", targetNodeId: "node2" }],
});
// 初始化时居中显示
setTimeout(() => {
const { transformModel } = lf;
// 这里也需要修改
const nodes = lf.graphModel.nodes;
if (nodes.length === 0) return;
const bounds = nodes.reduce((acc, node) => {
const { x, y } = node;
acc.minX = Math.min(acc.minX, x);
acc.maxX = Math.max(acc.maxX, x);
acc.minY = Math.min(acc.minY, y);
acc.maxY = Math.max(acc.maxY, y);
return acc;
}, { minX: Infinity, maxX: -Infinity, minY: Infinity, maxY: -Infinity });
const centerX = (bounds.minX + bounds.maxX) / 2;
const centerY = (bounds.minY + bounds.maxY) / 2;
const width = bounds.maxX - bounds.minX + 200;
const height = bounds.maxY - bounds.minY + 100;
// transformModel.focusOn(centerX, centerY, width, height);
}, 0);
});
</script>
<style scoped>
.container {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
}
.flow-container {
flex: 1;
width: 100%;
height: 100%;
}
.control-panel {
position: fixed;
top: 20px;
left: 20px;
display: flex;
gap: 10px;
}
button {
padding: 8px 16px;
border: none;
border-radius: 4px;
background-color: #4a90e2;
color: white;
cursor: pointer;
}
button:hover {
background-color: #357abd;
}
</style>