Lottie Animation

Lottie is a mobile library for Web, and iOS that parses Adobe After Effects animations exported as json with Bodymovin and renders them natively on mobile!

This plugin suport animation exported with Lolita.

Install

With NPM

  1. npm i @eva/plugin-renderer @eva/plugin-renderer-lottie -S

In Browser

  1. <script src="https://unpkg.com/@eva/plugin-renderer-lottie@1.2.x/dist/EVA.plugin.renderer.lottie.min.js"></script>

Usage

  1. import { Game, GameObject, resource } from '@eva/eva.js'
  2. import { RendererSystem } from '@eva/plugin-renderer'
  3. import { LottieSystem, Lottie } from '@eva/plugin-renderer-lottie'
  4. resource.addResource([
  5. {
  6. name: 'Halo',
  7. // @ts-ignore
  8. type: 'LOTTIE',
  9. src: {
  10. json: {
  11. type: 'json',
  12. url: 'https://gw.alipayobjects.com/os/bmw-prod/61d9cc77-12de-47a7-b6e5-06c836ce7083.json',
  13. },
  14. },
  15. },
  16. {
  17. name: 'Red',
  18. // @ts-ignore
  19. type: 'LOTTIE',
  20. src: {
  21. json: {
  22. type: 'json',
  23. url: 'https://gw.alipayobjects.com/os/bmw-prod/e327ad5b-80d6-4d3f-8ffc-a7dd15350648.json',
  24. },
  25. },
  26. },
  27. ]);
  28. const game = new Game({
  29. systems: [
  30. new RendererSystem({
  31. canvas: document.querySelector('#canvas'),
  32. width: 750,
  33. height: 1624,
  34. transparent: true,
  35. }),
  36. new LottieSystem(),
  37. ],
  38. autoStart: true,
  39. frameRate: 60,
  40. });
  41. game.scene.transform.size = {
  42. width: 750,
  43. height: 1624,
  44. };
  45. const halo = new Lottie({resource: 'Halo'});
  46. const red = new Lottie({resource: 'Red'});
  47. halo.on('complete', () => {
  48. console.log('halo play complete !');
  49. });
  50. red.on('complete', () => {
  51. console.log('Red play complete !');
  52. });
  53. halo.play([], {repeats: 0});
  54. red.play([], {
  55. repeats: 0,
  56. slot: [
  57. {
  58. name: '#number',
  59. type: 'TEXT',
  60. value: '10',
  61. style: {
  62. fontSize: 64,
  63. },
  64. },
  65. {
  66. name: '#unit',
  67. type: 'TEXT',
  68. value: '元',
  69. style: {
  70. fontSize: 22,
  71. },
  72. },
  73. {
  74. name: '#title',
  75. type: 'TEXT',
  76. value: '我是主标题',
  77. style: {
  78. fontSize: 32,
  79. },
  80. },
  81. {
  82. name: '#subtitle',
  83. type: 'TEXT',
  84. value: '我是副标题',
  85. style: {
  86. fontSize: 24,
  87. },
  88. },
  89. ],
  90. });
  91. red.onTap('#btn', () => {
  92. console.log('btn click !');
  93. });
  94. const haloGameObj = new GameObject('Halo', {
  95. anchor: {
  96. x: 0,
  97. y: 0,
  98. },
  99. });
  100. const redGameObj = new GameObject('Red', {
  101. anchor: {x: 0.5, y: 0.3},
  102. size: {width: 660, height: 757},
  103. origin: {x: 0.5, y: 0.5},
  104. });
  105. haloGameObj.addComponent(halo);
  106. redGameObj.addComponent(red);
  107. game.scene.addChild(haloGameObj);
  108. game.scene.addChild(redGameObj);

Options

  • resource resource name

Methods

LottieComponent.play

Play animation

  1. LottieComponent.play([], {repeats: Infinity })
  2. LottieComponent.play([0, 10])

Options

| | Play animation frame rate interval, the default is from the first frame to the last frame. | Array | [start, end] | | repeate: play times, Infinity loop play. | number | Infinity |

slot: -name: The name of the slot. -type: The type of the slot, distinguish between text and pictures. -value: The value to be filled in the slot, and the picture is a CDN link. -style: The style setting of the slot.

  1. interface options {
  2. Repeats?: number
  3. Slot?: Array<{
  4. Name: string;
  5. Type:'TEXT' |'IMAGE';
  6. Value: string;
  7. Style: IStyle;
  8. }>
  9. }

IStyle

| | x | position.x | number | | | y | position.y | number | | | width | bounds.width | number | | | height | bounds.height | number | | | anchor | document | {x: number, y: number} | {x: 0, y: 0} | | pivot | document | {x: number, y: number} | {x: 0, y: 0} | | TextStyle | Document | PIXI.TextStyle | |

LottieComponent.onTap

Bind the Tap event to the slot element in Lottie

  1. LottieComponent.onTap('#btn', () => {
  2. console.log('btn click !')
  3. })

Options

| | Slot name | string | | | Event callback after click | () => void | |

Events

  1. LottieComponent.on('complete', () => {
  2. console.log('LottieComponent play complete !')
  3. })