Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <div><canvas id="stage" width="600" height="500" ></canvas></div>


              
            
!

CSS

              
                #stage, body{
  background-color:#fff;
  max-width:100%;
}

div{
  text-align:center;
  background:#fff;

}
#stage{
    border:1px solid silver;
    margin:0 auto;
}

              
            
!

JS

              
                var c = document.getElementById("stage");
var ctx = c.getContext("2d");

var centerX = c.width / 2;
var centerY = c.height / 2;
var radius = 10;
var numCircles = 8;
var numParticles = 256;

var pct = 0.0;
var animLength = 7;
var lineLength = 16;

var stop = false;
var frameCount = 0;
//var $results=$("#results");
var fps, fpsInterval, startTime, now, then, elapsed;

// initialize the timer variables and start the animation
startAnimating(60);

function startAnimating(fps) {
    fpsInterval = 1000 / fps;
    then = Date.now();
    startTime = then;
    animate();
}

function Orb() {
  this.r = 0;  
  this.p = {};
    this.v = {};
    this.a = {
        x: 0,
        y: 0
    };
    this.tail = [];
    this.ageo = 0;
  this.id = -1;
}

Orb.prototype.init = function(_x, _y) {
    this.p.x = _x;
    this.p.y = _y;

    var f = Math.random() * 4;

    var angle = Math.random() * Math.PI * 2;

    //this.v.x = Math.cos(angle) * f;
    //this.v.y = Math.sin(angle) * f;
  this.v.x = this.v.y = 0;
    this.age = 0;
}
Orb.prototype.update = function() {

    this.age++;

    this.v.x += this.a.x;
    this.v.y += this.a.y;

    var maxSpeed = 3.0;
    var direction = Math.atan2(this.v.y, this.v.x);

    var fwdSpeed = Math.sqrt(this.v.x * this.v.x + this.v.y * this.v.y);
    if (fwdSpeed > maxSpeed || fwdSpeed < -1 * maxSpeed) {
        var modifier = fwdSpeed / maxSpeed;
        this.v.x /= modifier;
        this.v.y /= modifier;

    }

    this.p.x += this.v.x;
    this.p.y += this.v.y;
    // floor
    if (this.p.y > c.height) {
        this.p.y -= this.v.y;
        this.v.y *= -Math.random();
        this.v.x *= Math.random() / 2 + 0.5;
    }
   // ceiling
    if (this.p.y < 0) {
        this.p.y -= this.v.y;
        this.v.y *= -Math.random();
        this.v.x *= Math.random() / 2 + 0.5;
    }
      // right wall
    if (this.p.x > c.width) {
        this.p.x -= this.v.x;
        this.v.x *= -Math.random();
        this.v.y *= Math.random() / 2 + 0.5;
    }
   // left wall
    if (this.p.x < 0) {
        this.p.x -= this.v.x;
        this.v.x *= -Math.random();
        this.v.y *= Math.random() / 2 + 0.5;
    }

    this.v.x *= 0.99;
    this.v.y *= 0.99;
  
  this.r = Math.atan2(this.v.y,this.v.x);
  
}
Orb.prototype.draw = function() {
    ctx.beginPath();
    ctx.arc(this.p.x, this.p.y, 2, 0, 2 * Math.PI, false);
  var c = 0;
  var c2 = 0;  
  //var c = Math.min(255-this.age * 8, 255);
    //var c2 = Math.min(255-this.age * 16, 255);
  //  ctx.fillStyle = "rgba(" + c + "," + c + "," + c + ",0.5)";
  //ctx.fillStyle = "rgba(0,0,0,1.0)";  
  ctx.fill();
    ctx.beginPath();
    ctx.moveTo(this.p.x, this.p.y);
  ctx.lineTo(this.p.x+Math.cos(this.r)*5,this.p.y+Math.sin(this.r)*5);
    for (var j = 1; j < this.tail.length; j++) {

        ctx.lineTo(this.tail[j].x, this.tail[j].y);

    }
    ctx.lineWidth = 1;
    ctx.strokeStyle = "rgba(" + c2 + "," + c2 + "," + c2 + ",1.0)";
    ctx.stroke();
    this.tail.unshift({
        x: this.p.x,
        y: this.p.y
    });
    if (this.tail.length > lineLength) this.tail.pop();

}
function steer(){
  var steerCount = 0;
  var steerVec = {
    x:0,
                      y:0
  };
  for(var i=0;i<numParticles;i++){
    for(var j=0;j<i;j++){
      var theDist = lineDistance(orbs[i].p,orbs[j].p);
      if(theDist<15){

      steerCount++;
      steerVec.x += orbs[j].v.x;
      steerVec.y += orbs[j].v.y;
                                                              
                                                              
       
        


        
        
        
      }
          if(steerCount>0){
        orbs[i].v.x += steerVec.x/steerCount*0.0008;
        orbs[i].v.y += steerVec.y/steerCount*0.0008;
        }
    }
  }
}
function avoid(){
  
  for(var i=0;i<numParticles;i++){
    for(var j=0;j<i;j++){
      var theDist = lineDistance(orbs[i].p,orbs[j].p);
      if(theDist<10){
      var theAngle = Math.atan2(orbs[j].p.y-orbs[i].p.y,orbs[j].p.x-orbs[i].p.x);
        var xa = Math.cos(theAngle);
        var ya = Math.sin(theAngle);
        
        orbs[i].v.x -= xa*5/(theDist*theDist);
        orbs[i].v.y -= ya*5/(theDist*theDist);
        orbs[j].v.x += xa*5/(theDist*theDist);
        orbs[j].v.y += ya*5/(theDist*theDist);
        
        
        
      }
    }
  }
  
}

function cohesion(){
  for(var i=0;i<numParticles;i++){
    for(var j=0;j<i;j++){
      var theDist = lineDistance(orbs[i].p,orbs[j].p);
      if(theDist<50){
      var theAngle = Math.atan2(orbs[j].p.y-orbs[i].p.y,orbs[j].p.x-orbs[i].p.x);
        var xa = Math.cos(theAngle);
        var ya = Math.sin(theAngle);
        
        orbs[i].v.x += xa*40/(theDist*theDist);
        orbs[i].v.y += ya*40/(theDist*theDist);
        orbs[j].v.x -= xa*40/(theDist*theDist);
        orbs[j].v.y -= ya*40/(theDist*theDist);
        
        
        
      }
    }
  }
}

function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return {
        x: evt.clientX - rect.left,
        y: evt.clientY - rect.top
    };
}

var orbs;

function init(_x, _y) {
    orbs = [];
    for (var i = 0; i < numParticles; i++) {
        var o = new Orb();
        o.init(_x+Math.random()*10, _y+Math.random()*10);
      o.id = i;
        orbs.push(o);
    }
}
init(c.width / 2, c.height / 2);

c.addEventListener('mousedown', function(evt) {
    var mousePos = getMousePos(c, evt);

    init(mousePos.x, mousePos.y);
});

function animate() {

    // stop
    if (stop) {
        return;
    }

    // request another frame

    requestAnimationFrame(animate);

    // calc elapsed time since last loop

    now = Date.now();
    elapsed = now - then;

    // if enough time has elapsed, draw the next frame

    if (elapsed > fpsInterval) {

        // Get ready for next frame by setting then=now, but...
        // Also, adjust for fpsInterval not being multiple of 16.67
        then = now - (elapsed % fpsInterval);
        frameCount++;
        // draw stuff here
        drawStuff();

    }
}

function drawStuff() {

    ctx.clearRect(0, 0, c.width, c.height);

  avoid();
  steer();
  
    for (var i = 0; i < numParticles; i++) {
        var o = orbs[i];



       // o.a.x += Math.cos(Math.atan2(a.p.y - o.p.y, a.p.x - o.p.x)) * accel;
        //o.a.y += Math.sin(Math.atan2(a.p.y - o.p.y, a.p.x - o.p.x)) * accel;

        o.update();
    }

    for (var i = 0; i < numParticles; i++) {
        var o = orbs[i];
        o.draw();

    }

}


function lineDistance( point1, point2 )
{
  var xs = 0;
  var ys = 0;
 
  xs = point2.x - point1.x;
  xs = xs * xs;
 
  ys = point2.y - point1.y;
  ys = ys * ys;
 
  return Math.sqrt( xs + ys );
}
              
            
!
999px

Console