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-native', function(Y) {
11
* Provides the transition method for Node.
12
* Transition has no API of its own, but adds the transition method to Node.
18
var TRANSITION = '-webkit-transition',
19
TRANSITION_PROPERTY_CAMEL = 'WebkitTransition',
20
TRANSITION_PROPERTY = '-webkit-transition-property',
21
TRANSITION_DURATION = '-webkit-transition-duration',
22
TRANSITION_TIMING_FUNCTION = '-webkit-transition-timing-function',
23
TRANSITION_DELAY = '-webkit-transition-delay',
24
TRANSITION_END = 'webkitTransitionEnd',
25
TRANSFORM_CAMEL = 'WebkitTransform',
30
* A class for constructing transition instances.
31
* Adds the "transition" method to Node.
36
Transition = function() {
37
this.init.apply(this, arguments);
40
Transition._toCamel = function(property) {
41
property = property.replace(/-([a-z])/gi, function(m0, m1) {
42
return m1.toUpperCase();
48
Transition._toHyphen = function(property) {
49
property = property.replace(/([A-Z]?)([a-z]+)([A-Z]?)/g, function(m0, m1, m2, m3) {
52
str += '-' + m1.toLowerCase();
57
str += '-' + m3.toLowerCase();
67
Transition._reKeywords = /^(?:node|duration|iterations|easing|delay)$/;
69
Transition.useNative = false;
71
if (TRANSITION in Y.config.doc.documentElement.style) {
72
Transition.useNative = true;
73
Transition.supported = true; // TODO: remove
76
Y.Node.DOM_EVENTS[TRANSITION_END] = 1;
78
Transition.NAME = 'transition';
80
Transition.DEFAULT_EASING = 'ease';
81
Transition.DEFAULT_DURATION = 0.5;
82
Transition.DEFAULT_DELAY = 0;
84
Transition._nodeAttrs = {};
86
Transition.prototype = {
87
constructor: Transition,
88
init: function(node, config) {
92
anim._config = config;
93
node._transition = anim; // cache for reuse
95
anim._duration = ('duration' in config) ?
96
config.duration: anim.constructor.DEFAULT_DURATION;
98
anim._delay = ('delay' in config) ?
99
config.delay: anim.constructor.DEFAULT_DELAY;
101
anim._easing = config.easing || anim.constructor.DEFAULT_EASING;
102
anim._count = 0; // track number of animated properties
103
anim._running = false;
105
anim.initAttrs(config);
112
addProperty: function(prop, config) {
116
attrs = Transition._nodeAttrs[uid],
121
attrs = Transition._nodeAttrs[uid] = {};
126
// might just be a value
127
if (config && config.value !== undefined) {
129
} else if (config !== undefined) {
134
if (typeof val === 'function') {
135
val = val.call(node, node);
138
// take control if another transition owns this property
139
if (attr && attr.transition && attr.transition !== anim) {
140
attr.transition._count--; // remapping attr to this transition
143
anim._count++; // properties per transition
147
duration: ((typeof config.duration !== 'undefined') ? config.duration :
148
anim._duration) || 0.0001, // make 0 async and fire events
150
delay: (typeof config.delay !== 'undefined') ? config.delay :
153
easing: config.easing || anim._easing,
159
removeProperty: function(prop) {
161
attrs = Transition._nodeAttrs[Y.stamp(anim._node)];
163
if (attrs && attrs[prop]) {
170
initAttrs: function(config) {
173
if (config.transform && !config[TRANSFORM_CAMEL]) {
174
config[TRANSFORM_CAMEL] = config.transform;
175
delete config.transform; // TODO: copy
178
for (attr in config) {
179
if (config.hasOwnProperty(attr) && !Transition._reKeywords.test(attr)) {
180
this.addProperty(attr, config[attr]);
187
* Starts or an animation.
192
run: function(callback) {
195
if (!anim._running) {
196
anim._running = true;
198
anim._node.fire('transition:start', {
199
type: 'transition:start',
204
anim._callback = callback;
214
_prepDur: function(dur) {
215
dur = parseFloat(dur);
220
_runNative: function(time) {
224
domNode = node._node,
225
style = domNode.style,
226
computed = getComputedStyle(domNode),
227
attrs = Transition._nodeAttrs[uid],
229
cssTransition = computed[TRANSITION_PROPERTY],
231
transitionText = TRANSITION_PROPERTY + ': ',
232
duration = TRANSITION_DURATION + ': ',
233
easing = TRANSITION_TIMING_FUNCTION + ': ',
234
delay = TRANSITION_DELAY + ': ',
239
// preserve existing transitions
240
if (cssTransition !== 'all') {
241
transitionText += cssTransition + ',';
242
duration += computed[TRANSITION_DURATION] + ',';
243
easing += computed[TRANSITION_TIMING_FUNCTION] + ',';
244
delay += computed[TRANSITION_DELAY] + ',';
248
// run transitions mapped to this instance
249
for (name in attrs) {
250
hyphy = Transition._toHyphen(name);
252
if (attrs.hasOwnProperty(name) && attr.transition === anim) {
253
if (name in domNode.style) { // only native styles allowed
254
duration += anim._prepDur(attr.duration) + ',';
255
delay += anim._prepDur(attr.delay) + ',';
256
easing += (attr.easing) + ',';
258
transitionText += hyphy + ',';
259
cssText += hyphy + ': ' + attr.value + '; ';
261
this.removeProperty(name);
266
transitionText = transitionText.replace(/,$/, ';');
267
duration = duration.replace(/,$/, ';');
268
easing = easing.replace(/,$/, ';');
269
delay = delay.replace(/,$/, ';');
271
// only one native end event per node
272
if (!node._hasTransitionEnd) {
273
anim._detach = node.on(TRANSITION_END, anim._onNativeEnd);
274
node._hasTransitionEnd = true;
278
style.cssText += transitionText + duration + easing + delay + cssText;
282
_end: function(elapsed) {
285
callback = anim._callback,
287
type: 'transition:end',
288
config: anim._config,
292
anim._running = false;
294
anim._callback = null;
295
setTimeout(function() { // IE: allow previous update to finish
296
callback.call(node, data);
300
node.fire('transition:end', data);
303
_endNative: function(name) {
304
var node = this._node,
305
value = node.getComputedStyle(TRANSITION_PROPERTY);
307
if (typeof value === 'string') {
308
value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
309
value = value.replace(/^,|,$/, '');
310
node.setStyle(TRANSITION_PROPERTY_CAMEL, value);
314
_onNativeEnd: function(e) {
318
name = Transition._toCamel(event.propertyName),
319
elapsed = event.elapsedTime,
320
attrs = Transition._nodeAttrs[uid],
322
anim = (attr) ? attr.transition : null;
325
anim.removeProperty(name);
326
anim._endNative(name);
328
node.fire('transition:propertyEnd', {
334
if (anim._count <= 0) { // after propertEnd fires
341
destroy: function() {
344
anim._detach.detach();
350
Y.Transition = Transition;
351
Y.TransitionNative = Transition; // TODO: remove
354
* Animate one or more css properties to a given value. Requires the "transition" module.
355
* <pre>example usage:
356
* Y.one('#demo').transition({
357
* duration: 1, // in seconds, default is 0.5
358
* easing: 'ease-out', // default is 'ease'
359
* delay: '1', // delay start for 1 second, default is 0
364
* opacity: { // per property
374
* @param {Object} config An object containing one or more style properties, a duration and an easing.
375
* @param {Function} callback A function to run after the transition has completed.
378
Y.Node.prototype.transition = function(config, callback) {
379
var anim = this._transition;
381
if (anim && !anim._running) {
382
anim.init(this, config);
384
anim = new Transition(this, config);
392
* Animate one or more css properties to a given value. Requires the "transition" module.
393
* <pre>example usage:
394
* Y.all('.demo').transition({
395
* duration: 1, // in seconds, default is 0.5
396
* easing: 'ease-out', // default is 'ease'
397
* delay: '1', // delay start for 1 second, default is 0
402
* opacity: { // per property
412
* @param {Object} config An object containing one or more style properties, a duration and an easing.
413
* @param {Function} callback A function to run after the transition has completed. The callback fires
414
* once per item in the NodeList.
417
Y.NodeList.prototype.transition = function(config, callback) {
418
this.each(function(node) {
419
node.transition(config, callback);
426
}, '3.2.0' ,{requires:['node-base']});
427
YUI.add('transition-timer', function(Y) {
430
* The Transition Utility provides an API for creating advanced transitions.
435
* Provides the base Transition class, for animating numeric properties.
438
* @submodule transition-timer
442
var Transition = Y.Transition;
444
Y.mix(Transition.prototype, {
446
if (Transition.useNative) {
453
_runTimer: function() {
457
Transition._running[Y.stamp(anim)] = anim;
458
anim._startTime = new Date();
459
Transition._startTimer();
462
_endTimer: function() {
464
delete Transition._running[Y.stamp(anim)];
465
anim._startTime = null;
468
_runFrame: function() {
469
var t = new Date() - this._startTime;
473
_runAttrs: function(time) {
477
attrs = Transition._nodeAttrs[uid],
478
customAttr = Transition.behaviors,
490
for (name in attrs) {
491
attribute = attrs[name];
492
if ((attribute && attribute.transition === anim)) {
493
d = attribute.duration;
494
delay = attribute.delay;
495
elapsed = (time - delay) / 1000;
497
setter = (i in customAttr && 'set' in customAttr[i]) ?
498
customAttr[i].set : Transition.DEFAULT_SETTER;
506
if (!delay || time >= delay) {
507
setter(anim, name, attribute.from, attribute.to, t - delay, d - delay,
508
attribute.easing, attribute.unit);
514
node.fire('transition:propertyEnd', {
517
config: anim._config,
521
if (!allDone && anim._count <= 0) {
533
_initAttrs: function() {
535
customAttr = Transition.behaviors,
536
uid = Y.stamp(anim._node),
537
attrs = Transition._nodeAttrs[uid],
548
for (name in attrs) {
549
attribute = attrs[name];
550
if (attrs.hasOwnProperty(name) && (attribute && attribute.transition === anim)) {
551
duration = attribute.duration * 1000;
552
delay = attribute.delay * 1000;
553
easing = attribute.easing;
554
val = attribute.value;
556
// only allow supported properties
557
if (name in anim._node._node.style || name in Y.DOM.CUSTOM_STYLES) {
558
begin = (name in customAttr && 'get' in customAttr[name]) ?
559
customAttr[name].get(anim, name) : Transition.DEFAULT_GETTER(anim, name);
561
mFrom = Transition.RE_UNITS.exec(begin);
562
mTo = Transition.RE_UNITS.exec(val);
564
begin = mFrom ? mFrom[1] : begin;
565
end = mTo ? mTo[1] : val;
566
unit = mTo ? mTo[2] : mFrom ? mFrom[2] : ''; // one might be zero TODO: mixed units
568
if (!unit && Transition.RE_DEFAULT_UNIT.test(name)) {
569
unit = Transition.DEFAULT_UNIT;
572
if (typeof easing === 'string') {
573
if (easing.indexOf('cubic-bezier') > -1) {
574
easing = easing.substring(13, easing.length - 1).split(',');
575
} else if (Transition.easings[easing]) {
576
easing = Transition.easings[easing];
580
attribute.from = Number(begin);
581
attribute.to = Number(end);
582
attribute.unit = unit;
583
attribute.easing = easing;
584
attribute.duration = duration + delay;
585
attribute.delay = delay;
594
destroy: function() {
600
Y.mix(Y.Transition, {
603
* Regex of properties that should use the default unit.
605
* @property RE_DEFAULT_UNIT
608
RE_DEFAULT_UNIT: /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i,
611
* The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
613
* @property DEFAULT_UNIT
619
* Time in milliseconds passed to setInterval for frame processing
621
* @property intervalTime
628
* Bucket for custom getters and setters
630
* @property behaviors
635
get: function(anim, attr) {
636
return Y.DOM._getAttrOffset(anim._node._node, attr);
642
* The default setter to use when setting object properties.
644
* @property DEFAULT_SETTER
647
DEFAULT_SETTER: function(anim, att, from, to, elapsed, duration, fn, unit) {
651
var node = anim._node,
652
val = Transition.cubicBezier(fn, elapsed / duration);
654
val = from + val[0] * (to - from);
656
if (att in node._node.style || att in Y.DOM.CUSTOM_STYLES) {
658
node.setStyle(att, val + unit);
659
} else if (node._node.attributes[att]) {
660
node.setAttribute(att, val);
667
* The default getter to use when getting object properties.
669
* @property DEFAULT_GETTER
672
DEFAULT_GETTER: function(anim, att) {
673
var node = anim._node,
676
if (att in node._node.style || att in Y.DOM.CUSTOM_STYLES) {
677
val = node.getComputedStyle(att);
678
} else if (node._node.attributes[att]) {
679
val = node.getAttribute(att);
687
_startTimer: function() {
688
if (!Transition._timer) {
689
Transition._timer = setInterval(Transition._runFrame, Transition.intervalTime);
693
_stopTimer: function() {
694
clearInterval(Transition._timer);
695
Transition._timer = null;
699
* Called per Interval to handle each animation frame.
704
_runFrame: function() {
707
for (anim in Transition._running) {
708
if (Transition._running[anim]._runFrame) {
710
Transition._running[anim]._runFrame();
715
Transition._stopTimer();
719
cubicBezier: function(p, t) {
729
A = x3 - 3 * x2 + 3 * x1 - x0,
730
B = 3 * x2 - 6 * x1 + 3 * x0,
733
E = y3 - 3 * y2 + 3 * y1 - y0,
734
F = 3 * y2 - 6 * y1 + 3 * y0,
738
x = (((A*t) + B)*t + C)*t + D,
739
y = (((E*t) + F)*t + G)*t + H;
745
ease: [0.25, 0, 1, 0.25],
746
linear: [0, 0, 1, 1],
747
'ease-in': [0.42, 0, 1, 1],
748
'ease-out': [0, 0, 0.58, 1],
749
'ease-in-out': [0.42, 0, 0.58, 1]
755
RE_UNITS: /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/
758
Transition.behaviors.top = Transition.behaviors.bottom = Transition.behaviors.right = Transition.behaviors.left;
760
Y.Transition = Transition;
763
}, '3.2.0' ,{requires:['transition-native', 'node-style']});
766
YUI.add('transition', function(Y){}, '3.2.0' ,{use:['transition-native', 'transition-timer']});