cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              * {
  margin: 0;
  padding: 0;
}
body {
  background: #111;
}
canvas {
  margin: 0 auto;
  display: block;
  border-left: 0;
  border-right: 0;
}
p {
  position: absolute;
  margin: 10px;
  color: #ffffff;
}
            
          
!
            
              // https://twitter.com/msvaljek
// https://msvaljek.blogspot.com/2013/08/canvas-random-spherical-effects.html

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

// helper functions
function randomMax(max) {
	return ~~(Math.random() * max);
}
function getParticleColor() {
	var r = (155 + randomMax(100));
	var g = (155 + randomMax(100));
	var b = (155 + randomMax(100));

	return 'rgb(' + r + ',' + g + ',' + b +  ')';
}
function lightenColor(color, percent) {
	var num = parseInt(color.slice(1),16), amt = Math.round(2.55 * percent), R = (num >> 16) + amt, B = (num >> 8 & 0x00FF) + amt, G = (num & 0x0000FF) + amt;
	return "#" + (0x1000000 + (R<255?R<1?0:R:255)*0x10000 + (B<255?B<1?0:B:255)*0x100 + (G<255?G<1?0:G:255)).toString(16).slice(1);
}

// dom stuff and fps counter
var canvas = document.getElementById('mainCanvas');
var fpsOut = document.getElementById('fps');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext('2d');
var fps = 0, now, lastUpdate = (new Date())*1 - 1, fpsFilter = 50;

// globals
var numParticles = 300,
	angleSpeedx = 0.03,
	angleSpeedy = 0.0005,
	particleSize = 3,
	defaultColor = '#74C6F2',
	centerx = ~~(canvas.width / 2),
	centery = ~~(canvas.height / 2),
	sphereRadius = 140,
	xAngleSpacing = 0.09,
	yAngleSpacing = 0.08,
	spacingMemoryY = 0,
	spacingMemoryX = 0,
	singlecolor = false,
	trailLength = 0.08,
	pulseCycle = 100,
	pulse = true,
	pulsePercentage = 60,
	pulseCounter = 0,
	pulseInc = 1;

var Particle = function () {
	spacingMemoryX += xAngleSpacing;
	spacingMemoryY += yAngleSpacing;
	this.xangle = spacingMemoryX;
	this.yangle = spacingMemoryY;
	this.color = getParticleColor();
};

Particle.prototype.draw = function () {
	this.xangle += angleSpeedx;
	this.yangle += angleSpeedy;

	ctx.beginPath();
	ctx.fillStyle = singlecolor ? defaultColor : this.color;
	if (pulse) {
		ctx.fillStyle = lightenColor(ctx.fillStyle, -(pulseCounter / pulseCycle) * pulsePercentage);
	}
	var sizeFactor = (0.5 + (Math.cos(this.xangle) + 1) / 2);
	ctx.arc(
		centerx + ~~((Math.sin(this.xangle) * Math.cos(this.yangle)) * sphereRadius),
		centery + ~~((Math.sin(this.xangle) * Math.sin(this.yangle)) * sphereRadius),
		particleSize * sizeFactor, 0, Math.PI * 2);
	ctx.fill();
};

var ParticleSystem = function () {
	this.particles = [];
	for (var i = 0; i < numParticles; i++) {
		this.particles.push(new Particle(canvas.height * Math.random()));
	}
};
ParticleSystem.prototype.draw = function () {

	if (pulse) {
		pulseCounter += pulseInc;
		if (pulseCounter >= pulseCycle) {
      pulseCounter = pulseCycle;
			pulseInc = -1;
		}
		if (pulseCounter <= 0) {
			pulseInc = 1;
		}
	}

	for (var i = 0; i < this.particles.length; i++) {
		this.particles[i].draw();
	}

};
function refreshColor() {
	for (var i = 0; i < particleSystem.particles.length; i++) {
		particleSystem.particles[i].color = singlecolor ? defaultColor : getParticleColor();
	}
}

var gui = new dat.GUI();

var f1 = gui.addFolder('Configure Motion');
f1.close();

f1.add(window, 'numParticles').min(10).max(1000).step(1).name('Num. Particles').onFinishChange(function() {
	particleSystem = new ParticleSystem();
});
f1.add(window, 'angleSpeedx').min(-0.9).max(0.9).step(0.0005).name('X Speed');
f1.add(window, 'angleSpeedy').min(-0.9).max(0.9).step(0.0005).name('Y Speed');
f1.add(window, 'particleSize').min(3).max(10).name('Size');
f1.add(window, 'sphereRadius').min(50).max(600).name('sphereRadius');
f1.add(window, 'xAngleSpacing').min(0.005).max(0.1).step(0.005).name('xAngleSpacing').onFinishChange(function() {
	particleSystem = new ParticleSystem();
});
f1.add(window, 'yAngleSpacing').min(0.005).max(0.1).step(0.005).name('yAngleSpacing').onFinishChange(function() {
	particleSystem = new ParticleSystem();
});

var f2 = gui.addFolder('Configure Color');
f2.add(window, 'singlecolor').name('Single Color').onFinishChange(refreshColor);
f2.addColor(window, 'defaultColor').name('Default Color').onFinishChange(refreshColor);
f2.add(window, 'trailLength').min(0.001).max(0.4).step(0.001).name('Trail fading');
f2.add(window, 'pulse').name('Pulse');
f2.add(window, 'pulseCycle').min(20).max(500).step(1).name('Pulse cycle');
f2.add(window, 'pulsePercentage').min(20).max(100).step(1).name('Pulse %');
f2.open();

var particleSystem = new ParticleSystem();

window.onresize = function() {
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	particleSystem = new ParticleSystem();
	centerx = ~~(canvas.width / 2);
	centery = ~~(canvas.height / 2);
};

(function animloop(){
	requestAnimFrame(animloop);
	ctx.fillStyle = 'rgba(1, 1, 1, ' + trailLength + ')';
	ctx.fillRect(0, 0, canvas.width, canvas.height);
	particleSystem.draw();
	var thisFrameFPS = 1000 / ((now=new Date()) - lastUpdate);
	fps += (thisFrameFPS - fps) / fpsFilter;
	lastUpdate = now;
})();

setInterval(function(){
  fpsOut.innerHTML = fps.toFixed(1) + " fps";
}, 1000);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console