Source: tiny/core/math/ObservablePoint.js

/**
 * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
 * An observable point is a point that triggers a callback when the point's position is changed.
 *
 * @class
 * @memberof Tiny
 */
export default class ObservablePoint {
  /**
   * @param {function} cb - callback when changed
   * @param {object} scope - owner of callback
   * @param {number} [x=0] - position of the point on the x axis
   * @param {number} [y=0] - position of the point on the y axis
   */
  constructor(cb, scope, x = 0, y = 0) {
    this._x = x;
    this._y = y;

    this.cb = cb;
    this.scope = scope;
  }

  /**
   * Creates a clone of this point.
   * The callback and scope params can be overidden otherwise they will default to the clone object's values.
   *
   * @override
   * @version 1.2.0
   * @param {function} [cb=null] - callback when changed
   * @param {object} [scope=null] - owner of callback
   * @return {Tiny.ObservablePoint} a copy of the point
   */
  clone(cb = null, scope = null) {
    const _cb = cb || this.cb;
    const _scope = scope || this.scope;

    return new ObservablePoint(_cb, _scope, this._x, this._y);
  }

  /**
   * Sets the point to a new x and y position.
   * If y is omitted, both x and y will be set to x.
   *
   * @param {number} [x=0] - position of the point on the x axis
   * @param {number} [y=0] - position of the point on the y axis
   */
  set(x, y) {
    const _x = x || 0;
    const _y = y || ((y !== 0) ? _x : 0);

    if (this._x !== _x || this._y !== _y) {
      this._x = _x;
      this._y = _y;
      this.cb.call(this.scope);
    }
  }

  /**
   * Copies the data from another point
   *
   * @param {Tiny.Point|Tiny.ObservablePoint} point - point to copy from
   */
  copy(point) {
    if (this._x !== point.x || this._y !== point.y) {
      this._x = point.x;
      this._y = point.y;
      this.cb.call(this.scope);
    }
  }

  /**
   * Returns true if the given point is equal to this point
   *
   * @version 1.2.0
   * @param {Tiny.Point|Tiny.ObservablePoint} p - The point to check
   * @return {boolean} Whether the given point equal to this point
   */
  equals(p) {
    return (p.x === this._x) && (p.y === this._y);
  }

  /**
   * The position of the displayObject on the x axis relative to the local coordinates of the parent.
   *
   * @member {number}
   */
  get x() {
    return this._x;
  }

  set x(value) {
    if (this._x !== value) {
      this._x = value;
      this.cb.call(this.scope);
    }
  }

  /**
   * The position of the displayObject on the x axis relative to the local coordinates of the parent.
   *
   * @member {number}
   */
  get y() {
    return this._y;
  }

  set y(value) {
    if (this._y !== value) {
      this._y = value;
      this.cb.call(this.scope);
    }
  }
}
Documentation generated by JSDoc 3.4.3 on Fri Jul 09 2021 19:32:25 GMT+0800 (CST)