@x-cmd/draw 0.5.10 → 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.
- package/dist/draw-client.iife.js +24 -24
- package/dist/render/arch3d.js +8 -6
- package/dist/render/scene3d.d.ts +5 -0
- package/dist/render/scene3d.js +127 -9
- package/package.json +1 -1
package/dist/render/arch3d.js
CHANGED
|
@@ -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="
|
|
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="
|
|
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
|
-
|
|
147
|
-
|
|
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="
|
|
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>`);
|
package/dist/render/scene3d.d.ts
CHANGED
|
@@ -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 {
|
package/dist/render/scene3d.js
CHANGED
|
@@ -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,30 +289,119 @@ 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
|
|
294
|
-
shadow:
|
|
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
|
}
|
|
325
|
+
// 节点盒(顶面 + 侧面的投影包围盒)—— 别的字要躲开它们
|
|
326
|
+
// (原来标签正好落在边中点 ⇒ 有时正好压在节点的字上 ✗)
|
|
327
|
+
const nodeBoxes = nodes.map((n) => {
|
|
328
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
329
|
+
for (const p of [...n.top, ...n.sides.flatMap((s) => s.pts)]) {
|
|
330
|
+
x0 = Math.min(x0, p[0]);
|
|
331
|
+
x1 = Math.max(x1, p[0]);
|
|
332
|
+
y0 = Math.min(y0, p[1]);
|
|
333
|
+
y1 = Math.max(y1, p[1]);
|
|
334
|
+
}
|
|
335
|
+
return { x0, y0, x1, y1 };
|
|
336
|
+
});
|
|
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 = [];
|
|
349
|
+
/** 折线上按参数取点(0..1) */
|
|
350
|
+
const at = (pr, t) => {
|
|
351
|
+
const seg = (pr.length - 1) * t;
|
|
352
|
+
const i = Math.min(pr.length - 2, Math.floor(seg));
|
|
353
|
+
const f = seg - i;
|
|
354
|
+
const a = pr[i];
|
|
355
|
+
const b = pr[i + 1];
|
|
356
|
+
return { x: a.x + (b.x - a.x) * f, y: a.y + (b.y - a.y) * f };
|
|
357
|
+
};
|
|
301
358
|
const edges = scene.edges.map((e) => {
|
|
302
359
|
const pr = e.pts.map((p) => project(p, basis, focal));
|
|
303
360
|
const mid = pr[Math.floor((pr.length - 1) / 2)];
|
|
361
|
+
let lx = mid.x;
|
|
362
|
+
let ly = mid.y;
|
|
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]) {
|
|
368
|
+
const q = at(pr, t);
|
|
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;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
placed.push(labelBox(lx, ly, w, EDGE_LABEL_FS));
|
|
397
|
+
}
|
|
304
398
|
return {
|
|
305
399
|
from: e.from,
|
|
306
400
|
to: e.to,
|
|
307
401
|
label: e.label,
|
|
308
402
|
pts: pr.map((p) => [p.x, p.y]),
|
|
309
|
-
lx
|
|
310
|
-
ly
|
|
403
|
+
lx,
|
|
404
|
+
ly,
|
|
311
405
|
depth: mid.depth,
|
|
312
406
|
};
|
|
313
407
|
});
|
|
@@ -334,7 +428,32 @@ export function projectScene(scene, cam) {
|
|
|
334
428
|
[pr[b].x, pr[b].y],
|
|
335
429
|
]);
|
|
336
430
|
const c0 = project({ x: (min.x + max.x) / 2, y: (min.y + max.y) / 2, z: max.z }, basis, focal);
|
|
337
|
-
|
|
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 };
|
|
338
457
|
});
|
|
339
458
|
const tiers = scene.tiers.map((t) => {
|
|
340
459
|
const { min, max } = t;
|
|
@@ -415,9 +534,8 @@ export function projectScene(scene, cam) {
|
|
|
415
534
|
for (const t of tiers)
|
|
416
535
|
for (const p of t.pts)
|
|
417
536
|
eat(p[0], p[1]);
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
eat(p[0], p[1]);
|
|
537
|
+
// 地面网格**不算进画布范围**:它只是一层背景提示,画出来比内容大得多
|
|
538
|
+
// (算了它 ⇒ 画布宽出一大截,内容缩在左边一小块 ✗)
|
|
421
539
|
if (!Number.isFinite(minX)) {
|
|
422
540
|
minX = minY = 0;
|
|
423
541
|
maxX = maxY = 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.5.
|
|
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",
|