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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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="c"></canvas>
              
            
!

CSS

              
                body, html, canvas {
  margin: 0px; 
  padding: 0px; 
  position: absolute; 
  background-color: #343435; 
}
              
            
!

JS

              
                var partNum = 111;
//particle number - change it!

window.requestAnimFrame = (function(){
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        function(callback){
            window.setTimeout(callback, 1000 / 60);
        };
})();

function between(min, max) {
  return Math.random() * (max - min) + min;
}

var c = document.getElementById('c');
var ctx = c.getContext('2d');
//context and id of canvas

var w = window.innerWidth;
var h = window.innerHeight;
//width and height of canvas

c.width = w;
c.height = h;
//setting the width and height for canvas

var mouse = {
  x: w / 2, 
  y: h / 2
};
//mouse position

document.addEventListener('mousemove', function(e){ 
    mouse.x = e.clientX || e.pageX; 
    mouse.y = e.clientY || e.pageY 
}, false);
//finding the mouse position

var particles = [];
for(i = 0; i < partNum; i++) {
  particles.push(new particle);
}
    
//the particle function
function particle() {
  this.x = between(c.width / 2 - 85, c.width / 2 + 85);
  this.y = between(c.height / 2 - 85, c.height / 2 + 85);
  //setting the mouse position to the particle x and y
  
  
  this.vx = Math.random() * 8 - 4;
  this.vy = Math.random() * 8 - 4;
  //random velocity
  
  this.r = 1.5;
  //random radius
  
  this.color = '#fff';
  //only random colors of the variables
}

function draw() {
  requestAnimFrame(draw)
  
  ctx.fillStyle = '#343435';
  ctx.fillRect(0, 0, c.width, c.height)
  
  for(t = 0; t < particles.length; t++) {
    var p = particles[t];
    
    ctx.beginPath();
    ctx.fillStyle = p.color;
    ctx.arc(p.x, p.y, p.r, Math.PI * 2, false);
    ctx.fill();
    //the context of the particle(s)
    
    p.x+=p.vx;
    p.y+=p.vy;
    //setting velocities
    
    p.vx;
    p.vy;
    
    if(p.y < 0) {
      p.vy *= -1;
    }
  
    if(p.y > c.height) {
      p.vy *= -1;
    }

    if(p.x < 0) {
      p.vx *= -1;
    }

    if(p.x > c.width) {
      p.vx *= -1;
    }
    
    for(j = 0; j < particles.length; j++) {
			var pp = particles[j];
			distance(p, pp);
    }
  }
}

function distance(p1, p2) {
	var dist,
		dx = p1.x - p2.x,
		dy = p1.y - p2.y;
	
	dist = Math.sqrt(dx*dx + dy*dy);
			
	// Draw the line when distance is smaller
	// then the minimum distance
  var minDist = 33;
  
	if(dist <= minDist) {
		
		// Draw the line
		ctx.beginPath();
		ctx.strokeStyle = "rgba(255,255,255,"+ (1.2-dist/minDist) +")";
		ctx.moveTo(p1.x, p1.y);
		ctx.lineTo(p2.x, p2.y);
		ctx.stroke();
		ctx.closePath();
		

		var ax = dx/1000,
			ay = dy/1000;
    // Some acceleration for the partcles 
		// depending upon their distance
		
		p1.vx -= ax;
		p1.vy -= ay;
		
		p2.vx += ax;
		p2.vy += ay;
		// Apply the acceleration on the particles
    
     p1.r = 3;
		p2.r = 3;
    
     if(p1.r > 7) {
       p1.r = 7;
     }
     if(p2.r > 7) {
       p2.r = 7;
     }

	} else {
     p1.r = 1.5;
		p2.r = 1.5;
  }
}

draw();
              
            
!
999px

Console