@johnmorrisdotca/wakusei-planets 0.28.0 → 0.31.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 +30 -0
- package/NOTICE.md +20 -0
- package/README.md +61 -16
- package/dist/embed.d.ts +64 -0
- package/dist/embed.js +66 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/lineup/data.d.ts +160 -0
- package/dist/lineup/data.js +303 -0
- package/dist/lineup/element.d.ts +99 -0
- package/dist/lineup/element.js +782 -0
- package/dist/lineup/labels.d.ts +141 -0
- package/dist/lineup/labels.js +155 -0
- package/dist/lineup/layout.d.ts +174 -0
- package/dist/lineup/layout.js +162 -0
- package/dist/lineup/scene3d.d.ts +47 -0
- package/dist/lineup/scene3d.js +155 -0
- package/dist/portrait/element.d.ts +5 -2
- package/dist/portrait/element.js +57 -6
- package/dist/portrait/scene3d.d.ts +13 -0
- package/dist/portrait/scene3d.js +21 -2
- package/dist/scene/body.d.ts +3 -1
- package/dist/scene/body.js +2 -1
- package/dist/scene/detail.d.ts +8 -2
- package/dist/scene/detail.js +56 -28
- package/dist/scene/painter.d.ts +32 -0
- package/dist/scene/painter.js +94 -10
- package/dist/scene/textures.d.ts +4 -2
- package/dist/scene/textures.js +2 -0
- package/dist/scene/tiles.d.ts +46 -0
- package/dist/scene/tiles.js +165 -19
- package/dist/space/arrows.d.ts +87 -0
- package/dist/space/arrows.js +117 -0
- package/dist/space/beside.d.ts +74 -0
- package/dist/space/beside.js +76 -0
- package/dist/space/bodies.d.ts +83 -0
- package/dist/space/bodies.js +93 -0
- package/dist/space/clock.d.ts +31 -0
- package/dist/space/clock.js +45 -0
- package/dist/space/element.d.ts +98 -15
- package/dist/space/element.js +320 -63
- package/dist/space/frame.d.ts +45 -2
- package/dist/space/frame.js +56 -4
- package/dist/space/index.d.ts +6 -1
- package/dist/space/index.js +6 -1
- package/dist/space/labels.d.ts +52 -0
- package/dist/space/labels.js +40 -0
- package/dist/space/scale.d.ts +5 -5
- package/dist/space/scale.js +10 -4
- package/dist/space/scene.d.ts +50 -9
- package/dist/space/scene.js +313 -36
- package/dist/space/spin.d.ts +37 -0
- package/dist/space/spin.js +29 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +8 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { LineupBody } from "./data.ts";
|
|
2
|
+
import type { LineupDot } from "./layout.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The two languages of the words: `en` and `ja`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import type { LineupLang } from "@johnmorrisdotca/wakusei-planets";
|
|
9
|
+
*
|
|
10
|
+
* const lang: LineupLang = "ja";
|
|
11
|
+
* console.log(lang);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export type LineupLang = "en" | "ja";
|
|
15
|
+
/**
|
|
16
|
+
* A length in kilometres in words: grouped digits up to a million (up to a hundred million in Japanese), then `735 million km` or `7.35億 km`, and `billion` above a thousand million.
|
|
17
|
+
*
|
|
18
|
+
* @param km - The length in kilometres.
|
|
19
|
+
* @param lang - `en` or `ja`.
|
|
20
|
+
* @returns The text.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { lineupKm } from "@johnmorrisdotca/wakusei-planets";
|
|
25
|
+
*
|
|
26
|
+
* console.log(lineupKm(695_700, "en"), lineupKm(735_400_000, "en"), lineupKm(735_400_000, "ja"));
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function lineupKm(km: number, lang: LineupLang): string;
|
|
30
|
+
/**
|
|
31
|
+
* What the line-up says about one body, in words.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { lineupBodies, lineupText, type LineupText } from "@johnmorrisdotca/wakusei-planets";
|
|
36
|
+
*
|
|
37
|
+
* const text: LineupText = lineupText(lineupBodies().find((body) => body.id === "betelgeuse")!, "en");
|
|
38
|
+
* console.log(text.radius, text.km);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export interface LineupText {
|
|
42
|
+
name: string;
|
|
43
|
+
/** `1,057 Suns (+228 −162)`, or in Japanese `太陽の1,057倍 (+228 −162)`. */
|
|
44
|
+
radius: string;
|
|
45
|
+
/** `735 million km`. */
|
|
46
|
+
km: string;
|
|
47
|
+
/** The source, short, and `estimated` where the size is not measured. */
|
|
48
|
+
source: string;
|
|
49
|
+
/** How the radius was got, one sentence. Empty for a planet. */
|
|
50
|
+
method: string;
|
|
51
|
+
/** What the literature disagrees about, if it does. */
|
|
52
|
+
note: string;
|
|
53
|
+
/** True when the size is estimated (from brightness, or from a distance that is not a parallax), not measured. */
|
|
54
|
+
estimated: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The words for a body: its name, radius in Suns with the error, radius in kilometres, the source and (for a star) how the radius was got and what the literature disagrees about.
|
|
58
|
+
*
|
|
59
|
+
* @param body - A body from `lineupBodies`.
|
|
60
|
+
* @param lang - `en` or `ja`.
|
|
61
|
+
* @returns The words.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { lineupBodies, lineupText } from "@johnmorrisdotca/wakusei-planets";
|
|
66
|
+
*
|
|
67
|
+
* const antares = lineupBodies().find((body) => body.id === "antares")!;
|
|
68
|
+
* console.log(lineupText(antares, "ja").radius);
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function lineupText(body: LineupBody, lang: LineupLang): LineupText;
|
|
72
|
+
/**
|
|
73
|
+
* A label placed on the picture: a plate of lines at a place, and the dot it belongs to.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { lineupBodies, lineupLabels, lineupProject, lineupView, placeLineup, type LineupLabel } from "@johnmorrisdotca/wakusei-planets";
|
|
78
|
+
*
|
|
79
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
80
|
+
* const labels: LineupLabel[] = lineupLabels(lineupProject(places, lineupView(places, 0.5), 800, 450), 800, 450, "en").labels;
|
|
81
|
+
* console.log(labels.map((label) => label.id).join(", "));
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export interface LineupLabel {
|
|
85
|
+
id: string;
|
|
86
|
+
lines: string[];
|
|
87
|
+
/** The top left of the plate, in pixels. */
|
|
88
|
+
x: number;
|
|
89
|
+
y: number;
|
|
90
|
+
width: number;
|
|
91
|
+
height: number;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The labels and rings for a picture: the biggest bodies first, each with as much as fits, a label dropped when it would cover one already placed, and a ring round a body too small to see. A body outside the picture has none.
|
|
95
|
+
*
|
|
96
|
+
* @param dots - The bodies on the picture (`lineupProject`).
|
|
97
|
+
* @param width - The picture's width in pixels.
|
|
98
|
+
* @param height - The picture's height in pixels.
|
|
99
|
+
* @param lang - `en` or `ja`.
|
|
100
|
+
* @param options - `font` (pixels, 12 by default), `keep` (the id of a body whose label is never dropped).
|
|
101
|
+
* @returns The labels and the rings (`rings`: a centre and a radius for each body under 6 pixels across).
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* import { lineupBodies, lineupLabels, lineupProject, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
106
|
+
*
|
|
107
|
+
* const places = placeLineup(lineupBodies({ planets: true }), "row");
|
|
108
|
+
* const plan = lineupLabels(lineupProject(places, lineupView(places, 0.05), 800, 450), 800, 450, "en");
|
|
109
|
+
* console.log(plan.labels.length, plan.rings.length);
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare function lineupLabels(dots: readonly LineupDot[], width: number, height: number, lang: LineupLang, options?: {
|
|
113
|
+
font?: number;
|
|
114
|
+
keep?: string;
|
|
115
|
+
}): {
|
|
116
|
+
labels: LineupLabel[];
|
|
117
|
+
rings: {
|
|
118
|
+
id: string;
|
|
119
|
+
cx: number;
|
|
120
|
+
cy: number;
|
|
121
|
+
r: number;
|
|
122
|
+
}[];
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* The labels and rings as SVG markup (the contents of an `<svg>` that is `width` by `height`): a dark plate with white lines for each label, and a thin ring and tick for each body too small to see. The first line of a
|
|
126
|
+
* plate is bold.
|
|
127
|
+
*
|
|
128
|
+
* @param plan - What `lineupLabels` returned.
|
|
129
|
+
* @param font - The pixel size of the text; the same as was given to `lineupLabels` (12 by default).
|
|
130
|
+
* @returns SVG markup.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* import { lineupBodies, lineupLabels, lineupLabelsSvg, lineupProject, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
135
|
+
*
|
|
136
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
137
|
+
* const plan = lineupLabels(lineupProject(places, lineupView(places, 0.4), 800, 450), 800, 450, "en");
|
|
138
|
+
* console.log(lineupLabelsSvg(plan).startsWith("<g"));
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export declare function lineupLabelsSvg(plan: ReturnType<typeof lineupLabels>, font?: number): string;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
const group = (value) => value.toLocaleString("en-US");
|
|
2
|
+
/** A radius in Suns with as many places as its error deserves: whole above 100, one decimal above 10, two or three below, and three figures for a planet. */
|
|
3
|
+
function suns(value, error) {
|
|
4
|
+
if (value >= 100)
|
|
5
|
+
return group(Math.round(value));
|
|
6
|
+
if (value >= 10)
|
|
7
|
+
return group(Number(value.toFixed(1)));
|
|
8
|
+
if (value >= 1)
|
|
9
|
+
return String(Number(value.toFixed(error > 0 && error < 0.01 ? 3 : 2)));
|
|
10
|
+
return String(Number(value.toPrecision(3)));
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A length in kilometres in words: grouped digits up to a million (up to a hundred million in Japanese), then `735 million km` or `7.35億 km`, and `billion` above a thousand million.
|
|
14
|
+
*
|
|
15
|
+
* @param km - The length in kilometres.
|
|
16
|
+
* @param lang - `en` or `ja`.
|
|
17
|
+
* @returns The text.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { lineupKm } from "@johnmorrisdotca/wakusei-planets";
|
|
22
|
+
*
|
|
23
|
+
* console.log(lineupKm(695_700, "en"), lineupKm(735_400_000, "en"), lineupKm(735_400_000, "ja"));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function lineupKm(km, lang) {
|
|
27
|
+
if (lang === "ja")
|
|
28
|
+
return km >= 1e8 ? `${Number((km / 1e8).toPrecision(3))}億 km` : `${group(Math.round(km))} km`;
|
|
29
|
+
if (km >= 1e9)
|
|
30
|
+
return `${Number((km / 1e9).toPrecision(3))} billion km`;
|
|
31
|
+
if (km >= 1e6)
|
|
32
|
+
return `${Number((km / 1e6).toPrecision(3))} million km`;
|
|
33
|
+
return `${group(Math.round(km))} km`;
|
|
34
|
+
}
|
|
35
|
+
/** The error: `±0.2`, `+228 -162` (with a true minus), or nothing when there is none. */
|
|
36
|
+
function error(minus, plus) {
|
|
37
|
+
if (minus === 0 && plus === 0)
|
|
38
|
+
return "";
|
|
39
|
+
const fmt = (v) => (v >= 10 ? group(Math.round(v)) : String(Number(v.toPrecision(2))));
|
|
40
|
+
return Math.abs(minus - plus) < 1e-9 ? `±${fmt(plus)}` : `+${fmt(plus)} −${fmt(minus)}`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The words for a body: its name, radius in Suns with the error, radius in kilometres, the source and (for a star) how the radius was got and what the literature disagrees about.
|
|
44
|
+
*
|
|
45
|
+
* @param body - A body from `lineupBodies`.
|
|
46
|
+
* @param lang - `en` or `ja`.
|
|
47
|
+
* @returns The words.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { lineupBodies, lineupText } from "@johnmorrisdotca/wakusei-planets";
|
|
52
|
+
*
|
|
53
|
+
* const antares = lineupBodies().find((body) => body.id === "antares")!;
|
|
54
|
+
* console.log(lineupText(antares, "ja").radius);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export function lineupText(body, lang) {
|
|
58
|
+
const err = error(body.minus, body.plus);
|
|
59
|
+
const value = suns(body.radiusSun, Math.max(body.minus, body.plus));
|
|
60
|
+
const row = body.row;
|
|
61
|
+
const estimated = row !== undefined && (row.basis === "luminosity" || row.basis === "weak-distance");
|
|
62
|
+
const radius = lang === "ja" ? `太陽の${value}倍${err === "" ? "" : ` (${err})`}` : `${value} ${value === "1" ? "Sun" : "Suns"}${err === "" ? "" : ` (${err})`}`;
|
|
63
|
+
const cite = row === undefined ? (lang === "ja" ? "IAU・NASAの値 (赤道半径)" : "IAU and NASA (equatorial radius)") : row.cite;
|
|
64
|
+
return {
|
|
65
|
+
name: body.name[lang],
|
|
66
|
+
radius,
|
|
67
|
+
km: lineupKm(body.radiusKm, lang),
|
|
68
|
+
source: estimated ? `${cite} · ${lang === "ja" ? "推定" : "estimated"}` : cite,
|
|
69
|
+
method: row?.method[lang] ?? "",
|
|
70
|
+
note: row?.note?.[lang] ?? "",
|
|
71
|
+
estimated,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The labels and rings for a picture: the biggest bodies first, each with as much as fits, a label dropped when it would cover one already placed, and a ring round a body too small to see. A body outside the picture has none.
|
|
76
|
+
*
|
|
77
|
+
* @param dots - The bodies on the picture (`lineupProject`).
|
|
78
|
+
* @param width - The picture's width in pixels.
|
|
79
|
+
* @param height - The picture's height in pixels.
|
|
80
|
+
* @param lang - `en` or `ja`.
|
|
81
|
+
* @param options - `font` (pixels, 12 by default), `keep` (the id of a body whose label is never dropped).
|
|
82
|
+
* @returns The labels and the rings (`rings`: a centre and a radius for each body under 6 pixels across).
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { lineupBodies, lineupLabels, lineupProject, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
87
|
+
*
|
|
88
|
+
* const places = placeLineup(lineupBodies({ planets: true }), "row");
|
|
89
|
+
* const plan = lineupLabels(lineupProject(places, lineupView(places, 0.05), 800, 450), 800, 450, "en");
|
|
90
|
+
* console.log(plan.labels.length, plan.rings.length);
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function lineupLabels(dots, width, height, lang, options = {}) {
|
|
94
|
+
const font = options.font ?? 12;
|
|
95
|
+
const line = font * 1.4;
|
|
96
|
+
const pad = font * 0.7;
|
|
97
|
+
const placed = [];
|
|
98
|
+
const rings = [];
|
|
99
|
+
const inside = dots.filter((dot) => dot.cx + dot.r > 0 && dot.cx - dot.r < width && dot.cy + dot.r > 0 && dot.cy - dot.r < height && dot.r > 0.01);
|
|
100
|
+
// the body kept is placed first, so no other label can cover it; the rest go biggest first
|
|
101
|
+
const bySize = [...inside].sort((a, b) => (a.place.body.id === options.keep ? -1 : b.place.body.id === options.keep ? 1 : b.r - a.r));
|
|
102
|
+
for (const dot of bySize) {
|
|
103
|
+
const id = dot.place.body.id;
|
|
104
|
+
const ring = dot.r < 6;
|
|
105
|
+
if (ring)
|
|
106
|
+
rings.push({ id, cx: dot.cx, cy: dot.cy, r: Math.max(6, dot.r + 3) });
|
|
107
|
+
const tier = dot.r >= 70 ? "full" : dot.r >= 6 ? "short" : "marker";
|
|
108
|
+
const text = lineupText(dot.place.body, lang);
|
|
109
|
+
const lines = tier === "full" ? [text.name, text.radius, text.km, text.source] : tier === "short" ? [text.name, text.radius] : [text.name];
|
|
110
|
+
const chars = Math.max(...lines.map((one) => [...one].reduce((sum, ch) => sum + (ch.charCodeAt(0) > 0x2e80 ? 1.9 : 0.62), 0)));
|
|
111
|
+
const w = chars * font + pad * 2;
|
|
112
|
+
const h = lines.length * line + pad * 1.2;
|
|
113
|
+
// the visible part of the disc, so a body bigger than the picture is labelled where it can be seen
|
|
114
|
+
const left = Math.max(0, dot.cx - dot.r);
|
|
115
|
+
const right = Math.min(width, dot.cx + dot.r);
|
|
116
|
+
const x = Math.min(width - w - 6, Math.max(6, (left + right) / 2 - w / 2));
|
|
117
|
+
const base = ring ? dot.cy + Math.max(6, dot.r + 3) + 6 : dot.cy + dot.r + 8;
|
|
118
|
+
const y = Math.max(6, Math.min(height - h - 6, base));
|
|
119
|
+
const box = { id, lines, x, y, width: w, height: h };
|
|
120
|
+
const clash = placed.some((other) => box.x < other.x + other.width + 4 && box.x + box.width + 4 > other.x && box.y < other.y + other.height + 4 && box.y + box.height + 4 > other.y);
|
|
121
|
+
if (!clash || id === options.keep)
|
|
122
|
+
placed.push(box);
|
|
123
|
+
}
|
|
124
|
+
return { labels: placed, rings };
|
|
125
|
+
}
|
|
126
|
+
const esc = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
127
|
+
/**
|
|
128
|
+
* The labels and rings as SVG markup (the contents of an `<svg>` that is `width` by `height`): a dark plate with white lines for each label, and a thin ring and tick for each body too small to see. The first line of a
|
|
129
|
+
* plate is bold.
|
|
130
|
+
*
|
|
131
|
+
* @param plan - What `lineupLabels` returned.
|
|
132
|
+
* @param font - The pixel size of the text; the same as was given to `lineupLabels` (12 by default).
|
|
133
|
+
* @returns SVG markup.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* import { lineupBodies, lineupLabels, lineupLabelsSvg, lineupProject, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
138
|
+
*
|
|
139
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
140
|
+
* const plan = lineupLabels(lineupProject(places, lineupView(places, 0.4), 800, 450), 800, 450, "en");
|
|
141
|
+
* console.log(lineupLabelsSvg(plan).startsWith("<g"));
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function lineupLabelsSvg(plan, font = 12) {
|
|
145
|
+
const line = font * 1.4;
|
|
146
|
+
const pad = font * 0.7;
|
|
147
|
+
const rings = plan.rings.map((ring) => `<circle cx="${ring.cx.toFixed(1)}" cy="${ring.cy.toFixed(1)}" r="${ring.r.toFixed(1)}" fill="none" stroke="#ffffff" stroke-opacity="0.75" stroke-width="${(font / 12).toFixed(2)}"/>`).join("");
|
|
148
|
+
const plates = plan.labels
|
|
149
|
+
.map((label) => {
|
|
150
|
+
const text = label.lines.map((one, i) => `<text x="${(label.x + pad).toFixed(1)}" y="${(label.y + pad + line * (i + 0.8)).toFixed(1)}" font-size="${font}" fill="#ffffff"${i === 0 ? ' font-weight="700"' : ' fill-opacity="0.9"'}>${esc(one)}</text>`).join("");
|
|
151
|
+
return `<g data-label="${esc(label.id)}"><rect x="${label.x.toFixed(1)}" y="${label.y.toFixed(1)}" width="${label.width.toFixed(1)}" height="${label.height.toFixed(1)}" rx="${(font * 0.4).toFixed(1)}" fill="#05070d" fill-opacity="0.78" stroke="#ffffff" stroke-opacity="0.2"/>${text}</g>`;
|
|
152
|
+
})
|
|
153
|
+
.join("");
|
|
154
|
+
return `<g font-family="system-ui, -apple-system, 'Segoe UI', 'Hiragino Sans', 'Noto Sans JP', sans-serif">${rings}${plates}</g>`;
|
|
155
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { LineupBody } from "./data.ts";
|
|
2
|
+
/**
|
|
3
|
+
* How the bodies stand: `row` side by side, left to right, or `stack` one above the next, top to bottom, smallest first.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { LINEUP_LAYOUTS, type LineupLayout } from "@johnmorrisdotca/wakusei-planets";
|
|
8
|
+
*
|
|
9
|
+
* const layout: LineupLayout = LINEUP_LAYOUTS[1];
|
|
10
|
+
* console.log(layout);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare const LINEUP_LAYOUTS: readonly ["row", "stack"];
|
|
14
|
+
/**
|
|
15
|
+
* One of the layouts in `LINEUP_LAYOUTS`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import type { LineupLayout } from "@johnmorrisdotca/wakusei-planets";
|
|
20
|
+
*
|
|
21
|
+
* const layout: LineupLayout = "row";
|
|
22
|
+
* console.log(layout);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type LineupLayout = (typeof LINEUP_LAYOUTS)[number];
|
|
26
|
+
/**
|
|
27
|
+
* A body with its place: the centre in Suns and the radius in Suns.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { lineupBodies, placeLineup, type LineupPlace } from "@johnmorrisdotca/wakusei-planets";
|
|
32
|
+
*
|
|
33
|
+
* const second: LineupPlace = placeLineup(lineupBodies({ set: "bright" }), "row")[1]!;
|
|
34
|
+
* console.log(second.body.id, second.x, second.r);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export interface LineupPlace {
|
|
38
|
+
body: LineupBody;
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
r: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The camera: its centre and the half of the picture's height, in Suns. The picture is `2 * h` Suns tall and `2 * h * aspect` wide.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { lineupBodies, lineupView, placeLineup, type LineupView } from "@johnmorrisdotca/wakusei-planets";
|
|
49
|
+
*
|
|
50
|
+
* const view: LineupView = lineupView(placeLineup(lineupBodies(), "row"), 1);
|
|
51
|
+
* console.log(view.h);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export interface LineupView {
|
|
55
|
+
x: number;
|
|
56
|
+
y: number;
|
|
57
|
+
h: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Where each body sits. `bodies` is sorted by radius, smallest first, as `lineupBodies` gives it: the first is at the origin and each next one is a quarter of its own radius beyond the edge of the one before,
|
|
61
|
+
* to the right in a `row` and downward in a `stack`.
|
|
62
|
+
*
|
|
63
|
+
* @param bodies - The bodies, smallest first.
|
|
64
|
+
* @param layout - `row` (the default) or `stack`.
|
|
65
|
+
* @returns The places, in the same order.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { lineupBodies, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
70
|
+
*
|
|
71
|
+
* const places = placeLineup(lineupBodies({ set: "bright", planets: true }), "stack");
|
|
72
|
+
* console.log(places.map((place) => place.y.toFixed(2)).join(" "));
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function placeLineup(bodies: readonly LineupBody[], layout?: LineupLayout): LineupPlace[];
|
|
76
|
+
/**
|
|
77
|
+
* The camera at a zoom from 0 to 1: the size it frames grows from the smallest body's radius to the largest's on a logarithmic scale, and its centre glides from one body to the next, easing in and out, so it rests on each body as the framed size reaches that body's radius. A tall, narrow picture (`aspect` under 1) is zoomed out so the framed body still fits across.
|
|
78
|
+
*
|
|
79
|
+
* @param places - The places, smallest first.
|
|
80
|
+
* @param zoom - 0 (the smallest body fills the picture) to 1 (the largest does). Held to that range.
|
|
81
|
+
* @param aspect - The picture's width over its height, 1.78 by default.
|
|
82
|
+
* @returns The camera.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { lineupBodies, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
87
|
+
*
|
|
88
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
89
|
+
* console.log(lineupView(places, 0).h, lineupView(places, 1).h);
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function lineupView(places: readonly LineupPlace[], zoom: number, aspect?: number): LineupView;
|
|
93
|
+
/**
|
|
94
|
+
* The zoom at which a body fills the picture: where the camera frames its radius. 0 for a lone body.
|
|
95
|
+
*
|
|
96
|
+
* @param places - The places, smallest first.
|
|
97
|
+
* @param id - A body's id.
|
|
98
|
+
* @returns The zoom from 0 to 1, or `undefined` when the id is not in the line-up.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { lineupBodies, lineupZoomOf, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
103
|
+
*
|
|
104
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
105
|
+
* console.log(lineupZoomOf(places, "antares")?.toFixed(3));
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare function lineupZoomOf(places: readonly LineupPlace[], id: string): number | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* The zoom at which the camera frames a size: a radius in Suns, held to the line-up's smallest and largest.
|
|
111
|
+
*
|
|
112
|
+
* @param places - The places, smallest first.
|
|
113
|
+
* @param radius - A radius in Suns.
|
|
114
|
+
* @returns The zoom from 0 to 1 (0 for an empty or lone line-up).
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* import { lineupBodies, lineupZoomFor, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
119
|
+
*
|
|
120
|
+
* console.log(lineupZoomFor(placeLineup(lineupBodies(), "row"), 80).toFixed(3));
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare function lineupZoomFor(places: readonly LineupPlace[], radius: number): number;
|
|
124
|
+
/**
|
|
125
|
+
* The body whose size the camera frames at a zoom: the nearest in the logarithmic scale of radius.
|
|
126
|
+
*
|
|
127
|
+
* @param places - The places, smallest first.
|
|
128
|
+
* @param zoom - 0 to 1.
|
|
129
|
+
* @returns The place, or `undefined` for an empty line-up.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { lineupBodies, lineupFocus, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
134
|
+
*
|
|
135
|
+
* console.log(lineupFocus(placeLineup(lineupBodies(), "row"), 0.5)?.body.id);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function lineupFocus(places: readonly LineupPlace[], zoom: number): LineupPlace | undefined;
|
|
139
|
+
/**
|
|
140
|
+
* A body on the picture: its centre and radius in CSS pixels.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* import { lineupBodies, lineupProject, lineupView, placeLineup, type LineupDot } from "@johnmorrisdotca/wakusei-planets";
|
|
145
|
+
*
|
|
146
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
147
|
+
* const dots: LineupDot[] = lineupProject(places, lineupView(places, 0.2), 640, 360);
|
|
148
|
+
* console.log(dots.map((dot) => `${dot.place.body.id} ${dot.r.toFixed(1)}px`).join(", "));
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export interface LineupDot {
|
|
152
|
+
place: LineupPlace;
|
|
153
|
+
cx: number;
|
|
154
|
+
cy: number;
|
|
155
|
+
r: number;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Where each body is on a picture `width` by `height` pixels seen by `view`: the sizes are true, so a body smaller than a pixel has a radius under 1.
|
|
159
|
+
*
|
|
160
|
+
* @param places - The places.
|
|
161
|
+
* @param view - The camera.
|
|
162
|
+
* @param width - The picture's width in pixels.
|
|
163
|
+
* @param height - The picture's height in pixels.
|
|
164
|
+
* @returns A dot for each body, in the same order.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* import { lineupBodies, lineupProject, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
169
|
+
*
|
|
170
|
+
* const places = placeLineup(lineupBodies({ planets: true }), "row");
|
|
171
|
+
* console.log(lineupProject(places, lineupView(places, 0.3), 800, 450)[0]?.r);
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export declare function lineupProject(places: readonly LineupPlace[], view: LineupView, width: number, height: number): LineupDot[];
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the bodies stand: `row` side by side, left to right, or `stack` one above the next, top to bottom, smallest first.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { LINEUP_LAYOUTS, type LineupLayout } from "@johnmorrisdotca/wakusei-planets";
|
|
7
|
+
*
|
|
8
|
+
* const layout: LineupLayout = LINEUP_LAYOUTS[1];
|
|
9
|
+
* console.log(layout);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export const LINEUP_LAYOUTS = ["row", "stack"];
|
|
13
|
+
/** The gap between two neighbours, as a share of the bigger one's radius. */
|
|
14
|
+
const GAP = 0.25;
|
|
15
|
+
/** How many radii of the framed body the picture holds from the middle to the top: 1.45 leaves the body at about two thirds of the picture's height. */
|
|
16
|
+
const FRAME = 1.45;
|
|
17
|
+
/**
|
|
18
|
+
* Where each body sits. `bodies` is sorted by radius, smallest first, as `lineupBodies` gives it: the first is at the origin and each next one is a quarter of its own radius beyond the edge of the one before,
|
|
19
|
+
* to the right in a `row` and downward in a `stack`.
|
|
20
|
+
*
|
|
21
|
+
* @param bodies - The bodies, smallest first.
|
|
22
|
+
* @param layout - `row` (the default) or `stack`.
|
|
23
|
+
* @returns The places, in the same order.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { lineupBodies, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
28
|
+
*
|
|
29
|
+
* const places = placeLineup(lineupBodies({ set: "bright", planets: true }), "stack");
|
|
30
|
+
* console.log(places.map((place) => place.y.toFixed(2)).join(" "));
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function placeLineup(bodies, layout = "row") {
|
|
34
|
+
const out = [];
|
|
35
|
+
let along = 0;
|
|
36
|
+
bodies.forEach((body, index) => {
|
|
37
|
+
const r = body.radiusSun;
|
|
38
|
+
if (index > 0)
|
|
39
|
+
along += bodies[index - 1].radiusSun + GAP * r + r;
|
|
40
|
+
out.push({ body, x: layout === "row" ? along : 0, y: layout === "row" ? 0 : -along, r });
|
|
41
|
+
});
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
const SMALLEST = (places) => Math.log(places[0].r);
|
|
45
|
+
const LARGEST = (places) => Math.log(places[places.length - 1].r);
|
|
46
|
+
/**
|
|
47
|
+
* The camera at a zoom from 0 to 1: the size it frames grows from the smallest body's radius to the largest's on a logarithmic scale, and its centre glides from one body to the next, easing in and out, so it rests on each body as the framed size reaches that body's radius. A tall, narrow picture (`aspect` under 1) is zoomed out so the framed body still fits across.
|
|
48
|
+
*
|
|
49
|
+
* @param places - The places, smallest first.
|
|
50
|
+
* @param zoom - 0 (the smallest body fills the picture) to 1 (the largest does). Held to that range.
|
|
51
|
+
* @param aspect - The picture's width over its height, 1.78 by default.
|
|
52
|
+
* @returns The camera.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { lineupBodies, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
57
|
+
*
|
|
58
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
59
|
+
* console.log(lineupView(places, 0).h, lineupView(places, 1).h);
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function lineupView(places, zoom, aspect = 16 / 9) {
|
|
63
|
+
if (places.length === 0)
|
|
64
|
+
return { x: 0, y: 0, h: 1 };
|
|
65
|
+
const low = SMALLEST(places);
|
|
66
|
+
const span = LARGEST(places) - low;
|
|
67
|
+
const t = Math.min(1, Math.max(0, Number.isFinite(zoom) ? zoom : 0));
|
|
68
|
+
const size = low + span * t;
|
|
69
|
+
let i = 0;
|
|
70
|
+
while (i < places.length - 2 && Math.log(places[i + 1].r) < size)
|
|
71
|
+
i += 1;
|
|
72
|
+
const a = places[i];
|
|
73
|
+
const b = places[Math.min(places.length - 1, i + 1)];
|
|
74
|
+
const gap = Math.log(b.r) - Math.log(a.r);
|
|
75
|
+
const u = gap > 1e-9 ? Math.min(1, Math.max(0, (size - Math.log(a.r)) / gap)) : 0;
|
|
76
|
+
// the centre eases from one body to the next, so the camera rests on each body as it passes it
|
|
77
|
+
const w = u * u * (3 - 2 * u);
|
|
78
|
+
return { x: a.x + (b.x - a.x) * w, y: a.y + (b.y - a.y) * w, h: FRAME * Math.exp(size) * Math.max(1, 1 / Math.max(0.05, aspect)) };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The zoom at which a body fills the picture: where the camera frames its radius. 0 for a lone body.
|
|
82
|
+
*
|
|
83
|
+
* @param places - The places, smallest first.
|
|
84
|
+
* @param id - A body's id.
|
|
85
|
+
* @returns The zoom from 0 to 1, or `undefined` when the id is not in the line-up.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { lineupBodies, lineupZoomOf, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
90
|
+
*
|
|
91
|
+
* const places = placeLineup(lineupBodies(), "row");
|
|
92
|
+
* console.log(lineupZoomOf(places, "antares")?.toFixed(3));
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export function lineupZoomOf(places, id) {
|
|
96
|
+
const place = places.find((one) => one.body.id === id);
|
|
97
|
+
if (place === undefined)
|
|
98
|
+
return undefined;
|
|
99
|
+
const span = LARGEST(places) - SMALLEST(places);
|
|
100
|
+
return span < 1e-9 ? 0 : (Math.log(place.r) - SMALLEST(places)) / span;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The zoom at which the camera frames a size: a radius in Suns, held to the line-up's smallest and largest.
|
|
104
|
+
*
|
|
105
|
+
* @param places - The places, smallest first.
|
|
106
|
+
* @param radius - A radius in Suns.
|
|
107
|
+
* @returns The zoom from 0 to 1 (0 for an empty or lone line-up).
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* import { lineupBodies, lineupZoomFor, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
112
|
+
*
|
|
113
|
+
* console.log(lineupZoomFor(placeLineup(lineupBodies(), "row"), 80).toFixed(3));
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export function lineupZoomFor(places, radius) {
|
|
117
|
+
if (places.length === 0)
|
|
118
|
+
return 0;
|
|
119
|
+
const span = LARGEST(places) - SMALLEST(places);
|
|
120
|
+
return span < 1e-9 || !(radius > 0) ? 0 : Math.min(1, Math.max(0, (Math.log(radius) - SMALLEST(places)) / span));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The body whose size the camera frames at a zoom: the nearest in the logarithmic scale of radius.
|
|
124
|
+
*
|
|
125
|
+
* @param places - The places, smallest first.
|
|
126
|
+
* @param zoom - 0 to 1.
|
|
127
|
+
* @returns The place, or `undefined` for an empty line-up.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { lineupBodies, lineupFocus, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
132
|
+
*
|
|
133
|
+
* console.log(lineupFocus(placeLineup(lineupBodies(), "row"), 0.5)?.body.id);
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export function lineupFocus(places, zoom) {
|
|
137
|
+
if (places.length === 0)
|
|
138
|
+
return undefined;
|
|
139
|
+
const size = SMALLEST(places) + (LARGEST(places) - SMALLEST(places)) * Math.min(1, Math.max(0, zoom));
|
|
140
|
+
return places.reduce((best, one) => (Math.abs(Math.log(one.r) - size) < Math.abs(Math.log(best.r) - size) ? one : best));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Where each body is on a picture `width` by `height` pixels seen by `view`: the sizes are true, so a body smaller than a pixel has a radius under 1.
|
|
144
|
+
*
|
|
145
|
+
* @param places - The places.
|
|
146
|
+
* @param view - The camera.
|
|
147
|
+
* @param width - The picture's width in pixels.
|
|
148
|
+
* @param height - The picture's height in pixels.
|
|
149
|
+
* @returns A dot for each body, in the same order.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { lineupBodies, lineupProject, lineupView, placeLineup } from "@johnmorrisdotca/wakusei-planets";
|
|
154
|
+
*
|
|
155
|
+
* const places = placeLineup(lineupBodies({ planets: true }), "row");
|
|
156
|
+
* console.log(lineupProject(places, lineupView(places, 0.3), 800, 450)[0]?.r);
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export function lineupProject(places, view, width, height) {
|
|
160
|
+
const scale = height / (2 * view.h);
|
|
161
|
+
return places.map((place) => ({ place, cx: width / 2 + (place.x - view.x) * scale, cy: height / 2 - (place.y - view.y) * scale, r: place.r * scale }));
|
|
162
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Item3D, Scene3D } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
2
|
+
import { type ThreeParts } from "../scene/body.ts";
|
|
3
|
+
import type { StarLook } from "../star.ts";
|
|
4
|
+
import "../bodies/earth.ts";
|
|
5
|
+
import "../bodies/jupiter.ts";
|
|
6
|
+
import type { LineupBody } from "./data.ts";
|
|
7
|
+
import type { LineupPlace, LineupView } from "./layout.ts";
|
|
8
|
+
/** How a body is turned by hand: yaw about its pole, pitch toward the viewer, both in degrees. */
|
|
9
|
+
export interface LineupHand {
|
|
10
|
+
yaw: number;
|
|
11
|
+
pitch: number;
|
|
12
|
+
}
|
|
13
|
+
/** Everything that decides how the scene looks at one draw. */
|
|
14
|
+
export interface LineupDraw {
|
|
15
|
+
places: readonly LineupPlace[];
|
|
16
|
+
view: LineupView;
|
|
17
|
+
/** What a hand has turned each body by. */
|
|
18
|
+
hands: ReadonlyMap<string, LineupHand>;
|
|
19
|
+
/** The bodies turn by themselves. */
|
|
20
|
+
spin: boolean;
|
|
21
|
+
/** The body a hand holds: it does not turn by itself while it is held. */
|
|
22
|
+
holding?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface LineupScene extends Scene3D {
|
|
25
|
+
apply(state: LineupDraw): void;
|
|
26
|
+
/** Call `fn` right after the next draw, in the same task, while the canvas still holds the picture (it does not keep it after). */
|
|
27
|
+
afterDraw(fn: () => void): void;
|
|
28
|
+
/** The ids of the bodies drawn at the last draw: in the picture and at least a fifth of a pixel across. */
|
|
29
|
+
readonly drawn: readonly string[];
|
|
30
|
+
}
|
|
31
|
+
export interface LineupItemOptions {
|
|
32
|
+
/** A key that changes whenever the picture would: the renderer keeps one still for each key. */
|
|
33
|
+
key: string;
|
|
34
|
+
bodies: readonly LineupBody[];
|
|
35
|
+
/** The star look of each star, by id. */
|
|
36
|
+
stars: ReadonlyMap<string, StarLook>;
|
|
37
|
+
/** The planets' look: `realistic` or `painted`. */
|
|
38
|
+
look: "realistic" | "painted";
|
|
39
|
+
state(): LineupDraw;
|
|
40
|
+
textureBase(): string;
|
|
41
|
+
farBase(): string;
|
|
42
|
+
redraw(): void;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The item the shared renderer shows for a line-up: its `build` makes the scene, whose `apply` is handed each state.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createLineupItem(options: LineupItemOptions): Item3D<ThreeParts>;
|