morphTransitionHelper.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { separateMorph, combineMorph, morphPath, isCombineMorphing } from 'zrender/lib/tool/morphPath.js';
  41. import { Path } from '../util/graphic.js';
  42. import { defaults, isArray } from 'zrender/lib/core/util.js';
  43. import { getAnimationConfig } from './basicTransition.js';
  44. import { clonePath } from 'zrender/lib/tool/path.js';
  45. function isMultiple(elements) {
  46. return isArray(elements[0]);
  47. }
  48. function prepareMorphBatches(one, many) {
  49. var batches = [];
  50. var batchCount = one.length;
  51. for (var i = 0; i < batchCount; i++) {
  52. batches.push({
  53. one: one[i],
  54. many: []
  55. });
  56. }
  57. for (var i = 0; i < many.length; i++) {
  58. var len = many[i].length;
  59. var k = void 0;
  60. for (k = 0; k < len; k++) {
  61. batches[k % batchCount].many.push(many[i][k]);
  62. }
  63. }
  64. var off = 0;
  65. // If one has more paths than each one of many. average them.
  66. for (var i = batchCount - 1; i >= 0; i--) {
  67. if (!batches[i].many.length) {
  68. var moveFrom = batches[off].many;
  69. if (moveFrom.length <= 1) {
  70. // Not enough
  71. // Start from the first one.
  72. if (off) {
  73. off = 0;
  74. } else {
  75. return batches;
  76. }
  77. }
  78. var len = moveFrom.length;
  79. var mid = Math.ceil(len / 2);
  80. batches[i].many = moveFrom.slice(mid, len);
  81. batches[off].many = moveFrom.slice(0, mid);
  82. off++;
  83. }
  84. }
  85. return batches;
  86. }
  87. var pathDividers = {
  88. clone: function (params) {
  89. var ret = [];
  90. // Fitting the alpha
  91. var approxOpacity = 1 - Math.pow(1 - params.path.style.opacity, 1 / params.count);
  92. for (var i = 0; i < params.count; i++) {
  93. var cloned = clonePath(params.path);
  94. cloned.setStyle('opacity', approxOpacity);
  95. ret.push(cloned);
  96. }
  97. return ret;
  98. },
  99. // Use the default divider
  100. split: null
  101. };
  102. export function applyMorphAnimation(from, to, divideShape, seriesModel, dataIndex, animateOtherProps) {
  103. if (!from.length || !to.length) {
  104. return;
  105. }
  106. var updateAnimationCfg = getAnimationConfig('update', seriesModel, dataIndex);
  107. if (!(updateAnimationCfg && updateAnimationCfg.duration > 0)) {
  108. return;
  109. }
  110. var animationDelay = seriesModel.getModel('universalTransition').get('delay');
  111. var animationCfg = Object.assign({
  112. // Need to setToFinal so the further calculation based on the style can be correct.
  113. // Like emphasis color.
  114. setToFinal: true
  115. }, updateAnimationCfg);
  116. var many;
  117. var one;
  118. if (isMultiple(from)) {
  119. // manyToOne
  120. many = from;
  121. one = to;
  122. }
  123. if (isMultiple(to)) {
  124. // oneToMany
  125. many = to;
  126. one = from;
  127. }
  128. function morphOneBatch(batch, fromIsMany, animateIndex, animateCount, forceManyOne) {
  129. var batchMany = batch.many;
  130. var batchOne = batch.one;
  131. if (batchMany.length === 1 && !forceManyOne) {
  132. // Is one to one
  133. var batchFrom = fromIsMany ? batchMany[0] : batchOne;
  134. var batchTo = fromIsMany ? batchOne : batchMany[0];
  135. if (isCombineMorphing(batchFrom)) {
  136. // Keep doing combine animation.
  137. morphOneBatch({
  138. many: [batchFrom],
  139. one: batchTo
  140. }, true, animateIndex, animateCount, true);
  141. } else {
  142. var individualAnimationCfg = animationDelay ? defaults({
  143. delay: animationDelay(animateIndex, animateCount)
  144. }, animationCfg) : animationCfg;
  145. morphPath(batchFrom, batchTo, individualAnimationCfg);
  146. animateOtherProps(batchFrom, batchTo, batchFrom, batchTo, individualAnimationCfg);
  147. }
  148. } else {
  149. var separateAnimationCfg = defaults({
  150. dividePath: pathDividers[divideShape],
  151. individualDelay: animationDelay && function (idx, count, fromPath, toPath) {
  152. return animationDelay(idx + animateIndex, animateCount);
  153. }
  154. }, animationCfg);
  155. var _a = fromIsMany ? combineMorph(batchMany, batchOne, separateAnimationCfg) : separateMorph(batchOne, batchMany, separateAnimationCfg),
  156. fromIndividuals = _a.fromIndividuals,
  157. toIndividuals = _a.toIndividuals;
  158. var count = fromIndividuals.length;
  159. for (var k = 0; k < count; k++) {
  160. var individualAnimationCfg = animationDelay ? defaults({
  161. delay: animationDelay(k, count)
  162. }, animationCfg) : animationCfg;
  163. animateOtherProps(fromIndividuals[k], toIndividuals[k], fromIsMany ? batchMany[k] : batch.one, fromIsMany ? batch.one : batchMany[k], individualAnimationCfg);
  164. }
  165. }
  166. }
  167. var fromIsMany = many ? many === from
  168. // Is one to one. If the path number not match. also needs do merge and separate morphing.
  169. : from.length > to.length;
  170. var morphBatches = many ? prepareMorphBatches(one, many) : prepareMorphBatches(fromIsMany ? to : from, [fromIsMany ? from : to]);
  171. var animateCount = 0;
  172. for (var i = 0; i < morphBatches.length; i++) {
  173. animateCount += morphBatches[i].many.length;
  174. }
  175. var animateIndex = 0;
  176. for (var i = 0; i < morphBatches.length; i++) {
  177. morphOneBatch(morphBatches[i], fromIsMany, animateIndex, animateCount);
  178. animateIndex += morphBatches[i].many.length;
  179. }
  180. }
  181. export function getPathList(elements) {
  182. if (!elements) {
  183. return [];
  184. }
  185. if (isArray(elements)) {
  186. var pathList_1 = [];
  187. for (var i = 0; i < elements.length; i++) {
  188. pathList_1.push(getPathList(elements[i]));
  189. }
  190. return pathList_1;
  191. }
  192. var pathList = [];
  193. elements.traverse(function (el) {
  194. if (el instanceof Path && !el.disableMorphing && !el.invisible && !el.ignore) {
  195. pathList.push(el);
  196. }
  197. });
  198. return pathList;
  199. }