animation.js 3.9 KB

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