2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Particle Sim
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Particle Simulation in Canvas
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const canvas = document.getElementById('canvas');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const ctx = canvas.getContext('2d');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const particles = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								for (let i=0; i< 100 ;  i + + )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  particles.push(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      x: Math.random() * canvas.width,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: Math.random() * canvas.height,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      vx: 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      vy: 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      ax: 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      ay: 0
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In our draw loop, we render these particles.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const draw = () => {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  ctx.clearRect(0, 0, canvas.width, canvas.height);
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  for (let i=0; i< particles.length ;  i + + )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // update state
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // render state
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ctx.beginPath();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ctx.arc(particles[i].x, particles[i].y, 5, 0, 2*Math.PI);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ctx.fill();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  window.requestAnimationFrame(draw);
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const draw = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  for (let i=0; i< particles.length ;  i + + )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // update state
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    particles[i].x += particles[i].vx;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    particles[i].y += particles[i].vy;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    particles[i].vx += particles[i].ax;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    particles[i].vy += particles[i].ay;
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /* render code */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  window.requestAnimationFrame(draw);
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								canvas.addEventListener('click', e => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const clickX = e.clientX - canvas.offsetLeft;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const clickY = e.clientY - canvas.offsetTop;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  for (let i=0; i< particles.length ;  i + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const delx = particles[i].x - clickX;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const dely = particles[i].y - clickY;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const magnitudeSquared = Math.pow(delx, 2) + Math.pow(dely, 2);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    particles[i].ax = 0.1*delx/magnitudeSquared;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    particles[i].ay = 0.1*dely/magnitudeSquared;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								});
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-23 07:28:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This adds force via the [inverse-square law ](https://en.wikipedia.org/wiki/Inverse-square_law ).
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								That's all! The final codepen:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< p  data-height = "265"  data-theme-id = "0"  data-slug-hash = "OjMbpm"  data-default-tab = "js,result"  data-user = "alanluo"  data-embed-version = "2"  data-pen-title = "Particle Sim (FCC)"  class = "codepen" > See the Pen < a  href = "https://codepen.io/alanluo/pen/OjMbpm/" > Particle Sim (FCC)</ a >  by Alan Luo (< a  href = "https://codepen.io/alanluo" > @alanluo </ a > ) on < a  href = "https://codepen.io" > CodePen</ a > .</ p >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< script  async  src = "https://production-assets.codepen.io/assets/embed/ei.js" > < / script >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<!--  TODO: make it cooler!  -->  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### More Information:
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  [MDN Canvas API ](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API )