JointJS: Ports vs Anchors

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

const 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,
  gridSize: 10,
  async: true,
  sorting: joint.dia.Paper.sorting.APPROX,
  linkPinning: false,
  defaultLink: () => new joint.shapes.standard.Link()
});

// Example

const color = '#48b8cc';

const el1 = new joint.shapes.standard.BorderedImage({
  position: {
    x: 10,
    y: 170
  },
  size: {
    width: 100,
    height: 90
  },
  attrs: {
    image: {
      href: 'https://picsum.photos/id/1/100/90'
    },
    label: {
      text: 'A'
    }
  },
  ports: {
    groups: {
       out: {
         position: 'right',
         markup: joint.util.svg`<circle @selector="portBody" r="5" stroke="white" fill="${color}"/>`
       }    
    },
    items: [{
    id: 'out1',
      group: 'out'
    }]
  }
     
  
});

const el2 = new joint.shapes.standard.BorderedImage({
  position: {
    x: 450,
    y: 150
  },
  size: {
    width: 100,
    height: 90
  },
  attrs: {
    root: {
      magnetSelector: 'border'
    },
    image: {
      href: 'https://picsum.photos/id/2/100/90'
    },
    label: {
      text: 'B'
    }
  }
});

const textMargin = 10;

const l1 = new joint.shapes.standard.Link({
  source: {
    id: el1.id,
    port: 'out1'
  },
  target: {
    id: el2.id,
    anchor: {
      name: 'left',
      args: {
        rotate: true
      }      
    },
    connectionPoint: {
      name: 'anchor'
    }
  },
  attrs: {
    line: {
    stroke: color
    }
  }
});


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

paper.unfreeze();