pts 0.12.7 → 0.12.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -824,11 +824,11 @@ var Rectangle = class _Rectangle {
824
824
  /**
825
825
  * Create a new circle that either fits within or encloses the rectangle. Same as [`Circle.fromRect`](#link).
826
826
  * @param pts a Group or an Iterable<Pt> with 2 Pt representing a rectangle
827
- * @param within if `true`, the circle will be within the rectangle. If `false`, the circle will enclose the rectangle.
827
+ * @param enclose if `true`, the circle will enclose the rectangle. If `false`, the circle will fit inside the rectangle.
828
828
  * @returns a Group that represents a circle
829
829
  */
830
- static toCircle(pts, within = true) {
831
- return Circle.fromRect(pts, within);
830
+ static toCircle(pts, enclose = true) {
831
+ return Circle.fromRect(pts, enclose);
832
832
  }
833
833
  /**
834
834
  * Create a square that either fits within or encloses a rectangle.
@@ -4024,8 +4024,6 @@ var Bound = class _Bound extends Group {
4024
4024
  super(...args);
4025
4025
  this._center = new Pt();
4026
4026
  this._size = new Pt();
4027
- this._topLeft = new Pt();
4028
- this._bottomRight = new Pt();
4029
4027
  this._inited = false;
4030
4028
  this.init();
4031
4029
  }
@@ -4059,10 +4057,6 @@ var Bound = class _Bound extends Group {
4059
4057
  this._inited = true;
4060
4058
  }
4061
4059
  if (this.p1 && this.p2) {
4062
- const a = this.p1;
4063
- const b = this.p2;
4064
- this.topLeft = a.$min(b);
4065
- this._bottomRight = a.$max(b);
4066
4060
  this._updateSize();
4067
4061
  this._inited = true;
4068
4062
  }
@@ -4071,33 +4065,33 @@ var Bound = class _Bound extends Group {
4071
4065
  * Clone this bound and return a new one.
4072
4066
  */
4073
4067
  clone() {
4074
- return new _Bound(this._topLeft.clone(), this._bottomRight.clone());
4068
+ return new _Bound(this.topLeft.clone(), this.bottomRight.clone());
4075
4069
  }
4076
4070
  /**
4077
4071
  * Recalculte size and center.
4078
4072
  */
4079
4073
  _updateSize() {
4080
- this._size = this._bottomRight.$subtract(this._topLeft).abs();
4074
+ this._size = this.bottomRight.$subtract(this.topLeft).abs();
4081
4075
  this._updateCenter();
4082
4076
  }
4083
4077
  /**
4084
4078
  * Recalculate center.
4085
4079
  */
4086
4080
  _updateCenter() {
4087
- this._center = this._size.$multiply(0.5).add(this._topLeft);
4081
+ this._center = this._size.$multiply(0.5).add(this.topLeft);
4088
4082
  }
4089
4083
  /**
4090
4084
  * Recalculate based on top-left position and size.
4091
4085
  */
4092
4086
  _updatePosFromTop() {
4093
- this._bottomRight = this._topLeft.$add(this._size);
4087
+ this.bottomRight = this.topLeft.$add(this._size);
4094
4088
  this._updateCenter();
4095
4089
  }
4096
4090
  /**
4097
4091
  * Recalculate based on bottom-right position and size.
4098
4092
  */
4099
4093
  _updatePosFromBottom() {
4100
- this._topLeft = this._bottomRight.$subtract(this._size);
4094
+ this.topLeft = this.bottomRight.$subtract(this._size);
4101
4095
  this._updateCenter();
4102
4096
  }
4103
4097
  /**
@@ -4105,8 +4099,8 @@ var Bound = class _Bound extends Group {
4105
4099
  */
4106
4100
  _updatePosFromCenter() {
4107
4101
  const half = this._size.$multiply(0.5);
4108
- this._topLeft = this._center.$subtract(half);
4109
- this._bottomRight = this._center.$add(half);
4102
+ this.topLeft = this._center.$subtract(half);
4103
+ this.bottomRight = this._center.$add(half);
4110
4104
  }
4111
4105
  /**
4112
4106
  * Size of this Bound
@@ -4132,22 +4126,20 @@ var Bound = class _Bound extends Group {
4132
4126
  * Top-left position of this Bound
4133
4127
  */
4134
4128
  get topLeft() {
4135
- return new Pt(this._topLeft);
4129
+ return new Pt(this[0]);
4136
4130
  }
4137
4131
  set topLeft(p) {
4138
- this._topLeft = new Pt(p);
4139
- this[0] = this._topLeft;
4132
+ this[0] = new Pt(p);
4140
4133
  this._updateSize();
4141
4134
  }
4142
4135
  /**
4143
4136
  * Bottom-right position of this Bound
4144
4137
  */
4145
4138
  get bottomRight() {
4146
- return new Pt(this._bottomRight);
4139
+ return new Pt(this[1]);
4147
4140
  }
4148
4141
  set bottomRight(p) {
4149
- this._bottomRight = new Pt(p);
4150
- this[1] = this._bottomRight;
4142
+ this[1] = new Pt(p);
4151
4143
  this._updateSize();
4152
4144
  }
4153
4145
  /**
@@ -4209,8 +4201,8 @@ var Bound = class _Bound extends Group {
4209
4201
  * It's simpler and preferable to change the Bound's properties (eg, topLeft, bottomRight) instead of updating the Bound's Pts.
4210
4202
  */
4211
4203
  update() {
4212
- this._topLeft = this[0];
4213
- this._bottomRight = this[1];
4204
+ this.topLeft = this[0];
4205
+ this.bottomRight = this[1];
4214
4206
  this._updateSize();
4215
4207
  return this;
4216
4208
  }
@@ -5795,9 +5787,9 @@ var CanvasSpace2 = class extends MultiTouchSpace {
5795
5787
  constructor(elem, callback) {
5796
5788
  super();
5797
5789
  this._pixelScale = 1;
5798
- this._autoResize = true;
5799
5790
  this._bgcolor = "#e1e9f0";
5800
5791
  this._offscreen = false;
5792
+ this._autoResize = true;
5801
5793
  this._initialResize = false;
5802
5794
  let _selector = null;
5803
5795
  let _existed = false;
@@ -5888,8 +5880,7 @@ var CanvasSpace2 = class extends MultiTouchSpace {
5888
5880
  this.autoResize = opt.resize != void 0 ? opt.resize : false;
5889
5881
  if (opt.retina !== false) {
5890
5882
  const r1 = window ? window.devicePixelRatio || 1 : 1;
5891
- const r2 = this._ctx.webkitBackingStorePixelRatio || this._ctx.mozBackingStorePixelRatio || this._ctx.msBackingStorePixelRatio || this._ctx.oBackingStorePixelRatio || this._ctx.backingStorePixelRatio || 1;
5892
- this._pixelScale = Math.max(1, r1 / r2);
5883
+ this._pixelScale = Math.max(1, r1);
5893
5884
  }
5894
5885
  if (opt.offscreen) {
5895
5886
  this._offscreen = true;
@@ -5908,13 +5899,15 @@ var CanvasSpace2 = class extends MultiTouchSpace {
5908
5899
  * @param auto a boolean value indicating if auto size is set
5909
5900
  */
5910
5901
  set autoResize(auto) {
5911
- if (!window)
5912
- return;
5913
5902
  this._autoResize = auto;
5914
5903
  if (auto) {
5915
- window.addEventListener("resize", this._resizeHandler.bind(this));
5904
+ this._resizeObserver = new ResizeObserver((entries) => {
5905
+ this._resizeHandler(null);
5906
+ });
5907
+ this._resizeObserver.observe(this._container);
5916
5908
  } else {
5917
- window.removeEventListener("resize", this._resizeHandler.bind(this));
5909
+ if (this._resizeObserver)
5910
+ this._resizeObserver.disconnect();
5918
5911
  }
5919
5912
  }
5920
5913
  get autoResize() {
@@ -5959,12 +5952,10 @@ var CanvasSpace2 = class extends MultiTouchSpace {
5959
5952
  * @param evt
5960
5953
  */
5961
5954
  _resizeHandler(evt) {
5962
- if (!window)
5963
- return;
5964
5955
  const b = this._autoResize || this._initialResize ? this._container.getBoundingClientRect() : this._canvas.getBoundingClientRect();
5965
5956
  if (b) {
5966
5957
  const box = Bound.fromBoundingRect(b);
5967
- box.center = box.center.add(window.pageXOffset, window.pageYOffset);
5958
+ box.center = box.center.add((window == null ? void 0 : window.scrollX) || 0, (window == null ? void 0 : window.scrollY) || 0);
5968
5959
  this.resize(box, evt);
5969
5960
  }
5970
5961
  }
@@ -6091,9 +6082,7 @@ var CanvasSpace2 = class extends MultiTouchSpace {
6091
6082
  * Dispose of browser resources held by this space and remove all players. Call this before unmounting the canvas.
6092
6083
  */
6093
6084
  dispose() {
6094
- if (!window)
6095
- return;
6096
- window.removeEventListener("resize", this._resizeHandler.bind(this));
6085
+ this._resizeObserver.disconnect();
6097
6086
  this.stop();
6098
6087
  this.removeAll();
6099
6088
  return this;
package/dist/pts.js CHANGED
@@ -902,11 +902,11 @@ See https://github.com/williamngan/pts for details. */
902
902
  /**
903
903
  * Create a new circle that either fits within or encloses the rectangle. Same as [`Circle.fromRect`](#link).
904
904
  * @param pts a Group or an Iterable<Pt> with 2 Pt representing a rectangle
905
- * @param within if `true`, the circle will be within the rectangle. If `false`, the circle will enclose the rectangle.
905
+ * @param enclose if `true`, the circle will enclose the rectangle. If `false`, the circle will fit inside the rectangle.
906
906
  * @returns a Group that represents a circle
907
907
  */
908
- static toCircle(pts, within = true) {
909
- return Circle.fromRect(pts, within);
908
+ static toCircle(pts, enclose = true) {
909
+ return Circle.fromRect(pts, enclose);
910
910
  }
911
911
  /**
912
912
  * Create a square that either fits within or encloses a rectangle.
@@ -4102,8 +4102,6 @@ See https://github.com/williamngan/pts for details. */
4102
4102
  super(...args);
4103
4103
  this._center = new Pt();
4104
4104
  this._size = new Pt();
4105
- this._topLeft = new Pt();
4106
- this._bottomRight = new Pt();
4107
4105
  this._inited = false;
4108
4106
  this.init();
4109
4107
  }
@@ -4137,10 +4135,6 @@ See https://github.com/williamngan/pts for details. */
4137
4135
  this._inited = true;
4138
4136
  }
4139
4137
  if (this.p1 && this.p2) {
4140
- const a = this.p1;
4141
- const b = this.p2;
4142
- this.topLeft = a.$min(b);
4143
- this._bottomRight = a.$max(b);
4144
4138
  this._updateSize();
4145
4139
  this._inited = true;
4146
4140
  }
@@ -4149,33 +4143,33 @@ See https://github.com/williamngan/pts for details. */
4149
4143
  * Clone this bound and return a new one.
4150
4144
  */
4151
4145
  clone() {
4152
- return new _Bound(this._topLeft.clone(), this._bottomRight.clone());
4146
+ return new _Bound(this.topLeft.clone(), this.bottomRight.clone());
4153
4147
  }
4154
4148
  /**
4155
4149
  * Recalculte size and center.
4156
4150
  */
4157
4151
  _updateSize() {
4158
- this._size = this._bottomRight.$subtract(this._topLeft).abs();
4152
+ this._size = this.bottomRight.$subtract(this.topLeft).abs();
4159
4153
  this._updateCenter();
4160
4154
  }
4161
4155
  /**
4162
4156
  * Recalculate center.
4163
4157
  */
4164
4158
  _updateCenter() {
4165
- this._center = this._size.$multiply(0.5).add(this._topLeft);
4159
+ this._center = this._size.$multiply(0.5).add(this.topLeft);
4166
4160
  }
4167
4161
  /**
4168
4162
  * Recalculate based on top-left position and size.
4169
4163
  */
4170
4164
  _updatePosFromTop() {
4171
- this._bottomRight = this._topLeft.$add(this._size);
4165
+ this.bottomRight = this.topLeft.$add(this._size);
4172
4166
  this._updateCenter();
4173
4167
  }
4174
4168
  /**
4175
4169
  * Recalculate based on bottom-right position and size.
4176
4170
  */
4177
4171
  _updatePosFromBottom() {
4178
- this._topLeft = this._bottomRight.$subtract(this._size);
4172
+ this.topLeft = this.bottomRight.$subtract(this._size);
4179
4173
  this._updateCenter();
4180
4174
  }
4181
4175
  /**
@@ -4183,8 +4177,8 @@ See https://github.com/williamngan/pts for details. */
4183
4177
  */
4184
4178
  _updatePosFromCenter() {
4185
4179
  const half = this._size.$multiply(0.5);
4186
- this._topLeft = this._center.$subtract(half);
4187
- this._bottomRight = this._center.$add(half);
4180
+ this.topLeft = this._center.$subtract(half);
4181
+ this.bottomRight = this._center.$add(half);
4188
4182
  }
4189
4183
  /**
4190
4184
  * Size of this Bound
@@ -4210,22 +4204,20 @@ See https://github.com/williamngan/pts for details. */
4210
4204
  * Top-left position of this Bound
4211
4205
  */
4212
4206
  get topLeft() {
4213
- return new Pt(this._topLeft);
4207
+ return new Pt(this[0]);
4214
4208
  }
4215
4209
  set topLeft(p) {
4216
- this._topLeft = new Pt(p);
4217
- this[0] = this._topLeft;
4210
+ this[0] = new Pt(p);
4218
4211
  this._updateSize();
4219
4212
  }
4220
4213
  /**
4221
4214
  * Bottom-right position of this Bound
4222
4215
  */
4223
4216
  get bottomRight() {
4224
- return new Pt(this._bottomRight);
4217
+ return new Pt(this[1]);
4225
4218
  }
4226
4219
  set bottomRight(p) {
4227
- this._bottomRight = new Pt(p);
4228
- this[1] = this._bottomRight;
4220
+ this[1] = new Pt(p);
4229
4221
  this._updateSize();
4230
4222
  }
4231
4223
  /**
@@ -4287,8 +4279,8 @@ See https://github.com/williamngan/pts for details. */
4287
4279
  * It's simpler and preferable to change the Bound's properties (eg, topLeft, bottomRight) instead of updating the Bound's Pts.
4288
4280
  */
4289
4281
  update() {
4290
- this._topLeft = this[0];
4291
- this._bottomRight = this[1];
4282
+ this.topLeft = this[0];
4283
+ this.bottomRight = this[1];
4292
4284
  this._updateSize();
4293
4285
  return this;
4294
4286
  }
@@ -5895,9 +5887,9 @@ See https://github.com/williamngan/pts for details. */
5895
5887
  constructor(elem, callback) {
5896
5888
  super();
5897
5889
  this._pixelScale = 1;
5898
- this._autoResize = true;
5899
5890
  this._bgcolor = "#e1e9f0";
5900
5891
  this._offscreen = false;
5892
+ this._autoResize = true;
5901
5893
  this._initialResize = false;
5902
5894
  let _selector = null;
5903
5895
  let _existed = false;
@@ -5988,8 +5980,7 @@ See https://github.com/williamngan/pts for details. */
5988
5980
  this.autoResize = opt.resize != void 0 ? opt.resize : false;
5989
5981
  if (opt.retina !== false) {
5990
5982
  const r1 = window ? window.devicePixelRatio || 1 : 1;
5991
- const r2 = this._ctx.webkitBackingStorePixelRatio || this._ctx.mozBackingStorePixelRatio || this._ctx.msBackingStorePixelRatio || this._ctx.oBackingStorePixelRatio || this._ctx.backingStorePixelRatio || 1;
5992
- this._pixelScale = Math.max(1, r1 / r2);
5983
+ this._pixelScale = Math.max(1, r1);
5993
5984
  }
5994
5985
  if (opt.offscreen) {
5995
5986
  this._offscreen = true;
@@ -6008,13 +5999,15 @@ See https://github.com/williamngan/pts for details. */
6008
5999
  * @param auto a boolean value indicating if auto size is set
6009
6000
  */
6010
6001
  set autoResize(auto) {
6011
- if (!window)
6012
- return;
6013
6002
  this._autoResize = auto;
6014
6003
  if (auto) {
6015
- window.addEventListener("resize", this._resizeHandler.bind(this));
6004
+ this._resizeObserver = new ResizeObserver((entries) => {
6005
+ this._resizeHandler(null);
6006
+ });
6007
+ this._resizeObserver.observe(this._container);
6016
6008
  } else {
6017
- window.removeEventListener("resize", this._resizeHandler.bind(this));
6009
+ if (this._resizeObserver)
6010
+ this._resizeObserver.disconnect();
6018
6011
  }
6019
6012
  }
6020
6013
  get autoResize() {
@@ -6059,12 +6052,10 @@ See https://github.com/williamngan/pts for details. */
6059
6052
  * @param evt
6060
6053
  */
6061
6054
  _resizeHandler(evt) {
6062
- if (!window)
6063
- return;
6064
6055
  const b = this._autoResize || this._initialResize ? this._container.getBoundingClientRect() : this._canvas.getBoundingClientRect();
6065
6056
  if (b) {
6066
6057
  const box = Bound.fromBoundingRect(b);
6067
- box.center = box.center.add(window.pageXOffset, window.pageYOffset);
6058
+ box.center = box.center.add((window == null ? void 0 : window.scrollX) || 0, (window == null ? void 0 : window.scrollY) || 0);
6068
6059
  this.resize(box, evt);
6069
6060
  }
6070
6061
  }
@@ -6191,9 +6182,7 @@ See https://github.com/williamngan/pts for details. */
6191
6182
  * Dispose of browser resources held by this space and remove all players. Call this before unmounting the canvas.
6192
6183
  */
6193
6184
  dispose() {
6194
- if (!window)
6195
- return;
6196
- window.removeEventListener("resize", this._resizeHandler.bind(this));
6185
+ this._resizeObserver.disconnect();
6197
6186
  this.stop();
6198
6187
  this.removeAll();
6199
6188
  return this;