observablePoint.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { any } from "vue-types";
  2. import { Point } from "./point";
  3. export class ObservablePoint {
  4. cb: any;
  5. scope: any;
  6. _x = 0;
  7. _y = 0;
  8. constructor(cb:any, scope:any, x=0 , y=0) {
  9. this._x = x;
  10. this._y = y;
  11. this.cb = cb;
  12. this.scope = scope;
  13. }
  14. clone(cb = this.cb, scope = this.scope) {
  15. return new ObservablePoint(cb, scope, this._x, this._y);
  16. }
  17. set(x = 0, y = x) {
  18. if (this._x !== x || this._y !== y) {
  19. this._x = x;
  20. this._y = y;
  21. this.cb.call(this.scope);
  22. }
  23. }
  24. copyFrom(p: Point) {
  25. if (this._x !== p.x || this._y !== p.y) {
  26. this._x = p.x;
  27. this._y = p.y;
  28. this.cb.call(this.scope);
  29. }
  30. return this;
  31. }
  32. copyTo(p: Point) {
  33. p.set(this._x, this._y);
  34. return p;
  35. }
  36. equals(p: Point) {
  37. return p.x === this._x && p.y === this._y;
  38. }
  39. get x() {
  40. return this._x;
  41. }
  42. set x(
  43. value // eslint-disable-line require-jsdoc
  44. ) {
  45. if (this._x !== value) {
  46. this._x = value;
  47. this.cb.call(this.scope);
  48. }
  49. }
  50. get y() {
  51. return this._y;
  52. }
  53. set y(value) {
  54. if (this._y !== value) {
  55. this._y = value;
  56. this.cb.call(this.scope);
  57. }
  58. }
  59. }