snap() V4

Rounds a Number to the nearest specified increment or creates a snapping Function with a pre-defined increment parameter.
If an Array is provided as the increment, it selects the closest value from the array.

const snappedValue = utils.snap(value, increment);
const snapperFunction = utils.snap(increment);

Parameters

Name Accepts
value (opt) Number
increment Number | Array<Number>

Returns

A Number if a value is provided, otherwise a chain-able utility Function to snap numbers to the nearest increment or array value:

const snapTo10 = utils.snap(10);
snapTo10(94);  // 90
snapTo10(-17); // -20

const snapToArray = utils.snap([0, 50, 100]);
snapToArray(30);  // 50
snapToArray(75);  // 100
snapToArray(-10); // 0

const clampAndSnap = utils.clamp(0, 100).snap(30);
clampAndSnap(72.7523); // 60
clampAndSnap(120.2514); // 90

snap() code example

import { animate, utils } from 'animejs';

animate('.normal', {
  rotate: '1turn',
  duration: 3000,
  loop: true,
  ease: 'inOut',
});

animate('.snapped', {
  rotate: '1turn',
  modifier: utils.snap(.25), // Used as a modifier
  duration: 3000,
  loop: true,
  ease: 'inOut',
});
<div class="x-large spaced-evenly row">
  <div class="col">
    <div class="clock normal"></div>
    <div class="label">normal</div>
  </div>
  <div class="col">
    <div class="clock snapped"></div>
    <div class="label">snapped (.25)</div>
  </div>
</div>