实现通过JsPlumb 拓扑图
时间: 2023-08-29 14:13:17 浏览: 154
jsplumb 9种拓扑图示例
JsPlumb 是一个流程图和拓扑图的绘制库,可以用它来创建各种形状和连接线。下面是一个简单的使用 JsPlumb 创建拓扑图的例子:
首先,在 HTML 中添加必要的引用:
```html
<!DOCTYPE html>
<html>
<head>
<title>JsPlumb Topology Example</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsPlumb/2.15.5/js/jsplumb.min.js"></script>
</head>
<body>
<div id="canvas"></div>
</body>
</html>
```
然后,在 JavaScript 中创建拓扑图:
```javascript
// 创建一个 JsPlumb 实例
var jsPlumbInstance = jsPlumb.getInstance();
// 设置连接线的默认样式
var connectorPaintStyle = {
strokeWidth: 2,
stroke: "#61B7CF",
joinstyle: "round",
outlineStroke: "white",
outlineWidth: 2
};
// 设置连接线的箭头样式
var connectorHoverStyle = {
strokeWidth: 3,
stroke: "#216477",
outlineWidth: 5,
outlineStroke: "white"
};
// 设置端点的默认样式
var endpointStyle = {
endpoint: "Dot",
paintStyle: {
stroke: "#7AB02C",
fill: "transparent",
radius: 7,
strokeWidth: 1
},
hoverPaintStyle: {
fill: "#216477",
stroke: "#216477"
},
connectorStyle: connectorPaintStyle,
connectorHoverStyle: connectorHoverStyle,
connectorOverlays: [
[
"Arrow",
{
location: 1,
id: "arrow",
length: 14,
foldback: 0.8
}
],
[
"Label",
{
label: "",
id: "label",
cssClass: "aLabel"
}
]
],
dragOptions: {},
overlays: [
[
"PlainArrow",
{
location: 0.5,
width: 12,
length: 12
}
],
[
"Label",
{
label: "foo",
id: "label",
cssClass: "aLabel"
}
]
]
};
// 在画布上添加两个端点和一个连接线
var sourceEndpoint = {
endpoint: "Dot",
paintStyle: {
stroke: "#7AB02C",
fill: "transparent",
radius: 7,
strokeWidth: 1
},
isSource: true,
connector: [
"Bezier",
{
curviness: 63
}
],
connectorStyle: connectorPaintStyle,
hoverPaintStyle: {
fill: "#216477",
stroke: "#216477"
},
connectorHoverStyle: connectorHoverStyle,
dragOptions: {},
overlays: [
[
"Label",
{
location: [0.5, -0.5],
label: "Output",
cssClass: "endpointLabel"
}
]
]
};
var targetEndpoint = {
endpoint: "Dot",
paintStyle: {
fill: "#7AB02C",
radius: 7
},
hoverPaintStyle: {
fill: "#216477"
},
maxConnections: -1,
dropOptions: {
hoverClass: "hover",
activeClass: "active"
},
isTarget: true,
overlays: [
[
"Label",
{
location: [0.5, 1.5],
label: "Input",
cssClass: "endpointLabel"
}
]
]
};
jsPlumbInstance.addEndpoint($("#canvas"), {
uuid: "output"
}, sourceEndpoint);
jsPlumbInstance.addEndpoint($("#canvas"), {
uuid: "input"
}, targetEndpoint);
jsPlumbInstance.connect({
uuids: ["output", "input"]
});
```
上面的代码创建了一个画布,并在画布上添加了两个端点和一个连接线。该例子中使用了默认的样式,你可以根据需要自定义样式。
最后,你可以通过 jsPlumbInstance.reset() 方法来清除画布上的所有元素。
希望这个例子对你有所帮助。
阅读全文