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-Animator'>/**
19 </span> * @class Ext.fx.Animator
22 This class is used to run keyframe based animations, which follows the CSS3 based animation structure.
23 Keyframe animations differ from typical from/to animations in that they offer the ability to specify values
24 at various points throughout the animation.
27 The {@link #keyframes} option is the most important part of specifying an animation when using this
28 class. A key frame is a point in a particular animation. We represent this as a percentage of the
29 total animation duration. At each key frame, we can specify the target values at that time. Note that
30 you *must* specify the values at 0% and 100%, the start and ending values. There is also a {@link keyframe}
31 event that fires after each key frame is reached.
34 In the example below, we modify the values of the element at each fifth throughout the animation.
36 Ext.create('Ext.fx.Animator', {
37 target: Ext.getBody().createChild({
41 'background-color': 'red'
44 duration: 10000, // 10 seconds
48 backgroundColor: 'FF0000'
56 backgroundColor: '0000FF'
68 backgroundColor: '00FF00'
75 Ext.define('Ext.fx.Animator', {
77 /* Begin Definitions */
80 observable: 'Ext.util.Observable'
83 requires: ['Ext.fx.Manager'],
89 <span id='Ext-fx-Animator-cfg-duration'> /**
90 </span> * @cfg {Number} duration
91 * Time in milliseconds for the animation to last. Defaults to 250.
95 <span id='Ext-fx-Animator-cfg-delay'> /**
96 </span> * @cfg {Number} delay
97 * Time to delay before starting the animation. Defaults to 0.
101 /* private used to track a delayed starting time */
104 <span id='Ext-fx-Animator-cfg-dynamic'> /**
105 </span> * @cfg {Boolean} dynamic
106 * 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.
110 <span id='Ext-fx-Animator-cfg-easing'> /**
111 </span> * @cfg {String} easing
113 This describes how the intermediate values used during a transition will be calculated. It allows for a transition to change
114 speed over its duration.
126 - cubic-bezier(x1, y1, x2, y2)
128 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
129 as (x1, y1, x2, y2). All values must be in the range [0, 1] or the definition is invalid.
135 <span id='Ext-fx-Animator-property-running'> /**
136 </span> * Flag to determine if the animation has started
142 <span id='Ext-fx-Animator-property-paused'> /**
143 </span> * Flag to determine if the animation is paused. Only set this to true if you need to
144 * keep the Anim instance around to be unpaused later; otherwise call {@link #end}.
150 <span id='Ext-fx-Animator-property-damper'> /**
155 <span id='Ext-fx-Animator-cfg-iterations'> /**
156 </span> * @cfg {Number} iterations
157 * Number of times to execute the animation. Defaults to 1.
161 <span id='Ext-fx-Animator-property-currentIteration'> /**
162 </span> * Current iteration the animation is running.
163 * @property currentIteration
168 <span id='Ext-fx-Animator-property-keyframeStep'> /**
169 </span> * Current keyframe step of the animation.
170 * @property keyframeStep
175 <span id='Ext-fx-Animator-property-animKeyFramesRE'> /**
178 animKeyFramesRE: /^(from|to|\d+%?)$/,
180 <span id='Ext-fx-Animator-cfg-target'> /**
181 </span> * @cfg {Ext.fx.target} target
182 * The Ext.fx.target to apply the animation to. If not specified during initialization, this can be passed to the applyAnimator
183 * method to apply the same animation to many targets.
186 <span id='Ext-fx-Animator-cfg-keyframes'> /**
187 </span> * @cfg {Object} keyframes
188 * Animation keyframes follow the CSS3 Animation configuration pattern. 'from' is always considered '0%' and 'to'
189 * is considered '100%'.<b>Every keyframe declaration must have a keyframe rule for 0% and 100%, possibly defined using
190 * "from" or "to"</b>. A keyframe declaration without these keyframe selectors is invalid and will not be available for
191 * animation. The keyframe declaration for a keyframe rule consists of properties and values. Properties that are unable to
192 * be animated are ignored in these rules, with the exception of 'easing' which can be changed at each keyframe. For example:
193 <pre><code>
208 </code></pre>
210 constructor: function(config) {
212 config = Ext.apply(me, config || {});
214 me.id = Ext.id(null, 'ext-animator-');
216 <span id='Ext-fx-Animator-event-beforeanimate'> /**
217 </span> * @event beforeanimate
218 * Fires before the animation starts. A handler can return false to cancel the animation.
219 * @param {Ext.fx.Animator} this
222 <span id='Ext-fx-Animator-event-keyframe'> /**
223 </span> * @event keyframe
224 * Fires at each keyframe.
225 * @param {Ext.fx.Animator} this
226 * @param {Number} keyframe step number
229 <span id='Ext-fx-Animator-event-afteranimate'> /**
230 </span> * @event afteranimate
231 * Fires when the animation is complete.
232 * @param {Ext.fx.Animator} this
233 * @param {Date} startTime
237 me.mixins.observable.constructor.call(me, config);
239 me.createTimeline(me.keyframes);
241 me.applyAnimator(me.target);
242 Ext.fx.Manager.addAnim(me);
246 <span id='Ext-fx-Animator-method-sorter'> /**
249 sorter: function (a, b) {
250 return a.pct - b.pct;
253 <span id='Ext-fx-Animator-method-createTimeline'> /**
255 * Takes the given keyframe configuration object and converts it into an ordered array with the passed attributes per keyframe
256 * or applying the 'to' configuration to all keyframes. Also calculates the proper animation duration per keyframe.
258 createTimeline: function(keyframes) {
262 duration = me.duration,
263 prevMs, ms, i, ln, pct, anim, nextAnim, attr;
265 for (pct in keyframes) {
266 if (keyframes.hasOwnProperty(pct) && me.animKeyFramesRE.test(pct)) {
267 attr = {attrs: Ext.apply(keyframes[pct], to)};
268 // CSS3 spec allow for from/to to be specified.
269 if (pct == "from") {
272 else if (pct == "to") {
275 // convert % values into integers
276 attr.pct = parseInt(pct, 10);
280 // Sort by pct property
281 Ext.Array.sort(attrs, me.sorter);
283 //if (attrs[0].pct) {
284 // attrs.unshift({pct: 0, attrs: element.attrs});
288 for (i = 0; i < ln; i++) {
289 prevMs = (attrs[i - 1]) ? duration * (attrs[i - 1].pct / 100) : 0;
290 ms = duration * (attrs[i].pct / 100);
292 duration: ms - prevMs,
293 attrs: attrs[i].attrs
298 <span id='Ext-fx-Animator-property-applyAnimator'> /**
299 </span> * Applies animation to the Ext.fx.target
302 * @type string/object
304 applyAnimator: function(target) {
307 timeline = me.timeline,
308 reverse = me.reverse,
309 ln = timeline.length,
310 anim, easing, damper, initial, attrs, lastAttrs, i;
312 if (me.fireEvent('beforeanimate', me) !== false) {
313 for (i = 0; i < ln; i++) {
316 easing = attrs.easing || me.easing;
317 damper = attrs.damper || me.damper;
320 anim = Ext.create('Ext.fx.Anim', {
324 duration: anim.duration,
330 me.animations = anims;
331 me.target = anim.target;
332 for (i = 0; i < ln - 1; i++) {
334 anim.nextAnim = anims[i + 1];
335 anim.on('afteranimate', function() {
336 this.nextAnim.paused = false;
338 anim.on('afteranimate', function() {
339 this.fireEvent('keyframe', this, ++this.keyframeStep);
342 anims[ln - 1].on('afteranimate', function() {
350 * Fires beforeanimate and sets the running flag.
352 start: function(startTime) {
355 delayStart = me.delayStart,
359 me.delayStart = startTime;
363 delayDelta = startTime - delayStart;
364 if (delayDelta < delay) {
368 // Compensate for frame delay;
369 startTime = new Date(delayStart.getTime() + delay);
373 if (me.fireEvent('beforeanimate', me) !== false) {
374 me.startTime = startTime;
376 me.animations[me.keyframeStep].paused = false;
382 * Perform lastFrame cleanup and handle iterations
383 * @returns a hash of the new attributes.
385 lastFrame: function() {
387 iter = me.iterations,
388 iterCount = me.currentIteration;
391 if (iterCount < iter) {
392 me.startTime = new Date();
393 me.currentIteration = iterCount;
395 me.applyAnimator(me.target);
396 me.animations[me.keyframeStep].paused = false;
399 me.currentIteration = 0;
405 * Fire afteranimate event and end the animation. Usually called automatically when the
406 * animation reaches its final frame, but can also be called manually to pre-emptively
407 * stop and destroy the running animation.
411 me.fireEvent('afteranimate', me, me.startTime, new Date() - me.startTime);