过渡动画

      对 Component 上的属性做线性变化,实现过渡动画。

      安装

      使用 NPM

      1. npm i @eva/plugin-transition

      在浏览器中

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

      使用

      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)

      参数

      group: Object

      属性变化的时间轴,是一个对象,每个属性将对应一个动画,在一个 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

      需要变化的 component

      name: String

      需要变化的 component 的属性,比如 component.position.x 写成 ‘position.x’

      values: Array

      时间轴列表,时间对应的位置,和到下一个时间点所用的缓动函数

      • time: number 变化所对应的时间
      • value: number | string 当前时间所对应的值
      • tween 缓动函数,可选 linear,ease,ease-in,ease-out,ease-in-out,bounce-in,bounce-out,bounce-in-out

      方法

      play(name, iteration)

      播放动画,name 参数可选,不填写播放第一个动画。iteration 是循环次数,默认为 1,表示播放一次。

      stop(name)

      停止动画 name 参数可选,不传入 name 则停止所有动画。

      事件

      finish

      动画结束时触发

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