Custom Selection RestricTranslation
by Roman Bruckner
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.7.7/joint.css" />
</head>
<body>
<!-- content -->
<div id="paper"></div>
<!-- dependencies -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.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.7.7/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,
restrictTranslate: function(elementView, x0, y0) {
// x0 and y0 are pointer coordinates at the start of the translation
const path = new g.Path('M 20 20 L 200 200 L 380 20');
const { x: x1, y: y1, width, height } = elementView.model.getBBox();
// The initial difference between the pointer coordinates and the element position
const dx = x1 - x0;
const dy = y1 - y0;
return function(x, y) {
// Find the point on the path.
const point = path.closestPoint({ x: x - dx, y: y - dy });
// Put the center of the element on this point
point.offset(-width / 2, -height / 2);
return point;
};
}
});
var el1 = new joint.shapes.standard.Circle({
position: {
x: 10,
y: 10
},
size: {
width: 50,
height: 50
},
attrs: {
label: {
text: 'A'
}
}
});
var el2 = new joint.shapes.standard.Rectangle({
position: {
x: 100,
y: 200
},
size: {
width: 50,
height: 50
},
attrs: {
label: {
text: 'B'
}
}
});
var el3 = new joint.shapes.standard.Path({
position: {
x: 300,
y: 100
},
size: {
width: 50,
height: 50
},
attrs: {
body: {
refD: 'M 1 0 2 1 1 2 0 1 z'
},
label: {
text: 'C'
}
}
});
var l1 = new joint.shapes.standard.Link({
source: {
id: el1.id
},
target: {
id: el2.id
},
router: {
name: 'orthogonal'
}
});
var l2 = new joint.shapes.standard.Link({
source: {
id: el2.id
},
target: {
...