2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('anim-base', function(Y) {
11 * The Animation Utility provides an API for creating advanced transitions.
16 * Provides the base Anim class, for animating numeric properties.
19 * @submodule anim-base
23 * A class for constructing animation instances.
30 var RUNNING = 'running',
31 START_TIME = 'startTime',
32 ELAPSED_TIME = 'elapsedTime',
36 * @description fires when an animation begins.
37 * @param {Event} ev The start event.
44 * @description fires every frame of the animation.
45 * @param {Event} ev The tween event.
52 * @description fires after the animation completes.
53 * @param {Event} ev The end event.
59 REVERSE = 'reverse', // TODO: cleanup
60 ITERATION_COUNT = 'iterationCount',
68 Y.Anim.superclass.constructor.apply(this, arguments);
69 Y.Anim._instances[Y.stamp(this)] = this;
74 Y.Anim._instances = {};
77 * Regex of properties that should use the default unit.
79 * @property RE_DEFAULT_UNIT
82 Y.Anim.RE_DEFAULT_UNIT = /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i;
85 * The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
87 * @property DEFAULT_UNIT
90 Y.Anim.DEFAULT_UNIT = 'px';
92 Y.Anim.DEFAULT_EASING = function (t, b, c, d) {
93 return c * t / d + b; // linear easing
97 * Time in milliseconds passed to setInterval for frame processing
99 * @property intervalTime
103 Y.Anim._intervalTime = 20;
106 * Bucket for custom getters and setters
108 * @property behaviors
113 get: function(anim, attr) {
114 return anim._getOffset(attr);
119 Y.Anim.behaviors.top = Y.Anim.behaviors.left;
122 * The default setter to use when setting object properties.
124 * @property DEFAULT_SETTER
127 Y.Anim.DEFAULT_SETTER = function(anim, att, from, to, elapsed, duration, fn, unit) {
128 var node = anim._node,
129 val = fn(elapsed, NUM(from), NUM(to) - NUM(from), duration);
131 if (att in node._node.style || att in Y.DOM.CUSTOM_STYLES) {
133 node.setStyle(att, val + unit);
134 } else if (node._node.attributes[att]) {
135 node.setAttribute(att, val);
142 * The default getter to use when getting object properties.
144 * @property DEFAULT_GETTER
147 Y.Anim.DEFAULT_GETTER = function(anim, att) {
148 var node = anim._node,
151 if (att in node._node.style || att in Y.DOM.CUSTOM_STYLES) {
152 val = node.getComputedStyle(att);
153 } else if (node._node.attributes[att]) {
154 val = node.getAttribute(att);
164 * The object to be animated.
169 setter: function(node) {
179 * The length of the animation. Defaults to "1" (second).
180 * @attribute duration
188 * The method that will provide values to the attribute(s) during the animation.
189 * Defaults to "Easing.easeNone".
194 value: Y.Anim.DEFAULT_EASING,
196 setter: function(val) {
197 if (typeof val === 'string' && Y.Easing) {
198 return Y.Easing[val];
204 * The starting values for the animated properties.
205 * Fields may be strings, numbers, or functions.
206 * If a function is used, the return value becomes the from value.
207 * If no from value is specified, the DEFAULT_GETTER will be used.
210 * supports any unit, provided it matches the "to" (or default)
211 * unit (e.g. "{width: 10em', color: 'rgb(0, 0 0)', borderColor: '#ccc'}".
212 * If using the default ('px' for length-based units), the unit may be omitted (
213 * (e.g. "{width: 100}, borderColor: 'ccc'}", which defaults to pixels
214 * and hex, respectively).
219 * The ending values for the animated properties.
220 * Fields may be strings, numbers, or functions.
223 * supports any unit, provided it matches the "from" (or default)
224 * unit (e.g. "{width: '50%', color: 'red', borderColor: '#ccc'}".
225 * If using the default ('px' for length-based units), the unit may be omitted (
226 * (e.g. "{width: 100}, borderColor: 'ccc'}", which defaults to pixels
227 * and hex, respectively).
232 * Date stamp for the first frame of the animation.
233 * @attribute startTime
244 * Current time the animation has been running.
245 * @attribute elapsedTime
256 * Whether or not the animation is currently running.
264 return !!_running[Y.stamp(this)];
271 * The number of times the animation should run
272 * @attribute iterations
281 * The number of iterations that have occurred.
282 * Resets when an animation ends (reaches iteration count or stop() called).
283 * @attribute iterationCount
294 * How iterations of the animation should behave.
295 * Possible values are "normal" and "alternate".
296 * Normal will repeat the animation, alternate will reverse on every other pass.
298 * @attribute direction
303 value: 'normal' // | alternate (fwd on odd, rev on even per spec)
307 * Whether or not the animation is currently paused.
319 * If true, animation begins from last frame
332 * Runs all animation instances.
336 Y.Anim.run = function() {
337 var instances = Y.Anim._instances;
338 for (var i in instances) {
339 if (instances[i].run) {
346 * Pauses all animation instances.
350 Y.Anim.pause = function() {
351 for (var i in _running) { // stop timer if nothing running
352 if (_running[i].pause) {
361 * Stops all animation instances.
365 Y.Anim.stop = function() {
366 for (var i in _running) { // stop timer if nothing running
367 if (_running[i].stop) {
374 Y.Anim._startTimer = function() {
376 _timer = setInterval(Y.Anim._runFrame, Y.Anim._intervalTime);
380 Y.Anim._stopTimer = function() {
381 clearInterval(_timer);
386 * Called per Interval to handle each animation frame.
391 Y.Anim._runFrame = function() {
393 for (var anim in _running) {
394 if (_running[anim]._runFrame) {
396 _running[anim]._runFrame();
405 Y.Anim.RE_UNITS = /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/;
409 * Starts or resumes an animation.
414 if (this.get(PAUSED)) {
416 } else if (!this.get(RUNNING)) {
423 * Pauses the animation and
424 * freezes it in its current state and time.
425 * Calling run() will continue where it left off.
430 if (this.get(RUNNING)) {
437 * Stops the animation and resets its time.
439 * @param {Boolean} finish If true, the animation will move to the last frame
442 stop: function(finish) {
443 if (this.get(RUNNING) || this.get(PAUSED)) {
452 this._set(START_TIME, new Date() - this.get(ELAPSED_TIME));
453 this._actualFrames = 0;
454 if (!this.get(PAUSED)) {
455 this._initAnimAttr();
457 _running[Y.stamp(this)] = this;
458 Y.Anim._startTimer();
464 this._set(START_TIME, null);
465 this._set(PAUSED, true);
466 delete _running[Y.stamp(this)];
470 * @description fires when an animation is paused.
471 * @param {Event} ev The pause event.
477 _resume: function() {
478 this._set(PAUSED, false);
479 _running[Y.stamp(this)] = this;
480 this._set(START_TIME, new Date() - this.get(ELAPSED_TIME));
481 Y.Anim._startTimer();
485 * @description fires when an animation is resumed (run from pause).
486 * @param {Event} ev The pause event.
492 _end: function(finish) {
493 var duration = this.get('duration') * 1000;
494 if (finish) { // jump to last frame
495 this._runAttrs(duration, duration, this.get(REVERSE));
498 this._set(START_TIME, null);
499 this._set(ELAPSED_TIME, 0);
500 this._set(PAUSED, false);
502 delete _running[Y.stamp(this)];
503 this.fire(END, {elapsed: this.get(ELAPSED_TIME)});
506 _runFrame: function() {
507 var d = this._runtimeAttr.duration,
508 t = new Date() - this.get(START_TIME),
509 reverse = this.get(REVERSE),
514 this._runAttrs(t, d, reverse);
515 this._actualFrames += 1;
516 this._set(ELAPSED_TIME, t);
524 _runAttrs: function(t, d, reverse) {
525 var attr = this._runtimeAttr,
526 customAttr = Y.Anim.behaviors,
527 easing = attr.easing,
546 setter = (i in customAttr && 'set' in customAttr[i]) ?
547 customAttr[i].set : Y.Anim.DEFAULT_SETTER;
550 setter(this, i, attribute.from, attribute.to, t, d, easing, attribute.unit);
552 setter(this, i, attribute.from, attribute.to, lastFrame, d, easing, attribute.unit);
560 _lastFrame: function() {
561 var iter = this.get('iterations'),
562 iterCount = this.get(ITERATION_COUNT);
565 if (iter === 'infinite' || iterCount < iter) {
566 if (this.get('direction') === 'alternate') {
567 this.set(REVERSE, !this.get(REVERSE)); // flip it
571 * @description fires when an animation begins an iteration.
572 * @param {Event} ev The iteration event.
575 this.fire('iteration');
581 this._set(START_TIME, new Date());
582 this._set(ITERATION_COUNT, iterCount);
585 _initAnimAttr: function() {
586 var from = this.get('from') || {},
587 to = this.get('to') || {},
589 duration: this.get('duration') * 1000,
590 easing: this.get('easing')
592 customAttr = Y.Anim.behaviors,
593 node = this.get(NODE), // implicit attr init
596 Y.each(to, function(val, name) {
597 if (typeof val === 'function') {
598 val = val.call(this, node);
602 if (begin === undefined) {
603 begin = (name in customAttr && 'get' in customAttr[name]) ?
604 customAttr[name].get(this, name) : Y.Anim.DEFAULT_GETTER(this, name);
605 } else if (typeof begin === 'function') {
606 begin = begin.call(this, node);
609 var mFrom = Y.Anim.RE_UNITS.exec(begin);
610 var mTo = Y.Anim.RE_UNITS.exec(val);
612 begin = mFrom ? mFrom[1] : begin;
613 end = mTo ? mTo[1] : val;
614 unit = mTo ? mTo[2] : mFrom ? mFrom[2] : ''; // one might be zero TODO: mixed units
616 if (!unit && Y.Anim.RE_DEFAULT_UNIT.test(name)) {
617 unit = Y.Anim.DEFAULT_UNIT;
620 if (!begin || !end) {
621 Y.error('invalid "from" or "to" for "' + name + '"', 'Anim');
633 this._runtimeAttr = attr;
637 // TODO: move to computedStyle? (browsers dont agree on default computed offsets)
638 _getOffset: function(attr) {
639 var node = this._node,
640 val = node.getComputedStyle(attr),
641 get = (attr === 'left') ? 'getX': 'getY',
642 set = (attr === 'left') ? 'setX': 'setY';
644 if (val === 'auto') {
645 var position = node.getStyle('position');
646 if (position === 'absolute' || position === 'fixed') {
657 destructor: function() {
658 delete Y.Anim._instances[Y.stamp(this)];
662 Y.extend(Y.Anim, Y.Base, proto);
665 }, '3.3.0' ,{requires:['base-base', 'node-style']});