cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

            
              <div id="myholder"></div>
<input type="button" value="debug" id="debug" />
<input type="text" placeholder="New node label" id="new-node-label" />
<input type="button" value="add node" id="add-node" />
<input type="checkbox" id="link" />link
<input type="button" value="init" id="init" />

            
          
!
            
              const graph = new joint.dia.Graph;
graph.on("add", (elm) => {
  console.log("add : " + elm.cid);
});

const paper = new joint.dia.Paper({
  el: document.getElementById("myholder"),
  model: graph,
  width: 400,
  height: 300,
  gridSize: 1,
  drawGrid: true,
  background: {
    color: "rgba(0, 255, 0, 0.3)"
  }});

$("#debug").click(() => {
  console.log(graph);
});

$("#init").click(() => {
  graph.clear();
  init();
});

const colors = ["red", "blue", "black", "orange", "green"];
const selectColor = () => {
  const n = parseInt(Math.random() * Math.floor(colors.length));
  return colors[n];
};

const addNode = (label) => {
  const rect = new joint.shapes.standard.Rectangle();
  const x = Math.random() * 330;
  const y = Math.random() * 220;
  rect.position(x, y);
  rect.resize(100, 40);
  rect.attr({
    body: {
      fill: selectColor()
    },
    label: {
      text: label,
      fill: 'white'
    }
  });
  rect.on('change:position', (rect, position)=> {
    console.log(label, position.x, position.y);
  });
  rect.addTo(graph);
  return rect;
};

const append = () => {
  const label = $("#new-node-label").val();
  if (label != "") {
    addNode(label);
  }
  $("#new-node-label").val("");
};

$("#add-node").click(() => {
  append();
});

$("#new-node-label").keypress((key) => {
  if (key.charCode == 13) {
    append();
  }
});

const addLink = (source, target) => {
  const link = new joint.shapes.standard.Link();
  link.source(source);
  link.target(target);
  link.addTo(graph);
};

let cellViewFrom = null;
let from = null;
let to = null;

paper.on("cell:pointerup", (cellView) => {
  const linkMode = $("#link").prop("checked");
  if (!linkMode) return;
  if (from === null) {
    cellView.highlight();
    cellViewFrom = cellView;
    from = cellView.model;
  } else if (to === null) {
    to = cellView.model;
  }
  if (from && to) {
    addLink(from, to);
    from = null;
    to = null;
    $("#link").prop("checked", false);
    cellViewFrom.unhighlight();
  }
});

const init = () => {
  const rect1 = addNode("Hello");
  const rect2 = addNode("JointJS")
  const rect3 = addNode("World!");
  addLink(rect1, rect2);
  addLink(rect2, rect3);  
};

init();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console