bounds.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { __read } from "tslib";
  2. import { degToRad, parseSeriesAttr, textOf } from '../../../util';
  3. var Bounds = /** @class */ (function () {
  4. function Bounds(x1, y1, x2, y2) {
  5. this.set(x1, y1, x2, y2);
  6. }
  7. Object.defineProperty(Bounds.prototype, "left", {
  8. get: function () {
  9. return this.x1;
  10. },
  11. enumerable: false,
  12. configurable: true
  13. });
  14. Object.defineProperty(Bounds.prototype, "top", {
  15. get: function () {
  16. return this.y1;
  17. },
  18. enumerable: false,
  19. configurable: true
  20. });
  21. Object.defineProperty(Bounds.prototype, "right", {
  22. get: function () {
  23. return this.x2;
  24. },
  25. enumerable: false,
  26. configurable: true
  27. });
  28. Object.defineProperty(Bounds.prototype, "bottom", {
  29. get: function () {
  30. return this.y2;
  31. },
  32. enumerable: false,
  33. configurable: true
  34. });
  35. Object.defineProperty(Bounds.prototype, "width", {
  36. get: function () {
  37. return this.defined('x2') && this.defined('x1') ? this.x2 - this.x1 : undefined;
  38. },
  39. enumerable: false,
  40. configurable: true
  41. });
  42. Object.defineProperty(Bounds.prototype, "height", {
  43. get: function () {
  44. return this.defined('y2') && this.defined('y1') ? this.y2 - this.y1 : undefined;
  45. },
  46. enumerable: false,
  47. configurable: true
  48. });
  49. Bounds.prototype.rotatedPoints = function (radian, x, y) {
  50. var _a = this, x1 = _a.x1, y1 = _a.y1, x2 = _a.x2, y2 = _a.y2;
  51. var cos = Math.cos(radian);
  52. var sin = Math.sin(radian);
  53. var cx = x - x * cos + y * sin;
  54. var cy = y - x * sin - y * cos;
  55. var points = [
  56. [cos * x1 - sin * y2 + cx, sin * x1 + cos * y2 + cy],
  57. [cos * x2 - sin * y2 + cx, sin * x2 + cos * y2 + cy],
  58. [cos * x1 - sin * y1 + cx, sin * x1 + cos * y1 + cy],
  59. [cos * x2 - sin * y1 + cx, sin * x2 + cos * y1 + cy],
  60. ];
  61. return points;
  62. };
  63. Bounds.prototype.set = function (x1, y1, x2, y2) {
  64. if (x2 < x1) {
  65. this.x2 = x1;
  66. this.x1 = x2;
  67. }
  68. else {
  69. this.x1 = x1;
  70. this.x2 = x2;
  71. }
  72. if (y2 < y1) {
  73. this.y2 = y1;
  74. this.y1 = y2;
  75. }
  76. else {
  77. this.y1 = y1;
  78. this.y2 = y2;
  79. }
  80. return this;
  81. };
  82. Bounds.prototype.defined = function (key) {
  83. return this[key] !== Number.MAX_VALUE && this[key] !== -Number.MAX_VALUE;
  84. };
  85. return Bounds;
  86. }());
  87. export { Bounds };
  88. export function getBounds(item, margin) {
  89. var angle = item.getEulerAngles() || 0;
  90. item.setEulerAngles(0);
  91. // get dimensions
  92. var _a = item.getLocalBounds(), _b = __read(_a.min, 2), x = _b[0], y = _b[1], _c = __read(_a.max, 2), right = _c[0], bottom = _c[1];
  93. var _d = getBBox(item), w = _d.width, h = _d.height;
  94. var height = h;
  95. var dx = 0;
  96. var dy = 0;
  97. var anchorX = x;
  98. var anchorY = y;
  99. var text = textOf(item);
  100. if (text) {
  101. // [to fix] 目前 G 上计算 bbox 有一点误差
  102. height -= 1.5;
  103. var a = text.style.textAlign;
  104. var b_1 = text.style.textBaseline;
  105. // horizontal alignment
  106. if (a === 'center') {
  107. anchorX = (x + right) / 2;
  108. }
  109. else if (a === 'right' || a === 'end') {
  110. anchorX = right;
  111. }
  112. else {
  113. // left by default, do nothing
  114. }
  115. // vertical alignment
  116. if (b_1 === 'middle') {
  117. anchorY = (y + bottom) / 2;
  118. }
  119. else if (b_1 === 'bottom') {
  120. anchorY = bottom;
  121. }
  122. }
  123. var _e = __read(parseSeriesAttr(margin), 4), _f = _e[0], t = _f === void 0 ? 0 : _f, _g = _e[1], r = _g === void 0 ? 0 : _g, _h = _e[2], b = _h === void 0 ? t : _h, _j = _e[3], l = _j === void 0 ? r : _j;
  124. var bounds = new Bounds((dx += x) - l, (dy += y) - t, dx + w + r, dy + height + b);
  125. item.setEulerAngles(angle);
  126. return bounds.rotatedPoints(degToRad(angle), anchorX, anchorY);
  127. }
  128. export function getBBox(shape) {
  129. // @ts-ignore
  130. if (shape.__bbox__)
  131. return shape.__bbox__;
  132. return shape.getBBox();
  133. }
  134. //# sourceMappingURL=bounds.js.map