Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

animator-js.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import _Object$keys from 'babel-runtime/core-js/object/keys';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _createClass from 'babel-runtime/helpers/createClass';
  4. /*
  5. Copyright 2013-2016 ASIAL CORPORATION
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. import internal from '../../ons/internal';
  17. var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (cb) {
  18. setTimeout(function () {
  19. cb(new Date().getTime());
  20. }, 1000 / 60);
  21. };
  22. raf = raf.bind(window);
  23. /**
  24. * @class AnimatorJS - implementation of Animator class using javascript
  25. */
  26. var AnimatorJS = function () {
  27. _createClass(AnimatorJS, [{
  28. key: 'animate',
  29. /**
  30. * @method animate
  31. * @desc main animation function
  32. * @param {Element} element
  33. * @param {Object} finalCSS
  34. * @param {number} [duration=200] - duration in milliseconds
  35. * @return {Object} result
  36. * @return {Function} result.then(callback) - sets a callback to be executed after the animation has stopped
  37. * @return {Function} result.stop(options) - stops the animation; if options.stopNext is true then it doesn't call the callback
  38. * @return {Function} result.finish(ms) - finishes the animation in the specified time in milliseconds
  39. * @return {Function} result.speed(ms) - sets the animation speed so that it finishes as if the original duration was the one specified here
  40. * @example
  41. * ````
  42. * var result = animator.animate(el, {opacity: 0.5}, 1000);
  43. *
  44. * el.addEventListener('click', function(e){
  45. * result.speed(200).then(function(){
  46. * console.log('done');
  47. * });
  48. * }, 300);
  49. * ````
  50. */
  51. value: function animate(el, final) {
  52. var duration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 200;
  53. var start,
  54. initial = {},
  55. stopped = false,
  56. next,
  57. elapsed,
  58. properties = _Object$keys(final);
  59. var result = {
  60. stop: function stop() {
  61. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  62. if (options.stopNext) {
  63. next = false;
  64. }
  65. if (!stopped) {
  66. stopped = true;
  67. next && next();
  68. }
  69. return result;
  70. },
  71. then: function then(cb) {
  72. next = cb;
  73. if (stopped) {
  74. next && next();
  75. }
  76. return result;
  77. },
  78. finish: function finish() {
  79. var milliseconds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 50;
  80. var k = milliseconds / (duration - elapsed);
  81. if (internal.config.animationsDisabled) {
  82. k = 0;
  83. }
  84. if (k < 1) {
  85. start += elapsed - elapsed * k;
  86. duration *= k;
  87. }
  88. return result;
  89. },
  90. speed: function speed(newDuration) {
  91. return result.finish(newDuration * (1 - elapsed / duration));
  92. }
  93. };
  94. if (el.hasAttribute('disabled') || internal.config.animationsDisabled) {
  95. return result;
  96. }
  97. var cs = window.getComputedStyle(el);
  98. properties.forEach(function (i) {
  99. initial[i] = parseFloat(el.style[i] || cs.getPropertyValue(i));
  100. });
  101. this._onStopAnimations(el, result.stop);
  102. var step = function step(timestamp) {
  103. start = start || timestamp;
  104. elapsed = timestamp - start;
  105. if (!stopped) {
  106. properties.forEach(function (i) {
  107. el.style[i] = initial[i] + (final[i] - initial[i]) * Math.min(1, elapsed / duration) + (i == 'opacity' ? 0 : 'px');
  108. });
  109. stopped = stopped || elapsed >= duration;
  110. if (!stopped) {
  111. return raf(step);
  112. }
  113. }
  114. return next && next();
  115. };
  116. step(0);
  117. return result;
  118. }
  119. }]);
  120. function AnimatorJS() {
  121. _classCallCheck(this, AnimatorJS);
  122. this._queue = [];
  123. this._index = 0;
  124. }
  125. _createClass(AnimatorJS, [{
  126. key: '_onStopAnimations',
  127. value: function _onStopAnimations(el, listener) {
  128. var queue = this._queue;
  129. var i = this._index++;
  130. queue[el] = queue[el] || [];
  131. queue[el][i] = function (options) {
  132. delete queue[el][i];
  133. if (queue[el] && queue[el].length == 0) {
  134. delete queue[el];
  135. }
  136. return listener(options);
  137. };
  138. }
  139. /**
  140. * @method stopAnimations
  141. * @desc stops active animations on a specified element
  142. * @param {Element|Array} element - element or array of elements
  143. * @param {Object} [options={}]
  144. * @param {Boolean} [options.stopNext] - the callbacks after the animations won't be called if this option is true
  145. */
  146. }, {
  147. key: 'stopAnimations',
  148. value: function stopAnimations(el) {
  149. var _this = this;
  150. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  151. if (Array.isArray(el)) {
  152. return el.forEach(function (el) {
  153. _this.stopAnimations(el, options);
  154. });
  155. }
  156. (this._queue[el] || []).forEach(function (e) {
  157. e(options || {});
  158. });
  159. }
  160. /**
  161. * @method stopAll
  162. * @desc stops all active animations
  163. * @param {Object} [options={}]
  164. * @param {Boolean} [options.stopNext] - the callbacks after the animations won't be called if this option is true
  165. */
  166. }, {
  167. key: 'stopAll',
  168. value: function stopAll() {
  169. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  170. this.stopAnimations(_Object$keys(this._queue), options);
  171. }
  172. /**
  173. * @method fade
  174. * @desc fades the element (short version for animate(el, {opacity: 0}))
  175. * @param {Element} element
  176. * @param {number} [duration=200]
  177. */
  178. }, {
  179. key: 'fade',
  180. value: function fade(el) {
  181. var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
  182. return this.animate(el, { opacity: 0 }, duration);
  183. }
  184. }]);
  185. return AnimatorJS;
  186. }();
  187. export default AnimatorJS;