css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              body {
  margin: 0;
  overflow: hidden;
}

canvas {
  position: absolute;
  width: 100%;
  height: 100%;
}
            
          
!
            
              // Initial Setup
const canvas = document.querySelector('canvas');
const c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;


// Variables
let mouse = {
	x: innerWidth / 2,
	y: innerHeight / 2 
};

let ballCount = 750;
let balls = [];
let gravityPos = [];
let friction = .995;
let explosionDistance = 2;
let shouldExplode = false;

const colors = [
  '#81C3D7',
  '#D9DCD6',
  '#3A7CA5',
  '#2F6690'
];

const bgColor = '#16425B';


// Event Listeners
addEventListener("mousemove", function(event) {
	mouse.x = event.clientX;
	mouse.y = event.clientY;
  gravityPos = [mouse.x, mouse.y];
});

addEventListener("mouseout", function(event) {
  gravityPos = [canvas.width / 2, canvas.height / 2];
});

addEventListener("resize", function() {
	canvas.width = innerWidth;	
	canvas.height = innerHeight;

	init();
});

addEventListener("click", function() {
	init();
});


// Utility Functions
function randomIntFromRange(min,max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
}

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

function randomColor(colors) {
	return colors[Math.floor(Math.random() * colors.length)];
}


// Objects
function Ball(px, py, vx, vy, f, radius, color) {
	this.p = [px, py];
  this.v = [vx, vy];
  this.gv = [0, 0];
  this.gp = 0;
	this.radius = radius;
	this.color = color;
  this.f = f;

	this.update = function() {
    // calculate gravity vector
    this.gv = [gravityPos[0] - this.p[0], gravityPos[1] - this.p[1]];
  
    // Calculate gravity intensity
    let a = gravityPos[0] - this.p[0];
    let b = gravityPos[1] - this.p[1];
    this.gp = 1 / (Math.sqrt( a*a + b*b ));   
    
    // Explode if needed
    if (shouldExplode){
      this.v[0] *= randomeFloatFromRange(-10, 10);
      this.v[1] *= randomeFloatFromRange(-10, 10);
    }
    
    // Reduce ball's own velocity with friction
    this.v[0] *= this.f;
    this.v[1] *= this.f;
      
    // Calculate new velocity, add gravity
    this.v[0] += this.gv[0] * this.gp * this.f;
    this.v[1] += this.gv[1] * this.gp * this.f;
    
    // Move
		this.p[0] += this.v[0];
		this.p[1] += this.v[1];
		this.draw();
	};

	this.draw = function() {
    c.save();
		c.beginPath();
		c.arc(this.p[0], this.p[1], this.radius, 0, Math.PI * 2, false);	
		c.fillStyle = this.color;
		c.fill();
		c.closePath();
    c.restore();
	};
}


// Implementation
function init() {
  gravityPos = [canvas.width / 2, canvas.height / 2];
  balls = [];
  for(let i = 0 ; i < ballCount ; i++){
    let rd = randomeFloatFromRange(1, 7);
    let px = randomeFloatFromRange(0, canvas.width / 3) + (canvas.width / 3);
    let py = randomeFloatFromRange(0, canvas.height / 3) + (canvas.height / 3);
    let vx = randomeFloatFromRange(-10, 10);
    let vy = randomeFloatFromRange(-10, 10);
    let f = friction;
    balls.push(new Ball(px, py, vx, vy, f, rd, randomColor(colors)));
  }
}

// Animation Loop
function animate() {
	requestAnimationFrame(animate);
  resetCanvas(bgColor);
  updateShouldExplode();
  for(let i = 0 ; i < balls.length ; i++){
    balls[i].update();
  }
}

init();
animate();

function resetCanvas(color){
  if(color){
    c.save();
    c.fillStyle = color;
    c.fillRect(0, 0, canvas.width, canvas.height);
    c.restore();
  }else{
    c.clearRect(0, 0, canvas.width, canvas.height);
  }
}
  
  function updateShouldExplode(){
    let x = 0;
    let y = 0;
    for(let i = 0 ; i < balls.length ; i++){
      x += balls[i].v[0] < 0 ? balls[i].v[0] * -1 : balls[i].v[0];
      y += balls[i].v[1] < 0 ? balls[i].v[1] * -1 : balls[i].v[1];
    }
    shouldExplode = x / balls.length < explosionDistance && y / balls.length < explosionDistance;
  }
            
          
!
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