@johnmorrisdotca/chikyu-earth 0.35.0 → 0.36.0
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/CHANGELOG.md +13 -0
- package/NOTICE.md +23 -1
- package/README.md +20 -2
- package/dist/borders/layer.d.ts +75 -0
- package/dist/borders/layer.js +136 -0
- package/dist/borders/lines.d.ts +138 -0
- package/dist/borders/lines.js +227 -0
- package/dist/cities/host.d.ts +9 -0
- package/dist/cities/host.js +14 -2
- package/dist/country-names/geometry.d.ts +264 -0
- package/dist/country-names/geometry.js +372 -0
- package/dist/country-names/host.d.ts +82 -0
- package/dist/country-names/host.js +212 -0
- package/dist/grid/layer.d.ts +45 -0
- package/dist/grid/layer.js +66 -0
- package/dist/grid/lines.d.ts +85 -0
- package/dist/grid/lines.js +113 -0
- package/dist/markers/go-to-place.d.ts +27 -0
- package/dist/markers/go-to-place.js +35 -0
- package/dist/markers/index.d.ts +2 -1
- package/dist/markers/index.js +2 -1
- package/dist/markers/layer.d.ts +7 -0
- package/dist/markers/layer.js +7 -0
- package/dist/markers/place-layer.d.ts +58 -7
- package/dist/markers/place-layer.js +326 -64
- package/dist/markers/place-sprites.d.ts +33 -0
- package/dist/markers/place-sprites.js +161 -0
- package/dist/markers/places.d.ts +91 -6
- package/dist/markers/places.js +135 -6
- package/dist/realistic/earth.d.ts +11 -0
- package/dist/realistic/earth.js +54 -0
- package/dist/realistic/element.d.ts +65 -4
- package/dist/realistic/element.js +198 -26
- package/dist/realistic/immersive.d.ts +7 -0
- package/dist/realistic/immersive.js +17 -0
- package/dist/realistic/index.d.ts +6 -0
- package/dist/realistic/index.js +3 -0
- package/dist/realistic/textures.d.ts +3 -0
- package/dist/space/element.d.ts +81 -7
- package/dist/space/element.js +246 -16
- package/dist/space/index.d.ts +1 -1
- package/dist/space/scene.d.ts +53 -5
- package/dist/space/scene.js +88 -8
- package/package.json +3 -3
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// Borders on the land as lines on the sphere: the edges two shapes share (a country's border with its neighbour, a province's with the next), never a coast, which the pictures already show.
|
|
2
|
+
// Pure: longitude and latitude in, points on the unit sphere out (the axes of the Earth's own sphere: +z is longitude 0 on the equator, +x east, +y north).
|
|
3
|
+
const RAD = Math.PI / 180;
|
|
4
|
+
/**
|
|
5
|
+
* The Earth's radius on the screen, in CSS pixels, from which the regions (provinces, states, prefectures) are drawn: about 7 km to a pixel, where a region of a few hundred kilometres is tens of pixels across.
|
|
6
|
+
* The countries are drawn at any size.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { BORDER_REGIONS_RADIUS } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
11
|
+
*
|
|
12
|
+
* console.log(BORDER_REGIONS_RADIUS);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export const BORDER_REGIONS_RADIUS = 900;
|
|
16
|
+
/**
|
|
17
|
+
* The attributes the borders read: `borders`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { BORDER_ATTRIBUTES } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
22
|
+
*
|
|
23
|
+
* console.log(BORDER_ATTRIBUTES.join(" "));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const BORDER_ATTRIBUTES = ["borders"];
|
|
27
|
+
/**
|
|
28
|
+
* A `borders` attribute: absent draws none; a bare attribute draws both sets; otherwise the words in it, `countries` and `regions`, each switch one set on.
|
|
29
|
+
*
|
|
30
|
+
* @param value - The attribute's text, or `null` when it is not there.
|
|
31
|
+
* @returns Which sets are drawn.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { readBorders } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
36
|
+
*
|
|
37
|
+
* console.log(readBorders(null), readBorders(""), readBorders("countries"));
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function readBorders(value) {
|
|
41
|
+
if (value === null)
|
|
42
|
+
return { countries: false, regions: false };
|
|
43
|
+
const words = value.toLowerCase().split(/[\s,]+/).filter(Boolean);
|
|
44
|
+
if (words.length === 0)
|
|
45
|
+
return { countries: true, regions: true };
|
|
46
|
+
return { countries: words.includes("countries"), regions: words.includes("regions") };
|
|
47
|
+
}
|
|
48
|
+
const ringsOf = (geometry) => {
|
|
49
|
+
if (!geometry || !Array.isArray(geometry.coordinates))
|
|
50
|
+
return [];
|
|
51
|
+
if (geometry.type === "Polygon")
|
|
52
|
+
return geometry.coordinates;
|
|
53
|
+
if (geometry.type === "MultiPolygon")
|
|
54
|
+
return geometry.coordinates.flat();
|
|
55
|
+
return [];
|
|
56
|
+
};
|
|
57
|
+
const pointKey = (point) => `${Math.round(point[0] * 1e5)},${Math.round(point[1] * 1e5)}`;
|
|
58
|
+
/**
|
|
59
|
+
* The borders of a set of shapes as line segments on the unit sphere: each edge that two different shapes share, drawn once; an edge only one shape has (a coast) is left out.
|
|
60
|
+
* An edge longer than `maxStep` degrees is cut into pieces along the great circle, so the line keeps to the ground.
|
|
61
|
+
*
|
|
62
|
+
* @param shapes - The shapes, in longitude and latitude.
|
|
63
|
+
* @param maxStep - The longest piece in degrees (1 when left out).
|
|
64
|
+
* @returns The segments' ends, x, y, z for each, two points to a segment: ready for a `LineSegments` geometry.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { sharedEdges } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
69
|
+
*
|
|
70
|
+
* const west = { geometry: { type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]] } };
|
|
71
|
+
* const east = { geometry: { type: "Polygon", coordinates: [[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]] } };
|
|
72
|
+
* console.log(sharedEdges({ features: [west, east] }).length / 6);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function sharedEdges(shapes, maxStep = 1) {
|
|
76
|
+
const owners = new Map();
|
|
77
|
+
shapes.features.forEach((feature, owner) => {
|
|
78
|
+
for (const ring of ringsOf(feature.geometry)) {
|
|
79
|
+
for (let i = 0; i + 1 < ring.length; i += 1) {
|
|
80
|
+
const a = ring[i];
|
|
81
|
+
const b = ring[i + 1];
|
|
82
|
+
const ka = pointKey(a);
|
|
83
|
+
const kb = pointKey(b);
|
|
84
|
+
if (ka === kb)
|
|
85
|
+
continue;
|
|
86
|
+
const key = ka < kb ? `${ka}|${kb}` : `${kb}|${ka}`;
|
|
87
|
+
const seen = owners.get(key);
|
|
88
|
+
if (!seen)
|
|
89
|
+
owners.set(key, { a, b, owner, shared: false });
|
|
90
|
+
else if (seen.owner !== owner)
|
|
91
|
+
seen.shared = true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
const out = [];
|
|
96
|
+
const push = (lon, lat) => {
|
|
97
|
+
const phi = lat * RAD;
|
|
98
|
+
const lambda = lon * RAD;
|
|
99
|
+
out.push(Math.cos(phi) * Math.sin(lambda), Math.sin(phi), Math.cos(phi) * Math.cos(lambda));
|
|
100
|
+
};
|
|
101
|
+
for (const edge of owners.values()) {
|
|
102
|
+
if (!edge.shared)
|
|
103
|
+
continue;
|
|
104
|
+
const [lon0, lat0] = edge.a;
|
|
105
|
+
const [lon1, lat1] = edge.b;
|
|
106
|
+
let dLon = lon1 - lon0;
|
|
107
|
+
if (dLon > 180)
|
|
108
|
+
dLon -= 360;
|
|
109
|
+
else if (dLon < -180)
|
|
110
|
+
dLon += 360;
|
|
111
|
+
const pieces = Math.max(1, Math.ceil(Math.max(Math.abs(dLon) * Math.cos(((lat0 + lat1) / 2) * RAD), Math.abs(lat1 - lat0)) / maxStep));
|
|
112
|
+
if (pieces === 1) {
|
|
113
|
+
push(lon0, lat0);
|
|
114
|
+
push(lon1, lat1);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
// along the great circle: the ends as unit vectors, mixed and put back on the sphere
|
|
118
|
+
const p = [Math.cos(lat0 * RAD) * Math.sin(lon0 * RAD), Math.sin(lat0 * RAD), Math.cos(lat0 * RAD) * Math.cos(lon0 * RAD)];
|
|
119
|
+
const q = [Math.cos(lat1 * RAD) * Math.sin(lon1 * RAD), Math.sin(lat1 * RAD), Math.cos(lat1 * RAD) * Math.cos(lon1 * RAD)];
|
|
120
|
+
let last = p;
|
|
121
|
+
for (let k = 1; k <= pieces; k += 1) {
|
|
122
|
+
const t = k / pieces;
|
|
123
|
+
const m = [p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t, p[2] + (q[2] - p[2]) * t];
|
|
124
|
+
const length = Math.hypot(m[0], m[1], m[2]) || 1;
|
|
125
|
+
const next = [m[0] / length, m[1] / length, m[2] / length];
|
|
126
|
+
out.push(last[0], last[1], last[2], next[0], next[1], next[2]);
|
|
127
|
+
last = next;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return new Float32Array(out);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Each coded shape's box in longitude and latitude: `[west, south, east, north]`, with `west` greater than `east` for a shape across the 180th meridian (the shorter way round).
|
|
134
|
+
*
|
|
135
|
+
* @param shapes - The shapes; one with no code is left out.
|
|
136
|
+
* @returns The box of each code.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* import { shapeBoxes } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
141
|
+
*
|
|
142
|
+
* const fiji = { properties: { code: "FJ" }, geometry: { type: "Polygon", coordinates: [[[177, -18], [-179, -18], [-179, -16], [177, -16], [177, -18]]] } };
|
|
143
|
+
* console.log(shapeBoxes({ features: [fiji] }).get("FJ"));
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export function shapeBoxes(shapes) {
|
|
147
|
+
const boxes = new Map();
|
|
148
|
+
for (const feature of shapes.features) {
|
|
149
|
+
const code = feature.properties?.code;
|
|
150
|
+
if (!code)
|
|
151
|
+
continue;
|
|
152
|
+
let south = 90;
|
|
153
|
+
let north = -90;
|
|
154
|
+
const lons = [];
|
|
155
|
+
for (const ring of ringsOf(feature.geometry)) {
|
|
156
|
+
for (const point of ring) {
|
|
157
|
+
lons.push(point[0]);
|
|
158
|
+
south = Math.min(south, point[1]);
|
|
159
|
+
north = Math.max(north, point[1]);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (lons.length === 0)
|
|
163
|
+
continue;
|
|
164
|
+
// the narrowest span of longitudes: the widest gap between neighbouring longitudes, round the circle, is the part the shape leaves out
|
|
165
|
+
const sorted = [...new Set(lons.map((lon) => ((lon % 360) + 540) % 360 - 180))].sort((a, b) => a - b);
|
|
166
|
+
let gap = sorted[0] + 360 - sorted[sorted.length - 1];
|
|
167
|
+
let west = sorted[0];
|
|
168
|
+
let east = sorted[sorted.length - 1];
|
|
169
|
+
for (let i = 1; i < sorted.length; i += 1) {
|
|
170
|
+
const step = sorted[i] - sorted[i - 1];
|
|
171
|
+
if (step > gap) {
|
|
172
|
+
gap = step;
|
|
173
|
+
west = sorted[i];
|
|
174
|
+
east = sorted[i - 1];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
boxes.set(code, [west, south, east, north]);
|
|
178
|
+
}
|
|
179
|
+
return boxes;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The codes whose boxes come within `angle` degrees of a place: the countries whose regions a view may show.
|
|
183
|
+
*
|
|
184
|
+
* @param boxes - The boxes from `shapeBoxes`.
|
|
185
|
+
* @param lon - The place's longitude, degrees.
|
|
186
|
+
* @param lat - The place's latitude, degrees.
|
|
187
|
+
* @param angle - How far round the sphere the view reaches from the place, degrees (90 for the whole near side).
|
|
188
|
+
* @returns The codes, in the boxes' order.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* import { codesNear } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
193
|
+
*
|
|
194
|
+
* const boxes = new Map<string, [number, number, number, number]>([["JP", [122, 24, 154, 46]], ["BR", [-74, -34, -34, 5]]]);
|
|
195
|
+
* console.log(codesNear(boxes, 139.7, 35.7, 10));
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export function codesNear(boxes, lon, lat, angle) {
|
|
199
|
+
const phi0 = lat * RAD;
|
|
200
|
+
const sin0 = Math.sin(phi0);
|
|
201
|
+
const cos0 = Math.cos(phi0);
|
|
202
|
+
const reach = Math.cos(Math.min(180, Math.max(0, angle)) * RAD);
|
|
203
|
+
/** The cosine of the nearest distance from the place to a meridian `apart` degrees away, between two latitudes: the nearest point is where sin(lat) sin(lat0) + cos(lat) cos(lat0) cos(apart) is largest, held between them. */
|
|
204
|
+
const nearestOnMeridian = (apart, south, north) => {
|
|
205
|
+
const best = Math.atan2(sin0, cos0 * Math.cos(apart * RAD)) / RAD;
|
|
206
|
+
const at = Math.max(south, Math.min(north, best > 90 ? 90 : best < -90 ? -90 : best)) * RAD;
|
|
207
|
+
return sin0 * Math.sin(at) + cos0 * Math.cos(at) * Math.cos(apart * RAD);
|
|
208
|
+
};
|
|
209
|
+
const found = [];
|
|
210
|
+
for (const [code, [west, south, east, north]] of boxes) {
|
|
211
|
+
const width = (((east - west) % 360) + 360) % 360;
|
|
212
|
+
const fromWest = (((lon - west) % 360) + 360) % 360;
|
|
213
|
+
let near;
|
|
214
|
+
if (fromWest <= width) {
|
|
215
|
+
// the place is within the box's longitudes: the nearest point is straight north or south of it
|
|
216
|
+
const at = Math.max(south, Math.min(north, lat));
|
|
217
|
+
near = Math.cos((at - lat) * RAD);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
// outside them: the nearest point is on one of the box's two edge meridians (along a parallel the distance grows away from the place)
|
|
221
|
+
near = Math.max(nearestOnMeridian(fromWest, south, north), nearestOnMeridian(fromWest - width, south, north));
|
|
222
|
+
}
|
|
223
|
+
if (near >= reach - 1e-12)
|
|
224
|
+
found.push(code);
|
|
225
|
+
}
|
|
226
|
+
return found;
|
|
227
|
+
}
|
package/dist/cities/host.d.ts
CHANGED
|
@@ -34,6 +34,8 @@ export interface CityHostOptions {
|
|
|
34
34
|
onError(error: unknown): void;
|
|
35
35
|
/** Loads kuni's cities; the page's own copy when it has one (the property is for tests and for pages that bundle their own). */
|
|
36
36
|
load?(tier: CityNameTier): Promise<readonly NamedCity[]>;
|
|
37
|
+
/** Told when the names were laid out again between draws (a tier arrived, the language or a font changed), so names that keep clear of these can be laid out again. */
|
|
38
|
+
onLayout?(): void;
|
|
37
39
|
}
|
|
38
40
|
export interface CityHost {
|
|
39
41
|
/** Tells where the Earth is in the picture just drawn, or `null` when there is no Earth to name. */
|
|
@@ -51,6 +53,13 @@ export interface CityHost {
|
|
|
51
53
|
names: number;
|
|
52
54
|
renders: number;
|
|
53
55
|
};
|
|
56
|
+
/** The boxes the names and their dots take in the picture just drawn, for other names to keep clear of. */
|
|
57
|
+
readonly boxes: {
|
|
58
|
+
left: number;
|
|
59
|
+
top: number;
|
|
60
|
+
width: number;
|
|
61
|
+
height: number;
|
|
62
|
+
}[];
|
|
54
63
|
dispose(): void;
|
|
55
64
|
}
|
|
56
65
|
/**
|
package/dist/cities/host.js
CHANGED
|
@@ -117,6 +117,7 @@ export function createCityHost(options) {
|
|
|
117
117
|
lists.set(tier, cities);
|
|
118
118
|
version += 1;
|
|
119
119
|
compute();
|
|
120
|
+
options.onLayout?.();
|
|
120
121
|
})
|
|
121
122
|
.catch((error) => options.onError(error));
|
|
122
123
|
}
|
|
@@ -186,14 +187,18 @@ export function createCityHost(options) {
|
|
|
186
187
|
}
|
|
187
188
|
// the family's chooser tells the document when the page's language changes: the names are written again, though nothing else is drawn
|
|
188
189
|
const onLanguage = () => queueMicrotask(() => {
|
|
189
|
-
if (max !== null && last && usedLang !== undefined && usedLang !== options.language())
|
|
190
|
+
if (max !== null && last && usedLang !== undefined && usedLang !== options.language()) {
|
|
190
191
|
compute();
|
|
192
|
+
options.onLayout?.();
|
|
193
|
+
}
|
|
191
194
|
});
|
|
192
195
|
doc.addEventListener("kazoku-languagechange", onLanguage);
|
|
193
196
|
// a font arrives: the names are measured in the font they are drawn in, so they are laid out again
|
|
194
197
|
const onFonts = () => {
|
|
195
|
-
if (max !== null && last)
|
|
198
|
+
if (max !== null && last) {
|
|
196
199
|
compute();
|
|
200
|
+
options.onLayout?.();
|
|
201
|
+
}
|
|
197
202
|
};
|
|
198
203
|
doc.fonts?.addEventListener?.("loadingdone", onFonts);
|
|
199
204
|
return {
|
|
@@ -220,6 +225,13 @@ export function createCityHost(options) {
|
|
|
220
225
|
vars();
|
|
221
226
|
compute();
|
|
222
227
|
},
|
|
228
|
+
get boxes() {
|
|
229
|
+
const dot = (style.dotSize ?? DEFAULTS.dot) + 1;
|
|
230
|
+
return shown.flatMap((one) => [
|
|
231
|
+
{ left: one.left, top: one.top, width: one.width, height: one.height },
|
|
232
|
+
{ left: one.x - dot, top: one.y - dot, width: dot * 2, height: dot * 2 },
|
|
233
|
+
]);
|
|
234
|
+
},
|
|
223
235
|
get stats() {
|
|
224
236
|
return { on: max !== null, max: max ?? 0, radius: last?.radius ?? 0, tiers: [...lists.keys()], cities: [...lists.values()].reduce((sum, list) => sum + list.length, 0), names: shown.length, renders };
|
|
225
237
|
},
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import type { CityFrame } from "../cities/geometry.ts";
|
|
2
|
+
/**
|
|
3
|
+
* One country outline the names are made from: a GeoJSON feature in longitude and latitude (a `Polygon` or `MultiPolygon`) with its code and its English and Japanese names, as chizu's
|
|
4
|
+
* `world-geo` has them. A feature with no name is passed over.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import type { NamedShape } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
9
|
+
*
|
|
10
|
+
* const square: NamedShape = { properties: { code: "A", name: "Square", nameJa: "四角" }, geometry: { type: "Polygon", coordinates: [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]] } };
|
|
11
|
+
* console.log(square.properties?.name);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export interface NamedShape {
|
|
15
|
+
properties?: {
|
|
16
|
+
code?: string | null;
|
|
17
|
+
name?: string | null;
|
|
18
|
+
nameJa?: string | null;
|
|
19
|
+
} | null;
|
|
20
|
+
geometry: {
|
|
21
|
+
type: string;
|
|
22
|
+
coordinates: unknown;
|
|
23
|
+
} | null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The countries ready to be named: for each, its code and names, and points spread evenly over its land (longitude, latitude, the area each stands for in square degrees of the sphere, and which
|
|
27
|
+
* of the country's polygons it is in), with the cap of the sphere they all lie in.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { countrySpots, type CountrySpots } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
32
|
+
*
|
|
33
|
+
* const spots: CountrySpots = countrySpots({ features: [] });
|
|
34
|
+
* console.log(spots.countries.length);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export interface CountrySpots {
|
|
38
|
+
countries: {
|
|
39
|
+
code: string;
|
|
40
|
+
name: {
|
|
41
|
+
en: string;
|
|
42
|
+
ja?: string;
|
|
43
|
+
};
|
|
44
|
+
/** lon, lat, area, polygon for each point. */
|
|
45
|
+
points: Float32Array;
|
|
46
|
+
/** The middle of the points on the sphere, and how far from it (degrees) the farthest one is. */
|
|
47
|
+
cap: {
|
|
48
|
+
lon: number;
|
|
49
|
+
lat: number;
|
|
50
|
+
reach: number;
|
|
51
|
+
};
|
|
52
|
+
}[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A country named at one draw: its code, the text, where the middle of the part in view falls, the box the text goes in (top left corner and size) and the text's size in pixels.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import type { CountryName } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
60
|
+
*
|
|
61
|
+
* const say = (one: CountryName): string => `${one.text} at ${one.left}, ${one.top}`;
|
|
62
|
+
* console.log(typeof say);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export interface CountryName {
|
|
66
|
+
code: string;
|
|
67
|
+
text: string;
|
|
68
|
+
x: number;
|
|
69
|
+
y: number;
|
|
70
|
+
left: number;
|
|
71
|
+
top: number;
|
|
72
|
+
width: number;
|
|
73
|
+
height: number;
|
|
74
|
+
size: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Where the Earth is on the screen at one draw: the frame the city names use, and the Earth's disc (its middle and radius in CSS pixels), which no name may leave.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import type { CountryFrame } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
82
|
+
*
|
|
83
|
+
* const frame: CountryFrame = { width: 400, height: 400, radius: 150, disc: { x: 200, y: 200, radius: 150 }, project: (lon, lat, out) => { out.x = 200 + lon; out.y = 200 - lat; return 1; } };
|
|
84
|
+
* console.log(frame.disc?.radius);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export interface CountryFrame extends CityFrame {
|
|
88
|
+
disc?: {
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
radius: number;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* What the names are laid out with: the most names, the smallest and largest text in pixels, the boxes other names use, and the width and height of a text as the browser draws it.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* import type { CountryLayoutOptions } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
100
|
+
*
|
|
101
|
+
* const options: CountryLayoutOptions = { max: 40, smallest: 10, largest: 16 };
|
|
102
|
+
* console.log(options.max);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export interface CountryLayoutOptions {
|
|
106
|
+
max: number;
|
|
107
|
+
smallest: number;
|
|
108
|
+
largest: number;
|
|
109
|
+
/** Boxes other names already use (cities, places, the bodies' names): no country name is put over one. */
|
|
110
|
+
taken?: readonly {
|
|
111
|
+
left: number;
|
|
112
|
+
top: number;
|
|
113
|
+
width: number;
|
|
114
|
+
height: number;
|
|
115
|
+
}[];
|
|
116
|
+
/**
|
|
117
|
+
* The width and height of a text at a size, as the browser draws it in the names' own font (a canvas's `measureText`). The estimate from the length is a floor and the larger is used, so a
|
|
118
|
+
* fallback font with wider letters or taller lines is measured as it is. Left out, the estimate alone is used, as in Node.
|
|
119
|
+
*/
|
|
120
|
+
measure?: (text: string, size: number) => {
|
|
121
|
+
width: number;
|
|
122
|
+
height: number;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The space between the letters of a country's name, as a share of the text's size (the style sheet's `letter-spacing` in `em`).
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* import { COUNTRY_LETTER_SPACING } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
131
|
+
*
|
|
132
|
+
* console.log(COUNTRY_LETTER_SPACING * 12);
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare const COUNTRY_LETTER_SPACING = 0.06;
|
|
136
|
+
/**
|
|
137
|
+
* Spreads points over each named country's land, about 48 to a country on an even grid of longitude and latitude, so that a view can find the middle of the part of it in sight and how large
|
|
138
|
+
* that part is. A polygon too small for the grid gets one point, at the middle of its outline. Made once, when the outlines arrive.
|
|
139
|
+
*
|
|
140
|
+
* @param shapes - The outlines, as chizu's `world-geo` has them.
|
|
141
|
+
* @returns The countries and their points.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* import { countrySpots } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
146
|
+
*
|
|
147
|
+
* const square = { properties: { code: "A", name: "Square" }, geometry: { type: "Polygon", coordinates: [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]] } };
|
|
148
|
+
* const spots = countrySpots({ features: [square] });
|
|
149
|
+
* console.log(spots.countries[0]?.points.length! / 4);
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function countrySpots(shapes: {
|
|
153
|
+
features: readonly NamedShape[];
|
|
154
|
+
}): CountrySpots;
|
|
155
|
+
/**
|
|
156
|
+
* The name to write for a country: its Japanese name on a page in Japanese where there is one, else its English name in capitals.
|
|
157
|
+
*
|
|
158
|
+
* @param country - The country's names.
|
|
159
|
+
* @param lang - The page's language, `en` or `ja`.
|
|
160
|
+
* @returns The text.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* import { countryNameText } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
165
|
+
*
|
|
166
|
+
* const japan = { name: { en: "Japan", ja: "日本" } };
|
|
167
|
+
* console.log(countryNameText(japan, "en"), countryNameText(japan, "ja"));
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export declare function countryNameText(country: {
|
|
171
|
+
name: {
|
|
172
|
+
en: string;
|
|
173
|
+
ja?: string;
|
|
174
|
+
};
|
|
175
|
+
}, lang: "en" | "ja"): string;
|
|
176
|
+
/**
|
|
177
|
+
* About how wide and tall a country's name is, in pixels, without reading any layout: a Japanese character as wide as the text is tall, a capital about 0.7 of it, with the letters' spacing.
|
|
178
|
+
*
|
|
179
|
+
* @param text - The name.
|
|
180
|
+
* @param size - The text size in pixels.
|
|
181
|
+
* @returns The width and height.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* import { countryNameBox } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
186
|
+
*
|
|
187
|
+
* console.log(countryNameBox("JAPAN", 12), countryNameBox("日本", 12));
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export declare function countryNameBox(text: string, size: number): {
|
|
191
|
+
width: number;
|
|
192
|
+
height: number;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Lays the names out for one draw. Each country in sight is named at the middle of the largest part of it the view shows (snapped to a point of its own land, so a crescent's name is not out at
|
|
196
|
+
* sea), the largest on the screen first, its text sized by that part, only where the text fits within it, inside the Earth's disc, and free of the names already placed and of `taken`
|
|
197
|
+
* (tried at the middle, a line above and below, then at the next points of its land out from the middle). A country on the far side, or too small for its name, is left out: fewer names when the Earth is small, more as it grows.
|
|
198
|
+
*
|
|
199
|
+
* @param spots - The countries' points (see {@link countrySpots}).
|
|
200
|
+
* @param frame - Where the Earth is on the screen.
|
|
201
|
+
* @param lang - The page's language, `en` or `ja`.
|
|
202
|
+
* @param options - The most names, the text sizes, the boxes taken and how to measure.
|
|
203
|
+
* @returns The names to show, the largest first.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* import { countrySpots, layoutCountryNames } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
208
|
+
*
|
|
209
|
+
* const square = { properties: { code: "A", name: "Square", nameJa: "四角" }, geometry: { type: "Polygon", coordinates: [[[-20, -20], [20, -20], [20, 20], [-20, 20], [-20, -20]]] } };
|
|
210
|
+
* const frame = {
|
|
211
|
+
* width: 400, height: 400, radius: 180, disc: { x: 200, y: 200, radius: 180 },
|
|
212
|
+
* project: (lon: number, lat: number, out: { x: number; y: number }) => { out.x = 200 + lon * 3; out.y = 200 - lat * 3; return 1; },
|
|
213
|
+
* };
|
|
214
|
+
* console.log(layoutCountryNames(countrySpots({ features: [square] }), frame, "en", { max: 10, smallest: 10, largest: 16 }).map((one) => one.text));
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
export declare function layoutCountryNames(spots: CountrySpots, frame: CountryFrame, lang: "en" | "ja", options: CountryLayoutOptions): CountryName[];
|
|
218
|
+
/**
|
|
219
|
+
* The most names `country-names` may ask for at once.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* import { COUNTRY_NAMES_MAX } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
224
|
+
*
|
|
225
|
+
* console.log(COUNTRY_NAMES_MAX);
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
export declare const COUNTRY_NAMES_MAX = 120;
|
|
229
|
+
/**
|
|
230
|
+
* How many names a bare `country-names` asks for.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* import { COUNTRY_NAMES_DEFAULT } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
235
|
+
*
|
|
236
|
+
* console.log(COUNTRY_NAMES_DEFAULT);
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
export declare const COUNTRY_NAMES_DEFAULT = 40;
|
|
240
|
+
/**
|
|
241
|
+
* Reads the `country-names` attribute: absent (or `off`) is none, bare is 40 names at most, a number is that many at most (1 to 120).
|
|
242
|
+
*
|
|
243
|
+
* @param value - The attribute's value, or `null` when it is absent.
|
|
244
|
+
* @returns How many names at most, or `null` for none.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* import { readCountryNames } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
249
|
+
*
|
|
250
|
+
* console.log(readCountryNames(null), readCountryNames(""), readCountryNames("12"), readCountryNames("500"), readCountryNames("off"));
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
export declare function readCountryNames(value: string | null): number | null;
|
|
254
|
+
/**
|
|
255
|
+
* The attributes the country names read: `country-names`.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* import { COUNTRY_NAMES_ATTRIBUTES } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
260
|
+
*
|
|
261
|
+
* console.log(COUNTRY_NAMES_ATTRIBUTES.join(" "));
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
export declare const COUNTRY_NAMES_ATTRIBUTES: readonly ["country-names"];
|