Link Markers

Various link markers

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.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.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,
  async: true,
  sorting: joint.dia.Paper.sorting.APPROX,
});

const markers = [
  // No Marker
  null,
  // One
  {
    type: 'path',
    d: 'M 20 -10 20 10',
    'stroke-width': 2,
    'fill': 'none',
  },
  // Many
  {
    type: 'path',
    d: 'M 0 -10 20 0 0 10',
    'stroke-width': 2,
    'fill': 'none',
  },
  // One (and only one)
  {
    type: 'path',
    d: 'M 20 -10 20 10 M 10 -10 10 10',
    'stroke-width': 2,
    'fill': 'none',
    stroke: 'blue'
  },
  // Zero or one
  {
    markup: joint.util.svg /* xml */ `
    	<circle cx="0" r="10" stroke="#333" stroke-width="2" fill="#fff"/>
      <path d="M 20 -10 20 10" stroke="#333" stroke-width="2" fill="none"/>
    `,
  },
  // One or many
    {
    markup: joint.util.svg /* xml */ `
      <path d="M 0 -10 0 10 M 20 -10 0 0 20 10" stroke="#333" stroke-width="2" fill="none"/>
    `,
  },
];


markers.forEach((marker, i) => {
  const y = 50 + i * 50;
  const link = new joint.shapes.standard.Link({
    source: {
      x: 50,
      y
    },
    target: {
      x: 200,
      y
    },
    attrs: {
      line: {
        targetMarker: null,
        stroke: 'red',
        strokeWidth: 2,
        strokeLinecap: 'square'
      }
    }
  });
  link.attr('line/targetMarker', marker, {
    rewrite: true
  });
    link.attr('line/sourceMarker', marker, {
    rewrite: true
  });
  graph.addCell(link);
});