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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16/p5.min.js"></script>
              
            
!

CSS

              
                html,
body {
  margin: 0;
  padding: 0;
}
              
            
!

JS

              
                var points      = [];
var proximity   = null;
var maxPoints   = null;
var zoom        = null;
var resetCount  = 0;
var resetLimit  = null;

function setup() {
  //  Set the canvas size
  createCanvas(windowWidth, windowHeight);
  doReset();
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
  doReset();
}

function draw() {
  
  //  Move the dots
  for (i = 0; i < points.length; i++) {
    points[i].x += points[i].xInc * zoom;
    points[i].y += points[i].yInc * zoom;
    if (points[i].x >= windowWidth)   points[i].xInc = random(-1);
    if (points[i].x <= 0)             points[i].xInc = random( 1);
    if (points[i].y >= windowHeight)  points[i].yInc = random(-1);
    if (points[i].y <= 0)             points[i].yInc = random( 1);
  }
  
  var point1      = null;
  var point2      = null;
  var newColour   = null;
  var d           = null;
  
  //  Loop through all the points
  for (m = 0; m < points.length; m++) {
    //  Make a note of the current point
    point1 = points[m];
    
    //  Loop thru them all again so we can compare them
    for (n = 0; n < points.length; n++) {

      //  Don't bother if they are the same point
      if (m == n) continue;
      point2 = points[n];
      
      //  Get the distance between them
      d = dist(point1.x, point1.y, point2.x, point2.y);
      
      //  If they are close enough, then we draw them
      if (d <= proximity) {
        
        //  The colour is somewhere between the two point's colour values
        newColour = lerpColor(point1.c, point2.c, map(d,0,proximity,0,1));
        stroke(newColour);
        line(point1.x, point1.y, point2.x, point2.y);
      }
    }
    
  }
  
  resetCount++;
  if (resetCount >= resetLimit) {
    resetCount = 0;
    doReset();
  }
  
}

function setProximity() {
  if (windowWidth > windowHeight) {
    proximity = random(windowHeight*0.1, windowHeight*0.66);
  } else {
    proximity = random(windowWidth*0.1, windowWidth*0.66);
  }  
}

function doReset() {
  background(0);
  setProximity();
  points = [];
  maxPoints = random(8,24);
  resetLimit = random(300, 1200);
  zoom = random(1,8);
  
  //  Create the points
  var x = null;
  var y = null;
  var xInc = null;
  var yInc = null;
  var c = null;
  for (var i = 0; i < maxPoints; i++) {
    console.log(i);
    x     = random(windowWidth);
    y     = random(windowHeight);
    xInc  = random(-1, 1);
    yInc  = random(-1, 1);
    if (i % 3 == 0) {c = color(255,255,0, 8);}
    if (i % 3 == 1) {c = color(0,255,255, 8);}
    if (i % 3 == 2) {c = color(255,0,255, 8);}
    points.push(
      {
        x: x,
        y: y,
        xInc: xInc,
        yInc: yInc,
        c: c
      }
    )    
  }
}

              
            
!
999px

Console