Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <p>Left-click to add new boids, Right-click to clear background.</p>
<p>frame rate: <span id="fr"></span></p>
            
          
!

CSS

            
              
            
          
!

JS

            
              var boids = [];
var fr;

function setup() {
  var canvas = createCanvas(windowWidth, windowHeight);
  fr = select("#fr");
  
  canvas.position(0, 0);
  canvas.style("z-index", "-1");
  
  for (var i = 0; i < 200; i++) {
    boids.push(new Boid());
  }
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}

function mouseDragged() {
  var boid = new Boid();
  boid.position = createVector(mouseX, mouseY);
  boids.push(boid);
}

function draw() {
  background(51);
  for (var i = 0; i < boids.length; i++) {
    boids[i].applyBehaviors(boids);
    boids[i].update();
    boids[i].draw();
  }
  fr.html(nf(frameRate(), 1, 2));
}

function Boid() {
  this.acceleration = createVector(0,0);
  this.velocity = createVector(random(-1, 1), random(-1, 1));
  this.position = createVector(random(width), random(height));
  this.r = 6;
  this.maxspeed = 3;
  this.maxforce = 0.05;
  
  
}

Boid.prototype.applyForce = function(force) {
  this.acceleration.add(force);
}

Boid.prototype.applyBehaviors = function(boids) {
  var sep = this.separate(boids);
  var ali = this.align(boids);
  var coh = this.cohesion(boids);
  sep.mult(1.5);
  this.applyForce(sep);
  this.applyForce(ali);
  this.applyForce(coh);
}

Boid.prototype.seek = function(target) {
  var desired = p5.Vector.sub(target, this.pos);
  desired.normalize();
  desired.mult(this.maxspeed);
  var steer = p5.Vector.sub(desired, this.velocity);
  steer.limit(this.maxforce);
  return steer;
}

Boid.prototype.separate = function(boids) {
  var desiredseparation = 50;
  var steer = createVector();
  var count = 0;
  for (var i = 0; i < boids.length; i++) {
    var other = boids[i];
    var d = p5.Vector.dist(this.position, other.position);
    if ((d > 0) && (d < desiredseparation)) {
      var diff = p5.Vector.sub(this.position, other.position);
      diff.normalize();
      diff.div(d);
      steer.add(diff);
      count++;
    }
  }
  if (count > 0) {
    steer.div(count);
  }
  
  if (steer.mag() > 0) {
    steer.normalize();
    steer.mult(this.maxspeed);
    steer.sub(this.vel);
    steer.limit(this.maxforce);
  }
  return steer;
}

Boid.prototype.align = function(boids) {
  var neighbordist = 100;
  var steer = createVector();
  var count = 0;
  for (var i = 0; i < boids.length; i++) {
    var other = boids[i];
    var d = p5.Vector.dist(this.position, other.position);
    if ((d > 0) && (d < neighbordist)) {
      steer.add(other.vel);
      count++;
    }
  }
  if (count > 0) {
    steer.div(count);
    steer.normalize();
    steer.mult(this.maxspeed);
    steer.sub(this.velocity);
    steer.limit(this.maxforce);
    return steer;
  } else {
    return createVector();
  }
}

Boid.prototype.cohesion = function(boids) {
  var neighbordist = 100;
  var steer = createVector();
  var count = 0;
  for (var i = 0; i < boids.length; i++) {
    var other = boids[i];
    var d = p5.Vector.dist(this.position, other.position);
    if ((d > 0) && (d < neighbordist)) {
      steer.add(other.vel);
      count++;
    }
  }
  if (count > 0) {
    steer.div(count);
    return this.seek(steer);
  } else {
    return createVector();
  }
}

Boid.prototype.update = function() {
  this.velocity.add(this.acceleration);
  this.velocity.limit(this.maxspeed);
  this.position.add(this.velocity);
  this.acceleration.mult(0);
  
  if (this.position.x > width + this.r) {
    this.position.x = -this.r;
  } else if (this.position.x < -this.r) {
    this.position.x = width + this.r;
  }
  
  if (this.position.y > height + this.r) {
    this.position.y = -this.r;
  } else if (this.position.y < -this.r) {
    this.position.y = height + this.r;
  }
}
  
Boid.prototype.draw = function() {
  var angle = this.velocity.heading() + PI/2;
  fill(255);
  stroke(255);
  push();
  translate(this.position.x, this.position.y);
  rotate(angle);
  ellipse(0, 0, this.r*2);
  pop();
}
            
          
!
999px

Console