modern-canvas 0.4.16 → 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.cjs +106 -167
- package/dist/index.d.cts +13 -30
- package/dist/index.d.mts +13 -30
- package/dist/index.d.ts +13 -30
- package/dist/index.js +38 -38
- package/dist/index.mjs +106 -167
- package/package.json +1 -1
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
|
-
|
|
1978
|
-
|
|
1979
|
-
this
|
|
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,
|
|
1984
|
+
return this.skew(x, 1);
|
|
2079
1985
|
}
|
|
2080
1986
|
skewY(y) {
|
|
2081
|
-
return this.skew(
|
|
1987
|
+
return this.skew(1, y);
|
|
2082
1988
|
}
|
|
2083
|
-
|
|
2084
|
-
this.
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
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,
|
|
2011
|
+
return this.translate(x, 0);
|
|
2092
2012
|
}
|
|
2093
2013
|
translateY(y) {
|
|
2094
|
-
return this.translate(
|
|
2014
|
+
return this.translate(0, y);
|
|
2095
2015
|
}
|
|
2096
2016
|
translateZ(z) {
|
|
2097
|
-
return this.translate(
|
|
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
|
-
|
|
2103
|
-
this.
|
|
2104
|
-
|
|
2105
|
-
|
|
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,
|
|
2040
|
+
return this.scale(x, 1);
|
|
2110
2041
|
}
|
|
2111
2042
|
scaleY(y) {
|
|
2112
|
-
return this.scale(
|
|
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
|
-
|
|
2118
|
-
this.
|
|
2119
|
-
|
|
2120
|
-
|
|
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
|
-
|
|
4339
|
+
output.translate(values[0] * width, (values[1] ?? values[0]) * height);
|
|
4395
4340
|
break;
|
|
4396
4341
|
case "translateX":
|
|
4397
|
-
|
|
4342
|
+
output.translateX(values[0] * width);
|
|
4398
4343
|
break;
|
|
4399
4344
|
case "translateY":
|
|
4400
|
-
|
|
4345
|
+
output.translateY(values[0] * height);
|
|
4401
4346
|
break;
|
|
4402
4347
|
case "translateZ":
|
|
4403
|
-
|
|
4348
|
+
output.translateZ(values[0]);
|
|
4404
4349
|
break;
|
|
4405
4350
|
case "translate3d":
|
|
4406
|
-
|
|
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
|
-
|
|
4358
|
+
output.scale(values[0], values[1] ?? values[0]);
|
|
4414
4359
|
break;
|
|
4415
4360
|
case "scaleX":
|
|
4416
|
-
|
|
4361
|
+
output.scaleX(values[0]);
|
|
4417
4362
|
break;
|
|
4418
4363
|
case "scaleY":
|
|
4419
|
-
|
|
4364
|
+
output.scaleY(values[0]);
|
|
4420
4365
|
break;
|
|
4421
4366
|
case "scale3d":
|
|
4422
|
-
|
|
4367
|
+
output.scale3d(values[0], values[1] ?? values[0], values[2] ?? values[1] ?? values[0]);
|
|
4423
4368
|
break;
|
|
4424
4369
|
case "rotate":
|
|
4425
|
-
|
|
4370
|
+
output.rotate(values[0] * PI_2);
|
|
4426
4371
|
break;
|
|
4427
4372
|
case "rotateX":
|
|
4428
|
-
|
|
4373
|
+
output.rotateX(values[0] * PI_2);
|
|
4429
4374
|
break;
|
|
4430
4375
|
case "rotateY":
|
|
4431
|
-
|
|
4376
|
+
output.rotateY(values[0] * PI_2);
|
|
4432
4377
|
break;
|
|
4433
4378
|
case "rotateZ":
|
|
4434
|
-
|
|
4379
|
+
output.rotateZ(values[0] * PI_2);
|
|
4435
4380
|
break;
|
|
4436
4381
|
case "rotate3d":
|
|
4437
|
-
|
|
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
|
-
|
|
4390
|
+
output.skew(values[0], values[0] ?? values[1]);
|
|
4446
4391
|
break;
|
|
4447
4392
|
case "skewX":
|
|
4448
|
-
|
|
4393
|
+
output.skewX(values[0]);
|
|
4449
4394
|
break;
|
|
4450
4395
|
case "skewY":
|
|
4451
|
-
|
|
4396
|
+
output.skewY(values[0]);
|
|
4452
4397
|
break;
|
|
4453
4398
|
case "matrix":
|
|
4454
|
-
|
|
4399
|
+
output.set(values);
|
|
4455
4400
|
break;
|
|
4456
4401
|
}
|
|
4457
|
-
t2d.multiply(_temp);
|
|
4458
4402
|
});
|
|
4459
|
-
|
|
4460
|
-
return t2d;
|
|
4403
|
+
return output;
|
|
4461
4404
|
}
|
|
4462
4405
|
|
|
4463
4406
|
function parseCSSTransformOrigin(transformOrigin) {
|
|
@@ -8683,7 +8626,7 @@ let Node2D = class extends CanvasItem {
|
|
|
8683
8626
|
rotation = 0;
|
|
8684
8627
|
scale = new Vector2(1, 1);
|
|
8685
8628
|
skew = new Vector2();
|
|
8686
|
-
transform = new Transform2D(
|
|
8629
|
+
transform = new Transform2D();
|
|
8687
8630
|
globalPosition = new Vector2();
|
|
8688
8631
|
globalRotation = 0;
|
|
8689
8632
|
globalScale = new Vector2();
|
|
@@ -8695,7 +8638,7 @@ let Node2D = class extends CanvasItem {
|
|
|
8695
8638
|
this.setProperties(properties).append(nodes);
|
|
8696
8639
|
}
|
|
8697
8640
|
_updateTransform() {
|
|
8698
|
-
this.transform.identity().scale(this.scale.x, this.scale.y).skew(this.skew.x, this.skew.y).translate(this.position.x, this.position.y)
|
|
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);
|
|
8699
8642
|
}
|
|
8700
8643
|
_updateGlobalTransform() {
|
|
8701
8644
|
const parent = this.getParent();
|
|
@@ -8898,17 +8841,13 @@ let BaseElement2D = class extends Node2D {
|
|
|
8898
8841
|
this.requestRedraw();
|
|
8899
8842
|
}
|
|
8900
8843
|
_updateTransform() {
|
|
8901
|
-
super._updateTransform();
|
|
8902
8844
|
const { width, height } = this.size;
|
|
8903
|
-
const cssTransform = parseCSSTransform(this.style.transform, width, height);
|
|
8904
|
-
cssTransform.multiply(this.transform, this.transform);
|
|
8905
|
-
const t3dT2dArr = this.transform.toArray();
|
|
8906
8845
|
const [originX, originY] = parseCSSTransformOrigin(this.style.transformOrigin);
|
|
8907
8846
|
const offsetX = originX * width;
|
|
8908
8847
|
const offsetY = originY * height;
|
|
8909
|
-
|
|
8910
|
-
|
|
8911
|
-
this.transform.
|
|
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);
|
|
8912
8851
|
}
|
|
8913
8852
|
_updateGlobalTransform() {
|
|
8914
8853
|
super._updateGlobalTransform();
|