设置器

在可动画参数中定义的每个可动画属性都会被转换为方法,并且可以在可动画对象上访问。

当调用一个至少带有一个参数的方法时,该方法会作为设置器,并返回可动画的实例,从而允许链式调用方法。

animatable.property(value, duration, easing);

参数

名称 类型 描述
value Number |
Array<Number>
定义动画对象要动画到的新值
duration (opt) Number 可选的新过渡持续时间(毫秒)
easing (opt) ease 动画的新可选缓动函数

返回

可动画对象本身,允许链式调用多个属性设置器:

animatable.x(100).y(200); // Animate x to 100 and y to 200 in 500ms

设置器代码示例

import { createAnimatable, utils } from 'animejs';

const $demos = document.querySelector('#docs-demos');
const $demo = document.querySelector('.docs-demo.is-active');
let bounds = $demo.getBoundingClientRect();
const refreshBounds = () => bounds = $demo.getBoundingClientRect();

const circle = createAnimatable('.circle', {
  x: 0,
  y: 0,
  backgroundColor: 0,
  ease: 'outExpo',
});

const rgb = [164, 255, 79];

// Sets new durations and easings
circle.x(0, 500, 'out(2)');
circle.y(0, 500, 'out(3)');
circle.backgroundColor(rgb, 250);

const onMouseMove = e => {
  const { width, height, left, top } = bounds;
  const hw = width / 2;
  const hh = height / 2;
  const x = utils.clamp(e.clientX - left - hw, -hw, hw);
  const y = utils.clamp(e.clientY - top - hh, -hh, hh);
  rgb[0] = utils.mapRange(x, -hw, hw, 0, 164);
  rgb[2] = utils.mapRange(x, -hw, hw, 79, 255);
  circle.x(x).y(y).backgroundColor(rgb); // Update values
}

window.addEventListener('mousemove', onMouseMove);
$demos.addEventListener('scroll', refreshBounds);
<div class="large centered row">
  <div class="circle"></div>
</div>
<div class="medium centered row">
  <span class="label">Move cursor around</span>
</div>