bounds.js 4.4 KB

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