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

              
                
              
            
!

CSS

              
                body{
  margin: 0;
}

svg{
  position: fixed;
  top: 0;
}
              
            
!

JS

              
                var w = 1300,
  h = 1000,
  voronoi = new Voronoi(),
  bbox = {
    xl: 0,
    xr: w,
    yt: 0,
    yb: h
  },
  sites = [{
    id: 0,
    x: 50,
    y: 200
  }, {
    id: 1,
    x: 200,
    y: 250
  }, {
    id: 2,
    x: 300,
    y: 100
  }, {
    id: 3,
    x: 500,
    y: 200
  }, {
    id: 4,
    x: 500,
    y: 300
  }],
  diagram,
  v = new Snap(w, h),
  ui = new Snap(w, h),
  focus,
  colors = ['#3D9299', '#14BECC', '#00FF83', '#FF7985', '#CC1481'],
  currentColor = 0;

updateUI();
updateVoronoi();

function handle_addClick(e) {

  var _id = parseInt(this.attr('vId'));

  if (_id == sites.length - 1) {
    sites.push({
      id: sites.length,
      x: sites[sites.length - 1].x + 50,
      y: sites[sites.length - 1].y + 50
    });
  } else {

    //TODO:: children's children
    var parent = sites[_id];
    if (parent.collection) {
      parent.collection.push({
        id: _id + "_" + parent.collection.length,
        x: parent.collection[parent.collection.length - 1].x + 50,
        y: parent.collection[parent.collection.length - 1].y + 50
      });
    } else {
      parent.collection = [{
        id: _id + "_0",
        x: sites[_id].x + 30,
        y: sites[_id].y + 50
      }];
    }
  }

    updateUI();
    updateVoronoi();
}

function getById(_id) {
  var i,
    j;

  for (i = 0; i < sites.length; i += 1) {

    if (sites[i].id == _id) {
      return sites[i];
    } else if (sites[i].collection) {

      //TODO:: loop children's children
      for (j = 0; j < sites[i].collection.length; j += 1) {
        if (sites[i].collection[j].id == _id) {
          return sites[i].collection[j];
        }
      }

    }
  }

  return false;
}

function handle_dragStart() {
  this.data('origTransform', this.transform().local);
  removeFocus();
}

function handle_dragStop() {
    updatePaths();
    updateVoronoi();
  console.log('drag stop');
}

function handle_drag(dx, dy, px, py) {
  var x, y;
  this.attr({
    transform: this.data('origTransform') + (this.data('origTransform') ? "T" : "t") + [dx, dy]
  });

  if (this.data('origTransform')) {
    x = parseFloat(this.attr('cx')) + parseFloat(this.data('origTransform').replace('t', '').split(',')[0]) + dx,
      y = parseFloat(this.attr('cy')) + parseFloat(this.data('origTransform').replace('t', '').split(',')[1]) + dy;
  } else {
    x = parseFloat(this.attr('cx')) + dx;
    y = parseFloat(this.attr('cy')) + dy;
  }

  var movedCircle = getById(this.attr('vId'));

  if (movedCircle) {
    movedCircle.x = x;
    movedCircle.y = y;
  }

    updatePaths();
    //updateVoronoi();
}

function addFocus(e, t) {
  var x = e.target.cx.baseVal.value,
    y = e.target.cy.baseVal.value;

  focus = ui.g();
  focusCircle = ui.circle(0, 0, 50);
  focusCircle.attr({
    fill: 'transparent',
    stroke: '#222',
    'stroke-width': '2',
    pointerEvents: 'none'
  });
  focus.append(focusCircle);

  focusBtn = ui.g();
  focusBtn.transform('translate(0, 50)');
  focusBtn.click(handle_addClick, t);
  focus.append(focusBtn);

  focusBtnCircle = ui.circle(0, 0, 15);
  focusBtnCircle.attr('fill', '#666');
  focusBtn.append(focusBtnCircle);

  focusIcon = ui.path('M-3.8-1.3H-1v-2.8h2.3v2.8H4v2.1H1.3v2.8H-1V0.8h-2.8V-1.3z');
  focusIcon.attr('fill', 'white');
  focusBtn.append(focusIcon);

  var m = e.target.transform.baseVal[0].matrix,
    mat = new Snap.matrix(m);
  mat.translate(x, y);

  focus.transform(mat);
}

function removeFocus() {
  if (focus) {
    focus.remove();
  }
}

function handle_click(e) {
  addFocus(e, this);
}

function handle_clickOff(e) {
  removeFocus(e);
}

function flatten(_s) {
  var arr = [],
      n = 0,
      j = 0;
    
  for (n; n < _s.length; n += 1) {
    arr.push(_s[n]);

    if (_s[n].collection) {
      for (j = 0; j < _s[n].collection.length; j += 1) {
        arr.push(_s[n].collection[j]);
      }
    }
  }
  
  return arr;
}

function getMask() {
  var i,
    l,
    _l,
    maskGroup,
    arr = [],
    strokeOpts = {
      'fill': 'transparent',
      'stroke-width': '100',
      'stroke-linejoin': 'round',
      'stroke-linecap': 'round',
      'stroke': 'purple'
    };

  maskGroup = v.g();

  for (i = 0; i < sites.length; i += 1) {
    arr.push(sites[i].x);
    arr.push(sites[i].y);

    if (sites[i].collection) {
      var seg = [sites[i].x, sites[i].y];

      for (j = 0; j < sites[i].collection.length; j += 1) {
        seg.push(sites[i].collection[j].x);
        seg.push(sites[i].collection[j].y);
      }

      _l = v.polyline(seg);
      _l.attr(strokeOpts);
      maskGroup.append(_l);
    }
  }

  l = v.polyline(arr);
  l.attr(strokeOpts);
  maskGroup.append(l);

  return maskGroup;
}

function updateVoronoi() {
  var i,
    line,
    l,
    mask,
    maskContent,
    vGroup,
    flattenSites;

  flattenSites = flatten(sites);

  if (flattenSites.length == 0) {
    return;
  }
  
  diagram = voronoi.compute(flattenSites, bbox),
    
  v.clear();
  vGroup = v.g();
  
  for (i = 0; i < diagram.cells.length; i += 1) {
    var cell = diagram.cells[i];
    var arr = [];

    for (j = 0; j < cell.halfedges.length; j += 1) {
      start = cell.halfedges[j].getStartpoint();
      end = cell.halfedges[j].getEndpoint();

      arr.push(start.x);
      arr.push(start.y);
      arr.push(end.x);
      arr.push(end.y);
    }

    var l = v.polygon(arr);
    l.attr({
      'fill': colors[currentColor]
    });
    vGroup.append(l);

    currentColor = currentColor + 1 > colors.length - 1 ? 0 : currentColor + 1;
  }

  //mask
  maskContent = getMask();
  mask = v.mask();
  mask.attr({
    'mask-type': 'alpha'
  })
  mask.append(maskContent);
  vGroup.attr({
    'mask': mask
  });

  //depth
  var clone = vGroup.clone();
  clone.transform('translate(0, 10)');
  var satFilter = v.filter(Snap.filter.brightness(0.5));
  clone.attr({
    'filter': satFilter
  });
  clone.insertBefore(vGroup);

    //depth
  var clone2 = vGroup.clone();
  clone2.transform('translate(0, 20)');
  var satFilter2 = v.filter(Snap.filter.brightness(0.0));
  clone2.attr({
    'opacity': 0.1,
    'filter': satFilter2
  });
  clone2.insertBefore(clone);
}

function updatePaths() {
  var i;

  ui.selectAll('line').remove();

  for (i = 0; i < sites.length; i += 1) {
    if (i > 0) {
      var line = ui.line(sites[i - 1].x, sites[i - 1].y, sites[i].x, sites[i].y);
      line.attr({
        'stroke': '#efefef',
        'stroke-width': '7',
        'stroke-linejoin': 'round',
        'stroke-linecap': 'round'
      });
      childrenPaths(sites[i]);
    }
  }
}

function childrenPaths(_s) {
  var i;
  
  if (_s.collection) {
    for (i = 0; i < _s.collection.length; i += 1) {
      if (i == 0) {
        var line = ui.line(_s.x, _s.y, _s.collection[i].x, _s.collection[i].y);
      } else {
        var line = ui.line(_s.collection[i - 1].x, _s.collection[i - 1].y, _s.collection[i].x, _s.collection[i].y);
      }
      line.attr({
        'stroke': '#efefef',
        'stroke-width': '7',
        'stroke-linejoin': 'round',
        'stroke-linecap': 'round'
      });
      childrenPaths(_s.collection[i]);
    }
  }
}

function updateChildren(_s, id) {
  var i;

  if (_s.collection) {

    for (i = 0; i < _s.collection.length; i += 1) {
      var s = _s.collection[i];

      var site = ui.circle(s.x, s.y, 20);
      site.attr({
        'vId': id + '_' + i,
        'cursor': 'pointer',
        'fill': '#333'
      });
      site.drag(handle_drag, handle_dragStart, handle_dragStop);
      site.click(handle_click);

      updateChildren(s, id + '_' + i);
    }
  }
}

function updateUI() {
  var i;

  ui.clear();

  var rect = ui.rect(0, 0, w, h);
  rect.attr('fill', 'transparent');
  rect.click(handle_clickOff);

  for (i = 0; i < sites.length; i += 1) {
    var s = sites[i];

    var site = ui.circle(s.x, s.y, 20);
    site.attr({
      'vId': i,
      'cursor': 'pointer',
      'fill': '#333'
    });
    site.drag(handle_drag, handle_dragStart, handle_dragStop);
    site.click(handle_click);

    updateChildren(s, i);
  }

  updatePaths();
}
              
            
!
999px

Console