要使用Flowchart.js實現流程圖的縮放和平移,可以按照以下步驟進行:
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowchart/1.16.0/flowchart.min.js"></script>
<div>
元素,并設置一個唯一的ID以便后續操作。<div id="flowchart-container"></div>
<div>
元素中。var diagram = {
nodes: [
{ id: 'start', label: 'Start', shape: 'circle' },
{ id: 'end', label: 'End', shape: 'circle' },
// Add more nodes here
],
edges: [
{ source: 'start', target: 'end', label: 'Example Edge' },
// Add more edges here
]
};
var options = {
x: 0,
y: 0,
scale: 1,
flowchart: {
useMaxWidth: true,
htmlLabels: true
}
};
var container = document.getElementById('flowchart-container');
flowchart.parse(diagram);
flowchart.drawSVG(container, options);
var scale = 1;
var x = 0;
var y = 0;
container.addEventListener('wheel', function(e) {
e.preventDefault();
if (e.deltaY < 0) {
scale += 0.1;
} else {
scale -= 0.1;
}
options.scale = scale;
options.x = x;
options.y = y;
flowchart.drawSVG(container, options);
});
var isDragging = false;
var startX, startY;
container.addEventListener('mousedown', function(e) {
isDragging = true;
startX = e.clientX - x;
startY = e.clientY - y;
});
container.addEventListener('mousemove', function(e) {
if (isDragging) {
x = e.clientX - startX;
y = e.clientY - startY;
options.scale = scale;
options.x = x;
options.y = y;
flowchart.drawSVG(container, options);
}
});
container.addEventListener('mouseup', function() {
isDragging = false;
});
通過以上步驟,您可以使用Flowchart.js創建一個流程圖,并實現縮放和平移功能。您也可以根據自己的需求對代碼進行進一步的優化和定制。