Loading...
In addition to describing animations using the Web Animations API, we also support playback of Lottie formats, for which we provide a player like [lottie-web](https://github.com/airbnb/lottie- web/) player. Internally we will convert the graphics and Keyframe animations defined there into our basic graphics and animation descriptions, while providing simple animation control methods.
Install player first:
npm install @antv/g-lottie-player --save
Then use the loadAnimation method provided by the player to create a LottieAnimation object, passing in the Lottie JSON.
import { loadAnimation } from '@antv/g-lottie-player';const ballAnimation = loadAnimation(bouncy_ball, { loop: true });
Finally, render to canvas at the right time.
canvas.addEventListener(CanvasEvent.READY, () => {const wrapper = ballAnimation.render(canvas);});
Reference [lottie-web](https://github.com/airbnb/lottie-web/blob/6faae912910b2d7be6c5422ef4621f3933c19d60/player/js/animation/ AnimationManager.js#L227) method of the same name for loading Lottie files to create LottieAnimation.
The parameters are as follows.
data
Lottie JSONoptions
configuration item
loop
is of type boolean | number
. If or not loop is enabled, the default value is true
which means infinite loop. When number
is passed in, it means the number of loops.autoplay
is of type boolean
. The default value is false
to start autoplay immediately after loading.For example, to create an infinitely looping, immediately playable animation.
import { loadAnimation } from '@antv/g-lottie-player';const ballAnimation = loadAnimation(bouncy_ball, {loop: true,autoplay: true,});
This object can be created by loadAnimation to control the animation process.
Renders to canvas and returns a Group as a container, which can subsequently be transformed to.
const wrapper = animation.render(canvas);wrapper.scale(0.5);wrapper.translate(100, 100);
The following two parameters are supported to be passed in.
It is worth noting that, like animation, it needs to be done after canvas initialization is complete.
Start the animation.
animation.play();
Pause the animation.
animation.pause();
Pause if it is playing and vice versa.
animation.togglePause();
Stop the animation.
animation.stop();
Jump to the specified moment or frame.
The parameters are as follows.
value
specifies the second moment or frameisFrame
indicates whether value
is passed in as a frame, the default value is false
.// Jump to the 2s moment of the timelineanimation.goTo(2);// Jump to frame 10animation.goTo(10, true);
Returns the duration, in seconds or frames.
The parameters are as follows.
inFrames
if or not in frames, default is false
animation.getDuration(); // 2animation.getDuration(true); // 120
The conversion relationship between the two is:
const durationInSeconds = animation.getDuration();const durationInFrames = animation.getDuration(true);durationInFrames === animation.fps() * durationInSeconds; // true
Start playing the animation from the specified frame range.
The parameters are as follows.
segments
[number, number]
Specify the start and end frame rangeanimation.playSegments([firstFrame, lastFrame]);
Controls the playback speed, default is 1
. Greater than 1
means speed up, less than 1
means speed down.
// 2xanimation.setSpeed(2);
1
means forward, -1
means reverse. Default forward play.
animation.setSpeed(1);animation.setSpeed(-1);
Destroys all internal objects and, of course, terminates the animation at the same time.
animation.destroy();
Return to Lottie file size.
animation.size(); // { width: 1080, height: 260 }
Returns the version of Bodymovin contained in the Lottie file
animation.version();
Support the following Shape Layer
https://lottiefiles.github.io/lottie-docs/concepts/#transform
The following features are supported.
a
fieldp
fields
fieldr
fieldThe following features are not supported at this time.
sk
fieldsa
fieldIn this example, the dark blue is the base rectangle, and we use the red dot as the to rotate it by a certain angle to get the light blue rectangle.
https://lottiefiles.github.io/lottie-docs/concepts/#animated-position
The following style attributes are supported.
https://lottiefiles.github.io/lottie-docs/shapes/#fill
Fill color, while supporting the following features.
o
fieldr
fieldhttps://lottiefiles.github.io/lottie-docs/shapes/#stroke
Stroke color, while supporting the following features.
o
fieldw
fieldlc
fieldlj
fieldml
fieldd
fieldhttps://lottiefiles.github.io/lottie-docs/shapes/#gradients
Support linear and radial gradients.
The following features are not supported at this time.
h
and a
fields)https://lottiefiles.github.io/lottie-docs/layers/#layers
https://lottiefiles.github.io/lottie-docs/layers/#solid-color-layer
https://lottiefiles.github.io/lottie-docs/layers/#image-layer https://lottiefiles.github.io/lottie-docs/assets/#image
https://lottiefiles.github.io/lottie-docs/layers/#text-layer https://lottiefiles.github.io/lottie-docs/text/
https://lottiefiles.github.io/lottie-docs/layers/#precomposition-layer https://lottiefiles.github.io/lottie-docs/assets/#precomposition
https://lottie-animation-community.github.io/docs/specs/layers/shapes/#merge-paths-property
Internally, it will be converted to clipPath to be applied to the target element and support path animation on it.
Caution.
Add
operatorhttps://lottie-animation-community.github.io/docs/specs/layers/common/#clipping-masks
Post-processing effects for Layer are not supported at this time.
https://lottiefiles.github.io/lottie-docs/effects/#layer-effects
Expressions are not supported at this time.