modern-canvas 0.23.2 → 0.23.3
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.js
CHANGED
|
@@ -6695,6 +6695,7 @@ function Oi(e, t, n, r, i = {}) {
|
|
|
6695
6695
|
//#region src/scene/2d/element/Element2DForeground.ts
|
|
6696
6696
|
var ki = class extends J {
|
|
6697
6697
|
_sourceCanvas;
|
|
6698
|
+
_sourceImage;
|
|
6698
6699
|
setProperties(e) {
|
|
6699
6700
|
return super._setProperties(d(e) ? void 0 : g(e));
|
|
6700
6701
|
}
|
|
@@ -6709,24 +6710,38 @@ var ki = class extends J {
|
|
|
6709
6710
|
}
|
|
6710
6711
|
}
|
|
6711
6712
|
async loadTexture() {
|
|
6712
|
-
await super.loadTexture(), this._applyEffects();
|
|
6713
|
+
await super.loadTexture(), await this._applyEffects();
|
|
6713
6714
|
}
|
|
6714
|
-
_applyEffects() {
|
|
6715
|
-
if (!this.effects?.length || this.animatedTexture || !this.texture)
|
|
6716
|
-
|
|
6717
|
-
|
|
6718
|
-
let t = Math.max(1, Math.round(e.width)), n = Math.max(1, Math.round(e.height)), r = S(t, n), i = r?.getContext("2d");
|
|
6719
|
-
r && i && (i.drawImage(e, 0, 0, t, n), this._sourceCanvas = r);
|
|
6715
|
+
async _applyEffects() {
|
|
6716
|
+
if (!this.effects?.length || this.animatedTexture || !this.texture) {
|
|
6717
|
+
this._sourceCanvas = void 0, this._sourceImage = void 0;
|
|
6718
|
+
return;
|
|
6720
6719
|
}
|
|
6721
|
-
let
|
|
6722
|
-
if (!
|
|
6723
|
-
let
|
|
6720
|
+
let e = await this._resolveSourceCanvas();
|
|
6721
|
+
if (!e) return;
|
|
6722
|
+
let t = e.width, n = e.height, r = Oi(e, this.effects, t, n);
|
|
6724
6723
|
this.texture = new er({
|
|
6725
|
-
source:
|
|
6726
|
-
width:
|
|
6727
|
-
height:
|
|
6724
|
+
source: r,
|
|
6725
|
+
width: t,
|
|
6726
|
+
height: n
|
|
6728
6727
|
});
|
|
6729
6728
|
}
|
|
6729
|
+
async _resolveSourceCanvas() {
|
|
6730
|
+
this._sourceImage !== this.image && (this._sourceCanvas = void 0, this._sourceImage = this.image);
|
|
6731
|
+
let e = this.texture?.source;
|
|
6732
|
+
if (e && e.width > 0 && e.height > 0) return this._sourceCanvas = this._snapshot(e);
|
|
6733
|
+
if (this._sourceCanvas) return this._sourceCanvas;
|
|
6734
|
+
if (d(this.image) || this.image === "viewport") return;
|
|
6735
|
+
let t = this.image, n = await $.loadBy(`${t}#mc-foreground-source`, async () => {
|
|
6736
|
+
let e = await $.fetchImageBitmap(t), n = this._snapshot(e);
|
|
6737
|
+
return Ne && e instanceof ImageBitmap && e.close(), n;
|
|
6738
|
+
});
|
|
6739
|
+
return this._sourceImage === t && (this._sourceCanvas = n), n;
|
|
6740
|
+
}
|
|
6741
|
+
_snapshot(e) {
|
|
6742
|
+
let t = Math.max(1, Math.round(e.width)), n = Math.max(1, Math.round(e.height)), r = S(t, n), i = r?.getContext("2d");
|
|
6743
|
+
if (!(!r || !i)) return i.drawImage(e, 0, 0, t, n), r;
|
|
6744
|
+
}
|
|
6730
6745
|
};
|
|
6731
6746
|
O([_(), D("design:type", Object)], ki.prototype, "fillWithShape", void 0), O([_(), D("design:type", Array)], ki.prototype, "effects", void 0);
|
|
6732
6747
|
//#endregion
|
|
@@ -1,19 +1,36 @@
|
|
|
1
1
|
import type { Foreground, NormalizedEffect, NormalizedForeground } from 'modern-idoc';
|
|
2
2
|
import { Element2DFill } from './Element2DFill';
|
|
3
|
+
type Snapshotable = CanvasImageSource & {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
};
|
|
3
7
|
export declare class Element2DForeground extends Element2DFill implements NormalizedForeground {
|
|
4
8
|
fillWithShape: NormalizedForeground['fillWithShape'];
|
|
5
9
|
/** 图片效果叠层(filling/strokes/offset...);加载后烘焙到运行时 canvas,不持久化 */
|
|
6
10
|
effects?: NormalizedEffect[];
|
|
7
11
|
/**
|
|
8
|
-
* 原图的 CPU 副本(HTMLCanvas
|
|
9
|
-
*
|
|
10
|
-
* `
|
|
11
|
-
*
|
|
12
|
+
* 原图的 CPU 副本(HTMLCanvas),供烘焙 effects 使用。
|
|
13
|
+
*
|
|
14
|
+
* 不能依赖 `this.texture.source`:那是张 ImageBitmap,经 GPU 上传 / 资源 GC 后
|
|
15
|
+
* 会被 `close()`(detached,width/height 归零),此时 drawImage 会抛
|
|
16
|
+
* `InvalidStateError`。实测纹理加载返回时 source 往往已是 detached,故这份副本
|
|
17
|
+
* 必须从图片自身独立解码(见 `_resolveSourceCanvas`),与 GPU 纹理生命周期解耦。
|
|
12
18
|
*/
|
|
13
19
|
protected _sourceCanvas?: HTMLCanvasElement;
|
|
20
|
+
/** `_sourceCanvas` 对应的图片地址,image 变更时用于失效旧副本 */
|
|
21
|
+
protected _sourceImage?: string;
|
|
14
22
|
setProperties(properties?: Foreground): this;
|
|
15
23
|
protected _updateProperty(key: string, value: any, oldValue: any): void;
|
|
16
24
|
loadTexture(): Promise<void>;
|
|
17
|
-
/**
|
|
18
|
-
protected _applyEffects(): void
|
|
25
|
+
/** 把原图 + effects 烘焙成一张运行时 canvas,包成 CanvasTexture(gif/无 effects 时跳过) */
|
|
26
|
+
protected _applyEffects(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* 取得用于烘焙的 CPU 副本:
|
|
29
|
+
* 1) 若纹理 source 仍存活(width>0),直接快照(省一次解码);
|
|
30
|
+
* 2) 否则从 image 重新解码一份(资源层按 url 缓存复用,避免重复烘焙时反复解码)。
|
|
31
|
+
*/
|
|
32
|
+
protected _resolveSourceCanvas(): Promise<HTMLCanvasElement | undefined>;
|
|
33
|
+
/** 把一张存活的图源画进新 canvas(不会被 close,可反复用于烘焙) */
|
|
34
|
+
protected _snapshot(source: Snapshotable): HTMLCanvasElement | undefined;
|
|
19
35
|
}
|
|
36
|
+
export {};
|