@x-cmd/draw 0.5.13 → 0.5.14
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/client.d.ts +1 -0
- package/dist/client.js +2 -0
- package/dist/draw-client.iife.js +22 -22
- package/dist/render/globe-earth.d.ts +1 -0
- package/dist/render/globe-earth.js +26 -59
- package/dist/render/globe-labels.d.ts +31 -0
- package/dist/render/globe-labels.js +72 -0
- package/dist/render/globe-style.d.ts +35 -0
- package/dist/render/globe-style.js +69 -0
- package/dist/render/globe.js +31 -43
- package/package.json +1 -1
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
// 键:**主键一律 ISO 3166**(国家 alpha-2 ✓,别名 alpha-3 / 名字 / 世行码 ✓)。
|
|
22
22
|
import { globeProjection, globeLandPath, globeAreaPath, globePoint, globeArc, globeCentroid, resolveCountry, resolveGlobeArea, GLOBE_COUNTRIES, DEFAULT_VIEW, ARC_LABEL_MAX, } from '@x-cmd/draw/globe';
|
|
23
23
|
import { WORLD_COUNTRIES } from '../world-data.js';
|
|
24
|
+
import { globeStyle, sphereAttrs, landAttrs, edgeAttrs, edgeLabelAttrs, haloAttrs, dotAttrs, labelAttrs, labelPadAttrs, } from './globe-style.js';
|
|
25
|
+
import { placeCityLabels } from './globe-labels.js';
|
|
24
26
|
/** 转义(标签与名字要进 SVG ✓)—— 自己写一份,免得为它把整个库拖进来 ✓ */
|
|
25
27
|
const esc = (v) => v.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
26
28
|
/** 类型 → 色(跟结构图那一族一致 ✓) */
|
|
@@ -303,11 +305,11 @@ export function renderEarthGlobe(scene, opts = {}) {
|
|
|
303
305
|
// 大气圈的外沿**不超过球的半径** ✓ —— 伸到画布外会被切掉 ⇒ 看着像「地球超出画面」✗
|
|
304
306
|
base.push(`<circle class="g-atmo" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad)}" fill="url(#g-atmo)"/>`);
|
|
305
307
|
base.push(`<circle class="g-glow" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad)}" fill="none" stroke="var(--d-link)" stroke-opacity="0.25" stroke-width="10"/>`);
|
|
306
|
-
base.push(`<circle class="g-sphere" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad)}" fill="url(#g-grad)"/>`);
|
|
308
|
+
base.push(`<circle class="g-sphere" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad)}" fill="url(#g-grad)"${sphereAttrs}/>`);
|
|
307
309
|
// 陆地:整个球面拼成一条(没有国界的接缝 ✓)
|
|
308
310
|
const land = globeLandPath(width, height, view, { tolPx: TOL_PX });
|
|
309
311
|
if (land)
|
|
310
|
-
base.push(`<path class="g-land" d="${land}"/>`);
|
|
312
|
+
base.push(`<path class="g-land" d="${land}"${landAttrs('#2A3444', '#7C8CA5', 0.75, 0.5)}/>`);
|
|
311
313
|
// 有值的一块地:国土着色 ✓(量值越大越暖 ✓)
|
|
312
314
|
// 分层(g-l-countries)—— 客户端转球时要把背面转过来的**补画**进去 ✓
|
|
313
315
|
const countryParts = [];
|
|
@@ -340,11 +342,11 @@ export function renderEarthGlobe(scene, opts = {}) {
|
|
|
340
342
|
? ` data-tip="${esc(`${e.from} → ${e.to}${e.label ? ` · ${e.label}` : ''}`)}"` : '';
|
|
341
343
|
edgeParts.push(`<g class="g-edge${manyEdges ? ' many' : ''}"${ia}>` +
|
|
342
344
|
// 命中区:一条粗的透明线(2px 的线太细,悬停不着 ✗)
|
|
343
|
-
`<path class="g-edge-hit" d="${arc.d}" fill="none"/>` +
|
|
344
|
-
`<path d="${arc.d}"${ia}${tip} fill="none"/>` +
|
|
345
|
+
`<path class="g-edge-hit" d="${arc.d}" fill="none" stroke="transparent" stroke-width="12"/>` +
|
|
346
|
+
`<path d="${arc.d}"${ia}${tip} fill="none"${edgeAttrs(false, manyEdges)}/>` +
|
|
345
347
|
// 标签摆在**弧的中点**(不是两端经纬度的平均 —— 那不在弧上 ✗)
|
|
346
348
|
(e.label && showEdgeLabels && arc.mid
|
|
347
|
-
? `<text class="g-edge-label" x="${r2(arc.mid.x)}" y="${r2(arc.mid.y - 12)}" font-size="20" text-anchor="middle">${esc(e.label)}</text>`
|
|
349
|
+
? `<text class="g-edge-label" x="${r2(arc.mid.x)}" y="${r2(arc.mid.y - 12)}" font-size="20" text-anchor="middle"${edgeLabelAttrs}>${esc(e.label)}</text>`
|
|
348
350
|
: '') +
|
|
349
351
|
`</g>`);
|
|
350
352
|
}
|
|
@@ -358,30 +360,32 @@ export function renderEarthGlobe(scene, opts = {}) {
|
|
|
358
360
|
degree.set(e.to, (degree.get(e.to) ?? 0) + 1);
|
|
359
361
|
}
|
|
360
362
|
const nodeParts = [];
|
|
361
|
-
//
|
|
362
|
-
|
|
363
|
-
const
|
|
364
|
-
const hits = (b) => labelBoxes.some((o) => !(b[2] < o[0] || b[0] > o[2] || b[3] < o[1] || b[1] > o[3]));
|
|
363
|
+
// 城名摆在哪儿:两个球共用一份避让表 ✓(上海挨着东京 ⇒ 别叠成一团)
|
|
364
|
+
const LABEL_FS = 28;
|
|
365
|
+
const visible = [];
|
|
365
366
|
for (const n of scene.nodes) {
|
|
366
367
|
const at = globePoint(n.lon, n.lat, width, height, view);
|
|
367
368
|
if (!at)
|
|
368
369
|
continue; // 背面 ⇒ 不画 ✓(转过来再补 ✓)
|
|
369
|
-
const ia = opts.interactive ? ` data-node="${esc(n.id)}" data-tip="${esc(tipOf(n))}" tabindex="0"` : '';
|
|
370
370
|
const byVal = typeof n.value === 'number' && n.value > 0 ? 12 * Math.sqrt(n.value / maxVal) : 0;
|
|
371
371
|
const byDeg = Math.min(7, (degree.get(n.id) ?? 0) * 0.9);
|
|
372
|
-
|
|
372
|
+
visible.push({ n, at, rr: (n.hl ? 15 : 7) + byVal + byDeg });
|
|
373
|
+
}
|
|
374
|
+
const labels = placeCityLabels(visible.map((v) => ({ id: v.n.id, label: v.n.label, x: v.at.x, y: v.at.y, r: v.rr, hl: v.n.hl })), LABEL_FS,
|
|
375
|
+
// 名字不许跑出画布(球边上那些点,名字得往里放 ✓)
|
|
376
|
+
[0, 0, width, height]);
|
|
377
|
+
for (const { n, at, rr } of visible) {
|
|
378
|
+
const ia = opts.interactive ? ` data-node="${esc(n.id)}" data-tip="${esc(tipOf(n))}" tabindex="0"` : '';
|
|
379
|
+
const lab = labels.get(n.id);
|
|
373
380
|
nodeParts.push(`<g class="g-node${n.hl ? ' hl' : ''}"${ia}>` +
|
|
374
381
|
(n.hl ? `<circle class="g-ring" cx="${r2(at.x)}" cy="${r2(at.y)}" r="${r2(rr + 8)}" fill="none" stroke="${n.color}" stroke-width="2.5"/>` : '') +
|
|
375
|
-
`<circle class="g-dot-halo" cx="${r2(at.x)}" cy="${r2(at.y)}" r="${r2(rr + 4)}" fill="${n.color}"/>` +
|
|
376
|
-
`<circle class="g-dot" cx="${r2(at.x)}" cy="${r2(at.y)}" r="${r2(rr)}" fill="${n.color}"/>` +
|
|
377
|
-
(
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
labelBoxes.push(box);
|
|
383
|
-
return `<text class="g-label" x="${r2(at.x)}" y="${r2(at.y - 26)}" font-size="28" text-anchor="middle">${esc(n.label)}</text>`;
|
|
384
|
-
})() +
|
|
382
|
+
`<circle class="g-dot-halo" cx="${r2(at.x)}" cy="${r2(at.y)}" r="${r2(rr + 4)}" fill="${n.color}"${haloAttrs(n.hl)}/>` +
|
|
383
|
+
`<circle class="g-dot" cx="${r2(at.x)}" cy="${r2(at.y)}" r="${r2(rr)}" fill="${n.color}"${dotAttrs}/>` +
|
|
384
|
+
// 名字:底下垫一块深色(house style:字不描边 ⇒ 垫色)
|
|
385
|
+
(lab
|
|
386
|
+
? `<rect x="${r2(lab.pad[0])}" y="${r2(lab.pad[1])}" width="${r2(lab.pad[2] - lab.pad[0])}" height="${r2(lab.pad[3] - lab.pad[1])}" rx="4"${labelPadAttrs}/>` +
|
|
387
|
+
`<text class="g-label" x="${r2(lab.x)}" y="${r2(lab.y)}" font-size="${LABEL_FS}" text-anchor="${lab.anchor}"${labelAttrs(n.hl)}>${esc(n.label)}</text>`
|
|
388
|
+
: '') +
|
|
385
389
|
`</g>`);
|
|
386
390
|
}
|
|
387
391
|
// 色标 ✓ —— 分段块(跟平面地图的 visualMap 一个样 ✓)
|
|
@@ -424,41 +428,4 @@ function tipOf(n) {
|
|
|
424
428
|
const at = `${Math.abs(n.lat).toFixed(1)}°${n.lat >= 0 ? 'N' : 'S'} ${Math.abs(n.lon).toFixed(1)}°${n.lon >= 0 ? 'E' : 'W'}`;
|
|
425
429
|
return n.sub ? `${n.label} · ${n.sub} · ${at}` : `${n.label} · ${at}`;
|
|
426
430
|
}
|
|
427
|
-
/** 球的样子
|
|
428
|
-
function globeStyle() {
|
|
429
|
-
return (`<style>:root{color-scheme:dark;` +
|
|
430
|
-
`--d-s1:#34D399;--d-s2:#F87171;--d-s3:#A78BFA;--d-s4:#22D3EE;--d-s5:#60A5FA;--d-s6:#FB923C;` +
|
|
431
|
-
`--d-s7:#4ADE80;--d-s8:#FB7185;--d-s9:#F59E0B;--d-s10:#0EA5E9;` +
|
|
432
|
-
`--d-ink:#FAFAFA;--d-text:#D4D4D8;--d-muted:#A1A1AA;--d-faint:#71717A;--d-grid:#29292E;--d-card:#0E0F11;--d-link:#60A5FA;` +
|
|
433
|
-
`#am .g-sphere{fill-opacity:.92}` +
|
|
434
|
-
`#am .g-land{fill:#2A3444;fill-opacity:.75;stroke:#7C8CA5;stroke-opacity:.5;stroke-width:.9}` +
|
|
435
|
-
`#am .g-land.back{fill-opacity:.16;stroke-opacity:.12}` +
|
|
436
|
-
`#am .g-grid{stroke:#7DD3FC;stroke-opacity:.18;stroke-width:2}` +
|
|
437
|
-
`#am .g-edge path{stroke:#DBEAFE;stroke-opacity:.92;stroke-width:3}` +
|
|
438
|
-
// 命中区:看不见的粗线 —— 弧太细悬停不着 ⇒ 给它一条 12px 的透明线接着 ✓
|
|
439
|
-
`#am .g-edge path.g-edge-hit{stroke:transparent;stroke-width:12;pointer-events:stroke}` +
|
|
440
|
-
// 边多 ⇒ 画细、画淡(不然三十多条白线把球糊住 ✗)
|
|
441
|
-
`#am .g-edge.many path{stroke-opacity:.8;stroke-width:2.4}` +
|
|
442
|
-
`#am .g-edge.back path{stroke-opacity:.2}` +
|
|
443
|
-
`#am .g-edge-label{fill:#DBEAFE;fill-opacity:.9}` +
|
|
444
|
-
`#am .g-node .g-dot-halo{fill-opacity:.2}` +
|
|
445
|
-
`#am .g-node .g-dot{stroke:#050B1C;stroke-width:1.5}` +
|
|
446
|
-
`#am .g-node .g-label{fill:#F0F9FF;fill-opacity:.95;paint-order:stroke;stroke:#050B1C;stroke-width:3;stroke-opacity:.55}` +
|
|
447
|
-
`#am .g-node.back{opacity:.4}` +
|
|
448
|
-
`#am .g-node.hl .g-label{font-weight:600}` +
|
|
449
|
-
`#am .g-node.hl .g-dot-halo{fill-opacity:.3}` +
|
|
450
|
-
`#am g[data-node]{cursor:pointer;transition:filter .12s ease,opacity .15s ease}` +
|
|
451
|
-
`#am g[data-node]:hover,#am g[data-node]:focus-visible{filter:brightness(1.3)}` +
|
|
452
|
-
`#am g[data-node].on{filter:brightness(1.3)}` +
|
|
453
|
-
`#am.hl g[data-node]{opacity:.25}#am.hl g[data-node].on{opacity:1}` +
|
|
454
|
-
`#am.hl path[data-from]{opacity:.1}#am.hl path[data-from].on{opacity:1}` +
|
|
455
|
-
`}</style>` +
|
|
456
|
-
`<defs><radialGradient id="g-grad" cx="35%" cy="28%" r="78%">` +
|
|
457
|
-
`<stop offset="0%" stop-color="#1E3A8A"/><stop offset="55%" stop-color="#0C1E45"/><stop offset="100%" stop-color="#050B1C"/>` +
|
|
458
|
-
`</radialGradient>` +
|
|
459
|
-
`<radialGradient id="g-atmo" cx="50%" cy="50%" r="50%">` +
|
|
460
|
-
`<stop offset="76%" stop-color="#60A5FA" stop-opacity="0"/>` +
|
|
461
|
-
`<stop offset="88%" stop-color="#60A5FA" stop-opacity="0.16"/>` +
|
|
462
|
-
`<stop offset="100%" stop-color="#60A5FA" stop-opacity="0"/>` +
|
|
463
|
-
`</radialGradient></defs>`);
|
|
464
|
-
}
|
|
431
|
+
/** 球的样子 —— 与 globe.ts 那份**共用**(globe-style.ts)✓ 两张画才对得上 ✓ */
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface GlobeCity {
|
|
2
|
+
id: string;
|
|
3
|
+
label: string;
|
|
4
|
+
/** 点的位置 */
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
/** 点的半径 */
|
|
8
|
+
r: number;
|
|
9
|
+
/** 点名的(要显眼) */
|
|
10
|
+
hl?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface PlacedLabel {
|
|
13
|
+
/** 文字的锚点 */
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
anchor: 'start' | 'middle' | 'end';
|
|
17
|
+
/** 字的盒子(锚点算出来的) */
|
|
18
|
+
box: [number, number, number, number];
|
|
19
|
+
/** 垫在字底下那块的大小 */
|
|
20
|
+
pad: [number, number, number, number];
|
|
21
|
+
}
|
|
22
|
+
type Box = [number, number, number, number];
|
|
23
|
+
/**
|
|
24
|
+
* 城名的位置。
|
|
25
|
+
* @param cities 球面上的点(顺序不重要,重要的先摆 —— 点名的、点大的先占好位置)
|
|
26
|
+
* @param fs 字号
|
|
27
|
+
* @param bounds 画布的范围 —— 名字不许跑出画布(球边上那些点,名字得往里放 ✓)
|
|
28
|
+
* @param limit 最多摆几个(球缩得小的时候,名字太多糊成一团 ⇒ 只摆几个 ✓)
|
|
29
|
+
*/
|
|
30
|
+
export declare function placeCityLabels(cities: GlobeCity[], fs: number, bounds?: Box, limit?: number): Map<string, PlacedLabel | null>;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// 城名摆在哪儿 —— 球面上的排版,两个球共用这一份 ✓
|
|
2
|
+
//
|
|
3
|
+
// 为什么单独立一份:两个球(气泡球、正射地球)都会撞名
|
|
4
|
+
// (上海挨着东京、新加坡挨着孟买 ⇒ 两个名字叠成一团糊字 ✗),
|
|
5
|
+
// 避让的规矩得是**一处**,不然两个球摆得不一样。
|
|
6
|
+
//
|
|
7
|
+
// 规矩:每个名字在点四周挑一个**空位** ——
|
|
8
|
+
// 先试正上(惯例),再试斜上、两边、正下……
|
|
9
|
+
// 挑第一个**不压着别的名字、也不压着别的点**的位置;
|
|
10
|
+
// 哪儿都放不下 ⇒ 这个名字不画(少一个名字好过糊成一团 ✓ —— 读者还能看图猜位置)
|
|
11
|
+
import { textW } from './svg.js';
|
|
12
|
+
/** 两个盒子撞上了吗 */
|
|
13
|
+
const hits = (a, b) => !(a[2] <= b[0] || a[0] >= b[2] || a[3] <= b[1] || a[1] >= b[3]);
|
|
14
|
+
/**
|
|
15
|
+
* 城名的位置。
|
|
16
|
+
* @param cities 球面上的点(顺序不重要,重要的先摆 —— 点名的、点大的先占好位置)
|
|
17
|
+
* @param fs 字号
|
|
18
|
+
* @param bounds 画布的范围 —— 名字不许跑出画布(球边上那些点,名字得往里放 ✓)
|
|
19
|
+
* @param limit 最多摆几个(球缩得小的时候,名字太多糊成一团 ⇒ 只摆几个 ✓)
|
|
20
|
+
*/
|
|
21
|
+
export function placeCityLabels(cities, fs, bounds, limit = Infinity) {
|
|
22
|
+
const out = new Map();
|
|
23
|
+
// 重要的先摆:点名的、点大的先占位置,剩下的绕着走
|
|
24
|
+
const order = [...cities].sort((a, b) => (Number(b.hl ?? false) - Number(a.hl ?? false)) || (b.r - a.r));
|
|
25
|
+
const placed = [];
|
|
26
|
+
// 点的位置:名字不许压住点(自己的、别人的都不行)
|
|
27
|
+
const dots = cities.map((c) => [c.x - c.r - 2, c.y - c.r - 2, c.x + c.r + 2, c.y + c.r + 2]);
|
|
28
|
+
for (const c of order) {
|
|
29
|
+
if (placed.length >= limit) {
|
|
30
|
+
out.set(c.id, null);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const w = textW(c.label, fs);
|
|
34
|
+
// 候选:相对点的偏移 + 文字的锚点(先正上,再斜上,再两边,最后往下)
|
|
35
|
+
const cands = [
|
|
36
|
+
[0, -(c.r + fs * 0.95), 'middle'],
|
|
37
|
+
[c.r * 0.8 + 6, -(c.r * 0.8 + fs * 0.85), 'start'],
|
|
38
|
+
[-(c.r * 0.8 + 6), -(c.r * 0.8 + fs * 0.85), 'end'],
|
|
39
|
+
[c.r + 9, -fs * 0.35, 'start'],
|
|
40
|
+
[-(c.r + 9), -fs * 0.35, 'end'],
|
|
41
|
+
[c.r * 0.8 + 6, c.r * 0.8 + fs * 0.75, 'start'],
|
|
42
|
+
[-(c.r * 0.8 + 6), c.r * 0.8 + fs * 0.75, 'end'],
|
|
43
|
+
[0, c.r + fs * 0.95, 'middle'],
|
|
44
|
+
];
|
|
45
|
+
let done = null;
|
|
46
|
+
for (const [dx, dy, anchor] of cands) {
|
|
47
|
+
const x = c.x + dx;
|
|
48
|
+
const y = c.y + dy;
|
|
49
|
+
const box = anchor === 'middle'
|
|
50
|
+
? [x - w / 2, y - fs * 0.8, x + w / 2, y + fs * 0.25]
|
|
51
|
+
: anchor === 'end'
|
|
52
|
+
? [x - w, y - fs * 0.8, x, y + fs * 0.25]
|
|
53
|
+
: [x, y - fs * 0.8, x + w, y + fs * 0.25];
|
|
54
|
+
if (placed.some((b) => hits(b, box)))
|
|
55
|
+
continue;
|
|
56
|
+
if (dots.some((d) => hits(d, box)))
|
|
57
|
+
continue;
|
|
58
|
+
if (bounds && (box[0] < bounds[0] || box[1] < bounds[1] || box[2] > bounds[2] || box[3] > bounds[3]))
|
|
59
|
+
continue;
|
|
60
|
+
done = {
|
|
61
|
+
x, y, anchor, box,
|
|
62
|
+
// 垫色:字底下那一块(描边被撤掉了 —— 垫色才看得清)
|
|
63
|
+
pad: [box[0] - 4, box[1] - 1, box[2] + 4, box[3] + 1],
|
|
64
|
+
};
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
if (done)
|
|
68
|
+
placed.push(done.box);
|
|
69
|
+
out.set(c.id, done);
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** 弧线的颜色 */
|
|
2
|
+
export declare const GLOBE_EDGE = "#DBEAFE";
|
|
3
|
+
/** 地名的字色 */
|
|
4
|
+
export declare const GLOBE_LABEL = "#F0F9FF";
|
|
5
|
+
/** 垫在字与点底下的一层深色(球面上不垫就看不清) */
|
|
6
|
+
export declare const GLOBE_INK_BG = "#050B1C";
|
|
7
|
+
/** 暗色那套配色(两个球一样) */
|
|
8
|
+
export declare const GLOBE_VARS: string;
|
|
9
|
+
/** 球体与大气那两个渐变(两个球一样) */
|
|
10
|
+
export declare const GLOBE_DEFS: string;
|
|
11
|
+
/** 交互那几行(只有浏览器里用得上 ⇒ 留在 CSS 里) */
|
|
12
|
+
export declare const GLOBE_CSS_INTERACT: string;
|
|
13
|
+
/** 球的样式块:配色 + 交互(样子本身写在元素属性上) */
|
|
14
|
+
export declare function globeStyle(): string;
|
|
15
|
+
/** 球面那一层(半透明,看得见里面的陆地) */
|
|
16
|
+
export declare const sphereAttrs = " fill-opacity=\"0.92\"";
|
|
17
|
+
/** 陆地:正面深、背面淡 */
|
|
18
|
+
export declare const landAttrs: (fill: string, stroke: string, fillOpacity: number, strokeOpacity: number, back?: boolean) => string;
|
|
19
|
+
/** 弧:正面亮、背面淡;边多的时候画细一点 */
|
|
20
|
+
export declare const edgeAttrs: (back?: boolean, many?: boolean) => string;
|
|
21
|
+
/** 弧上的字 */
|
|
22
|
+
export declare const edgeLabelAttrs = " fill=\"#DBEAFE\" fill-opacity=\"0.9\"";
|
|
23
|
+
/** 点周围那圈淡光 */
|
|
24
|
+
export declare const haloAttrs: (hl?: boolean) => string;
|
|
25
|
+
/** 点本身(底下描一圈深色,免得跟陆地糊在一起) */
|
|
26
|
+
export declare const dotAttrs = " stroke=\"#050B1C\" stroke-width=\"1.5\"";
|
|
27
|
+
/**
|
|
28
|
+
* 地名:不描边(house style)—— 底下**垫**一块深色,
|
|
29
|
+
* 球面上才看得清(描边的字在深底上会糊成一团 ✗)。
|
|
30
|
+
*/
|
|
31
|
+
export declare const labelAttrs: (hl?: boolean) => string;
|
|
32
|
+
/** 垫在地名下面那块(圆角矩形) */
|
|
33
|
+
export declare const labelPadAttrs = " fill=\"#050B1C\" fill-opacity=\"0.55\"";
|
|
34
|
+
/** 背面那些点/字:整组淡下去 */
|
|
35
|
+
export declare const backAttrs = " opacity=\"0.4\"";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// 两个球(气泡球 globe.ts、正射地球 globe-earth.ts)**共用**的这一份:
|
|
2
|
+
// 配色、渐变、交互那几行 CSS。
|
|
3
|
+
//
|
|
4
|
+
// 为什么放在一处:
|
|
5
|
+
// ① 两个球本来就是同一个「球」的概念 —— 长成一样才对。
|
|
6
|
+
// 以前各写一份样式,改一处就忘了另一处(两份已经不一样了)✗
|
|
7
|
+
// ② **样子写在属性上**(不只写在 CSS 里):SVG 存成文件、或者用 resvg 转成位图时,
|
|
8
|
+
// `#am .g-xxx` 这类选择器未必生效 ⇒ 只靠 CSS 的话弧线没了、陆地没了、地名白了 ✗
|
|
9
|
+
// (2026-11 修过一次:球的 PNG 转出来只剩几个孤零零的点)
|
|
10
|
+
//
|
|
11
|
+
// CSS 里只留**交互**那几行(悬停变亮、点选高亮)—— 那些只有浏览器里用得上。
|
|
12
|
+
/** 弧线的颜色 */
|
|
13
|
+
export const GLOBE_EDGE = '#DBEAFE';
|
|
14
|
+
/** 地名的字色 */
|
|
15
|
+
export const GLOBE_LABEL = '#F0F9FF';
|
|
16
|
+
/** 垫在字与点底下的一层深色(球面上不垫就看不清) */
|
|
17
|
+
export const GLOBE_INK_BG = '#050B1C';
|
|
18
|
+
/** 暗色那套配色(两个球一样) */
|
|
19
|
+
export const GLOBE_VARS = `--d-s1:#34D399;--d-s2:#F87171;--d-s3:#A78BFA;--d-s4:#22D3EE;--d-s5:#60A5FA;--d-s6:#FB923C;` +
|
|
20
|
+
`--d-s7:#4ADE80;--d-s8:#FB7185;--d-s9:#F59E0B;--d-s10:#0EA5E9;` +
|
|
21
|
+
`--d-ink:#FAFAFA;--d-text:#D4D4D8;--d-muted:#A1A1AA;--d-faint:#71717A;--d-grid:#29292E;--d-card:#0E0F11;--d-link:#60A5FA;`;
|
|
22
|
+
/** 球体与大气那两个渐变(两个球一样) */
|
|
23
|
+
export const GLOBE_DEFS = `<defs><radialGradient id="g-grad" cx="35%" cy="28%" r="78%">` +
|
|
24
|
+
`<stop offset="0%" stop-color="#1E3A8A"/><stop offset="55%" stop-color="#0C1E45"/><stop offset="100%" stop-color="#050B1C"/>` +
|
|
25
|
+
`</radialGradient>` +
|
|
26
|
+
`<radialGradient id="g-atmo" cx="50%" cy="50%" r="50%">` +
|
|
27
|
+
`<stop offset="76%" stop-color="#60A5FA" stop-opacity="0"/>` +
|
|
28
|
+
`<stop offset="88%" stop-color="#60A5FA" stop-opacity="0.16"/>` +
|
|
29
|
+
`<stop offset="100%" stop-color="#60A5FA" stop-opacity="0"/>` +
|
|
30
|
+
`</radialGradient></defs>`;
|
|
31
|
+
/** 交互那几行(只有浏览器里用得上 ⇒ 留在 CSS 里) */
|
|
32
|
+
export const GLOBE_CSS_INTERACT = `#am g[data-node]{cursor:pointer;transition:filter .12s ease,opacity .15s ease}` +
|
|
33
|
+
`#am g[data-node]:hover,#am g[data-node]:focus-visible{filter:brightness(1.3)}` +
|
|
34
|
+
`#am g[data-node].on{filter:brightness(1.3)}` +
|
|
35
|
+
`#am.hl g[data-node]{opacity:.25}#am.hl g[data-node].on{opacity:1}` +
|
|
36
|
+
`#am.hl path[data-from]{opacity:.1}#am.hl path[data-from].on{opacity:1}`;
|
|
37
|
+
/** 球的样式块:配色 + 交互(样子本身写在元素属性上) */
|
|
38
|
+
export function globeStyle() {
|
|
39
|
+
return (`<style>:root{color-scheme:dark;${GLOBE_VARS}` +
|
|
40
|
+
// 命中区:看不见的粗线 —— 弧太细悬停不着 ⇒ 给它一条 12px 的透明线接着 ✓
|
|
41
|
+
`#am .g-edge path.g-edge-hit{pointer-events:stroke}` +
|
|
42
|
+
GLOBE_CSS_INTERACT +
|
|
43
|
+
`}</style>` +
|
|
44
|
+
GLOBE_DEFS);
|
|
45
|
+
}
|
|
46
|
+
/* ---------------------------------------------------------------- 那些属性
|
|
47
|
+
|
|
48
|
+
样子写成一份**函数**:两个球调同一处 ⇒ 改一处两边都跟着变 ✓ */
|
|
49
|
+
/** 球面那一层(半透明,看得见里面的陆地) */
|
|
50
|
+
export const sphereAttrs = ' fill-opacity="0.92"';
|
|
51
|
+
/** 陆地:正面深、背面淡 */
|
|
52
|
+
export const landAttrs = (fill, stroke, fillOpacity, strokeOpacity, back = false) => ` fill="${fill}" fill-opacity="${back ? 0.16 : fillOpacity}" stroke="${stroke}" stroke-opacity="${back ? 0.12 : strokeOpacity}" stroke-width="0.9"`;
|
|
53
|
+
/** 弧:正面亮、背面淡;边多的时候画细一点 */
|
|
54
|
+
export const edgeAttrs = (back = false, many = false) => ` stroke="${GLOBE_EDGE}" stroke-opacity="${back ? 0.2 : many ? 0.8 : 0.92}" stroke-width="${many ? 2.4 : 3}"`;
|
|
55
|
+
/** 弧上的字 */
|
|
56
|
+
export const edgeLabelAttrs = ` fill="${GLOBE_EDGE}" fill-opacity="0.9"`;
|
|
57
|
+
/** 点周围那圈淡光 */
|
|
58
|
+
export const haloAttrs = (hl = false) => ` fill-opacity="${hl ? 0.3 : 0.2}"`;
|
|
59
|
+
/** 点本身(底下描一圈深色,免得跟陆地糊在一起) */
|
|
60
|
+
export const dotAttrs = ` stroke="${GLOBE_INK_BG}" stroke-width="1.5"`;
|
|
61
|
+
/**
|
|
62
|
+
* 地名:不描边(house style)—— 底下**垫**一块深色,
|
|
63
|
+
* 球面上才看得清(描边的字在深底上会糊成一团 ✗)。
|
|
64
|
+
*/
|
|
65
|
+
export const labelAttrs = (hl = false) => ` fill="${GLOBE_LABEL}" fill-opacity="0.95"${hl ? ' font-weight="600"' : ''}`;
|
|
66
|
+
/** 垫在地名下面那块(圆角矩形) */
|
|
67
|
+
export const labelPadAttrs = ` fill="${GLOBE_INK_BG}" fill-opacity="0.55"`;
|
|
68
|
+
/** 背面那些点/字:整组淡下去 */
|
|
69
|
+
export const backAttrs = ' opacity="0.4"';
|
package/dist/render/globe.js
CHANGED
|
@@ -19,6 +19,11 @@ import { loadWorldSphericalGeoJson } from './maps.js';
|
|
|
19
19
|
import { DEFAULT_CAMERA } from './scene3d.js';
|
|
20
20
|
import { GLOBE_R, projectGlobe, sph, fibonacci, arc, layoutOnSphere, } from './globe3d.js';
|
|
21
21
|
import { WORLD_COUNTRIES } from '../world-data.js';
|
|
22
|
+
import { globeStyle, sphereAttrs, landAttrs, edgeAttrs, edgeLabelAttrs, haloAttrs, dotAttrs, labelAttrs, labelPadAttrs, backAttrs, } from './globe-style.js';
|
|
23
|
+
import { placeCityLabels } from './globe-labels.js';
|
|
24
|
+
/** 这个球上陆地的颜色(正射地球那份是灰的 —— 两个球本来就长得不一样) */
|
|
25
|
+
const LAND = '#2E6FA8';
|
|
26
|
+
const LAND_STROKE = '#9BDCFF';
|
|
22
27
|
const METRICS = {
|
|
23
28
|
gdp: {
|
|
24
29
|
key: 'gdp',
|
|
@@ -198,11 +203,11 @@ export function renderGlobe(scene, opts = {}) {
|
|
|
198
203
|
// 大气:球外面一圈很淡的蓝光(ECharts 的 globe-atmosphere 那个感觉)
|
|
199
204
|
parts.push(`<circle class="g-atmo" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad * 1.14)}" fill="url(#g-atmo)"/>`);
|
|
200
205
|
parts.push(`<circle class="g-glow" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad)}" fill="none" stroke="var(--d-link)" stroke-opacity="0.25" stroke-width="10"/>`);
|
|
201
|
-
parts.push(`<circle class="g-sphere" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad)}" fill="url(#g-grad)"/>`);
|
|
206
|
+
parts.push(`<circle class="g-sphere" cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(rad)}" fill="url(#g-grad)"${sphereAttrs}/>`);
|
|
202
207
|
// 大陆:地球才有 —— 一块淡的陆地,背面那半边更淡
|
|
203
208
|
for (const l of p.land) {
|
|
204
209
|
for (const ring of l.rings) {
|
|
205
|
-
parts.push(`<path class="g-land${l.back ? ' back' : ''}" d="${ring.map((q, i) => `${i === 0 ? 'M' : 'L'}${r2(q[0])} ${r2(q[1])}`).join('')}Z"/>`);
|
|
210
|
+
parts.push(`<path class="g-land${l.back ? ' back' : ''}" d="${ring.map((q, i) => `${i === 0 ? 'M' : 'L'}${r2(q[0])} ${r2(q[1])}`).join('')}Z"${landAttrs(LAND, LAND_STROKE, 0.5, 0.45, l.back)}/>`);
|
|
206
211
|
}
|
|
207
212
|
}
|
|
208
213
|
// 经纬网:球不要那些线(点已经贴着球面,线只是噪点)
|
|
@@ -212,30 +217,47 @@ export function renderGlobe(scene, opts = {}) {
|
|
|
212
217
|
const d = e.pts.map((q, i) => `${i === 0 ? 'M' : 'L'}${r2(q[0])} ${r2(q[1])}`).join('');
|
|
213
218
|
parts.push(`<g class="g-edge${e.back ? ' back' : ''}"${ia}>` +
|
|
214
219
|
// data-from/data-to 也要落在 path 上 —— explore 那套点击高亮找的是 path[data-from]
|
|
215
|
-
`<path d="${d}"${ia} fill="none"/>` +
|
|
216
|
-
(e.label ? `<text class="g-edge-label" x="${r2(e.lx)}" y="${r2(e.ly)}" font-size="20" text-anchor="middle">${esc(e.label)}</text>` : '') +
|
|
220
|
+
`<path d="${d}"${ia} fill="none"${edgeAttrs(e.back)}/>` +
|
|
221
|
+
(e.label ? `<text class="g-edge-label" x="${r2(e.lx)}" y="${r2(e.ly)}" font-size="20" text-anchor="middle"${edgeLabelAttrs}>${esc(e.label)}</text>` : '') +
|
|
217
222
|
`</g>`);
|
|
218
223
|
}
|
|
219
224
|
// 点:背面小而淡,正面大而亮
|
|
220
225
|
const order = [...p.nodes].sort((a, b) => b.depth - a.depth);
|
|
221
226
|
const nodeOf = new Map(scene.nodes.map((n) => [n.id, n]));
|
|
227
|
+
// 城名摆在哪儿:两个球共用一份避让表 ✓(上海挨着东京 ⇒ 别叠成一团)
|
|
228
|
+
const LABEL_FS = 28;
|
|
229
|
+
const labels = placeCityLabels(order.filter((n) => !nodeOf.get(n.id).bubble).map((n) => ({
|
|
230
|
+
id: n.id,
|
|
231
|
+
label: nodeOf.get(n.id).label,
|
|
232
|
+
x: n.x,
|
|
233
|
+
y: n.y,
|
|
234
|
+
r: nodeOf.get(n.id).hl ? 15 : 10,
|
|
235
|
+
hl: nodeOf.get(n.id).hl,
|
|
236
|
+
})), LABEL_FS,
|
|
237
|
+
// 名字不许跑出画布
|
|
238
|
+
[vb.x, vb.y, vb.x + vb.w, vb.y + vb.h]);
|
|
222
239
|
for (const n of order) {
|
|
223
240
|
const s = nodeOf.get(n.id);
|
|
224
241
|
const ia = opts.interactive ? ` data-node="${esc(n.id)}" data-tip="${esc(tipOf(s))}" tabindex="0"` : '';
|
|
225
242
|
// 数据气泡:实心圆 + 一圈淡边(背面淡一半)
|
|
226
243
|
if (s.bubble) {
|
|
227
|
-
parts.push(`<g class="g-bubble${n.back ? ' back' : ''}"${ia}>` +
|
|
244
|
+
parts.push(`<g class="g-bubble${n.back ? ' back' : ''}"${ia}${n.back ? backAttrs : ''}>` +
|
|
228
245
|
`<circle cx="${r2(n.x)}" cy="${r2(n.y)}" r="${r2(s.bubble.r)}" fill="${s.color}" fill-opacity="0.5" stroke="${s.color}" stroke-width="1.4"/>` +
|
|
229
246
|
`</g>`);
|
|
230
247
|
continue;
|
|
231
248
|
}
|
|
232
249
|
// 高亮的城市:大一点 + 一圈圈(圈是「点名」的意思)
|
|
233
250
|
const rad = s.hl ? 15 : 10;
|
|
234
|
-
|
|
251
|
+
const lab = labels.get(n.id);
|
|
252
|
+
parts.push(`<g class="g-node${n.back ? ' back' : ''}${s.hl ? ' hl' : ''}"${ia}${n.back ? backAttrs : ''}>` +
|
|
235
253
|
(s.hl ? `<circle class="g-ring" cx="${r2(n.x)}" cy="${r2(n.y)}" r="22" fill="none" stroke="${s.color}" stroke-width="2.5"/>` : '') +
|
|
236
|
-
`<circle class="g-dot-halo" cx="${r2(n.x)}" cy="${r2(n.y)}" r="24" fill="${s.color}"/>` +
|
|
237
|
-
`<circle class="g-dot" cx="${r2(n.x)}" cy="${r2(n.y)}" r="${rad}" fill="${s.color}"/>` +
|
|
238
|
-
|
|
254
|
+
`<circle class="g-dot-halo" cx="${r2(n.x)}" cy="${r2(n.y)}" r="24" fill="${s.color}"${haloAttrs(s.hl)}/>` +
|
|
255
|
+
`<circle class="g-dot" cx="${r2(n.x)}" cy="${r2(n.y)}" r="${rad}" fill="${s.color}"${dotAttrs}/>` +
|
|
256
|
+
// 名字:底下垫一块深色(house style:字不描边 ⇒ 垫色)
|
|
257
|
+
(lab
|
|
258
|
+
? `<rect x="${r2(lab.pad[0])}" y="${r2(lab.pad[1])}" width="${r2(lab.pad[2] - lab.pad[0])}" height="${r2(lab.pad[3] - lab.pad[1])}" rx="4"${labelPadAttrs}/>` +
|
|
259
|
+
`<text class="g-label" x="${r2(lab.x)}" y="${r2(lab.y)}" font-size="${LABEL_FS}" text-anchor="${lab.anchor}"${labelAttrs(s.hl)}>${esc(s.label)}</text>`
|
|
260
|
+
: '') +
|
|
239
261
|
`</g>`);
|
|
240
262
|
}
|
|
241
263
|
// 色标:这条 data= 那一档的说明(左下角,一小条渐变 + 两端的值)
|
|
@@ -270,37 +292,3 @@ function tipOf(n) {
|
|
|
270
292
|
const at = `${Math.abs(n.lat).toFixed(1)}°${n.lat >= 0 ? 'N' : 'S'} ${Math.abs(n.lon ?? 0).toFixed(1)}°${(n.lon ?? 0) >= 0 ? 'E' : 'W'}`;
|
|
271
293
|
return n.sub ? `${n.label} · ${n.sub} · ${at}` : `${n.label} · ${at}`;
|
|
272
294
|
}
|
|
273
|
-
function globeStyle() {
|
|
274
|
-
return (`<style>:root{color-scheme:dark;` +
|
|
275
|
-
`--d-s1:#34D399;--d-s2:#F87171;--d-s3:#A78BFA;--d-s4:#22D3EE;--d-s5:#60A5FA;--d-s6:#FB923C;` +
|
|
276
|
-
`--d-s7:#4ADE80;--d-s8:#FB7185;--d-s9:#F59E0B;--d-s10:#0EA5E9;` +
|
|
277
|
-
`--d-ink:#FAFAFA;--d-text:#D4D4D8;--d-muted:#A1A1AA;--d-faint:#71717A;--d-grid:#29292E;--d-card:#0E0F11;--d-link:#60A5FA;` +
|
|
278
|
-
`#am .g-sphere{fill-opacity:.92}` +
|
|
279
|
-
`#am .g-land{fill:#2E6FA8;fill-opacity:.5;stroke:#9BDCFF;stroke-opacity:.45;stroke-width:.9}` +
|
|
280
|
-
`#am .g-land.back{fill-opacity:.16;stroke-opacity:.12}` +
|
|
281
|
-
`#am .g-grid{stroke:#7DD3FC;stroke-opacity:.18;stroke-width:2}` +
|
|
282
|
-
`#am .g-edge path{stroke:#DBEAFE;stroke-opacity:.92;stroke-width:3}` +
|
|
283
|
-
`#am .g-edge.back path{stroke-opacity:.2}` +
|
|
284
|
-
`#am .g-edge-label{fill:#DBEAFE;fill-opacity:.9}` +
|
|
285
|
-
`#am .g-node .g-dot-halo{fill-opacity:.2}` +
|
|
286
|
-
`#am .g-node .g-dot{stroke:#050B1C;stroke-width:1.5}` +
|
|
287
|
-
`#am .g-node .g-label{fill:#F0F9FF;fill-opacity:.95;paint-order:stroke;stroke:#050B1C;stroke-width:3;stroke-opacity:.55}` +
|
|
288
|
-
`#am .g-node.back{opacity:.4}` +
|
|
289
|
-
`#am .g-node.hl .g-label{font-weight:600}` +
|
|
290
|
-
`#am .g-node.hl .g-dot-halo{fill-opacity:.3}` +
|
|
291
|
-
`#am g[data-node]{cursor:pointer;transition:filter .12s ease,opacity .15s ease}` +
|
|
292
|
-
`#am g[data-node]:hover,#am g[data-node]:focus-visible{filter:brightness(1.3)}` +
|
|
293
|
-
`#am g[data-node].on{filter:brightness(1.3)}` +
|
|
294
|
-
`#am.hl g[data-node]{opacity:.25}#am.hl g[data-node].on{opacity:1}` +
|
|
295
|
-
`#am.hl path[data-from]{opacity:.1}#am.hl path[data-from].on{opacity:1}` +
|
|
296
|
-
`}</style>` +
|
|
297
|
-
`<defs><radialGradient id="g-grad" cx="35%" cy="28%" r="78%">` +
|
|
298
|
-
`<stop offset="0%" stop-color="#1E3A8A"/><stop offset="55%" stop-color="#0C1E45"/><stop offset="100%" stop-color="#050B1C"/>` +
|
|
299
|
-
`</radialGradient>` +
|
|
300
|
-
// 大气:球外面那圈淡蓝
|
|
301
|
-
`<radialGradient id="g-atmo" cx="50%" cy="50%" r="50%">` +
|
|
302
|
-
`<stop offset="76%" stop-color="#60A5FA" stop-opacity="0"/>` +
|
|
303
|
-
`<stop offset="88%" stop-color="#60A5FA" stop-opacity="0.16"/>` +
|
|
304
|
-
`<stop offset="100%" stop-color="#60A5FA" stop-opacity="0"/>` +
|
|
305
|
-
`</radialGradient></defs>`);
|
|
306
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.14",
|
|
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",
|