Source: core/math/shapes/RoundedRectangle.js

core/math/shapes/RoundedRectangle.js

  1. import { SHAPES } from '../../const';
  2. /**
  3. * The Rounded Rectangle object is an area that has nice rounded corners, as indicated by its
  4. * top-left corner point (x, y) and by its width and its height and its radius.
  5. *
  6. * @class
  7. * @memberof PIXI
  8. */
  9. export default class RoundedRectangle
  10. {
  11. /**
  12. * @param {number} [x=0] - The X coordinate of the upper-left corner of the rounded rectangle
  13. * @param {number} [y=0] - The Y coordinate of the upper-left corner of the rounded rectangle
  14. * @param {number} [width=0] - The overall width of this rounded rectangle
  15. * @param {number} [height=0] - The overall height of this rounded rectangle
  16. * @param {number} [radius=20] - Controls the radius of the rounded corners
  17. */
  18. constructor(x = 0, y = 0, width = 0, height = 0, radius = 20)
  19. {
  20. /**
  21. * @member {number}
  22. * @default 0
  23. */
  24. this.x = x;
  25. /**
  26. * @member {number}
  27. * @default 0
  28. */
  29. this.y = y;
  30. /**
  31. * @member {number}
  32. * @default 0
  33. */
  34. this.width = width;
  35. /**
  36. * @member {number}
  37. * @default 0
  38. */
  39. this.height = height;
  40. /**
  41. * @member {number}
  42. * @default 20
  43. */
  44. this.radius = radius;
  45. /**
  46. * The type of the object, mainly used to avoid `instanceof` checks
  47. *
  48. * @member {number}
  49. * @readonly
  50. * @default PIXI.SHAPES.RREC
  51. * @see PIXI.SHAPES
  52. */
  53. this.type = SHAPES.RREC;
  54. }
  55. /**
  56. * Creates a clone of this Rounded Rectangle
  57. *
  58. * @return {PIXI.RoundedRectangle} a copy of the rounded rectangle
  59. */
  60. clone()
  61. {
  62. return new RoundedRectangle(this.x, this.y, this.width, this.height, this.radius);
  63. }
  64. /**
  65. * Checks whether the x and y coordinates given are contained within this Rounded Rectangle
  66. *
  67. * @param {number} x - The X coordinate of the point to test
  68. * @param {number} y - The Y coordinate of the point to test
  69. * @return {boolean} Whether the x/y coordinates are within this Rounded Rectangle
  70. */
  71. contains(x, y)
  72. {
  73. if (this.width <= 0 || this.height <= 0)
  74. {
  75. return false;
  76. }
  77. if (x >= this.x && x <= this.x + this.width)
  78. {
  79. if (y >= this.y && y <= this.y + this.height)
  80. {
  81. if ((y >= this.y + this.radius && y <= this.y + this.height - this.radius)
  82. || (x >= this.x + this.radius && x <= this.x + this.width - this.radius))
  83. {
  84. return true;
  85. }
  86. let dx = x - (this.x + this.radius);
  87. let dy = y - (this.y + this.radius);
  88. const radius2 = this.radius * this.radius;
  89. if ((dx * dx) + (dy * dy) <= radius2)
  90. {
  91. return true;
  92. }
  93. dx = x - (this.x + this.width - this.radius);
  94. if ((dx * dx) + (dy * dy) <= radius2)
  95. {
  96. return true;
  97. }
  98. dy = y - (this.y + this.height - this.radius);
  99. if ((dx * dx) + (dy * dy) <= radius2)
  100. {
  101. return true;
  102. }
  103. dx = x - (this.x + this.radius);
  104. if ((dx * dx) + (dy * dy) <= radius2)
  105. {
  106. return true;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. }