Tween.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. export class TweenManager {
  2. constructor() {
  3. this.clear()
  4. }
  5. add(tween) {
  6. tween.setId(this.tweenIdIndex++)
  7. this.activeTweens.push(tween);
  8. return tween.getId();
  9. }
  10. remove(tween) {
  11. this.activeTweens.splice(this.activeTweens.findIndex(t => t == tween), 1);
  12. }
  13. cancel(tweenId) {
  14. let tweenIndex = this.activeTweens.findIndex(t => t.getId() == tweenId);
  15. if(tweenIndex != -1) {
  16. this.activeTweens.splice(tweenIndex, 1)
  17. }
  18. }
  19. update() {
  20. let completedTweens = [];
  21. for(let i = 0; i < this.activeTweens.length; i++) {
  22. let tween = this.activeTweens[i];
  23. tween.update();
  24. if(tween.isComplete()) {
  25. completedTweens.push(tween);
  26. }
  27. }
  28. for(let i = 0; i < completedTweens.length; i++) {
  29. let tween = completedTweens[i];
  30. tween.onFinish();
  31. this.remove(tween);
  32. }
  33. }
  34. clear() {
  35. this.activeTweens = [];
  36. this.tweenIdIndex = 0;
  37. }
  38. }
  39. export class Tween {
  40. constructor(objToTween, targetProperties, duration, easing = Easing.Linear.EaseNone, onFinish = () => {}) {
  41. this.objectToTween = objToTween;
  42. this.startProperties = {};
  43. this.targetProperties = targetProperties;
  44. this.saveInitialValues(this.startProperties, objToTween, targetProperties);
  45. this.startTime = new Date().getTime()
  46. this.endTime = this.startTime + duration
  47. this.easing = easing;
  48. this.onFinish = onFinish;
  49. this.id = -1;
  50. }
  51. saveInitialValues(propertiesObj, obj, targetProperties) {
  52. for(let property in targetProperties) {
  53. let startValue = obj[property];
  54. if(typeof startValue == "object") {
  55. propertiesObj[property] = {};
  56. this.saveInitialValues(propertiesObj[property], obj[property], targetProperties[property]);
  57. continue;
  58. }
  59. propertiesObj[property] = startValue;
  60. }
  61. }
  62. update() {
  63. let currentTime = new Date().getTime();
  64. let runTime = currentTime - this.startTime;
  65. let duration = this.endTime - this.startTime;
  66. let percentageComplete = Math.min(1, runTime / duration);
  67. let valueToUpdate = this.easing(percentageComplete);
  68. this.updateProperties(this.objectToTween, this.startProperties, this.targetProperties, valueToUpdate);
  69. }
  70. updateProperties(obj, objStart, objTarget, valueToUpdate) {
  71. for(let property in objStart) {
  72. let startValue = objStart[property];
  73. if(typeof startValue == "object") {
  74. this.updateProperties(obj[property], objStart[property], objTarget[property], valueToUpdate);
  75. continue;
  76. }
  77. let endValue = objTarget[property];
  78. let delta = endValue - startValue;
  79. obj[property] = startValue + (delta * valueToUpdate);
  80. }
  81. }
  82. setId(newId){
  83. this.id = newId
  84. }
  85. getId() {
  86. return this.id
  87. }
  88. isComplete() {
  89. return this.endTime <= new Date().getTime()
  90. }
  91. }
  92. export class Easing {}
  93. Easing.Linear = {};
  94. Easing.Linear.EaseNone = (percentageComplete) => percentageComplete;
  95. Easing.Quadratic = {};
  96. Easing.Quadratic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete;
  97. Easing.Quadratic.EaseOut = (percentageComplete) => -percentageComplete * ( percentageComplete - 2 );
  98. Easing.Quadratic.EaseInOut = (percentageComplete) => {
  99. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * percentageComplete * percentageComplete;
  100. return - 0.5 * ( --percentageComplete * ( percentageComplete - 2 ) - 1 );
  101. };
  102. Easing.Cubic = {};
  103. Easing.Cubic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete * percentageComplete;
  104. Easing.Cubic.EaseOut = (percentageComplete) => --percentageComplete * percentageComplete * percentageComplete + 1;
  105. Easing.Cubic.EaseInOut = (percentageComplete) => {
  106. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * percentageComplete * percentageComplete * percentageComplete;
  107. return 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * percentageComplete + 2 );
  108. };
  109. Easing.Quartic = {};
  110. Easing.Quartic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete * percentageComplete * percentageComplete;
  111. Easing.Quartic.EaseOut = (percentageComplete) => -( --percentageComplete * percentageComplete * percentageComplete * percentageComplete - 1 );
  112. Easing.Quartic.EaseInOut = (percentageComplete) => {
  113. if ( ( percentageComplete *= 2 ) < 1) return 0.5 * percentageComplete * percentageComplete * percentageComplete * percentageComplete;
  114. return - 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * percentageComplete * percentageComplete - 2 );
  115. };
  116. Easing.Quintic = {};
  117. Easing.Quintic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete * percentageComplete * percentageComplete * percentageComplete;
  118. Easing.Quintic.EaseOut = (percentageComplete) => ( percentageComplete = percentageComplete - 1 ) * percentageComplete * percentageComplete * percentageComplete * percentageComplete + 1;
  119. Easing.Quintic.EaseInOut = (percentageComplete) => {
  120. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * percentageComplete * percentageComplete * percentageComplete * percentageComplete * percentageComplete;
  121. return 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * percentageComplete * percentageComplete * percentageComplete + 2 );
  122. };
  123. Easing.Sinusoidal = {};
  124. Easing.Sinusoidal.EaseIn = (percentageComplete) => -Math.cos( percentageComplete * Math.PI / 2 ) + 1;
  125. Easing.Sinusoidal.EaseOut = (percentageComplete) => Math.sin( percentageComplete * Math.PI / 2 );
  126. Easing.Sinusoidal.EaseInOut = (percentageComplete) => -0.5 * ( Math.cos( Math.PI * percentageComplete ) - 1 );
  127. Easing.Exponential = {};
  128. Easing.Exponential.EaseIn = (percentageComplete) => {
  129. return percentageComplete == 0 ? 0 : Math.pow( 2, 10 * ( percentageComplete - 1 ) );
  130. };
  131. Easing.Exponential.EaseOut = (percentageComplete) => {
  132. return percentageComplete == 1 ? 1 : - Math.pow( 2, - 10 * percentageComplete ) + 1;
  133. };
  134. Easing.Exponential.EaseInOut = (percentageComplete) => {
  135. if ( percentageComplete == 0 ) return 0;
  136. if ( percentageComplete == 1 ) return 1;
  137. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * Math.pow( 2, 10 * ( percentageComplete - 1 ) );
  138. return 0.5 * ( - Math.pow( 2, - 10 * ( percentageComplete - 1 ) ) + 2 );
  139. };
  140. Easing.Circular = {};
  141. Easing.Circular.EaseIn = (percentageComplete) => -(Math.sqrt( 1 - percentageComplete * percentageComplete ) - 1);
  142. Easing.Circular.EaseOut = (percentageComplete) => Math.sqrt( 1 - (--percentageComplete * percentageComplete) );
  143. Easing.Circular.EaseInOut = (percentageComplete) => {
  144. if ( ( percentageComplete /= 0.5 ) < 1) return - 0.5 * ( Math.sqrt( 1 - percentageComplete * percentageComplete) - 1);
  145. return 0.5 * ( Math.sqrt( 1 - ( percentageComplete -= 2) * percentageComplete) + 1);
  146. };
  147. Easing.Elastic = {};
  148. Easing.Elastic.EaseIn = (percentageComplete) => {
  149. var s, a = 0.1, p = 0.4;
  150. if ( percentageComplete == 0 ) return 0; if ( percentageComplete == 1 ) return 1; if ( !p ) p = 0.3;
  151. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  152. else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
  153. return - ( a * Math.pow( 2, 10 * ( percentageComplete -= 1 ) ) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) );
  154. };
  155. Easing.Elastic.EaseOut = (percentageComplete) => {
  156. var s, a = 0.1, p = 0.4;
  157. if ( percentageComplete == 0 ) return 0; if ( percentageComplete == 1 ) return 1; if ( !p ) p = 0.3;
  158. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  159. else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
  160. return ( a * Math.pow( 2, - 10 * percentageComplete) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) + 1 );
  161. };
  162. Easing.Elastic.EaseInOut = (percentageComplete) => {
  163. var s, a = 0.1, p = 0.4;
  164. if ( percentageComplete == 0 ) return 0; if ( percentageComplete == 1 ) return 1; if ( !p ) p = 0.3;
  165. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  166. else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
  167. if ( ( percentageComplete *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( percentageComplete -= 1 ) ) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) );
  168. return a * Math.pow( 2, -10 * ( percentageComplete -= 1 ) ) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1;
  169. };
  170. Easing.Back = {};
  171. Easing.Back.EaseIn = (percentageComplete) => {
  172. var s = 1.70158;
  173. return percentageComplete * percentageComplete * ( ( s + 1 ) * percentageComplete - s );
  174. };
  175. Easing.Back.EaseOut = (percentageComplete) => {
  176. var s = 1.70158;
  177. return ( percentageComplete = percentageComplete - 1 ) * percentageComplete * ( ( s + 1 ) * percentageComplete + s ) + 1;
  178. };
  179. Easing.Back.EaseInOut = (percentageComplete) => {
  180. var s = 1.70158 * 1.525;
  181. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * ( percentageComplete * percentageComplete * ( ( s + 1 ) * percentageComplete - s ) );
  182. return 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * ( ( s + 1 ) * percentageComplete + s ) + 2 );
  183. };
  184. Easing.Bounce = {};
  185. Easing.Bounce.EaseIn = (percentageComplete) => 1 - Easing.Bounce.EaseOut( 1 - percentageComplete );
  186. Easing.Bounce.EaseOut = (percentageComplete) => {
  187. if ( ( percentageComplete /= 1 ) < ( 1 / 2.75 ) ) {
  188. return 7.5625 * percentageComplete * percentageComplete;
  189. } else if ( percentageComplete < ( 2 / 2.75 ) ) {
  190. return 7.5625 * ( percentageComplete -= ( 1.5 / 2.75 ) ) * percentageComplete + 0.75;
  191. } else if ( percentageComplete < ( 2.5 / 2.75 ) ) {
  192. return 7.5625 * ( percentageComplete -= ( 2.25 / 2.75 ) ) * percentageComplete + 0.9375;
  193. } else {
  194. return 7.5625 * ( percentageComplete -= ( 2.625 / 2.75 ) ) * percentageComplete + 0.984375;
  195. }
  196. };
  197. Easing.Bounce.EaseInOut = (percentageComplete) => {
  198. if ( percentageComplete < 0.5 ) return Easing.Bounce.EaseIn( percentageComplete * 2 ) * 0.5;
  199. return Easing.Bounce.EaseOut( percentageComplete * 2 - 1 ) * 0.5 + 0.5;
  200. }