customGraphicTransition.js 18 KB

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