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

animator-css.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /**
  18. * @class AnimatorCSS - implementation of Animator class using css transitions
  19. */
  20. var AnimatorCSS = function () {
  21. _createClass(AnimatorCSS, [{
  22. key: 'animate',
  23. /**
  24. * @method animate
  25. * @desc main animation function
  26. * @param {Element} element
  27. * @param {Object} finalCSS
  28. * @param {number} [duration=200] - duration in milliseconds
  29. * @return {Object} result
  30. * @return {Function} result.then(callback) - sets a callback to be executed after the animation has stopped
  31. * @return {Function} result.stop(options) - stops the animation; if options.stopNext is true then it doesn't call the callback
  32. * @return {Function} result.finish(ms) - finishes the animation in the specified time in milliseconds
  33. * @return {Function} result.speed(ms) - sets the animation speed so that it finishes as if the original duration was the one specified here
  34. * @example
  35. * ````
  36. * var result = animator.animate(el, {opacity: 0.5}, 1000);
  37. *
  38. * el.addEventListener('click', function(e){
  39. * result.speed(200).then(function(){
  40. * console.log('done');
  41. * });
  42. * }, 300);
  43. * ````
  44. */
  45. value: function animate(el, final) {
  46. var duration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 200;
  47. var start = new Date().getTime(),
  48. initial = {},
  49. stopped = false,
  50. next = false,
  51. timeout = false,
  52. properties = _Object$keys(final);
  53. var updateStyles = function updateStyles() {
  54. var s = window.getComputedStyle(el);
  55. properties.forEach(s.getPropertyValue.bind(s));
  56. s = el.offsetHeight;
  57. };
  58. var result = {
  59. stop: function stop() {
  60. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  61. timeout && clearTimeout(timeout);
  62. var k = Math.min(1, (new Date().getTime() - start) / duration);
  63. properties.forEach(function (i) {
  64. el.style[i] = (1 - k) * initial[i] + k * final[i] + (i == 'opacity' ? '' : 'px');
  65. });
  66. el.style.transitionDuration = '0s';
  67. if (options.stopNext) {
  68. next = false;
  69. } else if (!stopped) {
  70. stopped = true;
  71. next && next();
  72. }
  73. return result;
  74. },
  75. then: function then(cb) {
  76. next = cb;
  77. if (stopped) {
  78. next && next();
  79. }
  80. return result;
  81. },
  82. speed: function speed(newDuration) {
  83. if (internal.config.animationsDisabled) {
  84. newDuration = 0;
  85. }
  86. if (!stopped) {
  87. timeout && clearTimeout(timeout);
  88. var passed = new Date().getTime() - start;
  89. var k = passed / duration;
  90. var remaining = newDuration * (1 - k);
  91. properties.forEach(function (i) {
  92. el.style[i] = (1 - k) * initial[i] + k * final[i] + (i == 'opacity' ? '' : 'px');
  93. });
  94. updateStyles();
  95. start = el.speedUpTime;
  96. duration = remaining;
  97. el.style.transitionDuration = duration / 1000 + 's';
  98. properties.forEach(function (i) {
  99. el.style[i] = final[i] + (i == 'opacity' ? '' : 'px');
  100. });
  101. timeout = setTimeout(result.stop, remaining);
  102. }
  103. return result;
  104. },
  105. finish: function finish() {
  106. var milliseconds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 50;
  107. var k = (new Date().getTime() - start) / duration;
  108. result.speed(milliseconds / (1 - k));
  109. return result;
  110. }
  111. };
  112. if (el.hasAttribute('disabled') || stopped || internal.config.animationsDisabled) {
  113. return result;
  114. }
  115. var style = window.getComputedStyle(el);
  116. properties.forEach(function (e) {
  117. var v = parseFloat(style.getPropertyValue(e));
  118. initial[e] = isNaN(v) ? 0 : v;
  119. });
  120. if (!stopped) {
  121. el.style.transitionProperty = properties.join(',');
  122. el.style.transitionDuration = duration / 1000 + 's';
  123. properties.forEach(function (e) {
  124. el.style[e] = final[e] + (e == 'opacity' ? '' : 'px');
  125. });
  126. }
  127. timeout = setTimeout(result.stop, duration);
  128. this._onStopAnimations(el, result.stop);
  129. return result;
  130. }
  131. }]);
  132. function AnimatorCSS() {
  133. _classCallCheck(this, AnimatorCSS);
  134. this._queue = [];
  135. this._index = 0;
  136. }
  137. _createClass(AnimatorCSS, [{
  138. key: '_onStopAnimations',
  139. value: function _onStopAnimations(el, listener) {
  140. var queue = this._queue;
  141. var i = this._index++;
  142. queue[el] = queue[el] || [];
  143. queue[el][i] = function (options) {
  144. delete queue[el][i];
  145. if (queue[el] && queue[el].length == 0) {
  146. delete queue[el];
  147. }
  148. return listener(options);
  149. };
  150. }
  151. /**
  152. * @method stopAnimations
  153. * @desc stops active animations on a specified element
  154. * @param {Element|Array} element - element or array of elements
  155. * @param {Object} [options={}]
  156. * @param {Boolean} [options.stopNext] - the callbacks after the animations won't be called if this option is true
  157. */
  158. }, {
  159. key: 'stopAnimations',
  160. value: function stopAnimations(el) {
  161. var _this = this;
  162. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  163. if (Array.isArray(el)) {
  164. return el.forEach(function (el) {
  165. _this.stopAnimations(el, options);
  166. });
  167. }
  168. (this._queue[el] || []).forEach(function (e) {
  169. e(options || {});
  170. });
  171. }
  172. /**
  173. * @method stopAll
  174. * @desc stops all active animations
  175. * @param {Object} [options={}]
  176. * @param {Boolean} [options.stopNext] - the callbacks after the animations won't be called if this option is true
  177. */
  178. }, {
  179. key: 'stopAll',
  180. value: function stopAll() {
  181. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  182. this.stopAnimations(_Object$keys(this._queue), options);
  183. }
  184. /**
  185. * @method fade
  186. * @desc fades the element (short version for animate(el, {opacity: 0}))
  187. * @param {Element} element
  188. * @param {number} [duration=200]
  189. */
  190. }, {
  191. key: 'fade',
  192. value: function fade(el) {
  193. var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
  194. return this.animate(el, { opacity: 0 }, duration);
  195. }
  196. }]);
  197. return AnimatorCSS;
  198. }();
  199. export default AnimatorCSS;