tween.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * $Id: TinyMCE_DOMUtils.class.js 91 2006-10-02 14:53:22Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
  6. */
  7. mox.require([
  8. 'mox.DOM',
  9. 'mox.util.Dispatcher'
  10. ], function() {
  11. // Shorten class names
  12. var DOM = mox.DOM;
  13. var Dispatcher = mox.util.Dispatcher;
  14. /**#@+
  15. * @member mox.dom.Tween
  16. */
  17. mox.create('mox.dom.Tween', {
  18. timer : null,
  19. settings : null,
  20. items : null,
  21. target : null,
  22. noNeg : /opacity|width|height|padding|border\-width/i,
  23. Tween : function(t, a, s) {
  24. var n, o, tr;
  25. this.target = t;
  26. // Default settings
  27. this.settings = {
  28. fps : 100,
  29. frames : 100,
  30. transition : this.linear.easeNone,
  31. time : 0
  32. };
  33. tr = s.transition;
  34. if (typeof(tr) == 'string') {
  35. tr = tr.split('.');
  36. s.transition = this[tr[0]][tr[1]];
  37. s.transitionScope = this[tr[0]];
  38. }
  39. // Override
  40. for (n in s)
  41. this.settings[n] = s[n];
  42. s = this.settings;
  43. if (s.time > 0)
  44. s.frames = Math.round(s.time / 1000.0 * s.fps);
  45. for (n in a) {
  46. o = a[n];
  47. if (typeof(o.from) == 'undefined')
  48. o.from = parseInt(DOM.getStyle(t, n));
  49. if (typeof(o.to) == 'undefined')
  50. o.to = parseInt(DOM.getStyle(t, n));
  51. }
  52. this.items = a;
  53. this.onStart = new Dispatcher();
  54. this.onEnd = new Dispatcher();
  55. },
  56. onStart : null,
  57. onEnd : null,
  58. start : function(e) {
  59. var t = this, s = t.settings, st, fc = 0, fs = 1;
  60. if (this.timer)
  61. return false;
  62. st = new Date().getTime();
  63. if (this.onStart.dispatch() === false)
  64. return;
  65. this.update(0);
  66. // Setup timer
  67. this.timer = window.setInterval(function() {
  68. var f = (new Date().getTime() - st) / 1000.0 * s.fps;
  69. // Calculate frame skip
  70. if (f > fc)
  71. fs = f - fc;
  72. // Count up current frame
  73. if ((fc += fs) > s.frames) {
  74. fc = s.frames;
  75. t.update(fc);
  76. t.stop();
  77. return;
  78. }
  79. //DOM.get('data').innerHTML += f + ',' + fc + '<br>';
  80. // Update
  81. t.update(fc);
  82. }, 1000 / this.settings.fps);
  83. return true;
  84. },
  85. stop : function(e) {
  86. if (this.timer) {
  87. window.clearInterval(this.timer);
  88. this.timer = null;
  89. this.onEnd.dispatch();
  90. }
  91. },
  92. update : function(fc) {
  93. var n, v, il = this.items, it, s = this.settings;
  94. for (n in il) {
  95. it = il[n];
  96. it.value = s.transition.call(s.transitionScope, fc, it.from, it.to - it.from, this.settings.frames);
  97. if (n == 'opacity')
  98. v = it.value / 100.0;
  99. else
  100. v = Math.round(it.value) + (it.unit ? it.unit : 'px');
  101. if (v < 0 && this.noNeg.test(n))
  102. v = Math.abs(v);
  103. DOM.setStyle(this.target, n, v);
  104. }
  105. },
  106. // Tweening calculations by Robert Penner http://www.robertpenner.com/easing/
  107. back : {
  108. easeIn : function(t, b, c, d, s) {
  109. if (s == undefined) s = 1.70158;
  110. return c*(t/=d)*t*((s+1)*t - s) + b;
  111. },
  112. easeOut : function(t, b, c, d, s) {
  113. if (s == undefined) s = 1.70158;
  114. return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  115. },
  116. easeInOut : function(t, b, c, d, s) {
  117. if (s == undefined) s = 1.70158;
  118. if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
  119. return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  120. }
  121. },
  122. bounce : {
  123. easeOut : function(t, b, c, d) {
  124. if ((t/=d) < (1/2.75)) {
  125. return c*(7.5625*t*t) + b;
  126. } else if (t < (2/2.75)) {
  127. return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
  128. } else if (t < (2.5/2.75)) {
  129. return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
  130. } else {
  131. return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
  132. }
  133. },
  134. easeIn : function(t, b, c, d) {
  135. return c - this.easeOut (d-t, 0, c, d) + b;
  136. },
  137. easeInOut : function(t, b, c, d) {
  138. if (t < d/2) return this.easeIn (t*2, 0, c, d) * .5 + b;
  139. else return this.easeOut (t*2-d, 0, c, d) * .5 + c*.5 + b;
  140. }
  141. },
  142. circ : {
  143. easeIn : function(t, b, c, d) {
  144. return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  145. },
  146. easeOut : function(t, b, c, d) {
  147. return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  148. },
  149. easeInOut : function(t, b, c, d) {
  150. if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
  151. return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  152. }
  153. },
  154. cubic : {
  155. easeIn : function(t, b, c, d) {
  156. return c*(t/=d)*t*t + b;
  157. },
  158. easeOut : function(t, b, c, d) {
  159. return c*((t=t/d-1)*t*t + 1) + b;
  160. },
  161. easeInOut : function(t, b, c, d) {
  162. if ((t/=d/2) < 1) return c/2*t*t*t + b;
  163. return c/2*((t-=2)*t*t + 2) + b;
  164. }
  165. },
  166. elastic : {
  167. easeIn : function(t, b, c, d, a, p) {
  168. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  169. if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
  170. else var s = p/(2*Math.PI) * Math.asin (c/a);
  171. return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  172. },
  173. easeOut : function(t, b, c, d, a, p) {
  174. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  175. if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
  176. else var s = p/(2*Math.PI) * Math.asin (c/a);
  177. return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);
  178. },
  179. easeInOut : function(t, b, c, d, a, p) {
  180. if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
  181. if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
  182. else var s = p/(2*Math.PI) * Math.asin (c/a);
  183. if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  184. return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  185. }
  186. },
  187. expo : {
  188. easeIn : function(t, b, c, d) {
  189. return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  190. },
  191. easeOut : function(t, b, c, d) {
  192. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  193. },
  194. easeInOut : function(t, b, c, d) {
  195. if (t==0) return b;
  196. if (t==d) return b+c;
  197. if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
  198. return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  199. }
  200. },
  201. linear : {
  202. easeNone : function(t, b, c, d) {
  203. return c*t/d + b;
  204. },
  205. easeIn : function(t, b, c, d) {
  206. return c*t/d + b;
  207. },
  208. easeOut : function(t, b, c, d) {
  209. return c*t/d + b;
  210. },
  211. easeInOut : function(t, b, c, d) {
  212. return c*t/d + b;
  213. }
  214. },
  215. quad : {
  216. easeIn : function(t, b, c, d) {
  217. return c*(t/=d)*t + b;
  218. },
  219. easeOut : function(t, b, c, d) {
  220. return -c *(t/=d)*(t-2) + b;
  221. },
  222. easeInOut : function(t, b, c, d) {
  223. if ((t/=d/2) < 1) return c/2*t*t + b;
  224. return -c/2 * ((--t)*(t-2) - 1) + b;
  225. }
  226. },
  227. quart : {
  228. easeIn : function(t, b, c, d) {
  229. return c*(t/=d)*t*t*t + b;
  230. },
  231. easeOut : function(t, b, c, d) {
  232. return -c * ((t=t/d-1)*t*t*t - 1) + b;
  233. },
  234. easeInOut : function(t, b, c, d) {
  235. if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
  236. return -c/2 * ((t-=2)*t*t*t - 2) + b;
  237. }
  238. },
  239. quint : {
  240. easeIn : function(t, b, c, d) {
  241. return c*(t/=d)*t*t*t*t + b;
  242. },
  243. easeOut : function(t, b, c, d) {
  244. return c*((t=t/d-1)*t*t*t*t + 1) + b;
  245. },
  246. easeInOut : function(t, b, c, d) {
  247. if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  248. return c/2*((t-=2)*t*t*t*t + 2) + b;
  249. }
  250. },
  251. sine : {
  252. easeIn : function(t, b, c, d) {
  253. return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  254. },
  255. easeOut : function(t, b, c, d) {
  256. return c * Math.sin(t/d * (Math.PI/2)) + b;
  257. },
  258. easeInOut : function(t, b, c, d) {
  259. return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  260. }
  261. }
  262. /**#@-*/
  263. });
  264. });