Box.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { isDimensions, isValidNumber } from '../utils';
  2. import { Point } from './Point';
  3. var Box = /** @class */ (function () {
  4. function Box(_box, allowNegativeDimensions) {
  5. if (allowNegativeDimensions === void 0) { allowNegativeDimensions = true; }
  6. var box = (_box || {});
  7. var isBbox = [box.left, box.top, box.right, box.bottom].every(isValidNumber);
  8. var isRect = [box.x, box.y, box.width, box.height].every(isValidNumber);
  9. if (!isRect && !isBbox) {
  10. throw new Error("Box.constructor - expected box to be IBoundingBox | IRect, instead have " + JSON.stringify(box));
  11. }
  12. var _a = isRect
  13. ? [box.x, box.y, box.width, box.height]
  14. : [box.left, box.top, box.right - box.left, box.bottom - box.top], x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  15. Box.assertIsValidBox({ x: x, y: y, width: width, height: height }, 'Box.constructor', allowNegativeDimensions);
  16. this._x = x;
  17. this._y = y;
  18. this._width = width;
  19. this._height = height;
  20. }
  21. Box.isRect = function (rect) {
  22. return !!rect && [rect.x, rect.y, rect.width, rect.height].every(isValidNumber);
  23. };
  24. Box.assertIsValidBox = function (box, callee, allowNegativeDimensions) {
  25. if (allowNegativeDimensions === void 0) { allowNegativeDimensions = false; }
  26. if (!Box.isRect(box)) {
  27. throw new Error(callee + " - invalid box: " + JSON.stringify(box) + ", expected object with properties x, y, width, height");
  28. }
  29. if (!allowNegativeDimensions && (box.width < 0 || box.height < 0)) {
  30. throw new Error(callee + " - width (" + box.width + ") and height (" + box.height + ") must be positive numbers");
  31. }
  32. };
  33. Object.defineProperty(Box.prototype, "x", {
  34. get: function () { return this._x; },
  35. enumerable: true,
  36. configurable: true
  37. });
  38. Object.defineProperty(Box.prototype, "y", {
  39. get: function () { return this._y; },
  40. enumerable: true,
  41. configurable: true
  42. });
  43. Object.defineProperty(Box.prototype, "width", {
  44. get: function () { return this._width; },
  45. enumerable: true,
  46. configurable: true
  47. });
  48. Object.defineProperty(Box.prototype, "height", {
  49. get: function () { return this._height; },
  50. enumerable: true,
  51. configurable: true
  52. });
  53. Object.defineProperty(Box.prototype, "left", {
  54. get: function () { return this.x; },
  55. enumerable: true,
  56. configurable: true
  57. });
  58. Object.defineProperty(Box.prototype, "top", {
  59. get: function () { return this.y; },
  60. enumerable: true,
  61. configurable: true
  62. });
  63. Object.defineProperty(Box.prototype, "right", {
  64. get: function () { return this.x + this.width; },
  65. enumerable: true,
  66. configurable: true
  67. });
  68. Object.defineProperty(Box.prototype, "bottom", {
  69. get: function () { return this.y + this.height; },
  70. enumerable: true,
  71. configurable: true
  72. });
  73. Object.defineProperty(Box.prototype, "area", {
  74. get: function () { return this.width * this.height; },
  75. enumerable: true,
  76. configurable: true
  77. });
  78. Object.defineProperty(Box.prototype, "topLeft", {
  79. get: function () { return new Point(this.left, this.top); },
  80. enumerable: true,
  81. configurable: true
  82. });
  83. Object.defineProperty(Box.prototype, "topRight", {
  84. get: function () { return new Point(this.right, this.top); },
  85. enumerable: true,
  86. configurable: true
  87. });
  88. Object.defineProperty(Box.prototype, "bottomLeft", {
  89. get: function () { return new Point(this.left, this.bottom); },
  90. enumerable: true,
  91. configurable: true
  92. });
  93. Object.defineProperty(Box.prototype, "bottomRight", {
  94. get: function () { return new Point(this.right, this.bottom); },
  95. enumerable: true,
  96. configurable: true
  97. });
  98. Box.prototype.round = function () {
  99. var _a = [this.x, this.y, this.width, this.height]
  100. .map(function (val) { return Math.round(val); }), x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  101. return new Box({ x: x, y: y, width: width, height: height });
  102. };
  103. Box.prototype.floor = function () {
  104. var _a = [this.x, this.y, this.width, this.height]
  105. .map(function (val) { return Math.floor(val); }), x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  106. return new Box({ x: x, y: y, width: width, height: height });
  107. };
  108. Box.prototype.toSquare = function () {
  109. var _a = this, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
  110. var diff = Math.abs(width - height);
  111. if (width < height) {
  112. x -= (diff / 2);
  113. width += diff;
  114. }
  115. if (height < width) {
  116. y -= (diff / 2);
  117. height += diff;
  118. }
  119. return new Box({ x: x, y: y, width: width, height: height });
  120. };
  121. Box.prototype.rescale = function (s) {
  122. var scaleX = isDimensions(s) ? s.width : s;
  123. var scaleY = isDimensions(s) ? s.height : s;
  124. return new Box({
  125. x: this.x * scaleX,
  126. y: this.y * scaleY,
  127. width: this.width * scaleX,
  128. height: this.height * scaleY
  129. });
  130. };
  131. Box.prototype.pad = function (padX, padY) {
  132. var _a = [
  133. this.x - (padX / 2),
  134. this.y - (padY / 2),
  135. this.width + padX,
  136. this.height + padY
  137. ], x = _a[0], y = _a[1], width = _a[2], height = _a[3];
  138. return new Box({ x: x, y: y, width: width, height: height });
  139. };
  140. Box.prototype.clipAtImageBorders = function (imgWidth, imgHeight) {
  141. var _a = this, x = _a.x, y = _a.y, right = _a.right, bottom = _a.bottom;
  142. var clippedX = Math.max(x, 0);
  143. var clippedY = Math.max(y, 0);
  144. var newWidth = right - clippedX;
  145. var newHeight = bottom - clippedY;
  146. var clippedWidth = Math.min(newWidth, imgWidth - clippedX);
  147. var clippedHeight = Math.min(newHeight, imgHeight - clippedY);
  148. return (new Box({ x: clippedX, y: clippedY, width: clippedWidth, height: clippedHeight })).floor();
  149. };
  150. Box.prototype.shift = function (sx, sy) {
  151. var _a = this, width = _a.width, height = _a.height;
  152. var x = this.x + sx;
  153. var y = this.y + sy;
  154. return new Box({ x: x, y: y, width: width, height: height });
  155. };
  156. Box.prototype.padAtBorders = function (imageHeight, imageWidth) {
  157. var w = this.width + 1;
  158. var h = this.height + 1;
  159. var dx = 1;
  160. var dy = 1;
  161. var edx = w;
  162. var edy = h;
  163. var x = this.left;
  164. var y = this.top;
  165. var ex = this.right;
  166. var ey = this.bottom;
  167. if (ex > imageWidth) {
  168. edx = -ex + imageWidth + w;
  169. ex = imageWidth;
  170. }
  171. if (ey > imageHeight) {
  172. edy = -ey + imageHeight + h;
  173. ey = imageHeight;
  174. }
  175. if (x < 1) {
  176. edy = 2 - x;
  177. x = 1;
  178. }
  179. if (y < 1) {
  180. edy = 2 - y;
  181. y = 1;
  182. }
  183. return { dy: dy, edy: edy, dx: dx, edx: edx, y: y, ey: ey, x: x, ex: ex, w: w, h: h };
  184. };
  185. Box.prototype.calibrate = function (region) {
  186. return new Box({
  187. left: this.left + (region.left * this.width),
  188. top: this.top + (region.top * this.height),
  189. right: this.right + (region.right * this.width),
  190. bottom: this.bottom + (region.bottom * this.height)
  191. }).toSquare().round();
  192. };
  193. return Box;
  194. }());
  195. export { Box };
  196. //# sourceMappingURL=Box.js.map