Google News
logo
Anime.js - Interview Questions
What are the Values in Anime.js?
Unitless : If the original value has a unit, it will be automatically added to the animated value.

Type Example
Number translateX: 250

Example :
anime({
  targets: '.unitless-values-demo .el',
  translateX: 250, // -> '250px'
  rotate: 540 // -> '540deg'
});


Specific Unit : Forces the animation to use a certain unit and will automatically convert the initial target value.

Type Example
String width: '100%'

Example : 
anime({
  targets: '.specific-unit-values-demo .el',
  width: '100%', // -> from '28px' to '100%',
  easing: 'easeInOutQuad',
  direction: 'alternate',
  loop: true
});


Relative : Adds, substracts or multiplies the original value.

Accepts Effect Example
'+=' Add '+=100'
'-=' Substract '-=2turn'
'*=' Multiply '*=10'

Example : 
var relativeEl = document.querySelector('.el.relative-values');
relativeEl.style.transform = 'translateX(100px)';

anime({
  targets: '.el.relative-values',
  translateX: {
    value: '*=2.5', // 100px * 2.5 = '250px'
    duration: 1000
  },
  width: {
    value: '-=20px', // 28 - 20 = '8px'
    duration: 1800,
    easing: 'easeInOutSine'
  },
  rotate: {
    value: '+=2turn', // 0 + 2 = '2turn'
    duration: 1800,
    easing: 'easeInOutSine'
  },
  direction: 'alternate'
});


Color : anime.js accepts and converts Hexadecimal, RGB, RGBA, HSL, and HSLA color values.
  * CSS color codes ( e.g. : 'red', 'yellow', 'aqua' ) are not supported.

Accepts Example
Hexadecimal '#FFF' or '#FFFFFF'
RGB 'rgb(255, 255, 255)'
RGBA 'rgba(255, 255, 255, .2)'
HSL 'hsl(0, 100%, 100%)'
HSLA 'hsla(0, 100%, 100%, .2)'

Example : 
var colorsExamples = anime.timeline({
  endDelay: 1000,
  easing: 'easeInOutQuad',
  direction: 'alternate',
  loop: true
})
.add({ targets: '.color-hex',  background: '#FFF' }, 0)
.add({ targets: '.color-rgb',  background: 'rgb(255,255,255)' }, 0)
.add({ targets: '.color-hsl',  background: 'hsl(0, 100%, 100%)' }, 0)
.add({ targets: '.color-rgba', background: 'rgba(255,255,255, .2)' }, 0)
.add({ targets: '.color-hsla', background: 'hsla(0, 100%, 100%, .2)' }, 0)
.add({ targets: '.colors-demo .el', translateX: 270 }, 0);


From To : Forces the animation to start at a specified value.

Type Example
Array ['50%', '100%']

Example : 
anime({
  targets: '.el.from-to-values',
  translateX: [100, 250], // from 100 to 250
  delay: 500,
  direction: 'alternate',
  loop: true
});


Function Based Values :
Get different values for every target and property of the animation.
The function accepts 3 arguments :

Arguments Infos
target The curently animated targeted element
index The index of the animated targeted element
targetsLength The total number of animated targets

Example : 
anime({
  targets: '.function-based-values-demo .el',
  translateX: function(el) {
    return el.getAttribute('data-x');
  },
  translateY: function(el, i) {
    return 50 + (-50 * i);
  },
  scale: function(el, i, l) {
    return (l - i) + .25;
  },
  rotate: function() { return anime.random(-360, 360); },
  borderRadius: function() { return ['50%', anime.random(10, 35) + '%']; },
  duration: function() { return anime.random(1200, 1800); },
  delay: function() { return anime.random(0, 400); },
  direction: 'alternate',
  loop: true
});
Advertisement