Partial Link Connector

by Roman Bruckner

HTML

<html>

  <head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.7.1/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.1/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,
  cellViewNamespace: joint.shapes,
  gridSize: 10,
  async: true,
  sorting: joint.dia.Paper.sorting.APPROX,
  defaultLink: (elementView, magnet) => {
    return new joint.shapes.standard.Link({
      attrs: {
        line: {
          stroke: 'red'
        }
      }
    });
  },
  defaultRouter: {
    name: 'rightAngle'
  },
  defaultConnectionPoint: {
    name: 'boundary'
  },
  defaultConnector: {
    name: 'partial',
    args: {
      length: 120
    }
  },
  connectorNamespace: {
    ...joint.connectors,
    partial: function(sourcePoint, targetPoint, router, opt) {
      const {
        length = 100
      } = opt;
      const path = joint.connectors.normal(sourcePoint, targetPoint, router, opt);
      const paths = path.divideAtLength(length);
      if (paths === null) return path;
      return paths[0];
    }
  }
});

paper.on({
  'link:mouseenter': (linkView) => linkView.model.set('connector', { name: 'normal' }),
  'link:mouseleave': (linkView) => linkView.model.unset('connector')
});


// Example

var el1 = new joint.shapes.standard.Rectangle({
  position: {
    x: 10,
    y: 170
  },
  size: {
    width: 100,
    height: 90
  },
});

var el2 = el1.clone().position(300, 100);
var el3 = el1.clone().position(300, 400);


const l1 = new joint.shapes.standard.Link({
  source: {
    id: el2.id,
    anchor: {
      name: 'bottom'
    }
  },
  target: {
    id: el3.id,
    anchor: {
      name: 'right'
    }
  }
});

const l2 = new joint.shapes.standard.Link({
  source: {
    id: el1.id,
    anchor: {
      name: 'right'
    }
  },
  target: {
    id: el3.id,
    anchor: {
      name: 'left'
    }
  }
});

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

paper.unfreeze();