js-animation.vue.stories.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Vue from 'vue';
  2. import { storiesOf } from '@storybook/vue';
  3. import { Animation } from '../index';
  4. import noop from '../utils/noop';
  5. const stories = storiesOf('semi-animation/jsAnimation-vue', module);
  6. const itemStyle = {
  7. backgroundColor: 'rgb(241, 101, 101)',
  8. color: 'white',
  9. borderRadius: '4px',
  10. padding: '10px',
  11. textAlign: 'center',
  12. };
  13. const bigSquareStyle = {
  14. backgroundColor: 'rgb(241, 101, 101)',
  15. width: '100px',
  16. height: '100px',
  17. margin: '20px',
  18. marginLeft: '50px',
  19. borderRadius: '4px',
  20. fontSize: '32px',
  21. color: 'white',
  22. display: 'flex',
  23. alignItems: 'center',
  24. justifyContent: 'center',
  25. };
  26. const Anim = {
  27. data() {
  28. return {};
  29. },
  30. props: {
  31. from: {
  32. type: Object,
  33. default: () => ({ value: 0 }),
  34. },
  35. to: {
  36. type: Object,
  37. default: () => ({ value: 10 }),
  38. },
  39. onStart: {
  40. type: Function,
  41. default: () => noop,
  42. },
  43. onFrame: {
  44. type: Function,
  45. default: () => noop,
  46. },
  47. onPause: {
  48. type: Function,
  49. default: () => noop,
  50. },
  51. onResume: {
  52. type: Function,
  53. default: () => noop,
  54. },
  55. onStop: {
  56. type: Function,
  57. default: () => noop,
  58. },
  59. onRest: {
  60. type: Function,
  61. default: () => noop,
  62. },
  63. },
  64. created() {
  65. this.animation = new Animation({
  66. from: { ...this.from },
  67. to: { ...this.to },
  68. });
  69. ['start', 'frame', 'pause', 'resume', 'stop', 'rest'].forEach(event => {
  70. const propName = `on${event[0].toUpperCase() + event.slice(1)}`;
  71. this.animation.on(event, props => this[propName](props));
  72. });
  73. },
  74. mounted() {
  75. this.animation.start();
  76. },
  77. beforeDestroy() {
  78. this.animation.destroy();
  79. },
  80. methods: {
  81. reset() {
  82. this.animation.reset();
  83. this.animation.start();
  84. },
  85. reverse() {
  86. this.animation.reverse();
  87. this.animation.start();
  88. },
  89. pause() {
  90. this.animation.pause();
  91. },
  92. resume() {
  93. this.animation.resume();
  94. },
  95. stop() {
  96. this.animation.stop();
  97. },
  98. },
  99. template: `
  100. <div>
  101. <slot></slot>
  102. <div>
  103. <button @click="reset">reset</button>
  104. <button @click="reverse">reverse</button>
  105. <button @click="pause">pause</button>
  106. <button @click="resume">resume</button>
  107. <button @click="stop">stop</button>
  108. </div>
  109. </div>`,
  110. };
  111. stories.add('Hello World', () => ({
  112. components: {
  113. Anim,
  114. },
  115. data() {
  116. return {
  117. value: 0,
  118. bigSquareStyle: Object.freeze({ ...bigSquareStyle }),
  119. };
  120. },
  121. methods: {
  122. onFrame(props) {
  123. this.value = props.value.toFixed(2);
  124. },
  125. },
  126. created() {},
  127. mounted() {},
  128. template:
  129. '<Anim :from="{ value: 0 }" :to="{ value: 1 }" :on-frame="onFrame" :on-stop="onFrame"><div :style="{...bigSquareStyle, transform: `scale(${value})`}">{{value}}</div></Anim>',
  130. // template: '<div>123</div>',
  131. // render: h => h(`<div>123</div>`),
  132. }));
  133. stories.add('Hello World2', () => ({
  134. // render: h => h(`<div>123</div>`),
  135. template: '<div>123</div>',
  136. }));