utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transition = exports.transitionShape = exports.animate = exports.onAnimatesFinished = exports.onAnimateFinished = exports.parseAnimationOption = void 0;
  4. var tslib_1 = require("tslib");
  5. /* global Keyframe */
  6. var util_1 = require("@antv/util");
  7. var util_2 = require("../util");
  8. function isStandardAnimationOption(option) {
  9. if (typeof option === 'boolean')
  10. return false;
  11. return 'enter' in option && 'update' in option && 'exit' in option;
  12. }
  13. function parseAnimationOption(option) {
  14. // option is false => all animation is false
  15. // option is { enter: {}, update: {}, exit: {}, ...baseOption } =>
  16. // { enter: { ...enter, ...baseOption }, update: { ...update, ...baseOption }, exit: { ...exit, ...baseOption } }
  17. // option is { enter: {}, update: {}, exit: {} } => option
  18. if (!option)
  19. return { enter: false, update: false, exit: false };
  20. var keys = ['enter', 'update', 'exit'];
  21. var baseOption = Object.fromEntries(Object.entries(option).filter(function (_a) {
  22. var _b = tslib_1.__read(_a, 1), k = _b[0];
  23. return !keys.includes(k);
  24. }));
  25. return Object.fromEntries(keys.map(function (k) {
  26. if (isStandardAnimationOption(option)) {
  27. if (option[k] === false)
  28. return [k, false];
  29. return [k, tslib_1.__assign(tslib_1.__assign({}, option[k]), baseOption)];
  30. }
  31. return [k, baseOption];
  32. }));
  33. }
  34. exports.parseAnimationOption = parseAnimationOption;
  35. function onAnimateFinished(animation, callback) {
  36. if (!animation)
  37. callback();
  38. else
  39. animation.finished.then(callback);
  40. }
  41. exports.onAnimateFinished = onAnimateFinished;
  42. function onAnimatesFinished(animations, callback) {
  43. if (animations.length === 0)
  44. callback();
  45. else
  46. Promise.all(animations.map(function (a) { return a === null || a === void 0 ? void 0 : a.finished; })).then(callback);
  47. }
  48. exports.onAnimatesFinished = onAnimatesFinished;
  49. function attr(target, value) {
  50. if ('update' in target)
  51. target.update(value);
  52. else
  53. target.attr(value);
  54. }
  55. function animate(target, keyframes, options) {
  56. if (keyframes.length === 0)
  57. return null;
  58. if (!options) {
  59. var state = keyframes.slice(-1)[0];
  60. attr(target, { style: state });
  61. return null;
  62. }
  63. return target.animate(keyframes, options);
  64. }
  65. exports.animate = animate;
  66. /**
  67. * transition source shape to target shape
  68. * @param source
  69. * @param target
  70. * @param options
  71. * @param after destroy or hide source shape after transition
  72. */
  73. function transitionShape(source, target, options, after) {
  74. if (after === void 0) { after = 'destroy'; }
  75. var afterTransition = function () {
  76. if (after === 'destroy')
  77. source.destroy();
  78. else if (after === 'hide')
  79. (0, util_2.hide)(source);
  80. if (target.isVisible())
  81. (0, util_2.show)(target);
  82. };
  83. if (!options) {
  84. afterTransition();
  85. return [null];
  86. }
  87. var _a = options.duration, duration = _a === void 0 ? 0 : _a, _b = options.delay, delay = _b === void 0 ? 0 : _b;
  88. var middle = Math.ceil(+duration / 2);
  89. var offset = +duration / 4;
  90. var getPosition = function (shape) {
  91. if (shape.nodeName === 'circle') {
  92. var _a = tslib_1.__read(shape.getLocalPosition(), 2), cx = _a[0], cy = _a[1];
  93. var r = shape.attr('r');
  94. return [cx - r, cy - r];
  95. }
  96. return shape.getLocalPosition();
  97. };
  98. var _c = tslib_1.__read(getPosition(source), 2), sx = _c[0], sy = _c[1];
  99. var _d = tslib_1.__read(getPosition(target), 2), ex = _d[0], ey = _d[1];
  100. var _e = tslib_1.__read([(sx + ex) / 2 - sx, (sy + ey) / 2 - sy], 2), mx = _e[0], my = _e[1];
  101. var sourceAnimation = source.animate([
  102. { opacity: 1, transform: 'translate(0, 0)' },
  103. { opacity: 0, transform: "translate(".concat(mx, ", ").concat(my, ")") },
  104. ], tslib_1.__assign(tslib_1.__assign({ fill: 'both' }, options), { duration: delay + middle + offset }));
  105. var targetAnimation = target.animate([
  106. { opacity: 0, transform: "translate(".concat(-mx, ", ").concat(-my, ")"), offset: 0.01 },
  107. { opacity: 1, transform: 'translate(0, 0)' },
  108. ], tslib_1.__assign(tslib_1.__assign({ fill: 'both' }, options), { duration: middle + offset, delay: delay + middle - offset }));
  109. onAnimateFinished(targetAnimation, afterTransition);
  110. return [sourceAnimation, targetAnimation];
  111. }
  112. exports.transitionShape = transitionShape;
  113. /**
  114. * execute transition animation on element
  115. * @description in the current stage, only support the following properties:
  116. * x, y, width, height, opacity, fill, stroke, lineWidth, radius
  117. * @param target element to be animated
  118. * @param state target properties or element
  119. * @param options transition options
  120. * @param animate whether to animate
  121. * @returns transition instance
  122. */
  123. function transition(target, state, options) {
  124. var from = {};
  125. var to = {};
  126. Object.entries(state).forEach(function (_a) {
  127. var _b = tslib_1.__read(_a, 2), key = _b[0], tarStyle = _b[1];
  128. if (!(0, util_1.isNil)(tarStyle)) {
  129. // 关闭 CSS 解析后,attr / getAttribute 只能获取到用户显式传入的属性,此时可以
  130. // 获取解析值,如果仍获取不到(例如 x/y),则使用 0 作为默认值
  131. var currStyle = target.style[key] || target.parsedStyle[key] || 0; // x/y
  132. if (currStyle !== tarStyle) {
  133. from[key] = currStyle;
  134. to[key] = tarStyle;
  135. }
  136. }
  137. });
  138. if (!options) {
  139. attr(target, to);
  140. return null;
  141. }
  142. return animate(target, [from, to], tslib_1.__assign({ fill: 'both' }, options));
  143. }
  144. exports.transition = transition;
  145. //# sourceMappingURL=utils.js.map