precision

定义动画过程中字符串值舍入到的小数位数。
小数位数越多,动画越精确。设置为 0 基本上会移除动画过程中所有的十进制小数。
仅 CSS 属性、SVG 和 DOM 属性的字符串值会被舍入(例如 '120.725px''1.523'),并且舍入仅在动画执行期间应用,动画的第一帧和最后一帧会使用完整数值。

在 99% 的情况下,你不需要将精度提高到 4 以上,因为视觉上的差异并不明显。在同时为大量元素制作动画时,降低精度会有所帮助,但这可能会大幅降低动画的视觉质量和流畅度。
engine.precision = 1; // values will be rounded to 1 decimal place ('120.7px')

接受

  • 一个大于或等于 0Number,用于定义小数位数
  • 一个小于 0Number,用于跳过舍入过程

默认值

4

precision 代码示例

import { engine, animate, utils } from 'animejs';

const [ $container ] = utils.$('.container');
const [ $range ] = utils.$('.range');

for (let i = 0; i < 150; i++) {
  const $particle = document.createElement('div');
  $particle.classList.add('particle');
  $container.appendChild($particle);
  animate($particle, {
    x: utils.random(-10, 10, 2) + 'rem',
    y: utils.random(-3, 3, 2) + 'rem',
    scale: [{ from: 0, to: 1 }, { to: 0 }],
    delay: utils.random(0, 1000),
    loop: true,
  });  
}

function onInput() {
  engine.precision = this.value;
}

$range.addEventListener('input', onInput);
<div class="large row container"></div>
<div class="medium row">
  <fieldset class="controls">
    <input type="range" min=0 max=5 value=4 step=1 class="range" />
  </fieldset>
</div>