modern-canvas 0.4.15 → 0.4.17

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
@@ -1289,6 +1289,7 @@ class Matrix extends EventEmitter {
1289
1289
  }
1290
1290
  }
1291
1291
  _array = [];
1292
+ dirtyId = 0;
1292
1293
  get length() {
1293
1294
  return this.cols * this.rows;
1294
1295
  }
@@ -1393,6 +1394,7 @@ class Matrix extends EventEmitter {
1393
1394
  return this._operate("*", value, output);
1394
1395
  }
1395
1396
  _onUpdate(_array) {
1397
+ this.dirtyId++;
1396
1398
  }
1397
1399
  toArray(transpose = false) {
1398
1400
  const { cols, rows, _array: array } = this;
@@ -1974,150 +1976,90 @@ class Rect2 {
1974
1976
  }
1975
1977
 
1976
1978
  class Transform2D extends Matrix3 {
1977
- constructor(autoUpdate = true) {
1978
- super();
1979
- this.autoUpdate = autoUpdate;
1980
- }
1981
- _cx = 1;
1982
- _sx = 0;
1983
- _cy = 0;
1984
- _sy = 1;
1985
- _translateX = 0;
1986
- _translateY = 0;
1987
- _translateZ = 1;
1988
- _scaleX = 1;
1989
- _scaleY = 1;
1990
- _skewX = 0;
1991
- _skewY = 0;
1992
- _rotate = 0;
1993
- dirtyId = 0;
1994
- _needsUpdateArray = false;
1995
- _needsUpdateFields = false;
1996
- _onUpdate(array) {
1997
- super._onUpdate(array);
1998
- this._requestUpdateFields();
1999
- }
2000
- _updateSkew() {
2001
- this._cx = Math.cos(this._rotate + this._skewY);
2002
- this._sx = Math.sin(this._rotate + this._skewY);
2003
- this._cy = -Math.sin(this._rotate - this._skewX);
2004
- this._sy = Math.cos(this._rotate - this._skewX);
2005
- }
2006
- _requestUpdateArray() {
2007
- if (this.autoUpdate) {
2008
- this._performUpdateArray();
2009
- } else {
2010
- this._needsUpdateArray = true;
2011
- }
2012
- }
2013
- _requestUpdateFields() {
2014
- if (this.autoUpdate) {
2015
- this._performUpdateFields();
2016
- } else {
2017
- this._needsUpdateFields = true;
2018
- }
2019
- }
2020
- _performUpdateArray() {
2021
- const a = this._cx * this._scaleX;
2022
- const b = this._sx * this._scaleX;
2023
- const c = this._cy * this._scaleY;
2024
- const d = this._sy * this._scaleY;
2025
- const tx = this._translateX;
2026
- const ty = this._translateY;
2027
- const tz = this._translateZ;
2028
- const array = this._array;
2029
- this._array = [
2030
- a,
2031
- c,
2032
- tx,
2033
- b,
2034
- d,
2035
- ty,
2036
- array[6],
2037
- array[7],
2038
- tz
2039
- ];
2040
- this.dirtyId++;
2041
- }
2042
- _performUpdateFields() {
2043
- const {
2044
- a,
2045
- c,
2046
- tx,
2047
- b,
2048
- d,
2049
- ty,
2050
- tz
2051
- } = this.toObject();
2052
- const skewX = -Math.atan2(-c, d);
2053
- const skewY = Math.atan2(b, a);
2054
- const delta = Math.abs(skewX + skewY);
2055
- if (delta < 1e-5 || Math.abs(PI_2 - delta) < 1e-5) {
2056
- this._rotate = skewY;
2057
- this._skewX = this._skewY = 0;
2058
- } else {
2059
- this._rotate = 0;
2060
- this._skewX = skewX;
2061
- this._skewY = skewY;
2062
- }
2063
- this._scaleX = Math.sqrt(a * a + b * b);
2064
- this._scaleY = Math.sqrt(c * c + d * d);
2065
- this._translateX = tx;
2066
- this._translateY = ty;
2067
- this._translateZ = tz;
2068
- this.dirtyId++;
2069
- }
2070
- skew(x, y) {
2071
- this._skewX = x;
2072
- this._skewY = y;
2073
- this._updateSkew();
2074
- this._requestUpdateArray();
2075
- return this;
1979
+ static _t2d = /* @__PURE__ */ new Transform2D();
1980
+ premultiply(t2d) {
1981
+ return t2d.multiply(this, this);
2076
1982
  }
2077
1983
  skewX(x) {
2078
- return this.skew(x, this._skewY);
1984
+ return this.skew(x, 1);
2079
1985
  }
2080
1986
  skewY(y) {
2081
- return this.skew(this._skewX, y);
1987
+ return this.skew(1, y);
2082
1988
  }
2083
- translate(x, y, z = 1) {
2084
- this._translateX = x;
2085
- this._translateY = y;
2086
- this._translateZ = z;
2087
- this._requestUpdateArray();
1989
+ skew(x, y) {
1990
+ return this.premultiply(Transform2D._t2d.makeSkew(x, y));
1991
+ }
1992
+ makeSkew(x, y) {
1993
+ const cx = Math.cos(y);
1994
+ const sx = Math.sin(y);
1995
+ const cy = -Math.sin(-x);
1996
+ const sy = Math.cos(-x);
1997
+ this.set([
1998
+ cx,
1999
+ cy,
2000
+ 0,
2001
+ sx,
2002
+ sy,
2003
+ 0,
2004
+ 0,
2005
+ 0,
2006
+ 1
2007
+ ]);
2088
2008
  return this;
2089
2009
  }
2090
2010
  translateX(x) {
2091
- return this.translate(x, this._translateY);
2011
+ return this.translate(x, 0);
2092
2012
  }
2093
2013
  translateY(y) {
2094
- return this.translate(this._translateX, y);
2014
+ return this.translate(0, y);
2095
2015
  }
2096
2016
  translateZ(z) {
2097
- return this.translate(this._translateX, this._translateY, z);
2017
+ return this.translate(0, 0, z);
2098
2018
  }
2099
2019
  translate3d(x, y, z) {
2100
2020
  return this.translate(x, y, z);
2101
2021
  }
2102
- scale(x, y, _z = 1) {
2103
- this._scaleX = x;
2104
- this._scaleY = y;
2105
- this._requestUpdateArray();
2022
+ translate(x, y, z = 0) {
2023
+ return this.premultiply(Transform2D._t2d.makeTranslation(x, y, z));
2024
+ }
2025
+ makeTranslation(x, y, _z = 0) {
2026
+ this.set([
2027
+ 1,
2028
+ 0,
2029
+ x,
2030
+ 0,
2031
+ 1,
2032
+ y,
2033
+ 0,
2034
+ 0,
2035
+ 1
2036
+ ]);
2106
2037
  return this;
2107
2038
  }
2108
2039
  scaleX(x) {
2109
- return this.scale(x, this._scaleY);
2040
+ return this.scale(x, 1);
2110
2041
  }
2111
2042
  scaleY(y) {
2112
- return this.scale(this._scaleX, y);
2043
+ return this.scale(1, y);
2113
2044
  }
2114
- scale3d(x, y, z) {
2045
+ scale3d(x, y, z = 1) {
2115
2046
  return this.scale(x, y, z);
2116
2047
  }
2117
- rotate(rad) {
2118
- this._rotate = rad;
2119
- this._updateSkew();
2120
- this._requestUpdateArray();
2048
+ scale(x, y, z = 1) {
2049
+ return this.premultiply(Transform2D._t2d.makeScale(x, y, z));
2050
+ }
2051
+ makeScale(x, y, z = 1) {
2052
+ this.set([
2053
+ x,
2054
+ 0,
2055
+ 0,
2056
+ 0,
2057
+ y,
2058
+ 0,
2059
+ 0,
2060
+ 0,
2061
+ z
2062
+ ]);
2121
2063
  return this;
2122
2064
  }
2123
2065
  rotateX(x) {
@@ -2129,6 +2071,9 @@ class Transform2D extends Matrix3 {
2129
2071
  rotateZ(z) {
2130
2072
  return this.rotate(z);
2131
2073
  }
2074
+ rotate(rad) {
2075
+ return this.premultiply(Transform2D._t2d.makeRotation(rad));
2076
+ }
2132
2077
  rotate3d(x, y, z, rad) {
2133
2078
  const [rx, ry, rz] = this._rotate3d(x, y, z, rad);
2134
2079
  rx && this.rotateX(rx);
@@ -2162,6 +2107,22 @@ class Transform2D extends Matrix3 {
2162
2107
  return [rotateX, rotateY, rotateZ];
2163
2108
  }
2164
2109
  }
2110
+ makeRotation(theta) {
2111
+ const c = Math.cos(theta);
2112
+ const s = Math.sin(theta);
2113
+ this.set([
2114
+ c,
2115
+ -s,
2116
+ 0,
2117
+ s,
2118
+ c,
2119
+ 0,
2120
+ 0,
2121
+ 0,
2122
+ 1
2123
+ ]);
2124
+ return this;
2125
+ }
2165
2126
  applyToPoint(x, y) {
2166
2127
  const { a, c, tx, b, d, ty } = this.toObject();
2167
2128
  return [
@@ -2172,20 +2133,6 @@ class Transform2D extends Matrix3 {
2172
2133
  inverse() {
2173
2134
  return this.clone().invert();
2174
2135
  }
2175
- update() {
2176
- let updated = false;
2177
- if (this._needsUpdateArray) {
2178
- this._needsUpdateArray = false;
2179
- this._performUpdateArray();
2180
- updated = true;
2181
- }
2182
- if (this._needsUpdateFields) {
2183
- this._needsUpdateFields = false;
2184
- this._performUpdateFields();
2185
- updated = true;
2186
- }
2187
- return updated;
2188
- }
2189
2136
  isIdentity() {
2190
2137
  const { a, b, c, d, tx, ty } = this.toObject();
2191
2138
  return a === 1 && b === 0 && c === 0 && d === 1 && tx === 0 && ty === 0;
@@ -4383,58 +4330,56 @@ function parseCSSFilter(filter) {
4383
4330
  return m;
4384
4331
  }
4385
4332
 
4386
- function parseCSSTransform(transform, width, height) {
4387
- const t2d = new Transform2D(false);
4333
+ function parseCSSTransform(transform, width, height, output = new Transform2D()) {
4388
4334
  transform = !transform || transform === "none" ? "" : transform;
4389
4335
  parseCssFunctions(transform, { width, height }).forEach(({ name, args }) => {
4390
4336
  const values = args.map((arg) => arg.normalizedIntValue);
4391
- const _temp = new Transform2D();
4392
4337
  switch (name) {
4393
4338
  case "translate":
4394
- _temp.translate(values[0] * width, (values[1] ?? values[0]) * height);
4339
+ output.translate(values[0] * width, (values[1] ?? values[0]) * height);
4395
4340
  break;
4396
4341
  case "translateX":
4397
- _temp.translateX(values[0] * width);
4342
+ output.translateX(values[0] * width);
4398
4343
  break;
4399
4344
  case "translateY":
4400
- _temp.translateY(values[0] * height);
4345
+ output.translateY(values[0] * height);
4401
4346
  break;
4402
4347
  case "translateZ":
4403
- _temp.translateZ(values[0]);
4348
+ output.translateZ(values[0]);
4404
4349
  break;
4405
4350
  case "translate3d":
4406
- _temp.translate3d(
4351
+ output.translate3d(
4407
4352
  values[0] * width,
4408
4353
  (values[1] ?? values[0]) * height,
4409
4354
  values[2] ?? values[1] ?? values[0]
4410
4355
  );
4411
4356
  break;
4412
4357
  case "scale":
4413
- _temp.scale(values[0], values[1] ?? values[0]);
4358
+ output.scale(values[0], values[1] ?? values[0]);
4414
4359
  break;
4415
4360
  case "scaleX":
4416
- _temp.scaleX(values[0]);
4361
+ output.scaleX(values[0]);
4417
4362
  break;
4418
4363
  case "scaleY":
4419
- _temp.scaleY(values[0]);
4364
+ output.scaleY(values[0]);
4420
4365
  break;
4421
4366
  case "scale3d":
4422
- _temp.scale3d(values[0], values[1] ?? values[0], values[2] ?? values[1] ?? values[0]);
4367
+ output.scale3d(values[0], values[1] ?? values[0], values[2] ?? values[1] ?? values[0]);
4423
4368
  break;
4424
4369
  case "rotate":
4425
- _temp.rotate(values[0] * PI_2);
4370
+ output.rotate(values[0] * PI_2);
4426
4371
  break;
4427
4372
  case "rotateX":
4428
- _temp.rotateX(values[0] * PI_2);
4373
+ output.rotateX(values[0] * PI_2);
4429
4374
  break;
4430
4375
  case "rotateY":
4431
- _temp.rotateY(values[0] * PI_2);
4376
+ output.rotateY(values[0] * PI_2);
4432
4377
  break;
4433
4378
  case "rotateZ":
4434
- _temp.rotateZ(values[0] * PI_2);
4379
+ output.rotateZ(values[0] * PI_2);
4435
4380
  break;
4436
4381
  case "rotate3d":
4437
- _temp.rotate3d(
4382
+ output.rotate3d(
4438
4383
  values[0] * PI_2,
4439
4384
  (values[1] ?? values[0]) * PI_2,
4440
4385
  (values[2] ?? values[1] ?? values[0]) * PI_2,
@@ -4442,22 +4387,20 @@ function parseCSSTransform(transform, width, height) {
4442
4387
  );
4443
4388
  break;
4444
4389
  case "skew":
4445
- _temp.skew(values[0], values[0] ?? values[1]);
4390
+ output.skew(values[0], values[0] ?? values[1]);
4446
4391
  break;
4447
4392
  case "skewX":
4448
- _temp.skewX(values[0]);
4393
+ output.skewX(values[0]);
4449
4394
  break;
4450
4395
  case "skewY":
4451
- _temp.skewY(values[0]);
4396
+ output.skewY(values[0]);
4452
4397
  break;
4453
4398
  case "matrix":
4454
- _temp.set(values);
4399
+ output.set(values);
4455
4400
  break;
4456
4401
  }
4457
- t2d.multiply(_temp);
4458
4402
  });
4459
- t2d.update();
4460
- return t2d;
4403
+ return output;
4461
4404
  }
4462
4405
 
4463
4406
  function parseCSSTransformOrigin(transformOrigin) {
@@ -5817,6 +5760,22 @@ let Node = class extends CoreObject {
5817
5760
  const tree = this._tree;
5818
5761
  const canRender = this.canRender();
5819
5762
  const canProcess = this.canProcess();
5763
+ const childrenInBefore = [];
5764
+ const childrenInAfter = [];
5765
+ for (let len = this._children.length, i = 0; i < len; i++) {
5766
+ const child = this._children[i];
5767
+ switch (child.processSortMode) {
5768
+ case "default":
5769
+ childrenInAfter.push(child);
5770
+ break;
5771
+ case "parent_before":
5772
+ childrenInBefore.push(child);
5773
+ break;
5774
+ }
5775
+ }
5776
+ childrenInBefore.forEach((child) => {
5777
+ child.emit("process", delta);
5778
+ });
5820
5779
  if (canProcess) {
5821
5780
  tree?.emit("nodeProcessing", this);
5822
5781
  this.emit("processing", delta);
@@ -5839,9 +5798,9 @@ let Node = class extends CoreObject {
5839
5798
  this.removeChild(mask);
5840
5799
  }
5841
5800
  }
5842
- for (let len = this._children.length, i = 0; i < len; i++) {
5843
- this._children[i].emit("process", delta);
5844
- }
5801
+ childrenInAfter.forEach((child) => {
5802
+ child.emit("process", delta);
5803
+ });
5845
5804
  if (canRender) {
5846
5805
  tree.renderStack.currentCall = oldRenderCall;
5847
5806
  }
@@ -6109,6 +6068,9 @@ __decorateClass$I([
6109
6068
  __decorateClass$I([
6110
6069
  property({ default: "inherit" })
6111
6070
  ], Node.prototype, "processMode", 2);
6071
+ __decorateClass$I([
6072
+ property({ default: "default" })
6073
+ ], Node.prototype, "processSortMode", 2);
6112
6074
  __decorateClass$I([
6113
6075
  property({ default: "inherit" })
6114
6076
  ], Node.prototype, "renderMode", 2);
@@ -8664,7 +8626,7 @@ let Node2D = class extends CanvasItem {
8664
8626
  rotation = 0;
8665
8627
  scale = new Vector2(1, 1);
8666
8628
  skew = new Vector2();
8667
- transform = new Transform2D(false);
8629
+ transform = new Transform2D();
8668
8630
  globalPosition = new Vector2();
8669
8631
  globalRotation = 0;
8670
8632
  globalScale = new Vector2();
@@ -8676,7 +8638,7 @@ let Node2D = class extends CanvasItem {
8676
8638
  this.setProperties(properties).append(nodes);
8677
8639
  }
8678
8640
  _updateTransform() {
8679
- this.transform.identity().scale(this.scale.x, this.scale.y).skew(this.skew.x, this.skew.y).translate(this.position.x, this.position.y).rotate(this.rotation).update();
8641
+ this.transform.identity().scale(this.scale.x, this.scale.y).skew(this.skew.x, this.skew.y).rotate(this.rotation).translate(this.position.x, this.position.y);
8680
8642
  }
8681
8643
  _updateGlobalTransform() {
8682
8644
  const parent = this.getParent();
@@ -8879,17 +8841,13 @@ let BaseElement2D = class extends Node2D {
8879
8841
  this.requestRedraw();
8880
8842
  }
8881
8843
  _updateTransform() {
8882
- super._updateTransform();
8883
8844
  const { width, height } = this.size;
8884
- const cssTransform = parseCSSTransform(this.style.transform, width, height);
8885
- cssTransform.multiply(this.transform, this.transform);
8886
- const t3dT2dArr = this.transform.toArray();
8887
8845
  const [originX, originY] = parseCSSTransformOrigin(this.style.transformOrigin);
8888
8846
  const offsetX = originX * width;
8889
8847
  const offsetY = originY * height;
8890
- t3dT2dArr[2] += t3dT2dArr[0] * -offsetX + t3dT2dArr[1] * -offsetY + offsetX;
8891
- t3dT2dArr[5] += t3dT2dArr[3] * -offsetX + t3dT2dArr[4] * -offsetY + offsetY;
8892
- this.transform.set(t3dT2dArr);
8848
+ this.transform.identity().translate(-offsetX, -offsetY).scale(this.scale.x, this.scale.y).skew(this.skew.x, this.skew.y).rotate(this.rotation);
8849
+ parseCSSTransform(this.style.transform, width, height, this.transform);
8850
+ this.transform.translate(offsetX + this.position.x, offsetY + this.position.y);
8893
8851
  }
8894
8852
  _updateGlobalTransform() {
8895
8853
  super._updateGlobalTransform();
@@ -10170,19 +10128,14 @@ let Animation = class extends TimelineNode {
10170
10128
  _stoped = false;
10171
10129
  constructor(properties, children = []) {
10172
10130
  super();
10173
- this._process = this._process.bind(this);
10174
10131
  this.setProperties(properties).append(children);
10175
10132
  }
10176
- _treeEnter(tree) {
10177
- tree.timeline.on("updateCurrentTime", this._process);
10133
+ _treeEnter(_tree) {
10178
10134
  this._updateCachedProps();
10179
10135
  }
10180
- _treeExit(oldTree) {
10181
- oldTree.timeline.on("updateCurrentTime", this._process);
10136
+ _treeExit(_oldTree) {
10182
10137
  this.cancel();
10183
10138
  }
10184
- _onProcess() {
10185
- }
10186
10139
  _process() {
10187
10140
  if (this.canProcess()) {
10188
10141
  this.commitStyles();
@@ -10441,6 +10394,7 @@ Animation = __decorateClass$d([
10441
10394
  customNode("Animation", {
10442
10395
  renderMode: "disabled",
10443
10396
  processMode: "pausable",
10397
+ processSortMode: "parent_before",
10444
10398
  duration: 2e3
10445
10399
  })
10446
10400
  ], Animation);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.4.15",
4
+ "version": "0.4.17",
5
5
  "packageManager": "pnpm@9.15.1",
6
6
  "description": "A JavaScript WebGL rendering engine.",
7
7
  "author": "wxm",