Transition animation

Make linear changes to the properties on the Component to achieve transition animation.

-https://eva.js.org/playground/#/transition -https://eva.js.org/playground/#/transition2

Install

With NPM

  1. npm i @eva/plugin-transition

In Browser

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

Usage

  1. import {Game, GameObject, resource, RESOURCE_TYPE} from '@eva/eva.js'
  2. import {RendererSystem} from '@eva/plugin-renderer'
  3. import {Img, ImgSystem} from '@eva/plugin-renderer-img'
  4. import {Transition, TransitionSystem} from '@eva/plugin-transition'
  5. resource.addResource([
  6. {
  7. name:'heart',
  8. type: RESOURCE_TYPE.IMAGE,
  9. src: {
  10. image: {
  11. type:'png',
  12. url:'//gw.alicdn.com/bao/uploaded/TB1lVHuaET1gK0jSZFhXXaAtVXa-200-200.png'
  13. }
  14. },
  15. preload: false
  16. }
  17. ])
  18. const game = new Game({
  19. systems: [
  20. new RendererSystem({
  21. canvas: document.querySelector('#canvas'),
  22. width: 750,
  23. height: 1000
  24. }),
  25. new ImgSystem(),
  26. new TransitionSystem()
  27. ]
  28. })
  29. const image = new GameObject('image', {
  30. size: {width: 200, height: 200 },
  31. origin: {x: 0, y: 0 },
  32. position: {
  33. x: 0,
  34. y: 0
  35. },
  36. anchor: {x: 0.5, y: 0.5}
  37. })
  38. const img = image.addComponent(
  39. new Img({
  40. resource:'heart'
  41. })
  42. )
  43. const animation = image.addComponent(new Transition())
  44. animation.group = {
  45. idle: [
  46. {
  47. name:'scale.x',
  48. component: image.transform,
  49. values: [
  50. {
  51. time: 0,
  52. value: 1,
  53. tween:'ease-out'
  54. },
  55. {
  56. time: 300,
  57. value: 1.2,
  58. tween:'ease-in'
  59. },
  60. {
  61. time: 600,
  62. value: 1
  63. }
  64. ]
  65. },
  66. {
  67. name:'scale.y',
  68. component: image.transform,
  69. values: [
  70. {
  71. time: 0,
  72. value: 1,
  73. tween:'ease-out'
  74. },
  75. {
  76. time: 300,
  77. value: 1.2,
  78. tween:'ease-in'
  79. },
  80. {
  81. time: 600,
  82. value: 1
  83. }
  84. ]
  85. }
  86. ],
  87. move: [
  88. {
  89. name:'position.x',
  90. component: image.transform,
  91. values: [
  92. {
  93. time: 0,
  94. value: 1,
  95. tween:'ease-out'
  96. },
  97. {
  98. time: 300,
  99. value: 300,
  100. tween:'ease-in'
  101. }
  102. ]
  103. },
  104. {
  105. name:'position.y',
  106. component: image.transform,
  107. values: [
  108. {
  109. time: 0,
  110. value: 1,
  111. tween:'ease-in'
  112. },
  113. {
  114. time: 300,
  115. value: 300
  116. }
  117. ]
  118. }
  119. ]
  120. }
  121. animation.play('move', 1)
  122. animation.on('finish', name => {
  123. name ==='move' && animation.play('idle', 5)
  124. })
  125. game.scene.addChild(image)

Options

group: Object

The timeline of property changes is an object, and each property corresponds to an animation. Multiple animations can be configured on a gameObject.

  1. const gameObject = new GameObject()
  2. let transition = gameObject.addComponent(new Transition())
  3. transition.group = {
  4. up: [
  5. {
  6. component: gameObject.transform,
  7. name:'position.y',
  8. values: [
  9. {
  10. time: 0,
  11. tween:'linear',
  12. value: 10
  13. }
  14. ]
  15. },
  16. {
  17. component: gameObject.transform,
  18. name:'position.y',
  19. values: [
  20. {
  21. time: 1,
  22. tween:'linear',
  23. value: 20
  24. }
  25. ]
  26. }
  27. ]
  28. }
component

The component that needs to be changed

name: String

The attribute of the component that needs to be changed, for example, component.position.x is written as 'position.x'

values: Array

Timeline list, the position corresponding to the time, and the easing function used to the next time point

-time: the time corresponding to the change of number -value: number | string the value corresponding to the current time -tween easing function, optional linear,ease,ease-in,ease-out,ease-in-out,bounce-in,bounce-out,bounce -in-out

Methods

play(name, iteration)

Play animation, name parameter is optional, do not fill in and play the first animation. iteration is the number of loops, the default is 1, which means to play once.

stop(name)

Stop animation. The name parameter is optional. If name is not passed in, all animations will be stopped.

Events

finish

Triggered when the animation ends

  1. transition.on('finish', animationName => {})