modern-canvas 0.23.1 → 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
@@ -6694,6 +6694,8 @@ function Oi(e, t, n, r, i = {}) {
6694
6694
  //#endregion
6695
6695
  //#region src/scene/2d/element/Element2DForeground.ts
6696
6696
  var ki = class extends J {
6697
+ _sourceCanvas;
6698
+ _sourceImage;
6697
6699
  setProperties(e) {
6698
6700
  return super._setProperties(d(e) ? void 0 : g(e));
6699
6701
  }
@@ -6708,12 +6710,15 @@ var ki = class extends J {
6708
6710
  }
6709
6711
  }
6710
6712
  async loadTexture() {
6711
- await super.loadTexture(), this._applyEffects();
6713
+ await super.loadTexture(), await this._applyEffects();
6712
6714
  }
6713
- _applyEffects() {
6714
- if (!this.effects?.length || this.animatedTexture || !this.texture) return;
6715
- let e = this.texture.source;
6716
- if (!e || typeof e.width != "number") return;
6715
+ async _applyEffects() {
6716
+ if (!this.effects?.length || this.animatedTexture || !this.texture) {
6717
+ this._sourceCanvas = void 0, this._sourceImage = void 0;
6718
+ return;
6719
+ }
6720
+ let e = await this._resolveSourceCanvas();
6721
+ if (!e) return;
6717
6722
  let t = e.width, n = e.height, r = Oi(e, this.effects, t, n);
6718
6723
  this.texture = new er({
6719
6724
  source: r,
@@ -6721,6 +6726,22 @@ var ki = class extends J {
6721
6726
  height: n
6722
6727
  });
6723
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
+ }
6724
6745
  };
6725
6746
  O([_(), D("design:type", Object)], ki.prototype, "fillWithShape", void 0), O([_(), D("design:type", Array)], ki.prototype, "effects", void 0);
6726
6747
  //#endregion
@@ -1,12 +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[];
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 纹理生命周期解耦。
18
+ */
19
+ protected _sourceCanvas?: HTMLCanvasElement;
20
+ /** `_sourceCanvas` 对应的图片地址,image 变更时用于失效旧副本 */
21
+ protected _sourceImage?: string;
7
22
  setProperties(properties?: Foreground): this;
8
23
  protected _updateProperty(key: string, value: any, oldValue: any): void;
9
24
  loadTexture(): Promise<void>;
10
- /** 把原纹理 + effects 烘焙成一张运行时 canvas,包成 CanvasTexture(gif/无 effects 时跳过) */
11
- 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;
12
35
  }
36
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.23.1",
4
+ "version": "0.23.3",
5
5
  "packageManager": "pnpm@10.19.0",
6
6
  "description": "A JavaScript WebGL rendering engine. only the ESM.",
7
7
  "author": "wxm",