@x-cmd/draw 0.5.10 → 0.5.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.
@@ -298,16 +298,56 @@ export function projectScene(scene, cam) {
298
298
  depth: topMid.depth,
299
299
  });
300
300
  }
301
+ // 节点盒(顶面 + 侧面的投影包围盒)—— 边上的文字要躲开它们
302
+ // (原来标签正好落在边中点 ⇒ 有时正好压在节点的字上 ✗)
303
+ const nodeBoxes = nodes.map((n) => {
304
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
305
+ for (const p of [...n.top, ...n.sides.flatMap((s) => s.pts)]) {
306
+ x0 = Math.min(x0, p[0]);
307
+ x1 = Math.max(x1, p[0]);
308
+ y0 = Math.min(y0, p[1]);
309
+ y1 = Math.max(y1, p[1]);
310
+ }
311
+ return { x0, y0, x1, y1 };
312
+ });
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);
315
+ /** 折线上按参数取点(0..1) */
316
+ const at = (pr, t) => {
317
+ const seg = (pr.length - 1) * t;
318
+ const i = Math.min(pr.length - 2, Math.floor(seg));
319
+ const f = seg - i;
320
+ const a = pr[i];
321
+ const b = pr[i + 1];
322
+ return { x: a.x + (b.x - a.x) * f, y: a.y + (b.y - a.y) * f };
323
+ };
301
324
  const edges = scene.edges.map((e) => {
302
325
  const pr = e.pts.map((p) => project(p, basis, focal));
303
326
  const mid = pr[Math.floor((pr.length - 1) / 2)];
327
+ let lx = mid.x;
328
+ 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]) {
333
+ 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;
341
+ }
342
+ }
343
+ }
304
344
  return {
305
345
  from: e.from,
306
346
  to: e.to,
307
347
  label: e.label,
308
348
  pts: pr.map((p) => [p.x, p.y]),
309
- lx: mid.x,
310
- ly: mid.y,
349
+ lx,
350
+ ly,
311
351
  depth: mid.depth,
312
352
  };
313
353
  });
@@ -415,9 +455,8 @@ export function projectScene(scene, cam) {
415
455
  for (const t of tiers)
416
456
  for (const p of t.pts)
417
457
  eat(p[0], p[1]);
418
- for (const l of grid)
419
- for (const p of l)
420
- eat(p[0], p[1]);
458
+ // 地面网格**不算进画布范围**:它只是一层背景提示,画出来比内容大得多
459
+ // (算了它 ⇒ 画布宽出一大截,内容缩在左边一小块 ✗)
421
460
  if (!Number.isFinite(minX)) {
422
461
  minX = minY = 0;
423
462
  maxX = maxY = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-cmd/draw",
3
- "version": "0.5.10",
3
+ "version": "0.5.11",
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",