rectangle.d.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { Line } from './line';
  2. import { Point } from './point';
  3. import { Geometry } from './geometry';
  4. import { Ellipse } from './ellipse';
  5. export declare class Rectangle extends Geometry implements Rectangle.RectangleLike {
  6. x: number;
  7. y: number;
  8. width: number;
  9. height: number;
  10. get left(): number;
  11. get top(): number;
  12. get right(): number;
  13. get bottom(): number;
  14. get origin(): Point;
  15. get topLeft(): Point;
  16. get topCenter(): Point;
  17. get topRight(): Point;
  18. get center(): Point;
  19. get bottomLeft(): Point;
  20. get bottomCenter(): Point;
  21. get bottomRight(): Point;
  22. get corner(): Point;
  23. get rightMiddle(): Point;
  24. get leftMiddle(): Point;
  25. get topLine(): Line;
  26. get rightLine(): Line;
  27. get bottomLine(): Line;
  28. get leftLine(): Line;
  29. constructor(x?: number, y?: number, width?: number, height?: number);
  30. getOrigin(): Point;
  31. getTopLeft(): Point;
  32. getTopCenter(): Point;
  33. getTopRight(): Point;
  34. getCenter(): Point;
  35. getCenterX(): number;
  36. getCenterY(): number;
  37. getBottomLeft(): Point;
  38. getBottomCenter(): Point;
  39. getBottomRight(): Point;
  40. getCorner(): Point;
  41. getRightMiddle(): Point;
  42. getLeftMiddle(): Point;
  43. getTopLine(): Line;
  44. getRightLine(): Line;
  45. getBottomLine(): Line;
  46. getLeftLine(): Line;
  47. /**
  48. * Returns a rectangle that is the bounding box of the rectangle.
  49. *
  50. * If `angle` is specified, the bounding box calculation will take into
  51. * account the rotation of the rectangle by angle degrees around its center.
  52. */
  53. bbox(angle?: number): Rectangle;
  54. round(precision?: number): this;
  55. add(x: number, y: number, width: number, height: number): this;
  56. add(rect: Rectangle.RectangleLike | Rectangle.RectangleData): this;
  57. update(x: number, y: number, width: number, height: number): this;
  58. update(rect: Rectangle.RectangleLike | Rectangle.RectangleData): this;
  59. inflate(amount: number): this;
  60. /**
  61. * Returns a rectangle inflated in axis-x by `2*dx` and in axis-y by `2*dy`.
  62. */
  63. inflate(dx: number, dy: number): this;
  64. /**
  65. * Adjust the position and dimensions of the rectangle such that its edges
  66. * are on the nearest increment of `gx` on the x-axis and `gy` on the y-axis.
  67. */
  68. snapToGrid(gridSize: number): this;
  69. snapToGrid(gx: number, gy: number): this;
  70. snapToGrid(gx: number, gy?: number): this;
  71. translate(tx: number, ty: number): this;
  72. translate(p: Point.PointLike | Point.PointData): this;
  73. scale(sx: number, sy: number, origin?: Point.PointLike | Point.PointData): this;
  74. rotate(degree: number, center?: Point.PointLike | Point.PointData): this;
  75. rotate90(): this;
  76. /**
  77. * Translates the rectangle by `rect.x` and `rect.y` and expand it by
  78. * `rect.width` and `rect.height`.
  79. */
  80. moveAndExpand(rect: Rectangle.RectangleLike | Rectangle.RectangleData): this;
  81. /**
  82. * Returns an object where `sx` and `sy` give the maximum scaling that can be
  83. * applied to the rectangle so that it would still fit into `limit`. If
  84. * `origin` is specified, the rectangle is scaled around it; otherwise, it is
  85. * scaled around its center.
  86. */
  87. getMaxScaleToFit(limit: Rectangle.RectangleLike | Rectangle.RectangleData, origin?: Point): {
  88. sx: number;
  89. sy: number;
  90. };
  91. /**
  92. * Returns a number that specifies the maximum scaling that can be applied to
  93. * the rectangle along both axes so that it would still fit into `limit`. If
  94. * `origin` is specified, the rectangle is scaled around it; otherwise, it is
  95. * scaled around its center.
  96. */
  97. getMaxUniformScaleToFit(limit: Rectangle.RectangleLike | Rectangle.RectangleData, origin?: Point): number;
  98. /**
  99. * Returns `true` if the point is inside the rectangle (inclusive).
  100. * Returns `false` otherwise.
  101. */
  102. containsPoint(x: number, y: number): boolean;
  103. containsPoint(point: Point.PointLike | Point.PointData): boolean;
  104. /**
  105. * Returns `true` if the rectangle is (completely) inside the
  106. * rectangle (inclusive). Returns `false` otherwise.
  107. */
  108. containsRect(x: number, y: number, w: number, h: number): boolean;
  109. containsRect(rect: Rectangle.RectangleLike | Rectangle.RectangleData): boolean;
  110. /**
  111. * Returns an array of the intersection points of the rectangle and the line.
  112. * Return `null` if no intersection exists.
  113. */
  114. intersectsWithLine(line: Line): Point[] | null;
  115. /**
  116. * Returns the point on the boundary of the rectangle that is the intersection
  117. * of the rectangle with a line starting in the center the rectangle ending in
  118. * the point `p`.
  119. *
  120. * If `angle` is specified, the intersection will take into account the
  121. * rotation of the rectangle by `angle` degrees around its center.
  122. */
  123. intersectsWithLineFromCenterToPoint(p: Point.PointLike | Point.PointData, angle?: number): Point | null;
  124. /**
  125. * Returns a rectangle that is a subtraction of the two rectangles if such an
  126. * object exists (the two rectangles intersect). Returns `null` otherwise.
  127. */
  128. intersectsWithRect(x: number, y: number, w: number, h: number): Rectangle | null;
  129. intersectsWithRect(rect: Rectangle.RectangleLike | Rectangle.RectangleData): Rectangle | null;
  130. isIntersectWithRect(x: number, y: number, w: number, h: number): boolean;
  131. isIntersectWithRect(rect: Rectangle.RectangleLike | Rectangle.RectangleData): boolean;
  132. /**
  133. * Normalize the rectangle, i.e. make it so that it has non-negative
  134. * width and height. If width is less than `0`, the function swaps left and
  135. * right corners and if height is less than `0`, the top and bottom corners
  136. * are swapped.
  137. */
  138. normalize(): this;
  139. /**
  140. * Returns a rectangle that is a union of this rectangle and rectangle `rect`.
  141. */
  142. union(rect: Rectangle.RectangleLike | Rectangle.RectangleData): Rectangle;
  143. /**
  144. * Returns a string ("top", "left", "right" or "bottom") denoting the side of
  145. * the rectangle which is nearest to the point `p`.
  146. */
  147. getNearestSideToPoint(p: Point.PointLike | Point.PointData): Rectangle.Side;
  148. /**
  149. * Returns a point on the boundary of the rectangle nearest to the point `p`.
  150. */
  151. getNearestPointToPoint(p: Point.PointLike | Point.PointData): Point;
  152. equals(rect: Rectangle.RectangleLike): boolean;
  153. clone(): Rectangle;
  154. toJSON(): {
  155. x: number;
  156. y: number;
  157. width: number;
  158. height: number;
  159. };
  160. serialize(): string;
  161. }
  162. export declare namespace Rectangle {
  163. function isRectangle(instance: any): instance is Rectangle;
  164. }
  165. export declare namespace Rectangle {
  166. type RectangleData = [number, number, number, number];
  167. interface RectangleLike extends Point.PointLike {
  168. x: number;
  169. y: number;
  170. width: number;
  171. height: number;
  172. }
  173. function isRectangleLike(o: any): o is RectangleLike;
  174. type Side = 'left' | 'right' | 'top' | 'bottom';
  175. type KeyPoint = 'center' | 'origin' | 'corner' | 'topLeft' | 'topCenter' | 'topRight' | 'bottomLeft' | 'bottomCenter' | 'bottomRight' | 'rightMiddle' | 'leftMiddle';
  176. }
  177. export declare namespace Rectangle {
  178. export function create(rect: RectangleLike | RectangleData): Rectangle;
  179. export function create(x?: number, y?: number, width?: number, height?: number): Rectangle;
  180. export function create(x?: number | RectangleLike | RectangleData, y?: number, width?: number, height?: number): Rectangle;
  181. export function clone(rect: RectangleLike | RectangleData): Rectangle;
  182. /**
  183. * Returns a new rectangle from the given ellipse.
  184. */
  185. export function fromEllipse(ellipse: Ellipse): Rectangle;
  186. interface Size {
  187. width: number;
  188. height: number;
  189. }
  190. export function fromSize(size: Size): Rectangle;
  191. export function fromPositionAndSize(pos: Point.PointLike, size: Size): Rectangle;
  192. export {};
  193. }