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

Quick-add: + add another resource

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.

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: black;
  color: white;
  display: flex;
  align-items: center;
  flex-direction: column;
  margin: 20px 0 0 0;
}
            
          
!
            
              var params = {
  fps: 60,
  maxR: 50,
  minR: 5,
  nP: 130,
  minV: 1,
  maxV: 1,
};
var updates = {
  line: true,
  repel: 0,
};
var renderers = {
  circleMidpoints: true,
  circle: false,
  point: false,
  lengthColor: false,
  radiusColor: false,
};


function sub(p,q) {return {x: p.x - q.x, y: p.y - q.y}}
function add(p,q) {return {x: p.x + q.x, y: p.y + q.y}}
function norm(p) {return Math.sqrt(Math.pow(p.x,2) + Math.pow(p.y,2))}
function normalize(p) {
  return norm(p) < .0001 ? {x:0,y:0} : {x: p.x/norm(p), y: p.y/norm(p)}
}
function rgbaToColor(r,g,b,a) {
  return "rgba("+r+","+g+","+b+","+a+")";
}

var c = document.getElementById("canvas");
c.width = window.innerWidth;
c.height = window.innerHeight - 20;
var ctx = c.getContext("2d");
ctx.strokeStyle = "rgba(255,255,255,.05)";
ctx.fillStyle = "white";
var previous_time = 0;


var circles;

function init(mode) {
  ctx.clearRect(0,0,c.width,c.height);
  circles = Array(params.nP).fill().map((e,i) => {
    var theta = 2 * Math.PI * Math.random();
    var v = params.minV + Math.random() * (params.maxV - params.minV);
    var theta2 = 2 * Math.PI * Math.random();
    var r = Math.min(c.width, c.height)/2 - 20;
    return {
      x: mode === "circle" ? r*Math.cos(theta2) + c.width/2 : c.width * Math.random(),
      y: mode === "circle" ? r*Math.sin(theta2) + c.height/2 : (mode === "line" ? c.height/2 : c.height * Math.random()),
      r: params.minR + Math.random() * (params.maxR - params.minR),
      vy: v * Math.cos(theta),
      vx: v * Math.sin(theta)
    }
  });
}
init("circle");

function update() {
  ctx.strokeStyle = "rgba(255,255,255,.05)";
  var time = (new Date()).getTime()/1000;
  if (time - previous_time > 1/params.fps) {
    previous_time = time;
    var repels = [];
    circles.forEach((p, i) => {
      repels[i] = {x:0, y:0};
      for (var j=i+1; j<circles.length; j++) {
        if (norm(sub(p, circles[j])) < p.r + circles[j].r) {
          repels[i] = add(repels[i], sub(p, circles[j]));
          if (renderers.radiusColor) {
            var v = Math.floor(Math.min(p.r, circles[j].r) * 255/55);
            ctx.strokeStyle = rgbaToColor(v,v,v,255);
          }
          if (renderers.lengthColor) {
            var v = Math.floor(norm(sub(p, circles[j])) * 255/(params.maxR*2));
            ctx.strokeStyle = rgbaToColor(v,v,v,255);
          }
          if (renderers.circleMidpoints) {
            ctx.beginPath();
            ctx.moveTo(p.x,p.y);
            ctx.lineTo(circles[j].x,circles[j].y);
            ctx.stroke();
          }
          if (renderers.circle) {
            ctx.beginPath();
            ctx.arc(p.x,p.y,p.r,0,2*Math.PI);
            ctx.stroke();
          }
          
        }
      }
      if (renderers.point) {
        ctx.fillRect(p.x, p.y, 1, 1);
      }
      if (updates.line) {
        p.x += p.vx; p.y += p.vy;
      }
      if (updates.repel) {
        repels[i] = normalize(repels[i]);
        p.x += updates.repel * repels[i].x; 
        p.y += updates.repel * repels[i].y;
      }
    })
  }
  window.requestAnimationFrame(update);
}

var gui = new dat.GUI();
gui.add(params, "fps", 1, 60);

var seedF = gui.addFolder("Seed (restart buttons)");
seedF.add(params, "nP", 3, 500).step(1);
seedF.add(params, "minR", 0, 50);
seedF.add(params, "maxR", 3, 200);
seedF.add(params, "minV", 0, 5);
seedF.add(params, "maxV", 1, 20);
params.random = ()=>{init()};
seedF.add(params, "random");
params.line = ()=>{init("line")};
seedF.add(params, "line");
params.circle = ()=>{init("circle")};
seedF.add(params, "circle");

var updateF = gui.addFolder("Updates");
updateF.add(updates, "line");
updateF.add(updates, "repel", 0, 10).step(.001);

var renderF = gui.addFolder("Renderers");
renderF.add(renderers, "circleMidpoints");
renderF.add(renderers, "circle");
renderF.add(renderers, "point");
renderF.add(renderers, "lengthColor");
renderF.add(renderers, "radiusColor");


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

Console