Pipes + Valves

by Roman Bruckner

HTML

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

JavaScript

var graph = new joint.dia.Graph();
var paper = new joint.dia.Paper({
    el: document.getElementById('paper'),
    width: 800,
    height: 600,
    model: graph,
    async: true
});

var pipe1 = new joint.shapes.standard.Link({
    source: { x: 180, y: 50 },
    target: { x: 180, y: 450 },
    attrs: {
        line: {
            stroke: 'red',
            targetMarker: null
        }
    }
});

var pipe2 = new joint.shapes.standard.Link({
    source: { x: 50, y: 300 },
    target: { x: 450, y: 300 },
    attrs: {
        line: {
            stroke: 'green',
            targetMarker: null
        }
    }
});

graph.resetCells([pipe1, pipe2]);

// Links are not connected
console.log(graph.isNeighbor(pipe1, pipe2));

var valve = new joint.shapes.standard.Link({
    source: { id: pipe1.id, anchor: { name: 'connectionRatio', args: { ratio: 0.5 }}},
    target: { id: pipe2.id, anchor: { name: 'connectionRatio', args: { ratio: 0.5 }}},
    router: { name: 'orthogonal' },
    attrs: {
        line: {
            stroke: 'black',
            targetMarker: null
        },
        valve: {
            atConnectionRatio: 0.3,
            d: 'M -15 -10 15 10 15 -10 -15 10 Z M 0 0 0 -10',
            stroke: 'black',
            fill: 'white',
            strokeWidth: 2
        }
    },
    markup: [{
        tagName: 'path',
        selector: 'wrapper',
        attributes: {
            'fill': 'none',
            'cursor': 'pointer',
            'stroke': 'transparent',
            'stroke-linecap': 'round'
        }
    }, {
        tagName: 'path',
        selector: 'line',
        attributes: {
            'fill': 'none',
            'pointer-events': 'none'
        }
    }, {
        tagName: 'path',
        selector: 'valve'
    }]
});

valve.addTo(graph);

// Links are now connected via a valve
console.log(graph.isNeighbor(pipe1, pipe2));