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('transition-timer', function(Y) {
11 * The Transition Utility provides an API for creating advanced transitions.
16 * Provides the base Transition class, for animating numeric properties.
19 * @submodule transition-timer
23 var Transition = Y.Transition;
25 Y.mix(Transition.prototype, {
27 if (Transition.useNative) {
34 _runTimer: function() {
38 Transition._running[Y.stamp(anim)] = anim;
39 anim._startTime = new Date();
40 Transition._startTimer();
43 _endTimer: function() {
45 delete Transition._running[Y.stamp(anim)];
46 anim._startTime = null;
49 _runFrame: function() {
50 var t = new Date() - this._startTime;
54 _runAttrs: function(time) {
57 config = anim._config,
59 attrs = Transition._nodeAttrs[uid],
60 customAttr = Transition.behaviors,
74 attribute = attrs[name];
75 if ((attribute && attribute.transition === anim)) {
76 d = attribute.duration;
77 delay = attribute.delay;
78 elapsed = (time - delay) / 1000;
87 setter = (i in customAttr && 'set' in customAttr[i]) ?
88 customAttr[i].set : Transition.DEFAULT_SETTER;
96 if (!delay || time >= delay) {
97 setter(anim, name, attribute.from, attribute.to, t - delay, d - delay,
98 attribute.easing, attribute.unit);
104 if (config[name] && config[name].on && config[name].on.end) {
105 config[name].on.end.call(Y.one(node), data);
108 //node.fire('transition:propertyEnd', data);
110 if (!allDone && anim._count <= 0) {
122 _initAttrs: function() {
124 customAttr = Transition.behaviors,
125 uid = Y.stamp(anim._node),
126 attrs = Transition._nodeAttrs[uid],
137 for (name in attrs) {
138 attribute = attrs[name];
139 if (attrs.hasOwnProperty(name) && (attribute && attribute.transition === anim)) {
140 duration = attribute.duration * 1000;
141 delay = attribute.delay * 1000;
142 easing = attribute.easing;
143 val = attribute.value;
145 // only allow supported properties
146 if (name in anim._node.style || name in Y.DOM.CUSTOM_STYLES) {
147 begin = (name in customAttr && 'get' in customAttr[name]) ?
148 customAttr[name].get(anim, name) : Transition.DEFAULT_GETTER(anim, name);
150 mFrom = Transition.RE_UNITS.exec(begin);
151 mTo = Transition.RE_UNITS.exec(val);
153 begin = mFrom ? mFrom[1] : begin;
154 end = mTo ? mTo[1] : val;
155 unit = mTo ? mTo[2] : mFrom ? mFrom[2] : ''; // one might be zero TODO: mixed units
157 if (!unit && Transition.RE_DEFAULT_UNIT.test(name)) {
158 unit = Transition.DEFAULT_UNIT;
161 if (typeof easing === 'string') {
162 if (easing.indexOf('cubic-bezier') > -1) {
163 easing = easing.substring(13, easing.length - 1).split(',');
164 } else if (Transition.easings[easing]) {
165 easing = Transition.easings[easing];
169 attribute.from = Number(begin);
170 attribute.to = Number(end);
171 attribute.unit = unit;
172 attribute.easing = easing;
173 attribute.duration = duration + delay;
174 attribute.delay = delay;
183 destroy: function() {
189 Y.mix(Y.Transition, {
192 * Regex of properties that should use the default unit.
194 * @property RE_DEFAULT_UNIT
197 RE_DEFAULT_UNIT: /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i,
200 * The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
202 * @property DEFAULT_UNIT
208 * Time in milliseconds passed to setInterval for frame processing
210 * @property intervalTime
217 * Bucket for custom getters and setters
219 * @property behaviors
224 get: function(anim, attr) {
225 return Y.DOM._getAttrOffset(anim._node, attr);
231 * The default setter to use when setting object properties.
233 * @property DEFAULT_SETTER
236 DEFAULT_SETTER: function(anim, att, from, to, elapsed, duration, fn, unit) {
240 var node = anim._node,
241 val = Transition.cubicBezier(fn, elapsed / duration);
243 val = from + val[0] * (to - from);
246 if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
248 Y.DOM.setStyle(node, att, val + unit);
256 * The default getter to use when getting object properties.
258 * @property DEFAULT_GETTER
261 DEFAULT_GETTER: function(anim, att) {
262 var node = anim._node,
265 if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
266 val = Y.DOM.getComputedStyle(node, att);
272 _startTimer: function() {
273 if (!Transition._timer) {
274 Transition._timer = setInterval(Transition._runFrame, Transition.intervalTime);
278 _stopTimer: function() {
279 clearInterval(Transition._timer);
280 Transition._timer = null;
284 * Called per Interval to handle each animation frame.
289 _runFrame: function() {
292 for (anim in Transition._running) {
293 if (Transition._running[anim]._runFrame) {
295 Transition._running[anim]._runFrame();
300 Transition._stopTimer();
304 cubicBezier: function(p, t) {
314 A = x3 - 3 * x2 + 3 * x1 - x0,
315 B = 3 * x2 - 6 * x1 + 3 * x0,
318 E = y3 - 3 * y2 + 3 * y1 - y0,
319 F = 3 * y2 - 6 * y1 + 3 * y0,
323 x = (((A*t) + B)*t + C)*t + D,
324 y = (((E*t) + F)*t + G)*t + H;
330 ease: [0.25, 0, 1, 0.25],
331 linear: [0, 0, 1, 1],
332 'ease-in': [0.42, 0, 1, 1],
333 'ease-out': [0, 0, 0.58, 1],
334 'ease-in-out': [0.42, 0, 0.58, 1]
340 RE_UNITS: /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/
343 Transition.behaviors.top = Transition.behaviors.bottom = Transition.behaviors.right = Transition.behaviors.left;
345 Y.Transition = Transition;
348 }, '3.3.0' ,{requires:['transition-native', 'node-style']});