modern-canvas 0.4.51 → 0.4.52
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 +92 -81
- package/dist/index.d.cts +17 -12
- package/dist/index.d.mts +17 -12
- package/dist/index.d.ts +17 -12
- package/dist/index.js +10 -10
- package/dist/index.mjs +92 -81
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -7144,13 +7144,15 @@ ColorReplaceEffect = __decorateClass$J([
|
|
|
7144
7144
|
], ColorReplaceEffect);
|
|
7145
7145
|
|
|
7146
7146
|
class CanvasContext extends Path2D {
|
|
7147
|
-
textureTransform;
|
|
7148
7147
|
fillStyle;
|
|
7149
7148
|
strokeStyle;
|
|
7150
7149
|
lineCap;
|
|
7151
7150
|
lineJoin;
|
|
7152
7151
|
lineWidth;
|
|
7153
7152
|
miterLimit;
|
|
7153
|
+
// custom
|
|
7154
|
+
uvTransform;
|
|
7155
|
+
vertTransform;
|
|
7154
7156
|
_defaultStyle = Texture2D.EMPTY;
|
|
7155
7157
|
_draws = [];
|
|
7156
7158
|
_toTexture(source) {
|
|
@@ -7172,7 +7174,8 @@ class CanvasContext extends Path2D {
|
|
|
7172
7174
|
type: "stroke",
|
|
7173
7175
|
path,
|
|
7174
7176
|
texture,
|
|
7175
|
-
|
|
7177
|
+
uvTransform: this.uvTransform,
|
|
7178
|
+
vertTransform: this.vertTransform,
|
|
7176
7179
|
style: {
|
|
7177
7180
|
alignment: 0.5,
|
|
7178
7181
|
cap: this.lineCap ?? "butt",
|
|
@@ -7201,7 +7204,8 @@ class CanvasContext extends Path2D {
|
|
|
7201
7204
|
type: "fill",
|
|
7202
7205
|
path,
|
|
7203
7206
|
texture,
|
|
7204
|
-
|
|
7207
|
+
uvTransform: this.uvTransform,
|
|
7208
|
+
vertTransform: this.vertTransform
|
|
7205
7209
|
});
|
|
7206
7210
|
super.reset();
|
|
7207
7211
|
}
|
|
@@ -7209,7 +7213,8 @@ class CanvasContext extends Path2D {
|
|
|
7209
7213
|
super.copy(source);
|
|
7210
7214
|
this.strokeStyle = source.strokeStyle;
|
|
7211
7215
|
this.fillStyle = source.fillStyle;
|
|
7212
|
-
this.
|
|
7216
|
+
this.uvTransform = source.uvTransform;
|
|
7217
|
+
this.vertTransform = source.vertTransform;
|
|
7213
7218
|
this.lineCap = source.lineCap;
|
|
7214
7219
|
this.lineJoin = source.lineJoin;
|
|
7215
7220
|
this.lineWidth = source.lineWidth;
|
|
@@ -7221,7 +7226,8 @@ class CanvasContext extends Path2D {
|
|
|
7221
7226
|
super.reset();
|
|
7222
7227
|
this.strokeStyle = void 0;
|
|
7223
7228
|
this.fillStyle = void 0;
|
|
7224
|
-
this.
|
|
7229
|
+
this.uvTransform = void 0;
|
|
7230
|
+
this.vertTransform = void 0;
|
|
7225
7231
|
this.lineCap = void 0;
|
|
7226
7232
|
this.lineJoin = void 0;
|
|
7227
7233
|
this.lineWidth = void 0;
|
|
@@ -7229,8 +7235,9 @@ class CanvasContext extends Path2D {
|
|
|
7229
7235
|
this._draws.length = 0;
|
|
7230
7236
|
return this;
|
|
7231
7237
|
}
|
|
7232
|
-
buildUvs(start, vertices, uvs, texture,
|
|
7238
|
+
buildUvs(start, vertices, uvs, texture, uvTransform) {
|
|
7233
7239
|
if (texture) {
|
|
7240
|
+
const _uvTransform = uvTransform ? typeof uvTransform === "function" ? uvTransform : (x, y) => uvTransform.applyToPoint(x, y) : uvTransform;
|
|
7234
7241
|
const w = texture.width;
|
|
7235
7242
|
const h = texture.height;
|
|
7236
7243
|
for (let len = vertices.length, i = start; i < len; i += 2) {
|
|
@@ -7238,8 +7245,8 @@ class CanvasContext extends Path2D {
|
|
|
7238
7245
|
const y = vertices[i + 1];
|
|
7239
7246
|
let uvX;
|
|
7240
7247
|
let uvY;
|
|
7241
|
-
if (
|
|
7242
|
-
[uvX, uvY] =
|
|
7248
|
+
if (_uvTransform) {
|
|
7249
|
+
[uvX, uvY] = _uvTransform(x, y);
|
|
7243
7250
|
} else {
|
|
7244
7251
|
[uvX, uvY] = [x / w, y / h];
|
|
7245
7252
|
}
|
|
@@ -7253,59 +7260,35 @@ class CanvasContext extends Path2D {
|
|
|
7253
7260
|
}
|
|
7254
7261
|
toBatchables() {
|
|
7255
7262
|
const batchables = [];
|
|
7256
|
-
let vertices = [];
|
|
7257
|
-
let indices = [];
|
|
7258
|
-
let uvs = [];
|
|
7259
|
-
let texture;
|
|
7260
|
-
const push = (draw) => {
|
|
7261
|
-
batchables.push({
|
|
7262
|
-
vertices,
|
|
7263
|
-
indices,
|
|
7264
|
-
uvs,
|
|
7265
|
-
texture,
|
|
7266
|
-
type: draw.type,
|
|
7267
|
-
disableWrapMode: draw.disableWrapMode
|
|
7268
|
-
});
|
|
7269
|
-
vertices = [];
|
|
7270
|
-
indices = [];
|
|
7271
|
-
uvs = [];
|
|
7272
|
-
texture = void 0;
|
|
7273
|
-
};
|
|
7274
7263
|
for (let len = this._draws.length, i = 0; i < len; i++) {
|
|
7275
|
-
const
|
|
7276
|
-
const
|
|
7277
|
-
|
|
7278
|
-
|
|
7279
|
-
|
|
7280
|
-
|
|
7281
|
-
if (!oldTexture) {
|
|
7282
|
-
texture = draw.texture;
|
|
7283
|
-
}
|
|
7284
|
-
if (vertices.length && oldTexture !== draw.texture && !oldTexture?.is(draw.texture)) {
|
|
7285
|
-
push(draw);
|
|
7286
|
-
texture = draw.texture;
|
|
7287
|
-
}
|
|
7288
|
-
const start = vertices.length;
|
|
7289
|
-
if (draw.type === "fill") {
|
|
7290
|
-
draw.path.fillTriangulate({
|
|
7264
|
+
const current = this._draws[i];
|
|
7265
|
+
const vertices = [];
|
|
7266
|
+
const indices = [];
|
|
7267
|
+
const uvs = [];
|
|
7268
|
+
if (current.type === "fill") {
|
|
7269
|
+
current.path.fillTriangulate({
|
|
7291
7270
|
vertices,
|
|
7292
7271
|
indices
|
|
7293
7272
|
});
|
|
7294
|
-
this.buildUvs(start, vertices, uvs, draw.texture, draw.textureTransform);
|
|
7295
7273
|
} else {
|
|
7296
|
-
|
|
7274
|
+
current.path.strokeTriangulate({
|
|
7297
7275
|
vertices,
|
|
7298
7276
|
indices,
|
|
7299
|
-
lineStyle:
|
|
7277
|
+
lineStyle: current.style,
|
|
7300
7278
|
flipAlignment: false,
|
|
7301
7279
|
closed: true
|
|
7302
7280
|
});
|
|
7303
|
-
this.buildUvs(start, vertices, uvs, draw.texture, draw.textureTransform);
|
|
7304
7281
|
}
|
|
7305
|
-
|
|
7306
|
-
|
|
7307
|
-
|
|
7308
|
-
|
|
7282
|
+
this.buildUvs(0, vertices, uvs, current.texture, current.uvTransform);
|
|
7283
|
+
batchables.push({
|
|
7284
|
+
vertices,
|
|
7285
|
+
indices,
|
|
7286
|
+
uvs,
|
|
7287
|
+
texture: current.texture,
|
|
7288
|
+
type: current.type,
|
|
7289
|
+
disableWrapMode: current.disableWrapMode,
|
|
7290
|
+
vertTransform: current.vertTransform
|
|
7291
|
+
});
|
|
7309
7292
|
}
|
|
7310
7293
|
return batchables;
|
|
7311
7294
|
}
|
|
@@ -7384,11 +7367,6 @@ let CanvasItem = class extends TimelineNode {
|
|
|
7384
7367
|
requestRelayout() {
|
|
7385
7368
|
this._relayouting = true;
|
|
7386
7369
|
this.requestUpdate();
|
|
7387
|
-
this.forEachChild((node) => {
|
|
7388
|
-
if (node instanceof CanvasItem) {
|
|
7389
|
-
node.requestRelayout();
|
|
7390
|
-
}
|
|
7391
|
-
});
|
|
7392
7370
|
}
|
|
7393
7371
|
requestRepaint() {
|
|
7394
7372
|
this._repainting = true;
|
|
@@ -9018,7 +8996,7 @@ class BaseElement2DFill extends CoreObject {
|
|
|
9018
8996
|
_getDrawOptions() {
|
|
9019
8997
|
let disableWrapMode = false;
|
|
9020
8998
|
const { width, height } = this.parent.size;
|
|
9021
|
-
const
|
|
8999
|
+
const uvTransform = new Transform2D().scale(1 / width, 1 / height);
|
|
9022
9000
|
if (this.cropRect) {
|
|
9023
9001
|
const {
|
|
9024
9002
|
left = 0,
|
|
@@ -9026,7 +9004,7 @@ class BaseElement2DFill extends CoreObject {
|
|
|
9026
9004
|
right = 0,
|
|
9027
9005
|
bottom = 0
|
|
9028
9006
|
} = this.cropRect;
|
|
9029
|
-
|
|
9007
|
+
uvTransform.scale(
|
|
9030
9008
|
Math.abs(1 - (left + right)),
|
|
9031
9009
|
Math.abs(1 - (top + bottom))
|
|
9032
9010
|
).translate(left, top);
|
|
@@ -9041,24 +9019,26 @@ class BaseElement2DFill extends CoreObject {
|
|
|
9041
9019
|
// flip, TODO
|
|
9042
9020
|
// alignment, TODO
|
|
9043
9021
|
} = this.tile;
|
|
9044
|
-
|
|
9022
|
+
uvTransform.translate(-translateX / width, -translateY / height).scale(1 / scaleX, 1 / scaleY);
|
|
9045
9023
|
disableWrapMode = true;
|
|
9046
9024
|
} else if (this.stretchRect) {
|
|
9047
9025
|
const { left = 0, top = 0, right = 0, bottom = 0 } = this.stretchRect;
|
|
9048
|
-
|
|
9026
|
+
uvTransform.scale(
|
|
9049
9027
|
Math.abs(1 - (-left + -right)),
|
|
9050
9028
|
Math.abs(1 - (-top + -bottom))
|
|
9051
9029
|
).translate(-left, -top);
|
|
9052
9030
|
disableWrapMode = true;
|
|
9053
9031
|
}
|
|
9054
|
-
return { disableWrapMode,
|
|
9032
|
+
return { disableWrapMode, uvTransform };
|
|
9055
9033
|
}
|
|
9056
9034
|
draw() {
|
|
9057
9035
|
const ctx = this.parent.context;
|
|
9058
|
-
const {
|
|
9059
|
-
ctx.
|
|
9036
|
+
const { uvTransform, disableWrapMode } = this._getDrawOptions();
|
|
9037
|
+
ctx.uvTransform = uvTransform;
|
|
9060
9038
|
ctx.fillStyle = this._texture ?? this.color;
|
|
9061
|
-
ctx.fill({
|
|
9039
|
+
ctx.fill({
|
|
9040
|
+
disableWrapMode
|
|
9041
|
+
});
|
|
9062
9042
|
}
|
|
9063
9043
|
}
|
|
9064
9044
|
__decorateClass$u([
|
|
@@ -9163,9 +9143,9 @@ class BaseElement2DOutline extends BaseElement2DFill {
|
|
|
9163
9143
|
}
|
|
9164
9144
|
draw() {
|
|
9165
9145
|
const ctx = this.parent.context;
|
|
9166
|
-
const {
|
|
9146
|
+
const { uvTransform, disableWrapMode } = this._getDrawOptions();
|
|
9167
9147
|
ctx.lineWidth = this.width || 1;
|
|
9168
|
-
ctx.
|
|
9148
|
+
ctx.uvTransform = uvTransform;
|
|
9169
9149
|
ctx.strokeStyle = this._texture ?? this.color;
|
|
9170
9150
|
ctx.stroke({ disableWrapMode });
|
|
9171
9151
|
}
|
|
@@ -9413,7 +9393,6 @@ class BaseElement2DText extends CoreObject {
|
|
|
9413
9393
|
);
|
|
9414
9394
|
}
|
|
9415
9395
|
draw() {
|
|
9416
|
-
const ctx = this.parent.context;
|
|
9417
9396
|
this.baseText.render({
|
|
9418
9397
|
pixelRatio: this.texture.pixelRatio,
|
|
9419
9398
|
view: this.texture.source
|
|
@@ -9421,8 +9400,17 @@ class BaseElement2DText extends CoreObject {
|
|
|
9421
9400
|
this.texture.requestUpload();
|
|
9422
9401
|
const textWidth = this.measureResult?.boundingBox.width ?? this.parent.size.width;
|
|
9423
9402
|
const textHeight = this.measureResult?.boundingBox.height ?? this.parent.size.height;
|
|
9403
|
+
const ctx = this.parent.context;
|
|
9424
9404
|
ctx.fillStyle = this.texture;
|
|
9425
|
-
ctx.
|
|
9405
|
+
ctx.uvTransform = new Transform2D().scale(1 / textWidth, 1 / textHeight);
|
|
9406
|
+
ctx.vertTransform = () => {
|
|
9407
|
+
const parent = this.parent;
|
|
9408
|
+
const origin = parent.getTransformOrigin();
|
|
9409
|
+
return new Transform2D().translate(-origin.x, -origin.y).scale(
|
|
9410
|
+
parent.globalScale.x > 0 ? 1 : -1,
|
|
9411
|
+
parent.globalScale.y > 0 ? 1 : -1
|
|
9412
|
+
).translate(origin.x, origin.y);
|
|
9413
|
+
};
|
|
9426
9414
|
ctx.fill();
|
|
9427
9415
|
}
|
|
9428
9416
|
}
|
|
@@ -9463,8 +9451,19 @@ let Node2D = class extends CanvasItem {
|
|
|
9463
9451
|
super();
|
|
9464
9452
|
this.setProperties(properties).append(nodes);
|
|
9465
9453
|
}
|
|
9454
|
+
getTransformOrigin() {
|
|
9455
|
+
return new Vector2(0, 0);
|
|
9456
|
+
}
|
|
9457
|
+
getTransform(cb) {
|
|
9458
|
+
const origin = this.getTransformOrigin();
|
|
9459
|
+
const transform = new Transform2D();
|
|
9460
|
+
transform.translate(-origin.x, -origin.y).scale(this.scale.x, this.scale.y).skew(this.skew.x, this.skew.y).rotate(this.rotation);
|
|
9461
|
+
cb?.(transform);
|
|
9462
|
+
transform.translate(this.position.x, this.position.y).translate(origin.x, origin.y);
|
|
9463
|
+
return transform;
|
|
9464
|
+
}
|
|
9466
9465
|
_updateTransform() {
|
|
9467
|
-
this.transform.
|
|
9466
|
+
this.transform.copy(this.getTransform());
|
|
9468
9467
|
}
|
|
9469
9468
|
_updateGlobalTransform() {
|
|
9470
9469
|
const parent = this.getParent();
|
|
@@ -9491,8 +9490,17 @@ let Node2D = class extends CanvasItem {
|
|
|
9491
9490
|
this.globalSkew.y = Math.atan2(b, d) - this.globalRotation;
|
|
9492
9491
|
this.requestRelayout();
|
|
9493
9492
|
}
|
|
9494
|
-
_transformVertices(vertices) {
|
|
9495
|
-
|
|
9493
|
+
_transformVertices(vertices, vertTransform) {
|
|
9494
|
+
let a, c, tx, b, d, ty;
|
|
9495
|
+
if (vertTransform) {
|
|
9496
|
+
const globalTransform = this.globalTransform.clone();
|
|
9497
|
+
globalTransform.multiply(
|
|
9498
|
+
typeof vertTransform === "function" ? vertTransform?.() : vertTransform
|
|
9499
|
+
);
|
|
9500
|
+
[a, c, tx, b, d, ty] = globalTransform.toArray();
|
|
9501
|
+
} else {
|
|
9502
|
+
[a, c, tx, b, d, ty] = this.globalTransform.toArray();
|
|
9503
|
+
}
|
|
9496
9504
|
const newVertices = vertices.slice();
|
|
9497
9505
|
for (let len = vertices.length, i = 0; i < len; i += 2) {
|
|
9498
9506
|
const x = vertices[i];
|
|
@@ -9508,7 +9516,7 @@ let Node2D = class extends CanvasItem {
|
|
|
9508
9516
|
return super._relayout(batchables).map((batchable) => {
|
|
9509
9517
|
return {
|
|
9510
9518
|
...batchable,
|
|
9511
|
-
vertices: this._transformVertices(batchable.vertices)
|
|
9519
|
+
vertices: this._transformVertices(batchable.vertices, batchable.vertTransform)
|
|
9512
9520
|
};
|
|
9513
9521
|
});
|
|
9514
9522
|
}
|
|
@@ -9715,14 +9723,17 @@ let BaseElement2D = class extends Node2D {
|
|
|
9715
9723
|
}
|
|
9716
9724
|
}
|
|
9717
9725
|
}
|
|
9718
|
-
|
|
9726
|
+
getTransformOrigin() {
|
|
9719
9727
|
const { width, height } = this.size;
|
|
9720
9728
|
const [originX, originY] = parseCSSTransformOrigin(this.style.transformOrigin);
|
|
9721
|
-
|
|
9722
|
-
|
|
9723
|
-
|
|
9724
|
-
|
|
9725
|
-
|
|
9729
|
+
return new Vector2(originX * width, originY * height);
|
|
9730
|
+
}
|
|
9731
|
+
getTransform(cb) {
|
|
9732
|
+
const { width, height } = this.size;
|
|
9733
|
+
return super.getTransform((transform) => {
|
|
9734
|
+
parseCSSTransform(this.style.transform ?? "", width, height, transform);
|
|
9735
|
+
cb?.(transform);
|
|
9736
|
+
});
|
|
9726
9737
|
}
|
|
9727
9738
|
_updateGlobalTransform() {
|
|
9728
9739
|
super._updateGlobalTransform();
|
|
@@ -10514,7 +10525,7 @@ let Image2D = class extends Element2D {
|
|
|
10514
10525
|
const sy = 1 / h;
|
|
10515
10526
|
const tx = left * width * sx;
|
|
10516
10527
|
const ty = top * height * sy;
|
|
10517
|
-
this.context.
|
|
10528
|
+
this.context.uvTransform = new Transform2D().scale(sx, sy).translate(tx, ty);
|
|
10518
10529
|
this.shape.draw();
|
|
10519
10530
|
this.context.fill();
|
|
10520
10531
|
}
|
|
@@ -10554,7 +10565,7 @@ class TextureRect2D extends Element2D {
|
|
|
10554
10565
|
if (this.texture?.valid) {
|
|
10555
10566
|
const { width, height } = this.size;
|
|
10556
10567
|
this.context.fillStyle = this.texture;
|
|
10557
|
-
this.context.
|
|
10568
|
+
this.context.uvTransform = new Transform2D().scale(
|
|
10558
10569
|
1 / width,
|
|
10559
10570
|
1 / height
|
|
10560
10571
|
);
|
|
@@ -12464,7 +12475,7 @@ let AudioWaveform = class extends Element2D {
|
|
|
12464
12475
|
const src = this._src;
|
|
12465
12476
|
if (src?.valid) {
|
|
12466
12477
|
this.context.fillStyle = src;
|
|
12467
|
-
this.context.
|
|
12478
|
+
this.context.uvTransform = new Transform2D().scale(
|
|
12468
12479
|
1 / this.style.width,
|
|
12469
12480
|
1 / this.style.height
|
|
12470
12481
|
);
|
|
@@ -12738,7 +12749,7 @@ let Ruler = class extends Control {
|
|
|
12738
12749
|
const texture = this.texture;
|
|
12739
12750
|
if (texture?.valid) {
|
|
12740
12751
|
this.context.fillStyle = texture;
|
|
12741
|
-
this.context.
|
|
12752
|
+
this.context.uvTransform = new Transform2D().scale(
|
|
12742
12753
|
1 / this.size.width,
|
|
12743
12754
|
1 / this.size.height
|
|
12744
12755
|
);
|