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 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.

            
              <svg viewBox="0 0 400 200"/>
            
          
!
            
              * {
  box-sizing: border-box;  
}

svg {
  width: 100vw;
  background-color: rgba(10,20,0,0.03);
}

.bug-line {
  cursor: pointer;
  animation: .1s linear appears;
}

@keyframes appears {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
            
          
!
            
              function getRandom2016Date(){
  return new Date(2016, Math.floor(Math.random()*100 % 11) +1, Math.floor(Math.random()*100 % 29) +1);
} 
var bugs = Array.from({length: 100}, function(){
  var d = getRandom2016Date();
  var bug = {
    id: (Math.floor(Math.random() * 1000000)),
    start : d,
    end: new Date(d.getTime() + (1000 * 60 * 60 * 24 * ((Math.floor(Math.random() * 100) + 1) % 45 ) ))
  }; 
  return bug;
}).sort((a,b) => a.start - b.start);

const COLORS = ["rgb(244, 67, 54)","rgb(233, 30, 99)","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(0, 150, 136)","rgb(76, 175, 80)","rgb(139, 195, 74)","rgb(255, 193, 7)","rgb(255, 152, 0)","rgb(255, 87, 34)","rgb(121, 85, 72)","rgb(96, 125, 139)"];



var svg = document.querySelector('svg');

const jan4 = new Date('2016-01-04');  
const X_PADDING = 17;
const LANE_HEIGHT = 10;
const BOTTOM = svg.viewBox.baseVal.height;
const MILLISECOND_A_DAY = (1000*60*60*24);
var lanes = [];


drawWeeks();
drawToday();
bugs.forEach(drawBug);

function drawBug(bug, idx){
  setTimeout(function(){
  var colorIndex = (bug.id % (COLORS.length - 1));
  var bugColor = COLORS[colorIndex];
  var startDayNumber = ((bug.start - jan4)/(1000*60*60*24));
  var endDayNumber = (((bug.end || new Date('2016-12-31')) - jan4)/MILLISECOND_A_DAY);
  
  var startPoint = X_PADDING + startDayNumber;
  var endPoint = X_PADDING + endDayNumber;
    
  var lane = findLane(startPoint,endPoint);
  if(!lanes[lane]){
    lanes[lane] = [];
  }
  lanes[lane].push([startPoint,endPoint]);
  var y = (lane + 1) * LANE_HEIGHT;
  var bugGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
  bugGroup.classList.add('bug-line'); 

  var bugLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
  bugLine.setAttribute('x1', startPoint); 
  bugLine.setAttribute('y1', y);
  bugLine.setAttribute('x2', endPoint);
  bugLine.setAttribute('y2', y);
  bugLine.setAttribute('stroke', bugColor);
  bugLine.setAttribute('stroke-width', 2);
  bugLine.setAttribute('stroke-linecap', "round");
  bugGroup.appendChild(bugLine);
  
  if(bug.end){
    var endCircle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
    endCircle.setAttribute('cx',endPoint);
    endCircle.setAttribute('cy', y);
    endCircle.setAttribute('r', 2);
    endCircle.setAttribute('fill', bugColor);
    bugGroup.appendChild(endCircle);
  } 
  
  svg.appendChild(bugGroup);
  },idx * 50);
}

function drawWeeks(){
  for(var i = 0; i <= 52; i++){
    var weekLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
    var x = X_PADDING + (i * 7);
    weekLine.setAttribute('x1', x);
    weekLine.setAttribute('y1', 0);
    weekLine.setAttribute('x2', x);
    weekLine.setAttribute('y2', BOTTOM);
    weekLine.setAttribute('stroke', 'rgba(0,0,0,0.3)');
    weekLine.setAttribute('stroke-width', 0.1);
    svg.appendChild(weekLine); 
  }
}

function drawToday(){
  var todayNumber = (((new Date()) - jan4)/MILLISECOND_A_DAY);
  var todayX = todayNumber + X_PADDING;

  var todayLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
  todayLine.setAttribute('x1', todayX);
  todayLine.setAttribute('y1', 0);
  todayLine.setAttribute('x2', todayX);
  todayLine.setAttribute('y2', BOTTOM);
  todayLine.setAttribute('stroke', 'red');
  todayLine.setAttribute('stroke-width', 0.2);
  svg.appendChild(todayLine); 
}

function findLane(start, end){
  var lane = 0;
  var safe_space = 6;
  start = start - safe_space;
  end = end + safe_space;
  for(;lane < lanes.length;lane++){
    var fit = lanes[lane].every(function(xs){
      return !( 
        ( xs[0] >= start && xs[0] <= end ) ||
        ( start >= xs[0] && start <= xs[1] )
      );
    });
    if(fit === true){
      break;
    }
  }
  return lane;
}
            
          
!
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.
Loading ..................

Console