customGraphicTransition.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 { makeInner, normalizeToArray } from '../util/model.js';
  41. import { assert, bind, each, eqNaN, extend, hasOwn, indexOf, isArrayLike, keys, reduce } from 'zrender/lib/core/util.js';
  42. import { cloneValue } from 'zrender/lib/animation/Animator.js';
  43. import Displayable from 'zrender/lib/graphic/Displayable.js';
  44. import { getAnimationConfig } from './basicTransition.js';
  45. import { Path } from '../util/graphic.js';
  46. import { warn } from '../util/log.js';
  47. import { TRANSFORMABLE_PROPS } from 'zrender/lib/core/Transformable.js';
  48. var LEGACY_TRANSFORM_PROPS_MAP = {
  49. position: ['x', 'y'],
  50. scale: ['scaleX', 'scaleY'],
  51. origin: ['originX', 'originY']
  52. };
  53. var LEGACY_TRANSFORM_PROPS = keys(LEGACY_TRANSFORM_PROPS_MAP);
  54. var TRANSFORM_PROPS_MAP = reduce(TRANSFORMABLE_PROPS, function (obj, key) {
  55. obj[key] = 1;
  56. return obj;
  57. }, {});
  58. var transformPropNamesStr = TRANSFORMABLE_PROPS.join(', '); // '' means root
  59. export var ELEMENT_ANIMATABLE_PROPS = ['', 'style', 'shape', 'extra'];
  60. ;
  61. var transitionInnerStore = makeInner();
  62. ;
  63. function getElementAnimationConfig(animationType, el, elOption, parentModel, dataIndex) {
  64. var animationProp = animationType + "Animation";
  65. var config = getAnimationConfig(animationType, parentModel, dataIndex) || {};
  66. var userDuring = transitionInnerStore(el).userDuring; // Only set when duration is > 0 and it's need to be animated.
  67. if (config.duration > 0) {
  68. // For simplicity, if during not specified, the previous during will not work any more.
  69. config.during = userDuring ? bind(duringCall, {
  70. el: el,
  71. userDuring: userDuring
  72. }) : null;
  73. config.setToFinal = true;
  74. config.scope = animationType;
  75. }
  76. extend(config, elOption[animationProp]);
  77. return config;
  78. }
  79. export function applyUpdateTransition(el, elOption, animatableModel, opts) {
  80. opts = opts || {};
  81. var dataIndex = opts.dataIndex,
  82. isInit = opts.isInit,
  83. clearStyle = opts.clearStyle;
  84. var hasAnimation = animatableModel.isAnimationEnabled(); // Save the meta info for further morphing. Like apply on the sub morphing elements.
  85. var store = transitionInnerStore(el);
  86. var styleOpt = elOption.style;
  87. store.userDuring = elOption.during;
  88. var transFromProps = {};
  89. var propsToSet = {};
  90. prepareTransformAllPropsFinal(el, elOption, propsToSet);
  91. prepareShapeOrExtraAllPropsFinal('shape', elOption, propsToSet);
  92. prepareShapeOrExtraAllPropsFinal('extra', elOption, propsToSet);
  93. if (!isInit && hasAnimation) {
  94. prepareTransformTransitionFrom(el, elOption, transFromProps);
  95. prepareShapeOrExtraTransitionFrom('shape', el, elOption, transFromProps);
  96. prepareShapeOrExtraTransitionFrom('extra', el, elOption, transFromProps);
  97. prepareStyleTransitionFrom(el, elOption, styleOpt, transFromProps);
  98. }
  99. propsToSet.style = styleOpt;
  100. applyPropsDirectly(el, propsToSet, clearStyle);
  101. applyMiscProps(el, elOption);
  102. if (hasAnimation) {
  103. if (isInit) {
  104. var enterFromProps_1 = {};
  105. each(ELEMENT_ANIMATABLE_PROPS, function (propName) {
  106. var prop = propName ? elOption[propName] : elOption;
  107. if (prop && prop.enterFrom) {
  108. if (propName) {
  109. enterFromProps_1[propName] = enterFromProps_1[propName] || {};
  110. }
  111. extend(propName ? enterFromProps_1[propName] : enterFromProps_1, prop.enterFrom);
  112. }
  113. });
  114. var config = getElementAnimationConfig('enter', el, elOption, animatableModel, dataIndex);
  115. if (config.duration > 0) {
  116. el.animateFrom(enterFromProps_1, config);
  117. }
  118. } else {
  119. applyPropsTransition(el, elOption, dataIndex || 0, animatableModel, transFromProps);
  120. }
  121. } // Store leave to be used in leave transition.
  122. updateLeaveTo(el, elOption);
  123. styleOpt ? el.dirty() : el.markRedraw();
  124. }
  125. export function updateLeaveTo(el, elOption) {
  126. // Try merge to previous set leaveTo
  127. var leaveToProps = transitionInnerStore(el).leaveToProps;
  128. for (var i = 0; i < ELEMENT_ANIMATABLE_PROPS.length; i++) {
  129. var propName = ELEMENT_ANIMATABLE_PROPS[i];
  130. var prop = propName ? elOption[propName] : elOption;
  131. if (prop && prop.leaveTo) {
  132. if (!leaveToProps) {
  133. leaveToProps = transitionInnerStore(el).leaveToProps = {};
  134. }
  135. if (propName) {
  136. leaveToProps[propName] = leaveToProps[propName] || {};
  137. }
  138. extend(propName ? leaveToProps[propName] : leaveToProps, prop.leaveTo);
  139. }
  140. }
  141. }
  142. export function applyLeaveTransition(el, elOption, animatableModel, onRemove) {
  143. if (el) {
  144. var parent_1 = el.parent;
  145. var leaveToProps = transitionInnerStore(el).leaveToProps;
  146. if (leaveToProps) {
  147. // TODO TODO use leave after leaveAnimation in series is introduced
  148. // TODO Data index?
  149. var config = getElementAnimationConfig('update', el, elOption, animatableModel, 0);
  150. config.done = function () {
  151. parent_1.remove(el);
  152. onRemove && onRemove();
  153. };
  154. el.animateTo(leaveToProps, config);
  155. } else {
  156. parent_1.remove(el);
  157. onRemove && onRemove();
  158. }
  159. }
  160. }
  161. export function isTransitionAll(transition) {
  162. return transition === 'all';
  163. }
  164. function applyPropsDirectly(el, // Can be null/undefined
  165. allPropsFinal, clearStyle) {
  166. var styleOpt = allPropsFinal.style;
  167. if (!el.isGroup && styleOpt) {
  168. if (clearStyle) {
  169. el.useStyle({}); // When style object changed, how to trade the existing animation?
  170. // It is probably complicated and not needed to cover all the cases.
  171. // But still need consider the case:
  172. // (1) When using init animation on `style.opacity`, and before the animation
  173. // ended users triggers an update by mousewhel. At that time the init
  174. // animation should better be continued rather than terminated.
  175. // So after `useStyle` called, we should change the animation target manually
  176. // to continue the effect of the init animation.
  177. // (2) PENDING: If the previous animation targeted at a `val1`, and currently we need
  178. // to update the value to `val2` and no animation declared, should be terminate
  179. // the previous animation or just modify the target of the animation?
  180. // Therotically That will happen not only on `style` but also on `shape` and
  181. // `transfrom` props. But we haven't handle this case at present yet.
  182. // (3) PENDING: Is it proper to visit `animators` and `targetName`?
  183. var animators = el.animators;
  184. for (var i = 0; i < animators.length; i++) {
  185. var animator = animators[i]; // targetName is the "topKey".
  186. if (animator.targetName === 'style') {
  187. animator.changeTarget(el.style);
  188. }
  189. }
  190. }
  191. el.setStyle(styleOpt);
  192. }
  193. if (allPropsFinal) {
  194. // Not set style here.
  195. allPropsFinal.style = null; // Set el to the final state firstly.
  196. allPropsFinal && el.attr(allPropsFinal);
  197. allPropsFinal.style = styleOpt;
  198. }
  199. }
  200. function applyPropsTransition(el, elOption, dataIndex, model, // Can be null/undefined
  201. transFromProps) {
  202. if (transFromProps) {
  203. var config = getElementAnimationConfig('update', el, elOption, model, dataIndex);
  204. if (config.duration > 0) {
  205. el.animateFrom(transFromProps, config);
  206. }
  207. }
  208. }
  209. function applyMiscProps(el, elOption) {
  210. // Merge by default.
  211. hasOwn(elOption, 'silent') && (el.silent = elOption.silent);
  212. hasOwn(elOption, 'ignore') && (el.ignore = elOption.ignore);
  213. if (el instanceof Displayable) {
  214. hasOwn(elOption, 'invisible') && (el.invisible = elOption.invisible);
  215. }
  216. if (el instanceof Path) {
  217. hasOwn(elOption, 'autoBatch') && (el.autoBatch = elOption.autoBatch);
  218. }
  219. } // Use it to avoid it be exposed to user.
  220. var tmpDuringScope = {};
  221. var transitionDuringAPI = {
  222. // Usually other props do not need to be changed in animation during.
  223. setTransform: function (key, val) {
  224. if (process.env.NODE_ENV !== 'production') {
  225. assert(hasOwn(TRANSFORM_PROPS_MAP, key), 'Only ' + transformPropNamesStr + ' available in `setTransform`.');
  226. }
  227. tmpDuringScope.el[key] = val;
  228. return this;
  229. },
  230. getTransform: function (key) {
  231. if (process.env.NODE_ENV !== 'production') {
  232. assert(hasOwn(TRANSFORM_PROPS_MAP, key), 'Only ' + transformPropNamesStr + ' available in `getTransform`.');
  233. }
  234. return tmpDuringScope.el[key];
  235. },
  236. setShape: function (key, val) {
  237. if (process.env.NODE_ENV !== 'production') {
  238. assertNotReserved(key);
  239. }
  240. var el = tmpDuringScope.el;
  241. var shape = el.shape || (el.shape = {});
  242. shape[key] = val;
  243. el.dirtyShape && el.dirtyShape();
  244. return this;
  245. },
  246. getShape: function (key) {
  247. if (process.env.NODE_ENV !== 'production') {
  248. assertNotReserved(key);
  249. }
  250. var shape = tmpDuringScope.el.shape;
  251. if (shape) {
  252. return shape[key];
  253. }
  254. },
  255. setStyle: function (key, val) {
  256. if (process.env.NODE_ENV !== 'production') {
  257. assertNotReserved(key);
  258. }
  259. var el = tmpDuringScope.el;
  260. var style = el.style;
  261. if (style) {
  262. if (process.env.NODE_ENV !== 'production') {
  263. if (eqNaN(val)) {
  264. warn('style.' + key + ' must not be assigned with NaN.');
  265. }
  266. }
  267. style[key] = val;
  268. el.dirtyStyle && el.dirtyStyle();
  269. }
  270. return this;
  271. },
  272. getStyle: function (key) {
  273. if (process.env.NODE_ENV !== 'production') {
  274. assertNotReserved(key);
  275. }
  276. var style = tmpDuringScope.el.style;
  277. if (style) {
  278. return style[key];
  279. }
  280. },
  281. setExtra: function (key, val) {
  282. if (process.env.NODE_ENV !== 'production') {
  283. assertNotReserved(key);
  284. }
  285. var extra = tmpDuringScope.el.extra || (tmpDuringScope.el.extra = {});
  286. extra[key] = val;
  287. return this;
  288. },
  289. getExtra: function (key) {
  290. if (process.env.NODE_ENV !== 'production') {
  291. assertNotReserved(key);
  292. }
  293. var extra = tmpDuringScope.el.extra;
  294. if (extra) {
  295. return extra[key];
  296. }
  297. }
  298. };
  299. function assertNotReserved(key) {
  300. if (process.env.NODE_ENV !== 'production') {
  301. if (key === 'transition' || key === 'enterFrom' || key === 'leaveTo') {
  302. throw new Error('key must not be "' + key + '"');
  303. }
  304. }
  305. }
  306. function duringCall() {
  307. // Do not provide "percent" until some requirements come.
  308. // Because consider thies case:
  309. // enterFrom: {x: 100, y: 30}, transition: 'x'.
  310. // And enter duration is different from update duration.
  311. // Thus it might be confused about the meaning of "percent" in during callback.
  312. var scope = this;
  313. var el = scope.el;
  314. if (!el) {
  315. return;
  316. } // If el is remove from zr by reason like legend, during still need to called,
  317. // because el will be added back to zr and the prop value should not be incorrect.
  318. var latestUserDuring = transitionInnerStore(el).userDuring;
  319. var scopeUserDuring = scope.userDuring; // Ensured a during is only called once in each animation frame.
  320. // If a during is called multiple times in one frame, maybe some users' calculation logic
  321. // might be wrong (not sure whether this usage exists).
  322. // The case of a during might be called twice can be: by default there is a animator for
  323. // 'x', 'y' when init. Before the init animation finished, call `setOption` to start
  324. // another animators for 'style'/'shape'/'extra'.
  325. if (latestUserDuring !== scopeUserDuring) {
  326. // release
  327. scope.el = scope.userDuring = null;
  328. return;
  329. }
  330. tmpDuringScope.el = el; // Give no `this` to user in "during" calling.
  331. scopeUserDuring(transitionDuringAPI); // FIXME: if in future meet the case that some prop will be both modified in `during` and `state`,
  332. // consider the issue that the prop might be incorrect when return to "normal" state.
  333. }
  334. function prepareShapeOrExtraTransitionFrom(mainAttr, fromEl, elOption, transFromProps) {
  335. var attrOpt = elOption[mainAttr];
  336. if (!attrOpt) {
  337. return;
  338. }
  339. var elPropsInAttr = fromEl[mainAttr];
  340. var transFromPropsInAttr;
  341. if (elPropsInAttr) {
  342. var transition = elOption.transition;
  343. var attrTransition = attrOpt.transition;
  344. if (attrTransition) {
  345. !transFromPropsInAttr && (transFromPropsInAttr = transFromProps[mainAttr] = {});
  346. if (isTransitionAll(attrTransition)) {
  347. extend(transFromPropsInAttr, elPropsInAttr);
  348. } else {
  349. var transitionKeys = normalizeToArray(attrTransition);
  350. for (var i = 0; i < transitionKeys.length; i++) {
  351. var key = transitionKeys[i];
  352. var elVal = elPropsInAttr[key];
  353. transFromPropsInAttr[key] = elVal;
  354. }
  355. }
  356. } else if (isTransitionAll(transition) || indexOf(transition, mainAttr) >= 0) {
  357. !transFromPropsInAttr && (transFromPropsInAttr = transFromProps[mainAttr] = {});
  358. var elPropsInAttrKeys = keys(elPropsInAttr);
  359. for (var i = 0; i < elPropsInAttrKeys.length; i++) {
  360. var key = elPropsInAttrKeys[i];
  361. var elVal = elPropsInAttr[key];
  362. if (isNonStyleTransitionEnabled(attrOpt[key], elVal)) {
  363. transFromPropsInAttr[key] = elVal;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. function prepareShapeOrExtraAllPropsFinal(mainAttr, elOption, allProps) {
  370. var attrOpt = elOption[mainAttr];
  371. if (!attrOpt) {
  372. return;
  373. }
  374. var allPropsInAttr = allProps[mainAttr] = {};
  375. var keysInAttr = keys(attrOpt);
  376. for (var i = 0; i < keysInAttr.length; i++) {
  377. var key = keysInAttr[i]; // To avoid share one object with different element, and
  378. // to avoid user modify the object inexpectedly, have to clone.
  379. allPropsInAttr[key] = cloneValue(attrOpt[key]);
  380. }
  381. }
  382. function prepareTransformTransitionFrom(el, elOption, transFromProps) {
  383. var transition = elOption.transition;
  384. var transitionKeys = isTransitionAll(transition) ? TRANSFORMABLE_PROPS : normalizeToArray(transition || []);
  385. for (var i = 0; i < transitionKeys.length; i++) {
  386. var key = transitionKeys[i];
  387. if (key === 'style' || key === 'shape' || key === 'extra') {
  388. continue;
  389. }
  390. var elVal = el[key];
  391. if (process.env.NODE_ENV !== 'production') {
  392. checkTransformPropRefer(key, 'el.transition');
  393. } // Do not clone, animator will perform that clone.
  394. transFromProps[key] = elVal;
  395. }
  396. }
  397. function prepareTransformAllPropsFinal(el, elOption, allProps) {
  398. for (var i = 0; i < LEGACY_TRANSFORM_PROPS.length; i++) {
  399. var legacyName = LEGACY_TRANSFORM_PROPS[i];
  400. var xyName = LEGACY_TRANSFORM_PROPS_MAP[legacyName];
  401. var legacyArr = elOption[legacyName];
  402. if (legacyArr) {
  403. allProps[xyName[0]] = legacyArr[0];
  404. allProps[xyName[1]] = legacyArr[1];
  405. }
  406. }
  407. for (var i = 0; i < TRANSFORMABLE_PROPS.length; i++) {
  408. var key = TRANSFORMABLE_PROPS[i];
  409. if (elOption[key] != null) {
  410. allProps[key] = elOption[key];
  411. }
  412. }
  413. }
  414. function prepareStyleTransitionFrom(fromEl, elOption, styleOpt, transFromProps) {
  415. if (!styleOpt) {
  416. return;
  417. }
  418. var fromElStyle = fromEl.style;
  419. var transFromStyleProps;
  420. if (fromElStyle) {
  421. var styleTransition = styleOpt.transition;
  422. var elTransition = elOption.transition;
  423. if (styleTransition && !isTransitionAll(styleTransition)) {
  424. var transitionKeys = normalizeToArray(styleTransition);
  425. !transFromStyleProps && (transFromStyleProps = transFromProps.style = {});
  426. for (var i = 0; i < transitionKeys.length; i++) {
  427. var key = transitionKeys[i];
  428. var elVal = fromElStyle[key]; // Do not clone, see `checkNonStyleTansitionRefer`.
  429. transFromStyleProps[key] = elVal;
  430. }
  431. } else if (fromEl.getAnimationStyleProps && (isTransitionAll(elTransition) || isTransitionAll(styleTransition) || indexOf(elTransition, 'style') >= 0)) {
  432. var animationProps = fromEl.getAnimationStyleProps();
  433. var animationStyleProps = animationProps ? animationProps.style : null;
  434. if (animationStyleProps) {
  435. !transFromStyleProps && (transFromStyleProps = transFromProps.style = {});
  436. var styleKeys = keys(styleOpt);
  437. for (var i = 0; i < styleKeys.length; i++) {
  438. var key = styleKeys[i];
  439. if (animationStyleProps[key]) {
  440. var elVal = fromElStyle[key];
  441. transFromStyleProps[key] = elVal;
  442. }
  443. }
  444. }
  445. }
  446. }
  447. }
  448. function isNonStyleTransitionEnabled(optVal, elVal) {
  449. // The same as `checkNonStyleTansitionRefer`.
  450. return !isArrayLike(optVal) ? optVal != null && isFinite(optVal) : optVal !== elVal;
  451. }
  452. var checkTransformPropRefer;
  453. if (process.env.NODE_ENV !== 'production') {
  454. checkTransformPropRefer = function (key, usedIn) {
  455. if (!hasOwn(TRANSFORM_PROPS_MAP, key)) {
  456. warn('Prop `' + key + '` is not a permitted in `' + usedIn + '`. ' + 'Only `' + keys(TRANSFORM_PROPS_MAP).join('`, `') + '` are permitted.');
  457. }
  458. };
  459. }