Source: core/math/ObservablePoint.js

core/math/ObservablePoint.js

  1. /**
  2. * The Point object represents a location in a two-dimensional coordinate system, where x represents
  3. * the horizontal axis and y represents the vertical axis.
  4. * An observable point is a point that triggers a callback when the point's position is changed.
  5. *
  6. * @class
  7. * @memberof PIXI
  8. */
  9. export default class ObservablePoint
  10. {
  11. /**
  12. * @param {Function} cb - callback when changed
  13. * @param {object} scope - owner of callback
  14. * @param {number} [x=0] - position of the point on the x axis
  15. * @param {number} [y=0] - position of the point on the y axis
  16. */
  17. constructor(cb, scope, x = 0, y = 0)
  18. {
  19. this._x = x;
  20. this._y = y;
  21. this.cb = cb;
  22. this.scope = scope;
  23. }
  24. /**
  25. * Creates a clone of this point.
  26. * The callback and scope params can be overidden otherwise they will default
  27. * to the clone object's values.
  28. *
  29. * @override
  30. * @param {Function} [cb=null] - callback when changed
  31. * @param {object} [scope=null] - owner of callback
  32. * @return {PIXI.ObservablePoint} a copy of the point
  33. */
  34. clone(cb = null, scope = null)
  35. {
  36. const _cb = cb || this.cb;
  37. const _scope = scope || this.scope;
  38. return new ObservablePoint(_cb, _scope, this._x, this._y);
  39. }
  40. /**
  41. * Sets the point to a new x and y position.
  42. * If y is omitted, both x and y will be set to x.
  43. *
  44. * @param {number} [x=0] - position of the point on the x axis
  45. * @param {number} [y=0] - position of the point on the y axis
  46. */
  47. set(x, y)
  48. {
  49. const _x = x || 0;
  50. const _y = y || ((y !== 0) ? _x : 0);
  51. if (this._x !== _x || this._y !== _y)
  52. {
  53. this._x = _x;
  54. this._y = _y;
  55. this.cb.call(this.scope);
  56. }
  57. }
  58. /**
  59. * Copies the data from another point
  60. *
  61. * @param {PIXI.Point|PIXI.ObservablePoint} point - point to copy from
  62. */
  63. copy(point)
  64. {
  65. if (this._x !== point.x || this._y !== point.y)
  66. {
  67. this._x = point.x;
  68. this._y = point.y;
  69. this.cb.call(this.scope);
  70. }
  71. }
  72. /**
  73. * Returns true if the given point is equal to this point
  74. *
  75. * @param {PIXI.Point|PIXI.ObservablePoint} p - The point to check
  76. * @returns {boolean} Whether the given point equal to this point
  77. */
  78. equals(p)
  79. {
  80. return (p.x === this._x) && (p.y === this._y);
  81. }
  82. /**
  83. * The position of the displayObject on the x axis relative to the local coordinates of the parent.
  84. *
  85. * @member {number}
  86. */
  87. get x()
  88. {
  89. return this._x;
  90. }
  91. set x(value) // eslint-disable-line require-jsdoc
  92. {
  93. if (this._x !== value)
  94. {
  95. this._x = value;
  96. this.cb.call(this.scope);
  97. }
  98. }
  99. /**
  100. * The position of the displayObject on the x axis relative to the local coordinates of the parent.
  101. *
  102. * @member {number}
  103. */
  104. get y()
  105. {
  106. return this._y;
  107. }
  108. set y(value) // eslint-disable-line require-jsdoc
  109. {
  110. if (this._y !== value)
  111. {
  112. this._y = value;
  113. this.cb.call(this.scope);
  114. }
  115. }
  116. }