@x-cmd/draw 0.5.11 → 0.5.12

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.
@@ -12,7 +12,7 @@
12
12
  import { parseNodes, parseEdges, parseBoundaries } from './archmap.js';
13
13
  import { esc } from './svg.js';
14
14
  import { iconOf } from './icons.js';
15
- import { buildScene, projectScene, DEFAULT_CAMERA } from './scene3d.js';
15
+ import { buildScene, projectScene, DEFAULT_CAMERA, NODE_LABEL_FS, EDGE_LABEL_FS, GROUP_LABEL_FS } from './scene3d.js';
16
16
  /** 语义类型 → 色板序号(跟库那套一致:ext 中性,其余各有各的色) */
17
17
  const TYPE_COLOR = {
18
18
  fe: 0, be: 1, db: 2, cloud: 3, sec: 4, bus: 5, svc: 6,
@@ -121,7 +121,7 @@ function sceneSvg(p, scene, interactive, bstyle, planes, aux) {
121
121
  parts.push(`<g class="a3d-tier" data-tier="${t.tier}">` +
122
122
  sides +
123
123
  `<polygon class="a3d-tier-top" points="${pts}" fill="var(--d-s${t.tier === 0 ? 4 : t.tier === 1 ? 5 : 6})" fill-opacity="0.07" stroke="var(--d-grid)" stroke-width="1"/>` +
124
- `<text class="a3d-tier-name" x="${r2(t.lx)}" y="${r2(t.ly)}" font-size="10" fill="var(--d-faint)" text-anchor="middle">${esc(nameEn)}</text>` +
124
+ `<text class="a3d-tier-name" x="${r2(t.lx)}" y="${r2(t.ly)}" font-size="${EDGE_LABEL_FS}" fill="var(--d-faint)" text-anchor="middle">${esc(nameEn)}</text>` +
125
125
  `</g>`);
126
126
  }
127
127
  // 分组的辅助线:默认只留个牌子(那些线框是多余的,要做再要)
@@ -134,7 +134,7 @@ function sceneSvg(p, scene, interactive, bstyle, planes, aux) {
134
134
  : `<path class="a3d-group-box" d="${d}" fill="none" stroke="var(--d-faint)" stroke-opacity="0.5" stroke-width="1" stroke-dasharray="4 4"/>`;
135
135
  parts.push(`<g class="a3d-group"${ia}>` +
136
136
  box +
137
- `<text x="${r2(g.lx)}" y="${r2(g.ly)}" font-size="11" fill="var(--d-muted)" text-anchor="middle">${esc(g.label)}</text>` +
137
+ `<text x="${r2(g.lx)}" y="${r2(g.ly)}" font-size="${GROUP_LABEL_FS}" fill="var(--d-muted)" text-anchor="middle">${esc(g.label)}</text>` +
138
138
  `</g>`);
139
139
  }
140
140
  // 边
@@ -143,8 +143,10 @@ function sceneSvg(p, scene, interactive, bstyle, planes, aux) {
143
143
  const d = e.pts.map((q, i) => `${i === 0 ? 'M' : 'L'}${r2(q[0])} ${r2(q[1])}`).join('');
144
144
  parts.push(`<g class="a3d-edge"${ia}>` +
145
145
  // data-from/data-to 也要落在 path 上 —— explore 那套点击高亮找的是 path[data-from]
146
- `<path class="a3d-edge-path" d="${d}"${ia} fill="none"/>` +
147
- (e.label ? `<text class="a3d-edge-label" data-edge-label="${esc(e.from)} ${esc(e.to)}" x="${r2(e.lx)}" y="${r2(e.ly)}" font-size="10" fill="var(--d-muted)" text-anchor="middle">${esc(e.label)}</text>` : '') +
146
+ // 线的颜色**写在属性上**(不只靠 CSS):SVG 单独存下来、或者转成 PNG 时,
147
+ // 那些选择器未必生效 ⇒ 只靠 CSS 的话线整个没了 ✗
148
+ `<path class="a3d-edge-path" d="${d}"${ia} fill="none" stroke="#7DD3FC" stroke-opacity="0.8" stroke-width="1.5"/>` +
149
+ (e.label ? `<text class="a3d-edge-label" data-edge-label="${esc(e.from)} ${esc(e.to)}" x="${r2(e.lx)}" y="${r2(e.ly)}" font-size="${EDGE_LABEL_FS}" fill="var(--d-muted)" text-anchor="middle">${esc(e.label)}</text>` : '') +
148
150
  `</g>`);
149
151
  }
150
152
  // 节点:顶面 + 四个侧面(看不见的那些不透明度 0)
@@ -164,7 +166,7 @@ function sceneSvg(p, scene, interactive, bstyle, planes, aux) {
164
166
  (n.icon
165
167
  ? `<image class="a3d-icon" href="${s.icon}" x="${r2(n.icon.x - 17)}" y="${r2(n.icon.y - 17)}" width="34" height="34" preserveAspectRatio="xMidYMid meet"/>`
166
168
  : '') +
167
- `<text class="a3d-label" x="${r2(n.lx)}" y="${r2(n.ly)}" font-size="12" fill="var(--d-ink)" text-anchor="middle">${esc(s.label)}</text>` +
169
+ `<text class="a3d-label" x="${r2(n.lx)}" y="${r2(n.ly)}" font-size="${NODE_LABEL_FS}" fill="var(--d-ink)" text-anchor="middle">${esc(s.label)}</text>` +
168
170
  `</g>`);
169
171
  }
170
172
  parts.push(`<g class="a3d-layer">${nodeParts.join('')}</g>`);
@@ -1,3 +1,6 @@
1
+ export declare const NODE_LABEL_FS = 12;
2
+ export declare const EDGE_LABEL_FS = 10;
3
+ export declare const GROUP_LABEL_FS = 11;
1
4
  export interface Vec3 {
2
5
  x: number;
3
6
  y: number;
@@ -135,6 +138,8 @@ export interface ProjNode {
135
138
  /** 标签落点 */
136
139
  lx: number;
137
140
  ly: number;
141
+ /** 标签是不是挪到了板砖下面(名字太长、贴不上正面 ⇒ 挂到下面当说明) */
142
+ labelBelow: boolean;
138
143
  depth: number;
139
144
  }
140
145
  export interface ProjEdge {
@@ -25,6 +25,11 @@
25
25
  // ── 3D 坐标 ──────────────────────────────────────────────────────
26
26
  // x 向右、y 向里(纵深)、z 向上。节点是一块板砖(有厚度),浮在自己那层的高度上。
27
27
  import { layeredCore } from '../layout/layered.js';
28
+ import { textW } from './svg.js';
29
+ // 三个字号(渲染器也用这几个 —— 尺寸得是**一处**说了算,不然量不准)
30
+ export const NODE_LABEL_FS = 12; // 节点的名字
31
+ export const EDGE_LABEL_FS = 10; // 边上的字(「转发」「查询」)
32
+ export const GROUP_LABEL_FS = 11; // 分组的牌子(「公有子网」)
28
33
  /** 默认相机:等轴测那一眼(俯 30°,转 30°) */
29
34
  export const DEFAULT_CAMERA = { yaw: 32, pitch: -38, dist: 1400, focal: 1500 };
30
35
  /** 层与层的高度:底 0、中 tierGap、顶 2*tierGap */
@@ -284,21 +289,40 @@ export function projectScene(scene, cam) {
284
289
  const mid = project(n.p, basis, focal);
285
290
  // 顶面朝不朝相机:相机在块的上方才看得见顶面
286
291
  const topVis = basis.cam.z > n.p.z + n.h / 2;
292
+ // 标签贴在**正面那块竖面**上 —— 那块面有多宽,字就有多宽的地方
293
+ let faceW = 0;
294
+ sides.forEach((s, i) => {
295
+ if (!s.vis)
296
+ return;
297
+ if (Math.abs(sideNormals[i].y) < 0.5)
298
+ return; // 只看竖面(法线朝前后)
299
+ const xs = s.pts.map((p) => p[0]);
300
+ faceW = Math.max(faceW, Math.max(...xs) - Math.min(...xs));
301
+ });
302
+ if (!faceW) {
303
+ // 没有竖面朝着相机(从侧面看)⇒ 退一步,用顶面的跨度
304
+ const xs = top.map((p) => p[0]);
305
+ faceW = Math.max(...xs) - Math.min(...xs);
306
+ }
307
+ // 字比正面宽 ⇒ 硬塞进去会溢出方块 ✗ ⇒ 挂到板砖下面当说明
308
+ const labelBelow = textW(n.label, NODE_LABEL_FS) > faceW + 2;
309
+ const bottom = [pr[0], pr[1], pr[2], pr[3]].map((p) => [p.x, p.y]);
287
310
  nodes.push({
288
311
  id: n.id,
289
312
  top,
290
313
  // 图标:贴在板上,直立着(跟着面歪就认不出是什么)
291
314
  icon: n.icon ? { x: topMid.x, y: topMid.y - 20 } : null,
292
315
  topVis,
293
- bottom: [pr[0], pr[1], pr[2], pr[3]].map((p) => [p.x, p.y]),
294
- shadow: [pr[0], pr[1], pr[2], pr[3]].map((p) => [p.x, p.y]),
316
+ bottom,
317
+ shadow: bottom,
295
318
  sides,
296
319
  lx: mid.x,
297
- ly: mid.y + 17, // 标签在板下(图标在板上,各占一边)
320
+ ly: labelBelow ? Math.max(...bottom.map((p) => p[1])) + 13 : mid.y + 17,
321
+ labelBelow,
298
322
  depth: topMid.depth,
299
323
  });
300
324
  }
301
- // 节点盒(顶面 + 侧面的投影包围盒)—— 边上的文字要躲开它们
325
+ // 节点盒(顶面 + 侧面的投影包围盒)—— 别的字要躲开它们
302
326
  // (原来标签正好落在边中点 ⇒ 有时正好压在节点的字上 ✗)
303
327
  const nodeBoxes = nodes.map((n) => {
304
328
  let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
@@ -310,8 +334,18 @@ export function projectScene(scene, cam) {
310
334
  }
311
335
  return { x0, y0, x1, y1 };
312
336
  });
313
- /** 这个点离所有节点盒都够远吗(够远 = 标签放这儿不压字 ✓) */
314
- const clearOfNodes = (x, y) => !nodeBoxes.some((b) => x > b.x0 - 8 && x < b.x1 + 8 && y > b.y0 - 6 && y < b.y1 + 6);
337
+ /** 一个字的盒子(锚点在中点上) */
338
+ const labelBox = (x, y, w, fs) => ({
339
+ x0: x - w / 2, x1: x + w / 2,
340
+ y0: y - fs * 0.8, y1: y + fs * 0.25,
341
+ });
342
+ const hits = (b, others) => others.some((o) => b.x0 < o.x1 + 2 && b.x1 > o.x0 - 2 && b.y0 < o.y1 + 2 && b.y1 > o.y0 - 2);
343
+ /** 挂到板砖下面的那些名字(它们在方块外面 ⇒ 也是要躲的) */
344
+ const nodeLabelBoxes = nodes
345
+ .filter((n) => n.labelBelow)
346
+ .map((n) => labelBox(n.lx, n.ly, textW(scene.nodes.find((s) => s.id === n.id).label, NODE_LABEL_FS), NODE_LABEL_FS));
347
+ /** 已经放好的字(边上的、组上的)—— 后放的躲先放的 */
348
+ const placed = [];
315
349
  /** 折线上按参数取点(0..1) */
316
350
  const at = (pr, t) => {
317
351
  const seg = (pr.length - 1) * t;
@@ -326,20 +360,40 @@ export function projectScene(scene, cam) {
326
360
  const mid = pr[Math.floor((pr.length - 1) / 2)];
327
361
  let lx = mid.x;
328
362
  let ly = mid.y;
329
- if (!clearOfNodes(lx, ly)) {
330
- // 中点压着节点 ⇒ 沿着边找一个空位(离中点越近越好)
331
- let bestD = Infinity;
332
- for (const t of [0.5, 0.42, 0.58, 0.34, 0.66, 0.26, 0.74, 0.18, 0.82]) {
363
+ if (e.label) {
364
+ const w = textW(e.label, EDGE_LABEL_FS);
365
+ // 候选点:沿边走(离中点越近越好)× 垂直挪开一点(别骑在线上)
366
+ const cands = [];
367
+ for (const t of [0.5, 0.44, 0.56, 0.38, 0.62, 0.32, 0.68, 0.26, 0.74, 0.2, 0.8]) {
333
368
  const q = at(pr, t);
334
- if (!clearOfNodes(q.x, q.y))
335
- continue;
336
- const d = Math.hypot(q.x - mid.x, q.y - mid.y);
337
- if (d < bestD) {
338
- bestD = d;
339
- lx = q.x;
340
- ly = q.y;
369
+ const i = Math.min(pr.length - 2, Math.floor((pr.length - 1) * t));
370
+ const dx = pr[i + 1].x - pr[i].x;
371
+ const dy = pr[i + 1].y - pr[i].y;
372
+ const len = Math.hypot(dx, dy) || 1;
373
+ const px = -dy / len;
374
+ const py = dx / len;
375
+ for (const off of [0, -12, 12, -24, 24]) {
376
+ cands.push({ x: q.x + px * off, y: q.y + py * off });
377
+ }
378
+ }
379
+ const ok = (c) => {
380
+ const b = labelBox(c.x, c.y, w, EDGE_LABEL_FS);
381
+ return !hits(b, nodeBoxes) && !hits(b, nodeLabelBoxes) && !hits(b, placed);
382
+ };
383
+ const pick = cands.find(ok);
384
+ if (pick) {
385
+ lx = pick.x;
386
+ ly = pick.y;
387
+ }
388
+ else {
389
+ // 哪儿都压着 ⇒ 退一步:至少别压着别的字
390
+ const alt = cands.find((c) => !hits(labelBox(c.x, c.y, w, EDGE_LABEL_FS), placed));
391
+ if (alt) {
392
+ lx = alt.x;
393
+ ly = alt.y;
341
394
  }
342
395
  }
396
+ placed.push(labelBox(lx, ly, w, EDGE_LABEL_FS));
343
397
  }
344
398
  return {
345
399
  from: e.from,
@@ -374,7 +428,32 @@ export function projectScene(scene, cam) {
374
428
  [pr[b].x, pr[b].y],
375
429
  ]);
376
430
  const c0 = project({ x: (min.x + max.x) / 2, y: (min.y + max.y) / 2, z: max.z }, basis, focal);
377
- return { label: g.label, ids: g.ids, lines, lx: c0.x, ly: c0.y, depth: c0.depth };
431
+ // 牌子挂在这个盒子的**外面**(上方 → 左 → 右 → 下方)——
432
+ // 原来直接挂在盒子中间 ⇒ 正好压在成员方块上 ✗
433
+ const gw = textW(g.label, GROUP_LABEL_FS);
434
+ let gx0 = Infinity, gy0 = Infinity, gx1 = -Infinity, gy1 = -Infinity;
435
+ for (const p of pr) {
436
+ gx0 = Math.min(gx0, p.x);
437
+ gx1 = Math.max(gx1, p.x);
438
+ gy0 = Math.min(gy0, p.y);
439
+ gy1 = Math.max(gy1, p.y);
440
+ }
441
+ const cx = (gx0 + gx1) / 2;
442
+ const cy = (gy0 + gy1) / 2;
443
+ const cands = [
444
+ { x: cx, y: gy0 - 7 },
445
+ { x: gx0 - gw / 2 - 8, y: cy },
446
+ { x: gx1 + gw / 2 + 8, y: cy },
447
+ { x: cx, y: gy1 + 15 },
448
+ { x: c0.x, y: c0.y },
449
+ ];
450
+ const gok = (c) => {
451
+ const b = labelBox(c.x, c.y, gw, GROUP_LABEL_FS);
452
+ return !hits(b, nodeBoxes) && !hits(b, nodeLabelBoxes) && !hits(b, placed);
453
+ };
454
+ const gpick = cands.find(gok) ?? cands[0];
455
+ placed.push(labelBox(gpick.x, gpick.y, gw, GROUP_LABEL_FS));
456
+ return { label: g.label, ids: g.ids, lines, lx: gpick.x, ly: gpick.y, depth: c0.depth };
378
457
  });
379
458
  const tiers = scene.tiers.map((t) => {
380
459
  const { min, max } = t;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-cmd/draw",
3
- "version": "0.5.11",
3
+ "version": "0.5.12",
4
4
  "description": "SVG chart rendering as a service, a library, and a CLI — pie/bar/hbar/line/area/radar/gauge/scatter/combo/sankey/qr, same params everywhere",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",