c-animation


动画组件

示例

  1. <template>
  2. <view class="page-contain">
  3. <view class="container">
  4. <view class="btn-box">
  5. <text class="title">Transform</text>
  6. <view class="btn-wraper">
  7. <button text="Rotate" size="medium" c-bind:onclick="rotate"></button>
  8. </view>
  9. <view class="btn-wraper">
  10. <button text="Scale" size="medium" c-bind:onclick="scale"></button>
  11. </view>
  12. <view class="btn-wraper">
  13. <button text="Translate" size="medium" c-bind:onclick="translate"></button>
  14. </view>
  15. <view class="btn-wraper">
  16. <button text="Transform" size="medium" type="green" c-bind:onclick="transform"></button>
  17. </view>
  18. <text class="title">Others</text>
  19. <view class="btn-wraper">
  20. <button text="BgColor" size="medium" c-bind:onclick="backgroundColor"></button>
  21. </view>
  22. <view class="btn-wraper">
  23. <button text="Opacity" size="medium" c-bind:onclick="opacity"></button>
  24. </view>
  25. <view class="btn-wraper">
  26. <button text="width" size="medium" c-bind:onclick="width"></button>
  27. </view>
  28. <view class="btn-wraper">
  29. <button text="height" size="medium" c-bind:onclick="height"></button>
  30. </view>
  31. <view class="btn-wraper">
  32. <button text="All" size="medium" type="green" c-bind:onclick="composite"></button>
  33. </view>
  34. </view>
  35. <view class="animation-entity" c-animation="{{animationData}}">
  36. <text class="animation-text">Anim</text>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import cml from 'chameleon-api';
  43. const animation = cml.createAnimation();
  44. class CAnimation {
  45. data = {
  46. animationData: {},
  47. current_rotate: 0,
  48. current_scale: 1,
  49. current_translate: 50,
  50. current_transform: [45, 1],
  51. current_color: '#F0AD4E',
  52. current_opacity: 1,
  53. current_width: 250,
  54. current_height: 250,
  55. }
  56. methods = {
  57. rotate () {
  58. let animationCreation = animation;
  59. while (this.current_rotate !== 360) {
  60. this.current_rotate += 90;
  61. animationCreation = animationCreation
  62. .rotate(this.current_rotate)
  63. .step({
  64. duration: 500,
  65. timingFunction: 'ease-in-out',
  66. delay: 0,
  67. })
  68. }
  69. this.animationData = animationCreation.export();
  70. this.current_rotate = 0
  71. },
  72. scale () {
  73. this.current_scale = this.current_scale === 2 ? 1 : 2
  74. this.animationData = animation
  75. .scale(this.current_scale)
  76. .step({
  77. duration: 500,
  78. timingFunction: 'linear',
  79. delay: 0
  80. })
  81. .export()
  82. },
  83. translate () {
  84. this.current_translate = this.current_translate === 50? -50 : 50;
  85. this.animationData = animation
  86. .translate(this.current_translate, this.current_translate)
  87. .step({
  88. duration: 500,
  89. timingFunction: 'ease-in',
  90. delay: 0,
  91. })
  92. .export()
  93. },
  94. transform () {
  95. this.current_transform = this.current_transform[0] === 0 ? [45, 1.2]:[0, 1];
  96. this.animationData = animation
  97. .rotate(this.current_transform[0])
  98. .scale(this.current_transform[1])
  99. .step({
  100. duration: 500,
  101. timingFunction: 'ease-out',
  102. delay: 0
  103. })
  104. .rotate("-90deg")
  105. .scale(1.2)
  106. .step({
  107. duration: 500,
  108. timingFunction: 'ease-out',
  109. delay: 0
  110. })
  111. .export()
  112. },
  113. backgroundColor () {
  114. this.current_color = this.current_color === '#F0AD4E' ? '#D9534F' : '#F0AD4E';
  115. this.animationData = animation
  116. .backgroundColor(this.current_color)
  117. .step({
  118. duration: 500,
  119. timingFunction: 'linear',
  120. delay: 0
  121. })
  122. .export()
  123. },
  124. opacity () {
  125. this.current_opacity = this.current_opacity === 1 ? 0.1 : 1;
  126. this.animationData = animation
  127. .opacity(this.current_opacity)
  128. .step({
  129. duration: 500,
  130. timingFunction: 'linear',
  131. delay: 0
  132. })
  133. .export()
  134. },
  135. width () {
  136. this.current_width = this.current_width === 250 ? 125 : 250;
  137. this.animationData = animation
  138. .width(this.current_width)
  139. .step({
  140. timingFunction: 'linear',
  141. delay: 0,
  142. duration: 500,
  143. })
  144. .export()
  145. },
  146. height () {
  147. this.current_height = this.current_height === 250 ? 125 : 250;
  148. this.animationData = animation
  149. .height(this.current_height)
  150. .step({
  151. duration: 500,
  152. timingFunction: 'linear',
  153. delay: 0
  154. })
  155. .export()
  156. },
  157. composite () {
  158. this.current_transform = this.current_transform[0] === 0 ? [45, 1.5]:[0, 1];
  159. this.current_color = this.current_color === '#F0AD4E' ? '#D9534F' : '#F0AD4E';
  160. this.current_opacity = this.current_opacity === 1 ? 0.1 : 1;
  161. this.current_translate = this.current_translate === 50? -50 : 50;
  162. this.current_width = this.current_width === 250 ? 125 : 250;
  163. this.current_height = this.current_height === 250 ? 125 : 250;
  164. this.animationData = animation
  165. .width(this.current_width)
  166. .height(this.current_height)
  167. .rotate(this.current_transform[0])
  168. .scale(this.current_transform[1])
  169. .opacity(this.current_opacity)
  170. .backgroundColor(this.current_color)
  171. .translate(this.current_translate, this.current_translate)
  172. .step({
  173. duration: 1000,
  174. timingFunction: 'ease-out',
  175. delay: 0
  176. })
  177. .export()
  178. }
  179. }
  180. }
  181. export default new CAnimation();
  182. </script>
  183. <style scoped lang="less">
  184. .title {
  185. font-size: 45cpx;
  186. font-weight: 400;
  187. margin: 20cpx 0;
  188. padding: 8cpx 30cpx;
  189. background: #4a4c5b;
  190. color:#FFFFFF;
  191. display: block;
  192. }
  193. .animation-entity {
  194. position: fixed;
  195. width: 250cpx;
  196. height: 250cpx;
  197. top: 500cpx;
  198. left: 400cpx;
  199. background-color: #F0AD4E;
  200. display:flex;
  201. justify-content:center;
  202. align-items:center;
  203. }
  204. .animation-text {
  205. color:#FFFFFF;
  206. font-size:70cpx;
  207. }
  208. .btn-wraper {
  209. margin: 6cpx 10cpx;
  210. display: flex;
  211. align-items: start;
  212. justify-content: flex-start;
  213. flex-direction: column;
  214. }
  215. .page-container {
  216. font-size: 32cpx;
  217. color: #000;
  218. padding: 20cpx 25cpx;
  219. background: #fafafa;
  220. }
  221. </style>
  222. <script cml-type="json">
  223. {
  224. "base": {
  225. "usingComponents": {}
  226. }
  227. }
  228. </script>