Animatable.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. var Animator = require("../animation/Animator");
  2. var logError = 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 module: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. logError('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(this, target, time, delay, easing, callback, forceAnimate);
  118. },
  119. /**
  120. * Animate from the target state to current state.
  121. * The params and the return value are the same as `this.animateTo`.
  122. */
  123. animateFrom: function (target, time, delay, easing, callback, forceAnimate) {
  124. animateTo(this, target, time, delay, easing, callback, forceAnimate, true);
  125. }
  126. };
  127. function animateTo(animatable, target, time, delay, easing, callback, forceAnimate, reverse) {
  128. // animateTo(target, time, easing, callback);
  129. if (isString(delay)) {
  130. callback = easing;
  131. easing = delay;
  132. delay = 0;
  133. } // animateTo(target, time, delay, callback);
  134. else if (isFunction(easing)) {
  135. callback = easing;
  136. easing = 'linear';
  137. delay = 0;
  138. } // animateTo(target, time, callback);
  139. else if (isFunction(delay)) {
  140. callback = delay;
  141. delay = 0;
  142. } // animateTo(target, callback)
  143. else if (isFunction(time)) {
  144. callback = time;
  145. time = 500;
  146. } // animateTo(target)
  147. else if (!time) {
  148. time = 500;
  149. } // Stop all previous animations
  150. animatable.stopAnimation();
  151. animateToShallow(animatable, '', animatable, target, time, delay, reverse); // Animators may be removed immediately after start
  152. // if there is nothing to animate
  153. var animators = animatable.animators.slice();
  154. var count = animators.length;
  155. function done() {
  156. count--;
  157. if (!count) {
  158. callback && callback();
  159. }
  160. } // No animators. This should be checked before animators[i].start(),
  161. // because 'done' may be executed immediately if no need to animate.
  162. if (!count) {
  163. callback && callback();
  164. } // Start after all animators created
  165. // Incase any animator is done immediately when all animation properties are not changed
  166. for (var i = 0; i < animators.length; i++) {
  167. animators[i].done(done).start(easing, forceAnimate);
  168. }
  169. }
  170. /**
  171. * @param {string} path=''
  172. * @param {Object} source=animatable
  173. * @param {Object} target
  174. * @param {number} [time=500]
  175. * @param {number} [delay=0]
  176. * @param {boolean} [reverse] If `true`, animate
  177. * from the `target` to current state.
  178. *
  179. * @example
  180. * // Animate position
  181. * el._animateToShallow({
  182. * position: [10, 10]
  183. * })
  184. *
  185. * // Animate shape, style and position in 100ms, delayed 100ms
  186. * el._animateToShallow({
  187. * shape: {
  188. * width: 500
  189. * },
  190. * style: {
  191. * fill: 'red'
  192. * }
  193. * position: [10, 10]
  194. * }, 100, 100)
  195. */
  196. function animateToShallow(animatable, path, source, target, time, delay, reverse) {
  197. var objShallow = {};
  198. var propertyCount = 0;
  199. for (var name in target) {
  200. if (!target.hasOwnProperty(name)) {
  201. continue;
  202. }
  203. if (source[name] != null) {
  204. if (isObject(target[name]) && !isArrayLike(target[name])) {
  205. animateToShallow(animatable, path ? path + '.' + name : name, source[name], target[name], time, delay, reverse);
  206. } else {
  207. if (reverse) {
  208. objShallow[name] = source[name];
  209. setAttrByPath(animatable, path, name, target[name]);
  210. } else {
  211. objShallow[name] = target[name];
  212. }
  213. propertyCount++;
  214. }
  215. } else if (target[name] != null && !reverse) {
  216. setAttrByPath(animatable, path, name, target[name]);
  217. }
  218. }
  219. if (propertyCount > 0) {
  220. animatable.animate(path, false).when(time == null ? 500 : time, objShallow).delay(delay || 0);
  221. }
  222. }
  223. function setAttrByPath(el, path, name, value) {
  224. // Attr directly if not has property
  225. // FIXME, if some property not needed for element ?
  226. if (!path) {
  227. el.attr(name, value);
  228. } else {
  229. // Only support set shape or style
  230. var props = {};
  231. props[path] = {};
  232. props[path][name] = value;
  233. el.attr(props);
  234. }
  235. }
  236. var _default = Animatable;
  237. module.exports = _default;