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 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.

HTML

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

CSS

              
                body{
	background-color: #111;
	margin: 0;
	padding: 0;
	overflow: hidden;
}
              
            
!

JS

              
                $(function(){

	var myResizeTimer = null;
	var widthWindow = 0;
	var heightWindow = 0;
	var canvas = document.getElementById('canvas');
	var ctx = null;
	var particleArray = [];

	function init(){

		var minRadiusParticle = 5;
		var maxRadiusParticle = 50;
		var speed = 4;
		var density = 500;
		particleArray = [];
		widthWindow = window.innerWidth;
		heightWindow = window.innerHeight;

		var constColors = [
		   	"#4ABDAC", "#FC4A1A", "#F7B733", "#DFDCE3",
		   	"#BFD8D2", "#FEDCD2", "#DF744A", "#DCB239",
		   	"#C0B283", "#DCD0C0", "#F4F4F4", "#FFFFFF",
		   	"#96858F", "#6D7993", "#9099A2", "#D5D5D5",
		   	"#94618E", "#49274A", "#F4DECB", "#F8EEE7",
		   	"#D7CEC7", "#565656", "#76323F", "#C09F80",
		   	"#DDDDDD", "#30AED8", "#984B43", "#EAC67A",
		   	"#6B7A8F", "#F7882F", "#F7C331", "#DCC7AA"
		];

		if (canvas.getContext) {
			ctx = canvas.getContext('2d');
			canvas.setAttribute('width', widthWindow);
			canvas.setAttribute('height', heightWindow);
			var longerSide = Math.max(widthWindow, heightWindow);
			var numParticules = Math.round(((((widthWindow*heightWindow)/longerSide)/100)*density)/maxRadiusParticle);

			for( var i=0 ; i<numParticules ; i++){

				var randomRadius = Math.floor(Math.random()*(maxRadiusParticle-minRadiusParticle)+minRadiusParticle);
				var RandomColor = constColors[ Math.floor(Math.random()*constColors.length) ];
				var x = (Math.random()*(widthWindow-randomRadius*2))+randomRadius;
				var y = (Math.random()*(heightWindow-randomRadius*2))+randomRadius;

				if(i !== 0){
					for( var j=0 ; j<particleArray.length; j++){
						//IF PARTICLES ARE TOUCHING WHEN SPAWNING, RECALCULATE
						if(distance(x, y, particleArray[j].x, particleArray[j].y)-particleArray[j].radius - randomRadius < 0){
							x = (Math.random()*(widthWindow-randomRadius*2))+randomRadius;
							y = (Math.random()*(heightWindow-randomRadius*2))+randomRadius;
							j = -1;
						}
					}
				}

				var particle = new Particle(x, y, speed, randomRadius, RandomColor);
				particleArray.push(particle);
			}

		}
	}

	function distance(x0, y0, x1, y1){
		var distanceX = x1 - x0;
		var distanceY = y1 - y0;

		return Math.sqrt(Math.pow(distanceX,2) + Math.pow(distanceY,2))
	}

	function Particle(X, Y, Speed, Radius, Color){
		this.x = X;
		this.y = Y;
		this.radius = Radius;
		this.originalRadius = Radius;
		this.color = Color;
		this.mass = 1;
		this.velocity = {
			x: (Math.random()-0.5)*Speed,
			y: (Math.random()-0.5)*Speed
		}
		this.collision = false;

		this.draw = function(){
			ctx.beginPath();
			ctx.arc(
				this.x,
				this.y, 
				this.radius, 
				0, 
				2*Math.PI
			);
			ctx.strokeStyle = this.color;
			ctx.stroke();
		}

		this.update = function(){
			if( this.x + this.radius > widthWindow || this.x - this.radius < 0 ){
				this.velocity.x = -this.velocity.x;
			}
			if( this.y + this.radius > heightWindow || this.y - this.radius < 0 ){
				this.velocity.y = -this.velocity.y;
			}

			for(var i=0 ; i<particleArray.length ; i++){
				if(this === particleArray[i]) continue;
				if(distance(this.x, this.y, particleArray[i].x, particleArray[i].y) - this.radius - particleArray[i].radius < 0){
					resolveCollision(this, particleArray[i]);
				}
			}

			this.x += this.velocity.x;
			this.y += this.velocity.y;

			this.draw();
		}

	}

	function animate(){
		requestAnimationFrame(animate);
		ctx.clearRect(0,0,widthWindow,heightWindow);
		for( var i=0 ; i<particleArray.length ; i++){
			particleArray[i].update();
		}
	}

	function rotate(velocity, angle) {
	    const rotatedVelocities = {
	        x: velocity.x * Math.cos(angle) - velocity.y * Math.sin(angle),
	        y: velocity.x * Math.sin(angle) + velocity.y * Math.cos(angle)
	    };

	    return rotatedVelocities;
	}

	function resolveCollision(particle, otherParticle) {

	    const xVelocityDiff = particle.velocity.x - otherParticle.velocity.x;
	    const yVelocityDiff = particle.velocity.y - otherParticle.velocity.y;

	    const xDist = otherParticle.x - particle.x;
	    const yDist = otherParticle.y - particle.y;

	    // Prevent accidental overlap of particles
	    if (xVelocityDiff * xDist + yVelocityDiff * yDist >= 0) {

	        // Grab angle between the two colliding particles
	        const angle = -Math.atan2(otherParticle.y - particle.y, otherParticle.x - particle.x);

	        // Store mass in var for better readability in collision equation
	        const m1 = particle.mass;
	        const m2 = otherParticle.mass;

	        // Velocity before equation
	        const u1 = rotate(particle.velocity, angle);
	        const u2 = rotate(otherParticle.velocity, angle);

	        // Velocity after 1d collision equation
	        const v1 = { x: u1.x * (m1 - m2) / (m1 + m2) + u2.x * 2 * m2 / (m1 + m2), y: u1.y };
	        const v2 = { x: u2.x * (m1 - m2) / (m1 + m2) + u1.x * 2 * m2 / (m1 + m2), y: u2.y };

	        // Final velocity after rotating axis back to original location
	        const vFinal1 = rotate(v1, -angle);
	        const vFinal2 = rotate(v2, -angle);

	        // Swap particle velocities for realistic bounce effect
	        particle.velocity.x = vFinal1.x;
	        particle.velocity.y = vFinal1.y;

	        otherParticle.velocity.x = vFinal2.x;
	        otherParticle.velocity.y = vFinal2.y;

	    }
	}

	window.onresize = function(){
		if(myResizeTimer != null) clearTimeout(myResizeTimer);
    	myResizeTimer = setTimeout(init, 100);
	}

	init();
	animate();

});
              
            
!
999px

Console