Source: core/math/shapes/Ellipse.js

core/math/shapes/Ellipse.js

  1. import Rectangle from './Rectangle';
  2. import { SHAPES } from '../../const';
  3. /**
  4. * The Ellipse object can be used to specify a hit area for displayObjects
  5. *
  6. * @class
  7. * @memberof PIXI
  8. */
  9. export default class Ellipse
  10. {
  11. /**
  12. * @param {number} [x=0] - The X coordinate of the center of this ellipse
  13. * @param {number} [y=0] - The Y coordinate of the center of this ellipse
  14. * @param {number} [halfWidth=0] - The half width of this ellipse
  15. * @param {number} [halfHeight=0] - The half height of this ellipse
  16. */
  17. constructor(x = 0, y = 0, halfWidth = 0, halfHeight = 0)
  18. {
  19. /**
  20. * @member {number}
  21. * @default 0
  22. */
  23. this.x = x;
  24. /**
  25. * @member {number}
  26. * @default 0
  27. */
  28. this.y = y;
  29. /**
  30. * @member {number}
  31. * @default 0
  32. */
  33. this.width = halfWidth;
  34. /**
  35. * @member {number}
  36. * @default 0
  37. */
  38. this.height = halfHeight;
  39. /**
  40. * The type of the object, mainly used to avoid `instanceof` checks
  41. *
  42. * @member {number}
  43. * @readOnly
  44. * @default PIXI.SHAPES.ELIP
  45. * @see PIXI.SHAPES
  46. */
  47. this.type = SHAPES.ELIP;
  48. }
  49. /**
  50. * Creates a clone of this Ellipse instance
  51. *
  52. * @return {PIXI.Ellipse} a copy of the ellipse
  53. */
  54. clone()
  55. {
  56. return new Ellipse(this.x, this.y, this.width, this.height);
  57. }
  58. /**
  59. * Checks whether the x and y coordinates given are contained within this ellipse
  60. *
  61. * @param {number} x - The X coordinate of the point to test
  62. * @param {number} y - The Y coordinate of the point to test
  63. * @return {boolean} Whether the x/y coords are within this ellipse
  64. */
  65. contains(x, y)
  66. {
  67. if (this.width <= 0 || this.height <= 0)
  68. {
  69. return false;
  70. }
  71. // normalize the coords to an ellipse with center 0,0
  72. let normx = ((x - this.x) / this.width);
  73. let normy = ((y - this.y) / this.height);
  74. normx *= normx;
  75. normy *= normy;
  76. return (normx + normy <= 1);
  77. }
  78. /**
  79. * Returns the framing rectangle of the ellipse as a Rectangle object
  80. *
  81. * @return {PIXI.Rectangle} the framing rectangle
  82. */
  83. getBounds()
  84. {
  85. return new Rectangle(this.x - this.width, this.y - this.height, this.width, this.height);
  86. }
  87. }