animation.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import * as zrUtil from 'zrender/src/core/util';
  20. /**
  21. * @param {number} [time=500] Time in ms
  22. * @param {string} [easing='linear']
  23. * @param {number} [delay=0]
  24. * @param {Function} [callback]
  25. *
  26. * @example
  27. * // Animate position
  28. * animation
  29. * .createWrap()
  30. * .add(el1, {position: [10, 10]})
  31. * .add(el2, {shape: {width: 500}, style: {fill: 'red'}}, 400)
  32. * .done(function () { // done })
  33. * .start('cubicOut');
  34. */
  35. export function createWrap() {
  36. var storage = [];
  37. var elExistsMap = {};
  38. var doneCallback;
  39. return {
  40. /**
  41. * Caution: a el can only be added once, otherwise 'done'
  42. * might not be called. This method checks this (by el.id),
  43. * suppresses adding and returns false when existing el found.
  44. *
  45. * @param {modele:zrender/Element} el
  46. * @param {Object} target
  47. * @param {number} [time=500]
  48. * @param {number} [delay=0]
  49. * @param {string} [easing='linear']
  50. * @return {boolean} Whether adding succeeded.
  51. *
  52. * @example
  53. * add(el, target, time, delay, easing);
  54. * add(el, target, time, easing);
  55. * add(el, target, time);
  56. * add(el, target);
  57. */
  58. add: function (el, target, time, delay, easing) {
  59. if (zrUtil.isString(delay)) {
  60. easing = delay;
  61. delay = 0;
  62. }
  63. if (elExistsMap[el.id]) {
  64. return false;
  65. }
  66. elExistsMap[el.id] = 1;
  67. storage.push(
  68. {el: el, target: target, time: time, delay: delay, easing: easing}
  69. );
  70. return true;
  71. },
  72. /**
  73. * Only execute when animation finished. Will not execute when any
  74. * of 'stop' or 'stopAnimation' called.
  75. *
  76. * @param {Function} callback
  77. */
  78. done: function (callback) {
  79. doneCallback = callback;
  80. return this;
  81. },
  82. /**
  83. * Will stop exist animation firstly.
  84. */
  85. start: function () {
  86. var count = storage.length;
  87. for (var i = 0, len = storage.length; i < len; i++) {
  88. var item = storage[i];
  89. item.el.animateTo(item.target, item.time, item.delay, item.easing, done);
  90. }
  91. return this;
  92. function done() {
  93. count--;
  94. if (!count) {
  95. storage.length = 0;
  96. elExistsMap = {};
  97. doneCallback && doneCallback();
  98. }
  99. }
  100. }
  101. };
  102. }