modern-canvas 0.7.11 → 0.7.13

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
@@ -1944,10 +1944,23 @@ class Rect2 {
1944
1944
  switch (args.length) {
1945
1945
  case 0:
1946
1946
  break;
1947
- case 1:
1948
- position.set(args[0].position);
1949
- size.set(args[0].size);
1947
+ case 1: {
1948
+ const arg = args[0];
1949
+ if (arg instanceof Rect2) {
1950
+ position.set(arg.position);
1951
+ size.set(arg.size);
1952
+ } else {
1953
+ const xx = arg.map((p) => p[0]);
1954
+ const yy = arg.map((p) => p[1]);
1955
+ const minX = Math.min(...xx);
1956
+ const maxX = Math.max(...xx);
1957
+ const minY = Math.min(...yy);
1958
+ const maxY = Math.max(...yy);
1959
+ position.set(minX, minY);
1960
+ size.set(maxX - minX, maxY - minY);
1961
+ }
1950
1962
  break;
1963
+ }
1951
1964
  case 2:
1952
1965
  position.set(args[0]);
1953
1966
  size.set(args[1]);
@@ -1969,6 +1982,14 @@ class Rect2 {
1969
1982
  );
1970
1983
  return this;
1971
1984
  }
1985
+ toMinmax() {
1986
+ return {
1987
+ minX: this.position.x,
1988
+ minY: this.position.y,
1989
+ maxX: this.end.x,
1990
+ maxY: this.end.y
1991
+ };
1992
+ }
1972
1993
  toArray() {
1973
1994
  return [this.x, this.y, this.width, this.height];
1974
1995
  }
@@ -2135,6 +2156,30 @@ class Transform2D extends Matrix3 {
2135
2156
  b * x + d * y + ty
2136
2157
  ];
2137
2158
  }
2159
+ decompose(pivot = { x: 0, y: 0 }, output = {
2160
+ position: { x: 0, y: 0 },
2161
+ scale: { x: 0, y: 0 },
2162
+ skew: { x: 0, y: 0 },
2163
+ rotation: 0
2164
+ }) {
2165
+ const { a, b, c, d, tx, ty } = this.toObject();
2166
+ const skewX = -Math.atan2(-c, d);
2167
+ const skewY = Math.atan2(b, a);
2168
+ const delta = Math.abs(skewX + skewY);
2169
+ if (delta < 1e-5 || Math.abs(PI_2 - delta) < 1e-5) {
2170
+ output.rotation = skewY;
2171
+ output.skew.x = output.skew.y = 0;
2172
+ } else {
2173
+ output.rotation = 0;
2174
+ output.skew.x = skewX;
2175
+ output.skew.y = skewY;
2176
+ }
2177
+ output.scale.x = Math.sqrt(a * a + b * b);
2178
+ output.scale.y = Math.sqrt(c * c + d * d);
2179
+ output.position.x = tx + (pivot.x * a + pivot.y * c);
2180
+ output.position.y = ty + (pivot.x * b + pivot.y * d);
2181
+ return output;
2182
+ }
2138
2183
  inverse() {
2139
2184
  return this.clone().invert();
2140
2185
  }
@@ -2143,7 +2188,17 @@ class Transform2D extends Matrix3 {
2143
2188
  return a === 1 && b === 0 && c === 0 && d === 1 && tx === 0 && ty === 0;
2144
2189
  }
2145
2190
  toObject() {
2146
- const [a, c, tx, b, d, ty, , , tz] = this._array;
2191
+ const [
2192
+ a,
2193
+ c,
2194
+ tx,
2195
+ b,
2196
+ d,
2197
+ ty,
2198
+ ,
2199
+ ,
2200
+ tz
2201
+ ] = this._array;
2147
2202
  return { a, c, tx, b, d, ty, tz };
2148
2203
  }
2149
2204
  }
@@ -6885,10 +6940,10 @@ __decorateClass$O([
6885
6940
  property()
6886
6941
  ], Effect.prototype, "effectMode", 2);
6887
6942
  __decorateClass$O([
6888
- property()
6943
+ property({ fallback: "" })
6889
6944
  ], Effect.prototype, "glsl", 2);
6890
6945
  __decorateClass$O([
6891
- property()
6946
+ property({ fallback: "" })
6892
6947
  ], Effect.prototype, "glslSrc", 2);
6893
6948
  Effect = __decorateClass$O([
6894
6949
  customNode("Effect")
@@ -7164,25 +7219,27 @@ let Node2D = class extends CanvasItem {
7164
7219
  const parent = this.getParent();
7165
7220
  if (parent?.globalTransform) {
7166
7221
  this._parentTransformDirtyId = parent.globalTransform.dirtyId;
7167
- this.globalScale.set(parent.globalScale.x * this.scale.x, parent.globalScale.y * this.scale.y);
7222
+ this.globalPosition.set(
7223
+ parent.globalPosition.x + this.position.x,
7224
+ parent.globalPosition.y + this.position.y
7225
+ );
7226
+ this.globalScale.set(
7227
+ parent.globalScale.x * this.scale.x,
7228
+ parent.globalScale.y * this.scale.y
7229
+ );
7230
+ this.globalSkew.set(
7231
+ parent.globalSkew.x * this.skew.x,
7232
+ parent.globalSkew.y * this.skew.y
7233
+ );
7168
7234
  this.globalRotation = parent.globalRotation + this.rotation;
7169
7235
  parent.globalTransform.multiply(this.transform, this.globalTransform);
7170
7236
  } else {
7237
+ this.globalPosition.copy(this.position);
7171
7238
  this.globalScale.copy(this.scale);
7239
+ this.globalSkew.copy(this.skew);
7172
7240
  this.globalRotation = this.rotation;
7173
7241
  this.globalTransform.copy(this.transform);
7174
7242
  }
7175
- const [
7176
- a,
7177
- c,
7178
- tx,
7179
- b,
7180
- d,
7181
- ty
7182
- ] = this.globalTransform.toArray();
7183
- this.globalPosition.set(tx, ty);
7184
- this.globalSkew.x = Math.atan2(c, a) - this.globalRotation;
7185
- this.globalSkew.y = Math.atan2(b, d) - this.globalRotation;
7186
7243
  this.requestRelayout();
7187
7244
  }
7188
7245
  _transformVertices(vertices, vertTransform) {
@@ -7283,7 +7340,7 @@ let Camera2D = class extends Node2D {
7283
7340
  }
7284
7341
  } else if (key === "pointerdown") {
7285
7342
  const e = event;
7286
- if (!this.grabbing && this.spaceKey) {
7343
+ if (!this.grabbing && (this.spaceKey || e.button === 1)) {
7287
7344
  this.grabbing = true;
7288
7345
  e.cursor = "grabbing";
7289
7346
  this._screenOffset = { x: e.screenX, y: e.screenY };
@@ -7301,7 +7358,11 @@ let Camera2D = class extends Node2D {
7301
7358
  const e = event;
7302
7359
  if (this.grabbing) {
7303
7360
  this.grabbing = false;
7304
- e.cursor = "grab";
7361
+ if (this.spaceKey) {
7362
+ e.cursor = "grab";
7363
+ } else {
7364
+ e.cursor = "default";
7365
+ }
7305
7366
  }
7306
7367
  } else if (key === "wheel") {
7307
7368
  this._onWheel(event);
@@ -7565,28 +7626,28 @@ void main(void) {
7565
7626
  }`
7566
7627
  }));
7567
7628
  __decorateClass$H([
7568
- property()
7629
+ property({ fallback: 1 })
7569
7630
  ], ColorAdjustEffect.prototype, "saturation", 2);
7570
7631
  __decorateClass$H([
7571
- property()
7632
+ property({ fallback: 1 })
7572
7633
  ], ColorAdjustEffect.prototype, "contrast", 2);
7573
7634
  __decorateClass$H([
7574
- property()
7635
+ property({ fallback: 1 })
7575
7636
  ], ColorAdjustEffect.prototype, "brightness", 2);
7576
7637
  __decorateClass$H([
7577
- property()
7638
+ property({ fallback: 1 })
7578
7639
  ], ColorAdjustEffect.prototype, "red", 2);
7579
7640
  __decorateClass$H([
7580
- property()
7641
+ property({ fallback: 1 })
7581
7642
  ], ColorAdjustEffect.prototype, "green", 2);
7582
7643
  __decorateClass$H([
7583
- property()
7644
+ property({ fallback: 1 })
7584
7645
  ], ColorAdjustEffect.prototype, "blue", 2);
7585
7646
  __decorateClass$H([
7586
- property()
7647
+ property({ fallback: 1 })
7587
7648
  ], ColorAdjustEffect.prototype, "alpha", 2);
7588
7649
  __decorateClass$H([
7589
- property()
7650
+ property({ fallback: 1 })
7590
7651
  ], ColorAdjustEffect.prototype, "gamma", 2);
7591
7652
  ColorAdjustEffect = __decorateClass$H([
7592
7653
  customNode("ColorAdjustEffect")
@@ -7772,10 +7833,10 @@ void main(void) {
7772
7833
  }`
7773
7834
  }));
7774
7835
  __decorateClass$F([
7775
- property()
7836
+ property({ default: () => [] })
7776
7837
  ], ColorOverlayEffect.prototype, "colors", 2);
7777
7838
  __decorateClass$F([
7778
- property()
7839
+ property({ fallback: 0.5 })
7779
7840
  ], ColorOverlayEffect.prototype, "alpha", 2);
7780
7841
  ColorOverlayEffect = __decorateClass$F([
7781
7842
  customNode("ColorOverlayEffect")
@@ -7857,10 +7918,10 @@ void main(void) {
7857
7918
  }`
7858
7919
  }));
7859
7920
  __decorateClass$E([
7860
- property()
7921
+ property({ default: () => [] })
7861
7922
  ], ColorRemoveEffect.prototype, "colors", 2);
7862
7923
  __decorateClass$E([
7863
- property()
7924
+ property({ fallback: 0.5 })
7864
7925
  ], ColorRemoveEffect.prototype, "epsilon", 2);
7865
7926
  ColorRemoveEffect = __decorateClass$E([
7866
7927
  customNode("ColorRemoveEffect")
@@ -7964,10 +8025,10 @@ void main(void) {
7964
8025
  }`
7965
8026
  }));
7966
8027
  __decorateClass$D([
7967
- property()
8028
+ property({ default: () => [] })
7968
8029
  ], ColorReplaceEffect.prototype, "colors", 2);
7969
8030
  __decorateClass$D([
7970
- property()
8031
+ property({ fallback: 0.05 })
7971
8032
  ], ColorReplaceEffect.prototype, "epsilon", 2);
7972
8033
  ColorReplaceEffect = __decorateClass$D([
7973
8034
  customNode("ColorReplaceEffect")
@@ -8088,10 +8149,10 @@ void main(void) {
8088
8149
  frag: frag$2
8089
8150
  }));
8090
8151
  __decorateClass$C([
8091
- property({ default: 4 })
8152
+ property({ fallback: 4 })
8092
8153
  ], GaussianBlurEffect.prototype, "strength", 2);
8093
8154
  __decorateClass$C([
8094
- property({ default: 3 })
8155
+ property({ fallback: 3 })
8095
8156
  ], GaussianBlurEffect.prototype, "quality", 2);
8096
8157
  GaussianBlurEffect = __decorateClass$C([
8097
8158
  customNode("GaussianBlurEffect")
@@ -8170,19 +8231,19 @@ void main(void) {
8170
8231
  }`
8171
8232
  }));
8172
8233
  __decorateClass$B([
8173
- property()
8234
+ property({ fallback: "#000000FF" })
8174
8235
  ], DropShadowEffect.prototype, "color", 2);
8175
8236
  __decorateClass$B([
8176
- property()
8237
+ property({ fallback: 4 })
8177
8238
  ], DropShadowEffect.prototype, "blur", 2);
8178
8239
  __decorateClass$B([
8179
- property()
8240
+ property({ fallback: 4 })
8180
8241
  ], DropShadowEffect.prototype, "offsetX", 2);
8181
8242
  __decorateClass$B([
8182
- property()
8243
+ property({ fallback: 4 })
8183
8244
  ], DropShadowEffect.prototype, "offsetY", 2);
8184
8245
  __decorateClass$B([
8185
- property()
8246
+ property({ fallback: false })
8186
8247
  ], DropShadowEffect.prototype, "shadowOnly", 2);
8187
8248
  DropShadowEffect = __decorateClass$B([
8188
8249
  customNode("DropShadowEffect")
@@ -8242,7 +8303,7 @@ void main(void) {
8242
8303
  }`
8243
8304
  }));
8244
8305
  __decorateClass$A([
8245
- property()
8306
+ property({ fallback: 5 })
8246
8307
  ], EmbossEffect.prototype, "strength", 2);
8247
8308
  EmbossEffect = __decorateClass$A([
8248
8309
  customNode("EmbossEffect")
@@ -8431,31 +8492,31 @@ void main(void) {
8431
8492
  }`
8432
8493
  }));
8433
8494
  __decorateClass$z([
8434
- property()
8495
+ property({ fallback: 10 })
8435
8496
  ], GlitchEffect.prototype, "slices", 2);
8436
8497
  __decorateClass$z([
8437
- property()
8498
+ property({ fallback: 512 })
8438
8499
  ], GlitchEffect.prototype, "sampleSize", 2);
8439
8500
  __decorateClass$z([
8440
- property()
8501
+ property({ fallback: 100 })
8441
8502
  ], GlitchEffect.prototype, "offset", 2);
8442
8503
  __decorateClass$z([
8443
- property()
8504
+ property({ fallback: 0 })
8444
8505
  ], GlitchEffect.prototype, "direction", 2);
8445
8506
  __decorateClass$z([
8446
- property()
8507
+ property({ fallback: 2 })
8447
8508
  ], GlitchEffect.prototype, "fillMode", 2);
8448
8509
  __decorateClass$z([
8449
- property()
8510
+ property({ fallback: 0 })
8450
8511
  ], GlitchEffect.prototype, "seed", 2);
8451
8512
  __decorateClass$z([
8452
- property()
8513
+ property({ default: () => [2, 2] })
8453
8514
  ], GlitchEffect.prototype, "red", 2);
8454
8515
  __decorateClass$z([
8455
- property()
8516
+ property({ default: () => [-10, 4] })
8456
8517
  ], GlitchEffect.prototype, "green", 2);
8457
8518
  __decorateClass$z([
8458
- property()
8519
+ property({ default: () => [10, -4] })
8459
8520
  ], GlitchEffect.prototype, "blue", 2);
8460
8521
  GlitchEffect = __decorateClass$z([
8461
8522
  customNode("GlitchEffect")
@@ -8648,25 +8709,25 @@ void main(void) {
8648
8709
  }`
8649
8710
  }));
8650
8711
  __decorateClass$y([
8651
- property()
8712
+ property({ fallback: 0 })
8652
8713
  ], GodrayEffect.prototype, "time", 2);
8653
8714
  __decorateClass$y([
8654
- property()
8715
+ property({ fallback: 30 })
8655
8716
  ], GodrayEffect.prototype, "angle", 2);
8656
8717
  __decorateClass$y([
8657
- property()
8718
+ property({ fallback: 0.5 })
8658
8719
  ], GodrayEffect.prototype, "gain", 2);
8659
8720
  __decorateClass$y([
8660
- property()
8721
+ property({ fallback: 2.5 })
8661
8722
  ], GodrayEffect.prototype, "lacunarity", 2);
8662
8723
  __decorateClass$y([
8663
- property()
8724
+ property({ fallback: true })
8664
8725
  ], GodrayEffect.prototype, "parallel", 2);
8665
8726
  __decorateClass$y([
8666
- property()
8727
+ property({ default: () => [0, 0] })
8667
8728
  ], GodrayEffect.prototype, "center", 2);
8668
8729
  __decorateClass$y([
8669
- property()
8730
+ property({ fallback: 1 })
8670
8731
  ], GodrayEffect.prototype, "alpha", 2);
8671
8732
  GodrayEffect = __decorateClass$y([
8672
8733
  customNode("GodrayEffect")
@@ -8770,13 +8831,13 @@ void main() {
8770
8831
  }
8771
8832
  };
8772
8833
  __decorateClass$x([
8773
- property()
8834
+ property({ fallback: 4 })
8774
8835
  ], KawaseBlurEffect.prototype, "strength", 2);
8775
8836
  __decorateClass$x([
8776
- property()
8837
+ property({ fallback: 3 })
8777
8838
  ], KawaseBlurEffect.prototype, "quality", 2);
8778
8839
  __decorateClass$x([
8779
- property()
8840
+ property({ default: () => [1, 1] })
8780
8841
  ], KawaseBlurEffect.prototype, "pixelSize", 2);
8781
8842
  KawaseBlurEffect = __decorateClass$x([
8782
8843
  customNode("KawaseBlurEffect")
@@ -8882,7 +8943,7 @@ __decorateClass$w([
8882
8943
  property({ protected: true })
8883
8944
  ], MaskEffect.prototype, "texture", 2);
8884
8945
  __decorateClass$w([
8885
- property({ default: "" })
8946
+ property({ fallback: "" })
8886
8947
  ], MaskEffect.prototype, "src", 2);
8887
8948
  MaskEffect = __decorateClass$w([
8888
8949
  customNode("MaskEffect")
@@ -8984,25 +9045,25 @@ void main() {
8984
9045
  __publicField$7(OutlineEffect, "MIN_SAMPLES", 1);
8985
9046
  __publicField$7(OutlineEffect, "MAX_SAMPLES", 100);
8986
9047
  __decorateClass$v([
8987
- property()
9048
+ property({ fallback: "#000000ff" })
8988
9049
  ], OutlineEffect.prototype, "color", 2);
8989
9050
  __decorateClass$v([
8990
- property()
9051
+ property({ fallback: 1 })
8991
9052
  ], OutlineEffect.prototype, "width", 2);
8992
9053
  __decorateClass$v([
8993
- property()
9054
+ property({ fallback: "solid" })
8994
9055
  ], OutlineEffect.prototype, "style", 2);
8995
9056
  __decorateClass$v([
8996
9057
  property()
8997
9058
  ], OutlineEffect.prototype, "image", 2);
8998
9059
  __decorateClass$v([
8999
- property()
9060
+ property({ fallback: 1 })
9000
9061
  ], OutlineEffect.prototype, "opacity", 2);
9001
9062
  __decorateClass$v([
9002
- property()
9063
+ property({ fallback: 0.1 })
9003
9064
  ], OutlineEffect.prototype, "quality", 2);
9004
9065
  __decorateClass$v([
9005
- property()
9066
+ property({ fallback: false })
9006
9067
  ], OutlineEffect.prototype, "knockout", 2);
9007
9068
  OutlineEffect = __decorateClass$v([
9008
9069
  customNode("OutlineEffect")
@@ -9073,7 +9134,7 @@ void main(void) {
9073
9134
  }`
9074
9135
  }));
9075
9136
  __decorateClass$u([
9076
- property()
9137
+ property({ fallback: 10 })
9077
9138
  ], PixelateEffect.prototype, "strength", 2);
9078
9139
  PixelateEffect = __decorateClass$u([
9079
9140
  customNode("PixelateEffect")
@@ -9204,13 +9265,13 @@ __decorateClass$t([
9204
9265
  property()
9205
9266
  ], ZoomBlurEffect.prototype, "center", 2);
9206
9267
  __decorateClass$t([
9207
- property()
9268
+ property({ fallback: 20 })
9208
9269
  ], ZoomBlurEffect.prototype, "innerRadius", 2);
9209
9270
  __decorateClass$t([
9210
- property()
9271
+ property({ fallback: -1 })
9211
9272
  ], ZoomBlurEffect.prototype, "radius", 2);
9212
9273
  __decorateClass$t([
9213
- property()
9274
+ property({ fallback: 0.1 })
9214
9275
  ], ZoomBlurEffect.prototype, "strength", 2);
9215
9276
  ZoomBlurEffect = __decorateClass$t([
9216
9277
  customNode("ZoomBlurEffect")
@@ -9952,36 +10013,67 @@ let BaseElement2D = class extends Node2D {
9952
10013
  this._updateOverflow();
9953
10014
  }
9954
10015
  getRect() {
9955
- const { width: w, height: h } = this.size;
10016
+ return this.getGlobalAabb();
10017
+ }
10018
+ _getPointArray() {
10019
+ const { width, height } = this.size;
9956
10020
  const x1 = 0;
9957
10021
  const y1 = 0;
9958
- const x2 = x1 + w;
9959
- const y2 = y1 + h;
9960
- const [a, c, tx, b, d, ty] = this.globalTransform.toArray();
9961
- const points = [
10022
+ const x2 = x1 + width;
10023
+ const y2 = y1 + height;
10024
+ return [
9962
10025
  [x1, y1],
9963
10026
  [x1, y2],
9964
10027
  [x2, y1],
9965
10028
  [x2, y2]
9966
- ].map((p) => {
9967
- return [
9968
- a * p[0] + c * p[1] + tx,
9969
- b * p[0] + d * p[1] + ty
9970
- ];
9971
- });
9972
- const xx = points.map((p) => p[0]);
9973
- const yy = points.map((p) => p[1]);
9974
- const minX = Math.min(...xx);
9975
- const maxX = Math.max(...xx);
9976
- const minY = Math.min(...yy);
9977
- const maxY = Math.max(...yy);
10029
+ ];
10030
+ }
10031
+ getAabb() {
9978
10032
  return new Rect2(
9979
- minX,
9980
- minY,
9981
- maxX - minX,
9982
- maxY - minY
10033
+ this._getPointArray().map((p) => {
10034
+ return this.transform.applyToPoint(p[0], p[1]);
10035
+ })
10036
+ );
10037
+ }
10038
+ getGlobalAabb() {
10039
+ return new Rect2(
10040
+ this._getPointArray().map((p) => {
10041
+ return this.globalTransform.applyToPoint(p[0], p[1]);
10042
+ })
9983
10043
  );
9984
10044
  }
10045
+ getObb() {
10046
+ const origin = this.getTransformOrigin();
10047
+ const _origin = this.transform.applyToPoint(origin.x, origin.y);
10048
+ const offset = [_origin[0] - origin.x, _origin[1] - origin.y];
10049
+ return {
10050
+ rect: new Rect2(
10051
+ this._getPointArray().map((p) => {
10052
+ return [
10053
+ p[0] + offset[0],
10054
+ p[1] + offset[1]
10055
+ ];
10056
+ })
10057
+ ),
10058
+ rotation: this.rotation
10059
+ };
10060
+ }
10061
+ getGlobalObb() {
10062
+ const origin = this.getTransformOrigin();
10063
+ const _origin = this.globalTransform.applyToPoint(origin.x, origin.y);
10064
+ const offset = [_origin[0] - origin.x, _origin[1] - origin.y];
10065
+ return {
10066
+ rect: new Rect2(
10067
+ this._getPointArray().map((p) => {
10068
+ return [
10069
+ p[0] + offset[0],
10070
+ p[1] + offset[1]
10071
+ ];
10072
+ })
10073
+ ),
10074
+ rotation: this.globalRotation
10075
+ };
10076
+ }
9985
10077
  // protected _rectsOverlap(r1: any, r2: any): boolean {
9986
10078
  // return (
9987
10079
  // r1.x < r2.x + r2.width
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.7.11",
4
+ "version": "0.7.13",
5
5
  "packageManager": "pnpm@9.15.1",
6
6
  "description": "A JavaScript WebGL rendering engine.",
7
7
  "author": "wxm",