@johnmorrisdotca/chikyu-earth 0.9.1 → 0.10.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 +9 -0
- package/dist/adopt.d.ts +1 -0
- package/dist/adopt.js +26 -0
- package/dist/element.js +4 -1
- package/dist/realistic/element.d.ts +3 -3
- package/dist/realistic/element.js +26 -13
- package/dist/realistic/gate.d.ts +2 -0
- package/dist/realistic/gate.js +18 -7
- package/dist/realistic/shared.d.ts +16 -2
- package/dist/realistic/shared.js +45 -11
- package/dist/space/element.js +4 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.10.0
|
|
4
|
+
|
|
5
|
+
Three fixes for a page that puts the Earth behind something else.
|
|
6
|
+
|
|
7
|
+
- **A time and a view changed together are one draw** (MODULES-177). `time` and `setView` in one task each asked the rate gate; the first drew at once and the second 33 ms later, so a page moving the Sun and the camera on every tick drew twice. `Gate.soon()` makes every ask of one task a single ask, made when the task ends, and the realistic element uses it. Measured before: 2 draws for the two changes, now 1 (`e2e/realistic.gl.ts`).
|
|
8
|
+
- **`max-ratio`** on `<chikyu-earth-realistic>`: the most device pixels to a CSS pixel the canvas is drawn at (2 at most; `0.5` draws at half the size and scales it up). It is read when the element connects and is the shared renderer's cap for the page; the textures are chosen for it too. Test: at a device ratio of 2, `max-ratio="1"` draws a canvas under a third of the pixels.
|
|
9
|
+
- **A strict Content-Security-Policy no longer refuses the elements' styles** (MODULES-180). `<chikyu-earth>`, `<chikyu-earth-realistic>` and `<chikyu-space>` wrote a `<style>` into their shadow roots, which `style-src 'self'` blocks. They now adopt a constructable stylesheet (shared by every element with the same text), and where a browser has none, a `<style>` carrying the element's `nonce`. Test (`e2e/csp.gl.ts`): each element in a page served with `style-src 'self'` reports no violation, its styles apply, and no `<style>` is written.
|
|
10
|
+
- **`setSharedRenderer(renderer)`** (`@johnmorrisdotca/chikyu-earth/realistic/element`; MODULES-171): the page hands the Earth and the space view a renderer made with kazoku-ui's `createSharedRenderer`, so a page that draws other things with it keeps one WebGL context. The page owns it: its `maxRatio` and `onContext` apply and the elements never dispose it. Call it before the first element connects (it throws while one is in the page). Unit-tested (`test/shared.test.ts`).
|
|
11
|
+
|
|
3
12
|
## 0.9.1
|
|
4
13
|
|
|
5
14
|
The ground now follows the pointer, in every look and when riding.
|
package/dist/adopt.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function adoptStyles(root: ShadowRoot, css: string, nonce?: string): void;
|
package/dist/adopt.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Puts an element's styles into its shadow root so a strict Content-Security-Policy (`style-src 'self'`) allows them: a constructable stylesheet is not inline
|
|
2
|
+
// markup. A browser without that gets a <style> carrying the element's `nonce` attribute, which a page can allow by nonce.
|
|
3
|
+
const sheets = new Map();
|
|
4
|
+
export function adoptStyles(root, css, nonce = "") {
|
|
5
|
+
if (typeof CSSStyleSheet === "function" && "replaceSync" in CSSStyleSheet.prototype && "adoptedStyleSheets" in root) {
|
|
6
|
+
try {
|
|
7
|
+
let sheet = sheets.get(css);
|
|
8
|
+
if (!sheet) {
|
|
9
|
+
sheet = new CSSStyleSheet();
|
|
10
|
+
sheet.replaceSync(css);
|
|
11
|
+
sheets.set(css, sheet);
|
|
12
|
+
}
|
|
13
|
+
if (!root.adoptedStyleSheets.includes(sheet))
|
|
14
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// a browser that has the API and refuses it: the <style> below
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const style = root.ownerDocument.createElement("style");
|
|
22
|
+
if (nonce)
|
|
23
|
+
style.setAttribute("nonce", nonce);
|
|
24
|
+
style.textContent = css;
|
|
25
|
+
root.prepend(style);
|
|
26
|
+
}
|
package/dist/element.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// <chikyu-earth>: the globe as a web component. A shared view controller supplies its yaw, tilt, zoom and pan;
|
|
2
2
|
// it opens centred on where you are (the `center` attribute wins over geolocation). Importing registers it once.
|
|
3
|
+
import { adoptStyles } from "./adopt.js";
|
|
3
4
|
import { globe } from "./globe.js";
|
|
4
5
|
import { cleanStyle } from "./styles.js";
|
|
5
6
|
import { globeBaseScale, HOME_VIEW, lookAt } from "./view.js";
|
|
@@ -13,6 +14,7 @@ const readCentre = (value) => {
|
|
|
13
14
|
return null;
|
|
14
15
|
return { lon, lat };
|
|
15
16
|
};
|
|
17
|
+
const STYLE = ":host{display:block;position:relative}.holder svg{display:block;width:100%;height:auto}.overlay{position:absolute;inset:0;pointer-events:none;transform-origin:50% 50%}";
|
|
16
18
|
/**
|
|
17
19
|
* The `<chikyu-earth>` custom element: the line globe, drawn as SVG in its shadow root. Importing this entry point registers the tag once. It takes the attributes `size` (pixels, 320 by default), `center` (`"lon,lat"`; wins over the browser's location), `look` (`line` or `thick`; `style` is the older name for it, and a framework that reads `style` as CSS cannot use it) and `graticule`, and a shared view controller's camera through `setView`.
|
|
18
20
|
*
|
|
@@ -97,7 +99,8 @@ export class ChikyuEarth extends HTMLElement {
|
|
|
97
99
|
#ensureShell() {
|
|
98
100
|
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
99
101
|
if (!this.#overlay || !root.contains(this.#overlay)) {
|
|
100
|
-
root.innerHTML = `<
|
|
102
|
+
root.innerHTML = `<div class="holder"></div><div class="overlay"></div>`;
|
|
103
|
+
adoptStyles(root, STYLE, this.nonce);
|
|
101
104
|
this.#overlay = root.querySelector(".overlay");
|
|
102
105
|
}
|
|
103
106
|
return root;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { GeoPoint, GlobeView } from "../types.ts";
|
|
2
|
-
import { setThreeLoader } from "./shared.ts";
|
|
3
|
-
export { setThreeLoader };
|
|
2
|
+
import { setSharedRenderer, setThreeLoader } from "./shared.ts";
|
|
3
|
+
export { setSharedRenderer, setThreeLoader };
|
|
4
4
|
import type { OverlayHost } from "../markers/layer.ts";
|
|
5
5
|
import { type Layer } from "./tiers.ts";
|
|
6
6
|
type EarthLayer = Extract<Layer, "day" | "night" | "clouds" | "water">;
|
|
@@ -31,7 +31,7 @@ export type EarthState = "loading" | "live" | "unavailable" | "lost" | "idle";
|
|
|
31
31
|
*/
|
|
32
32
|
export declare const readTime: (value: string | null) => number | "now";
|
|
33
33
|
/**
|
|
34
|
-
* The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds
|
|
34
|
+
* The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds`, `texture-base` (where the pictures are served from) and `max-ratio` (the most device pixels to a CSS pixel the canvas is drawn at, 2 at most, and `0.5` draws at half the size and scales it up: for a background; read when the element connects, and it is the shared renderer's cap for the whole page), and a shared view controller's camera through `setView`. It draws only when something changes, at most 30 times a second, and nothing while it is out of sight or the tab is hidden.
|
|
35
35
|
*
|
|
36
36
|
* @example
|
|
37
37
|
* ```ts no-run
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
// <chikyu-earth-realistic>: the Earth from space as a web component, drawn by kazoku-ui's shared 3D renderer (one WebGL context for the
|
|
2
|
+
// page) with three.js, which is the page's own. Optional entry point: the flat `<chikyu-earth>` needs neither. It draws only when something
|
|
3
|
+
// changes, at most 30 times a second, and nothing at all while it is out of sight or the tab is hidden.
|
|
4
|
+
import { adoptStyles } from "../adopt.js";
|
|
1
5
|
import { createEarthItem, lookAt, newModel } from "./earth.js";
|
|
2
6
|
import { createGate, frameCap } from "./gate.js";
|
|
3
|
-
import { setThreeLoader, takeRenderer } from "./shared.js";
|
|
4
|
-
export { setThreeLoader };
|
|
7
|
+
import { setSharedRenderer, setThreeLoader, takeRenderer } from "./shared.js";
|
|
8
|
+
export { setSharedRenderer, setThreeLoader };
|
|
5
9
|
import { MAX_TEXTURE, TIERS } from "./tiers.js";
|
|
10
|
+
const STYLE = ":host{display:block;aspect-ratio:1/1;position:relative}#stage,#overlay{position:absolute;inset:0}#overlay{pointer-events:none}";
|
|
6
11
|
const RAD = Math.PI / 180;
|
|
7
12
|
/** The Sun and the clouds are re-placed this often while the time is "now": 15 degrees an hour is 0.25 degree a minute. */
|
|
8
13
|
const LIVE_TICK_MS = 60_000;
|
|
@@ -15,6 +20,10 @@ const readCentre = (value) => {
|
|
|
15
20
|
return null;
|
|
16
21
|
return { lon: lon, lat: lat };
|
|
17
22
|
};
|
|
23
|
+
const readRatio = (value) => {
|
|
24
|
+
const ratio = Number(value);
|
|
25
|
+
return value !== null && Number.isFinite(ratio) && ratio > 0 ? Math.min(ratio, 2) : undefined;
|
|
26
|
+
};
|
|
18
27
|
/**
|
|
19
28
|
* A `time` attribute: "now" (or none) follows the clock, else an ISO date such as 2026-06-21T12:00:00Z.
|
|
20
29
|
*
|
|
@@ -35,7 +44,7 @@ export const readTime = (value) => {
|
|
|
35
44
|
return Number.isFinite(ms) ? ms : "now";
|
|
36
45
|
};
|
|
37
46
|
/**
|
|
38
|
-
* The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds
|
|
47
|
+
* The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds`, `texture-base` (where the pictures are served from) and `max-ratio` (the most device pixels to a CSS pixel the canvas is drawn at, 2 at most, and `0.5` draws at half the size and scales it up: for a background; read when the element connects, and it is the shared renderer's cap for the whole page), and a shared view controller's camera through `setView`. It draws only when something changes, at most 30 times a second, and nothing while it is out of sight or the tab is hidden.
|
|
39
48
|
*
|
|
40
49
|
* @example
|
|
41
50
|
* ```ts no-run
|
|
@@ -80,7 +89,8 @@ export class ChikyuEarthRealistic extends HTMLElement {
|
|
|
80
89
|
if (!this.hasAttribute("aria-label"))
|
|
81
90
|
this.setAttribute("aria-label", "The Earth from space, with its sunlit and dark sides");
|
|
82
91
|
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
83
|
-
root.innerHTML = `<
|
|
92
|
+
root.innerHTML = `<div id="stage"></div><div id="overlay"></div>`;
|
|
93
|
+
adoptStyles(root, STYLE, this.nonce);
|
|
84
94
|
this.#stage = root.getElementById("stage");
|
|
85
95
|
this.#overlay = root.getElementById("overlay");
|
|
86
96
|
this.#stage.addEventListener("webglcontextlost", this.#lost, true);
|
|
@@ -92,14 +102,17 @@ export class ChikyuEarthRealistic extends HTMLElement {
|
|
|
92
102
|
if (!battery.charging && battery.level <= 0.2)
|
|
93
103
|
this.#gate?.setMinMs(1000 / Math.min(cap, 15));
|
|
94
104
|
}, () => undefined);
|
|
95
|
-
const
|
|
105
|
+
const maxRatio = readRatio(this.getAttribute("max-ratio"));
|
|
106
|
+
if (maxRatio)
|
|
107
|
+
this.#model.ratio = () => Math.min(globalThis.devicePixelRatio || 1, 2, maxRatio);
|
|
108
|
+
const taken = takeRenderer(this.#onContext, maxRatio);
|
|
96
109
|
this.#taken = taken;
|
|
97
110
|
const item = createEarthItem(this.#model, {
|
|
98
111
|
assets: taken.renderer.assets,
|
|
99
112
|
textureBase: () => this.#textureBase(),
|
|
100
113
|
cap: () => this.#textureCap(),
|
|
101
114
|
drift: 1.5,
|
|
102
|
-
invalidate: () => this.#gate?.
|
|
115
|
+
invalidate: () => this.#gate?.soon(),
|
|
103
116
|
onPose: (pose) => {
|
|
104
117
|
this.#pose = { ...pose, transform: "" };
|
|
105
118
|
for (const listener of this.#listeners)
|
|
@@ -169,7 +182,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
|
|
|
169
182
|
}
|
|
170
183
|
set centre(value) {
|
|
171
184
|
this.#model.centre = { ...value };
|
|
172
|
-
this.#gate?.
|
|
185
|
+
this.#gate?.soon();
|
|
173
186
|
}
|
|
174
187
|
/** The moment shown: milliseconds since the epoch, or "now" to follow the clock. */
|
|
175
188
|
get time() {
|
|
@@ -182,21 +195,21 @@ export class ChikyuEarthRealistic extends HTMLElement {
|
|
|
182
195
|
else
|
|
183
196
|
this.#model.time = this.#mode;
|
|
184
197
|
this.#startTick();
|
|
185
|
-
this.#gate?.
|
|
198
|
+
this.#gate?.soon();
|
|
186
199
|
}
|
|
187
200
|
get clouds() {
|
|
188
201
|
return this.#model.clouds;
|
|
189
202
|
}
|
|
190
203
|
set clouds(value) {
|
|
191
204
|
this.#model.clouds = value;
|
|
192
|
-
this.#gate?.
|
|
205
|
+
this.#gate?.soon();
|
|
193
206
|
}
|
|
194
207
|
get lights() {
|
|
195
208
|
return this.#model.lights;
|
|
196
209
|
}
|
|
197
210
|
set lights(value) {
|
|
198
211
|
this.#model.lights = value;
|
|
199
|
-
this.#gate?.
|
|
212
|
+
this.#gate?.soon();
|
|
200
213
|
}
|
|
201
214
|
/** What the element is doing, for a page or a test: its state, the texture widths held, the draws made. */
|
|
202
215
|
get stats() {
|
|
@@ -218,7 +231,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
|
|
|
218
231
|
this.#model.view = { ...view };
|
|
219
232
|
const drawn = this.#model.drawn;
|
|
220
233
|
if (!drawn) {
|
|
221
|
-
this.#gate?.
|
|
234
|
+
this.#gate?.soon();
|
|
222
235
|
return;
|
|
223
236
|
}
|
|
224
237
|
const shifted = view.zoom !== drawn.view.zoom || view.panX !== drawn.view.panX || view.panY !== drawn.view.panY;
|
|
@@ -226,7 +239,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
|
|
|
226
239
|
const b = lookAt(this.#model.centre, view);
|
|
227
240
|
const px = (Math.abs(b.lon - a.lon) * Math.cos(((a.lat + b.lat) / 2) * RAD) + Math.abs(b.lat - a.lat)) * RAD * drawn.radius;
|
|
228
241
|
if (shifted || px >= 0.5 || (final && px > 0.01))
|
|
229
|
-
this.#gate?.
|
|
242
|
+
this.#gate?.soon();
|
|
230
243
|
}
|
|
231
244
|
/** The view as it is (for a page that wants to read it back). */
|
|
232
245
|
get view() {
|
|
@@ -244,7 +257,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
|
|
|
244
257
|
}
|
|
245
258
|
#onTick = () => {
|
|
246
259
|
this.#model.time = Date.now();
|
|
247
|
-
this.#gate?.
|
|
260
|
+
this.#gate?.soon();
|
|
248
261
|
};
|
|
249
262
|
#take = () => {
|
|
250
263
|
if (this.#view && !this.#view.live)
|
package/dist/realistic/gate.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export declare const browserTimers: Timers;
|
|
|
19
19
|
export interface Gate {
|
|
20
20
|
/** Ask for `fire` to run: now if the last run was `minMs` ago or more, else once, when that time has passed. */
|
|
21
21
|
request(): void;
|
|
22
|
+
/** Like `request`, but every ask made in one task is one ask, made when the task ends: a time and a view set together are one draw. */
|
|
23
|
+
soon(): void;
|
|
22
24
|
/** Forget a run that is waiting. */
|
|
23
25
|
cancel(): void;
|
|
24
26
|
/** Change the least time between runs (a lower frame rate on a battery). */
|
package/dist/realistic/gate.js
CHANGED
|
@@ -27,20 +27,31 @@ export function createGate(minMs, fire, timers = browserTimers) {
|
|
|
27
27
|
let least = minMs;
|
|
28
28
|
let last = -Infinity;
|
|
29
29
|
let waiting;
|
|
30
|
+
let soon = false;
|
|
30
31
|
const run = () => {
|
|
31
32
|
waiting = undefined;
|
|
32
33
|
last = timers.now();
|
|
33
34
|
fire();
|
|
34
35
|
};
|
|
36
|
+
const request = () => {
|
|
37
|
+
if (waiting !== undefined)
|
|
38
|
+
return;
|
|
39
|
+
const wait = last + least - timers.now();
|
|
40
|
+
if (wait <= 0)
|
|
41
|
+
run();
|
|
42
|
+
else
|
|
43
|
+
waiting = timers.set(run, wait);
|
|
44
|
+
};
|
|
35
45
|
return {
|
|
36
|
-
request
|
|
37
|
-
|
|
46
|
+
request,
|
|
47
|
+
soon() {
|
|
48
|
+
if (soon)
|
|
38
49
|
return;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
soon = true;
|
|
51
|
+
queueMicrotask(() => {
|
|
52
|
+
soon = false;
|
|
53
|
+
request();
|
|
54
|
+
});
|
|
44
55
|
},
|
|
45
56
|
cancel() {
|
|
46
57
|
if (waiting !== undefined)
|
|
@@ -15,8 +15,22 @@ export type ContextState = "lost" | "restored" | "unavailable";
|
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
export declare function setThreeLoader(load: () => Promise<Three>): void;
|
|
18
|
-
/**
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Hands the elements a renderer the page made, so a page that draws other things with kazoku-ui's shared renderer keeps one WebGL context for all of them. Call it before the first element connects, or while none is in the page. The page owns the renderer: the elements never dispose it, and its own `maxRatio` and `onContext` apply. Pass `undefined` to go back to the package's own.
|
|
20
|
+
*
|
|
21
|
+
* @param renderer - A renderer made with `createSharedRenderer` from `@johnmorrisdotca/kazoku-ui/renderer`, whose `load` gives the same three.js, or `undefined`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts no-run
|
|
25
|
+
* import { createSharedRenderer } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
26
|
+
* import { setSharedRenderer } from "@johnmorrisdotca/chikyu-earth/realistic/element";
|
|
27
|
+
*
|
|
28
|
+
* setSharedRenderer(createSharedRenderer({ load: () => import("three"), maxRatio: 1.5 }));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function setSharedRenderer(renderer: SharedRenderer<Three> | undefined): void;
|
|
32
|
+
/** The page's renderer, made on first use. Call `release()` (from the result) when the element leaves. A `maxRatio` lower than the one it was made with makes a new one if no element holds it. */
|
|
33
|
+
export declare function takeRenderer(onContext: (state: ContextState) => void, maxRatio?: number): {
|
|
20
34
|
renderer: SharedRenderer<Three>;
|
|
21
35
|
release(): void;
|
|
22
36
|
};
|
package/dist/realistic/shared.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
// WebGL context between them. The last element to leave lets it go (after a second, in case it is only moving).
|
|
3
3
|
import { createSharedRenderer } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
4
4
|
let shared;
|
|
5
|
+
// A renderer the page made and handed over: used as it is, and never disposed here.
|
|
6
|
+
let given;
|
|
7
|
+
let sharedRatio;
|
|
5
8
|
let loader = () => import("three");
|
|
6
9
|
const contextListeners = new Set();
|
|
7
10
|
let elements = 0;
|
|
@@ -21,24 +24,55 @@ let releaseTimer;
|
|
|
21
24
|
export function setThreeLoader(load) {
|
|
22
25
|
loader = load;
|
|
23
26
|
}
|
|
24
|
-
/**
|
|
25
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Hands the elements a renderer the page made, so a page that draws other things with kazoku-ui's shared renderer keeps one WebGL context for all of them. Call it before the first element connects, or while none is in the page. The page owns the renderer: the elements never dispose it, and its own `maxRatio` and `onContext` apply. Pass `undefined` to go back to the package's own.
|
|
29
|
+
*
|
|
30
|
+
* @param renderer - A renderer made with `createSharedRenderer` from `@johnmorrisdotca/kazoku-ui/renderer`, whose `load` gives the same three.js, or `undefined`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts no-run
|
|
34
|
+
* import { createSharedRenderer } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
35
|
+
* import { setSharedRenderer } from "@johnmorrisdotca/chikyu-earth/realistic/element";
|
|
36
|
+
*
|
|
37
|
+
* setSharedRenderer(createSharedRenderer({ load: () => import("three"), maxRatio: 1.5 }));
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function setSharedRenderer(renderer) {
|
|
41
|
+
if (renderer !== given && elements > 0)
|
|
42
|
+
throw new Error("chikyu-earth: setSharedRenderer must be called before an element connects");
|
|
43
|
+
if (renderer !== given) {
|
|
44
|
+
clearTimeout(releaseTimer);
|
|
45
|
+
shared?.dispose();
|
|
46
|
+
shared = undefined;
|
|
47
|
+
}
|
|
48
|
+
given = renderer;
|
|
49
|
+
}
|
|
50
|
+
/** The page's renderer, made on first use. Call `release()` (from the result) when the element leaves. A `maxRatio` lower than the one it was made with makes a new one if no element holds it. */
|
|
51
|
+
export function takeRenderer(onContext, maxRatio) {
|
|
26
52
|
clearTimeout(releaseTimer);
|
|
53
|
+
if (!given && shared && elements <= 0 && sharedRatio !== maxRatio) {
|
|
54
|
+
shared.dispose();
|
|
55
|
+
shared = undefined;
|
|
56
|
+
}
|
|
27
57
|
elements += 1;
|
|
28
58
|
contextListeners.add(onContext);
|
|
29
|
-
shared
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
59
|
+
if (!given && !shared) {
|
|
60
|
+
sharedRatio = maxRatio;
|
|
61
|
+
shared = createSharedRenderer({
|
|
62
|
+
load: () => loader(),
|
|
63
|
+
...(maxRatio === undefined ? {} : { maxRatio }),
|
|
64
|
+
onContext: (state) => {
|
|
65
|
+
for (const listener of contextListeners)
|
|
66
|
+
listener(state);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
36
70
|
return {
|
|
37
|
-
renderer: shared,
|
|
71
|
+
renderer: (given ?? shared),
|
|
38
72
|
release() {
|
|
39
73
|
contextListeners.delete(onContext);
|
|
40
74
|
elements -= 1;
|
|
41
|
-
if (elements <= 0) {
|
|
75
|
+
if (elements <= 0 && !given) {
|
|
42
76
|
releaseTimer = setTimeout(() => {
|
|
43
77
|
if (elements <= 0) {
|
|
44
78
|
shared?.dispose();
|
package/dist/space/element.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { adoptStyles } from "../adopt.js";
|
|
1
2
|
import { createGate, frameCap } from "../realistic/gate.js";
|
|
2
3
|
import { setThreeLoader, takeRenderer } from "../realistic/shared.js";
|
|
3
4
|
import { MAX_TEXTURE } from "../realistic/tiers.js";
|
|
@@ -49,6 +50,7 @@ const readTime = (value) => {
|
|
|
49
50
|
const ms = Date.parse(value);
|
|
50
51
|
return Number.isFinite(ms) ? ms : "now";
|
|
51
52
|
};
|
|
53
|
+
const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:hidden}#stage,#labels{position:absolute;inset:0}#labels{pointer-events:none;overflow:hidden}.sp-label{position:absolute;left:0;top:0;margin:0;padding:0;border:0;background:none;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-label:focus-visible{outline:2px solid #6cd0ff;outline-offset:2px;border-radius:4px}.sp-label::before{content:"";display:inline-block;width:5px;height:5px;margin:0 5px 1px -2px;border-radius:50%;background:#fff}.sp-label[hidden]{display:none}.sp-sat{position:absolute;left:0;top:0;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-sat[hidden]{display:none}`;
|
|
52
54
|
/**
|
|
53
55
|
* The `<chikyu-space>` custom element: the Earth, the Moon, the Sun and the planets where they are at a date and time, drawn with three.js through the shared renderer. It takes `center`, `time` (`now` or an ISO date), `speed` (1 is real time, 0 holds, 86400 is a day a second, negative runs backwards), `scale` (`compressed` or `true`), `labels` and `orbits` (`off` hides them); positions are computed in the browser by astronomy-engine, an optional peer dependency. It draws only when something changes, and nothing while it is out of sight.
|
|
54
56
|
*
|
|
@@ -98,7 +100,8 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
98
100
|
if (!this.hasAttribute("aria-label"))
|
|
99
101
|
this.setAttribute("aria-label", "The Earth, the Moon, the Sun and the planets in their places for the date shown");
|
|
100
102
|
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
101
|
-
root.innerHTML = `<
|
|
103
|
+
root.innerHTML = `<div id="stage"></div><div id="labels" aria-hidden="true"></div>`;
|
|
104
|
+
adoptStyles(root, STYLE, this.nonce);
|
|
102
105
|
this.#stage = root.getElementById("stage");
|
|
103
106
|
this.#labelBox = root.getElementById("labels");
|
|
104
107
|
this.#pool.clear();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmorrisdotca/chikyu-earth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on where you are, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"earth",
|