123

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <script type='text/javascript'>
// DEMO
// ========

function doStuff(){ 
  // wait until the node class is actually defined
  if (window.XG5 === undefined){
    setTimeout(doStuff, 50);
    return;
  }
  
  // Node 1
  var node = new XG5.GUINode({name: 'Another One'});
  node.addInput('Value1');
  node.addInput('Value2');
  node.addInput('Value3');

  // Node 2
  var node2 = new XG5.GUINode({name: 'Node 2'});
  node2.addInput('Text In');
  node2.addInput('Value 5');

  // Node 3
  var node3 = new XG5.GUINode({name: 'Something Else'});
  node3.addInput('Color4');
  node3.addInput('Position');
  node3.addInput('Noise Octaves');
  
  // Node 4
  var node4 = new XG5.GUINode({name: 'TextString'});
  node4.addInput('Value', 'input');

  // Move to initial positions
  node.moveTo({x: 300, y: 80});
  node2.moveTo({x: 20, y: 70});
  node3.moveTo({x:150, y:150});
  node4.moveTo({x:150, y:20});

  // Connect Nodes
  node.connectTo(node3.inputs[0]);
  node3.connectTo(node2.inputs[1]);
  node4.connectTo(node2.inputs[0]);

  // Add to DOM
  node.initUI();
  node2.initUI();
  node3.initUI();
  node4.initUI();
  
  node4.inputs[0].value = 'Some String';
  
  window.guiNodes = [];
  window.guiNodes.push(node);
  window.guiNodes.push(node2);
  window.guiNodes.push(node3);
  window.guiNodes.push(node4);
}

doStuff();
</script>
            
          
!
            
              .x-node:before{
  content:attr(title) " ";
  display: block;
  border-top-left-radius:.75em;
  border-top-right-radius:.75em;
  background-color:#6e6e6e;
  padding:0.1em .3em 0em;
  margin:-.1em -.3em 0.2em;
}
.x-node{
  color: #d4d4d4;
  font-family: sans-serif;
  background-color: #4e4e4e;
  border-radius: .75em;
  display: inline-block;
  padding:0.1em .3em .25em;
  position:absolute;
  box-shadow: 2px 2px 10px black;
  white-space: nowrap;
}
.x-output,
.x-connection:after{
  position:absolute;
  border:solid 1px #dedede;
  background-color:#2e2e2e;
  width:0.5em;
  height:0.5em;
  border-radius:0.5em;  
}
.x-node.connected > .x-output,
.x-connection.filled:after{
  border:solid 1px transparent;
  background-color:#aeaeae;
}
.x-node > .x-output:hover,
.x-connection:hover:after{
  border-color:red;
}
.x-output{
  left: -.5em;
  top:1em;
  cursor: pointer;
}
.x-output.hide{
  display: none;
}
.x-connection, .x-input{
  width:100%;
  position:relative;
  padding-right:0.5em;
  cursor:pointer;
}
.x-connection:after{
  content:"";
  right:0em;
  top:0.25em;
}
.x-input{
  display: flex;
  flex-grow: 1;
  justify-content: space-between;
  cursor: default;
}
.x-input input{
  width: 15ch;
}
#node-graph{
  background-image: linear-gradient(to right, transparent 18px, #4e4e4e 20px),
                    linear-gradient(to bottom, #2e2e2e 18px, #4e4e4e 20px);
  background-size: 20px 20px;
  position:absolute;
  top:0px;
  left:0px;
  z-index:-100;
  width:100%;
  height:100%;
}
            
          
!
            
              var XG5 = window.XG5 || {};
if (!jQuery) alert('Must include jQueryUI');

(function(sys, _$){
  if (sys.GUINode) return;

  // SVG Setup
  var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  svg.id = 'node-graph';
  svg.ns = svg.namespaceURI;
  document.body.appendChild(svg);
  
  function createPath(a, b){
    var diff = {
      x: b.x - a.x,
      y: b.y - a.y
    };
    
    var pathStr = [
      'M' + a.x + ',' + a.y + ' C',
      a.x + diff.x / 3 * 2 + ',' + a.y + ' ',
      a.x + diff.x / 3 + ',' + b.y + ' ',
      b.x + ',' + b.y
    ].join('');
    
    return pathStr;
  }
  
  var mouse = {
    currentInput: undefined
  };
  
  svg.onmousemove = function(e){
    if (mouse.currentInput){
      var path = mouse.currentInput.path;
      var inputPt = mouse.currentInput.getAttachPoint();
      var outputPt = {x: e.pageX, y: e.pageY};
      var val = createPath(inputPt, outputPt);
      path.setAttributeNS(null, 'd', val);
    }
  };
  
  svg.onclick = function(e){
    if (mouse.currentInput){
      mouse.currentInput.path.removeAttribute('d');
      
      if (mouse.currentInput.node)
        mouse.currentInput.node.detachInput(mouse.currentInput);
      
      mouse.currentInput = undefined;
    }
  };
  
  var getFullOffset = function(e){
    var offset = {
      top: e.offsetTop,
      left: e.offsetLeft
    };
    
    if (e.offsetParent){
      var parentOff = getFullOffset(e.offsetParent);
      offset.top += parentOff.top;
      offset.left += parentOff.left;
    }
    
    return offset;
  };
  
  function node(options){
    this.name = ' ';
    this.value = '';
    this.isRoot = false;
    
    for (var prop in options)
      if (this.hasOwnProperty(prop))
        this[prop] = options[prop];
    
    this.inputs = [];
    this.attachedPaths = [];
    this.connected = false;
    
    this.domElement = document.createElement('div');
    this.domElement.classList.add('x-node');
    this.domElement.setAttribute('title', this.name);
    
    var outputDom = document.createElement('span');
    outputDom.classList.add('x-output');
    outputDom.textContent = ' ';
    
    if (this.isRoot)
      outputDom.classList.add('hide');
    
    this.domElement.appendChild(outputDom);
    
    var that = this;
    outputDom.onclick = function(e){
      if (mouse.currentInput && !that.ownsInput(mouse.currentInput)){
        that.connectTo(mouse.currentInput);
        mouse.currentInput = undefined;
      }
      
      e.stopPropagation();
    };
  }
  
  function nodeInput(options){
    this.name = '';
    this.type = 'connection';
    
    for (var prop in options)
      if (this.hasOwnProperty(prop))
        this[prop] = options[prop];
    
    this.node = undefined;
    
    this.domElement = document.createElement('div');
    this.domElement.textContent = this.name;
    this.domElement.title = this.name;
    
    this.domElement.classList.add('x-' + this.type);
    this.domElement.classList.add('empty');
    
    var that = this;
    if (this.type == 'input'){
      var input = document.createElement('input');
      Object.defineProperty(that, 'value', {
        get: function(){ return input.value; },
        set: function(val){ input.value = val },
        enumerable: true
      });
      this.domElement.textContent += ' ';
      this.domElement.appendChild(input);
    }
    
    this.path = document.createElementNS(svg.ns, 'path');
    this.path.setAttributeNS(null, 'stroke', '#8e8e8e');
    this.path.setAttributeNS(null, 'stroke-width', '2');
    this.path.setAttributeNS(null, 'fill', 'none');
    svg.appendChild(this.path);
    
    if (this.type == 'connection'){
      this.domElement.onclick = function(e){
        if (mouse.currentInput){
          if (mouse.currentInput.path.hasAttribute('d'))
            mouse.currentInput.path.removeAttribute('d');
          if (mouse.currentInput.node){
            mouse.currentInput.node.detachInput(mouse.currentInput);
            mouse.currentInput.node = undefined;
          }
        }
        
        mouse.currentInput = that;
        if (that.node){
          that.node.detachInput(that);
          that.domElement.classList.remove('filled');
          that.domElement.classList.add('empty');
        }
        
        e.stopPropagation();
      };
    }
  }
  
  nodeInput.prototype = {
    getAttachPoint: function(){
      var offset = getFullOffset(this.domElement);
      return {
        x: offset.left + this.domElement.offsetWidth - 2,
        y: offset.top + this.domElement.offsetHeight / 2
      };
    }
  };
  
  node.prototype = {
    getOutputPoint: function(){
      var fchild = this.domElement.firstElementChild;
      var offset = getFullOffset(fchild);
      return {
        x: offset.left + fchild.offsetWidth / 2,
        y: offset.top + fchild.offsetHeight / 2
      };
    },
    addInput: function(name, type){
      var options = {};
      options.name = name;
      type === undefined ? true : options.type = type;
      
      var input = new nodeInput(options);
      this.inputs.push(input);
      this.domElement.appendChild(input.domElement);
      
      return input;
    },
    detachInput: function(input){
      var index = -1;
      for (var i = 0; i < this.attachedPaths.length; i++){
        if (this.attachedPaths[i].input == input)
          index = i;
      }
      
      if (index >= 0){
        this.attachedPaths[index].path.removeAttribute('d');
        this.attachedPaths[index].input.node = undefined;
        this.attachedPaths.splice(index, 1);
      }
      
      if (this.attachedPaths.length <= 0)
        this.domElement.classList.remove('connected');
    },
    ownsInput: function(input){
      for (var i = 0; i < this.inputs.length; i++){
        if (this.inputs[i] == input)
          return true;
      }
      
      return false;
    },
    updatePosition: function(){
      var outputPt = this.getOutputPoint();
      
      for (var i = 0; i < this.attachedPaths.length; i++){
        var inputPt = this.attachedPaths[i].input.getAttachPoint();
        var pathStr = createPath(inputPt, outputPt);
        this.attachedPaths[i].path.setAttributeNS(null, 'd', pathStr);
      }
      
      for (var j = 0; j < this.inputs.length; j++){
        if (this.inputs[j].node === undefined) continue;
        
        var inputPt = this.inputs[j].getAttachPoint();
        var outputPt = this.inputs[j].node.getOutputPoint();
        
        var pathStr = createPath(inputPt, outputPt);
        this.inputs[j].path.setAttributeNS(null, 'd', pathStr);
      }
    },
    connectTo: function(input){
      input.node = this;
      this.connected = true;
      this.domElement.classList.add('connected');
      
      input.domElement.classList.remove('empty');
      input.domElement.classList.add('filled');
      
      this.attachedPaths.push({
        input: input,
        path: input.path
      });
      
      var inputPt = input.getAttachPoint();
      var outputPt = this.getOutputPoint();
      
      var pathStr = createPath(inputPt, outputPt);
      input.path.setAttributeNS(null, 'd', pathStr);
    },
    moveTo: function(point){
      this.domElement.style.top = point.y + 'px';
      this.domElement.style.left = point.x + 'px';
      this.updatePosition();
    },
    initUI: function(){
      var that = this;
      
      _$(this.domElement).draggable({
        containment: 'window',
        cancel: '.x-connection, .x-output, .x-input',
        drag: function(e, ui){
          that.updatePosition();
        }
      });
      
      this.domElement.style.position = 'absolute';
      document.body.appendChild(this.domElement);
      this.updatePosition();
    }
  };
  
  sys.GUINode = node;
})(XG5, jQuery);
            
          
!
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.

Console