modern-canvas 0.7.12 → 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) {
@@ -7569,28 +7626,28 @@ void main(void) {
7569
7626
  }`
7570
7627
  }));
7571
7628
  __decorateClass$H([
7572
- property()
7629
+ property({ fallback: 1 })
7573
7630
  ], ColorAdjustEffect.prototype, "saturation", 2);
7574
7631
  __decorateClass$H([
7575
- property()
7632
+ property({ fallback: 1 })
7576
7633
  ], ColorAdjustEffect.prototype, "contrast", 2);
7577
7634
  __decorateClass$H([
7578
- property()
7635
+ property({ fallback: 1 })
7579
7636
  ], ColorAdjustEffect.prototype, "brightness", 2);
7580
7637
  __decorateClass$H([
7581
- property()
7638
+ property({ fallback: 1 })
7582
7639
  ], ColorAdjustEffect.prototype, "red", 2);
7583
7640
  __decorateClass$H([
7584
- property()
7641
+ property({ fallback: 1 })
7585
7642
  ], ColorAdjustEffect.prototype, "green", 2);
7586
7643
  __decorateClass$H([
7587
- property()
7644
+ property({ fallback: 1 })
7588
7645
  ], ColorAdjustEffect.prototype, "blue", 2);
7589
7646
  __decorateClass$H([
7590
- property()
7647
+ property({ fallback: 1 })
7591
7648
  ], ColorAdjustEffect.prototype, "alpha", 2);
7592
7649
  __decorateClass$H([
7593
- property()
7650
+ property({ fallback: 1 })
7594
7651
  ], ColorAdjustEffect.prototype, "gamma", 2);
7595
7652
  ColorAdjustEffect = __decorateClass$H([
7596
7653
  customNode("ColorAdjustEffect")
@@ -7776,10 +7833,10 @@ void main(void) {
7776
7833
  }`
7777
7834
  }));
7778
7835
  __decorateClass$F([
7779
- property()
7836
+ property({ default: () => [] })
7780
7837
  ], ColorOverlayEffect.prototype, "colors", 2);
7781
7838
  __decorateClass$F([
7782
- property()
7839
+ property({ fallback: 0.5 })
7783
7840
  ], ColorOverlayEffect.prototype, "alpha", 2);
7784
7841
  ColorOverlayEffect = __decorateClass$F([
7785
7842
  customNode("ColorOverlayEffect")
@@ -7861,10 +7918,10 @@ void main(void) {
7861
7918
  }`
7862
7919
  }));
7863
7920
  __decorateClass$E([
7864
- property()
7921
+ property({ default: () => [] })
7865
7922
  ], ColorRemoveEffect.prototype, "colors", 2);
7866
7923
  __decorateClass$E([
7867
- property()
7924
+ property({ fallback: 0.5 })
7868
7925
  ], ColorRemoveEffect.prototype, "epsilon", 2);
7869
7926
  ColorRemoveEffect = __decorateClass$E([
7870
7927
  customNode("ColorRemoveEffect")
@@ -7968,10 +8025,10 @@ void main(void) {
7968
8025
  }`
7969
8026
  }));
7970
8027
  __decorateClass$D([
7971
- property()
8028
+ property({ default: () => [] })
7972
8029
  ], ColorReplaceEffect.prototype, "colors", 2);
7973
8030
  __decorateClass$D([
7974
- property()
8031
+ property({ fallback: 0.05 })
7975
8032
  ], ColorReplaceEffect.prototype, "epsilon", 2);
7976
8033
  ColorReplaceEffect = __decorateClass$D([
7977
8034
  customNode("ColorReplaceEffect")
@@ -8092,10 +8149,10 @@ void main(void) {
8092
8149
  frag: frag$2
8093
8150
  }));
8094
8151
  __decorateClass$C([
8095
- property({ default: 4 })
8152
+ property({ fallback: 4 })
8096
8153
  ], GaussianBlurEffect.prototype, "strength", 2);
8097
8154
  __decorateClass$C([
8098
- property({ default: 3 })
8155
+ property({ fallback: 3 })
8099
8156
  ], GaussianBlurEffect.prototype, "quality", 2);
8100
8157
  GaussianBlurEffect = __decorateClass$C([
8101
8158
  customNode("GaussianBlurEffect")
@@ -8174,19 +8231,19 @@ void main(void) {
8174
8231
  }`
8175
8232
  }));
8176
8233
  __decorateClass$B([
8177
- property()
8234
+ property({ fallback: "#000000FF" })
8178
8235
  ], DropShadowEffect.prototype, "color", 2);
8179
8236
  __decorateClass$B([
8180
- property()
8237
+ property({ fallback: 4 })
8181
8238
  ], DropShadowEffect.prototype, "blur", 2);
8182
8239
  __decorateClass$B([
8183
- property()
8240
+ property({ fallback: 4 })
8184
8241
  ], DropShadowEffect.prototype, "offsetX", 2);
8185
8242
  __decorateClass$B([
8186
- property()
8243
+ property({ fallback: 4 })
8187
8244
  ], DropShadowEffect.prototype, "offsetY", 2);
8188
8245
  __decorateClass$B([
8189
- property()
8246
+ property({ fallback: false })
8190
8247
  ], DropShadowEffect.prototype, "shadowOnly", 2);
8191
8248
  DropShadowEffect = __decorateClass$B([
8192
8249
  customNode("DropShadowEffect")
@@ -8246,7 +8303,7 @@ void main(void) {
8246
8303
  }`
8247
8304
  }));
8248
8305
  __decorateClass$A([
8249
- property()
8306
+ property({ fallback: 5 })
8250
8307
  ], EmbossEffect.prototype, "strength", 2);
8251
8308
  EmbossEffect = __decorateClass$A([
8252
8309
  customNode("EmbossEffect")
@@ -8435,31 +8492,31 @@ void main(void) {
8435
8492
  }`
8436
8493
  }));
8437
8494
  __decorateClass$z([
8438
- property()
8495
+ property({ fallback: 10 })
8439
8496
  ], GlitchEffect.prototype, "slices", 2);
8440
8497
  __decorateClass$z([
8441
- property()
8498
+ property({ fallback: 512 })
8442
8499
  ], GlitchEffect.prototype, "sampleSize", 2);
8443
8500
  __decorateClass$z([
8444
- property()
8501
+ property({ fallback: 100 })
8445
8502
  ], GlitchEffect.prototype, "offset", 2);
8446
8503
  __decorateClass$z([
8447
- property()
8504
+ property({ fallback: 0 })
8448
8505
  ], GlitchEffect.prototype, "direction", 2);
8449
8506
  __decorateClass$z([
8450
- property()
8507
+ property({ fallback: 2 })
8451
8508
  ], GlitchEffect.prototype, "fillMode", 2);
8452
8509
  __decorateClass$z([
8453
- property()
8510
+ property({ fallback: 0 })
8454
8511
  ], GlitchEffect.prototype, "seed", 2);
8455
8512
  __decorateClass$z([
8456
- property()
8513
+ property({ default: () => [2, 2] })
8457
8514
  ], GlitchEffect.prototype, "red", 2);
8458
8515
  __decorateClass$z([
8459
- property()
8516
+ property({ default: () => [-10, 4] })
8460
8517
  ], GlitchEffect.prototype, "green", 2);
8461
8518
  __decorateClass$z([
8462
- property()
8519
+ property({ default: () => [10, -4] })
8463
8520
  ], GlitchEffect.prototype, "blue", 2);
8464
8521
  GlitchEffect = __decorateClass$z([
8465
8522
  customNode("GlitchEffect")
@@ -8652,25 +8709,25 @@ void main(void) {
8652
8709
  }`
8653
8710
  }));
8654
8711
  __decorateClass$y([
8655
- property()
8712
+ property({ fallback: 0 })
8656
8713
  ], GodrayEffect.prototype, "time", 2);
8657
8714
  __decorateClass$y([
8658
- property()
8715
+ property({ fallback: 30 })
8659
8716
  ], GodrayEffect.prototype, "angle", 2);
8660
8717
  __decorateClass$y([
8661
- property()
8718
+ property({ fallback: 0.5 })
8662
8719
  ], GodrayEffect.prototype, "gain", 2);
8663
8720
  __decorateClass$y([
8664
- property()
8721
+ property({ fallback: 2.5 })
8665
8722
  ], GodrayEffect.prototype, "lacunarity", 2);
8666
8723
  __decorateClass$y([
8667
- property()
8724
+ property({ fallback: true })
8668
8725
  ], GodrayEffect.prototype, "parallel", 2);
8669
8726
  __decorateClass$y([
8670
- property()
8727
+ property({ default: () => [0, 0] })
8671
8728
  ], GodrayEffect.prototype, "center", 2);
8672
8729
  __decorateClass$y([
8673
- property()
8730
+ property({ fallback: 1 })
8674
8731
  ], GodrayEffect.prototype, "alpha", 2);
8675
8732
  GodrayEffect = __decorateClass$y([
8676
8733
  customNode("GodrayEffect")
@@ -8774,13 +8831,13 @@ void main() {
8774
8831
  }
8775
8832
  };
8776
8833
  __decorateClass$x([
8777
- property()
8834
+ property({ fallback: 4 })
8778
8835
  ], KawaseBlurEffect.prototype, "strength", 2);
8779
8836
  __decorateClass$x([
8780
- property()
8837
+ property({ fallback: 3 })
8781
8838
  ], KawaseBlurEffect.prototype, "quality", 2);
8782
8839
  __decorateClass$x([
8783
- property()
8840
+ property({ default: () => [1, 1] })
8784
8841
  ], KawaseBlurEffect.prototype, "pixelSize", 2);
8785
8842
  KawaseBlurEffect = __decorateClass$x([
8786
8843
  customNode("KawaseBlurEffect")
@@ -8886,7 +8943,7 @@ __decorateClass$w([
8886
8943
  property({ protected: true })
8887
8944
  ], MaskEffect.prototype, "texture", 2);
8888
8945
  __decorateClass$w([
8889
- property({ default: "" })
8946
+ property({ fallback: "" })
8890
8947
  ], MaskEffect.prototype, "src", 2);
8891
8948
  MaskEffect = __decorateClass$w([
8892
8949
  customNode("MaskEffect")
@@ -8988,25 +9045,25 @@ void main() {
8988
9045
  __publicField$7(OutlineEffect, "MIN_SAMPLES", 1);
8989
9046
  __publicField$7(OutlineEffect, "MAX_SAMPLES", 100);
8990
9047
  __decorateClass$v([
8991
- property()
9048
+ property({ fallback: "#000000ff" })
8992
9049
  ], OutlineEffect.prototype, "color", 2);
8993
9050
  __decorateClass$v([
8994
- property()
9051
+ property({ fallback: 1 })
8995
9052
  ], OutlineEffect.prototype, "width", 2);
8996
9053
  __decorateClass$v([
8997
- property()
9054
+ property({ fallback: "solid" })
8998
9055
  ], OutlineEffect.prototype, "style", 2);
8999
9056
  __decorateClass$v([
9000
9057
  property()
9001
9058
  ], OutlineEffect.prototype, "image", 2);
9002
9059
  __decorateClass$v([
9003
- property()
9060
+ property({ fallback: 1 })
9004
9061
  ], OutlineEffect.prototype, "opacity", 2);
9005
9062
  __decorateClass$v([
9006
- property()
9063
+ property({ fallback: 0.1 })
9007
9064
  ], OutlineEffect.prototype, "quality", 2);
9008
9065
  __decorateClass$v([
9009
- property()
9066
+ property({ fallback: false })
9010
9067
  ], OutlineEffect.prototype, "knockout", 2);
9011
9068
  OutlineEffect = __decorateClass$v([
9012
9069
  customNode("OutlineEffect")
@@ -9077,7 +9134,7 @@ void main(void) {
9077
9134
  }`
9078
9135
  }));
9079
9136
  __decorateClass$u([
9080
- property()
9137
+ property({ fallback: 10 })
9081
9138
  ], PixelateEffect.prototype, "strength", 2);
9082
9139
  PixelateEffect = __decorateClass$u([
9083
9140
  customNode("PixelateEffect")
@@ -9208,13 +9265,13 @@ __decorateClass$t([
9208
9265
  property()
9209
9266
  ], ZoomBlurEffect.prototype, "center", 2);
9210
9267
  __decorateClass$t([
9211
- property()
9268
+ property({ fallback: 20 })
9212
9269
  ], ZoomBlurEffect.prototype, "innerRadius", 2);
9213
9270
  __decorateClass$t([
9214
- property()
9271
+ property({ fallback: -1 })
9215
9272
  ], ZoomBlurEffect.prototype, "radius", 2);
9216
9273
  __decorateClass$t([
9217
- property()
9274
+ property({ fallback: 0.1 })
9218
9275
  ], ZoomBlurEffect.prototype, "strength", 2);
9219
9276
  ZoomBlurEffect = __decorateClass$t([
9220
9277
  customNode("ZoomBlurEffect")
@@ -9956,36 +10013,67 @@ let BaseElement2D = class extends Node2D {
9956
10013
  this._updateOverflow();
9957
10014
  }
9958
10015
  getRect() {
9959
- const { width: w, height: h } = this.size;
10016
+ return this.getGlobalAabb();
10017
+ }
10018
+ _getPointArray() {
10019
+ const { width, height } = this.size;
9960
10020
  const x1 = 0;
9961
10021
  const y1 = 0;
9962
- const x2 = x1 + w;
9963
- const y2 = y1 + h;
9964
- const [a, c, tx, b, d, ty] = this.globalTransform.toArray();
9965
- const points = [
10022
+ const x2 = x1 + width;
10023
+ const y2 = y1 + height;
10024
+ return [
9966
10025
  [x1, y1],
9967
10026
  [x1, y2],
9968
10027
  [x2, y1],
9969
10028
  [x2, y2]
9970
- ].map((p) => {
9971
- return [
9972
- a * p[0] + c * p[1] + tx,
9973
- b * p[0] + d * p[1] + ty
9974
- ];
9975
- });
9976
- const xx = points.map((p) => p[0]);
9977
- const yy = points.map((p) => p[1]);
9978
- const minX = Math.min(...xx);
9979
- const maxX = Math.max(...xx);
9980
- const minY = Math.min(...yy);
9981
- const maxY = Math.max(...yy);
10029
+ ];
10030
+ }
10031
+ getAabb() {
9982
10032
  return new Rect2(
9983
- minX,
9984
- minY,
9985
- maxX - minX,
9986
- maxY - minY
10033
+ this._getPointArray().map((p) => {
10034
+ return this.transform.applyToPoint(p[0], p[1]);
10035
+ })
9987
10036
  );
9988
10037
  }
10038
+ getGlobalAabb() {
10039
+ return new Rect2(
10040
+ this._getPointArray().map((p) => {
10041
+ return this.globalTransform.applyToPoint(p[0], p[1]);
10042
+ })
10043
+ );
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
+ }
9989
10077
  // protected _rectsOverlap(r1: any, r2: any): boolean {
9990
10078
  // return (
9991
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.12",
4
+ "version": "0.7.13",
5
5
  "packageManager": "pnpm@9.15.1",
6
6
  "description": "A JavaScript WebGL rendering engine.",
7
7
  "author": "wxm",