ObservablePoint.ts 3.2 KB

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