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.

            
              body {
  background-color: #1D1F20;
  overflow: hidden;
  margin: 0
}

#canvas {
  position: absolute;
}
            
          
!
            
              // Click to reset the image with new random variables.
// You can vary the number of circles, vertices, spread, or other basic variables here.
var numberOfCircles = 4;
var numberOfVertices = 40;
var vertexSpread = 3;
var radialVariance = 30;
var radialSpeed = 1;
var angularSpeed = 0.008;
var phiVariance = Math.PI / 6;
var time = 0;

const mainColors = ['#B200A9', '#FF19F2', '#40003C', '#630063'];
const highlightColors = ['#4FFF00', '#37B200', '#184F00', '#184F00', '#103300'];
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
canvas.addEventListener('click', reset);

class Circle {
  // Phi controls the angle of the circles that are created. Color is currently not used.
  constructor(x, y, radius, color, phi) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.initialRadius = radius;
    this.radialVelocity = radialSpeed * Math.random();
    this.maxRadialSpeed = this.radialVelocity;
    this.radialAcceleration = 0;
    this.angularVelocity = angularSpeed * Math.random();
    this.maxAngularSpeed = this.angularVelocity;
    this.angularAcceleration = 0;
    this.color = color;
    this.phi = phi;
    this.initialPhi = phi;
    this.vertices = this.generateVertices();
  }
  
  generateVertices() {
    let vertices = [];
    for(var i = 0; i < numberOfVertices; i++) {
      vertices.push(new Vertex(
        this.x + this.radius * Math.cos(this.phi + i * 2 * Math.PI / numberOfVertices),
        this.y + this.radius * Math.sin(this.phi + i * 2 * Math.PI / numberOfVertices),
        this.phi
      ));
    }
    return vertices;
  }
  
  updateVertices() {
    for(var i = 0; i < numberOfVertices; i++) {
      this.vertices[i].x = this.x + this.radius * Math.cos(this.phi + i * 2 * Math.PI / numberOfVertices);
      this.vertices[i].y = this.y + this.radius * Math.sin(this.phi + i * 2 * Math.PI / numberOfVertices);
      this.vertices[i].phi = this.phi;
    }
  }
  
  updateCircle() {
    if (this.radius > this.initialRadius + radialVariance) {
      this.radialAcceleration = -.02;
      this.radialVelocity += this.radialAcceleration;
    } else if (this.radius < this.initialRadius) {
      this.radialAcceleration = .02;
      this.radialVelocity += this.radialAcceleration;
    }
    if (this.phi > this.initialPhi + phiVariance) {
      this.angularAcceleration = -.0001;
      this.angularVelocity += this.angularAcceleration;
    } else if (this.phi < this.initialPhi) {
      this.angularAcceleration = .0001;
      this.angularVelocity += this.angularAcceleration;
    }
    if (Math.abs(this.radialVelocity) < this.maxRadialSpeed) {
      this.radialVelocity += this.radialAcceleration;
    }
    if (Math.abs(this.AngularVelocity) < this.maxAngularSpeed) {
      this.angularVelocity += this.angularAcceleration;
    }
    this.radius += this.radialVelocity;
    this.phi += this.angularVelocity;
    // this.vertices = this.generateVertices();
    this.updateVertices();
    this.drawCircle();
  }
  
  drawCircle() {
    for (var i = 0; i < numberOfVertices; i++) {
      var v1 = this.vertices[i];
      var v2 = this.vertices[(i + 1) % numberOfVertices];
          
      const gradient = context.createLinearGradient(v1.x, v1.y, v2.x, v2.y);
      gradient.addColorStop(0, mainColors[2]);
      gradient.addColorStop(0.5, mainColors[0]);
      gradient.addColorStop(1, mainColors[2]);
      
      context.beginPath();
      context.lineWidth = .25;
      context.strokeStyle = gradient;
      context.moveTo(v1.x, v1.y);
      context.lineTo(v2.x, v2.y);
      context.stroke();
      context.closePath();
    }
  }
}

class Vertex {
  constructor(x, y, phi) {
    this.x = x;
    this.y = y;
    this.phi = phi;
  }
}

let circles = [];

function start() {
  context.translate(0.5, 0.5);
  addCircles();
  
  window.requestAnimationFrame(draw);
}

function addCircles() {
  var radius = 30 + 80 * Math.random();
  for(var i = 0; i < numberOfCircles; i++) {
    circles.push(new Circle(canvas.width/2, canvas.height/2, radius, mainColors[i % mainColors.length], 0));
    radius += 40 + 80 * Math.random();
  }
}

function draw() {
  context.clearRect(0, 0, canvas.width, canvas.height);
  for (var i = 0; i < numberOfCircles; i++) {
    circles[i].drawCircle();
    circles[i].updateCircle();
    
    if (i != numberOfCircles - 1) {
      drawConnections(circles[i], circles[i+1]);
    }
  }
  
  time++;
  window.requestAnimationFrame(draw);
}

function drawConnections(c1, c2) {
  for (var i = 0; i < numberOfVertices; i++) {
    var v1 = c1.vertices[i];
    
    for (var j = -vertexSpread; j <= vertexSpread; j++) {
      var k = i + j;
      if (k < 0) { k += numberOfVertices; }
      var v2 = c2.vertices[k % numberOfVertices];
      const gradient = context.createLinearGradient(v1.x, v1.y, v2.x, v2.y);
      gradient.addColorStop(0, highlightColors[4]);
      gradient.addColorStop(0.5, highlightColors[1]);
      gradient.addColorStop(1, highlightColors[4]);
      
      context.beginPath();
      context.lineWidth = .5;
      context.strokeStyle = gradient;
      context.moveTo(v1.x, v1.y);
      context.lineTo(v2.x, v2.y);
      context.stroke();
      context.closePath();
    }
  }
}

start();

window.onresize = function() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}

function reset() {
  time = 0;
  circles = [];
  addCircles();
}
            
          
!
999px
Loading ..................

Console