首页 > 语言 > JavaScript > 正文

javascript动画算法实例分析

2024-05-06 16:24:11
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了javascript动画算法,实例分析了javascript实现动画效果的实现及使用技巧,需要的朋友可以参考下

本文实例讲述了javascript动画算法。分享给大家供大家参考。具体如下:

动画算法

Linear:无缓动效果(匀速运动);

Quadratic:二次方的缓动;

Cubic:三次方的缓动

Quartic:四次方的缓动;

Quintic:五次方的缓动;

Sinusoidal:正弦曲线的缓动;

Exponential:指数曲线的缓动;

Circular:圆形曲线的缓动;

Elastic:指数衰减的正弦曲线缓动;

Back:超过范围的三次方缓动);

Bounce:指数衰减的反弹缓动。

每个效果都分三个缓动方式(方法),分别是:

easeIn:从0开始加速的运动;

easeOut:减速到0的运动;

easeInOut:前半段从0开始加速,后半段减速到0的运动。

函数的四个参数分别代表:

t--- current time(当前时间);

b--- beginning value(初始值);

c--- change in value(变化量);

d---duration(持续时间)

运算的结果就是当前的运动路程。

Tween.js如下:

 

 
  1. Tween = {  
  2. Linear: function(t,b,c,d){ return c*t/d + b; }, 
  3. Quad: { 
  4. easeIn: function(t,b,c,d){ 
  5. return c*(t/=d)*t + b; 
  6. }, 
  7. easeOut: function(t,b,c,d){ 
  8. return -c *(t/=d)*(t-2) + b; 
  9. }, 
  10. easeInOut: function(t,b,c,d){ 
  11. if ((t/=d/2) < 1) return c/2*t*t + b; 
  12. return -c/2 * ((--t)*(t-2) - 1) + b; 
  13. } 
  14. }, 
  15. Cubic: { 
  16. easeIn: function(t,b,c,d){ 
  17. return c*(t/=d)*t*t + b; 
  18. }, 
  19. easeOut: function(t,b,c,d){ 
  20. return c*((t=t/d-1)*t*t + 1) + b; 
  21. }, 
  22. easeInOut: function(t,b,c,d){ 
  23. if ((t/=d/2) < 1) return c/2*t*t*t + b; 
  24. return c/2*((t-=2)*t*t + 2) + b; 
  25. } 
  26. }, 
  27. Quart: { 
  28. easeIn: function(t,b,c,d){ 
  29. return c*(t/=d)*t*t*t + b; 
  30. }, 
  31. easeOut: function(t,b,c,d){ 
  32. return -c * ((t=t/d-1)*t*t*t - 1) + b; 
  33. }, 
  34. easeInOut: function(t,b,c,d){ 
  35. if ((t/=d/2) < 1) return c/2*t*t*t*t + b; 
  36. return -c/2 * ((t-=2)*t*t*t - 2) + b; 
  37. } 
  38. }, 
  39. Quint: { 
  40. easeIn: function(t,b,c,d){ 
  41. return c*(t/=d)*t*t*t*t + b; 
  42. }, 
  43. easeOut: function(t,b,c,d){ 
  44. return c*((t=t/d-1)*t*t*t*t + 1) + b; 
  45. }, 
  46. easeInOut: function(t,b,c,d){ 
  47. if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b; 
  48. return c/2*((t-=2)*t*t*t*t + 2) + b; 
  49. } 
  50. }, 
  51. Sine: { 
  52. easeIn: function(t,b,c,d){ 
  53. return -c * Math.cos(t/d * (Math.PI/2)) + c + b; 
  54. }, 
  55. easeOut: function(t,b,c,d){ 
  56. return c * Math.sin(t/d * (Math.PI/2)) + b; 
  57. }, 
  58. easeInOut: function(t,b,c,d){ 
  59. return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b; 
  60. } 
  61. }, 
  62. Expo: { 
  63. easeIn: function(t,b,c,d){ 
  64. return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; 
  65. }, 
  66. easeOut: function(t,b,c,d){ 
  67. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; 
  68. }, 
  69. easeInOut: function(t,b,c,d){ 
  70. if (t==0) return b; 
  71. if (t==d) return b+c; 
  72. if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; 
  73. return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; 
  74. } 
  75. }, 
  76. Circ: { 
  77. easeIn: function(t,b,c,d){ 
  78. return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b; 
  79. }, 
  80. easeOut: function(t,b,c,d){ 
  81. return c * Math.sqrt(1 - (t=t/d-1)*t) + b; 
  82. }, 
  83. easeInOut: function(t,b,c,d){ 
  84. if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b; 
  85. return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b; 
  86. } 
  87. }, 
  88. Elastic: { 
  89. easeIn: function(t,b,c,d,a,p){ 
  90. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3; 
  91. if (!a || a < Math.abs(c)) { a=c; var s=p/4; } 
  92. else var s = p/(2*Math.PI) * Math.asin (c/a); 
  93. return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b; 
  94. }, 
  95. easeOut: function(t,b,c,d,a,p){ 
  96. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3; 
  97. if (!a || a < Math.abs(c)) { a=c; var s=p/4; } 
  98. else var s = p/(2*Math.PI) * Math.asin (c/a); 
  99. return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b); 
  100. }, 
  101. easeInOut: function(t,b,c,d,a,p){ 
  102. if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5); 
  103. if (!a || a < Math.abs(c)) { a=c; var s=p/4; } 
  104. else var s = p/(2*Math.PI) * Math.asin (c/a); 
  105. if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b; 
  106. return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b; 
  107. } 
  108. }, 
  109. Back: { 
  110. easeIn: function(t,b,c,d,s){ 
  111. if (s == undefined) s = 1.70158; 
  112. return c*(t/=d)*t*((s+1)*t - s) + b; 
  113. }, 
  114. easeOut: function(t,b,c,d,s){ 
  115. if (s == undefined) s = 1.70158; 
  116. return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; 
  117. }, 
  118. easeInOut: function(t,b,c,d,s){ 
  119. if (s == undefined) s = 1.70158;  
  120. if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
  121. return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
  122. } 
  123. }, 
  124. Bounce: { 
  125. easeIn: function(t,b,c,d){ 
  126. return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b; 
  127. }, 
  128. easeOut: function(t,b,c,d){ 
  129. if ((t/=d) < (1/2.75)) { 
  130. return c*(7.5625*t*t) + b; 
  131. } else if (t < (2/2.75)) { 
  132. return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b; 
  133. } else if (t < (2.5/2.75)) { 
  134. return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b; 
  135. } else { 
  136. return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; 
  137. } 
  138. }, 
  139. easeInOut: function(t,b,c,d){ 
  140. if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 + b; 
  141. else return Tween.Bounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b; 
  142. } 
  143. } 
  144. } 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选