GitHub Discussion #1854
https://github.com/clientIO/joint/discussions/1854
by Roman Bruckner
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.6.2/joint.css" />
</head>
<body>
<!-- content -->
<div id="paper"></div>
<!-- dependencies -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.0/backbone.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.6.2/joint.js"></script>
</body>
</html>
JavaScript
var graph = new joint.dia.Graph({}, {
cellNamespace: joint.shapes
});
const paper = new joint.dia.Paper({
el: document.getElementById('paper'),
width: 800,
height: 900,
model: graph,
defaultConnectionPoint: {
name: 'boundary'
},
cellViewNamespace: joint.shapes,
gridSize: 10,
async: true,
sorting: joint.dia.Paper.sorting.APPROX,
linkPinning: false,
defaultLink: () => new joint.shapes.standard.DoubleLink(),
defaultRouter: (vertices, opt, linkView) => {
/* Alternative */
// const sv = linkView.getEndView('source');
// const sourceBBox = sv ? sv.model.getBBox() : new g.Rect(linkView.model.source());
// const tv = linkView.getEndView('target');
// const targetBBox = tv ? tv.model.getBBox() : new g.Rect(linkView.model.target());
const sv = linkView.getEndView('source');
const tv = linkView.getEndView('target');
if (!sv || !tv) {
// the link is not connected either to source or target
// we draw a straigth line
return [];
}
const sourceBBox = sv.model.getBBox();
const targetBBox = tv.model.getBBox();
const sa = linkView.getEndAnchor('source');
const ta = linkView.getEndAnchor('target');
const length = 40;
const p1 = {
x: sourceBBox.x + sourceBBox.width / 2,
y: sourceBBox.y + sourceBBox.height + length
};
const p4 = {
x: targetBBox.x + targetBBox.width / 2,
y: targetBBox.y - length
}
const p2 = {
x: (p1.x + p4.x) / 2,
y: p1.y
};
const p3 = {
x: p2.x,
y: p4.y
};
return [p1, p2, ...vertices, p3, p4];
}
});
// Example
var el1 = new joint.shapes.standard.Rectangle({
position: {
x: 10,
y: 170
},
size: {
width: 100,
height: 90
},
attrs: {
label: {
text: 'A'
}
}
});
var el2 = new joint.shapes.standard.Rectangle({
position: {
x: 400,
y: 150
},
size: {
width: 100,
height: 90
},
attrs: {
label: {
...