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

              
                <svg id="svg">         
  
  <g id="diagram" data-drag="diagram:diagram" data-drag-type="diagram">     
    <g id="node-layer">
      
      <!-- Тайтл блок -->   
      <g class="node-container">
        <rect class="node-background" width="204" height="108" x="0" y="0" rx="14" ry="14" />

        <g class="node-header">
          <rect class="header-round-rect" width="200" height="40" x="2" y="2" rx="14" ry="14" />
          <text class="header-title" x="60" y="22">My Title</text>
          <text class="sub-header-title" x="28" y="35">Targe is inside component</text>
        </g>

        <g class="node-content">
          <rect class="content-round-rect" fill="url(#linear-gradient)" width="200" height="60" x="2" y="44" rx="14" ry="14" />
          <rect class="content-rect"  width="200" height="60" x="2" y="44" />

          <g class="inputs">

            <g class="input-field" transform="translate(0, 50)">
              <g class="port">
                <circle class="port-outer" cx="15" cy="10" r="7.5" />
                <circle class="port-inner" cx="15" cy="10" r="5" />
                <circle class="port-scrim" cx="15" cy="10" r="7.5" />
              </g>
              <text class="port-label" x="28" y="14">Input 1</text>
            </g>
            
            <g class="input-field" transform="translate(0, 75)">
              <g class="port">
                <circle class="port-outer" cx="15" cy="10" r="7.5" />
                <circle class="port-inner" cx="15" cy="10" r="5" />
                <circle class="port-scrim" cx="15" cy="10" r="7.5" />
              </g>
              <text class="port-label" x="28" y="14">Input 2</text>
            </g> 
          </g>

          <g class="outputs">

            <g class="output-field" transform="translate(0, 50)">
              <g class="port" data-clickable="false">
                <circle class="port-outer" cx="189" cy="10" r="7.5" />
                <circle class="port-inner" cx="189" cy="10" r="5" />
                <circle class="port-scrim" cx="189" cy="10" r="7.5" data-clickable="false" />
              </g>
              <text class="port-label" x="176" y="14">Output 1</text>
            </g>
            
            <g class="output-field" transform="translate(0, 75)">
              <g class="port" data-clickable="false">
                <circle class="port-outer" cx="189" cy="10" r="7.5" />
                <circle class="port-inner" cx="189" cy="10" r="5" />
                <circle class="port-scrim" cx="189" cy="10" r="7.5" data-clickable="false" />
              </g>
              <text class="port-label" x="176" y="14">Output 2</text>
            </g>
           
          </g>
          <svg x="7" y="10">
            <image width="16" height="16" xlink:href="https://vladgohn.com/park/i-func.png" />
          </svg>
        </g>
      </g> 
      <!--  Конец - Тайтл блок -->  
      
      <!--  Блок с фоном  -->    
      <g class="node-container">    

        <g class="node-content">
          <defs>
            <filter id="f1" x="0" y="0">
              <feGaussianBlur in="SourceGraphic" stdDeviation="2,2"/>
            </filter>
          </defs>
          <g>
            <rect class="background-squar" width="16" height="16" x="73" y="56" />
            <rect class="background-squar" width="16" height="16" x="93" y="56" />
            <rect class="background-squar" width="16" height="16" x="113" y="56" />
            <rect class="background-squar" width="16" height="16" x="73" y="76" />
            <rect class="background-squar" width="16" height="16" x="93" y="76" />
            <rect class="background-squar" width="16" height="16" x="113" y="76" />
            <rect class="background-squar" width="16" height="16" x="73" y="96" />
            <rect class="background-squar" width="16" height="16" x="93" y="96" />
            <rect class="background-squar" width="16" height="16" x="113" y="96" />
          </g>
          <rect fill="url(#linear-gradient)" class="content-round-rect" width="200" height="82" x="2" y="44" rx="14" ry="14"  style="box-shadow: 0 2px 1px rgba(0, 0, 0, 0.3), 0 4px 3px rgba(0, 0, 0, 0.2), 0 7px 8px rgba(0, 0, 0, 0.1);" />
           <rect rx="8" ry="8" x="150" y="37" width="27" height="14" style="fill: aqua;" />
          <text class="main-text-block" x="76" y="94">BUNK</text>
          <text class="text-block-call" style="font-size: 12px;font-weight: bold;" x="153" y="48">call</text>
          <g class="inputs">

            <g class="input-field" transform="translate(0, 50)">
              <g class="port">
                <circle class="port-outer" cx="15" cy="10" r="7.5" />
                <circle class="port-inner" cx="15" cy="10" r="5" />
                <circle class="port-scrim" cx="15" cy="10" r="7.5" />
              </g>
              <text class="port-label" x="28" y="14">Input 4</text>
            </g>
            
            <g class="input-field" transform="translate(0, 75)">
              <g class="port">
                <circle class="port-outer" cx="15" cy="10" r="7.5" />
                <circle class="port-inner" cx="15" cy="10" r="5" />
                <circle class="port-scrim" cx="15" cy="10" r="7.5" />
              </g>
              <text class="port-label" x="28" y="14">Input 5</text>
            </g>
            
            <g class="input-field" transform="translate(0, 100)">
              <g class="port">
                <circle class="port-outer" cx="15" cy="10" r="7.5" />
                <circle class="port-inner" cx="15" cy="10" r="5" />
                <circle class="port-scrim" cx="15" cy="10" r="7.5" />
              </g>
              <text class="port-label" x="28" y="14">Input 6</text>
            </g>
            
          </g>

          <g class="outputs" style="stroke: 1px solid rgba(0, 0, 0, 0.6);">

            <g class="output-field" transform="translate(0, 50)">
              <g class="port" data-clickable="false">
                <circle class="port-outer" cx="189" cy="10" r="7.5" />
                <circle class="port-inner" cx="189" cy="10" r="5" />
                <circle class="port-scrim" cx="189" cy="10" r="7.5" data-clickable="false" />
              </g>
              <text class="port-label" x="176" y="14">Output1</text>
            </g>              
          </g>

        </g>
      </g> 
      <!--  Конец - Блок с фоном -->  
      
      <!--  Одиночный блок -->   
      <g class="node-container" >    
        
        <g class="node-content" >

          <rect class="content-round-rect" width="74" height="34" x="2" y="44" rx="14" ry="14" />
          <rect class="content-rect" width="74" rx="14" ry="14" height="34" x="2" y="44" />
          <g class="outputs">

            <g class="output-field" transform="translate(-125, 50)">
              <g class="port" data-clickable="false">
                <circle class="port-outer" cx="189" cy="10" r="7.5" />
                <circle class="port-inner" cx="189" cy="10" r="5" /> 
                <circle class="port-scrim" cx="189" cy="10" r="7.5" data-clickable="false" />
              </g>
              <text class="port-label" x="176" y="14">output1</text>
            </g>      
          </g>

        </g>
      </g>
      <!--  Конец - Одиночный блок -->  
      
      <!--  Одиночный блок -->  
       <g class="node-container" >    
        <g class="node-content" >
          <rect class="content-round-rect" width="74" height="34" x="2" y="44" rx="14" ry="14" />
          <rect class="content-rect" width="74" rx="14" ry="14" height="34" x="2" y="44" />
          <g class="outputs">
            <g class="output-field" transform="translate(-125, 50)">
              <g class="port" data-clickable="false">
                <circle class="port-outer" cx="189" cy="10" r="7.5" />
                <circle class="port-inner" cx="189" cy="10" r="5" /> 
                <circle class="port-scrim" cx="189" cy="10" r="7.5" data-clickable="false" />
              </g>
              <text class="port-label" x="176" y="14">output2</text>
            </g>      
          </g>
        </g>
      </g>
      <!--  Конец - Одиночный блок --> 
      
      <!-- До этого можно вставлять блоки -->
    </g>

    <g id="connections-layer"></g>

    <g class="connector" onclick="(function(_this){_this.parentNode.removeChild(_this);})(this);">

      <path class="connector-path-outline" />
      <path class="connector-path" />

      <circle class="connector-handle input-handle" cx="0" cy="0" r="4" />
      <circle class="connector-handle output-handle" cx="0" cy="0" r="4" />
    </g>
    
    <circle id="drag-proxy" cx="0" cy="0" r="1" fill="none" />    
  </g>
          <linearGradient id="linear-gradient" x2="0%" y1="100%">
            <stop offset="0%" stop-color="rgba(20, 20, 20, 0.77)"/>
            <stop offset="100%" stop-color="rgba(44, 44, 44, 0.42)"/>
          </linearGradient>
          <linearGradient id="title-linear-gradient">
            <stop offset="0%" stop-color="#04388f"/>
            <stop offset="100%" stop-color="rgba(13, 0, 231, 0)"/>
          </linearGradient>
</svg>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap");
body {
  overflow: hidden;
  background: #1a1a1a;
}

#svg {
  position: fixed;
  width: 100%;
  height: 100%;  
  font-family: Arial, sans-serif;
}
.main-text-block {
  font-family: "Bebas Neue";
  font-size: 30px;
  fill: rgba(255, 255, 255, 0.4);
}
.node-container {
  cursor: move;
}

.node-background {
  fill: url(#linear-gradient);
}

.node-header {  
  fill: url(#title-linear-gradient);
  
  .header-title {
    font-size: 1.1em;
    fill: #fff;
    font-weight: 700;
    text-anchor: middle;
    line-height: 1em;
  }
  .sub-header-title {
    fill: rgba(255, 255, 255, 0.6);
    font-size: .7em;
    font-weight: 400;
    font-style: italic;
  }
}

.node-content {  
  fill: url(#linear-gradient);
}

.port {
  cursor: pointer;
}

.port-scrim {
  fill: transparent;
}

.port-outer {
  fill: #23e3ad;
}

.port-inner {
  fill: #202020;
}

.port-label {
  font-size: 0.8em;
  fill: #ededed;
  line-height: 2em;
  font-family: "SF Pro Text", -apple-system, BlinkMacSystemFont, Roboto, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

.input-field {  
  .port-label {
    text-anchor: start;
  }
}

.output-field {  
  .port-label {
    text-anchor: end;
  }
}

.connector-handle {
  fill: #23e3ad;
}

.connector-path {
  stroke: #23e3ad;  
  stroke-width: 2;
  fill: none;
}
.connector-path:hover{
  stroke: red; 
}
.connector-path:hover ~ .connector-handle {
  fill: red;
}
.connector-path-outline {
  stroke: #333;
  stroke-width: 2;
  fill: none;
}

.content-round-rect {
  stroke:rgba(0, 0, 0, 0.6);
  stroke-width: 1px;
  stroke-opacity: .6;
}

.background-squar {
  fill: #22e3ad;
  filter:  blur(100px);
}
              
            
!

JS

              
                SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement || function(toElement) {
  return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());  
};

//Строим линию из коннектов
class Connector {
  
  constructor() {
    
    this.id = `connector_${++nextUid}`;
    this.dragType = "connector";    
    this.isSelected = false;    
    this.element = connectorElement.cloneNode(true);
    this.path = this.element.querySelector(".connector-path");
    this.pathOutline = this.element.querySelector(".connector-path-outline");
    this.inputHandle = this.element.querySelector(".input-handle");
    this.outputHandle = this.element.querySelector(".output-handle");
  }
  
  init(port) {
       
    connectorLayer.appendChild(this.element);
    
    this.isInput = port.isInput;
    
    if (port.isInput) {
      this.inputPort = port;      
      this.dragElement = this.outputHandle;
      this.staticElement = this.inputHandle;
    } else {
      this.outputPort = port;
      this.dragElement = this.inputHandle;
      this.staticElement = this.outputHandle;
    }
       
    this.staticPort = port;    
    this.dragElement.setAttribute("data-drag", `${this.id}:connector`);
    this.staticElement.setAttribute("data-drag", `${port.id}:port`);       
    
    TweenLite.set([this.inputHandle, this.outputHandle], {
      x: port.global.x,
      y: port.global.y
    });
    
    
  }
  
  updatePath() {
    
    const x1 = this.inputHandle._gsTransform.x;
    const y1 = this.inputHandle._gsTransform.y;
    
    const x4 = this.outputHandle._gsTransform.x;
    const y4 = this.outputHandle._gsTransform.y;
    
    const dx = Math.abs(x1 - x4) * bezierWeight;
    
    const p1x = x1;
    const p1y = y1;
    
    const p2x = x1 - dx;
    const p2y = y1;
        
    const p4x = x4;
    const p4y = y4;
    
    const p3x = x4 + dx;
    const p3y = y4;
    
    const data = `M${p1x} ${p1y} C ${p2x} ${p2y} ${p3x} ${p3y} ${p4x} ${p4y}`;
    
    this.path.setAttribute("d", data);
    this.pathOutline.setAttribute("d", data);
  }
  
  updateHandle(port) {
    
    if (port === this.inputPort) {
      
      TweenLite.set(this.inputHandle, {
        x: port.global.x,
        y: port.global.y
      });
      
    } else if (port === this.outputPort) {
      
      TweenLite.set(this.outputHandle, {
        x: port.global.x,
        y: port.global.y
      });
    }
    
    this.updatePath();    
  }
  
  placeHandle() {
    
    const skipShape = this.staticPort.parentNode.element;
    
    let hitPort;
    
    for (let shape of shapes) {
      
      if (shape.element === skipShape) {
        continue;
      }
      
      if (Draggable.hitTest(this.dragElement, shape.element)) {
        
        const ports = this.isInput ? shape.outputs : shape.inputs;
        
        for (let port of ports) {
          
          if (Draggable.hitTest(this.dragElement, port.portElement)) {
            hitPort = port;
            break;
          }
        }
        
        if (hitPort) {
          break;
        }
      }
    }
        
    if (hitPort) {      
      
      if (this.isInput) {
        this.outputPort = hitPort;
      } else {
        this.inputPort = hitPort;
      }
      
      this.dragElement.setAttribute("data-drag", `${hitPort.id}:port`);
      
      hitPort.addConnector(this);
      this.updateHandle(hitPort);
      
    } else {
      this.remove();
    }
  }
  
  remove() {
    console.log('removed');
    if (this.inputPort) {
      console.log('remove input');
      this.inputPort.removeConnector(this);
    }
    
    if (this.outputPort) {
      console.log('remove output');
      this.outputPort.removeConnector(this);
    }
    
    this.isSelected = false;
    
    this.path.removeAttribute("d");
    this.pathOutline.removeAttribute("d");
    this.dragElement.removeAttribute("data-drag");
    this.staticElement.removeAttribute("data-drag");     
    
    this.staticPort = null;    
    this.inputPort = null;
    this.outputPort = null;
    this.dragElement = null;
    this.staticElement = null;
    
    connectorLayer.removeChild(this.element);
    connectorPool.push(this);
  }
  
  onDrag() {    
    this.updatePath();
  }
  
  onDragEnd() {
    this.placeHandle();
  }
}

//Указываем точки привязки
class NodePort {
  
  constructor(parentNode, element, isInput) {
        
    this.id = `port_${++nextUid}`;
    this.dragType = "port";
        
    this.parentNode = parentNode;
    this.isInput = isInput;
    
    this.element = element;
    this.portElement = element.querySelector(".port");
    this.portScrim = element.querySelector(".port-scrim");
    
    this.portScrim.setAttribute("data-drag", `${this.id}:port`);
    
    this.connectors = [];
    this.lastConnector;
    
    const bbox = this.portElement.getBBox();
    
    this.global = svg.createSVGPoint();        
    this.center = svg.createSVGPoint();
    this.center.x = bbox.x + bbox.width / 2;
    this.center.y = bbox.y + bbox.height / 2;
        
    this.update();
  }
  
  createConnector() {
    
    let connector;
    
    if (connectorPool.length) {
      connector = connectorPool.pop();
      connectorLookup[connector.id] = connector;
    } else {
      connector = new Connector();  
    }
    
    connector.init(this);
    this.lastConnector = connector;
    this.connectors.push(connector);
  }
  
  removeConnector(connection) {
    
    const index = this.connectors.indexOf(connection);
    
    if (index > -1) {
      this.connectors.splice(index, 1);
    }
  }
  
  addConnector(connection) {
    this.connectors.push(connection);
  }
      
  update() {   
    
    const transform = this.portElement.getTransformToElement(diagramElement);
    this.global = this.center.matrixTransform(transform);
    
    for (let connector of this.connectors) {
      connector.updateHandle(this);
    }
  }
}


class NodeShape {
  
  constructor(element, x, y) {
        
    this.id = `shape_${++nextUid}`;
    this.dragType = "shape";
    
    element.setAttribute("data-drag", `${this.id}:shape`);
    
    this.element = element;    
    this.dragElement = element;
    
    TweenLite.set(element, { x, y });    
    
    const inputElements = Array.from(element.querySelectorAll(".input-field"));
    const outputElements = Array.from(element.querySelectorAll(".output-field"));
    
    this.inputs = inputElements.map(element => {
      const port = new NodePort(this, element, true);
      portLookup[port.id] = port;
      ports.push(port);
      return port;
    });
    
    this.outputs = outputElements.map(element => {
      const port = new NodePort(this, element, false);
      portLookup[port.id] = port;
      ports.push(port);
      return port;
    });    
  }  
  
  onDrag() {
    
    for (let input of this.inputs) {
      input.update();
    }
    
    for (let output of this.outputs) {
      output.update();
    }    
  }  
}

class Diagram {
  
  constructor() {
    
    this.dragElement = this.element = diagramElement;
    
    shapeElements.forEach((element, i) => {     
      const shape = new NodeShape(element, 50 + i * 250, 50);      
      shapeLookup[shape.id] = shape;
      shapes.push(shape);
    });
       
    this.target = null;
    this.dragType = null;
    
    this.dragTarget = this.dragTarget.bind(this);
    this.prepareTarget = this.prepareTarget.bind(this);
    this.stopDragging = this.stopDragging.bind(this);
    
    this.draggable = new Draggable(dragProxy, {
      allowContextMenu: true,
      trigger: svg,
      onDrag: this.dragTarget,
      onDragEnd: this.stopDragging,
      onPress: this.prepareTarget,
    });
  }
  
  stopDragging() {
    this.target.onDragEnd && this.target.onDragEnd();
  }
  
  prepareTarget(event) {
    
    let element = event.target;
    let drag;
    
    while (!(drag = element.getAttribute("data-drag")) && element !== svg) {
      element = element.parentNode;
    }
    
    drag = drag || "diagram:diagram";
    const split = drag.split(":");
    const id = split[0];
    const dragType = split[1];
    
    switch (dragType) {
      case "diagram":
        this.target = this;
        break;
        
      case "shape":
        this.target = shapeLookup[id];
        break;
        
      case "port":
        const port = portLookup[id];
        port.createConnector();
        this.target = port.lastConnector;
        this.dragType = this.target.dragType;
        break;
        
      case "connector":
        this.target = connectorLookup[id];
        break;
    }
  }
    
  dragTarget() {
    
    TweenLite.set(this.target.dragElement, {
      x: `+=${this.draggable.deltaX}`,
      y: `+=${this.draggable.deltaY}`,
    });
    
    this.target.onDrag && this.target.onDrag();
  }
}

let nextUid = 0;

const bezierWeight = 0.675;

const svg = document.querySelector("#svg");
const diagramElement = document.querySelector("#diagram");

const shapeLookup = {};
const portLookup = {};
const connectorLookup = {};

const ports = [];
const shapes = [];
const connectorPool = [];

const dragProxy = document.querySelector("#drag-proxy");
const shapeElements = Array.from(document.querySelectorAll(".node-container"));

const frag = document.createDocumentFragment();
frag.appendChild(document.querySelector(".connector"));
const connectorElement = frag.querySelector(".connector");
const connectorLayer = document.querySelector("#connections-layer");

const diagram = new Diagram();

jQuery('.connector').on('click', function(){
  this.remove();
})
              
            
!
999px

Console