modern-canvas 0.7.13 → 0.7.15
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 +113 -80
- package/dist/index.d.cts +28 -22
- package/dist/index.d.mts +28 -22
- package/dist/index.d.ts +28 -22
- package/dist/index.js +15 -15
- package/dist/index.mjs +113 -80
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1740,7 +1740,7 @@ class Matrix3 extends Matrix {
|
|
|
1740
1740
|
constructor(array) {
|
|
1741
1741
|
super(3, 3, array);
|
|
1742
1742
|
}
|
|
1743
|
-
|
|
1743
|
+
affineInvert() {
|
|
1744
1744
|
const [
|
|
1745
1745
|
n11,
|
|
1746
1746
|
n21,
|
|
@@ -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,8 +2183,27 @@ class Transform2D extends Matrix3 {
|
|
|
2180
2183
|
output.position.y = ty + (pivot.x * b + pivot.y * d);
|
|
2181
2184
|
return output;
|
|
2182
2185
|
}
|
|
2183
|
-
|
|
2184
|
-
|
|
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
|
+
}
|
|
2195
|
+
affineInverse() {
|
|
2196
|
+
return this.clone().affineInvert();
|
|
2197
|
+
}
|
|
2198
|
+
applyAffineInverse(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;
|
|
2185
2207
|
}
|
|
2186
2208
|
isIdentity() {
|
|
2187
2209
|
const { a, b, c, d, tx, ty } = this.toObject();
|
|
@@ -2552,7 +2574,7 @@ attribute vec4 aBackgroundColor;
|
|
|
2552
2574
|
attribute vec4 aDisableWrapMode;
|
|
2553
2575
|
|
|
2554
2576
|
uniform mat3 projectionMatrix;
|
|
2555
|
-
uniform mat3
|
|
2577
|
+
uniform mat3 viewMatrix;
|
|
2556
2578
|
uniform vec4 modulate;
|
|
2557
2579
|
|
|
2558
2580
|
varying float vTextureId;
|
|
@@ -2568,7 +2590,7 @@ void main(void) {
|
|
|
2568
2590
|
0.0, 0.0, 1.0
|
|
2569
2591
|
);
|
|
2570
2592
|
vTextureId = aTextureId;
|
|
2571
|
-
mat3 modelViewProjectionMatrix = projectionMatrix *
|
|
2593
|
+
mat3 modelViewProjectionMatrix = projectionMatrix * viewMatrix * modelMatrix;
|
|
2572
2594
|
gl_Position = vec4((modelViewProjectionMatrix * vec3(aPosition, 1.0)).xy, 0.0, 1.0);
|
|
2573
2595
|
vUv = aUv;
|
|
2574
2596
|
vModulate = aModulate * modulate;
|
|
@@ -3299,7 +3321,7 @@ class WebGLProgramModule extends WebGLModule {
|
|
|
3299
3321
|
boundProgram = null;
|
|
3300
3322
|
uniforms = {
|
|
3301
3323
|
projectionMatrix: [1, 0, 0, 0, 1, 0, 0, 0, 1],
|
|
3302
|
-
|
|
3324
|
+
viewMatrix: [1, 0, 0, 0, 1, 0, 0, 0, 1]
|
|
3303
3325
|
};
|
|
3304
3326
|
create(options) {
|
|
3305
3327
|
const program = this.gl.createProgram();
|
|
@@ -3503,7 +3525,7 @@ ${gl.getShaderInfoLog(shader)}`);
|
|
|
3503
3525
|
this.boundProgram = null;
|
|
3504
3526
|
this.uniforms = {
|
|
3505
3527
|
projectionMatrix: [1, 0, 0, 0, 1, 0, 0, 0, 1],
|
|
3506
|
-
|
|
3528
|
+
viewMatrix: [1, 0, 0, 0, 1, 0, 0, 0, 1]
|
|
3507
3529
|
};
|
|
3508
3530
|
}
|
|
3509
3531
|
free() {
|
|
@@ -3597,8 +3619,8 @@ class WebGLScissorModule extends WebGLModule {
|
|
|
3597
3619
|
use() {
|
|
3598
3620
|
const renderer = this._renderer;
|
|
3599
3621
|
const { pixelRatio, mask, viewport, screen, gl, program } = renderer;
|
|
3600
|
-
const {
|
|
3601
|
-
const rect = transformRectToAABB(
|
|
3622
|
+
const { viewMatrix } = program.uniforms;
|
|
3623
|
+
const rect = transformRectToAABB(viewMatrix, mask.last.mask);
|
|
3602
3624
|
const { x, y, width, height } = rect;
|
|
3603
3625
|
let scissorY;
|
|
3604
3626
|
if (viewport.boundViewport) {
|
|
@@ -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) {
|
|
@@ -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.
|
|
6677
|
+
renderer.program.uniforms.viewMatrix = this.canvasTransform.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
|
+
toCanvasGlobal(screenPos, newPos) {
|
|
6693
|
+
return this.canvasTransform.applyAffineInverse(screenPos, newPos);
|
|
6694
|
+
}
|
|
6695
|
+
toCanvasScreen(globalPos, newPos) {
|
|
6696
|
+
return this.canvasTransform.apply(globalPos, newPos);
|
|
6697
|
+
}
|
|
6670
6698
|
};
|
|
6671
6699
|
__decorateClass$P([
|
|
6672
6700
|
property({ fallback: 0 })
|
|
@@ -7203,35 +7231,28 @@ let Node2D = class extends CanvasItem {
|
|
|
7203
7231
|
getTransformOrigin() {
|
|
7204
7232
|
return new Vector2(0, 0);
|
|
7205
7233
|
}
|
|
7206
|
-
|
|
7234
|
+
updateTransform(cb) {
|
|
7207
7235
|
const origin = this.getTransformOrigin();
|
|
7208
|
-
const transform =
|
|
7209
|
-
transform.translate(-origin.x, -origin.y).scale(this.scale.x, this.scale.y).skew(this.skew.x, this.skew.y).rotate(this.rotation);
|
|
7236
|
+
const transform = this.transform.identity().translate(-origin.x, -origin.y).scale(this.scale.x, this.scale.y).skew(this.skew.x, this.skew.y).rotate(this.rotation);
|
|
7210
7237
|
cb?.(transform);
|
|
7211
7238
|
transform.translate(this.position.x, this.position.y).translate(origin.x, origin.y);
|
|
7212
|
-
return transform;
|
|
7213
|
-
}
|
|
7214
|
-
updateTransform() {
|
|
7215
|
-
this.transform.copy(this.getTransform());
|
|
7216
7239
|
}
|
|
7217
7240
|
updateGlobalTransform() {
|
|
7218
7241
|
this.updateTransform();
|
|
7219
7242
|
const parent = this.getParent();
|
|
7220
7243
|
if (parent?.globalTransform) {
|
|
7221
|
-
|
|
7222
|
-
|
|
7223
|
-
|
|
7224
|
-
|
|
7225
|
-
|
|
7226
|
-
|
|
7227
|
-
|
|
7228
|
-
|
|
7229
|
-
);
|
|
7230
|
-
this.
|
|
7231
|
-
|
|
7232
|
-
|
|
7233
|
-
);
|
|
7234
|
-
this.globalRotation = parent.globalRotation + this.rotation;
|
|
7244
|
+
const {
|
|
7245
|
+
globalPosition,
|
|
7246
|
+
globalScale,
|
|
7247
|
+
globalSkew,
|
|
7248
|
+
globalTransform,
|
|
7249
|
+
globalRotation
|
|
7250
|
+
} = parent;
|
|
7251
|
+
this._parentTransformDirtyId = globalTransform.dirtyId;
|
|
7252
|
+
this.globalPosition.set(globalPosition.x + this.position.x, globalPosition.y + this.position.y);
|
|
7253
|
+
this.globalScale.set(globalScale.x * this.scale.x, globalScale.y * this.scale.y);
|
|
7254
|
+
this.globalSkew.set(globalSkew.x * this.skew.x, globalSkew.y * this.skew.y);
|
|
7255
|
+
this.globalRotation = globalRotation + this.rotation;
|
|
7235
7256
|
parent.globalTransform.multiply(this.transform, this.globalTransform);
|
|
7236
7257
|
} else {
|
|
7237
7258
|
this.globalPosition.copy(this.position);
|
|
@@ -7279,6 +7300,12 @@ let Node2D = class extends CanvasItem {
|
|
|
7279
7300
|
this.requestRelayout();
|
|
7280
7301
|
}
|
|
7281
7302
|
}
|
|
7303
|
+
toLocal(globalPos, newPos) {
|
|
7304
|
+
return this.globalTransform.applyAffineInverse(globalPos, newPos);
|
|
7305
|
+
}
|
|
7306
|
+
toGlobal(localPos, newPos) {
|
|
7307
|
+
return this.globalTransform.apply(localPos, newPos);
|
|
7308
|
+
}
|
|
7282
7309
|
};
|
|
7283
7310
|
__decorateClass$J([
|
|
7284
7311
|
property({ protected: true, fallback: 0 })
|
|
@@ -7397,6 +7424,18 @@ let Camera2D = class extends Node2D {
|
|
|
7397
7424
|
viewport.canvasTransform.identity().scale(this.zoom.x, this.zoom.y).translate(this.position.x, this.position.y);
|
|
7398
7425
|
this.emit("updateCanvasTransform");
|
|
7399
7426
|
}
|
|
7427
|
+
toGlobal(screenPos, newPos) {
|
|
7428
|
+
const viewport = this.getViewport();
|
|
7429
|
+
if (!viewport)
|
|
7430
|
+
throw new Error("Failed to toGlobal, viewport is empty");
|
|
7431
|
+
return viewport.toCanvasGlobal(screenPos, newPos);
|
|
7432
|
+
}
|
|
7433
|
+
toScreen(globalPos, newPos) {
|
|
7434
|
+
const viewport = this.getViewport();
|
|
7435
|
+
if (!viewport)
|
|
7436
|
+
throw new Error("Failed to toScreen, viewport is empty");
|
|
7437
|
+
return viewport.toCanvasScreen(globalPos, newPos);
|
|
7438
|
+
}
|
|
7400
7439
|
};
|
|
7401
7440
|
__decorateClass$I([
|
|
7402
7441
|
property({ protected: true, fallback: false })
|
|
@@ -9757,7 +9796,10 @@ class BaseElement2DText extends CoreObject {
|
|
|
9757
9796
|
break;
|
|
9758
9797
|
}
|
|
9759
9798
|
}
|
|
9760
|
-
|
|
9799
|
+
setContent(content) {
|
|
9800
|
+
this.content = normalizeTextContent(content);
|
|
9801
|
+
}
|
|
9802
|
+
measure() {
|
|
9761
9803
|
this.base.style = {
|
|
9762
9804
|
justifyContent: "center",
|
|
9763
9805
|
alignItems: "center",
|
|
@@ -9765,12 +9807,6 @@ class BaseElement2DText extends CoreObject {
|
|
|
9765
9807
|
...this.parent.style.toJSON()
|
|
9766
9808
|
};
|
|
9767
9809
|
this.base.requestUpdate();
|
|
9768
|
-
}
|
|
9769
|
-
setContent(content) {
|
|
9770
|
-
this.content = normalizeTextContent(content);
|
|
9771
|
-
}
|
|
9772
|
-
measure() {
|
|
9773
|
-
this._updateText();
|
|
9774
9810
|
return this.base.measure();
|
|
9775
9811
|
}
|
|
9776
9812
|
updateMeasure() {
|
|
@@ -10001,9 +10037,9 @@ let BaseElement2D = class extends Node2D {
|
|
|
10001
10037
|
const [originX, originY] = parseCSSTransformOrigin(this.style.transformOrigin);
|
|
10002
10038
|
return new Vector2(originX * width, originY * height);
|
|
10003
10039
|
}
|
|
10004
|
-
|
|
10040
|
+
updateTransform(cb) {
|
|
10005
10041
|
const { width, height } = this.size;
|
|
10006
|
-
|
|
10042
|
+
super.updateTransform((transform) => {
|
|
10007
10043
|
parseCSSTransform(this.style.transform ?? "", width, height, transform);
|
|
10008
10044
|
cb?.(transform);
|
|
10009
10045
|
});
|
|
@@ -10022,37 +10058,35 @@ let BaseElement2D = class extends Node2D {
|
|
|
10022
10058
|
const x2 = x1 + width;
|
|
10023
10059
|
const y2 = y1 + height;
|
|
10024
10060
|
return [
|
|
10025
|
-
|
|
10026
|
-
|
|
10027
|
-
|
|
10028
|
-
|
|
10061
|
+
{ x: x1, y: y1 },
|
|
10062
|
+
{ x: x1, y: y2 },
|
|
10063
|
+
{ x: x2, y: y1 },
|
|
10064
|
+
{ x: x2, y: y2 }
|
|
10029
10065
|
];
|
|
10030
10066
|
}
|
|
10031
10067
|
getAabb() {
|
|
10032
10068
|
return new Rect2(
|
|
10033
10069
|
this._getPointArray().map((p) => {
|
|
10034
|
-
return this.transform.
|
|
10070
|
+
return this.transform.apply(p);
|
|
10035
10071
|
})
|
|
10036
10072
|
);
|
|
10037
10073
|
}
|
|
10038
10074
|
getGlobalAabb() {
|
|
10039
10075
|
return new Rect2(
|
|
10040
10076
|
this._getPointArray().map((p) => {
|
|
10041
|
-
return this.globalTransform.
|
|
10077
|
+
return this.globalTransform.apply(p);
|
|
10042
10078
|
})
|
|
10043
10079
|
);
|
|
10044
10080
|
}
|
|
10045
10081
|
getObb() {
|
|
10046
10082
|
const origin = this.getTransformOrigin();
|
|
10047
|
-
const _origin = this.transform.
|
|
10048
|
-
const offset = [_origin[0] - origin.x, _origin[1] - origin.y];
|
|
10083
|
+
const _origin = this.transform.apply(origin).sub(origin);
|
|
10049
10084
|
return {
|
|
10050
10085
|
rect: new Rect2(
|
|
10051
10086
|
this._getPointArray().map((p) => {
|
|
10052
|
-
|
|
10053
|
-
|
|
10054
|
-
|
|
10055
|
-
];
|
|
10087
|
+
p.x += _origin.x;
|
|
10088
|
+
p.y += _origin.y;
|
|
10089
|
+
return p;
|
|
10056
10090
|
})
|
|
10057
10091
|
),
|
|
10058
10092
|
rotation: this.rotation
|
|
@@ -10060,15 +10094,13 @@ let BaseElement2D = class extends Node2D {
|
|
|
10060
10094
|
}
|
|
10061
10095
|
getGlobalObb() {
|
|
10062
10096
|
const origin = this.getTransformOrigin();
|
|
10063
|
-
const _origin = this.globalTransform.
|
|
10064
|
-
const offset = [_origin[0] - origin.x, _origin[1] - origin.y];
|
|
10097
|
+
const _origin = this.globalTransform.apply(origin).sub(origin);
|
|
10065
10098
|
return {
|
|
10066
10099
|
rect: new Rect2(
|
|
10067
10100
|
this._getPointArray().map((p) => {
|
|
10068
|
-
|
|
10069
|
-
|
|
10070
|
-
|
|
10071
|
-
];
|
|
10101
|
+
p.x += _origin.x;
|
|
10102
|
+
p.y += _origin.y;
|
|
10103
|
+
return p;
|
|
10072
10104
|
})
|
|
10073
10105
|
),
|
|
10074
10106
|
rotation: this.globalRotation
|
|
@@ -10086,12 +10118,12 @@ let BaseElement2D = class extends Node2D {
|
|
|
10086
10118
|
// override isVisibleInTree(): boolean {
|
|
10087
10119
|
// if (this._tree) {
|
|
10088
10120
|
// const root = this._tree.root
|
|
10089
|
-
// const camera = root.
|
|
10121
|
+
// const camera = root.worldTransform.inverse()
|
|
10090
10122
|
// const { x, y, width, height } = root
|
|
10091
|
-
// const p1 = camera.
|
|
10092
|
-
// const p2 = camera.
|
|
10093
|
-
// const p3 = camera.
|
|
10094
|
-
// const p4 = camera.
|
|
10123
|
+
// const p1 = camera.apply(x, y)
|
|
10124
|
+
// const p2 = camera.apply(x + width, y)
|
|
10125
|
+
// const p3 = camera.apply(x + width, y + height)
|
|
10126
|
+
// const p4 = camera.apply(x, y + height)
|
|
10095
10127
|
// const pts = [p1, p2, p3, p4]
|
|
10096
10128
|
// const xs = pts.map(p => p[0])
|
|
10097
10129
|
// const ys = pts.map(p => p[1])
|
|
@@ -10181,9 +10213,9 @@ let BaseElement2D = class extends Node2D {
|
|
|
10181
10213
|
}
|
|
10182
10214
|
}
|
|
10183
10215
|
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
10184
|
-
|
|
10216
|
+
_positionInput(localPos, key) {
|
|
10185
10217
|
const { width, height } = this.size;
|
|
10186
|
-
return
|
|
10218
|
+
return localPos.x >= 0 && localPos.x < width && localPos.y >= 0 && localPos.y < height;
|
|
10187
10219
|
}
|
|
10188
10220
|
_input(event, key) {
|
|
10189
10221
|
switch (key) {
|
|
@@ -10191,14 +10223,15 @@ let BaseElement2D = class extends Node2D {
|
|
|
10191
10223
|
case "pointermove":
|
|
10192
10224
|
case "pointerup": {
|
|
10193
10225
|
if (this.canPointerEvents()) {
|
|
10194
|
-
|
|
10226
|
+
const { screenX, screenY } = event;
|
|
10195
10227
|
if (screenX && screenY) {
|
|
10228
|
+
const pos = new Vector2(screenX, screenY);
|
|
10196
10229
|
const viewport = this.getViewport();
|
|
10197
10230
|
if (viewport) {
|
|
10198
|
-
|
|
10231
|
+
viewport.toCanvasGlobal(pos, pos);
|
|
10199
10232
|
}
|
|
10200
|
-
|
|
10201
|
-
if (this.
|
|
10233
|
+
this.toLocal(pos, pos);
|
|
10234
|
+
if (this._positionInput(pos, key)) {
|
|
10202
10235
|
if (!event.target) {
|
|
10203
10236
|
event.target = this;
|
|
10204
10237
|
}
|