modern-canvas 0.23.10 → 0.23.11

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
@@ -5011,6 +5011,9 @@ var Sr = class extends Tt {
5011
5011
  root = new xr().setTree(this);
5012
5012
  timeline = new br().setTree(this);
5013
5013
  nodeMap = /* @__PURE__ */ new Map();
5014
+ _boundFonts;
5015
+ _textRemeasureScheduled = !1;
5016
+ _onFontLoad = () => this._scheduleTextRemeasure();
5014
5017
  _backgroundColor = new T();
5015
5018
  _previousViewport;
5016
5019
  _currentViewport;
@@ -5027,13 +5030,27 @@ var Sr = class extends Tt {
5027
5030
  return this.nodeMap.get(e);
5028
5031
  }
5029
5032
  constructor(e) {
5030
- super(), this.on("nodeEnter", (e) => this.nodeMap.set(e.id, e)), this.on("nodeExit", (e) => this.nodeMap.delete(e.id)), this.setProperties(e);
5033
+ super(), this.on("nodeEnter", (e) => this.nodeMap.set(e.id, e)), this.on("nodeExit", (e) => this.nodeMap.delete(e.id)), this.setProperties(e), this._bindFonts(this.fonts);
5034
+ }
5035
+ _bindFonts(e) {
5036
+ this._boundFonts !== e && (this._boundFonts?.off("load", this._onFontLoad), this._boundFonts = e, e?.on("load", this._onFontLoad));
5037
+ }
5038
+ _scheduleTextRemeasure() {
5039
+ this._textRemeasureScheduled || (this._textRemeasureScheduled = !0, queueMicrotask(() => {
5040
+ this._textRemeasureScheduled = !1, this.nodeMap.forEach((e) => {
5041
+ let t = e.text;
5042
+ t?.enabled && typeof t.update == "function" && t.update();
5043
+ });
5044
+ }));
5031
5045
  }
5032
5046
  _updateProperty(e, t, n) {
5033
5047
  switch (super._updateProperty(e, t, n), e) {
5034
5048
  case "backgroundColor":
5035
5049
  this._backgroundColor.value = t;
5036
5050
  break;
5051
+ case "fonts":
5052
+ this._bindFonts(t), this._scheduleTextRemeasure();
5053
+ break;
5037
5054
  }
5038
5055
  }
5039
5056
  setProperties(e) {
@@ -5058,7 +5075,7 @@ var Sr = class extends Tt {
5058
5075
  t.activate(e, 0), L.draw(e), t.inactivate(e);
5059
5076
  }
5060
5077
  _destroy() {
5061
- super._destroy(), this.root.destroy(), this.input.destroy(), this.nodeMap.clear();
5078
+ super._destroy(), this._bindFonts(void 0), this.root.destroy(), this.input.destroy(), this.nodeMap.clear();
5062
5079
  }
5063
5080
  };
5064
5081
  k([_({ alias: "root.msaa" }), O("design:type", Boolean)], Sr.prototype, "msaa", void 0), k([_({ fallback: !1 }), O("design:type", Boolean)], Sr.prototype, "pixelate", void 0), k([_({ fallback: !1 }), O("design:type", Boolean)], Sr.prototype, "roundPixels", void 0), k([_(), O("design:type", Object)], Sr.prototype, "backgroundColor", void 0), k([_({
@@ -6474,7 +6491,7 @@ var X = class extends C {
6474
6491
  }
6475
6492
  let n = await yi();
6476
6493
  if (!n) {
6477
- console.warn("[modern-canvas] chart rendering requires the optional \"echarts\" dependency (e.g. `pnpm add echarts`)");
6494
+ console.warn("[modern-canvas] chart rendering requires the optional \"echarts\" dependency (e.g. `pnpm add echarts`)"), this._renderPlaceholder(e, t);
6478
6495
  return;
6479
6496
  }
6480
6497
  if (!(this.destroyed || !this.isValid())) {
@@ -6505,6 +6522,16 @@ var X = class extends C {
6505
6522
  this._texture.requestUpdate("source"), this._parent.requestDraw();
6506
6523
  }
6507
6524
  }
6525
+ _renderPlaceholder(e, t) {
6526
+ if (typeof document > "u" || !e || !t) return;
6527
+ let n = document.createElement("canvas");
6528
+ n.width = Math.ceil(e), n.height = Math.ceil(t);
6529
+ let r = n.getContext("2d");
6530
+ r && (r.fillStyle = "#f3f4f6", r.fillRect(0, 0, e, t), r.strokeStyle = "#d1d5db", r.setLineDash([6, 4]), r.strokeRect(1, 1, e - 2, t - 2), r.fillStyle = "#9ca3af", r.font = "14px sans-serif", r.textAlign = "center", r.textBaseline = "middle", r.fillText("图表需安装 echarts", e / 2, t / 2), this._texture ? this._texture.source = n : this._texture = new er({
6531
+ source: n,
6532
+ pixelRatio: 1
6533
+ }), this._texture.requestUpdate("source"), this._parent.requestDraw());
6534
+ }
6508
6535
  draw() {
6509
6536
  let e = this._texture;
6510
6537
  if (!e?.isValid()) return;
@@ -32,6 +32,8 @@ export declare class Element2DChart extends CoreObject implements NormalizedChar
32
32
  isValid(): boolean;
33
33
  /** Render with echarts into the offscreen canvas backing `_texture`. */
34
34
  update(): Promise<void>;
35
+ /** echarts 缺失时画一个虚线框 + 文案的占位纹理,而非静默空白。 */
36
+ protected _renderPlaceholder(width: number, height: number): void;
35
37
  /** Paint the echarts texture onto the element, like fill/background do. */
36
38
  draw(): void;
37
39
  /** Map the normalized idoc chart to an echarts option. */
@@ -46,6 +46,10 @@ export declare class SceneTree extends MainLoop {
46
46
  readonly root: Window;
47
47
  timeline: Timeline;
48
48
  readonly nodeMap: Map<string, Node>;
49
+ /** 已订阅 load 事件的 fonts 实例,用于在 fonts 切换 / 销毁时解绑。 */
50
+ protected _boundFonts?: Fonts;
51
+ protected _textRemeasureScheduled: boolean;
52
+ protected _onFontLoad: () => void;
49
53
  protected _backgroundColor: Color;
50
54
  protected _previousViewport?: Viewport;
51
55
  protected _currentViewport?: Viewport;
@@ -54,6 +58,14 @@ export declare class SceneTree extends MainLoop {
54
58
  setCurrentViewport(viewport: Viewport | undefined): void;
55
59
  getNodeById<T extends Node = Node>(id: string): T | undefined;
56
60
  constructor(properties?: Partial<SceneTreeProperties>);
61
+ /** 订阅 fonts 的 load 事件,并在切换 fonts / 销毁时解绑,避免重复订阅与泄漏。 */
62
+ protected _bindFonts(next?: Fonts): void;
63
+ /**
64
+ * 字体到位后,重排树内全部文字(含表格 back 层单元格——它们也在 nodeMap)。
65
+ * 一个 tick 内多次 load 合并为一次。文字创建时字体可能尚未就绪、按 0 宽 glyph 测量挤成一坨,
66
+ * 这里在字体可用时自动重新测量,使所有消费方无需各自轮询字体状态。
67
+ */
68
+ protected _scheduleTextRemeasure(): void;
57
69
  protected _updateProperty(key: string, value: any, oldValue: any): void;
58
70
  setProperties(properties?: Record<string, any>): this;
59
71
  log(...args: any[]): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.23.10",
4
+ "version": "0.23.11",
5
5
  "packageManager": "pnpm@10.19.0",
6
6
  "description": "A JavaScript WebGL rendering engine. only the ESM.",
7
7
  "author": "wxm",
@@ -62,7 +62,7 @@
62
62
  "dependencies": {
63
63
  "colord": "^2.9.3",
64
64
  "earcut": "^3.0.2",
65
- "modern-font": "^0.6.0",
65
+ "modern-font": "^0.6.1",
66
66
  "modern-idoc": "^0.11.9",
67
67
  "modern-path2d": "^1.8.5",
68
68
  "modern-text": "^2.0.6"