Pen Settings

HTML

CSS

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

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

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.

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

              
                <html>

<head>
   <title>Node Editor</title>
</head>

<body>
   <div id="nodeEditor" class="node-editor"></div>
   <a target="_blank" href="https://codepen.io/Ni55aN/pen/xzgQYq" class="alert">A new version of the library under the name Rete.js is available</a>
  
</body>

</html>
              
            
!

CSS

              
                html, body{
   height: 100%;
   margin: 0;
}

#nodeEditor{
   position: relative;
  height: 100%
}

.socket.number{
    background: #96b38a
}

.control input {
   width: 150px
}


.alert {
   position: absolute;
   left: 0;
   top: 0;
   width: 100%;
   background: rgba(255, 30,3, 0.7);
   color: white;
   padding: 1%;
   font-size: 120%;
   font-family: monospace;
   text-decoration: none;
}


.alert:hover {
   color: #ffcccc;
}
              
            
!

JS

              
                var numSocket = new D3NE.Socket("number", "Number value", "hint");

var componentNum = new D3NE.Component("Number", {
   builder(node) {
      var out1 = new D3NE.Output("Number", numSocket);
      var numControl = new D3NE.Control('<input type="number">',
         (el, c) => {
            el.value = c.getData('num') || 1;
         
            function upd() {
               c.putData("num", parseFloat(el.value));
            }

            el.addEventListener("input", ()=>{
               upd();
               editor.eventListener.trigger("change");
            });
            el.addEventListener("mousedown", function(e){e.stopPropagation()});// prevent node movement when selecting text in the input field
           upd();
         }
      );

      return node.addControl(numControl).addOutput(out1);
   },
   worker(node, inputs, outputs) {
      outputs[0] = node.data.num;
   }
});

var componentAdd = new D3NE.Component("Add", {
   builder(node) {
      var inp1 = new D3NE.Input("Number", numSocket);
      var inp2 = new D3NE.Input("Number", numSocket);
      var out = new D3NE.Output("Number", numSocket);

      var numControl = new D3NE.Control(
         '<input readonly type="number">',
         (el, control) => {
            control.setValue = val => {
               el.value = val;
            };
         }
      );

      return node
         .addInput(inp1)
         .addInput(inp2)
         .addControl(numControl)
         .addOutput(out);
   },
   worker(node, inputs, outputs) {
      var sum = inputs[0][0] + inputs[1][0];
      editor.nodes.find(n => n.id == node.id).controls[0].setValue(sum);
      outputs[0] = sum;
   }
});

var menu = new D3NE.ContextMenu({
   Values: {
      Value: componentNum,
      Action: function() {
         alert("ok");
      }
   },
   Add: componentAdd
});

var container = document.getElementById("nodeEditor");
var components = [componentNum, componentAdd];
var editor = new D3NE.NodeEditor("demo@0.1.0", container, components, menu);

var nn = componentNum.newNode();
nn.data.num = 2;
var n1 = componentNum.builder(nn);
var n2 = componentNum.builder(componentNum.newNode());
var add = componentAdd.builder(componentAdd.newNode());

n1.position = [80, 200];
n2.position = [80, 400];
add.position = [500, 240];

editor.connect(n1.outputs[0], add.inputs[0]);
editor.connect(n2.outputs[0], add.inputs[1]);

editor.addNode(n1);
editor.addNode(n2);
editor.addNode(add);
//  editor.selectNode(tnode);

var engine = new D3NE.Engine("demo@0.1.0", components);

editor.eventListener.on("change", async function() {
   await engine.abort();
   await engine.process(editor.toJSON());
});

editor.view.zoomAt(editor.nodes);
editor.eventListener.trigger("change");
editor.view.resize();

              
            
!
999px

Console