3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('transition-timer', function(Y) {
10 * The Transition Utility provides an API for creating advanced transitions.
15 * Provides the base Transition class, for animating numeric properties.
18 * @submodule transition-timer
22 var Transition = Y.Transition;
24 Y.mix(Transition.prototype, {
26 if (Transition.useNative) {
33 _runTimer: function() {
37 Transition._running[Y.stamp(anim)] = anim;
38 anim._startTime = new Date();
39 Transition._startTimer();
42 _endTimer: function() {
44 delete Transition._running[Y.stamp(anim)];
45 anim._startTime = null;
48 _runFrame: function() {
49 var t = new Date() - this._startTime;
53 _runAttrs: function(time) {
56 config = anim._config,
58 attrs = Transition._nodeAttrs[uid],
59 customAttr = Transition.behaviors,
73 if ((attribute = attrs[name]) && attribute.transition === anim) {
74 d = attribute.duration;
75 delay = attribute.delay;
76 elapsed = (time - delay) / 1000;
85 setter = (i in customAttr && 'set' in customAttr[i]) ?
86 customAttr[i].set : Transition.DEFAULT_SETTER;
94 if (!delay || time >= delay) {
95 setter(anim, name, attribute.from, attribute.to, t - delay, d - delay,
96 attribute.easing, attribute.unit);
102 if (config[name] && config[name].on && config[name].on.end) {
103 config[name].on.end.call(Y.one(node), data);
106 //node.fire('transition:propertyEnd', data);
108 if (!allDone && anim._count <= 0) {
120 _initAttrs: function() {
122 customAttr = Transition.behaviors,
123 uid = Y.stamp(anim._node),
124 attrs = Transition._nodeAttrs[uid],
135 for (name in attrs) {
136 if ((attribute = attrs[name]) && attribute.transition === anim) {
137 duration = attribute.duration * 1000;
138 delay = attribute.delay * 1000;
139 easing = attribute.easing;
140 val = attribute.value;
142 // only allow supported properties
143 if (name in anim._node.style || name in Y.DOM.CUSTOM_STYLES) {
144 begin = (name in customAttr && 'get' in customAttr[name]) ?
145 customAttr[name].get(anim, name) : Transition.DEFAULT_GETTER(anim, name);
147 mFrom = Transition.RE_UNITS.exec(begin);
148 mTo = Transition.RE_UNITS.exec(val);
150 begin = mFrom ? mFrom[1] : begin;
151 end = mTo ? mTo[1] : val;
152 unit = mTo ? mTo[2] : mFrom ? mFrom[2] : ''; // one might be zero TODO: mixed units
154 if (!unit && Transition.RE_DEFAULT_UNIT.test(name)) {
155 unit = Transition.DEFAULT_UNIT;
158 if (typeof easing === 'string') {
159 if (easing.indexOf('cubic-bezier') > -1) {
160 easing = easing.substring(13, easing.length - 1).split(',');
161 } else if (Transition.easings[easing]) {
162 easing = Transition.easings[easing];
166 attribute.from = Number(begin);
167 attribute.to = Number(end);
168 attribute.unit = unit;
169 attribute.easing = easing;
170 attribute.duration = duration + delay;
171 attribute.delay = delay;
180 destroy: function() {
186 Y.mix(Y.Transition, {
189 * Regex of properties that should use the default unit.
191 * @property RE_DEFAULT_UNIT
194 RE_DEFAULT_UNIT: /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i,
197 * The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
199 * @property DEFAULT_UNIT
205 * Time in milliseconds passed to setInterval for frame processing
207 * @property intervalTime
214 * Bucket for custom getters and setters
216 * @property behaviors
221 get: function(anim, attr) {
222 return Y.DOM._getAttrOffset(anim._node, attr);
228 * The default setter to use when setting object properties.
230 * @property DEFAULT_SETTER
233 DEFAULT_SETTER: function(anim, att, from, to, elapsed, duration, fn, unit) {
237 var node = anim._node,
238 val = Transition.cubicBezier(fn, elapsed / duration);
240 val = from + val[0] * (to - from);
243 if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
245 Y.DOM.setStyle(node, att, val + unit);
253 * The default getter to use when getting object properties.
255 * @property DEFAULT_GETTER
258 DEFAULT_GETTER: function(anim, att) {
259 var node = anim._node,
262 if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
263 val = Y.DOM.getComputedStyle(node, att);
269 _startTimer: function() {
270 if (!Transition._timer) {
271 Transition._timer = setInterval(Transition._runFrame, Transition.intervalTime);
275 _stopTimer: function() {
276 clearInterval(Transition._timer);
277 Transition._timer = null;
281 * Called per Interval to handle each animation frame.
286 _runFrame: function() {
289 for (anim in Transition._running) {
290 if (Transition._running[anim]._runFrame) {
292 Transition._running[anim]._runFrame();
297 Transition._stopTimer();
301 cubicBezier: function(p, t) {
311 A = x3 - 3 * x2 + 3 * x1 - x0,
312 B = 3 * x2 - 6 * x1 + 3 * x0,
315 E = y3 - 3 * y2 + 3 * y1 - y0,
316 F = 3 * y2 - 6 * y1 + 3 * y0,
320 x = (((A*t) + B)*t + C)*t + D,
321 y = (((E*t) + F)*t + G)*t + H;
327 ease: [0.25, 0, 1, 0.25],
328 linear: [0, 0, 1, 1],
329 'ease-in': [0.42, 0, 1, 1],
330 'ease-out': [0, 0, 0.58, 1],
331 'ease-in-out': [0.42, 0, 0.58, 1]
337 RE_UNITS: /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/
340 Transition.behaviors.top = Transition.behaviors.bottom = Transition.behaviors.right = Transition.behaviors.left;
342 Y.Transition = Transition;
345 }, '3.5.0' ,{requires:['transition']});