4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../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.
42 * var myComponent = Ext.create('Ext.Component', {
43 * renderTo: document.body,
46 * style: 'border: 1px solid red;'
50 * target: myComponent,
53 * width: 400 //starting width 400
56 * width: 300, //end width 300
57 * height: 300 // end width 300
61 Ext.define('Ext.fx.Anim', {
63 /* Begin Definitions */
66 observable: 'Ext.util.Observable'
69 requires: ['Ext.fx.Manager', 'Ext.fx.Animator', 'Ext.fx.Easing', 'Ext.fx.CubicBezier', 'Ext.fx.PropertyHandler'],
74 <span id='Ext-fx-Anim-cfg-duration'> /**
75 </span> * @cfg {Number} duration
76 * Time in milliseconds for a single animation to last. Defaults to 250. If the {@link #iterations} property is
77 * specified, then each animate will take the same duration for each iteration.
81 <span id='Ext-fx-Anim-cfg-delay'> /**
82 </span> * @cfg {Number} delay
83 * Time to delay before starting the animation. Defaults to 0.
87 /* private used to track a delayed starting time */
90 <span id='Ext-fx-Anim-cfg-dynamic'> /**
91 </span> * @cfg {Boolean} dynamic
92 * 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.
96 <span id='Ext-fx-Anim-cfg-easing'> /**
97 </span> * @cfg {String} easing
98 This describes how the intermediate values used during a transition will be calculated. It allows for a transition to change
99 speed over its duration.
111 -cubic-bezier(x1, y1, x2, y2)
113 Note that cubic-bezier will create a custom easing curve following the CSS3 [transition-timing-function][0]
114 specification. The four values specify points P1 and P2 of the curve as (x1, y1, x2, y2). All values must
115 be in the range [0, 1] or the definition is invalid.
117 [0]: http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag
123 <span id='Ext-fx-Anim-cfg-keyframes'> /**
124 </span> * @cfg {Object} keyframes
125 * Animation keyframes follow the CSS3 Animation configuration pattern. 'from' is always considered '0%' and 'to'
126 * is considered '100%'.<b>Every keyframe declaration must have a keyframe rule for 0% and 100%, possibly defined using
127 * "from" or "to"</b>. A keyframe declaration without these keyframe selectors is invalid and will not be available for
128 * animation. The keyframe declaration for a keyframe rule consists of properties and values. Properties that are unable to
129 * be animated are ignored in these rules, with the exception of 'easing' which can be changed at each keyframe. For example:
130 <pre><code>
145 </code></pre>
148 <span id='Ext-fx-Anim-property-damper'> /**
153 <span id='Ext-fx-Anim-property-bezierRE'> /**
156 bezierRE: /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
158 <span id='Ext-fx-Anim-cfg-reverse'> /**
159 </span> * Run the animation from the end to the beginning
161 * @cfg {Boolean} reverse
165 <span id='Ext-fx-Anim-property-running'> /**
166 </span> * Flag to determine if the animation has started
172 <span id='Ext-fx-Anim-property-paused'> /**
173 </span> * Flag to determine if the animation is paused. Only set this to true if you need to
174 * keep the Anim instance around to be unpaused later; otherwise call {@link #end}.
180 <span id='Ext-fx-Anim-cfg-iterations'> /**
181 </span> * Number of times to execute the animation. Defaults to 1.
182 * @cfg {int} iterations
186 <span id='Ext-fx-Anim-cfg-alternate'> /**
187 </span> * Used in conjunction with iterations to reverse the animation each time an iteration completes.
188 * @cfg {Boolean} alternate
193 <span id='Ext-fx-Anim-property-currentIteration'> /**
194 </span> * Current iteration the animation is running.
195 * @property currentIteration
200 <span id='Ext-fx-Anim-property-startTime'> /**
201 </span> * Starting time of the animation.
202 * @property startTime
207 <span id='Ext-fx-Anim-property-propHandlers'> /**
208 </span> * Contains a cache of the interpolators to be used.
210 * @property propHandlers
214 <span id='Ext-fx-Anim-cfg-target'> /**
215 </span> * @cfg {String/Object} target
216 * The {@link Ext.fx.target.Target} to apply the animation to. This should only be specified when creating an Ext.fx.Anim directly.
217 * The target does not need to be a {@link Ext.fx.target.Target} instance, it can be the underlying object. For example, you can
218 * pass a Component, Element or Sprite as the target and the Anim will create the appropriate {@link Ext.fx.target.Target} object
222 <span id='Ext-fx-Anim-cfg-from'> /**
223 </span> * @cfg {Object} from
224 * An object containing property/value pairs for the beginning of the animation. If not specified, the current state of the
225 * Ext.fx.target will be used. For example:
226 <pre><code>
228 opacity: 0, // Transparent
229 color: '#ffffff', // White
232 </code></pre>
235 <span id='Ext-fx-Anim-cfg-to'> /**
236 </span> * @cfg {Object} to
237 * An object containing property/value pairs for the end of the animation. For example:
238 <pre><code>
240 opacity: 1, // Opaque
241 color: '#00ff00', // Green
244 </code></pre>
248 constructor: function(config) {
250 config = config || {};
251 // If keyframes are passed, they really want an Animator instead.
252 if (config.keyframes) {
253 return Ext.create('Ext.fx.Animator', config);
255 config = Ext.apply(me, config);
256 if (me.from === undefined) {
259 me.propHandlers = {};
261 me.target = Ext.fx.Manager.createTarget(me.target);
262 me.easingFn = Ext.fx.Easing[me.easing];
263 me.target.dynamic = me.dynamic;
265 // If not a pre-defined curve, try a cubic-bezier
267 me.easingFn = String(me.easing).match(me.bezierRE);
268 if (me.easingFn && me.easingFn.length == 5) {
269 var curve = me.easingFn;
270 me.easingFn = Ext.fx.cubicBezier(+curve[1], +curve[2], +curve[3], +curve[4]);
273 me.id = Ext.id(null, 'ext-anim-');
274 Ext.fx.Manager.addAnim(me);
276 <span id='Ext-fx-Anim-event-beforeanimate'> /**
277 </span> * @event beforeanimate
278 * Fires before the animation starts. A handler can return false to cancel the animation.
279 * @param {Ext.fx.Anim} this
282 <span id='Ext-fx-Anim-event-afteranimate'> /**
283 </span> * @event afteranimate
284 * Fires when the animation is complete.
285 * @param {Ext.fx.Anim} this
286 * @param {Date} startTime
289 <span id='Ext-fx-Anim-event-lastframe'> /**
290 </span> * @event lastframe
291 * Fires when the animation's last frame has been set.
292 * @param {Ext.fx.Anim} this
293 * @param {Date} startTime
297 me.mixins.observable.constructor.call(me, config);
298 if (config.callback) {
299 me.on('afteranimate', config.callback, config.scope);
304 <span id='Ext-fx-Anim-method-setAttr'> /**
306 * Helper to the target
308 setAttr: function(attr, value) {
309 return Ext.fx.Manager.items.get(this.id).setAttr(this.target, attr, value);
314 * Set up the initial currentAttrs hash.
316 initAttrs: function() {
320 initialFrom = me.initialFrom || {},
322 start, end, propHandler, attr;
325 if (to.hasOwnProperty(attr)) {
326 start = me.target.getAttr(attr, from[attr]);
328 // Use default (numeric) property handler
329 if (!Ext.fx.PropertyHandler[attr]) {
330 if (Ext.isObject(end)) {
331 propHandler = me.propHandlers[attr] = Ext.fx.PropertyHandler.object;
333 propHandler = me.propHandlers[attr] = Ext.fx.PropertyHandler.defaultHandler;
336 // Use custom handler
338 propHandler = me.propHandlers[attr] = Ext.fx.PropertyHandler[attr];
340 out[attr] = propHandler.get(start, end, me.damper, initialFrom[attr], attr);
343 me.currentAttrs = out;
348 * Fires beforeanimate and sets the running flag.
350 start: function(startTime) {
353 delayStart = me.delayStart,
357 me.delayStart = startTime;
361 delayDelta = startTime - delayStart;
362 if (delayDelta < delay) {
366 // Compensate for frame delay;
367 startTime = new Date(delayStart.getTime() + delay);
371 if (me.fireEvent('beforeanimate', me) !== false) {
372 me.startTime = startTime;
373 if (!me.paused && !me.currentAttrs) {
382 * Calculate attribute value at the passed timestamp.
383 * @returns a hash of the new attributes.
385 runAnim: function(elapsedTime) {
387 attrs = me.currentAttrs,
388 duration = me.duration,
389 easingFn = me.easingFn,
390 propHandlers = me.propHandlers,
392 easing, values, attr, lastFrame;
394 if (elapsedTime >= duration) {
395 elapsedTime = duration;
399 elapsedTime = duration - elapsedTime;
402 for (attr in attrs) {
403 if (attrs.hasOwnProperty(attr)) {
404 values = attrs[attr];
405 easing = lastFrame ? 1 : easingFn(elapsedTime / duration);
406 ret[attr] = propHandlers[attr].set(values, easing);
414 * Perform lastFrame cleanup and handle iterations
415 * @returns a hash of the new attributes.
417 lastFrame: function() {
419 iter = me.iterations,
420 iterCount = me.currentIteration;
423 if (iterCount < iter) {
425 me.reverse = !me.reverse;
427 me.startTime = new Date();
428 me.currentIteration = iterCount;
429 // Turn off paused for CSS3 Transitions
433 me.currentIteration = 0;
435 me.fireEvent('lastframe', me, me.startTime);
440 * Fire afteranimate event and end the animation. Usually called automatically when the
441 * animation reaches its final frame, but can also be called manually to pre-emptively
442 * stop and destroy the running animation.
449 Ext.fx.Manager.removeAnim(me);
450 me.fireEvent('afteranimate', me, me.startTime);
453 // Set flag to indicate that Fx is available. Class might not be available immediately.