Animate.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-Animate'>/**
  19. </span> * This animation class is a mixin.
  20. *
  21. * Ext.util.Animate provides an API for the creation of animated transitions of properties and styles.
  22. * This class is used as a mixin and currently applied to {@link Ext.Element}, {@link Ext.CompositeElement},
  23. * {@link Ext.draw.Sprite}, {@link Ext.draw.CompositeSprite}, and {@link Ext.Component}. Note that Components
  24. * have a limited subset of what attributes can be animated such as top, left, x, y, height, width, and
  25. * opacity (color, paddings, and margins can not be animated).
  26. *
  27. * ## Animation Basics
  28. *
  29. * All animations require three things - `easing`, `duration`, and `to` (the final end value for each property)
  30. * you wish to animate. Easing and duration are defaulted values specified below.
  31. * Easing describes how the intermediate values used during a transition will be calculated.
  32. * {@link Ext.fx.Anim#easing Easing} allows for a transition to change speed over its duration.
  33. * You may use the defaults for easing and duration, but you must always set a
  34. * {@link Ext.fx.Anim#to to} property which is the end value for all animations.
  35. *
  36. * Popular element 'to' configurations are:
  37. *
  38. * - opacity
  39. * - x
  40. * - y
  41. * - color
  42. * - height
  43. * - width
  44. *
  45. * Popular sprite 'to' configurations are:
  46. *
  47. * - translation
  48. * - path
  49. * - scale
  50. * - stroke
  51. * - rotation
  52. *
  53. * The default duration for animations is 250 (which is a 1/4 of a second). Duration is denoted in
  54. * milliseconds. Therefore 1 second is 1000, 1 minute would be 60000, and so on. The default easing curve
  55. * used for all animations is 'ease'. Popular easing functions are included and can be found in {@link Ext.fx.Anim#easing Easing}.
  56. *
  57. * For example, a simple animation to fade out an element with a default easing and duration:
  58. *
  59. * var p1 = Ext.get('myElementId');
  60. *
  61. * p1.animate({
  62. * to: {
  63. * opacity: 0
  64. * }
  65. * });
  66. *
  67. * To make this animation fade out in a tenth of a second:
  68. *
  69. * var p1 = Ext.get('myElementId');
  70. *
  71. * p1.animate({
  72. * duration: 100,
  73. * to: {
  74. * opacity: 0
  75. * }
  76. * });
  77. *
  78. * ## Animation Queues
  79. *
  80. * By default all animations are added to a queue which allows for animation via a chain-style API.
  81. * For example, the following code will queue 4 animations which occur sequentially (one right after the other):
  82. *
  83. * p1.animate({
  84. * to: {
  85. * x: 500
  86. * }
  87. * }).animate({
  88. * to: {
  89. * y: 150
  90. * }
  91. * }).animate({
  92. * to: {
  93. * backgroundColor: '#f00' //red
  94. * }
  95. * }).animate({
  96. * to: {
  97. * opacity: 0
  98. * }
  99. * });
  100. *
  101. * You can change this behavior by calling the {@link Ext.util.Animate#syncFx syncFx} method and all
  102. * subsequent animations for the specified target will be run concurrently (at the same time).
  103. *
  104. * p1.syncFx(); //this will make all animations run at the same time
  105. *
  106. * p1.animate({
  107. * to: {
  108. * x: 500
  109. * }
  110. * }).animate({
  111. * to: {
  112. * y: 150
  113. * }
  114. * }).animate({
  115. * to: {
  116. * backgroundColor: '#f00' //red
  117. * }
  118. * }).animate({
  119. * to: {
  120. * opacity: 0
  121. * }
  122. * });
  123. *
  124. * This works the same as:
  125. *
  126. * p1.animate({
  127. * to: {
  128. * x: 500,
  129. * y: 150,
  130. * backgroundColor: '#f00' //red
  131. * opacity: 0
  132. * }
  133. * });
  134. *
  135. * The {@link Ext.util.Animate#stopAnimation stopAnimation} method can be used to stop any
  136. * currently running animations and clear any queued animations.
  137. *
  138. * ## Animation Keyframes
  139. *
  140. * You can also set up complex animations with {@link Ext.fx.Anim#keyframes keyframes} which follow the
  141. * CSS3 Animation configuration pattern. Note rotation, translation, and scaling can only be done for sprites.
  142. * The previous example can be written with the following syntax:
  143. *
  144. * p1.animate({
  145. * duration: 1000, //one second total
  146. * keyframes: {
  147. * 25: { //from 0 to 250ms (25%)
  148. * x: 0
  149. * },
  150. * 50: { //from 250ms to 500ms (50%)
  151. * y: 0
  152. * },
  153. * 75: { //from 500ms to 750ms (75%)
  154. * backgroundColor: '#f00' //red
  155. * },
  156. * 100: { //from 750ms to 1sec
  157. * opacity: 0
  158. * }
  159. * }
  160. * });
  161. *
  162. * ## Animation Events
  163. *
  164. * Each animation you create has events for {@link Ext.fx.Anim#beforeanimate beforeanimate},
  165. * {@link Ext.fx.Anim#afteranimate afteranimate}, and {@link Ext.fx.Anim#lastframe lastframe}.
  166. * Keyframed animations adds an additional {@link Ext.fx.Animator#keyframe keyframe} event which
  167. * fires for each keyframe in your animation.
  168. *
  169. * All animations support the {@link Ext.util.Observable#listeners listeners} configuration to attact functions to these events.
  170. *
  171. * startAnimate: function() {
  172. * var p1 = Ext.get('myElementId');
  173. * p1.animate({
  174. * duration: 100,
  175. * to: {
  176. * opacity: 0
  177. * },
  178. * listeners: {
  179. * beforeanimate: function() {
  180. * // Execute my custom method before the animation
  181. * this.myBeforeAnimateFn();
  182. * },
  183. * afteranimate: function() {
  184. * // Execute my custom method after the animation
  185. * this.myAfterAnimateFn();
  186. * },
  187. * scope: this
  188. * });
  189. * },
  190. * myBeforeAnimateFn: function() {
  191. * // My custom logic
  192. * },
  193. * myAfterAnimateFn: function() {
  194. * // My custom logic
  195. * }
  196. *
  197. * Due to the fact that animations run asynchronously, you can determine if an animation is currently
  198. * running on any target by using the {@link Ext.util.Animate#getActiveAnimation getActiveAnimation}
  199. * method. This method will return false if there are no active animations or return the currently
  200. * running {@link Ext.fx.Anim} instance.
  201. *
  202. * In this example, we're going to wait for the current animation to finish, then stop any other
  203. * queued animations before we fade our element's opacity to 0:
  204. *
  205. * var curAnim = p1.getActiveAnimation();
  206. * if (curAnim) {
  207. * curAnim.on('afteranimate', function() {
  208. * p1.stopAnimation();
  209. * p1.animate({
  210. * to: {
  211. * opacity: 0
  212. * }
  213. * });
  214. * });
  215. * }
  216. */
  217. Ext.define('Ext.util.Animate', {
  218. uses: ['Ext.fx.Manager', 'Ext.fx.Anim'],
  219. <span id='Ext-util-Animate-method-animate'> /**
  220. </span> * Performs custom animation on this object.
  221. *
  222. * This method is applicable to both the {@link Ext.Component Component} class and the {@link Ext.draw.Sprite Sprite}
  223. * class. It performs animated transitions of certain properties of this object over a specified timeline.
  224. *
  225. * ### Animating a {@link Ext.Component Component}
  226. *
  227. * When animating a Component, the following properties may be specified in `from`, `to`, and `keyframe` objects:
  228. *
  229. * - `x` - The Component's page X position in pixels.
  230. *
  231. * - `y` - The Component's page Y position in pixels
  232. *
  233. * - `left` - The Component's `left` value in pixels.
  234. *
  235. * - `top` - The Component's `top` value in pixels.
  236. *
  237. * - `width` - The Component's `width` value in pixels.
  238. *
  239. * - `width` - The Component's `width` value in pixels.
  240. *
  241. * - `dynamic` - Specify as true to update the Component's layout (if it is a Container) at every frame of the animation.
  242. * *Use sparingly as laying out on every intermediate size change is an expensive operation.*
  243. *
  244. * For example, to animate a Window to a new size, ensuring that its internal layout and any shadow is correct:
  245. *
  246. * myWindow = Ext.create('Ext.window.Window', {
  247. * title: 'Test Component animation',
  248. * width: 500,
  249. * height: 300,
  250. * layout: {
  251. * type: 'hbox',
  252. * align: 'stretch'
  253. * },
  254. * items: [{
  255. * title: 'Left: 33%',
  256. * margins: '5 0 5 5',
  257. * flex: 1
  258. * }, {
  259. * title: 'Left: 66%',
  260. * margins: '5 5 5 5',
  261. * flex: 2
  262. * }]
  263. * });
  264. * myWindow.show();
  265. * myWindow.header.el.on('click', function() {
  266. * myWindow.animate({
  267. * to: {
  268. * width: (myWindow.getWidth() == 500) ? 700 : 500,
  269. * height: (myWindow.getHeight() == 300) ? 400 : 300
  270. * }
  271. * });
  272. * });
  273. *
  274. * For performance reasons, by default, the internal layout is only updated when the Window reaches its final `&quot;to&quot;`
  275. * size. If dynamic updating of the Window's child Components is required, then configure the animation with
  276. * `dynamic: true` and the two child items will maintain their proportions during the animation.
  277. *
  278. * @param {Object} config Configuration for {@link Ext.fx.Anim}.
  279. * Note that the {@link Ext.fx.Anim#to to} config is required.
  280. * @return {Object} this
  281. */
  282. animate: function(animObj) {
  283. var me = this;
  284. if (Ext.fx.Manager.hasFxBlock(me.id)) {
  285. return me;
  286. }
  287. Ext.fx.Manager.queueFx(new Ext.fx.Anim(me.anim(animObj)));
  288. return this;
  289. },
  290. // @private - process the passed fx configuration.
  291. anim: function(config) {
  292. if (!Ext.isObject(config)) {
  293. return (config) ? {} : false;
  294. }
  295. var me = this;
  296. if (config.stopAnimation) {
  297. me.stopAnimation();
  298. }
  299. Ext.applyIf(config, Ext.fx.Manager.getFxDefaults(me.id));
  300. return Ext.apply({
  301. target: me,
  302. paused: true
  303. }, config);
  304. },
  305. <span id='Ext-util-Animate-method-stopFx'> /**
  306. </span> * Stops any running effects and clears this object's internal effects queue if it contains any additional effects
  307. * that haven't started yet.
  308. * @deprecated 4.0 Replaced by {@link #stopAnimation}
  309. * @return {Ext.Element} The Element
  310. * @method
  311. */
  312. stopFx: Ext.Function.alias(Ext.util.Animate, 'stopAnimation'),
  313. <span id='Ext-util-Animate-method-stopAnimation'> /**
  314. </span> * Stops any running effects and clears this object's internal effects queue if it contains any additional effects
  315. * that haven't started yet.
  316. * @return {Ext.Element} The Element
  317. */
  318. stopAnimation: function() {
  319. Ext.fx.Manager.stopAnimation(this.id);
  320. return this;
  321. },
  322. <span id='Ext-util-Animate-method-syncFx'> /**
  323. </span> * Ensures that all effects queued after syncFx is called on this object are run concurrently. This is the opposite
  324. * of {@link #sequenceFx}.
  325. * @return {Object} this
  326. */
  327. syncFx: function() {
  328. Ext.fx.Manager.setFxDefaults(this.id, {
  329. concurrent: true
  330. });
  331. return this;
  332. },
  333. <span id='Ext-util-Animate-method-sequenceFx'> /**
  334. </span> * Ensures that all effects queued after sequenceFx is called on this object are run in sequence. This is the
  335. * opposite of {@link #syncFx}.
  336. * @return {Object} this
  337. */
  338. sequenceFx: function() {
  339. Ext.fx.Manager.setFxDefaults(this.id, {
  340. concurrent: false
  341. });
  342. return this;
  343. },
  344. <span id='Ext-util-Animate-method-hasActiveFx'> /**
  345. </span> * @deprecated 4.0 Replaced by {@link #getActiveAnimation}
  346. * @inheritdoc Ext.util.Animate#getActiveAnimation
  347. * @method
  348. */
  349. hasActiveFx: Ext.Function.alias(Ext.util.Animate, 'getActiveAnimation'),
  350. <span id='Ext-util-Animate-method-getActiveAnimation'> /**
  351. </span> * Returns the current animation if this object has any effects actively running or queued, else returns false.
  352. * @return {Ext.fx.Anim/Boolean} Anim if element has active effects, else false
  353. */
  354. getActiveAnimation: function() {
  355. return Ext.fx.Manager.getActiveAnimation(this.id);
  356. }
  357. }, function(){
  358. // Apply Animate mixin manually until Element is defined in the proper 4.x way
  359. Ext.applyIf(Ext.Element.prototype, this.prototype);
  360. // We need to call this again so the animation methods get copied over to CE
  361. Ext.CompositeElementLite.importElementMethods();
  362. });</pre>
  363. </body>
  364. </html>