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; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-fx-Anim'>/**
19 </span> * @class Ext.fx.Anim
21 * This class manages animation for a specific {@link #target}. The animation allows
22 * animation of various properties on the target, such as size, position, color and others.
24 * ## Starting Conditions
25 * The starting conditions for the animation are provided by the {@link #from} configuration.
26 * Any/all of the properties in the {@link #from} configuration can be specified. If a particular
27 * property is not defined, the starting value for that property will be read directly from the target.
30 * The ending conditions for the animation are provided by the {@link #to} configuration. These mark
31 * the final values once the animations has finished. The values in the {@link #from} can mirror
32 * those in the {@link #to} configuration to provide a starting point.
35 * - {@link #duration}: Specifies the time period of the animation.
36 * - {@link #easing}: Specifies the easing of the animation.
37 * - {@link #iterations}: Allows the animation to repeat a number of times.
38 * - {@link #alternate}: Used in conjunction with {@link #iterations}, reverses the direction every second iteration.
43 * var myComponent = Ext.create('Ext.Component', {
44 * renderTo: document.body,
47 * style: 'border: 1px solid red;'
50 * Ext.create('Ext.fx.Anim', {
51 * target: myComponent,
54 * width: 400 //starting width 400
57 * width: 300, //end width 300
58 * height: 300 // end width 300
62 Ext.define('Ext.fx.Anim', {
64 /* Begin Definitions */
67 observable: 'Ext.util.Observable'
70 requires: ['Ext.fx.Manager', 'Ext.fx.Animator', 'Ext.fx.Easing', 'Ext.fx.CubicBezier', 'Ext.fx.PropertyHandler'],
76 <span id='Ext-fx-Anim-cfg-callback'> /**
77 </span> * @cfg {Function} callback
78 * A function to be run after the animation has completed.
81 <span id='Ext-fx-Anim-cfg-scope'> /**
82 </span> * @cfg {Function} scope
83 * The scope that the {@link #callback} function will be called with
86 <span id='Ext-fx-Anim-cfg-duration'> /**
87 </span> * @cfg {Number} duration
88 * Time in milliseconds for a single animation to last. Defaults to 250. If the {@link #iterations} property is
89 * specified, then each animate will take the same duration for each iteration.
93 <span id='Ext-fx-Anim-cfg-delay'> /**
94 </span> * @cfg {Number} delay
95 * Time to delay before starting the animation. Defaults to 0.
99 /* private used to track a delayed starting time */
102 <span id='Ext-fx-Anim-cfg-dynamic'> /**
103 </span> * @cfg {Boolean} dynamic
104 * Currently only for Component Animation: Only set a component's outer element size bypassing layouts. Set to true to do full layouts for every frame of the animation. Defaults to false.
108 <span id='Ext-fx-Anim-cfg-easing'> /**
109 </span> * @cfg {String} easing
110 This describes how the intermediate values used during a transition will be calculated. It allows for a transition to change
111 speed over its duration.
123 -cubic-bezier(x1, y1, x2, y2)
125 Note that cubic-bezier will create a custom easing curve following the CSS3 [transition-timing-function][0]
126 specification. The four values specify points P1 and P2 of the curve as (x1, y1, x2, y2). All values must
127 be in the range [0, 1] or the definition is invalid.
129 [0]: http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag
135 <span id='Ext-fx-Anim-cfg-keyframes'> /**
136 </span> * @cfg {Object} keyframes
137 * Animation keyframes follow the CSS3 Animation configuration pattern. 'from' is always considered '0%' and 'to'
138 * is considered '100%'.<b>Every keyframe declaration must have a keyframe rule for 0% and 100%, possibly defined using
139 * "from" or "to"</b>. A keyframe declaration without these keyframe selectors is invalid and will not be available for
140 * animation. The keyframe declaration for a keyframe rule consists of properties and values. Properties that are unable to
141 * be animated are ignored in these rules, with the exception of 'easing' which can be changed at each keyframe. For example:
142 <pre><code>
157 </code></pre>
160 <span id='Ext-fx-Anim-property-damper'> /**
165 <span id='Ext-fx-Anim-property-bezierRE'> /**
168 bezierRE: /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
170 <span id='Ext-fx-Anim-cfg-reverse'> /**
171 </span> * Run the animation from the end to the beginning
173 * @cfg {Boolean} reverse
177 <span id='Ext-fx-Anim-property-running'> /**
178 </span> * Flag to determine if the animation has started
184 <span id='Ext-fx-Anim-property-paused'> /**
185 </span> * Flag to determine if the animation is paused. Only set this to true if you need to
186 * keep the Anim instance around to be unpaused later; otherwise call {@link #end}.
192 <span id='Ext-fx-Anim-cfg-iterations'> /**
193 </span> * Number of times to execute the animation. Defaults to 1.
194 * @cfg {Number} iterations
198 <span id='Ext-fx-Anim-cfg-alternate'> /**
199 </span> * Used in conjunction with iterations to reverse the animation each time an iteration completes.
200 * @cfg {Boolean} alternate
205 <span id='Ext-fx-Anim-property-currentIteration'> /**
206 </span> * Current iteration the animation is running.
207 * @property currentIteration
212 <span id='Ext-fx-Anim-property-startTime'> /**
213 </span> * Starting time of the animation.
214 * @property startTime
219 <span id='Ext-fx-Anim-property-propHandlers'> /**
220 </span> * Contains a cache of the interpolators to be used.
222 * @property propHandlers
226 <span id='Ext-fx-Anim-cfg-target'> /**
227 </span> * @cfg {String/Object} target
228 * The {@link Ext.fx.target.Target} to apply the animation to. This should only be specified when creating an Ext.fx.Anim directly.
229 * The target does not need to be a {@link Ext.fx.target.Target} instance, it can be the underlying object. For example, you can
230 * pass a Component, Element or Sprite as the target and the Anim will create the appropriate {@link Ext.fx.target.Target} object
234 <span id='Ext-fx-Anim-cfg-from'> /**
235 </span> * @cfg {Object} from
236 * An object containing property/value pairs for the beginning of the animation. If not specified, the current state of the
237 * Ext.fx.target will be used. For example:
238 <pre><code>
240 opacity: 0, // Transparent
241 color: '#ffffff', // White
244 </code></pre>
247 <span id='Ext-fx-Anim-cfg-to'> /**
248 </span> * @cfg {Object} to
249 * An object containing property/value pairs for the end of the animation. For example:
250 <pre><code>
252 opacity: 1, // Opaque
253 color: '#00ff00', // Green
256 </code></pre>
260 constructor: function(config) {
264 config = config || {};
265 // If keyframes are passed, they really want an Animator instead.
266 if (config.keyframes) {
267 return Ext.create('Ext.fx.Animator', config);
269 config = Ext.apply(me, config);
270 if (me.from === undefined) {
273 me.propHandlers = {};
275 me.target = Ext.fx.Manager.createTarget(me.target);
276 me.easingFn = Ext.fx.Easing[me.easing];
277 me.target.dynamic = me.dynamic;
279 // If not a pre-defined curve, try a cubic-bezier
281 me.easingFn = String(me.easing).match(me.bezierRE);
282 if (me.easingFn && me.easingFn.length == 5) {
284 me.easingFn = Ext.fx.CubicBezier.cubicBezier(+curve[1], +curve[2], +curve[3], +curve[4]);
287 me.id = Ext.id(null, 'ext-anim-');
288 Ext.fx.Manager.addAnim(me);
290 <span id='Ext-fx-Anim-event-beforeanimate'> /**
291 </span> * @event beforeanimate
292 * Fires before the animation starts. A handler can return false to cancel the animation.
293 * @param {Ext.fx.Anim} this
296 <span id='Ext-fx-Anim-event-afteranimate'> /**
297 </span> * @event afteranimate
298 * Fires when the animation is complete.
299 * @param {Ext.fx.Anim} this
300 * @param {Date} startTime
303 <span id='Ext-fx-Anim-event-lastframe'> /**
304 </span> * @event lastframe
305 * Fires when the animation's last frame has been set.
306 * @param {Ext.fx.Anim} this
307 * @param {Date} startTime
311 me.mixins.observable.constructor.call(me, config);
312 if (config.callback) {
313 me.on('afteranimate', config.callback, config.scope);
318 <span id='Ext-fx-Anim-method-setAttr'> /**
320 * Helper to the target
322 setAttr: function(attr, value) {
323 return Ext.fx.Manager.items.get(this.id).setAttr(this.target, attr, value);
326 <span id='Ext-fx-Anim-method-initAttrs'> /**
328 * Set up the initial currentAttrs hash.
330 initAttrs: function() {
334 initialFrom = me.initialFrom || {},
336 start, end, propHandler, attr;
339 if (to.hasOwnProperty(attr)) {
340 start = me.target.getAttr(attr, from[attr]);
342 // Use default (numeric) property handler
343 if (!Ext.fx.PropertyHandler[attr]) {
344 if (Ext.isObject(end)) {
345 propHandler = me.propHandlers[attr] = Ext.fx.PropertyHandler.object;
347 propHandler = me.propHandlers[attr] = Ext.fx.PropertyHandler.defaultHandler;
350 // Use custom handler
352 propHandler = me.propHandlers[attr] = Ext.fx.PropertyHandler[attr];
354 out[attr] = propHandler.get(start, end, me.damper, initialFrom[attr], attr);
357 me.currentAttrs = out;
360 <span id='Ext-fx-Anim-method-start'> /**
362 * Fires beforeanimate and sets the running flag.
364 start: function(startTime) {
367 delayStart = me.delayStart,
371 me.delayStart = startTime;
375 delayDelta = startTime - delayStart;
376 if (delayDelta < delay) {
380 // Compensate for frame delay;
381 startTime = new Date(delayStart.getTime() + delay);
385 if (me.fireEvent('beforeanimate', me) !== false) {
386 me.startTime = startTime;
387 if (!me.paused && !me.currentAttrs) {
394 <span id='Ext-fx-Anim-method-runAnim'> /**
396 * Calculate attribute value at the passed timestamp.
397 * @returns a hash of the new attributes.
399 runAnim: function(elapsedTime) {
401 attrs = me.currentAttrs,
402 duration = me.duration,
403 easingFn = me.easingFn,
404 propHandlers = me.propHandlers,
406 easing, values, attr, lastFrame;
408 if (elapsedTime >= duration) {
409 elapsedTime = duration;
413 elapsedTime = duration - elapsedTime;
416 for (attr in attrs) {
417 if (attrs.hasOwnProperty(attr)) {
418 values = attrs[attr];
419 easing = lastFrame ? 1 : easingFn(elapsedTime / duration);
420 ret[attr] = propHandlers[attr].set(values, easing);
426 <span id='Ext-fx-Anim-method-lastFrame'> /**
428 * Perform lastFrame cleanup and handle iterations
429 * @returns a hash of the new attributes.
431 lastFrame: function() {
433 iter = me.iterations,
434 iterCount = me.currentIteration;
437 if (iterCount < iter) {
439 me.reverse = !me.reverse;
441 me.startTime = new Date();
442 me.currentIteration = iterCount;
443 // Turn off paused for CSS3 Transitions
447 me.currentIteration = 0;
449 me.fireEvent('lastframe', me, me.startTime);
453 <span id='Ext-fx-Anim-method-end'> /**
454 </span> * Fire afteranimate event and end the animation. Usually called automatically when the
455 * animation reaches its final frame, but can also be called manually to pre-emptively
456 * stop and destroy the running animation.
463 Ext.fx.Manager.removeAnim(me);
464 me.fireEvent('afteranimate', me, me.startTime);
467 // Set flag to indicate that Fx is available. Class might not be available immediately.