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

              
                <canvas id="canvas"></canvas>
              
            
!

CSS

              
                body {
  background: #171519;
  height: 100vh;
  margin: 0;
  overflow: hidden;
}
              
            
!

JS

              
                //Initializing all our variables
var w = window.innerWidth;
var h = window.innerHeight;

var canvasBody = document.getElementById("canvas");
var ctx = canvasBody.getContext("2d");
var animation = false;

var points = [];
var lines = [];

var count = 25;
var loopIndex = 0;
var speed;

var resetVariables = function() {
  points = [
    new Point(300, 400),
    new Point(200, -50),
    new Point(w-400, h+100),
    new Point(w/33, h+300),
    new Point(w+100, h/2),
  ];
  lines = [];
  loopIndex = 0;
  light.x = 300;
  light.y = 400;
  speed = Math.max(window.innerWidth, window.innerHeight) / 300;
};

var sizeCanvas = function () {
	w = canvasBody.width = window.innerWidth;
	h = canvasBody.height = window.innerHeight;
};

function Point(x, y) {
  this.x = x || Math.random() * (w + 300) - 300;
  this.y = y || Math.random() * (h + 300) - 300;
  this.count = 0;
};

var checkDistance = function (x1, y1, x2, y2) { 
	return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
};

var generatePoints = function () {
  while (points.length < count) {
    var point = new Point();
    var pointValid = true;
    for (var j = 0; j < points.length; j++) {
      if (checkDistance(points[j]['x'], points[j]['y'], point.x, point.y) < 150) {
        pointValid = false;
        break;
      }
    }
    if (pointValid){
      points.push(point);
    }
  }
};

var generateLines = function () {
  for (var i = 0; i < points.length; i++) {
    var connections = [];
    var numberOfConnections = Math.random() * 2 + 3;
    var origin = points[i];
    for (var j = 0; j < points.length; j++) {
      var target = points[j]; 
      
      // Making sure we have an appropriate amount of connections, and that we don't link to ourselves (also avoiding vertical lines)! Also avoiding extremely long lines that span most of the screen for a more 'network' like feel
      if (
        connections.length < numberOfConnections  &&
        target.count < 5 &&
        origin.x != target.x &&
        checkDistance(origin.x, origin.y, target.x, target.y) < ( Math.max(w, h) / 3)
      ) {
        // checking to see that lines with the same origin don't have similar angles to catch most of the line clumps
        var angleTooSmall = false;
        for (var k = 0; k < connections.length; k++) {
          var otherAngle = Math.atan2(connections[k]['target']['y'] - connections[k]['origin']['y'], connections[k]['target']['x'] - connections[k]['origin']['x']);
          var angle = Math.atan2(target.y - origin.y, target.x - origin.x);
          if (Math.abs(otherAngle - angle) < 0.5) {
            angleTooSmall = true;
            break;
          }                               
        }
        if (!angleTooSmall) {
          connections.push({origin: origin, target: target});
          origin.count += 1;
          target.count += 1;
        }
      }
    }
    lines = lines.concat(connections);
  }
};


var drawLines = function () {
  if (lines.length < 1) {
    return generateLines();
  }
  
  ctx.globalCompositeOperation = 'source-over';
  ctx.lineWidth = 4;
  ctx.lineJoin = "miter";
  ctx.miterLimit = 40;
  ctx.strokeStyle = '#333';
  for (var i = 0; i < lines.length; i++){
    var origin = lines[i]['origin'];
    var target = lines[i]['target'];
    ctx.beginPath();
    ctx.moveTo(origin.x, origin.y);
    ctx.lineTo(target.x, target.y);
    ctx.closePath();
    ctx.stroke();
  }
};

var light =  {
  x: 300,
  y: 400,
  length: 80,
  draw: function (angle, transformFactor) {
    // transformFactor is a number from 0.5 to 1.
    var scaleFactor = 1 - Math.pow(transformFactor, 1.5);
    var opacity = 1 - 1.2 * (Math.pow(transformFactor, 2));
    ctx.globalCompositeOperation = 'source-atop';
    ctx.save();
    ctx.translate(this.x, this.y -2);
    ctx.rotate(angle);
    ctx.scale(scaleFactor, 1);
    ctx.beginPath();
    ctx.rect(0, -(Math.PI - Math.abs(angle)) , this.length, 4);
    ctx.closePath();
    // The angle of the gradient needs to be updated for each path, so it needs to be in our draw loop
    var gradient = ctx.createLinearGradient(0, 0, this.length, 4);
    var gradientStops = [
      'rgba(25,18,33,' + opacity + ')',
      'rgba(33,3,123,' + opacity + ')',
      'rgba(38,31,246,' + opacity + ')',
      'rgba(12,93,246,' + opacity + ')',
    ];
    var gradientLength = gradientStops.length
    for (var i = 0; i < gradientLength; i++) {
      var stopLocation = (0.5/gradientLength) * i;
      gradient.addColorStop(stopLocation, gradientStops[i]);
      gradient.addColorStop(1 - stopLocation, gradientStops[i]);
    }
    ctx.fillStyle = gradient;
    ctx.fill();
    ctx.restore();
  }
};

var draw = function () {
  ctx.clearRect(0,0, w, h);
  drawLines();
  var path = lines[loopIndex];
  var origin = path['origin'];
  var target = path['target'];
  var angle = Math.atan2(target.y - origin.y, target.x - origin.x);
  
  // Calculate a transform factor so we can manipulate the light based on how far away it is from either point
  var distanceOrigin = checkDistance(light.x, light.y, origin.x, origin.y);
  var distanceTarget = checkDistance(light.x, light.y, target.x, target.y);
  var totalDistance = checkDistance(origin.x, origin.y, target.x, target.y);
  var transformFactor = (Math.max(distanceOrigin, distanceTarget)) / totalDistance;
  
  light.draw(angle, transformFactor);
  
  //Calculating our new position
  var Sin = Math.sin(angle) * speed;
  var Cos = Math.cos(angle) * speed;
  light.x += Cos;
  light.y += Sin;
  // If we're reaching the end of the path, get a new path
  if (Math.abs(target.x - light.x) < 10 &&
      Math.abs(target.y - light.y) < 10) {
    loopIndex += 1;
    if (loopIndex >= lines.length -1) {
      loopIndex = 0;
    }
    var newOrigin = lines[loopIndex]['origin'];
    light.x = newOrigin.x;
    light.y = newOrigin.y + 4;
    return setTimeout(function () {
      animation = window.requestAnimationFrame(draw)
    }, 500)
  } else {
    animation = window.requestAnimationFrame(draw);
  }
};

var init = function () {
  resetVariables();
  generatePoints();
  generateLines();
  draw();	
};

var handleResize = _.debounce(function(event) {
  if (Math.abs(window.innerHeight - h) < 70) {
    return;
  }
  window.cancelAnimationFrame(animation);
  sizeCanvas();
  ctx.clearRect(0,0, w, h);
  init();
}, 500);

window.onload = function () {
  sizeCanvas();
  init();
};

window.addEventListener("resize", handleResize, false);
              
            
!
999px

Console