util.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getTextPoint = exports.toPx = exports.updateClip = exports.getBBoxWithClip = exports.mergeBBox = exports.intersectBBox = exports.near = exports.wait = exports.distance = exports.getCirclePoint = exports.getValueByPercent = exports.createBBox = exports.pointsToBBox = exports.regionToBBox = exports.hasClass = exports.clearDom = exports.formatPadding = void 0;
  4. var util_1 = require("@antv/util");
  5. function formatPadding(padding) {
  6. var top = 0;
  7. var left = 0;
  8. var right = 0;
  9. var bottom = 0;
  10. if (util_1.isNumber(padding)) {
  11. top = left = right = bottom = padding;
  12. }
  13. else if (util_1.isArray(padding)) {
  14. top = padding[0];
  15. right = !util_1.isNil(padding[1]) ? padding[1] : padding[0];
  16. bottom = !util_1.isNil(padding[2]) ? padding[2] : padding[0];
  17. left = !util_1.isNil(padding[3]) ? padding[3] : right;
  18. }
  19. return [top, right, bottom, left];
  20. }
  21. exports.formatPadding = formatPadding;
  22. function clearDom(container) {
  23. var children = container.childNodes;
  24. var length = children.length;
  25. for (var i = length - 1; i >= 0; i--) {
  26. container.removeChild(children[i]);
  27. }
  28. }
  29. exports.clearDom = clearDom;
  30. function hasClass(elements, cName) {
  31. return !!elements.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
  32. }
  33. exports.hasClass = hasClass;
  34. function regionToBBox(region) {
  35. var start = region.start, end = region.end;
  36. var minX = Math.min(start.x, end.x);
  37. var minY = Math.min(start.y, end.y);
  38. var maxX = Math.max(start.x, end.x);
  39. var maxY = Math.max(start.y, end.y);
  40. return {
  41. x: minX,
  42. y: minY,
  43. minX: minX,
  44. minY: minY,
  45. maxX: maxX,
  46. maxY: maxY,
  47. width: maxX - minX,
  48. height: maxY - minY,
  49. };
  50. }
  51. exports.regionToBBox = regionToBBox;
  52. function pointsToBBox(points) {
  53. var xs = points.map(function (point) { return point.x; });
  54. var ys = points.map(function (point) { return point.y; });
  55. var minX = Math.min.apply(Math, xs);
  56. var minY = Math.min.apply(Math, ys);
  57. var maxX = Math.max.apply(Math, xs);
  58. var maxY = Math.max.apply(Math, ys);
  59. return {
  60. x: minX,
  61. y: minY,
  62. minX: minX,
  63. minY: minY,
  64. maxX: maxX,
  65. maxY: maxY,
  66. width: maxX - minX,
  67. height: maxY - minY,
  68. };
  69. }
  70. exports.pointsToBBox = pointsToBBox;
  71. function createBBox(x, y, width, height) {
  72. var maxX = x + width;
  73. var maxY = y + height;
  74. return {
  75. x: x,
  76. y: y,
  77. width: width,
  78. height: height,
  79. minX: x,
  80. minY: y,
  81. // 非常奇葩的 js 特性
  82. // Infinity + Infinity = Infinity
  83. // Infinity - Infinity = NaN
  84. // fixed https://github.com/antvis/G2Plot/issues/1243
  85. maxX: isNaN(maxX) ? 0 : maxX,
  86. maxY: isNaN(maxY) ? 0 : maxY,
  87. };
  88. }
  89. exports.createBBox = createBBox;
  90. function getValueByPercent(min, max, percent) {
  91. return (1 - percent) * min + max * percent;
  92. }
  93. exports.getValueByPercent = getValueByPercent;
  94. function getCirclePoint(center, radius, angle) {
  95. return {
  96. x: center.x + Math.cos(angle) * radius,
  97. y: center.y + Math.sin(angle) * radius,
  98. };
  99. }
  100. exports.getCirclePoint = getCirclePoint;
  101. function distance(p1, p2) {
  102. var dx = p2.x - p1.x;
  103. var dy = p2.y - p1.y;
  104. return Math.sqrt(dx * dx + dy * dy);
  105. }
  106. exports.distance = distance;
  107. exports.wait = function (interval) {
  108. return new Promise(function (resolve) {
  109. setTimeout(resolve, interval);
  110. });
  111. };
  112. /**
  113. * 判断两个数值 是否接近
  114. * - 解决精度问题(由于无法确定精度上限,根据具体场景可传入 精度 参数)
  115. */
  116. exports.near = function (x, y, e) {
  117. if (e === void 0) { e = Math.pow(Number.EPSILON, 0.5); }
  118. return [x, y].includes(Infinity) ? Math.abs(x) === Math.abs(y) : Math.abs(x - y) < e;
  119. };
  120. function intersectBBox(box1, box2) {
  121. var minX = Math.max(box1.minX, box2.minX);
  122. var minY = Math.max(box1.minY, box2.minY);
  123. var maxX = Math.min(box1.maxX, box2.maxX);
  124. var maxY = Math.min(box1.maxY, box2.maxY);
  125. return createBBox(minX, minY, maxX - minX, maxY - minY);
  126. }
  127. exports.intersectBBox = intersectBBox;
  128. function mergeBBox(box1, box2) {
  129. var minX = Math.min(box1.minX, box2.minX);
  130. var minY = Math.min(box1.minY, box2.minY);
  131. var maxX = Math.max(box1.maxX, box2.maxX);
  132. var maxY = Math.max(box1.maxY, box2.maxY);
  133. return createBBox(minX, minY, maxX - minX, maxY - minY);
  134. }
  135. exports.mergeBBox = mergeBBox;
  136. function getBBoxWithClip(element) {
  137. var clipShape = element.getClip();
  138. var clipBBox = clipShape && clipShape.getBBox();
  139. var bbox;
  140. if (!element.isGroup()) {
  141. // 如果是普通的图形
  142. bbox = element.getBBox();
  143. }
  144. else {
  145. var minX_1 = Infinity;
  146. var maxX_1 = -Infinity;
  147. var minY_1 = Infinity;
  148. var maxY_1 = -Infinity;
  149. var children = element.getChildren();
  150. if (children.length > 0) {
  151. util_1.each(children, function (child) {
  152. if (child.get('visible')) {
  153. // 如果分组没有子元素,则直接跳过
  154. if (child.isGroup() && child.get('children').length === 0) {
  155. return true;
  156. }
  157. var box = getBBoxWithClip(child);
  158. // 计算 4 个顶点
  159. var leftTop = child.applyToMatrix([box.minX, box.minY, 1]);
  160. var leftBottom = child.applyToMatrix([box.minX, box.maxY, 1]);
  161. var rightTop = child.applyToMatrix([box.maxX, box.minY, 1]);
  162. var rightBottom = child.applyToMatrix([box.maxX, box.maxY, 1]);
  163. // 从中取最小的范围
  164. var boxMinX = Math.min(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0]);
  165. var boxMaxX = Math.max(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0]);
  166. var boxMinY = Math.min(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1]);
  167. var boxMaxY = Math.max(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1]);
  168. if (boxMinX < minX_1) {
  169. minX_1 = boxMinX;
  170. }
  171. if (boxMaxX > maxX_1) {
  172. maxX_1 = boxMaxX;
  173. }
  174. if (boxMinY < minY_1) {
  175. minY_1 = boxMinY;
  176. }
  177. if (boxMaxY > maxY_1) {
  178. maxY_1 = boxMaxY;
  179. }
  180. }
  181. });
  182. }
  183. else {
  184. minX_1 = 0;
  185. maxX_1 = 0;
  186. minY_1 = 0;
  187. maxY_1 = 0;
  188. }
  189. bbox = createBBox(minX_1, minY_1, maxX_1 - minX_1, maxY_1 - minY_1);
  190. }
  191. if (clipBBox) {
  192. return intersectBBox(bbox, clipBBox);
  193. }
  194. else {
  195. return bbox;
  196. }
  197. }
  198. exports.getBBoxWithClip = getBBoxWithClip;
  199. function updateClip(element, newElement) {
  200. if (!element.getClip() && !newElement.getClip()) {
  201. // 两者都没有 clip
  202. return;
  203. }
  204. var newClipShape = newElement.getClip();
  205. if (!newClipShape) {
  206. // 新的 element 没有 clip
  207. element.setClip(null); // 移除 clip
  208. return;
  209. }
  210. var clipCfg = {
  211. type: newClipShape.get('type'),
  212. attrs: newClipShape.attr(),
  213. };
  214. element.setClip(clipCfg);
  215. }
  216. exports.updateClip = updateClip;
  217. function toPx(number) {
  218. return number + "px";
  219. }
  220. exports.toPx = toPx;
  221. function getTextPoint(start, end, position, offset) {
  222. var lineLength = distance(start, end);
  223. var offsetPercent = offset / lineLength; // 计算间距同线的比例,用于计算最终的位置
  224. var percent = 0;
  225. if (position === 'start') {
  226. percent = 0 - offsetPercent;
  227. }
  228. else if (position === 'end') {
  229. percent = 1 + offsetPercent;
  230. }
  231. return {
  232. x: getValueByPercent(start.x, end.x, percent),
  233. y: getValueByPercent(start.y, end.y, percent),
  234. };
  235. }
  236. exports.getTextPoint = getTextPoint;
  237. //# sourceMappingURL=util.js.map