1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-fx.Animator'>/**
2 </span> * @class Ext.fx.Animator
5 This class is used to run keyframe based animations, which follows the CSS3 based animation structure.
6 Keyframe animations differ from typical from/to animations in that they offer the ability to specify values
7 at various points throughout the animation.
10 The {@link #keyframes} option is the most important part of specifying an animation when using this
11 class. A key frame is a point in a particular animation. We represent this as a percentage of the
12 total animation duration. At each key frame, we can specify the target values at that time. Note that
13 you *must* specify the values at 0% and 100%, the start and ending values. There is also a {@link keyframe}
14 event that fires after each key frame is reached.
17 In the example below, we modify the values of the element at each fifth throughout the animation.
19 Ext.create('Ext.fx.Animator', {
20 target: Ext.getBody().createChild({
24 'background-color': 'red'
27 duration: 10000, // 10 seconds
31 backgroundColor: 'FF0000'
39 backgroundColor: '0000FF'
51 backgroundColor: '00FF00'
58 Ext.define('Ext.fx.Animator', {
60 /* Begin Definitions */
63 observable: 'Ext.util.Observable'
66 requires: ['Ext.fx.Manager'],
72 <span id='Ext-fx.Animator-cfg-duration'> /**
73 </span> * @cfg {Number} duration
74 * Time in milliseconds for the animation to last. Defaults to 250.
78 <span id='Ext-fx.Animator-cfg-delay'> /**
79 </span> * @cfg {Number} delay
80 * Time to delay before starting the animation. Defaults to 0.
84 /* private used to track a delayed starting time */
87 <span id='Ext-fx.Animator-cfg-dynamic'> /**
88 </span> * @cfg {Boolean} dynamic
89 * 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.
93 <span id='Ext-fx.Animator-cfg-easing'> /**
94 </span> * @cfg {String} easing
96 This describes how the intermediate values used during a transition will be calculated. It allows for a transition to change
97 speed over its duration.
109 - cubic-bezier(x1, y1, x2, y2)
111 Note that cubic-bezier will create a custom easing curve following the CSS3 transition-timing-function specification `{@link http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag}`. The four values specify points P1 and P2 of the curve
112 as (x1, y1, x2, y2). All values must be in the range [0, 1] or the definition is invalid.
118 <span id='Ext-fx.Animator-property-running'> /**
119 </span> * Flag to determine if the animation has started
125 <span id='Ext-fx.Animator-property-paused'> /**
126 </span> * Flag to determine if the animation is paused. Only set this to true if you need to
127 * keep the Anim instance around to be unpaused later; otherwise call {@link #end}.
133 <span id='Ext-fx.Animator-property-damper'> /**
138 <span id='Ext-fx.Animator-cfg-iterations'> /**
139 </span> * @cfg {Number} iterations
140 * Number of times to execute the animation. Defaults to 1.
144 <span id='Ext-fx.Animator-property-currentIteration'> /**
145 </span> * Current iteration the animation is running.
146 * @property currentIteration
151 <span id='Ext-fx.Animator-property-keyframeStep'> /**
152 </span> * Current keyframe step of the animation.
153 * @property keyframeStep
158 <span id='Ext-fx.Animator-property-animKeyFramesRE'> /**
161 animKeyFramesRE: /^(from|to|\d+%?)$/,
163 <span id='Ext-fx.Animator-cfg-target'> /**
164 </span> * @cfg {Ext.fx.target} target
165 * The Ext.fx.target to apply the animation to. If not specified during initialization, this can be passed to the applyAnimator
166 * method to apply the same animation to many targets.
169 <span id='Ext-fx.Animator-cfg-keyframes'> /**
170 </span> * @cfg {Object} keyframes
171 * Animation keyframes follow the CSS3 Animation configuration pattern. 'from' is always considered '0%' and 'to'
172 * is considered '100%'.<b>Every keyframe declaration must have a keyframe rule for 0% and 100%, possibly defined using
173 * "from" or "to"</b>. A keyframe declaration without these keyframe selectors is invalid and will not be available for
174 * animation. The keyframe declaration for a keyframe rule consists of properties and values. Properties that are unable to
175 * be animated are ignored in these rules, with the exception of 'easing' which can be changed at each keyframe. For example:
176 <pre><code>
191 </code></pre>
193 constructor: function(config) {
195 config = Ext.apply(me, config || {});
197 me.id = Ext.id(null, 'ext-animator-');
199 <span id='Ext-fx.Animator-event-beforeanimate'> /**
200 </span> * @event beforeanimate
201 * Fires before the animation starts. A handler can return false to cancel the animation.
202 * @param {Ext.fx.Animator} this
205 <span id='Ext-fx.Animator-event-keyframe'> /**
206 </span> * @event keyframe
207 * Fires at each keyframe.
208 * @param {Ext.fx.Animator} this
209 * @param {Number} keyframe step number
212 <span id='Ext-fx.Animator-event-afteranimate'> /**
213 </span> * @event afteranimate
214 * Fires when the animation is complete.
215 * @param {Ext.fx.Animator} this
216 * @param {Date} startTime
220 me.mixins.observable.constructor.call(me, config);
222 me.createTimeline(me.keyframes);
224 me.applyAnimator(me.target);
225 Ext.fx.Manager.addAnim(me);
229 <span id='Ext-fx.Animator-method-sorter'> /**
232 sorter: function (a, b) {
233 return a.pct - b.pct;
236 <span id='Ext-fx.Animator-method-createTimeline'> /**
238 * Takes the given keyframe configuration object and converts it into an ordered array with the passed attributes per keyframe
239 * or applying the 'to' configuration to all keyframes. Also calculates the proper animation duration per keyframe.
241 createTimeline: function(keyframes) {
245 duration = me.duration,
246 prevMs, ms, i, ln, pct, anim, nextAnim, attr;
248 for (pct in keyframes) {
249 if (keyframes.hasOwnProperty(pct) && me.animKeyFramesRE.test(pct)) {
250 attr = {attrs: Ext.apply(keyframes[pct], to)};
251 // CSS3 spec allow for from/to to be specified.
252 if (pct == "from") {
255 else if (pct == "to") {
258 // convert % values into integers
259 attr.pct = parseInt(pct, 10);
263 // Sort by pct property
264 Ext.Array.sort(attrs, me.sorter);
266 //if (attrs[0].pct) {
267 // attrs.unshift({pct: 0, attrs: element.attrs});
271 for (i = 0; i < ln; i++) {
272 prevMs = (attrs[i - 1]) ? duration * (attrs[i - 1].pct / 100) : 0;
273 ms = duration * (attrs[i].pct / 100);
275 duration: ms - prevMs,
276 attrs: attrs[i].attrs
281 <span id='Ext-fx.Animator-property-applyAnimator'> /**
282 </span> * Applies animation to the Ext.fx.target
285 * @type string/object
287 applyAnimator: function(target) {
290 timeline = me.timeline,
291 reverse = me.reverse,
292 ln = timeline.length,
293 anim, easing, damper, initial, attrs, lastAttrs, i;
295 if (me.fireEvent('beforeanimate', me) !== false) {
296 for (i = 0; i < ln; i++) {
299 easing = attrs.easing || me.easing;
300 damper = attrs.damper || me.damper;
303 anim = Ext.create('Ext.fx.Anim', {
307 duration: anim.duration,
313 me.animations = anims;
314 me.target = anim.target;
315 for (i = 0; i < ln - 1; i++) {
317 anim.nextAnim = anims[i + 1];
318 anim.on('afteranimate', function() {
319 this.nextAnim.paused = false;
321 anim.on('afteranimate', function() {
322 this.fireEvent('keyframe', this, ++this.keyframeStep);
325 anims[ln - 1].on('afteranimate', function() {
333 * Fires beforeanimate and sets the running flag.
335 start: function(startTime) {
338 delayStart = me.delayStart,
342 me.delayStart = startTime;
346 delayDelta = startTime - delayStart;
347 if (delayDelta < delay) {
351 // Compensate for frame delay;
352 startTime = new Date(delayStart.getTime() + delay);
356 if (me.fireEvent('beforeanimate', me) !== false) {
357 me.startTime = startTime;
359 me.animations[me.keyframeStep].paused = false;
365 * Perform lastFrame cleanup and handle iterations
366 * @returns a hash of the new attributes.
368 lastFrame: function() {
370 iter = me.iterations,
371 iterCount = me.currentIteration;
374 if (iterCount < iter) {
375 me.startTime = new Date();
376 me.currentIteration = iterCount;
378 me.applyAnimator(me.target);
379 me.animations[me.keyframeStep].paused = false;
382 me.currentIteration = 0;
388 * Fire afteranimate event and end the animation. Usually called automatically when the
389 * animation reaches its final frame, but can also be called manually to pre-emptively
390 * stop and destroy the running animation.
394 me.fireEvent('afteranimate', me, me.startTime, new Date() - me.startTime);
396 });</pre></pre></body></html>