JSFiddle - React, Tailwind, and code Playground
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,
interactive: {
labelMove: true
},
snapLabels: true,
gridSize: 10,
async: true,
sorting: joint.dia.Paper.sorting.APPROX,
linkPinning: false,
defaultLink: () => new joint.shapes.standard.DoubleLink()
});
// Example
var line = V("rect", {
x: 20,
y:80,
width: 100,
height: 100,
"stroke-width": 3,
fill: "transparent",
stroke: "#142f5a",
name: 'rect1',
})
V(paper.viewport).append(line);
let vcircle;
$('svg').on('mouseover', 'rect', function(evt){
if (vcircle) return;
const vrect = V(evt.currentTarget);
const bbox = evt.target.getBBox();
vcircle = V('circle', { r: 10, fill: 'red', cx: bbox.x, cy: bbox.y }).appendTo(paper.viewport);
$(vcircle.node).on('click', function() {
vrect.remove()
vcircle.remove();
vcircle = null;
});
});