@johnmorrisdotca/wakusei-planets 0.24.0 → 0.26.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +36 -8
  3. package/dist/blackbody.d.ts +2 -0
  4. package/dist/blackbody.js +10 -0
  5. package/dist/element.d.ts +8 -0
  6. package/dist/element.js +102 -16
  7. package/dist/embed.d.ts +3 -0
  8. package/dist/embed.js +30 -8
  9. package/dist/index.d.ts +2 -0
  10. package/dist/index.js +2 -0
  11. package/dist/kids/data.d.ts +109 -0
  12. package/dist/kids/data.js +164 -0
  13. package/dist/kids/index.d.ts +4 -0
  14. package/dist/kids/index.js +5 -0
  15. package/dist/kids/land.d.ts +3 -0
  16. package/dist/kids/land.js +29 -0
  17. package/dist/kids/picture.d.ts +39 -0
  18. package/dist/kids/picture.js +255 -0
  19. package/dist/kids/sheet.d.ts +60 -0
  20. package/dist/kids/sheet.js +79 -0
  21. package/dist/kids/svg.d.ts +46 -0
  22. package/dist/kids/svg.js +193 -0
  23. package/dist/line.d.ts +9 -1
  24. package/dist/line.js +3 -4
  25. package/dist/scene/body.d.ts +8 -1
  26. package/dist/scene/body.js +33 -9
  27. package/dist/scene/glsl.d.ts +7 -2
  28. package/dist/scene/glsl.js +49 -3
  29. package/dist/scene/look.d.ts +43 -0
  30. package/dist/scene/look.js +39 -1
  31. package/dist/scene/painter.js +2 -0
  32. package/dist/scene/store.d.ts +3 -0
  33. package/dist/scene/store.js +3 -1
  34. package/dist/scene/textures.d.ts +2 -2
  35. package/dist/space/element.d.ts +4 -1
  36. package/dist/space/element.js +17 -14
  37. package/dist/space/frame.d.ts +3 -3
  38. package/dist/space/frame.js +3 -3
  39. package/dist/space/index.d.ts +1 -1
  40. package/dist/space/index.js +1 -1
  41. package/dist/space/scale.d.ts +60 -7
  42. package/dist/space/scale.js +105 -9
  43. package/dist/space/scene.d.ts +7 -1
  44. package/dist/space/scene.js +36 -27
  45. package/dist/star-line.d.ts +20 -0
  46. package/dist/star-line.js +59 -0
  47. package/dist/star.d.ts +193 -0
  48. package/dist/star.js +261 -0
  49. package/dist/stars.js +3 -5
  50. package/dist/version.d.ts +1 -1
  51. package/dist/version.js +1 -1
  52. package/package.json +8 -2
@@ -1,10 +1,12 @@
1
1
  // The two scales the space view can show. The true one is the Earth's radius for a unit and the real distances in it; the compressed one keeps every direction but squeezes the
2
2
  // distances so that the Moon, the Sun and Neptune fit one view. Pure.
3
- import { BODIES } from "../facts.js";
3
+ import { BODIES, MOONS_OF } from "../facts.js";
4
+ import { meanSunAu } from "../ephemeris.js";
5
+ import { moonOffset } from "../scene/orient.js";
4
6
  import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM } from "./frame.js";
5
7
  const MOON_AU = 0.00257;
6
8
  /**
7
- * The Moon's distance in a compressed view, in Earth radii (the true one is about 60).
9
+ * The Moon's distance in a compressed view, in Earth radii (the true one is about 60). It is well inside the gap between the Earth's orbit and the next one in.
8
10
  *
9
11
  * @example
10
12
  * ```ts
@@ -13,9 +15,9 @@ const MOON_AU = 0.00257;
13
15
  * console.log(MOON_COMPRESSED);
14
16
  * ```
15
17
  */
16
- export const MOON_COMPRESSED = 10;
18
+ export const MOON_COMPRESSED = 3.5;
17
19
  /**
18
- * The Sun's distance from the Earth in a compressed view, in Earth radii (the true one is about 23,500). One astronomical unit is this long there.
20
+ * One astronomical unit in a compressed view, in Earth radii (the true one is about 23,500): the Sun is this far from the Earth, and each planet's distance from the Sun is this times its distance in astronomical units to the power 0.45.
19
21
  *
20
22
  * @example
21
23
  * ```ts
@@ -24,7 +26,8 @@ export const MOON_COMPRESSED = 10;
24
26
  * console.log(SUN_COMPRESSED);
25
27
  * ```
26
28
  */
27
- export const SUN_COMPRESSED = 44;
29
+ export const SUN_COMPRESSED = 70;
30
+ const SPREAD = 0.45;
28
31
  const length = (v) => Math.hypot(v[0], v[1], v[2]);
29
32
  const scaled = (v, k) => [v[0] * k, v[1] * k, v[2] * k];
30
33
  /**
@@ -74,7 +77,7 @@ export function compress(v) {
74
77
  */
75
78
  export const placeMoon = (mode, geo) => (mode === "true" ? truePosition(geo) : compress(geo));
76
79
  /**
77
- * The Sun, from the Earth's centre: in a compressed view always 44 Earth radii away, in its real direction.
80
+ * The Sun, from the Earth's centre. In a compressed view it is in its real direction at the distance the Earth's orbit has from the Sun there, so the Earth lies on its own orbit.
78
81
  *
79
82
  * @param mode - The scale.
80
83
  * @param geo - The Sun's place in astronomical units from the Earth's centre.
@@ -91,11 +94,11 @@ export function placeSun(mode, geo) {
91
94
  if (mode === "true")
92
95
  return truePosition(geo);
93
96
  const l = length(geo);
94
- return l === 0 ? [0, 0, 0] : scaled(geo, SUN_COMPRESSED / l);
97
+ return l === 0 ? [0, 0, 0] : scaled(geo, (SUN_COMPRESSED * l ** SPREAD) / l);
95
98
  }
96
99
  /**
97
100
  * A place `helio` astronomical units from the Sun (a planet, or a point on its orbit), round the Sun at `sun`. True: as it is. Compressed: the same direction from
98
- * the Sun, at 44 Earth radii times its distance in astronomical units to the power 0.45, so the Earth's own orbit is the 44 and Neptune's is about 200 (30 times as far, really).
101
+ * the Sun, at `SUN_COMPRESSED` Earth radii times its distance in astronomical units to the power 0.45, so Neptune's orbit is about 4.6 times the Earth's (30 times as far, really).
99
102
  *
100
103
  * @param mode - The scale.
101
104
  * @param sun - Where the Sun is drawn, in Earth radii.
@@ -115,7 +118,7 @@ export function placeAround(mode, sun, helio) {
115
118
  const l = length(helio);
116
119
  if (l === 0)
117
120
  return sun;
118
- const d = (SUN_COMPRESSED * l ** 0.45) / l;
121
+ const d = (SUN_COMPRESSED * l ** SPREAD) / l;
119
122
  return [sun[0] + helio[0] * d, sun[1] + helio[1] * d, sun[2] + helio[2] * d];
120
123
  }
121
124
  /** The sizes a compressed view gives bodies large enough to see, in Earth radii. */
@@ -137,3 +140,96 @@ const COMPRESSED_RADIUS = { sun: 5, earth: 1, moon: 0.9, mercury: 0.5, venus: 0.
137
140
  export function radiusIn(id, mode) {
138
141
  return mode === "true" ? BODIES[id].radiusKm / EARTH_RADIUS_KM : COMPRESSED_RADIUS[id];
139
142
  }
143
+ /** The planets, nearest the Sun first, whose orbits a moon's orbit must not reach across in a compressed view. */
144
+ const ORBIT_ROW = ["mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"];
145
+ const compressedOrbit = (au) => SUN_COMPRESSED * au ** SPREAD;
146
+ /** The room a planet's moons have round it in a compressed view, in Earth radii: well inside the gap to the nearer of the orbits next to its own. */
147
+ function room(host) {
148
+ const at = ORBIT_ROW.indexOf(host);
149
+ if (at < 0)
150
+ return Number.POSITIVE_INFINITY;
151
+ const mine = compressedOrbit(meanSunAu(host));
152
+ const gaps = [ORBIT_ROW[at - 1], ORBIT_ROW[at + 1]].filter((id) => id !== undefined).map((id) => Math.abs(compressedOrbit(meanSunAu(id)) - mine));
153
+ return 0.35 * Math.min(...gaps);
154
+ }
155
+ const reaches = new Map();
156
+ /** How far out a body's moons go, in its radii: the farthest any of them is found in a year of looks. */
157
+ function reachOf(host) {
158
+ let reach = reaches.get(host);
159
+ if (reach === undefined) {
160
+ reach = 0;
161
+ const start = Date.UTC(2026, 0, 1);
162
+ for (const moon of MOONS_OF[host] ?? [])
163
+ for (let i = 0; i < 24; i += 1)
164
+ reach = Math.max(reach, length(moonOffset(moon, start + i * 15 * 86_400_000)));
165
+ reaches.set(host, reach);
166
+ }
167
+ return reach;
168
+ }
169
+ /**
170
+ * How far from its planet a moon is drawn, in the planet's radii, when it is really `distance` radii away. True: as it is. Compressed: the same, until the farthest of the planet's moons would
171
+ * reach across the gap to the next planet's orbit; then every moon is drawn nearer, the farthest at about a third of the gap and the nearer ones in the order they are, so no moon's orbit crosses another planet's.
172
+ *
173
+ * @param mode - The scale.
174
+ * @param host - The planet or dwarf planet the moon goes round.
175
+ * @param distance - The moon's distance from the centre of the planet, in the planet's radii.
176
+ * @returns The distance to draw it at, in the planet's radii.
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * import { moonDistance } from "@johnmorrisdotca/wakusei-planets/space";
181
+ *
182
+ * console.log(moonDistance("compressed", "neptune", 222).toFixed(1));
183
+ * ```
184
+ */
185
+ export function moonDistance(mode, host, distance) {
186
+ if (mode === "true")
187
+ return distance;
188
+ const reach = reachOf(host);
189
+ const radius = radiusIn(host, "compressed");
190
+ const space = room(host);
191
+ if (reach === 0 || reach * radius <= space)
192
+ return distance;
193
+ return Math.min(distance, (space * Math.sqrt(distance / reach)) / radius);
194
+ }
195
+ /**
196
+ * Where every body of the view is, in Earth radii from the Earth's centre, at one moment: the Earth at the centre, the Moon, the Sun and the planets. The same mapping places
197
+ * the bodies and the lines of their orbits (`orbitLine`), so a body is always on its own orbit.
198
+ *
199
+ * @param mode - The scale.
200
+ * @param frame - Where everything is, from `frameAt`.
201
+ * @returns The place of each body.
202
+ *
203
+ * @example
204
+ * ```ts
205
+ * import { frameAt, placeBodies } from "@johnmorrisdotca/wakusei-planets/space";
206
+ *
207
+ * const places = placeBodies("compressed", frameAt(Date.UTC(2026, 10, 2)));
208
+ * console.log(Object.keys(places).length);
209
+ * ```
210
+ */
211
+ export function placeBodies(mode, frame) {
212
+ const sun = placeSun(mode, frame.sun);
213
+ const all = { earth: [0, 0, 0], moon: placeMoon(mode, frame.moon), sun };
214
+ const helio = (geo) => [geo[0] - frame.sun[0], geo[1] - frame.sun[1], geo[2] - frame.sun[2]];
215
+ for (const [id, geo] of Object.entries(frame.planets))
216
+ all[id] = placeAround(mode, sun, helio(geo));
217
+ return all;
218
+ }
219
+ /**
220
+ * The line of a body's orbit as the view draws it: the points from `planetOrbit` (astronomical units from the Sun) placed round the Sun at `sun` by the same mapping as the bodies.
221
+ *
222
+ * @param mode - The scale.
223
+ * @param sun - Where the Sun is drawn, from `placeSun` or `placeBodies`.
224
+ * @param points - The orbit, from `planetOrbit`.
225
+ * @returns The same points, in Earth radii from the Earth's centre.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * import { frameAt, orbitLine, placeSun, planetOrbit } from "@johnmorrisdotca/wakusei-planets/space";
230
+ *
231
+ * const ms = Date.UTC(2026, 10, 2);
232
+ * console.log(orbitLine("compressed", placeSun("compressed", frameAt(ms).sun), planetOrbit("earth", ms, 24)).length);
233
+ * ```
234
+ */
235
+ export const orbitLine = (mode, sun, points) => points.map((p) => placeAround(mode, sun, p));
@@ -35,7 +35,7 @@ export declare const SPACE_BODIES: readonly ["earth", "sun", "moon", "mercury",
35
35
  */
36
36
  export type SpaceBody = (typeof SPACE_BODIES)[number];
37
37
  /**
38
- * How the Earth is lit: `sun` is the true light; `lit` is daylight from the camera's side, so a view of the night side is never black.
38
+ * How the bodies are lit: `sun` is the true light; `lit` is daylight from the camera's side on every body, so a view of a night side is never black.
39
39
  *
40
40
  * @example
41
41
  * ```ts no-run
@@ -103,6 +103,9 @@ export interface SpaceModel {
103
103
  yaw: number;
104
104
  tilt: number;
105
105
  zoom: number;
106
+ roll: number;
107
+ panX: number;
108
+ panY: number;
106
109
  };
107
110
  /** The home place: the camera starts above it. */
108
111
  centre: {
@@ -141,6 +144,9 @@ export interface SpaceModel {
141
144
  yaw: number;
142
145
  tilt: number;
143
146
  zoom: number;
147
+ roll: number;
148
+ panX: number;
149
+ panY: number;
144
150
  } | null;
145
151
  heightPx: number;
146
152
  camera: {
@@ -5,12 +5,13 @@ import "../bodies/all.js";
5
5
  import { groundDirection } from "./aim.js";
6
6
  import { createTrack, planetOrbit, SPACE_PLANETS } from "./frame.js";
7
7
  import { ridePose } from "./ride.js";
8
- import { placeAround, placeMoon, placeSun, radiusIn } from "./scale.js";
8
+ import { moonDistance, orbitLine, placeBodies, radiusIn } from "./scale.js";
9
9
  const RAD = Math.PI / 180;
10
10
  /** The camera's distance, in radii of the body it looks at, at which that body fills the view at zoom 1. */
11
11
  export const FIT_DISTANCE = 3.4;
12
12
  const FOV = 35;
13
13
  const ORBIT_POINTS = 96;
14
+ const ORBIT_BODIES = ["earth", ...SPACE_PLANETS];
14
15
  /** The moons of a planet are shown once the planet is this many CSS pixels across its radius, and each is drawn at least this many pixels across its radius. */
15
16
  const MOON_MIN_PLANET_PX = 4;
16
17
  const MOON_MIN_PX = 2.2;
@@ -30,7 +31,7 @@ export const SPACE_BODIES = ["earth", "sun", "moon", ...SPACE_PLANETS];
30
31
  export const newSpaceModel = () => ({
31
32
  time: Date.now(),
32
33
  mode: "compressed",
33
- view: { yaw: 0, tilt: 0, zoom: 1 },
34
+ view: { yaw: 0, tilt: 0, zoom: 1, roll: 0, panX: 0, panY: 0 },
34
35
  centre: { lon: 0, lat: 0 },
35
36
  anchor: null,
36
37
  orbits: true,
@@ -107,11 +108,11 @@ export function createSpaceItem(model, deps) {
107
108
  return slot;
108
109
  };
109
110
  const orbits = new Map();
110
- for (const id of SPACE_PLANETS) {
111
+ for (const id of ORBIT_BODIES) {
111
112
  const positions = new Float32Array((ORBIT_POINTS + 1) * 3);
112
113
  const geometry = new three.BufferGeometry();
113
114
  geometry.setAttribute("position", new three.BufferAttribute(positions, 3));
114
- const line = new three.Line(geometry, new three.LineBasicMaterial({ color: BODIES[id].colour, transparent: true, opacity: 0.55 }));
115
+ const line = new three.Line(geometry, new three.LineBasicMaterial({ color: id === "earth" ? "#6aa8ff" : BODIES[id].colour, transparent: true, opacity: 0.55 }));
115
116
  line.frustumCulled = false;
116
117
  scene.add(line);
117
118
  orbits.set(id, { line, positions, samples: [], epoch: Number.NaN });
@@ -135,17 +136,16 @@ export function createSpaceItem(model, deps) {
135
136
  track.setStep(model.stepMs);
136
137
  }
137
138
  const mode = model.mode;
138
- const sunPos = placeSun(mode, frame.sun);
139
- const pos = { earth: [0, 0, 0], moon: placeMoon(mode, frame.moon), sun: sunPos };
140
- for (const id of SPACE_PLANETS)
141
- pos[id] = placeAround(mode, sunPos, minus(frame.planets[id], frame.sun));
139
+ const pos = placeBodies(mode, frame);
140
+ const sunPos = pos.sun;
142
141
  const radiusOf = (id) => radiusIn(id, mode);
143
- const placeAll = (at) => {
144
- const sunAt = placeSun(mode, at.sun);
145
- const all = { earth: [0, 0, 0], moon: placeMoon(mode, at.moon), sun: sunAt };
146
- for (const id of SPACE_PLANETS)
147
- all[id] = placeAround(mode, sunAt, minus(at.planets[id], at.sun));
148
- return all;
142
+ const placeAll = (at) => placeBodies(mode, at);
143
+ // where a moon is round its planet, as the view draws it: in the planet's radii, drawn nearer in a compressed view where the real distance would cross the next planet's orbit
144
+ const moonSite = (id, at, host) => {
145
+ const real = moonOffset(id, at);
146
+ const d = Math.hypot(real[0], real[1], real[2]);
147
+ const k = d === 0 ? 1 : moonDistance(mode, host, d) / d;
148
+ return [real[0] * k, real[1] * k, real[2] * k];
149
149
  };
150
150
  const toSunOf = (id) => unit(minus(frame.sun, id === "earth" ? [0, 0, 0] : id === "moon" ? frame.moon : id === "sun" ? [0, 0, 0] : frame.planets[id]));
151
151
  // riding along: the camera sits at a moving point and looks at the body it goes round (a moon over its planet, a point over any body). The point's place and way of going are in radii of
@@ -187,8 +187,8 @@ export function createSpaceItem(model, deps) {
187
187
  }
188
188
  else if (typeof t === "string") {
189
189
  const id = t;
190
- const offset = moonOffset(id, model.time);
191
- const later = moonOffset(id, model.time + 1000);
190
+ const offset = moonSite(id, model.time, around);
191
+ const later = moonSite(id, model.time + 1000, around);
192
192
  if (ride.view === "follow") {
193
193
  focusPos = [centre[0] + offset[0] * unitWorld, centre[1] + offset[1] * unitWorld, centre[2] + offset[2] * unitWorld];
194
194
  focusRadius = Math.max(1e-6, (BODIES[id].radiusKm / BODIES[around].radiusKm) * unitWorld);
@@ -265,15 +265,22 @@ export function createSpaceItem(model, deps) {
265
265
  if (k >= 1)
266
266
  model.transition = null;
267
267
  }
268
+ // the controls' roll turns the picture clockwise about the line of sight, and a pan moves everything by that many CSS pixels at any depth: the window of the projection moves the other way
269
+ if (model.view.roll !== 0)
270
+ camera.rotateZ(model.view.roll * RAD);
268
271
  camera.fov = fovNow;
269
272
  camera.aspect = size.width / Math.max(1, size.height);
273
+ if (model.view.panX !== 0 || model.view.panY !== 0)
274
+ camera.setViewOffset(size.width, size.height, -model.view.panX, -model.view.panY, size.width, size.height);
275
+ else
276
+ camera.clearViewOffset();
270
277
  camera.updateProjectionMatrix();
271
278
  camera.updateMatrixWorld();
272
279
  const hiddenBody = rideFrom?.hiddenBody ?? null;
273
280
  const hiddenMoon = rideFrom?.hiddenMoon ?? null;
274
281
  model.camera = { position: [camera.position.x, camera.position.y, camera.position.z], fov: fovNow };
275
282
  model.pose = { p: [camera.position.x, camera.position.y, camera.position.z], q: [camera.quaternion.x, camera.quaternion.y, camera.quaternion.z, camera.quaternion.w], fov: fovNow, valid: true };
276
- model.drawnView = { yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom };
283
+ model.drawnView = { yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom, roll: model.view.roll, panX: model.view.panX, panY: model.view.panY };
277
284
  model.heightPx = size.height;
278
285
  // how many pixels across each body is: for the pictures it needs and for what is chosen
279
286
  const half = Math.tan((fovNow / 2) * RAD);
@@ -304,13 +311,13 @@ export function createSpaceItem(model, deps) {
304
311
  drawing.object.scale.setScalar(radiusOf(id));
305
312
  if (drawing.billboard)
306
313
  drawing.object.quaternion.copy(camera.quaternion);
307
- const lit = id === "earth" && model.light === "lit";
308
- const toSun = lit ? unit([camera.position.x, camera.position.y, camera.position.z]) : toSunOf(id);
314
+ const lit = id !== "sun" && model.light === "lit";
315
+ const toSun = lit ? unit([camera.position.x - pos[id][0], camera.position.y - pos[id][1], camera.position.z - pos[id][2]]) : toSunOf(id);
309
316
  drawing.update({ toSun, ms: model.time, radiusPx: px[id] });
310
317
  }
311
318
  model.painted.bodies = paintedBodies;
312
- // the planets' orbits round the Sun
313
- for (const id of SPACE_PLANETS) {
319
+ // the orbits round the Sun, placed by the same mapping as the bodies
320
+ for (const id of ORBIT_BODIES) {
314
321
  const o = orbits.get(id);
315
322
  o.line.visible = model.orbits;
316
323
  if (!model.orbits)
@@ -319,11 +326,11 @@ export function createSpaceItem(model, deps) {
319
326
  o.samples = planetOrbit(id, model.time, ORBIT_POINTS);
320
327
  o.epoch = model.time;
321
328
  }
329
+ const line = orbitLine(mode, sunPos, o.samples);
322
330
  for (let i = 0; i <= ORBIT_POINTS; i += 1) {
323
- const p = placeAround(mode, sunPos, o.samples[i]);
324
- o.positions[i * 3] = p[0];
325
- o.positions[i * 3 + 1] = p[1];
326
- o.positions[i * 3 + 2] = p[2];
331
+ o.positions[i * 3] = line[i][0];
332
+ o.positions[i * 3 + 1] = line[i][1];
333
+ o.positions[i * 3 + 2] = line[i][2];
327
334
  }
328
335
  o.line.geometry.getAttribute("position").needsUpdate = true;
329
336
  }
@@ -336,7 +343,9 @@ export function createSpaceItem(model, deps) {
336
343
  continue;
337
344
  const radius = radiusOf(id);
338
345
  for (const site of deps.painter().moons(id, model.time)) {
339
- const at = [pos[id][0] + site.offset[0] * radius, pos[id][1] + site.offset[1] * radius, pos[id][2] + site.offset[2] * radius];
346
+ const reach = Math.hypot(site.offset[0], site.offset[1], site.offset[2]);
347
+ const k = reach === 0 ? 1 : moonDistance(mode, id, reach) / reach;
348
+ const at = [pos[id][0] + site.offset[0] * k * radius, pos[id][1] + site.offset[1] * k * radius, pos[id][2] + site.offset[2] * k * radius];
340
349
  projected.set(at[0], at[1], at[2]).project(camera);
341
350
  if (projected.z > 1 || Math.abs(projected.x) > 1.25 || Math.abs(projected.y) > 1.25)
342
351
  continue;
@@ -358,7 +367,7 @@ export function createSpaceItem(model, deps) {
358
367
  drawing.object.position.set(at[0], at[1], at[2]);
359
368
  drawing.object.scale.setScalar(drawn);
360
369
  const moonPx = away <= drawn * 1.001 ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
361
- drawing.update({ toSun: toSunOf(id), ms: model.time, radiusPx: moonPx });
370
+ drawing.update({ toSun: model.light === "lit" ? unit([camera.position.x - at[0], camera.position.y - at[1], camera.position.z - at[2]]) : toSunOf(id), ms: model.time, radiusPx: moonPx });
362
371
  }
363
372
  }
364
373
  let soonest = Number.POSITIVE_INFINITY;
@@ -0,0 +1,20 @@
1
+ import { type LineOptions } from "./line.ts";
2
+ import { type StarLook, type StarNumbers } from "./star.ts";
3
+ /**
4
+ * A star as a line drawing, as an SVG string: its outline (squeezed when it is oblate), its cells and spots as outlines, a ring of rays, and, drawn round it, the Sun's size and the orbits of the Earth and Jupiter
5
+ * wherever they fall in the picture, so the size can be read against what we know. A caption under it gives the radius, the temperature and the class. It takes the options of `lineSvg` (the size, `pose`, `fill`, `zoom`,
6
+ * `pan`, `lang` and the colours); a picture of a star with no `pose` is a portrait of the Sun's axis.
7
+ *
8
+ * @param star - The star's numbers, or a star already made with `starLook`.
9
+ * @param options - The drawing's size, pose and colours.
10
+ * @returns The SVG markup, `<svg ...>` to `</svg>`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { starLineSvg } from "@johnmorrisdotca/wakusei-planets";
15
+ *
16
+ * const svg = starLineSvg({ name: "Betelgeuse", temperatureK: 3600, radiusSun: 760, luminosityClass: "Ia" }, { size: 320 });
17
+ * console.log(svg.startsWith("<svg"), svg.includes("Earth's orbit"));
18
+ * ```
19
+ */
20
+ export declare function starLineSvg(star: StarNumbers | StarLook, options?: LineOptions): string;
@@ -0,0 +1,59 @@
1
+ // A star as a line drawing: the Sun's outline drawing with this star's size, shape, cells and spots, and the Sun and two orbits drawn round it, so the size can be read against what we know. Pure: one SVG string.
2
+ import { portrait } from "./pose.js";
3
+ import { bodyGroup, mix, NAVY } from "./line.js";
4
+ import { starLook } from "./star.js";
5
+ /** What a star's size is drawn beside: the Sun, the Earth's orbit and Jupiter's orbit, in solar radii (one astronomical unit is 215.03). */
6
+ const RINGS = [
7
+ { ratio: 1, en: "Sun", ja: "太陽" },
8
+ { ratio: 215.03, en: "Earth's orbit", ja: "地球の軌道" },
9
+ { ratio: 1118, en: "Jupiter's orbit", ja: "木星の軌道" },
10
+ ];
11
+ const n = (v) => (Math.round(v * 100) / 100).toString();
12
+ const escape = (text) => text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
13
+ /**
14
+ * A star as a line drawing, as an SVG string: its outline (squeezed when it is oblate), its cells and spots as outlines, a ring of rays, and, drawn round it, the Sun's size and the orbits of the Earth and Jupiter
15
+ * wherever they fall in the picture, so the size can be read against what we know. A caption under it gives the radius, the temperature and the class. It takes the options of `lineSvg` (the size, `pose`, `fill`, `zoom`,
16
+ * `pan`, `lang` and the colours); a picture of a star with no `pose` is a portrait of the Sun's axis.
17
+ *
18
+ * @param star - The star's numbers, or a star already made with `starLook`.
19
+ * @param options - The drawing's size, pose and colours.
20
+ * @returns The SVG markup, `<svg ...>` to `</svg>`.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { starLineSvg } from "@johnmorrisdotca/wakusei-planets";
25
+ *
26
+ * const svg = starLineSvg({ name: "Betelgeuse", temperatureK: 3600, radiusSun: 760, luminosityClass: "Ia" }, { size: 320 });
27
+ * console.log(svg.startsWith("<svg"), svg.includes("Earth's orbit"));
28
+ * ```
29
+ */
30
+ export function starLineSvg(star, options = {}) {
31
+ const made = "look" in star ? star : starLook(star);
32
+ const size = options.size ?? 320;
33
+ const lang = options.lang ?? "en";
34
+ const stroke = options.stroke ?? mix(made.tone, "#ffffff", 0.3);
35
+ const paper = options.paper ?? mix(made.tone, NAVY, 0.7);
36
+ const hair = Math.max(0.6, size / 320);
37
+ const pose = options.pose ?? portrait("sun", options);
38
+ const fit = options.fill === undefined ? 0.94 / 1.08 : options.fill;
39
+ const zoom = options.zoom ?? 1;
40
+ const frame = { cx: (size / 2) * (1 - (options.panX ?? 0)), cy: (size / 2) * (1 + (options.panY ?? 0)), r: (size / 2) * fit * zoom };
41
+ const label = lang === "ja" ? `${made.facts.name.ja}の線画 (数字から描いた絵)` : `Line drawing of ${made.facts.name.en}, drawn from its numbers`;
42
+ const rings = [];
43
+ for (const ring of RINGS) {
44
+ const r = (ring.ratio / made.radiusSun) * frame.r;
45
+ // the star's own edge is drawn already; a ring too small to see or far outside the picture is left out
46
+ if (Math.abs(ring.ratio / made.radiusSun - 1) < 0.04 || r < 3 || r > size * 1.5)
47
+ continue;
48
+ const text = lang === "ja" ? ring.ja : ring.en;
49
+ const x = frame.cx + r * Math.SQRT1_2;
50
+ const y = frame.cy + r * Math.SQRT1_2;
51
+ rings.push(`<circle cx="${n(frame.cx)}" cy="${n(frame.cy)}" r="${n(r)}" fill="none" stroke="${stroke}" stroke-opacity="0.5" stroke-width="${n(hair)}" stroke-dasharray="${n(hair * 4)} ${n(hair * 4)}"/>` +
52
+ (x < size - 4 && y < size - 14 ? `<text x="${n(x)}" y="${n(y)}" font-size="${n(size / 38)}" fill="${stroke}" fill-opacity="0.85" font-family="system-ui,sans-serif">${escape(text)}</text>` : ""));
53
+ }
54
+ const radius = made.radiusSun >= 10 ? Math.round(made.radiusSun).toLocaleString("en-US") : String(Number(made.radiusSun.toPrecision(3)));
55
+ const caption = lang === "ja" ? `太陽の${radius}倍 · ${Math.round(made.temperatureK).toLocaleString("en-US")} K · ${made.luminosityClass}` : `${radius} × Sun · ${Math.round(made.temperatureK).toLocaleString("en-US")} K · ${made.luminosityClass}`;
56
+ const text = `<text x="${n(size / 2)}" y="${n(size - size / 40)}" text-anchor="middle" font-size="${n(size / 34)}" fill="${stroke}" fill-opacity="0.85" font-family="system-ui,sans-serif">${escape(caption)}</text>`;
57
+ const group = bodyGroup("sun", pose, frame, { stroke, paper }, hair, 1, made.facts, `wk-star-${[...made.key].reduce((h, c) => (h * 31 + c.charCodeAt(0)) >>> 0, 7).toString(36)}`);
58
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${n(size)} ${n(size)}" width="${n(size)}" height="${n(size)}" role="img" aria-label="${escape(label)}"><title>${escape(label)}</title>${rings.join("")}${group}${text}</svg>`;
59
+ }
package/dist/star.d.ts ADDED
@@ -0,0 +1,193 @@
1
+ import { type BodyFacts } from "./facts.ts";
2
+ import type { BodyLook } from "./scene/look.ts";
3
+ /**
4
+ * A star's luminosity class, as the spectral type writes it: `0` a hypergiant, `Ia`, `Iab` and `Ib` supergiants, `II` a bright giant, `III` a giant, `IV` a subgiant, `V` a dwarf (the Sun is G2 V), `VI` a subdwarf and `D` a white dwarf.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { LUMINOSITY_CLASSES, type LuminosityClass } from "@johnmorrisdotca/wakusei-planets";
9
+ *
10
+ * const supergiant: LuminosityClass = LUMINOSITY_CLASSES[1];
11
+ * console.log(supergiant, LUMINOSITY_CLASSES.length);
12
+ * ```
13
+ */
14
+ export declare const LUMINOSITY_CLASSES: readonly ["0", "Ia", "Iab", "Ib", "II", "III", "IV", "V", "VI", "D"];
15
+ /**
16
+ * One of the classes in `LUMINOSITY_CLASSES`.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import type { LuminosityClass } from "@johnmorrisdotca/wakusei-planets";
21
+ *
22
+ * const dwarf: LuminosityClass = "V";
23
+ * console.log(dwarf);
24
+ * ```
25
+ */
26
+ export type LuminosityClass = (typeof LUMINOSITY_CLASSES)[number];
27
+ /**
28
+ * The numbers that make a star's picture in `StarNumbers`, by name: the ones a page may mark as `estimated`.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import type { StarNumberName } from "@johnmorrisdotca/wakusei-planets";
33
+ *
34
+ * const name: StarNumberName = "radiusSun";
35
+ * console.log(name);
36
+ * ```
37
+ */
38
+ export type StarNumberName = "temperatureK" | "radiusSun" | "luminosityClass" | "rotationDays" | "oblateness";
39
+ /**
40
+ * The numbers a star is drawn from. Only the temperature and the radius are needed; the rest has a stated default.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * import { starLook, type StarNumbers } from "@johnmorrisdotca/wakusei-planets";
45
+ *
46
+ * const betelgeuse: StarNumbers = { name: "Betelgeuse", temperatureK: 3600, radiusSun: 760, luminosityClass: "Ia", rotationDays: 2100 };
47
+ * console.log(starLook(betelgeuse).colour);
48
+ * ```
49
+ */
50
+ export interface StarNumbers {
51
+ /** What to call it: the picture's label. Default `Star`. */
52
+ name?: string;
53
+ /** The effective surface temperature in kelvin, 1,000 to 200,000. It sets the colour. */
54
+ temperatureK: number;
55
+ /** The radius in solar radii (the Sun is 1), above 0 and up to 5,000. It sets the size beside the Sun and the size of the rings drawn round the star. */
56
+ radiusSun: number;
57
+ /** The luminosity class: it sets how coarse the surface is (fine cells on a dwarf, a few giant cells on a supergiant, none on a white dwarf). Default `V`. */
58
+ luminosityClass?: LuminosityClass;
59
+ /** Days for one turn at the equator, negative when it turns backwards. Left out, a slow turn is drawn and the picture says the rotation is not given. */
60
+ rotationDays?: number;
61
+ /** How spotted it is, 0 to 1. Left out it follows the temperature and the class: none on hot stars, many on cool dwarfs and red dwarfs, few and large on giants. The spots are illustrative either way. */
62
+ activity?: number;
63
+ /** (equatorial radius - polar radius) / equatorial radius, 0 to 0.4. A fast spinner such as Altair is about 0.2. The star is drawn squashed, with the poles hotter than the equator (gravity darkening). Default 0. */
64
+ oblateness?: number;
65
+ /** The strength of the soft glow round the star, 0 to 1. Default 0.8. */
66
+ corona?: number;
67
+ /** The numbers above that are estimates, not measurements (a radius worked out from brightness, say). The picture says so. */
68
+ estimated?: readonly StarNumberName[];
69
+ }
70
+ /**
71
+ * How well a part of a star's picture is known: `measured` is a number the page gave, `estimated` one it marked as an estimate, `derived` is worked out from those numbers by a stated rule, and `illustrative` is invented to look right.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * import type { StarStatus } from "@johnmorrisdotca/wakusei-planets";
76
+ *
77
+ * const status: StarStatus = "illustrative";
78
+ * console.log(status);
79
+ * ```
80
+ */
81
+ export type StarStatus = "measured" | "estimated" | "derived" | "illustrative";
82
+ /**
83
+ * One part of a star's picture and how well it is known, with a sentence for a page to show in English and Japanese.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * import { starLook, type StarPart } from "@johnmorrisdotca/wakusei-planets";
88
+ *
89
+ * const parts: readonly StarPart[] = starLook({ temperatureK: 5772, radiusSun: 1 }).parts;
90
+ * console.log(parts.map((part) => `${part.part}: ${part.status}`).join(", "));
91
+ * ```
92
+ */
93
+ export interface StarPart {
94
+ part: "temperature" | "colour" | "size" | "edge" | "surface" | "spots" | "rotation" | "shape" | "poles" | "prominences" | "glow";
95
+ status: StarStatus;
96
+ note: {
97
+ en: string;
98
+ ja: string;
99
+ };
100
+ }
101
+ /**
102
+ * A star ready to draw: its numbers with the defaults filled in, its colour, the shaded look and the facts the line drawing uses, how fast it turns on a page, and which parts of the picture are known and which invented.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * import { starLook } from "@johnmorrisdotca/wakusei-planets";
107
+ *
108
+ * const star = starLook({ name: "Sirius A", temperatureK: 9940, radiusSun: 1.71, luminosityClass: "V", rotationDays: 5.5 });
109
+ * console.log(star.colour, star.disclaimer.en);
110
+ * ```
111
+ */
112
+ export interface StarLook {
113
+ /** The numbers it was made from, as they were given. */
114
+ numbers: StarNumbers;
115
+ name: string;
116
+ temperatureK: number;
117
+ radiusSun: number;
118
+ radiusKm: number;
119
+ /** Which drawing this is: `full` or `low` (see `StarLookOptions`). */
120
+ quality: "full" | "low";
121
+ luminosityClass: LuminosityClass;
122
+ /** `undefined` when the numbers gave none. */
123
+ rotationDays: number | undefined;
124
+ activity: number;
125
+ oblateness: number;
126
+ corona: number;
127
+ /** The black-body colour of the temperature, `#rrggbb`. */
128
+ colour: string;
129
+ /** The colour the line drawing and a page's accents take: the bright cells of the shaded look, a little stronger than the plain black body so a star reads as coloured, `#rrggbb`. */
130
+ tone: string;
131
+ /** The shaded look: its `surface` is shader code with the numbers written into it. Its `id` is `sun`, the one star among the bodies. Draw it through the element's `star` attribute; it is not registered under a body. */
132
+ look: BodyLook;
133
+ /** The facts the line drawing uses: the Sun's, with this star's name, size, shape and the outlines of its cells and spots. */
134
+ facts: BodyFacts;
135
+ /** How it turns on a page: degrees a second (negative for backwards), and how many times faster than the star really turns that is (`undefined` when its rotation is not given). */
136
+ turn: {
137
+ degreesPerSecond: number;
138
+ speedUp: number | undefined;
139
+ };
140
+ /** Which parts of the picture are measured, estimated, derived or illustrative. */
141
+ parts: readonly StarPart[];
142
+ /** The sentence for a page to show with the picture. */
143
+ disclaimer: {
144
+ en: string;
145
+ ja: string;
146
+ };
147
+ /** The same numbers give the same key: the renderer keeps one drawing for each. */
148
+ key: string;
149
+ }
150
+ /**
151
+ * The Sun as numbers: the first example of a star, and the size the others are drawn beside.
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * import { STAR_SUN, starLook } from "@johnmorrisdotca/wakusei-planets";
156
+ *
157
+ * console.log(STAR_SUN.temperatureK, starLook(STAR_SUN).turn.degreesPerSecond);
158
+ * ```
159
+ */
160
+ export declare const STAR_SUN: StarNumbers;
161
+ /**
162
+ * How much to spend drawing a star: `full` boils its granulation and draws prominences and flares on the edge of an active star; `low` is a still surface and a soft glow, for weak devices.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import { starLook, type StarLookOptions } from "@johnmorrisdotca/wakusei-planets";
167
+ *
168
+ * const options: StarLookOptions = { quality: "low" };
169
+ * console.log(starLook({ temperatureK: 3042, radiusSun: 0.154 }, options).quality);
170
+ * ```
171
+ */
172
+ export interface StarLookOptions {
173
+ quality?: "full" | "low";
174
+ }
175
+ /**
176
+ * Everything the package draws a star with, from the numbers alone: the same numbers always give the same star. Throws a `RangeError` that names the number when one is out of range.
177
+ *
178
+ * The colour is the black body of `temperatureK`. The surface is grained by the luminosity class (and faint on hot stars, smooth on a white dwarf), spotted by `activity` (none on hot stars by default, many on
179
+ * cool dwarfs), darker toward the edge by the temperature, flattened by `oblateness` with hotter poles, and ringed with a soft corona. The cells and the spots are invented; `parts` says which parts are not.
180
+ *
181
+ * @param numbers - The star's numbers.
182
+ * @param options - `quality: "low"` for a cheaper drawing for weak devices (no boiling granulation, no prominences or flares); the default is `full`.
183
+ * @returns The star ready to draw.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * import { starLook } from "@johnmorrisdotca/wakusei-planets";
188
+ *
189
+ * const rigel = starLook({ name: "Rigel", temperatureK: 12100, radiusSun: 78.9, luminosityClass: "Ia", rotationDays: 25 });
190
+ * console.log(rigel.colour, rigel.activity, rigel.parts.filter((part) => part.status === "illustrative").length);
191
+ * ```
192
+ */
193
+ export declare function starLook(numbers: StarNumbers, options?: StarLookOptions): StarLook;