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="scale-control">
  <span id="scale-value"></span>
  <input id="scale-range" min="0.75" max="2.5" type="range" step="0.25">
</div>
<div id="paper-container"></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

              
                #paper-container {
  position: absolute;
  right: 0;
  top: 0;
  left: 0;
  bottom: 0;
  overflow: scroll;
}

#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);
}

#scale-value {
  width: 32px;
  font-family: sans-serif;
}

#scale-control {
  width: 200px;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  z-index: 1;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  color: black;
  background-color: rgba(0, 0, 0, 0.2);
}

              
            
!

JS

              
                const { dia, shapes } = joint;

// Paper

const paperContainer = document.getElementById("paper-container");

const graph = new dia.Graph({}, { cellNamespace: shapes });
const paper = new dia.Paper({
  model: graph,
  cellViewNamespace: shapes,
  width: "100%",
  height: "100%",
  gridSize: 20,
  drawGrid: { name: "mesh" },
  async: true,
  sorting: dia.Paper.sorting.APPROX,
  background: { color: "#F3F7F6" },
  defaultLink: () => new joint.shapes.standard.Link()
});

paperContainer.appendChild(paper.el);

const buttonMarkup = [
  {
    tagName: "circle",
    selector: "button",
    attributes: {
      r: 7,
      fill: "#001DFF",
      cursor: "pointer"
    }
  },
  {
    tagName: "path",
    selector: "icon",
    attributes: {
      d: "M -2 4 2 4 M 0 3 0 0 M -2 -1 1 -1 M -1 -4 1 -4",
      fill: "none",
      stroke: "#FFFFFF",
      "stroke-width": 2,
      "pointer-events": "none"
    }
  }
];
const defaultScale = 1;
const scaleValueSpan = document.querySelector("#scale-value");
const scaleRange = document.querySelector("#scale-range");

scaleRange.addEventListener("input", ({ target: { value } }) =>
  setScaleValue(value)
);

const rect1 = new joint.shapes.standard.Rectangle({
  size: { width: 100, height: 100 },
  position: { x: 100, y: 100 }
});
const rect2 = rect1.clone().position(500, 100);
const link = new joint.shapes.standard.Link();
link.source(rect1);
link.target(rect2);

graph.addCells([rect1, rect2, link]);

function addElementTools(element, scale) {
  const removeTool = new joint.elementTools.Remove({ scale });
  const button = new joint.elementTools.Button({
    scale,
    action: () => alert("Button pressed"),
    x: "calc(w)",
    markup: buttonMarkup
  });
  const connectTool = new joint.elementTools.Connect({
    scale,
    x: "calc(w)",
    y: "calc(h)",
    magnet: "body"
  });

  element.findView(paper).addTools(
    new joint.dia.ToolsView({
      tools: [removeTool, button, connectTool]
    })
  );
}

function addLinkTools(link, scale) {
  const verticesTool = new joint.linkTools.Vertices({ scale });
  const targetArrowheadTool = new joint.linkTools.TargetArrowhead({ scale });
  const targetAnchorTool = new joint.linkTools.TargetAnchor({ scale });
  const removeTool = new joint.linkTools.Remove({ scale });

  link.findView(paper).addTools(
    new joint.dia.ToolsView({
      tools: [verticesTool, targetArrowheadTool, targetAnchorTool, removeTool]
    })
  );
}

function setScaleValue(value) {
  scaleValueSpan.innerText = value;
  scaleRange.value = value;

  graph.getLinks().forEach((link) => addLinkTools(link, value));
  graph.getElements().forEach((element) => addElementTools(element, value));
}

graph.on("add", (cell) => {
  if (!cell.isLink()) return;

  addLinkTools(cell, scaleRange.value);
});

setScaleValue(defaultScale);

              
            
!
999px

Console