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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <svg viewBox="0 0 100 20">
  <line x1=10 y1=10 x2=90 y2=10 stroke-width=6 stroke="#FF5722" stroke-linecap=round></line>
  <line x1=0 y1=10 x2=90 y2=10 stroke-width=1 stroke="black" stroke-linecap=round></line>
</svg>
            
          
!
            
              "use strict";

const COLORS = ["rgb(244, 67, 54)","rgb(0, 150, 136)","rgb(96, 125, 139)","rgb(156, 39, 176)","rgb(103, 58, 183)","rgb(63, 81, 181)","rgb(33, 150, 243)","rgb(3, 169, 244)","rgb(0, 188, 212)","rgb(76, 175, 80)","rgb(139, 195, 74)","rgb(255, 193, 7)","rgb(255, 152, 0)","rgb(255, 87, 34)","rgb(233, 30, 99)","rgb(121, 85, 72)"];

var svg = document.querySelector('svg');
  var groups = [];

run();

function getX(){
  return (10 + Math.random()*80);
}

function run(){
  var waitDelay = 500;

  drawCircleEntries()
    .then(function(){
      return new Promise(function(r){
        setTimeout(() => r(), waitDelay);
      });
    })
    .then(function(){
      return drawGroupOutlines();
    })
    .then(function(){
      return new Promise(function(r){
        setTimeout(() => r(), waitDelay);
      });
    })
    .then(function(){
      return drawGroupRectangles();
    })
    .then(function(data){
      return new Promise(function(r){
        setTimeout(() => r(data), waitDelay);
      });
    })
    .then(function(data){
      return drawGroupClipPath(data);
    }, function(err){
      console.warn(err);
    })
    .then(function(){
      return new Promise(function(r){
        setTimeout(() => r(), waitDelay);
      });
    })
    .then(function(){
      return drawGroupStroke();
    })
    .then(function(){
      return new Promise(function(r){
        setTimeout(() => r(), waitDelay * 4);
      });
    })
    .then(function(){
      return clear();
    })
    .then(run)
  .catch(function(ex){
    console.error(ex);
  });  
}

function drawCircleEntries(){
    var circlesGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
    svg.appendChild(circlesGroup);
    var insertPromises = [];
    for(var i = 0; i<(10 + Math.random() * 10); i++){
      var colorIndex = (i % (COLORS.length - 1));
      var bugColor = COLORS[colorIndex];
      let circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
      circle.classList.add('entry');
      circle.setAttribute('cx',getX());
      circle.setAttribute('cy', 10);
      circle.setAttribute('r', 2);
      circle.setAttribute('stroke-width', .2);
      circle.setAttribute('stroke', 'black');
      circle.setAttribute('fill', bugColor);
      let insertPromise = function(){
        return new Promise(function(resolve){
          setTimeout(function(){
            circlesGroup.appendChild(circle);
            resolve();
          }, i * 100);
        });
      }
      insertPromises.push(insertPromise());
    }
    return Promise.all(insertPromises);
}

function drawGroupOutlines(){
  var circles = Array.from(document.querySelectorAll('circle'));
  circles.sort(function(a, b){
    return (parseFloat(a.getAttribute('cx')) - parseFloat(b.getAttribute('cx')));
  });
  circles.reduce(function(previousValue, currentValue, currentIndex, array){
    if(groups.length > 0 && previousValue && parseFloat(currentValue.getAttribute('cx')) - parseFloat(previousValue.getAttribute('cx')) < 4){
      groups[groups.length - 1].push(currentValue);              
    } else {
      groups.push([currentValue]);
    }
    return currentValue;
  }, null);

  let insertPromises = [];
  let outlineGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
  outlineGroup.setAttribute('class','outline-group');
  svg.appendChild(outlineGroup);
  groups.forEach(function(group, index){
    let rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
    var start = parseFloat(group[0].getAttribute('cx')) - 2;
    var end = parseFloat((group[group.length - 1]).getAttribute('cx')) + 2;

    rect.setAttribute('x', start);
    rect.setAttribute('width', end - start);
    rect.setAttribute('y', 5);
    rect.setAttribute('height', 10) ;
    rect.setAttribute('stroke', 'green');
    rect.setAttribute('stroke-width', .2);
    rect.setAttribute('fill', 'transparent');
    let insertPromise = function(){
      return new Promise(function(resolve){
        setTimeout(function(){
          outlineGroup.appendChild(rect);
          resolve();
        }, index * 100);
      });
    }
    insertPromises.push(insertPromise());    
  });
  
  return Promise.all(insertPromises);
}

function drawGroupRectangles(){
  let insertPromises = [];
  let rectanglesGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
  rectanglesGroup.setAttribute('class','rectangles-group');
  svg.appendChild(rectanglesGroup);
  
  groups.forEach(function(group, groupIndex){
    var start = parseFloat(group[0].getAttribute('cx')) - 2;
    var end = parseFloat((group[group.length - 1]).getAttribute('cx')) + 2;
    var width = (end - start)/ group.length;

    let clipId = 'group-'+groupIndex;
    var groupClipath = document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
    groupClipath.setAttribute('id',clipId);
    var startCircleClip = document.createElementNS("http://www.w3.org/2000/svg", "circle");
    startCircleClip.setAttribute('cx',start + 2);
    startCircleClip.setAttribute('cy',10);
    startCircleClip.setAttribute('r',2);

    var endCircleClip = document.createElementNS("http://www.w3.org/2000/svg", "circle");
    endCircleClip.setAttribute('cx',end - 2);
    endCircleClip.setAttribute('cy',10);
    endCircleClip.setAttribute('r',2);

    var rectClip = document.createElementNS("http://www.w3.org/2000/svg", "rect");
    rectClip.setAttribute("x",start + 2);
    rectClip.setAttribute("y",8 );
    rectClip.setAttribute("height",4);
    rectClip.setAttribute("width", (end - 2) - (start  + 2));

    groupClipath.appendChild(startCircleClip);
    groupClipath.appendChild(endCircleClip);
    groupClipath.appendChild(rectClip);
    svg.appendChild(groupClipath);
    
    group.forEach(function(entry,index){
      let rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
      rect.setAttribute('x', start + (width * index));
      rect.setAttribute('width', width);
      rect.setAttribute('y', 5);
      rect.setAttribute('height', 10) ;
      rect.setAttribute('fill', entry.getAttribute('fill'));
      rect.setAttribute('stroke', 'black');
      rect.setAttribute('stroke-width', .1);

      let outline = document.createElementNS("http://www.w3.org/2000/svg", "line");
      outline.setAttribute('class','group-outline');
      outline.setAttribute('opacity',0);
      outline.setAttribute('x1', start + 2);
      outline.setAttribute('y1', 10);
      outline.setAttribute('x2', end - 2);
      outline.setAttribute('y2', 10);
      outline.setAttribute('stroke', 'black');
      outline.setAttribute('stroke-width', 4.6);
      outline.setAttribute('stroke-linecap', 'round');
      svg.insertBefore(outline, svg.children[1]);      
      
      let insertPromise = function(){
        return new Promise(function(resolve){
          setTimeout(function(){
            rectanglesGroup.appendChild(rect);
            resolve({
              clip: groupClipath,
              element: rect
            });
          }, index * 100);
        });
      }
      insertPromises.push(insertPromise());  
    });  
  });
  
  return Promise.all(insertPromises);
}

function drawGroupClipPath(data){
  Array.from(document.querySelectorAll('.outline-group, circle.entry')).forEach(function(g){
    g.remove();
  });
  let promises = [];
  data.forEach(function(obj, index){
    let p = function(){
      return new Promise(function(resolve){
        setTimeout(function(){
          obj.element.setAttribute('clip-path',`url(#${obj.clip.getAttribute('id')})`);
          resolve();
        }, index * 100);
      });
    }
    promises.push(p());  
  });
  return Promise.all(promises);
}

function drawGroupStroke(){
  let promises = [];
  Array.from(document.querySelectorAll('.group-outline')).forEach(function(el, index){
    let p = function(){
      return new Promise(function(resolve){
        setTimeout(function(){
          el.setAttribute('opacity', 1);
          resolve();
        }, index * 50);
      });
    }
    promises.push(p());  
  });
  return Promise.all(promises);
}

function clear(){
  groups = [];
  Array.from(svg.querySelectorAll('.group-outline, .rectangles-group, circles, clipPath')).forEach(function(el, index){
    el.remove();
  });
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console