Source: core/math/shapes/Rectangle.js

core/math/shapes/Rectangle.js

  1. import { SHAPES } from '../../const';
  2. /**
  3. * Rectangle object is an area defined by its position, as indicated by its top-left corner
  4. * point (x, y) and by its width and its height.
  5. *
  6. * @class
  7. * @memberof PIXI
  8. */
  9. export default class Rectangle
  10. {
  11. /**
  12. * @param {number} [x=0] - The X coordinate of the upper-left corner of the rectangle
  13. * @param {number} [y=0] - The Y coordinate of the upper-left corner of the rectangle
  14. * @param {number} [width=0] - The overall width of this rectangle
  15. * @param {number} [height=0] - The overall height of this rectangle
  16. */
  17. constructor(x = 0, y = 0, width = 0, height = 0)
  18. {
  19. /**
  20. * @member {number}
  21. * @default 0
  22. */
  23. this.x = Number(x);
  24. /**
  25. * @member {number}
  26. * @default 0
  27. */
  28. this.y = Number(y);
  29. /**
  30. * @member {number}
  31. * @default 0
  32. */
  33. this.width = Number(width);
  34. /**
  35. * @member {number}
  36. * @default 0
  37. */
  38. this.height = Number(height);
  39. /**
  40. * The type of the object, mainly used to avoid `instanceof` checks
  41. *
  42. * @member {number}
  43. * @readOnly
  44. * @default PIXI.SHAPES.RECT
  45. * @see PIXI.SHAPES
  46. */
  47. this.type = SHAPES.RECT;
  48. }
  49. /**
  50. * returns the left edge of the rectangle
  51. *
  52. * @member {number}
  53. */
  54. get left()
  55. {
  56. return this.x;
  57. }
  58. /**
  59. * returns the right edge of the rectangle
  60. *
  61. * @member {number}
  62. */
  63. get right()
  64. {
  65. return this.x + this.width;
  66. }
  67. /**
  68. * returns the top edge of the rectangle
  69. *
  70. * @member {number}
  71. */
  72. get top()
  73. {
  74. return this.y;
  75. }
  76. /**
  77. * returns the bottom edge of the rectangle
  78. *
  79. * @member {number}
  80. */
  81. get bottom()
  82. {
  83. return this.y + this.height;
  84. }
  85. /**
  86. * A constant empty rectangle.
  87. *
  88. * @static
  89. * @constant
  90. */
  91. static get EMPTY()
  92. {
  93. return new Rectangle(0, 0, 0, 0);
  94. }
  95. /**
  96. * Creates a clone of this Rectangle
  97. *
  98. * @return {PIXI.Rectangle} a copy of the rectangle
  99. */
  100. clone()
  101. {
  102. return new Rectangle(this.x, this.y, this.width, this.height);
  103. }
  104. /**
  105. * Copies another rectangle to this one.
  106. *
  107. * @param {PIXI.Rectangle} rectangle - The rectangle to copy.
  108. * @return {PIXI.Rectangle} Returns itself.
  109. */
  110. copy(rectangle)
  111. {
  112. this.x = rectangle.x;
  113. this.y = rectangle.y;
  114. this.width = rectangle.width;
  115. this.height = rectangle.height;
  116. return this;
  117. }
  118. /**
  119. * Checks whether the x and y coordinates given are contained within this Rectangle
  120. *
  121. * @param {number} x - The X coordinate of the point to test
  122. * @param {number} y - The Y coordinate of the point to test
  123. * @return {boolean} Whether the x/y coordinates are within this Rectangle
  124. */
  125. contains(x, y)
  126. {
  127. if (this.width <= 0 || this.height <= 0)
  128. {
  129. return false;
  130. }
  131. if (x >= this.x && x < this.x + this.width)
  132. {
  133. if (y >= this.y && y < this.y + this.height)
  134. {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * Pads the rectangle making it grow in all directions.
  142. *
  143. * @param {number} paddingX - The horizontal padding amount.
  144. * @param {number} [paddingY] - The vertical padding amount.
  145. */
  146. pad(paddingX, paddingY)
  147. {
  148. paddingX = paddingX || 0;
  149. paddingY = paddingY || ((paddingY !== 0) ? paddingX : 0);
  150. this.x -= paddingX;
  151. this.y -= paddingY;
  152. this.width += paddingX * 2;
  153. this.height += paddingY * 2;
  154. }
  155. /**
  156. * Fits this rectangle around the passed one.
  157. *
  158. * @param {PIXI.Rectangle} rectangle - The rectangle to fit.
  159. */
  160. fit(rectangle)
  161. {
  162. const x1 = Math.max(this.x, rectangle.x);
  163. const x2 = Math.min(this.x + this.width, rectangle.x + rectangle.width);
  164. const y1 = Math.max(this.y, rectangle.y);
  165. const y2 = Math.min(this.y + this.height, rectangle.y + rectangle.height);
  166. this.x = x1;
  167. this.width = Math.max(x2 - x1, 0);
  168. this.y = y1;
  169. this.height = Math.max(y2 - y1, 0);
  170. }
  171. /**
  172. * Enlarges this rectangle to include the passed rectangle.
  173. *
  174. * @param {PIXI.Rectangle} rectangle - The rectangle to include.
  175. */
  176. enlarge(rectangle)
  177. {
  178. const x1 = Math.min(this.x, rectangle.x);
  179. const x2 = Math.max(this.x + this.width, rectangle.x + rectangle.width);
  180. const y1 = Math.min(this.y, rectangle.y);
  181. const y2 = Math.max(this.y + this.height, rectangle.y + rectangle.height);
  182. this.x = x1;
  183. this.width = x2 - x1;
  184. this.y = y1;
  185. this.height = y2 - y1;
  186. }
  187. /**
  188. * Enlarges rectangle that way its corners lie on grid
  189. *
  190. * @param {number} [resolution=1] resolution
  191. * @param {number} [eps=0.001] precision
  192. */
  193. ceil(resolution = 1, eps = 0.001)
  194. {
  195. const x2 = Math.ceil((this.x + this.width - eps) * resolution) / resolution;
  196. const y2 = Math.ceil((this.y + this.height - eps) * resolution) / resolution;
  197. this.x = Math.floor((this.x + eps) * resolution) / resolution;
  198. this.y = Math.floor((this.y + eps) * resolution) / resolution;
  199. this.width = x2 - this.x;
  200. this.height = y2 - this.y;
  201. }
  202. }