Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="paper"></div>
<a target="_blank" href="https://www.jointjs.com">
  <img id="logo" src="https://assets.codepen.io/7589991/jointjs-logo.svg" width="200" height="50"></img>
</a>
              
            
!

CSS

              
                #logo {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background-color: #ffffff;
  border: 1px solid #d3d3d3;
  padding: 5px;
  box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.3);
}

#paper {
  margin: 0 auto;
}

.joint-tool[data-tool-name="button"] circle {
  fill: #333;
}

.joint-tool[data-tool-name="connect"]:not(:hover) {
  opacity: 0;
}

.active [joint-selector="line"] {
  stroke: #4666e5;
}

              
            
!

JS

              
                const { dia, shapes, linkTools, connectors } = joint;
const { Polygon, Ellipse, Rect, toDeg } = g;
const { TangentDirections } = connectors.curve;

// Theme

const highlighterAttributes = {
  stroke: "#4666E5",
  "stroke-width": 2,
  "stroke-linecap": "butt",
  "stroke-linejoin": "miter"
};

const connectToolAttributes = {
  fill: "none",
  "stroke-width": 10,
  "stroke-opacity": 0.4,
  stroke: "#4666E5",
  cursor: "cell"
};

const lineAttributes = {
  stroke: "#333333",
  strokeWidth: 2
};

const labelAttributes = {
  textVerticalAnchor: "middle",
  textAnchor: "middle",
  x: "calc(.5*w)",
  y: "calc(.5*h)",
  fill: "#333333",
  fontSize: 18,
  fontWeight: "bold",
  fontFamily: "sans-serif",
  fontVariant: "small-caps",
  pointerEvents: "none"
};

const bodyAttributes = {
  fill: "#FCFCFC",
  stroke: "#333333",
  strokeWidth: 2,
  cursor: "grab"
};

const ShapeTypes = {
  BASE: "Base",
  RHOMBUS: "Rhombus",
  RECTANGLE: "Rectangle",
  ELLIPSE: "Ellipse",
  LINK: "Link"
};

// Setup

const graph = new dia.Graph({}, { cellNamespace: shapes });
const paper = new dia.Paper({
  el: document.getElementById("paper"),
  model: graph,
  cellViewNamespace: shapes,
  width: 780,
  height: 490,
  gridSize: 1,
  async: true,
  sorting: dia.Paper.sorting.APPROX,
  background: { color: "#F3F7F6" },
  snapLinks: true,
  highlighting: {
    default: {
      name: "mask",
      options: {
        attrs: {
          ...highlighterAttributes
        }
      }
    }
  },
  // This demo does not use the connection points
  defaultConnectionPoint: { name: "anchor" },
  connectionStrategy: function (end, view, _, coords) {
    const { x, y } = view.model.getBoundaryPoint(coords);
    end.anchor = {
      name: "topLeft",
      args: {
        dx: x,
        dy: y,
        rotate: true
      }
    };
  },
  defaultConnector: function (sourcePoint, targetPoint, route, _, linkView) {
    const { model: link } = linkView;
    const targetElement = link.getTargetElement();
    const sourceElement = link.getSourceElement();
    const options = {
      targetDirection: targetElement
        ? targetElement.getCurveDirection(targetPoint)
        : TangentDirections.AUTO,
      sourceDirection: sourceElement
        ? sourceElement.getCurveDirection(sourcePoint)
        : TangentDirections.AUTO
    };
    return connectors.curve(sourcePoint, targetPoint, route, options, linkView);
  },
  defaultLink: () => new Link()
});

paper.svg.style.overflow = "visible";
paper.el.style.border = "1px solid #E5E5E5";
paper.on({
  "cell:mouseenter": (view) => {
    addTools(view);
  },
  "cell:mouseleave": (view) => {
    removeTools(view);
  }
});

graph.on({
  add: (cell) => {
    addTools(cell.findView(paper));
  }
});

const BaseShape = dia.Element.define(
  ShapeTypes.BASE,
  {
    z: 1
  },
  {
    getConnectToolMarkup() {
      return [
        {
          tagName: "rect",
          attributes: {
            ...this.size(),
            ...connectToolAttributes
          }
        }
      ];
    },

    getCurveDirection() {
      return TangentDirections.AUTO;
    },

    getBoundaryPoint(point, snapRadius = 20) {
      const bbox = this.getBBox();
      const angle = this.angle();
      // Relative to the element's position
      const relPoint = point
        .clone()
        .rotate(bbox.center(), angle)
        .difference(bbox.topLeft());
      const relBBox = new Rect(0, 0, bbox.width, bbox.height);
      if (!relBBox.containsPoint(relPoint)) {
        const relCenter = relBBox.center();
        const relTop = relBBox.topMiddle();
        const relLeft = relBBox.leftMiddle();
        if (Math.abs(relTop.x - relPoint.x) < snapRadius) {
          return relCenter.y > relPoint.y ? relTop : relBBox.bottomMiddle();
        }
        if (Math.abs(relLeft.y - relPoint.y) < snapRadius) {
          return relCenter.x > relPoint.x ? relLeft : relBBox.rightMiddle();
        }
      }
      return this.getClosestBoundaryPoint(relBBox, relPoint);
    },

    getClosestBoundaryPoint(bbox, point) {
      return bbox.pointNearestToPoint(point);
    },

    getTools() {
      return [
        new linkTools.Connect({
          focusOpacity: 0,
          markup: this.getConnectToolMarkup()
        })
      ];
    }
  }
);

const Link = shapes.standard.Link.define(
  ShapeTypes.LINK,
  {
    attrs: {
      line: {
        ...lineAttributes
      }
    },
    z: 2
  },
  {
    getTools() {
      return [
        new linkTools.Vertices(),
        new linkTools.Remove(),
        new linkTools.SourceArrowhead(),
        new linkTools.TargetArrowhead()
      ];
    }
  }
);

const RhombusShape = BaseShape.define(
  ShapeTypes.RHOMBUS,
  {
    size: { width: 140, height: 70 },
    attrs: {
      root: {
        highlighterSelector: "body"
      },
      body: {
        d:
          "M calc(.5*w) 0 calc(w) calc(.5*h) calc(.5*w) calc(h) 0 calc(.5*h) Z",
        ...bodyAttributes
      },
      label: {
        text: "Rhombus",
        ...labelAttributes
      }
    }
  },
  {
    markup: [
      {
        tagName: "path",
        selector: "body"
      },
      {
        tagName: "text",
        selector: "label"
      }
    ],

    getConnectToolMarkup() {
      const { width, height } = this.size();
      return [
        {
          tagName: "path",
          attributes: {
            d: `M ${width / 2} 0 ${width} ${height / 2} ${
              width / 2
            } ${height} 0 ${height / 2} Z`,
            ...connectToolAttributes
          }
        }
      ];
    },

    getCurveDirection(point) {
      const bbox = this.getBBox();
      const angle = bbox.center().angleBetween(point, bbox.topMiddle());
      if (angle % 90 === 0) {
        return "auto";
      }
      let ratio = bbox.height / bbox.width;
      if (angle % 180 < 90) {
        ratio = 1 / ratio;
      }
      return 360 - Math.floor(angle / 90) * 90 + toDeg(Math.atan(ratio));
    },

    getClosestBoundaryPoint(bbox, point) {
      const rhombus = new Polygon([
        bbox.topMiddle(),
        bbox.rightMiddle(),
        bbox.bottomMiddle(),
        bbox.leftMiddle()
      ]);
      return rhombus.closestPoint(point);
    }
  }
);

const RectangleShape = BaseShape.define(
  ShapeTypes.RECTANGLE,
  {
    size: { width: 140, height: 70 },
    attrs: {
      root: {
        highlighterSelector: "body"
      },
      body: {
        width: "calc(w)",
        height: "calc(h)",
        ...bodyAttributes
      },
      label: {
        text: "Rectangle",
        ...labelAttributes
      }
    }
  },
  {
    markup: [
      {
        tagName: "rect",
        selector: "body"
      },
      {
        tagName: "text",
        selector: "label"
      }
    ]
  }
);

const EllipseShape = BaseShape.define(
  ShapeTypes.ELLIPSE,
  {
    size: { width: 140, height: 70 },
    attrs: {
      root: {
        highlighterSelector: "body"
      },
      body: {
        cx: "calc(.5*w)",
        cy: "calc(.5*h)",
        rx: "calc(.5*w)",
        ry: "calc(.5*h)",
        ...bodyAttributes
      },
      label: {
        text: "Ellipse",
        ...labelAttributes
      }
    }
  },
  {
    markup: [
      {
        tagName: "ellipse",
        selector: "body"
      },
      {
        tagName: "text",
        selector: "label"
      }
    ],

    getConnectToolMarkup() {
      const { width, height } = this.size();
      return [
        {
          tagName: "ellipse",
          attributes: {
            rx: width / 2,
            ry: height / 2,
            cx: width / 2,
            cy: height / 2,
            ...connectToolAttributes
          }
        }
      ];
    },

    getCurveDirection() {
      return TangentDirections.OUTWARDS;
    },

    getClosestBoundaryPoint(bbox, point) {
      const ellipse = Ellipse.fromRect(bbox);
      return ellipse.intersectionWithLineFromCenterToPoint(point);
    }
  }
);

const rhombus = new RhombusShape({
  position: { x: 50, y: 250 }
});

const rectangle = new RectangleShape({
  position: { x: 450, y: 50 }
});

const ellipse = new EllipseShape({
  position: { x: 450, y: 300 }
});

const link1 = new Link({
  source: {
    id: rectangle.id,
    anchor: {
      name: "bottom"
    }
  },
  target: {
    id: rhombus.id,
    anchor: {
      name: "topLeft",
      args: {
        dx: 113.6,
        dy: 48.2,
        rotate: true
      }
    }
  }
});

const link2 = new Link({
  source: {
    id: rectangle.id,
    anchor: {
      name: "bottom"
    }
  },
  target: {
    id: ellipse.id,
    anchor: {
      name: "topLeft",
      args: {
        dx: 112.2,
        dy: 7,
        rotate: true
      }
    }
  }
});

const link3 = new Link({
  source: {
    id: ellipse.id,
    anchor: {
      name: "topLeft",
      args: {
        dx: 8.893956250391483,
        dy: 52.07374751827297,
        rotate: true
      }
    }
  },
  target: {
    id: rhombus.id,
    anchor: {
      name: "topLeft",
      args: {
        dx: 98.00000000000001,
        dy: 56,
        rotate: true
      }
    }
  }
});

graph.resetCells([rectangle, ellipse, rhombus, link1, link2, link3]);

// Tools

function addTools(view) {
  const { paper, model } = view;
  paper.removeTools();
  const tools = new dia.ToolsView({ tools: model.getTools() });
  view.el.classList.add("active");
  view.addTools(tools);
}

function removeTools(view) {
  view.el.classList.remove("active");
  view.removeTools();
}

              
            
!
999px

Console