Drop Element On Link

by Roman Bruckner

HTML

<html>
<head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.3.0/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.3.0/joint.js"></script>
    
    </body>
</html>

CSS

[joint-selector="wrapper"] {
   stroke-width: 30;
}

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
});

var el1 = new joint.shapes.standard.Circle({
    position: {
        x: 100,
        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: {
        id: el3.id
    },
    connector: {
        name: 'smooth'
    }
});


graph.addCells([el1, el2, el3, l1, l2]);

el1.clone().attr('label/text', 'Drop\nMe').position(10,10).addTo(graph);

paper.on({
    'element:pointermove': onPaperElementPointermove,
    'element:pointerup': onPaperElementPointerup,
});


function onPaperElementPointermove(elementView, evt) {
    const { data } = evt;
    validateInsert(elementView, evt);
    // Run the code below on the first `pointermove` event only
    if (data.pointermoveCalled) return;
    elementView.vel.attr('pointer-events', 'none');
    elementView.vel.attr('opacity', 0.3);
    data.pointermoveCalled = true;
 ...