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

              
                <div id="two"></div>
              
            
!

CSS

              
                html, body {
  height: 100%;
  background: #000;
  overflow: hidden;
}

svg {
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #000;
  //border: 1px solid blue;
}
              
            
!

JS

              
                // this code is kinda messy, but also it's a wip, so i have the perfect excuse right there, yeah? ..just go with it

var elem = document.getElementById('two');
var two = new Two({ width: 500, height: 500}).appendTo(elem);

function nutrient(x, y) {
  this.o = new Two.Vector(x, y);
  this.c = two.makeCircle(x, y, 1);
  this.c.noFill();
  // get a two-digit decimal b/t 0 and 1
  this.nutrition = Math.trunc(Math.random() * 100) / 100;
  this.x = x;
  this.y = y;
}

var nutritionMatrix = [];

for (var y = 0, yMax = 500; y <= 500; y += 25) {
  nutritionMatrix[y] = [];
}

for (var x = 0, xMax = 500; x <= xMax; x += 25) {
  for (var y = 0, yMax = 500; y <= yMax; y += 50) {
    nutritionMatrix[y].push(new nutrient(x, y));
  }
}
for (var x = 12.5, xMax = 487.5; x <= xMax; x += 25) {
  for (var y = 25, yMax = 475; y <= yMax; y += 50) {
    nutritionMatrix[y].push(new nutrient(x, y));
  }
}

function findNextPoint(point) {
  // find the next point to draw to
  var currentRow = nutritionMatrix[point.y];
  var currentIndex = currentRow.indexOf(point);
  var nextRow = nutritionMatrix[point.y - 25];
  if (!nextRow) {
    return false;
  }
  var targets;
  // odd-numbered rows, inc the last
  if (point.x % 1 === 0) {
    targets = [currentIndex - 1, currentIndex];
  } else {
    targets = [currentIndex, currentIndex + 1];
  }
  var left = nextRow[targets[0]];
  var right = nextRow[targets[1]];
  if (left && right) {
    return nextRow[targets[Math.round(Math.random())]];
  } else if (left) {
    return left;
  } else {
    return right;
  }
  
}

function Line(initialOrigin) {
  this.reset = false;
  this.group = two.makeGroup();

  this.retarget = () => {
    this.origin = this.target;
    this.target = findNextPoint(this.origin);
    if (!this.target) {
      this.reset = true;
    }
  };
  this.drawSegment = () => {
      this.newSegment = two.makeLine(this.origin.x, this.origin.y, this.target.x, this.target.y);
      this.newSegment.subdivide();
      this.newSegment.ending = 0;
      var stop1 = new Two.Stop(0, 'white', this.origin.nutrition);
      var stop2 = new Two.Stop(1, 'white', this.target.nutrition);
      var cX = (this.target.x + this.origin.x) / 2;
      var cY = (this.target.y + this.origin.y) / 2;
      this.newSegment.stroke = new Two.LinearGradient(cX - this.target.x, cY - this.target.y, cX - this.origin.x, cY - this.origin.y, [stop1, stop2]);
      if (this.origin.nutrition < .15) {
        this.origin.nutrition += .05;
      } else if (this.origin.nutrition > .75) {
        this.origin.nutrition -= .05;
      } else {
        if (Math.random() > .5) {
          this.origin.nutrition += .05;
        } else {
          this.origin.nutrition -= .05;
        }
      }
      this.animating = true;
  };
  this.finishDrawing = () => {
    this.group.add(this.newSegment);
    this.animating = false;
  }
  this.addNewSegment = () => {
    this.retarget();
    if (this.reset === true) {
      this.init();
    } else {
      this.drawSegment();
    }
  }
  this.init = () => {
    this.reset = false;
    this.origin = newOrigin();
    this.target = findNextPoint(this.origin);
    this.drawSegment();
  }
  this.remove = () => {
    two.remove(this.newSegment);
    two.remove(this.group);
  }
  this.init();
}

function newOrigin() {
  // find a random nutrient point from the lowest row
  var lastRow = nutritionMatrix[500];
  var pointFromRow = lastRow[Math.floor(Math.random() * lastRow.length)];
  return pointFromRow;
}

var maxLines = 6;
var lines = [];
lines.push(new Line(newOrigin()));

two.bind('update', function(frameCount, timeDelta) {
  if (frameCount % 260 === 0) {
    lines.unshift(new Line(newOrigin()));
    if (lines.length >= maxLines) {
      lines[lines.length - 1].removing = true;
    }
  }
  for (var i = 0, max = lines.length; i < max; i++) {
    if (lines[i].animating && !lines[i].removing) {
      if (lines[i].newSegment.ending < 1) {
        lines[i].newSegment.ending += .1;
      } else {
        lines[i].finishDrawing();
        lines[i].addNewSegment();
      }
    }
  }
  if (lines[lines.length - 1].removing === true) {
    if (lines[lines.length - 1].group.opacity >= 0) {
      lines[lines.length - 1].group.opacity -= .01;
    } else {
      lines[lines.length - 1].remove();
      lines[lines.length - 1] = null;
      lines.pop();
    }
  }

}).play();
              
            
!
999px

Console