API Docs for: 3.13.0
Show:

File: transition/js/transition-timer.js

  1. /**
  2. * Provides the base Transition class, for animating numeric properties.
  3. *
  4. * @module transition
  5. * @submodule transition-timer
  6. */
  7.  
  8.  
  9. var Transition = Y.Transition;
  10.  
  11. Y.mix(Transition.prototype, {
  12. _start: function() {
  13. if (Transition.useNative) {
  14. this._runNative();
  15. } else {
  16. this._runTimer();
  17. }
  18. },
  19.  
  20. _runTimer: function() {
  21. var anim = this;
  22. anim._initAttrs();
  23.  
  24. Transition._running[Y.stamp(anim)] = anim;
  25. anim._startTime = new Date();
  26. Transition._startTimer();
  27. },
  28.  
  29. _endTimer: function() {
  30. var anim = this;
  31. delete Transition._running[Y.stamp(anim)];
  32. anim._startTime = null;
  33. },
  34.  
  35. _runFrame: function() {
  36. var t = new Date() - this._startTime;
  37. this._runAttrs(t);
  38. },
  39.  
  40. _runAttrs: function(time) {
  41. var anim = this,
  42. node = anim._node,
  43. config = anim._config,
  44. uid = Y.stamp(node),
  45. attrs = Transition._nodeAttrs[uid],
  46. customAttr = Transition.behaviors,
  47. done = false,
  48. allDone = false,
  49. data,
  50. name,
  51. attribute,
  52. setter,
  53. elapsed,
  54. delay,
  55. d,
  56. t,
  57. i;
  58.  
  59. for (name in attrs) {
  60. if ((attribute = attrs[name]) && attribute.transition === anim) {
  61. d = attribute.duration;
  62. delay = attribute.delay;
  63. elapsed = (time - delay) / 1000;
  64. t = time;
  65. data = {
  66. type: 'propertyEnd',
  67. propertyName: name,
  68. config: config,
  69. elapsedTime: elapsed
  70. };
  71.  
  72. setter = (i in customAttr && 'set' in customAttr[i]) ?
  73. customAttr[i].set : Transition.DEFAULT_SETTER;
  74.  
  75. done = (t >= d);
  76.  
  77. if (t > d) {
  78. t = d;
  79. }
  80.  
  81. if (!delay || time >= delay) {
  82. setter(anim, name, attribute.from, attribute.to, t - delay, d - delay,
  83. attribute.easing, attribute.unit);
  84.  
  85. if (done) {
  86. delete attrs[name];
  87. anim._count--;
  88.  
  89. if (config[name] && config[name].on && config[name].on.end) {
  90. config[name].on.end.call(Y.one(node), data);
  91. }
  92.  
  93. //node.fire('transition:propertyEnd', data);
  94.  
  95. if (!allDone && anim._count <= 0) {
  96. allDone = true;
  97. anim._end(elapsed);
  98. anim._endTimer();
  99. }
  100. }
  101. }
  102.  
  103. }
  104. }
  105. },
  106.  
  107. _initAttrs: function() {
  108. var anim = this,
  109. customAttr = Transition.behaviors,
  110. uid = Y.stamp(anim._node),
  111. attrs = Transition._nodeAttrs[uid],
  112. attribute,
  113. duration,
  114. delay,
  115. easing,
  116. val,
  117. name,
  118. mTo,
  119. mFrom,
  120. unit, begin, end;
  121.  
  122. for (name in attrs) {
  123. if ((attribute = attrs[name]) && attribute.transition === anim) {
  124. duration = attribute.duration * 1000;
  125. delay = attribute.delay * 1000;
  126. easing = attribute.easing;
  127. val = attribute.value;
  128.  
  129. // only allow supported properties
  130. if (name in anim._node.style || name in Y.DOM.CUSTOM_STYLES) {
  131. begin = (name in customAttr && 'get' in customAttr[name]) ?
  132. customAttr[name].get(anim, name) : Transition.DEFAULT_GETTER(anim, name);
  133.  
  134. mFrom = Transition.RE_UNITS.exec(begin);
  135. mTo = Transition.RE_UNITS.exec(val);
  136.  
  137. begin = mFrom ? mFrom[1] : begin;
  138. end = mTo ? mTo[1] : val;
  139. unit = mTo ? mTo[2] : mFrom ? mFrom[2] : ''; // one might be zero TODO: mixed units
  140.  
  141. if (!unit && Transition.RE_DEFAULT_UNIT.test(name)) {
  142. unit = Transition.DEFAULT_UNIT;
  143. }
  144.  
  145. if (typeof easing === 'string') {
  146. if (easing.indexOf('cubic-bezier') > -1) {
  147. easing = easing.substring(13, easing.length - 1).split(',');
  148. } else if (Transition.easings[easing]) {
  149. easing = Transition.easings[easing];
  150. }
  151. }
  152.  
  153. attribute.from = Number(begin);
  154. attribute.to = Number(end);
  155. attribute.unit = unit;
  156. attribute.easing = easing;
  157. attribute.duration = duration + delay;
  158. attribute.delay = delay;
  159. } else {
  160. delete attrs[name];
  161. anim._count--;
  162. }
  163. }
  164. }
  165. },
  166.  
  167. destroy: function() {
  168. this.detachAll();
  169. this._node = null;
  170. }
  171. }, true);
  172.  
  173. Y.mix(Y.Transition, {
  174. _runtimeAttrs: {},
  175. /*
  176. * Regex of properties that should use the default unit.
  177. *
  178. * @property RE_DEFAULT_UNIT
  179. * @static
  180. */
  181. RE_DEFAULT_UNIT: /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i,
  182.  
  183. /*
  184. * The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
  185. *
  186. * @property DEFAULT_UNIT
  187. * @static
  188. */
  189. DEFAULT_UNIT: 'px',
  190.  
  191. /*
  192. * Time in milliseconds passed to setInterval for frame processing
  193. *
  194. * @property intervalTime
  195. * @default 20
  196. * @static
  197. */
  198. intervalTime: 20,
  199.  
  200. /*
  201. * Bucket for custom getters and setters
  202. *
  203. * @property behaviors
  204. * @static
  205. */
  206. behaviors: {
  207. left: {
  208. get: function(anim, attr) {
  209. return Y.DOM._getAttrOffset(anim._node, attr);
  210. }
  211. }
  212. },
  213.  
  214. /*
  215. * The default setter to use when setting object properties.
  216. *
  217. * @property DEFAULT_SETTER
  218. * @static
  219. */
  220. DEFAULT_SETTER: function(anim, att, from, to, elapsed, duration, fn, unit) {
  221. from = Number(from);
  222. to = Number(to);
  223.  
  224. var node = anim._node,
  225. val = Transition.cubicBezier(fn, elapsed / duration);
  226.  
  227. val = from + val[0] * (to - from);
  228.  
  229. if (node) {
  230. if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
  231. unit = unit || '';
  232. Y.DOM.setStyle(node, att, val + unit);
  233. }
  234. } else {
  235. anim._end();
  236. }
  237. },
  238.  
  239. /*
  240. * The default getter to use when getting object properties.
  241. *
  242. * @property DEFAULT_GETTER
  243. * @static
  244. */
  245. DEFAULT_GETTER: function(anim, att) {
  246. var node = anim._node,
  247. val = '';
  248.  
  249. if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
  250. val = Y.DOM.getComputedStyle(node, att);
  251. }
  252.  
  253. return val;
  254. },
  255.  
  256. _startTimer: function() {
  257. if (!Transition._timer) {
  258. Transition._timer = setInterval(Transition._runFrame, Transition.intervalTime);
  259. }
  260. },
  261.  
  262. _stopTimer: function() {
  263. clearInterval(Transition._timer);
  264. Transition._timer = null;
  265. },
  266.  
  267. /*
  268. * Called per Interval to handle each animation frame.
  269. * @method _runFrame
  270. * @private
  271. * @static
  272. */
  273. _runFrame: function() {
  274. var done = true,
  275. anim;
  276. for (anim in Transition._running) {
  277. if (Transition._running[anim]._runFrame) {
  278. done = false;
  279. Transition._running[anim]._runFrame();
  280. }
  281. }
  282.  
  283. if (done) {
  284. Transition._stopTimer();
  285. }
  286. },
  287.  
  288. cubicBezier: function(p, t) {
  289. var x0 = 0,
  290. y0 = 0,
  291. x1 = p[0],
  292. y1 = p[1],
  293. x2 = p[2],
  294. y2 = p[3],
  295. x3 = 1,
  296. y3 = 0,
  297.  
  298. A = x3 - 3 * x2 + 3 * x1 - x0,
  299. B = 3 * x2 - 6 * x1 + 3 * x0,
  300. C = 3 * x1 - 3 * x0,
  301. D = x0,
  302. E = y3 - 3 * y2 + 3 * y1 - y0,
  303. F = 3 * y2 - 6 * y1 + 3 * y0,
  304. G = 3 * y1 - 3 * y0,
  305. H = y0,
  306.  
  307. x = (((A*t) + B)*t + C)*t + D,
  308. y = (((E*t) + F)*t + G)*t + H;
  309.  
  310. return [x, y];
  311. },
  312.  
  313. easings: {
  314. ease: [0.25, 0, 1, 0.25],
  315. linear: [0, 0, 1, 1],
  316. 'ease-in': [0.42, 0, 1, 1],
  317. 'ease-out': [0, 0, 0.58, 1],
  318. 'ease-in-out': [0.42, 0, 0.58, 1]
  319. },
  320.  
  321. _running: {},
  322. _timer: null,
  323.  
  324. RE_UNITS: /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/
  325. }, true);
  326.  
  327. Transition.behaviors.top = Transition.behaviors.bottom = Transition.behaviors.right = Transition.behaviors.left;
  328.  
  329. Y.Transition = Transition;
  330.