title: Snapping to grid in JavaScript
body: <p>I'm looking for a javascript function that calculates a snap to grid value using a uniformly spaced grid.</p>
  
  <p>Now I have the following code, which works fine as a basic solution, but now I need it to snap only when within say 2px of the snap position e.g. using a 30px spaced grid and a 2px limit, it would snap between 0px and 2px, 28px and 32px, 58px and 62px, etc.</p>
  
  <pre><code>snapToGrip = function(val,gridSize){
      return gridSize * Math.round(val/gridSize);
  };
  </code></pre>
  
  <p>Many thanks</p>