Animatable.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. var Animator = require("../animation/Animator");
  2. var log = require("../core/log");
  3. var _util = require("../core/util");
  4. var isString = _util.isString;
  5. var isFunction = _util.isFunction;
  6. var isObject = _util.isObject;
  7. var isArrayLike = _util.isArrayLike;
  8. var indexOf = _util.indexOf;
  9. /**
  10. * @alias modue:zrender/mixin/Animatable
  11. * @constructor
  12. */
  13. var Animatable = function () {
  14. /**
  15. * @type {Array.<module:zrender/animation/Animator>}
  16. * @readOnly
  17. */
  18. this.animators = [];
  19. };
  20. Animatable.prototype = {
  21. constructor: Animatable,
  22. /**
  23. * 动画
  24. *
  25. * @param {string} path The path to fetch value from object, like 'a.b.c'.
  26. * @param {boolean} [loop] Whether to loop animation.
  27. * @return {module:zrender/animation/Animator}
  28. * @example:
  29. * el.animate('style', false)
  30. * .when(1000, {x: 10} )
  31. * .done(function(){ // Animation done })
  32. * .start()
  33. */
  34. animate: function (path, loop) {
  35. var target;
  36. var animatingShape = false;
  37. var el = this;
  38. var zr = this.__zr;
  39. if (path) {
  40. var pathSplitted = path.split('.');
  41. var prop = el; // If animating shape
  42. animatingShape = pathSplitted[0] === 'shape';
  43. for (var i = 0, l = pathSplitted.length; i < l; i++) {
  44. if (!prop) {
  45. continue;
  46. }
  47. prop = prop[pathSplitted[i]];
  48. }
  49. if (prop) {
  50. target = prop;
  51. }
  52. } else {
  53. target = el;
  54. }
  55. if (!target) {
  56. log('Property "' + path + '" is not existed in element ' + el.id);
  57. return;
  58. }
  59. var animators = el.animators;
  60. var animator = new Animator(target, loop);
  61. animator.during(function (target) {
  62. el.dirty(animatingShape);
  63. }).done(function () {
  64. // FIXME Animator will not be removed if use `Animator#stop` to stop animation
  65. animators.splice(indexOf(animators, animator), 1);
  66. });
  67. animators.push(animator); // If animate after added to the zrender
  68. if (zr) {
  69. zr.animation.addAnimator(animator);
  70. }
  71. return animator;
  72. },
  73. /**
  74. * 停止动画
  75. * @param {boolean} forwardToLast If move to last frame before stop
  76. */
  77. stopAnimation: function (forwardToLast) {
  78. var animators = this.animators;
  79. var len = animators.length;
  80. for (var i = 0; i < len; i++) {
  81. animators[i].stop(forwardToLast);
  82. }
  83. animators.length = 0;
  84. return this;
  85. },
  86. /**
  87. * Caution: this method will stop previous animation.
  88. * So do not use this method to one element twice before
  89. * animation starts, unless you know what you are doing.
  90. * @param {Object} target
  91. * @param {number} [time=500] Time in ms
  92. * @param {string} [easing='linear']
  93. * @param {number} [delay=0]
  94. * @param {Function} [callback]
  95. * @param {Function} [forceAnimate] Prevent stop animation and callback
  96. * immediently when target values are the same as current values.
  97. *
  98. * @example
  99. * // Animate position
  100. * el.animateTo({
  101. * position: [10, 10]
  102. * }, function () { // done })
  103. *
  104. * // Animate shape, style and position in 100ms, delayed 100ms, with cubicOut easing
  105. * el.animateTo({
  106. * shape: {
  107. * width: 500
  108. * },
  109. * style: {
  110. * fill: 'red'
  111. * }
  112. * position: [10, 10]
  113. * }, 100, 100, 'cubicOut', function () { // done })
  114. */
  115. // TODO Return animation key
  116. animateTo: function (target, time, delay, easing, callback, forceAnimate) {
  117. // animateTo(target, time, easing, callback);
  118. if (isString(delay)) {
  119. callback = easing;
  120. easing = delay;
  121. delay = 0;
  122. } // animateTo(target, time, delay, callback);
  123. else if (isFunction(easing)) {
  124. callback = easing;
  125. easing = 'linear';
  126. delay = 0;
  127. } // animateTo(target, time, callback);
  128. else if (isFunction(delay)) {
  129. callback = delay;
  130. delay = 0;
  131. } // animateTo(target, callback)
  132. else if (isFunction(time)) {
  133. callback = time;
  134. time = 500;
  135. } // animateTo(target)
  136. else if (!time) {
  137. time = 500;
  138. } // Stop all previous animations
  139. this.stopAnimation();
  140. this._animateToShallow('', this, target, time, delay); // Animators may be removed immediately after start
  141. // if there is nothing to animate
  142. var animators = this.animators.slice();
  143. var count = animators.length;
  144. function done() {
  145. count--;
  146. if (!count) {
  147. callback && callback();
  148. }
  149. } // No animators. This should be checked before animators[i].start(),
  150. // because 'done' may be executed immediately if no need to animate.
  151. if (!count) {
  152. callback && callback();
  153. } // Start after all animators created
  154. // Incase any animator is done immediately when all animation properties are not changed
  155. for (var i = 0; i < animators.length; i++) {
  156. animators[i].done(done).start(easing, forceAnimate);
  157. }
  158. },
  159. /**
  160. * @private
  161. * @param {string} path=''
  162. * @param {Object} source=this
  163. * @param {Object} target
  164. * @param {number} [time=500]
  165. * @param {number} [delay=0]
  166. *
  167. * @example
  168. * // Animate position
  169. * el._animateToShallow({
  170. * position: [10, 10]
  171. * })
  172. *
  173. * // Animate shape, style and position in 100ms, delayed 100ms
  174. * el._animateToShallow({
  175. * shape: {
  176. * width: 500
  177. * },
  178. * style: {
  179. * fill: 'red'
  180. * }
  181. * position: [10, 10]
  182. * }, 100, 100)
  183. */
  184. _animateToShallow: function (path, source, target, time, delay) {
  185. var objShallow = {};
  186. var propertyCount = 0;
  187. for (var name in target) {
  188. if (!target.hasOwnProperty(name)) {
  189. continue;
  190. }
  191. if (source[name] != null) {
  192. if (isObject(target[name]) && !isArrayLike(target[name])) {
  193. this._animateToShallow(path ? path + '.' + name : name, source[name], target[name], time, delay);
  194. } else {
  195. objShallow[name] = target[name];
  196. propertyCount++;
  197. }
  198. } else if (target[name] != null) {
  199. // Attr directly if not has property
  200. // FIXME, if some property not needed for element ?
  201. if (!path) {
  202. this.attr(name, target[name]);
  203. } else {
  204. // Shape or style
  205. var props = {};
  206. props[path] = {};
  207. props[path][name] = target[name];
  208. this.attr(props);
  209. }
  210. }
  211. }
  212. if (propertyCount > 0) {
  213. this.animate(path, false).when(time == null ? 500 : time, objShallow).delay(delay || 0);
  214. }
  215. return this;
  216. }
  217. };
  218. var _default = Animatable;
  219. module.exports = _default;