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) {
58
attrs = Transition._nodeAttrs[uid],
59
customAttr = Transition.behaviors,
72
attribute = attrs[name];
73
if ((attribute && attribute.transition === anim)) {
74
d = attribute.duration;
75
delay = attribute.delay;
76
elapsed = (time - delay) / 1000;
78
setter = (i in customAttr && 'set' in customAttr[i]) ?
79
customAttr[i].set : Transition.DEFAULT_SETTER;
87
if (!delay || time >= delay) {
88
setter(anim, name, attribute.from, attribute.to, t - delay, d - delay,
89
attribute.easing, attribute.unit);
95
node.fire('transition:propertyEnd', {
102
if (!allDone && anim._count <= 0) {
114
_initAttrs: function() {
116
customAttr = Transition.behaviors,
117
uid = Y.stamp(anim._node),
118
attrs = Transition._nodeAttrs[uid],
129
for (name in attrs) {
130
attribute = attrs[name];
131
if (attrs.hasOwnProperty(name) && (attribute && attribute.transition === anim)) {
132
duration = attribute.duration * 1000;
133
delay = attribute.delay * 1000;
134
easing = attribute.easing;
135
val = attribute.value;
137
// only allow supported properties
138
if (name in anim._node._node.style || name in Y.DOM.CUSTOM_STYLES) {
139
begin = (name in customAttr && 'get' in customAttr[name]) ?
140
customAttr[name].get(anim, name) : Transition.DEFAULT_GETTER(anim, name);
142
mFrom = Transition.RE_UNITS.exec(begin);
143
mTo = Transition.RE_UNITS.exec(val);
145
begin = mFrom ? mFrom[1] : begin;
146
end = mTo ? mTo[1] : val;
147
unit = mTo ? mTo[2] : mFrom ? mFrom[2] : ''; // one might be zero TODO: mixed units
149
if (!unit && Transition.RE_DEFAULT_UNIT.test(name)) {
150
unit = Transition.DEFAULT_UNIT;
153
if (typeof easing === 'string') {
154
if (easing.indexOf('cubic-bezier') > -1) {
155
easing = easing.substring(13, easing.length - 1).split(',');
156
} else if (Transition.easings[easing]) {
157
easing = Transition.easings[easing];
161
attribute.from = Number(begin);
162
attribute.to = Number(end);
163
attribute.unit = unit;
164
attribute.easing = easing;
165
attribute.duration = duration + delay;
166
attribute.delay = delay;
175
destroy: function() {
181
Y.mix(Y.Transition, {
184
* Regex of properties that should use the default unit.
186
* @property RE_DEFAULT_UNIT
189
RE_DEFAULT_UNIT: /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i,
192
* The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
194
* @property DEFAULT_UNIT
200
* Time in milliseconds passed to setInterval for frame processing
202
* @property intervalTime
209
* Bucket for custom getters and setters
211
* @property behaviors
216
get: function(anim, attr) {
217
return Y.DOM._getAttrOffset(anim._node._node, attr);
223
* The default setter to use when setting object properties.
225
* @property DEFAULT_SETTER
228
DEFAULT_SETTER: function(anim, att, from, to, elapsed, duration, fn, unit) {
232
var node = anim._node,
233
val = Transition.cubicBezier(fn, elapsed / duration);
235
val = from + val[0] * (to - from);
237
if (att in node._node.style || att in Y.DOM.CUSTOM_STYLES) {
239
node.setStyle(att, val + unit);
240
} else if (node._node.attributes[att]) {
241
node.setAttribute(att, val);
248
* The default getter to use when getting object properties.
250
* @property DEFAULT_GETTER
253
DEFAULT_GETTER: function(anim, att) {
254
var node = anim._node,
257
if (att in node._node.style || att in Y.DOM.CUSTOM_STYLES) {
258
val = node.getComputedStyle(att);
259
} else if (node._node.attributes[att]) {
260
val = node.getAttribute(att);
268
_startTimer: function() {
269
if (!Transition._timer) {
270
Transition._timer = setInterval(Transition._runFrame, Transition.intervalTime);
274
_stopTimer: function() {
275
clearInterval(Transition._timer);
276
Transition._timer = null;
280
* Called per Interval to handle each animation frame.
285
_runFrame: function() {
288
for (anim in Transition._running) {
289
if (Transition._running[anim]._runFrame) {
291
Transition._running[anim]._runFrame();
296
Transition._stopTimer();
300
cubicBezier: function(p, t) {
310
A = x3 - 3 * x2 + 3 * x1 - x0,
311
B = 3 * x2 - 6 * x1 + 3 * x0,
314
E = y3 - 3 * y2 + 3 * y1 - y0,
315
F = 3 * y2 - 6 * y1 + 3 * y0,
319
x = (((A*t) + B)*t + C)*t + D,
320
y = (((E*t) + F)*t + G)*t + H;
326
ease: [0.25, 0, 1, 0.25],
327
linear: [0, 0, 1, 1],
328
'ease-in': [0.42, 0, 1, 1],
329
'ease-out': [0, 0, 0.58, 1],
330
'ease-in-out': [0.42, 0, 0.58, 1]
336
RE_UNITS: /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/
339
Transition.behaviors.top = Transition.behaviors.bottom = Transition.behaviors.right = Transition.behaviors.left;
341
Y.Transition = Transition;
344
}, '3.2.0' ,{requires:['transition-native', 'node-style']});