morphTransitionHelper.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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; // If one has more paths than each one of many. average them.
  65. for (var i = batchCount - 1; i >= 0; i--) {
  66. if (!batches[i].many.length) {
  67. var moveFrom = batches[off].many;
  68. if (moveFrom.length <= 1) {
  69. // Not enough
  70. // Start from the first one.
  71. if (off) {
  72. off = 0;
  73. } else {
  74. return batches;
  75. }
  76. }
  77. var len = moveFrom.length;
  78. var mid = Math.ceil(len / 2);
  79. batches[i].many = moveFrom.slice(mid, len);
  80. batches[off].many = moveFrom.slice(0, mid);
  81. off++;
  82. }
  83. }
  84. return batches;
  85. }
  86. var pathDividers = {
  87. clone: function (params) {
  88. var ret = []; // Fitting the alpha
  89. var approxOpacity = 1 - Math.pow(1 - params.path.style.opacity, 1 / params.count);
  90. for (var i = 0; i < params.count; i++) {
  91. var cloned = clonePath(params.path);
  92. cloned.setStyle('opacity', approxOpacity);
  93. ret.push(cloned);
  94. }
  95. return ret;
  96. },
  97. // Use the default divider
  98. split: null
  99. };
  100. export function applyMorphAnimation(from, to, divideShape, seriesModel, dataIndex, animateOtherProps) {
  101. if (!from.length || !to.length) {
  102. return;
  103. }
  104. var updateAnimationCfg = getAnimationConfig('update', seriesModel, dataIndex);
  105. if (!(updateAnimationCfg && updateAnimationCfg.duration > 0)) {
  106. return;
  107. }
  108. var animationDelay = seriesModel.getModel('universalTransition').get('delay');
  109. var animationCfg = Object.assign({
  110. // Need to setToFinal so the further calculation based on the style can be correct.
  111. // Like emphasis color.
  112. setToFinal: true
  113. }, updateAnimationCfg);
  114. var many;
  115. var one;
  116. if (isMultiple(from)) {
  117. // manyToOne
  118. many = from;
  119. one = to;
  120. }
  121. if (isMultiple(to)) {
  122. // oneToMany
  123. many = to;
  124. one = from;
  125. }
  126. function morphOneBatch(batch, fromIsMany, animateIndex, animateCount, forceManyOne) {
  127. var batchMany = batch.many;
  128. var batchOne = batch.one;
  129. if (batchMany.length === 1 && !forceManyOne) {
  130. // Is one to one
  131. var batchFrom = fromIsMany ? batchMany[0] : batchOne;
  132. var batchTo = fromIsMany ? batchOne : batchMany[0];
  133. if (isCombineMorphing(batchFrom)) {
  134. // Keep doing combine animation.
  135. morphOneBatch({
  136. many: [batchFrom],
  137. one: batchTo
  138. }, true, animateIndex, animateCount, true);
  139. } else {
  140. var individualAnimationCfg = animationDelay ? defaults({
  141. delay: animationDelay(animateIndex, animateCount)
  142. }, animationCfg) : animationCfg;
  143. morphPath(batchFrom, batchTo, individualAnimationCfg);
  144. animateOtherProps(batchFrom, batchTo, batchFrom, batchTo, individualAnimationCfg);
  145. }
  146. } else {
  147. var separateAnimationCfg = defaults({
  148. dividePath: pathDividers[divideShape],
  149. individualDelay: animationDelay && function (idx, count, fromPath, toPath) {
  150. return animationDelay(idx + animateIndex, animateCount);
  151. }
  152. }, animationCfg);
  153. var _a = fromIsMany ? combineMorph(batchMany, batchOne, separateAnimationCfg) : separateMorph(batchOne, batchMany, separateAnimationCfg),
  154. fromIndividuals = _a.fromIndividuals,
  155. toIndividuals = _a.toIndividuals;
  156. var count = fromIndividuals.length;
  157. for (var k = 0; k < count; k++) {
  158. var individualAnimationCfg = animationDelay ? defaults({
  159. delay: animationDelay(k, count)
  160. }, animationCfg) : animationCfg;
  161. animateOtherProps(fromIndividuals[k], toIndividuals[k], fromIsMany ? batchMany[k] : batch.one, fromIsMany ? batch.one : batchMany[k], individualAnimationCfg);
  162. }
  163. }
  164. }
  165. var fromIsMany = many ? many === from // Is one to one. If the path number not match. also needs do merge and separate morphing.
  166. : from.length > to.length;
  167. var morphBatches = many ? prepareMorphBatches(one, many) : prepareMorphBatches(fromIsMany ? to : from, [fromIsMany ? from : to]);
  168. var animateCount = 0;
  169. for (var i = 0; i < morphBatches.length; i++) {
  170. animateCount += morphBatches[i].many.length;
  171. }
  172. var animateIndex = 0;
  173. for (var i = 0; i < morphBatches.length; i++) {
  174. morphOneBatch(morphBatches[i], fromIsMany, animateIndex, animateCount);
  175. animateIndex += morphBatches[i].many.length;
  176. }
  177. }
  178. export function getPathList(elements) {
  179. if (!elements) {
  180. return [];
  181. }
  182. if (isArray(elements)) {
  183. var pathList_1 = [];
  184. for (var i = 0; i < elements.length; i++) {
  185. pathList_1.push(getPathList(elements[i]));
  186. }
  187. return pathList_1;
  188. }
  189. var pathList = [];
  190. elements.traverse(function (el) {
  191. if (el instanceof Path && !el.disableMorphing && !el.invisible && !el.ignore) {
  192. pathList.push(el);
  193. }
  194. });
  195. return pathList;
  196. }