Platinum sponsors

composition JS

Defines how animations behave when another animation on the same target with the same property is playing simultaneously. The composition mode can be defined globally for all animation properties or locally for a specific property.

Accepts

Mode Description
'replace' Replace and cancel the running animation.
'none' JS Do not replace the running animation. This means the previous animation will continue running if its duration is longer than the new animation. This mode can also offer better performance.
'blend' JS Creates an additive animation and blends its values with the running animation.
0 JS Shorthand for 'replace'.
1 JS Shorthand for 'none'.
2 JS Shorthand for 'blend'.

Default

'replace' if the animation targets count is below 1000; otherwise, the default composition is set to 'none' on the JS version if no composition mode is defined.

Replacing order 5.0

A new animation takes a property from the animations of the same target that animate it, including the ones scheduled to start after it with a delay. The last action on a property wins: .restart(), .play() or .seek() on an animation that lost a property gives it back, and cancels the other animation when it has no property left. This lets two animations on the same property be kept and played in turn:

const enter = animate(target, { scale: 1.5, duration: 300, autoplay: false });
const leave = animate(target, { scale: 1, duration: 300, autoplay: false });

$el.addEventListener('mouseenter', () => enter.restart());
$el.addEventListener('mouseleave', () => leave.restart());

An animation that lost a property no longer writes it, even when it is sought to a time before the newer animation was created.

In a timeline, a child takes the property at the end of its delay, so the child before it keeps animating the property until then. A child with reversed: true or a playbackEase takes it at its position:

createTimeline()
.add(target, { x: 100, duration: 1000 })
.add(target, { x: 200, duration: 500, delay: 300 }, 500); // x keeps moving to 100 until 800

Additive animations

The 'blend' mode lets you create additive animations. This type of animation allows you to smoothly blend two animations of the same property on the same target together. This mode works best on properties that visually move on the screen, like 'translate', 'scale', and 'rotation'.

Composition blend gotchas

Blending only works by playing forward two or more animations with composition 'blend' simultaneously.

It is not currently possible to use composition 'blend' with:

Blended animations should be treated differently and should only be used when you actually need to mix multiple animations together.

To change the default value globally, update the engine.defaults object.

import { engine } from 'animejs';
engine.defaults.composition = 'blend';

composition code example

import { animate, utils } from 'animejs';

const squares = utils.$('.square');
const [ $none, $replace, $blend ] = squares;

// Animate each square with a different composition mode

squares.forEach($square => {
  // 'none', 'replace', 'blend'
  const mode = $square.classList[1];
  animate($square, {
    scale: [.5, 1],
    alternate: true,
    loop: true,
    duration: 750,
    composition: mode,
  });
});

// Common animation parameters

const enter = { scale: 1.5, duration: 350 };
const leave = { scale: 1.0, duration: 250 };

// Composition none animations

const enterNone = () => animate($none, {
  composition: 'none', ...enter
});

const leaveNone = () => animate($none, {
  composition: 'none', ...leave
});

$none.addEventListener('mouseenter', enterNone);
$none.addEventListener('mouseleave', leaveNone);

// Composition replace animations

const enterReplace = () => animate($replace, {
  composition: 'replace', ...enter
});

const leaveReplace = () => animate($replace, {
  composition: 'replace', ...leave
});

$replace.addEventListener('mouseenter', enterReplace);
$replace.addEventListener('mouseleave', leaveReplace);

// Composition blend animations

const enterBlend = () => animate($blend, {
  composition: 'blend', ...enter
});

const leaveBlend = () => animate($blend, {
  composition: 'blend', ...leave
});

$blend.addEventListener('mouseenter', enterBlend);
$blend.addEventListener('mouseleave', leaveBlend);
<div class="large spaced-evenly row">
  <div class="col">
    <div class="centered row">
      <span class="label centered">none<br><br></span>
      <div class="square none"></div>
    </div>
  </div>
  <div class="col">
    <div class="centered row">
      <span class="label centered">replace<br><br></span>
      <div class="square replace"></div>
    </div>
  </div>
  <div class="col">
    <div class="centered row">
      <span class="label centered">blend<br><br></span>
      <div class="square blend"></div>
    </div>
  </div>
</div>

<div class="medium spaced-evenly centered row">
  <div class="label"><br><br>(Hover the squares)</div>
</div>
Previous
Next