cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-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.

            
              #canvas {
  background-color: #212121;
  width: 100%;
  height: 100%;
  display: block;
  top: 0;
  left: 0;
  position: absolute;
}

#time {
	position: absolute;
	display: none;
}
            
          
!
            
              /*
Copyright Simon Bergqvist 2016. 
*/

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var timeText = document.getElementById('time');

var circles = [];
var numCircles = 32;
var circleRadius = 26;
var speed = -0.11;
var movementSpeed = 100;
var formationRadius = 191;
var formationPulseAmplitude = 48;
var friction = 0.95;
var glowSize = 20;

var mouseX = 0;
var mouseY = 0;

var timeLastFrame = Date.now();
var now = Date.now();
var dt = 0;
var timeElapsed = 0;

function Circle(_baseAngle){
	this.x = canvas.width/2;
	this.y = canvas.height/2;
	this.targetX = canvas.width/2;
	this.targetY = canvas.height/2;
	this.velocityX = 0;
	this.velocityY = 0;
	this.acceleration = 20;
	this.baseAngle = _baseAngle;
	this.startAngle = Math.PI*0.25;
	this.endAngle = Math.PI*1.75;
	this.radius = circleRadius;
	this.draw = function() {
		/*DEBUG
		ctx.beginPath();
		ctx.moveTo(this.x, this.y);
		ctx.lineTo(mouseX, mouseY);
		ctx.stroke();
		*/
		
		ctx.beginPath();
		ctx.save();
		ctx.translate(this.x, this.y);
		var dx = mouseX - this.x;
		var dy = mouseY - this.y;
		var h = Math.sqrt(dx*dx + dy*dy);
		var rotatedAngle = Math.acos(dx/h);
		if(mouseY < this.y){
			rotatedAngle = Math.PI*2 - rotatedAngle;
		}
		ctx.rotate(rotatedAngle);
		//outer circle
		ctx.shadowBlur = glowSize;
		ctx.shadowColor = 'hsla('+rotatedAngle*(180/Math.PI)+', 75%, 50%, 1)';
		ctx.strokeStyle = 'hsla('+rotatedAngle*(180/Math.PI)+', 75%, 50%, 1)';
		ctx.arc(0,0, circleRadius, this.startAngle, this.endAngle);
		ctx.stroke();
		//inner circle
		ctx.beginPath();
		ctx.strokeStyle = 'hsla('+rotatedAngle*(180/Math.PI)+', 75%, 50%, 1)';
		ctx.arc(0,0,circleRadius/2, 0, Math.PI*2);
		ctx.stroke();
		//innermost dot
		ctx.beginPath();
		ctx.fillStyle = 'hsla('+rotatedAngle*(180/Math.PI)+', 75%, 50%, 1)';
		ctx.arc(0,0,circleRadius/4, 0, Math.PI*2);
		ctx.fill();
		ctx.restore();
	};
	this.update = function(){
		this.baseAngle += speed * dt;
		/*
		this.targetX = mouseX + Math.cos(this.baseAngle) * (formationRadius + formationPulseAmplitude * Math.sin(timeElapsed));
		this.targetY = mouseY + Math.sin(this.baseAngle) * (formationRadius + formationPulseAmplitude * Math.sin(timeElapsed));
		if(this.targetX > this.x){
			this.velocityX += this.acceleration;
			//this.x += movementSpeed * dt;
		}
		if(this.targetX < this.x){
			this.velocityX -= this.acceleration;
			//this.x -= movementSpeed * dt;
		}		
		if(this.targetY > this.y){
			this.velocityY += this.acceleration;
			//this.y += movementSpeed * dt;
		}
		if(this.targetY < this.y){
			this.velocityY -= this.acceleration;
			//this.y -= movementSpeed * dt;
		}
		this.velocityX = clamp(-movementSpeed, movementSpeed, this.velocityX) * friction;
		this.velocityY = clamp(-movementSpeed, movementSpeed, this.velocityY) * friction;
		this.x += this.velocityX * dt;
		this.y += this.velocityY * dt;*/
		
		
		
		this.x = canvas.width/2 + Math.cos(this.baseAngle) * (formationRadius + formationPulseAmplitude * Math.sin(timeElapsed));
		this.y = canvas.height/2 + Math.sin(this.baseAngle) * (formationRadius + formationPulseAmplitude * Math.sin(timeElapsed));
		
	};
}

function OnMouseMove(e){
  mouseX = e.pageX;
  mouseY = e.pageY;
}

function Initialize(){
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
  canvas.addEventListener("mousemove", OnMouseMove, false);
  window.requestAnimationFrame(Loop);
  
	//set default values for preview
	mouseX = canvas.width/2;
	mouseY = canvas.height/2;
	
	AddCircles();
	var gui = new dat.GUI();
	gui.add(window, "speed", -1, 1);
	gui.add(window, "circleRadius", 10, 40);
	gui.add(window, "formationRadius", 100, 500);
	gui.add(window, "formationPulseAmplitude", 0, 100);
	gui.add(window, "glowSize", 0, 35);
	//gui.add(window, "movementSpeed", 0, 400);
	//gui.add(window, "friction", 0, 1);
}

function AddCircles(){
	var baseAngle = Math.PI*2/numCircles;
	for(var i = 0; i < numCircles; i++){
		circles.push(new Circle(baseAngle*i));
  }
}

function Loop(){
	//time related
	timeLastFrame = now;
	now = Date.now();
	dt = (now - timeLastFrame)/1000;
	timeElapsed += dt;
	timeText.innerHTML = Math.floor(timeElapsed);
	
	canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  window.requestAnimationFrame(Loop);
  Clear();
  for(var i = 0; i < circles.length; i++){
		circles[i].update();
    circles[i].draw(); 
  }
}

function Clear(){
  ctx.clearRect(0,0,canvas.width, canvas.height);
}

function clamp(min, max, value){
  return Math.max(min, Math.min(max, value));
}

Initialize();
            
          
!
999px
Loading ..................

Console