BounceIn.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Animation, presets } from '@douyinfe/semi-animation-react';
  2. import React from 'react';
  3. /**
  4. @keyframes semi-bounceIn {
  5. from,
  6. 20%,
  7. 40%,
  8. 60%,
  9. 80%,
  10. to {
  11. animation-timing-function: cubic-bezier(.215, .61, .355, 1);
  12. }
  13. 0% {
  14. opacity: 0;
  15. transform: scale3d(.3, .3, .3);
  16. }
  17. 20% {
  18. transform: scale3d(1.1, 1.1, 1.1);
  19. }
  20. 40% {
  21. transform: scale3d(.9, .9, .9);
  22. }
  23. 60% {
  24. opacity: 1;
  25. transform: scale3d(1.03, 1.03, 1.03);
  26. }
  27. 80% {
  28. transform: scale3d(.97, .97, .97);
  29. }
  30. to {
  31. opacity: 1;
  32. transform: scale3d(1, 1, 1);
  33. }
  34. }
  35. */
  36. export default class BounceIn extends React.PureComponent {
  37. constructor(props = {}) {
  38. super(props);
  39. this.state = {
  40. currentStyle: {},
  41. frameIndex: 0,
  42. config: {
  43. ...presets.wobbly,
  44. // easing: 'cubic-bezier(.215, .61, .355, 1)',
  45. // easing: 'easeInCubic',
  46. // duration: 750,
  47. },
  48. };
  49. this.frames = [
  50. {
  51. scale3d: 3,
  52. opacity: 0,
  53. },
  54. // { opacity: 0.333, scale3d: 1.1 },
  55. // { opacity: 0.666, scale3d: 0.9 },
  56. // { opacity: 1, scale3d: 1.03 },
  57. // { opacity: 1, scale3d: 0.97 },
  58. {
  59. scale3d: 10,
  60. opacity: { val: 10, easing: 'linear' },
  61. },
  62. ];
  63. }
  64. next = () => {
  65. let { frameIndex } = this.state;
  66. frameIndex++;
  67. if (frameIndex + 1 < this.frames.length) {
  68. this.setState({ frameIndex });
  69. }
  70. };
  71. onFrame = ({ opacity, scale3d }) => {
  72. scale3d = scale3d / 10;
  73. this.setState({
  74. currentStyle: {
  75. opacity: opacity / 10,
  76. transform: `scale3d(${scale3d}, ${scale3d}, ${scale3d})`,
  77. },
  78. });
  79. };
  80. componentWillUnmount() {
  81. this.instance && this.instance.destroy();
  82. }
  83. render() {
  84. let { children } = this.props;
  85. let { config, frameIndex, currentStyle } = this.state;
  86. const from = this.frames[frameIndex];
  87. const to = this.frames[frameIndex + 1];
  88. // console.log(currentStyle);
  89. return (
  90. <Animation
  91. forwardInstance={instance => (this.instance = instance)}
  92. // force
  93. from={{ ...from }}
  94. to={{ ...to }}
  95. config={{ ...config }}
  96. onFrame={this.onFrame}
  97. onRest={this.next}
  98. >
  99. {() => (typeof children === 'function' ? children(currentStyle) : children)}
  100. </Animation>
  101. );
  102. }
  103. }