how to write an anime library
The engine
We need a mechanism to deal with layout thrashing. So we have an engine that group all animations into to a requestAnimationFrame
callback.
1 | const engine = (function () { |
The tween and animation
A tween object repersent a property change. It is an object like this:
1 | const defaultTweenSetting = { |
This object can describe what to be change (target
, property
), how long it will last (duration
), how does the chnage looks like (easing
), and the start and end value (from
, to
and unit
).
An animation object group list of tweens and play the real animation by add it self to the engine and step the tween.
1 | class Animation { |
The Libraries
Now we have engine
, tween
and Animtaion
. Use those object or class you can build some style animatons you what to. But it’s not enough, we need construct the tweens
ourself and we only support css style property animation.
There are some realy awesome animation libraries exist:
- anime.js: This is what inspire our little animation
engine
with more features liketransform, svg and object property
(we can do it by add atype
field to ourtween
and add the corresponding changes code toAnimation.step
function),timeline
,can automaticlly transform params to tweens
etc. - velocityjs: It’s incredibly fast, and it features color animation, transforms, loops, easings, SVG support, and scrolling.
- react-spring: data driven animation library.
- Web Animations API: The W3C
Others
Unit transfrom
- jquery.adjustCSS: calculate the scale between current unit and the excepted unit, then apply the new united value.
- velocityjs:
1
2
3if (fromUnit !== toUnit) {
currentValue = `calc(${from * (1 - eased) + fromUnit} + ${to * eased + toUnit})`
}
FLIP Technique
For list reordering stuff.
https://aerotwist.com/blog/flip-your-animations/#the-general-approach
- Calculate the First position.
- Calculate the Last position.
- Invert the positions
- Play the animation