#Particle Sim

In this guide, we're going to build a basic particle simulation in Canvas using simple principles of animation.

We will want to set up an array of particles with accelerations and velocities. We will create 100 particles at random points on the canvas.

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

var particles = [];
for(var i=0; i<100; i++) {
  particles.push(
    {
      x:Math.random()*canvas.width,
      y:Math.random()*canvas.height,
      vx:0,
      vy:0,
      ax:0,
      ay:0
    }
  );
}

In our draw loop, we render these particles.

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  for(var i=0; i<particles.length; i++) {
    // update state

    // render state
    ctx.beginPath();
    ctx.arc(particles[i].x, particles[i].y, 5, 0, 2*Math.PI);
    ctx.fill();
  }

  window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);

Now, all we need to do is update velocity and acceleration each frame. We will add the acceleration to the velocity and add the velocity to the position.

function draw() {
  for(var i=0; i<particles.length; i++) {
    // update state
    particles[i].x+=particles[i].vx;
    particles[i].y+=particles[i].vy;
    particles[i].vx+=particles[i].ax;
    particles[i].vy+=particles[i].ay;

    /* render code */
  }

  window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);

That's it! All we need to do to is create a force somewhere. Let's do it with a click listener.

canvas.addEventListener("click", function(e) {
  var clickX = e.clientX-canvas.offsetLeft;
  var clickY = e.clientY-canvas.offsetTop;
  for(var i=0; i<particles.length; i++) {
    var delx = particles[i].x-clickX;
    var dely = particles[i].y-clickY;
    var magnitudeSquared = Math.pow(delx, 2)+Math.pow(dely, 2);

    particles[i].ax = 0.1*delx/magnitudeSquared;
    particles[i].ay = 0.1*dely/magnitudeSquared;

  }
});

This adds force via the inverse-square law. See this page for more details.

That's all! The final codepen:

See the Pen Particle Sim (FCC) by Alan Luo (@alanluo) on CodePen.