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.

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

              
                <canvas id="canvas"></canvas>
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}
              
            
!

JS

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

'floor|random|round|abs|sqrt|PI|atan2|sin|cos|pow|max|min|hypot'
  .split('|')
  .forEach(function(p) { window[p] = Math[p]; });

var TAU = PI*2;

function r(n) { return random()*n; }
function rrng(lo, hi) { return lo + r(hi-lo); }
function rint(lo, hi) { return lo + floor(r(hi - lo + 1)); }
function choose(args) { return args[rint(0, args.length-1)]; }

/*---------------------------------------------------------------------------*/

var W, H, frame, time, DPR;

function dpr(n) { return DPR*n; }

function resize() {
  var w = innerWidth;
  var h = innerHeight;
  DPR = devicePixelRatio || 1;

  canvas.style.width = w+'px';
  canvas.style.height = h+'px';

  W = canvas.width = w * DPR;
  H = canvas.height = h * DPR;
}

function loop(t) {
  frame = requestAnimationFrame(loop);
  draw();
  time++;
}

function pause() {
  cancelAnimationFrame(frame);
  frame = frame ? null : requestAnimationFrame(loop);
}

function reset() {
  cancelAnimationFrame(frame);
  resize();
  ctx.clearRect(0, 0, W, H);
  init();
  time = 0;
  frame = requestAnimationFrame(loop);
}

/*---------------------------------------------------------------------------*/

function Vector(x, y) {
  this.x = x;
  this.y = y;
}

Vector.add = function(a, b) { return new Vector(a.x + b.x, a.y + b.y); };
Vector.sub = function(a, b) { return new Vector(a.x - b.x, a.y - b.y); };
Vector.mul = function(a, n) { return new Vector(a.x * n, a.y * n); };
Vector.div = function(a, n) { return new Vector(a.x / n, a.y / n); };

Vector.prototype.clone = function() {
  return new Vector(this.x, this.y);
};

Vector.prototype.add = function(other) {
  this.x += other.x;
  this.y += other.y;
  return this;
};

Vector.prototype.sub = function(other) {
  this.x -= other.x;
  this.y -= other.y;
  return this;
};

Vector.prototype.mul = function(n) {
  this.x *= n;
  this.y *= n;
  return this;
};

Vector.prototype.div = function(n) {
  this.x /= n;
  this.y /= n;
  return this;
};

Vector.prototype.mag = function() {
  var x = this.x, y = this.y;
  return sqrt(x*x + y*y);
};

Vector.prototype.normalize = function() {
  return this.div(this.mag());
};

Vector.prototype.constrain = function(n) {
  var m = this.mag();
  return m > n ? this.mul(n/m) : this;
};

Vector.prototype.zero = function() {
  this.x = 0;
  this.y = 0;
};

function vec(x, y) {
  return new Vector(x, y);
}

/*---------------------------------------------------------------------------*/

function Particle(max, loc, target) {
  this.start = loc.clone();
  this.loc = loc;
  var t = r(TAU);
  this.vel = vec(max*cos(t), max*sin(t));
  this.max = max;
  this.acc = vec(0, 0);
  this.target = target;
}

Particle.prototype.applyForce = function(force) {
  this.acc.add(force);
};

Particle.prototype.seekTarget = function() {
  var desired = Vector.sub(this.target, this.loc);
  var steer = Vector.sub(desired, this.vel);
  this.applyForce(steer.constrain(this.max*0.051));
};

Particle.prototype.deviate = function() {
  var x = this.loc.x;
  var y = this.loc.y;
  x = L + round((x - L) / Step) * Step;
  y = T + round((y - T) / Step) * Step;
  var desired = Vector.sub(vec(x, y), this.loc);
  var steer = Vector.sub(desired, this.vel);
  this.applyForce(steer.constrain(this.max*0.05));
};

Particle.prototype.move = function() {
  this.vel.add(this.acc).constrain(this.max);
  this.loc.add(this.vel);
  this.acc.zero();
};

Particle.prototype.draw = function() {
  ctx.moveTo(this.loc.x, this.loc.y);
  ctx.lineTo(W/2, H/2);
};


/*---------------------------------------------------------------------------*/


var P, particles;
var L, R, T, B;
var Grid;
var Step 
var Density = 1;
var Dim;

function init() {
  Dim = dpr(300);
  Step = dpr(100);
  L = W/2-(Dim);
  R = W/2+(Dim);
  T = H/2-(Dim);
  B = H/2+(Dim);

  particles = [];
  
  for (var x = L; x <= R; x += Step) {
    for (var y = T; y <= B; y += Step) {
      for (var d = rint(0, Density); d > 0; d--) {
        particles.push(new Particle(dpr(1), 
                                    vec(x, y), 
                                    vec(L + rint(0, (Dim*2)/Step)*Step,
                                        T + rint(0, (Dim*2)/Step)*Step)));
        
      }
    }
  }

  ctx.strokeStyle = black;
  ctx.fillStyle = 'white';
  ctx.fillRect(0, 0, W, H);
  ctx.fillStyle = 'blue';
}

var black = 'rgba(0, 0, 0, 0.1)';

function draw() {
  ctx.beginPath();
  for (var i = 0; i < particles.length; i++) {
    var p = particles[i];
    p.seekTarget();
    p.deviate();
    p.move();
    p.draw();
  }
  ctx.stroke();
}

/*---------------------------------------------------------------------------*/

document.onclick = pause;
document.ondblclick = reset;

reset();

              
            
!
999px

Console