2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Animation
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Animation in Canvas
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To animate things in `canvas` , use `window.requestAnimationFrame`  to set up a draw loop.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-04-17 15:00:35 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const draw = () => {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  /* code goes here */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  window.requestAnimationFrame(draw);
							 
						 
					
						
							
								
									
										
										
										
											2019-04-17 15:00:35 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								window.requestAnimationFrame(draw);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`canvas`  has no special functions that allow for animating. You just have to be used to writing in animation loops. The usual design paradigm for animation loops is to update the state, then draw the state. For instance, to draw a square moving across the screen: 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-04-17 15:00:35 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const canvas = document.getElementById('canvas');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const ctx = canvas.getContext('2d');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-17 15:00:35 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const y = 50;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								let x = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const draw = () => {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  // reset canvas
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ctx.clearRect(0, 0, canvas.width, canvas.height);
							 
						 
					
						
							
								
									
										
										
										
											2019-04-17 15:00:35 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // update state
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  x += 1;
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // render state
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ctx.beginPath();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ctx.rect(x, y, 50, 50);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ctx.fill();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  window.requestAnimationFrame(draw);
							 
						 
					
						
							
								
									
										
										
										
											2019-04-17 15:00:35 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								window.requestAnimationFrame(draw);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-17 15:00:35 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To see this concept in action, see the [Particle Sim ](/articles/canvas/particle-sim ) page.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### More Information:
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  [MDN Canvas API ](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API )