modern-canvas 0.7.13 → 0.7.14
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 +82 -54
- package/dist/index.d.cts +24 -19
- package/dist/index.d.mts +24 -19
- package/dist/index.d.ts +24 -19
- package/dist/index.js +10 -10
- package/dist/index.mjs +82 -54
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1757,7 +1757,17 @@ class Matrix3 extends Matrix {
|
|
|
1757
1757
|
const t13 = n23 * n12 - n22 * n13;
|
|
1758
1758
|
const det = n11 * t11 + n21 * t12 + n31 * t13;
|
|
1759
1759
|
if (det === 0) {
|
|
1760
|
-
return this.set([
|
|
1760
|
+
return this.set([
|
|
1761
|
+
0,
|
|
1762
|
+
0,
|
|
1763
|
+
0,
|
|
1764
|
+
0,
|
|
1765
|
+
0,
|
|
1766
|
+
0,
|
|
1767
|
+
0,
|
|
1768
|
+
0,
|
|
1769
|
+
0
|
|
1770
|
+
]);
|
|
1761
1771
|
}
|
|
1762
1772
|
const detInv = 1 / det;
|
|
1763
1773
|
return this.set([
|
|
@@ -1950,8 +1960,8 @@ class Rect2 {
|
|
|
1950
1960
|
position.set(arg.position);
|
|
1951
1961
|
size.set(arg.size);
|
|
1952
1962
|
} else {
|
|
1953
|
-
const xx = arg.map((p) => p
|
|
1954
|
-
const yy = arg.map((p) => p
|
|
1963
|
+
const xx = arg.map((p) => p.x);
|
|
1964
|
+
const yy = arg.map((p) => p.y);
|
|
1955
1965
|
const minX = Math.min(...xx);
|
|
1956
1966
|
const maxX = Math.max(...xx);
|
|
1957
1967
|
const minY = Math.min(...yy);
|
|
@@ -2149,13 +2159,6 @@ class Transform2D extends Matrix3 {
|
|
|
2149
2159
|
]);
|
|
2150
2160
|
return this;
|
|
2151
2161
|
}
|
|
2152
|
-
applyToPoint(x, y) {
|
|
2153
|
-
const { a, c, tx, b, d, ty } = this.toObject();
|
|
2154
|
-
return [
|
|
2155
|
-
a * x + c * y + tx,
|
|
2156
|
-
b * x + d * y + ty
|
|
2157
|
-
];
|
|
2158
|
-
}
|
|
2159
2162
|
decompose(pivot = { x: 0, y: 0 }, output = {
|
|
2160
2163
|
position: { x: 0, y: 0 },
|
|
2161
2164
|
scale: { x: 0, y: 0 },
|
|
@@ -2180,9 +2183,28 @@ class Transform2D extends Matrix3 {
|
|
|
2180
2183
|
output.position.y = ty + (pivot.x * b + pivot.y * d);
|
|
2181
2184
|
return output;
|
|
2182
2185
|
}
|
|
2186
|
+
apply(pos, newPos) {
|
|
2187
|
+
newPos = newPos || new Vector2();
|
|
2188
|
+
const { a, c, tx, b, d, ty } = this.toObject();
|
|
2189
|
+
const x = pos.x;
|
|
2190
|
+
const y = pos.y;
|
|
2191
|
+
newPos.x = a * x + c * y + tx;
|
|
2192
|
+
newPos.y = b * x + d * y + ty;
|
|
2193
|
+
return newPos;
|
|
2194
|
+
}
|
|
2183
2195
|
inverse() {
|
|
2184
2196
|
return this.clone().invert();
|
|
2185
2197
|
}
|
|
2198
|
+
applyInverse(pos, newPos) {
|
|
2199
|
+
newPos = newPos || new Vector2();
|
|
2200
|
+
const { a, b, c, d, tx, ty } = this.toObject();
|
|
2201
|
+
const id = 1 / (a * d + c * -b);
|
|
2202
|
+
const x = pos.x;
|
|
2203
|
+
const y = pos.y;
|
|
2204
|
+
newPos.x = d * id * x + -c * id * y + (ty * c - tx * d) * id;
|
|
2205
|
+
newPos.y = a * id * y + -b * id * x + (-ty * a + tx * b) * id;
|
|
2206
|
+
return newPos;
|
|
2207
|
+
}
|
|
2186
2208
|
isIdentity() {
|
|
2187
2209
|
const { a, b, c, d, tx, ty } = this.toObject();
|
|
2188
2210
|
return a === 1 && b === 0 && c === 0 && d === 1 && tx === 0 && ty === 0;
|
|
@@ -5630,7 +5652,7 @@ class CanvasContext extends Path2D {
|
|
|
5630
5652
|
}
|
|
5631
5653
|
buildUvs(start, vertices, uvs, texture, uvTransform) {
|
|
5632
5654
|
if (texture) {
|
|
5633
|
-
const _uvTransform = uvTransform ? typeof uvTransform === "function" ? uvTransform : (x, y) => uvTransform.
|
|
5655
|
+
const _uvTransform = uvTransform ? typeof uvTransform === "function" ? uvTransform : (x, y) => uvTransform.apply({ x, y }).toArray() : uvTransform;
|
|
5634
5656
|
const w = texture.width;
|
|
5635
5657
|
const h = texture.height;
|
|
5636
5658
|
for (let len = vertices.length, i = start; i < len; i += 2) {
|
|
@@ -6537,7 +6559,7 @@ let Viewport = class extends Node {
|
|
|
6537
6559
|
this.projection.flipY(flipY);
|
|
6538
6560
|
}
|
|
6539
6561
|
projection = new Projection2D();
|
|
6540
|
-
|
|
6562
|
+
worldTransform = new Transform2D();
|
|
6541
6563
|
_framebufferIndex = 0;
|
|
6542
6564
|
_framebuffers = [
|
|
6543
6565
|
{ texture: new ViewportTexture(), needsUpload: false },
|
|
@@ -6652,7 +6674,7 @@ let Viewport = class extends Node {
|
|
|
6652
6674
|
render(renderer, next) {
|
|
6653
6675
|
const oldViewport = this._tree?.getCurrentViewport();
|
|
6654
6676
|
renderer.program.uniforms.projectionMatrix = this.projection.toArray(true);
|
|
6655
|
-
renderer.program.uniforms.worldTransformMatrix = this.
|
|
6677
|
+
renderer.program.uniforms.worldTransformMatrix = this.worldTransform.toArray(true);
|
|
6656
6678
|
this.activate(renderer);
|
|
6657
6679
|
renderer.clear();
|
|
6658
6680
|
super.render(renderer, next);
|
|
@@ -6667,6 +6689,12 @@ let Viewport = class extends Node {
|
|
|
6667
6689
|
getRect() {
|
|
6668
6690
|
return new Rect2(this.x, this.y, this.width, this.height);
|
|
6669
6691
|
}
|
|
6692
|
+
toGlobal(worldPos, newPos) {
|
|
6693
|
+
return this.worldTransform.applyInverse(worldPos, newPos);
|
|
6694
|
+
}
|
|
6695
|
+
toWorld(globalPos, newPos) {
|
|
6696
|
+
return this.worldTransform.apply(globalPos, newPos);
|
|
6697
|
+
}
|
|
6670
6698
|
};
|
|
6671
6699
|
__decorateClass$P([
|
|
6672
6700
|
property({ fallback: 0 })
|
|
@@ -7279,6 +7307,12 @@ let Node2D = class extends CanvasItem {
|
|
|
7279
7307
|
this.requestRelayout();
|
|
7280
7308
|
}
|
|
7281
7309
|
}
|
|
7310
|
+
toLocal(globalPos, newPos) {
|
|
7311
|
+
return this.globalTransform.applyInverse(globalPos, newPos);
|
|
7312
|
+
}
|
|
7313
|
+
toGlobal(localPos, newPos) {
|
|
7314
|
+
return this.globalTransform.apply(localPos, newPos);
|
|
7315
|
+
}
|
|
7282
7316
|
};
|
|
7283
7317
|
__decorateClass$J([
|
|
7284
7318
|
property({ protected: true, fallback: 0 })
|
|
@@ -7301,7 +7335,7 @@ var __decorateClass$I = (decorators, target, key, kind) => {
|
|
|
7301
7335
|
return result;
|
|
7302
7336
|
};
|
|
7303
7337
|
let Camera2D = class extends Node2D {
|
|
7304
|
-
zoom = new Vector2(1, 1).on("update", () => this.
|
|
7338
|
+
zoom = new Vector2(1, 1).on("update", () => this.updateWorldTransform());
|
|
7305
7339
|
maxZoom = new Vector2(6, 6);
|
|
7306
7340
|
minZoom = new Vector2(0.1, 0.1);
|
|
7307
7341
|
_screenOffset = { x: 0, y: 0 };
|
|
@@ -7388,14 +7422,14 @@ let Camera2D = class extends Node2D {
|
|
|
7388
7422
|
}
|
|
7389
7423
|
updateTransform() {
|
|
7390
7424
|
super.updateTransform();
|
|
7391
|
-
this.
|
|
7425
|
+
this.updateWorldTransform();
|
|
7392
7426
|
}
|
|
7393
|
-
|
|
7427
|
+
updateWorldTransform() {
|
|
7394
7428
|
const viewport = this.getViewport();
|
|
7395
7429
|
if (!viewport)
|
|
7396
7430
|
return;
|
|
7397
|
-
viewport.
|
|
7398
|
-
this.emit("
|
|
7431
|
+
viewport.worldTransform.identity().scale(this.zoom.x, this.zoom.y).translate(this.position.x, this.position.y);
|
|
7432
|
+
this.emit("updateWorldTransform");
|
|
7399
7433
|
}
|
|
7400
7434
|
};
|
|
7401
7435
|
__decorateClass$I([
|
|
@@ -9757,7 +9791,10 @@ class BaseElement2DText extends CoreObject {
|
|
|
9757
9791
|
break;
|
|
9758
9792
|
}
|
|
9759
9793
|
}
|
|
9760
|
-
|
|
9794
|
+
setContent(content) {
|
|
9795
|
+
this.content = normalizeTextContent(content);
|
|
9796
|
+
}
|
|
9797
|
+
measure() {
|
|
9761
9798
|
this.base.style = {
|
|
9762
9799
|
justifyContent: "center",
|
|
9763
9800
|
alignItems: "center",
|
|
@@ -9765,12 +9802,6 @@ class BaseElement2DText extends CoreObject {
|
|
|
9765
9802
|
...this.parent.style.toJSON()
|
|
9766
9803
|
};
|
|
9767
9804
|
this.base.requestUpdate();
|
|
9768
|
-
}
|
|
9769
|
-
setContent(content) {
|
|
9770
|
-
this.content = normalizeTextContent(content);
|
|
9771
|
-
}
|
|
9772
|
-
measure() {
|
|
9773
|
-
this._updateText();
|
|
9774
9805
|
return this.base.measure();
|
|
9775
9806
|
}
|
|
9776
9807
|
updateMeasure() {
|
|
@@ -10022,37 +10053,35 @@ let BaseElement2D = class extends Node2D {
|
|
|
10022
10053
|
const x2 = x1 + width;
|
|
10023
10054
|
const y2 = y1 + height;
|
|
10024
10055
|
return [
|
|
10025
|
-
|
|
10026
|
-
|
|
10027
|
-
|
|
10028
|
-
|
|
10056
|
+
{ x: x1, y: y1 },
|
|
10057
|
+
{ x: x1, y: y2 },
|
|
10058
|
+
{ x: x2, y: y1 },
|
|
10059
|
+
{ x: x2, y: y2 }
|
|
10029
10060
|
];
|
|
10030
10061
|
}
|
|
10031
10062
|
getAabb() {
|
|
10032
10063
|
return new Rect2(
|
|
10033
10064
|
this._getPointArray().map((p) => {
|
|
10034
|
-
return this.transform.
|
|
10065
|
+
return this.transform.apply(p);
|
|
10035
10066
|
})
|
|
10036
10067
|
);
|
|
10037
10068
|
}
|
|
10038
10069
|
getGlobalAabb() {
|
|
10039
10070
|
return new Rect2(
|
|
10040
10071
|
this._getPointArray().map((p) => {
|
|
10041
|
-
return this.globalTransform.
|
|
10072
|
+
return this.globalTransform.apply(p);
|
|
10042
10073
|
})
|
|
10043
10074
|
);
|
|
10044
10075
|
}
|
|
10045
10076
|
getObb() {
|
|
10046
10077
|
const origin = this.getTransformOrigin();
|
|
10047
|
-
const _origin = this.transform.
|
|
10048
|
-
const offset = [_origin[0] - origin.x, _origin[1] - origin.y];
|
|
10078
|
+
const _origin = this.transform.apply(origin).sub(origin);
|
|
10049
10079
|
return {
|
|
10050
10080
|
rect: new Rect2(
|
|
10051
10081
|
this._getPointArray().map((p) => {
|
|
10052
|
-
|
|
10053
|
-
|
|
10054
|
-
|
|
10055
|
-
];
|
|
10082
|
+
p.x += _origin.x;
|
|
10083
|
+
p.y += _origin.y;
|
|
10084
|
+
return p;
|
|
10056
10085
|
})
|
|
10057
10086
|
),
|
|
10058
10087
|
rotation: this.rotation
|
|
@@ -10060,15 +10089,13 @@ let BaseElement2D = class extends Node2D {
|
|
|
10060
10089
|
}
|
|
10061
10090
|
getGlobalObb() {
|
|
10062
10091
|
const origin = this.getTransformOrigin();
|
|
10063
|
-
const _origin = this.globalTransform.
|
|
10064
|
-
const offset = [_origin[0] - origin.x, _origin[1] - origin.y];
|
|
10092
|
+
const _origin = this.globalTransform.apply(origin).sub(origin);
|
|
10065
10093
|
return {
|
|
10066
10094
|
rect: new Rect2(
|
|
10067
10095
|
this._getPointArray().map((p) => {
|
|
10068
|
-
|
|
10069
|
-
|
|
10070
|
-
|
|
10071
|
-
];
|
|
10096
|
+
p.x += _origin.x;
|
|
10097
|
+
p.y += _origin.y;
|
|
10098
|
+
return p;
|
|
10072
10099
|
})
|
|
10073
10100
|
),
|
|
10074
10101
|
rotation: this.globalRotation
|
|
@@ -10086,12 +10113,12 @@ let BaseElement2D = class extends Node2D {
|
|
|
10086
10113
|
// override isVisibleInTree(): boolean {
|
|
10087
10114
|
// if (this._tree) {
|
|
10088
10115
|
// const root = this._tree.root
|
|
10089
|
-
// const camera = root.
|
|
10116
|
+
// const camera = root.worldTransform.inverse()
|
|
10090
10117
|
// const { x, y, width, height } = root
|
|
10091
|
-
// const p1 = camera.
|
|
10092
|
-
// const p2 = camera.
|
|
10093
|
-
// const p3 = camera.
|
|
10094
|
-
// const p4 = camera.
|
|
10118
|
+
// const p1 = camera.apply(x, y)
|
|
10119
|
+
// const p2 = camera.apply(x + width, y)
|
|
10120
|
+
// const p3 = camera.apply(x + width, y + height)
|
|
10121
|
+
// const p4 = camera.apply(x, y + height)
|
|
10095
10122
|
// const pts = [p1, p2, p3, p4]
|
|
10096
10123
|
// const xs = pts.map(p => p[0])
|
|
10097
10124
|
// const ys = pts.map(p => p[1])
|
|
@@ -10181,9 +10208,9 @@ let BaseElement2D = class extends Node2D {
|
|
|
10181
10208
|
}
|
|
10182
10209
|
}
|
|
10183
10210
|
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
10184
|
-
|
|
10211
|
+
_positionInput(localPos, key) {
|
|
10185
10212
|
const { width, height } = this.size;
|
|
10186
|
-
return
|
|
10213
|
+
return localPos.x >= 0 && localPos.x < width && localPos.y >= 0 && localPos.y < height;
|
|
10187
10214
|
}
|
|
10188
10215
|
_input(event, key) {
|
|
10189
10216
|
switch (key) {
|
|
@@ -10191,14 +10218,15 @@ let BaseElement2D = class extends Node2D {
|
|
|
10191
10218
|
case "pointermove":
|
|
10192
10219
|
case "pointerup": {
|
|
10193
10220
|
if (this.canPointerEvents()) {
|
|
10194
|
-
|
|
10221
|
+
const { screenX, screenY } = event;
|
|
10195
10222
|
if (screenX && screenY) {
|
|
10223
|
+
const pos = new Vector2(screenX, screenY);
|
|
10196
10224
|
const viewport = this.getViewport();
|
|
10197
10225
|
if (viewport) {
|
|
10198
|
-
|
|
10226
|
+
viewport.toGlobal(pos, pos);
|
|
10199
10227
|
}
|
|
10200
|
-
|
|
10201
|
-
if (this.
|
|
10228
|
+
this.toLocal(pos, pos);
|
|
10229
|
+
if (this._positionInput(pos, key)) {
|
|
10202
10230
|
if (!event.target) {
|
|
10203
10231
|
event.target = this;
|
|
10204
10232
|
}
|