@mark1russell7/visuals 0.2.1 → 0.2.2
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/README.md +61 -1
- package/build/globe.d.ts +5 -0
- package/build/globe.d.ts.map +1 -1
- package/build/globe.js +8 -1
- package/build/globe.js.map +1 -1
- package/build/index.d.ts +11 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -1
- package/build/labels.d.ts +115 -0
- package/build/labels.d.ts.map +1 -0
- package/build/labels.js +251 -0
- package/build/labels.js.map +1 -0
- package/build/screen-DeOx4_90.js +29 -0
- package/build/screen-DeOx4_90.js.map +1 -0
- package/build/three.d.ts +13 -1
- package/build/three.d.ts.map +1 -0
- package/build/three.js +38 -1
- package/build/three.js.map +1 -1
- package/package.json +9 -2
- package/styles/labels.css +88 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { i as StageContent } from "./stage-CtWLSoV-.js";
|
|
2
|
+
import { Point3, Rect } from "@mark1russell7/layout-contract";
|
|
3
|
+
/**
|
|
4
|
+
* The side of an anchor that the text of a label is on.
|
|
5
|
+
* `center` puts the middle of the text on the anchor. Use it for the name of an area.
|
|
6
|
+
*/
|
|
7
|
+
type LabelSide = "right" | "left" | "above" | "below" | "center";
|
|
8
|
+
/** The default sides of a label, in the sequence in which `placeLabels` tries them. */
|
|
9
|
+
export declare const LABEL_SIDES: readonly LabelSide[];
|
|
10
|
+
/** The default distance between the mark of an anchor and the text of its label, in CSS pixels. */
|
|
11
|
+
export declare const LABEL_GAP = 4;
|
|
12
|
+
/**
|
|
13
|
+
* One label for `placeLabels`: its anchor in a picture, and the size of its text.
|
|
14
|
+
* Each length is in CSS pixels. A position is from the top left corner of the picture.
|
|
15
|
+
*/
|
|
16
|
+
interface LabelAnchor {
|
|
17
|
+
/** The position of the anchor: the point of the picture that the label names. */
|
|
18
|
+
readonly x: number;
|
|
19
|
+
readonly y: number;
|
|
20
|
+
/** The width of the text. */
|
|
21
|
+
readonly width: number;
|
|
22
|
+
/** The height of the text. */
|
|
23
|
+
readonly height: number;
|
|
24
|
+
/**
|
|
25
|
+
* The radius of the mark at the anchor, for example a half of the width of a dot. The default is 0: the anchor has no mark.
|
|
26
|
+
* No text is on the square around a mark.
|
|
27
|
+
*/
|
|
28
|
+
readonly clear?: number | undefined;
|
|
29
|
+
/** The sides that the text can have, in the sequence of the tries. The default is `LABEL_SIDES`. */
|
|
30
|
+
readonly sides?: readonly LabelSide[] | undefined;
|
|
31
|
+
}
|
|
32
|
+
/** The picture that the labels are in. Each length is in CSS pixels. */
|
|
33
|
+
interface LabelFrame {
|
|
34
|
+
readonly width: number;
|
|
35
|
+
readonly height: number;
|
|
36
|
+
/** The distance between a mark and the text of its label. The default is `LABEL_GAP`. */
|
|
37
|
+
readonly gap?: number | undefined;
|
|
38
|
+
/** The smallest distance between a text and an edge of the picture. The default is 0. */
|
|
39
|
+
readonly edge?: number | undefined;
|
|
40
|
+
}
|
|
41
|
+
/** The place of the text of a label: its side, and its rectangle in the picture. */
|
|
42
|
+
interface LabelPlace {
|
|
43
|
+
readonly side: LabelSide;
|
|
44
|
+
readonly box: Rect;
|
|
45
|
+
}
|
|
46
|
+
/** This function gives the rectangle of the text of a label on one side of its anchor. `gap` is the distance between the mark and the text. */
|
|
47
|
+
export declare function labelBox(label: LabelAnchor, side: LabelSide, gap?: number): Rect;
|
|
48
|
+
/**
|
|
49
|
+
* This function gives each label a place for its text, or `undefined` for a label that the picture does not show.
|
|
50
|
+
* The result has one value for each label, in the sequence of the labels.
|
|
51
|
+
*
|
|
52
|
+
* - The first label has the first choice. Thus give the labels from the most important to the least important.
|
|
53
|
+
* - A label tries its sides in sequence. It takes the first side on which its text is in the picture and on no other text and on no mark.
|
|
54
|
+
* - A label with no free side is not in the picture. A label that comes later does not move a label that has a place.
|
|
55
|
+
* - A label with an anchor out of the picture, or with a text of no size, is not in the picture. Its mark continues to keep the texts away.
|
|
56
|
+
*
|
|
57
|
+
* The function compares each label with each other label. It is for some hundreds of labels.
|
|
58
|
+
*/
|
|
59
|
+
export declare function placeLabels(labels: readonly LabelAnchor[], frame: LabelFrame): (LabelPlace | undefined)[];
|
|
60
|
+
/** A label of a stage: a short text next to a point of the world. */
|
|
61
|
+
interface StageLabel {
|
|
62
|
+
/** The name of the record that the label names. The label gets the highlight of this record. */
|
|
63
|
+
readonly record: string;
|
|
64
|
+
/** The point of the world that the label names. */
|
|
65
|
+
readonly at: Point3;
|
|
66
|
+
readonly text: string;
|
|
67
|
+
/** The kind of the label. The element of the label has it in `data-kind`, so a style sheet gives each kind a look. */
|
|
68
|
+
readonly kind?: string | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* The importance of the label. When two texts cannot be in the picture together, the label with the higher rank stays.
|
|
71
|
+
* The default is 0. A label of a highlighted record is before each other label.
|
|
72
|
+
*/
|
|
73
|
+
readonly rank?: number | undefined;
|
|
74
|
+
/** The radius of the mark at the point, in CSS pixels, for example a half of the width of a dot. The default is 0. */
|
|
75
|
+
readonly clear?: number | undefined;
|
|
76
|
+
/** The sides that the text can have, in the sequence of the tries. The default is `LABEL_SIDES`. Use `["center"]` for the name of an area. */
|
|
77
|
+
readonly sides?: readonly LabelSide[] | undefined;
|
|
78
|
+
}
|
|
79
|
+
/** The options of a layer of labels. */
|
|
80
|
+
interface LabelsOptions {
|
|
81
|
+
readonly labels: readonly StageLabel[];
|
|
82
|
+
/**
|
|
83
|
+
* This function tells if the camera sees a point of the world from its position.
|
|
84
|
+
* For a globe, use `inSight`: a point on the far side of the Earth then has no label. The default shows each point.
|
|
85
|
+
*/
|
|
86
|
+
readonly visible?: ((at: Point3, eye: Point3) => boolean) | undefined;
|
|
87
|
+
/** The distance between a mark and the text of its label, in CSS pixels. The default is `LABEL_GAP`. */
|
|
88
|
+
readonly gap?: number | undefined;
|
|
89
|
+
/** The smallest distance between a text and an edge of the picture, in CSS pixels. The default is 0. */
|
|
90
|
+
readonly edge?: number | undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A layer of labels: a content of a stage with no three.js object. Use `stackContents` to show it with the content that it names.
|
|
94
|
+
* The layer has no pick: the pointer goes through each text to the picture.
|
|
95
|
+
*/
|
|
96
|
+
interface LabelsContent extends StageContent<never> {
|
|
97
|
+
/** The element of the layer. It is in the container. */
|
|
98
|
+
readonly element: HTMLElement;
|
|
99
|
+
/** This function shows the labels that a test accepts, and it removes the other labels from the picture. */
|
|
100
|
+
show(test: (label: StageLabel) => boolean): void;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* This function makes a layer of labels in a container of a page. The style sheet `@mark1russell7/visuals/labels.css` draws it.
|
|
104
|
+
*
|
|
105
|
+
* - Use the container of the stage, and give it `position: relative`. The layer then fills the picture, above the canvas.
|
|
106
|
+
* - Each label is an HTML element, so its text is sharp at each size and it uses the font of the page.
|
|
107
|
+
* - Before each picture, the layer puts each text next to its point. `placeLabels` keeps each text off each other text and off each mark.
|
|
108
|
+
* - The places are a function of the picture: after a move of the camera, each label tries its sides from the first side.
|
|
109
|
+
* - While the picture does not move, a label keeps its side. So a highlight or a change of `show` moves no other text.
|
|
110
|
+
* - The layer writes no inline style attribute. It sets each position through the CSS object model, so a strict content security policy permits it.
|
|
111
|
+
* - A screen reader ignores the layer. Give the names of the records in the page, for example in a list.
|
|
112
|
+
*/
|
|
113
|
+
export declare function createLabels(container: HTMLElement, options: LabelsOptions): LabelsContent;
|
|
114
|
+
export type { LabelAnchor, LabelFrame, LabelPlace, LabelSide, LabelsContent, LabelsOptions, StageLabel };
|
|
115
|
+
//# sourceMappingURL=labels.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"labels.d.ts","names":[],"sources":["../src/labels/place.ts","../src/labels/layer.ts"],"mappings":";;;;;;KAMY;;qBAGC,sBAAsB;;qBAGtB;;;;;UAMI;;WAEN;WACA;;WAEA;;WAEA;;;;;WAKA;;WAEA,iBAAiB;;;UAIX;WACN;WACA;;WAEA;;WAEA;;;UAIM;WACN,MAAM;WACN,KAAK;;;wBAIA,SAAS,OAAO,aAAa,MAAM,WAAW,eAA0B;;;;;;;;;;;;wBA+BxE,YAAY,iBAAiB,eAAe,OAAO,cAAc;;UC3EhE;;WAEN;;WAEA,IAAI;WACJ;;WAEA;;;;;WAKA;;WAEA;;WAEA,iBAAiB;;;UAIX;WACN,iBAAiB;;;;;WAKjB,YAAY,IAAI,QAAQ,KAAK;;WAE7B;;WAEA;;;;;;UAOM,sBAAsB;;WAE5B,SAAS;;EAElB,KAAK,OAAO,OAAO;;;;;;;;;;;;;wBA4CL,aAAa,WAAW,aAAa,SAAS,gBAAgB"}
|
package/build/labels.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { t as screenPoint } from "./screen-DeOx4_90.js";
|
|
2
|
+
import { overlapOf } from "@mark1russell7/layout-contract";
|
|
3
|
+
// src/labels/place.ts
|
|
4
|
+
/** The default sides of a label, in the sequence in which `placeLabels` tries them. */
|
|
5
|
+
const LABEL_SIDES = [
|
|
6
|
+
"right",
|
|
7
|
+
"left",
|
|
8
|
+
"above",
|
|
9
|
+
"below"
|
|
10
|
+
];
|
|
11
|
+
/** The default distance between the mark of an anchor and the text of its label, in CSS pixels. */
|
|
12
|
+
const LABEL_GAP = 4;
|
|
13
|
+
/** This function gives the rectangle of the text of a label on one side of its anchor. `gap` is the distance between the mark and the text. */
|
|
14
|
+
function labelBox(label, side, gap = 4) {
|
|
15
|
+
const { width: w, height: h } = label;
|
|
16
|
+
const reach = (label.clear ?? 0) + gap;
|
|
17
|
+
return {
|
|
18
|
+
x: side === "right" ? label.x + reach : side === "left" ? label.x - reach - w : label.x - w / 2,
|
|
19
|
+
y: side === "below" ? label.y + reach : side === "above" ? label.y - reach - h : label.y - h / 2,
|
|
20
|
+
w,
|
|
21
|
+
h
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** This function tells if the insides of two rectangles overlap. Two rectangles with a common edge do not overlap. */
|
|
25
|
+
function overlaps(a, b) {
|
|
26
|
+
const overlap = overlapOf(a, b);
|
|
27
|
+
return overlap.x > 0 && overlap.y > 0;
|
|
28
|
+
}
|
|
29
|
+
/** This function gives the square around the mark of an anchor, or `undefined` for an anchor with no mark. */
|
|
30
|
+
function markOf(label) {
|
|
31
|
+
const clear = label.clear ?? 0;
|
|
32
|
+
return clear > 0 ? {
|
|
33
|
+
x: label.x - clear,
|
|
34
|
+
y: label.y - clear,
|
|
35
|
+
w: 2 * clear,
|
|
36
|
+
h: 2 * clear
|
|
37
|
+
} : void 0;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* This function gives each label a place for its text, or `undefined` for a label that the picture does not show.
|
|
41
|
+
* The result has one value for each label, in the sequence of the labels.
|
|
42
|
+
*
|
|
43
|
+
* - The first label has the first choice. Thus give the labels from the most important to the least important.
|
|
44
|
+
* - A label tries its sides in sequence. It takes the first side on which its text is in the picture and on no other text and on no mark.
|
|
45
|
+
* - A label with no free side is not in the picture. A label that comes later does not move a label that has a place.
|
|
46
|
+
* - A label with an anchor out of the picture, or with a text of no size, is not in the picture. Its mark continues to keep the texts away.
|
|
47
|
+
*
|
|
48
|
+
* The function compares each label with each other label. It is for some hundreds of labels.
|
|
49
|
+
*/
|
|
50
|
+
function placeLabels(labels, frame) {
|
|
51
|
+
const gap = frame.gap ?? 4;
|
|
52
|
+
const edge = frame.edge ?? 0;
|
|
53
|
+
const marks = labels.map(markOf);
|
|
54
|
+
const texts = [];
|
|
55
|
+
return labels.map((label, index) => {
|
|
56
|
+
if (!(label.width > 0 && label.height > 0)) return void 0;
|
|
57
|
+
if (!(label.x >= 0 && label.x <= frame.width && label.y >= 0 && label.y <= frame.height)) return void 0;
|
|
58
|
+
for (const side of label.sides ?? LABEL_SIDES) {
|
|
59
|
+
const box = labelBox(label, side, gap);
|
|
60
|
+
if (!(box.x >= edge && box.y >= edge && box.x + box.w <= frame.width - edge && box.y + box.h <= frame.height - edge)) continue;
|
|
61
|
+
if (texts.some((text) => overlaps(box, text))) continue;
|
|
62
|
+
if (marks.some((mark, other) => mark !== void 0 && other !== index && overlaps(box, mark))) continue;
|
|
63
|
+
texts.push(box);
|
|
64
|
+
return {
|
|
65
|
+
side,
|
|
66
|
+
box
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// src/labels/layer.ts
|
|
72
|
+
/** This function gives the space that labels fill: each point, around the middle of the points. */
|
|
73
|
+
function extentOf(labels) {
|
|
74
|
+
const count = Math.max(labels.length, 1);
|
|
75
|
+
const sum = labels.reduce((to, label) => ({
|
|
76
|
+
x: to.x + label.at.x,
|
|
77
|
+
y: to.y + label.at.y,
|
|
78
|
+
z: to.z + label.at.z
|
|
79
|
+
}), {
|
|
80
|
+
x: 0,
|
|
81
|
+
y: 0,
|
|
82
|
+
z: 0
|
|
83
|
+
});
|
|
84
|
+
const center = {
|
|
85
|
+
x: sum.x / count,
|
|
86
|
+
y: sum.y / count,
|
|
87
|
+
z: sum.z / count
|
|
88
|
+
};
|
|
89
|
+
return {
|
|
90
|
+
center,
|
|
91
|
+
corners: labels.length === 0 ? [center] : labels.map((label) => label.at)
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* This function makes a layer of labels in a container of a page. The style sheet `@mark1russell7/visuals/labels.css` draws it.
|
|
96
|
+
*
|
|
97
|
+
* - Use the container of the stage, and give it `position: relative`. The layer then fills the picture, above the canvas.
|
|
98
|
+
* - Each label is an HTML element, so its text is sharp at each size and it uses the font of the page.
|
|
99
|
+
* - Before each picture, the layer puts each text next to its point. `placeLabels` keeps each text off each other text and off each mark.
|
|
100
|
+
* - The places are a function of the picture: after a move of the camera, each label tries its sides from the first side.
|
|
101
|
+
* - While the picture does not move, a label keeps its side. So a highlight or a change of `show` moves no other text.
|
|
102
|
+
* - The layer writes no inline style attribute. It sets each position through the CSS object model, so a strict content security policy permits it.
|
|
103
|
+
* - A screen reader ignores the layer. Give the names of the records in the page, for example in a list.
|
|
104
|
+
*/
|
|
105
|
+
function createLabels(container, options) {
|
|
106
|
+
const page = container.ownerDocument;
|
|
107
|
+
const visible = options.visible;
|
|
108
|
+
const records = new Set(options.labels.map((label) => label.record));
|
|
109
|
+
let width = 0;
|
|
110
|
+
let height = 0;
|
|
111
|
+
const element = page.createElement("div");
|
|
112
|
+
element.className = "visuals-labels";
|
|
113
|
+
element.setAttribute("aria-hidden", "true");
|
|
114
|
+
const parts = options.labels.map((label, index) => {
|
|
115
|
+
const text = page.createElement("span");
|
|
116
|
+
text.className = "visuals-label visuals-label-off";
|
|
117
|
+
if (label.kind !== void 0) text.dataset["kind"] = label.kind;
|
|
118
|
+
text.textContent = label.text;
|
|
119
|
+
element.append(text);
|
|
120
|
+
return {
|
|
121
|
+
label,
|
|
122
|
+
index,
|
|
123
|
+
element: text,
|
|
124
|
+
width: 0,
|
|
125
|
+
height: 0,
|
|
126
|
+
shown: true,
|
|
127
|
+
highlight: 0,
|
|
128
|
+
point: void 0,
|
|
129
|
+
side: void 0,
|
|
130
|
+
on: false,
|
|
131
|
+
left: NaN,
|
|
132
|
+
top: NaN
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
const partOf = new Map(parts.map((part) => [part.element, part]));
|
|
136
|
+
/** This function puts one text at its place, or removes it from the picture. It changes the page only for a new value. */
|
|
137
|
+
const put = (part, place) => {
|
|
138
|
+
if (place === void 0) {
|
|
139
|
+
if (part.on) part.element.classList.add("visuals-label-off");
|
|
140
|
+
part.on = false;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const left = Math.round(place.box.x);
|
|
144
|
+
const top = Math.round(place.box.y);
|
|
145
|
+
if (left !== part.left || top !== part.top) {
|
|
146
|
+
part.left = left;
|
|
147
|
+
part.top = top;
|
|
148
|
+
part.element.style.transform = `translate(${String(left)}px, ${String(top)}px)`;
|
|
149
|
+
}
|
|
150
|
+
if (place.side !== part.side) {
|
|
151
|
+
part.side = place.side;
|
|
152
|
+
part.element.dataset["side"] = place.side;
|
|
153
|
+
}
|
|
154
|
+
if (!part.on) part.element.classList.remove("visuals-label-off");
|
|
155
|
+
part.on = true;
|
|
156
|
+
};
|
|
157
|
+
/** This function gives the sides of a label, with its last side first. */
|
|
158
|
+
const sidesOf = (part) => {
|
|
159
|
+
const sides = part.label.sides ?? LABEL_SIDES;
|
|
160
|
+
const last = part.side;
|
|
161
|
+
return last === void 0 || sides[0] === last || !sides.includes(last) ? sides : [last, ...sides.filter((side) => side !== last)];
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* This function gives each text its place from the anchors of the last picture.
|
|
165
|
+
* `fresh` is true after a move of the picture: each label then tries its sides from the first side.
|
|
166
|
+
*/
|
|
167
|
+
const layout = (fresh) => {
|
|
168
|
+
const live = parts.filter((part) => part.shown && part.point !== void 0);
|
|
169
|
+
live.sort((a, b) => b.highlight - a.highlight || (b.label.rank ?? 0) - (a.label.rank ?? 0) || a.index - b.index);
|
|
170
|
+
const places = placeLabels(live.map((part) => ({
|
|
171
|
+
x: part.point?.x ?? 0,
|
|
172
|
+
y: part.point?.y ?? 0,
|
|
173
|
+
width: part.width,
|
|
174
|
+
height: part.height,
|
|
175
|
+
clear: part.label.clear,
|
|
176
|
+
sides: fresh ? part.label.sides : sidesOf(part)
|
|
177
|
+
})), {
|
|
178
|
+
width,
|
|
179
|
+
height,
|
|
180
|
+
gap: options.gap,
|
|
181
|
+
edge: options.edge
|
|
182
|
+
});
|
|
183
|
+
const placed = /* @__PURE__ */ new Set();
|
|
184
|
+
live.forEach((part, at) => {
|
|
185
|
+
placed.add(part);
|
|
186
|
+
put(part, places[at]);
|
|
187
|
+
});
|
|
188
|
+
for (const part of parts) if (!placed.has(part)) put(part, void 0);
|
|
189
|
+
};
|
|
190
|
+
const observer = new ResizeObserver((entries) => {
|
|
191
|
+
for (const entry of entries) {
|
|
192
|
+
const part = partOf.get(entry.target);
|
|
193
|
+
const size = entry.borderBoxSize[0];
|
|
194
|
+
if (part === void 0 || size === void 0) continue;
|
|
195
|
+
part.width = size.inlineSize;
|
|
196
|
+
part.height = size.blockSize;
|
|
197
|
+
}
|
|
198
|
+
layout(false);
|
|
199
|
+
});
|
|
200
|
+
for (const part of parts) observer.observe(part.element);
|
|
201
|
+
container.append(element);
|
|
202
|
+
return {
|
|
203
|
+
element,
|
|
204
|
+
objects: [],
|
|
205
|
+
extent: extentOf(options.labels),
|
|
206
|
+
has: (record) => records.has(record),
|
|
207
|
+
pick: () => void 0,
|
|
208
|
+
paint(_theme, highlight) {
|
|
209
|
+
let changed = false;
|
|
210
|
+
for (const part of parts) {
|
|
211
|
+
const next = highlight(part.label.record);
|
|
212
|
+
if (next === part.highlight) continue;
|
|
213
|
+
part.highlight = next;
|
|
214
|
+
part.element.classList.toggle("visuals-label-hovered", next === 1);
|
|
215
|
+
part.element.classList.toggle("visuals-label-selected", next === 2);
|
|
216
|
+
changed = true;
|
|
217
|
+
}
|
|
218
|
+
if (changed) layout(false);
|
|
219
|
+
},
|
|
220
|
+
step(frame) {
|
|
221
|
+
const { camera } = frame;
|
|
222
|
+
camera.updateMatrixWorld();
|
|
223
|
+
const nextHeight = frame.height;
|
|
224
|
+
const nextWidth = frame.height * camera.aspect;
|
|
225
|
+
let moved = nextWidth !== width || nextHeight !== height;
|
|
226
|
+
width = nextWidth;
|
|
227
|
+
height = nextHeight;
|
|
228
|
+
const view = camera.matrixWorldInverse.elements;
|
|
229
|
+
const projection = camera.projectionMatrix.elements;
|
|
230
|
+
for (const part of parts) {
|
|
231
|
+
const { at } = part.label;
|
|
232
|
+
const point = visible === void 0 || visible(at, camera.position) ? screenPoint(at, view, projection, width, height) : void 0;
|
|
233
|
+
if (point?.x !== part.point?.x || point?.y !== part.point?.y) moved = true;
|
|
234
|
+
part.point = point;
|
|
235
|
+
}
|
|
236
|
+
layout(moved);
|
|
237
|
+
return false;
|
|
238
|
+
},
|
|
239
|
+
show(test) {
|
|
240
|
+
for (const part of parts) part.shown = test(part.label);
|
|
241
|
+
layout(false);
|
|
242
|
+
},
|
|
243
|
+
dispose() {
|
|
244
|
+
observer.disconnect();
|
|
245
|
+
element.remove();
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
export { LABEL_GAP, LABEL_SIDES, createLabels, labelBox, placeLabels };
|
|
250
|
+
|
|
251
|
+
//# sourceMappingURL=labels.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"labels.js","names":[],"sources":["../src/labels/place.ts","../src/labels/layer.ts"],"sourcesContent":["import { overlapOf, type Rect } from \"@mark1russell7/layout-contract\";\n\n/**\n * The side of an anchor that the text of a label is on.\n * `center` puts the middle of the text on the anchor. Use it for the name of an area.\n */\nexport type LabelSide = \"right\" | \"left\" | \"above\" | \"below\" | \"center\";\n\n/** The default sides of a label, in the sequence in which `placeLabels` tries them. */\nexport const LABEL_SIDES: readonly LabelSide[] = [\"right\", \"left\", \"above\", \"below\"];\n\n/** The default distance between the mark of an anchor and the text of its label, in CSS pixels. */\nexport const LABEL_GAP = 4;\n\n/**\n * One label for `placeLabels`: its anchor in a picture, and the size of its text.\n * Each length is in CSS pixels. A position is from the top left corner of the picture.\n */\nexport interface LabelAnchor {\n /** The position of the anchor: the point of the picture that the label names. */\n readonly x: number;\n readonly y: number;\n /** The width of the text. */\n readonly width: number;\n /** The height of the text. */\n readonly height: number;\n /**\n * The radius of the mark at the anchor, for example a half of the width of a dot. The default is 0: the anchor has no mark.\n * No text is on the square around a mark.\n */\n readonly clear?: number | undefined;\n /** The sides that the text can have, in the sequence of the tries. The default is `LABEL_SIDES`. */\n readonly sides?: readonly LabelSide[] | undefined;\n}\n\n/** The picture that the labels are in. Each length is in CSS pixels. */\nexport interface LabelFrame {\n readonly width: number;\n readonly height: number;\n /** The distance between a mark and the text of its label. The default is `LABEL_GAP`. */\n readonly gap?: number | undefined;\n /** The smallest distance between a text and an edge of the picture. The default is 0. */\n readonly edge?: number | undefined;\n}\n\n/** The place of the text of a label: its side, and its rectangle in the picture. */\nexport interface LabelPlace {\n readonly side: LabelSide;\n readonly box: Rect;\n}\n\n/** This function gives the rectangle of the text of a label on one side of its anchor. `gap` is the distance between the mark and the text. */\nexport function labelBox(label: LabelAnchor, side: LabelSide, gap: number = LABEL_GAP): Rect {\n const { width: w, height: h } = label;\n const reach = (label.clear ?? 0) + gap;\n const x = side === \"right\" ? label.x + reach : side === \"left\" ? label.x - reach - w : label.x - w / 2;\n const y = side === \"below\" ? label.y + reach : side === \"above\" ? label.y - reach - h : label.y - h / 2;\n return { x, y, w, h };\n}\n\n/** This function tells if the insides of two rectangles overlap. Two rectangles with a common edge do not overlap. */\nfunction overlaps(a: Rect, b: Rect): boolean {\n const overlap = overlapOf(a, b);\n return overlap.x > 0 && overlap.y > 0;\n}\n\n/** This function gives the square around the mark of an anchor, or `undefined` for an anchor with no mark. */\nfunction markOf(label: LabelAnchor): Rect | undefined {\n const clear = label.clear ?? 0;\n return clear > 0 ? { x: label.x - clear, y: label.y - clear, w: 2 * clear, h: 2 * clear } : undefined;\n}\n\n/**\n * This function gives each label a place for its text, or `undefined` for a label that the picture does not show.\n * The result has one value for each label, in the sequence of the labels.\n *\n * - The first label has the first choice. Thus give the labels from the most important to the least important.\n * - A label tries its sides in sequence. It takes the first side on which its text is in the picture and on no other text and on no mark.\n * - A label with no free side is not in the picture. A label that comes later does not move a label that has a place.\n * - A label with an anchor out of the picture, or with a text of no size, is not in the picture. Its mark continues to keep the texts away.\n *\n * The function compares each label with each other label. It is for some hundreds of labels.\n */\nexport function placeLabels(labels: readonly LabelAnchor[], frame: LabelFrame): (LabelPlace | undefined)[] {\n const gap = frame.gap ?? LABEL_GAP;\n const edge = frame.edge ?? 0;\n const marks = labels.map(markOf);\n const texts: Rect[] = [];\n return labels.map((label, index) => {\n if (!(label.width > 0 && label.height > 0)) return undefined;\n if (!(label.x >= 0 && label.x <= frame.width && label.y >= 0 && label.y <= frame.height)) return undefined;\n for (const side of label.sides ?? LABEL_SIDES) {\n const box = labelBox(label, side, gap);\n if (!(box.x >= edge && box.y >= edge && box.x + box.w <= frame.width - edge && box.y + box.h <= frame.height - edge)) continue;\n if (texts.some((text) => overlaps(box, text))) continue;\n if (marks.some((mark, other) => mark !== undefined && other !== index && overlaps(box, mark))) continue;\n texts.push(box);\n return { side, box };\n }\n return undefined;\n });\n}\n","import type { Point, Point3 } from \"@mark1russell7/layout-contract\";\nimport type { StageExtent } from \"../extent.ts\";\nimport type { Highlight } from \"../highlight.ts\";\nimport { screenPoint } from \"../screen.ts\";\nimport type { StageContent, StageFrame } from \"../stage/stage.ts\";\nimport { LABEL_SIDES, placeLabels, type LabelAnchor, type LabelPlace, type LabelSide } from \"./place.ts\";\n\n/** A label of a stage: a short text next to a point of the world. */\nexport interface StageLabel {\n /** The name of the record that the label names. The label gets the highlight of this record. */\n readonly record: string;\n /** The point of the world that the label names. */\n readonly at: Point3;\n readonly text: string;\n /** The kind of the label. The element of the label has it in `data-kind`, so a style sheet gives each kind a look. */\n readonly kind?: string | undefined;\n /**\n * The importance of the label. When two texts cannot be in the picture together, the label with the higher rank stays.\n * The default is 0. A label of a highlighted record is before each other label.\n */\n readonly rank?: number | undefined;\n /** The radius of the mark at the point, in CSS pixels, for example a half of the width of a dot. The default is 0. */\n readonly clear?: number | undefined;\n /** The sides that the text can have, in the sequence of the tries. The default is `LABEL_SIDES`. Use `[\"center\"]` for the name of an area. */\n readonly sides?: readonly LabelSide[] | undefined;\n}\n\n/** The options of a layer of labels. */\nexport interface LabelsOptions {\n readonly labels: readonly StageLabel[];\n /**\n * This function tells if the camera sees a point of the world from its position.\n * For a globe, use `inSight`: a point on the far side of the Earth then has no label. The default shows each point.\n */\n readonly visible?: ((at: Point3, eye: Point3) => boolean) | undefined;\n /** The distance between a mark and the text of its label, in CSS pixels. The default is `LABEL_GAP`. */\n readonly gap?: number | undefined;\n /** The smallest distance between a text and an edge of the picture, in CSS pixels. The default is 0. */\n readonly edge?: number | undefined;\n}\n\n/**\n * A layer of labels: a content of a stage with no three.js object. Use `stackContents` to show it with the content that it names.\n * The layer has no pick: the pointer goes through each text to the picture.\n */\nexport interface LabelsContent extends StageContent<never> {\n /** The element of the layer. It is in the container. */\n readonly element: HTMLElement;\n /** This function shows the labels that a test accepts, and it removes the other labels from the picture. */\n show(test: (label: StageLabel) => boolean): void;\n}\n\n/** One label with its element and its state in the picture. */\ninterface Part {\n readonly label: StageLabel;\n readonly index: number;\n readonly element: HTMLElement;\n /** The size of the text, in CSS pixels. It is 0 until the browser gives the size. */\n width: number;\n height: number;\n /** This value is false for a label that `show` removed. */\n shown: boolean;\n highlight: Highlight;\n /** The anchor in the picture, or `undefined` for a point that the camera does not see. */\n point: Point | undefined;\n /** The last side of the text. While the picture does not move, the label tries it first, so a highlight moves no text. */\n side: LabelSide | undefined;\n /** This value is true while the text is in the picture. */\n on: boolean;\n /** The position of the text in the picture, in whole CSS pixels. */\n left: number;\n top: number;\n}\n\n/** This function gives the space that labels fill: each point, around the middle of the points. */\nfunction extentOf(labels: readonly StageLabel[]): StageExtent {\n const count = Math.max(labels.length, 1);\n const sum = labels.reduce((to, label) => ({ x: to.x + label.at.x, y: to.y + label.at.y, z: to.z + label.at.z }), { x: 0, y: 0, z: 0 });\n const center = { x: sum.x / count, y: sum.y / count, z: sum.z / count };\n return { center, corners: labels.length === 0 ? [center] : labels.map((label) => label.at) };\n}\n\n/**\n * This function makes a layer of labels in a container of a page. The style sheet `@mark1russell7/visuals/labels.css` draws it.\n *\n * - Use the container of the stage, and give it `position: relative`. The layer then fills the picture, above the canvas.\n * - Each label is an HTML element, so its text is sharp at each size and it uses the font of the page.\n * - Before each picture, the layer puts each text next to its point. `placeLabels` keeps each text off each other text and off each mark.\n * - The places are a function of the picture: after a move of the camera, each label tries its sides from the first side.\n * - While the picture does not move, a label keeps its side. So a highlight or a change of `show` moves no other text.\n * - The layer writes no inline style attribute. It sets each position through the CSS object model, so a strict content security policy permits it.\n * - A screen reader ignores the layer. Give the names of the records in the page, for example in a list.\n */\nexport function createLabels(container: HTMLElement, options: LabelsOptions): LabelsContent {\n const page = container.ownerDocument;\n const visible = options.visible;\n const records = new Set(options.labels.map((label) => label.record));\n let width = 0;\n let height = 0;\n\n const element = page.createElement(\"div\");\n element.className = \"visuals-labels\";\n element.setAttribute(\"aria-hidden\", \"true\");\n const parts: Part[] = options.labels.map((label, index) => {\n const text = page.createElement(\"span\");\n text.className = \"visuals-label visuals-label-off\";\n if (label.kind !== undefined) text.dataset[\"kind\"] = label.kind;\n text.textContent = label.text;\n element.append(text);\n return { label, index, element: text, width: 0, height: 0, shown: true, highlight: 0, point: undefined, side: undefined, on: false, left: Number.NaN, top: Number.NaN };\n });\n const partOf = new Map<Element, Part>(parts.map((part) => [part.element, part]));\n\n /** This function puts one text at its place, or removes it from the picture. It changes the page only for a new value. */\n const put = (part: Part, place: LabelPlace | undefined): void => {\n if (place === undefined) {\n if (part.on) part.element.classList.add(\"visuals-label-off\");\n part.on = false;\n return;\n }\n const left = Math.round(place.box.x);\n const top = Math.round(place.box.y);\n if (left !== part.left || top !== part.top) {\n part.left = left;\n part.top = top;\n part.element.style.transform = `translate(${String(left)}px, ${String(top)}px)`;\n }\n if (place.side !== part.side) {\n part.side = place.side;\n part.element.dataset[\"side\"] = place.side;\n }\n if (!part.on) part.element.classList.remove(\"visuals-label-off\");\n part.on = true;\n };\n\n /** This function gives the sides of a label, with its last side first. */\n const sidesOf = (part: Part): readonly LabelSide[] => {\n const sides = part.label.sides ?? LABEL_SIDES;\n const last = part.side;\n return last === undefined || sides[0] === last || !sides.includes(last) ? sides : [last, ...sides.filter((side) => side !== last)];\n };\n\n /**\n * This function gives each text its place from the anchors of the last picture.\n * `fresh` is true after a move of the picture: each label then tries its sides from the first side.\n */\n const layout = (fresh: boolean): void => {\n const live = parts.filter((part) => part.shown && part.point !== undefined);\n // A highlighted record is first, then the higher rank, then the sequence of the labels.\n live.sort((a, b) => b.highlight - a.highlight || (b.label.rank ?? 0) - (a.label.rank ?? 0) || a.index - b.index);\n const anchors: LabelAnchor[] = live.map((part) => ({\n x: part.point?.x ?? 0,\n y: part.point?.y ?? 0,\n width: part.width,\n height: part.height,\n clear: part.label.clear,\n sides: fresh ? part.label.sides : sidesOf(part),\n }));\n const places = placeLabels(anchors, { width, height, gap: options.gap, edge: options.edge });\n const placed = new Set<Part>();\n live.forEach((part, at) => {\n placed.add(part);\n put(part, places[at]);\n });\n for (const part of parts) if (!placed.has(part)) put(part, undefined);\n };\n\n // The browser gives the size of each text one time at the start, and again after a change of the font or of the zoom.\n const observer = new ResizeObserver((entries) => {\n for (const entry of entries) {\n const part = partOf.get(entry.target);\n const size = entry.borderBoxSize[0];\n if (part === undefined || size === undefined) continue;\n part.width = size.inlineSize;\n part.height = size.blockSize;\n }\n layout(false);\n });\n for (const part of parts) observer.observe(part.element);\n container.append(element);\n\n return {\n element,\n objects: [],\n extent: extentOf(options.labels),\n has: (record) => records.has(record),\n pick: () => undefined,\n paint(_theme, highlight): void {\n let changed = false;\n for (const part of parts) {\n const next = highlight(part.label.record);\n if (next === part.highlight) continue;\n part.highlight = next;\n part.element.classList.toggle(\"visuals-label-hovered\", next === 1);\n part.element.classList.toggle(\"visuals-label-selected\", next === 2);\n changed = true;\n }\n if (changed) layout(false);\n },\n step(frame: StageFrame): boolean {\n const { camera } = frame;\n // The stage moves the camera before the step, and three.js sets the matrices of the camera only when it makes the picture.\n camera.updateMatrixWorld();\n const nextHeight = frame.height;\n const nextWidth = frame.height * camera.aspect;\n let moved = nextWidth !== width || nextHeight !== height;\n width = nextWidth;\n height = nextHeight;\n const view = camera.matrixWorldInverse.elements;\n const projection = camera.projectionMatrix.elements;\n for (const part of parts) {\n const { at } = part.label;\n const point = visible === undefined || visible(at, camera.position) ? screenPoint(at, view, projection, width, height) : undefined;\n if (point?.x !== part.point?.x || point?.y !== part.point?.y) moved = true;\n part.point = point;\n }\n layout(moved);\n return false;\n },\n show(test): void {\n for (const part of parts) part.shown = test(part.label);\n layout(false);\n },\n dispose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values. The matrix has 16 numbers, one column after the other. */
|
|
3
|
+
function row(matrix, at, x, y, z, w) {
|
|
4
|
+
return (matrix[at] ?? 0) * x + (matrix[at + 4] ?? 0) * y + (matrix[at + 8] ?? 0) * z + (matrix[at + 12] ?? 0) * w;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* This function gives the point of a picture that shows a point of the world, in CSS pixels from the top left corner.
|
|
8
|
+
* It gives `undefined` for a point that is not in front of a camera with perspective.
|
|
9
|
+
*
|
|
10
|
+
* - `view` is the matrix from the world to the camera: `matrixWorldInverse.elements` of a three.js camera.
|
|
11
|
+
* - `projection` is the matrix of the lens of the camera: `projectionMatrix.elements` of a three.js camera.
|
|
12
|
+
* - Each matrix has 16 numbers, one column after the other. This is the sequence of three.js and of WebGL.
|
|
13
|
+
* - A point to the side of the picture gives a position out of the picture. The function does not remove it.
|
|
14
|
+
*/
|
|
15
|
+
function screenPoint(point, view, projection, width, height) {
|
|
16
|
+
const x = row(view, 0, point.x, point.y, point.z, 1);
|
|
17
|
+
const y = row(view, 1, point.x, point.y, point.z, 1);
|
|
18
|
+
const z = row(view, 2, point.x, point.y, point.z, 1);
|
|
19
|
+
const w = row(view, 3, point.x, point.y, point.z, 1);
|
|
20
|
+
const depth = row(projection, 3, x, y, z, w);
|
|
21
|
+
if (!(depth > 0)) return void 0;
|
|
22
|
+
return {
|
|
23
|
+
x: (row(projection, 0, x, y, z, w) / depth + 1) / 2 * width,
|
|
24
|
+
y: (1 - row(projection, 1, x, y, z, w) / depth) / 2 * height
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export { screenPoint as t };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=screen-DeOx4_90.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-DeOx4_90.js","names":[],"sources":["../src/screen.ts"],"sourcesContent":["import type { Point, Point3 } from \"@mark1russell7/layout-contract\";\n\n/** This function gives one row of the product of a matrix and a vector of four values. The matrix has 16 numbers, one column after the other. */\nfunction row(matrix: ArrayLike<number>, at: number, x: number, y: number, z: number, w: number): number {\n return (matrix[at] ?? 0) * x + (matrix[at + 4] ?? 0) * y + (matrix[at + 8] ?? 0) * z + (matrix[at + 12] ?? 0) * w;\n}\n\n/**\n * This function gives the point of a picture that shows a point of the world, in CSS pixels from the top left corner.\n * It gives `undefined` for a point that is not in front of a camera with perspective.\n *\n * - `view` is the matrix from the world to the camera: `matrixWorldInverse.elements` of a three.js camera.\n * - `projection` is the matrix of the lens of the camera: `projectionMatrix.elements` of a three.js camera.\n * - Each matrix has 16 numbers, one column after the other. This is the sequence of three.js and of WebGL.\n * - A point to the side of the picture gives a position out of the picture. The function does not remove it.\n */\nexport function screenPoint(point: Point3, view: ArrayLike<number>, projection: ArrayLike<number>, width: number, height: number): Point | undefined {\n const x = row(view, 0, point.x, point.y, point.z, 1);\n const y = row(view, 1, point.x, point.y, point.z, 1);\n const z = row(view, 2, point.x, point.y, point.z, 1);\n const w = row(view, 3, point.x, point.y, point.z, 1);\n const depth = row(projection, 3, x, y, z, w);\n if (!(depth > 0)) return undefined;\n return {\n x: ((row(projection, 0, x, y, z, w) / depth + 1) / 2) * width,\n y: ((1 - row(projection, 1, x, y, z, w) / depth) / 2) * height,\n };\n}\n"],"mappings":";;AAGA,SAAS,IAAI,QAA2B,IAAY,GAAW,GAAW,GAAW,GAAmB;CACtG,QAAQ,OAAO,OAAO,KAAK,KAAK,OAAO,KAAK,MAAM,KAAK,KAAK,OAAO,KAAK,MAAM,KAAK,KAAK,OAAO,KAAK,OAAO,KAAK;AAClH;;;;;;;;;;AAWA,SAAgB,YAAY,OAAe,MAAyB,YAA+B,OAAe,QAAmC;CACnJ,MAAM,IAAI,IAAI,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC;CACnD,MAAM,IAAI,IAAI,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC;CACnD,MAAM,IAAI,IAAI,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC;CACnD,MAAM,IAAI,IAAI,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC;CACnD,MAAM,QAAQ,IAAI,YAAY,GAAG,GAAG,GAAG,GAAG,CAAC;CAC3C,IAAI,EAAE,QAAQ,IAAI,OAAO,KAAA;CACzB,OAAO;EACL,IAAK,IAAI,YAAY,GAAG,GAAG,GAAG,GAAG,CAAC,IAAI,QAAQ,KAAK,IAAK;EACxD,IAAK,IAAI,IAAI,YAAY,GAAG,GAAG,GAAG,GAAG,CAAC,IAAI,SAAS,IAAK;CAC1D;AACF"}
|
package/build/three.d.ts
CHANGED
|
@@ -1,2 +1,14 @@
|
|
|
1
1
|
import { a as StageFrame, c as StagePick, i as StageContent, l as createStage, n as STAGE_LIGHTS, o as StageLights, r as Stage, s as StageOptions, t as FIELD_OF_VIEW } from "./stage-CtWLSoV-.js";
|
|
2
|
-
|
|
2
|
+
import { t as StageTheme } from "./theme-CKXd_emk.js";
|
|
3
|
+
/**
|
|
4
|
+
* This function makes one content from a list of contents, for a stage that shows them together.
|
|
5
|
+
* An example is a globe with a layer of labels, or a room with its marks.
|
|
6
|
+
*
|
|
7
|
+
* - The first content gives the extent: the views and the limits of the camera come from it.
|
|
8
|
+
* - The last content is on top. A pick asks the last content first, and it stops at the first record.
|
|
9
|
+
* - A record that two contents have is one record: each of them gets its highlight.
|
|
10
|
+
* - The stage disposes the stack, and the stack disposes each content.
|
|
11
|
+
*/
|
|
12
|
+
export declare function stackContents<P, T extends StageTheme = StageTheme>(contents: readonly [StageContent<P, T>, ...StageContent<P, T>[]]): StageContent<P, T>;
|
|
13
|
+
export { FIELD_OF_VIEW, STAGE_LIGHTS, type Stage, type StageContent, type StageFrame, type StageLights, type StageOptions, type StagePick, createStage };
|
|
14
|
+
//# sourceMappingURL=three.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"three.d.ts","names":[],"sources":["../src/stage/stack.ts"],"mappings":";;;;;;;;;;;wBAYgB,cAAc,GAAG,UAAU,aAAa,YAAY,oBAAoB,aAAa,GAAG,OAAO,aAAa,GAAG,QAAQ,aAAa,GAAG"}
|
package/build/three.js
CHANGED
|
@@ -405,6 +405,43 @@ function createStage(container, options) {
|
|
|
405
405
|
}
|
|
406
406
|
};
|
|
407
407
|
}
|
|
408
|
-
|
|
408
|
+
// src/stage/stack.ts
|
|
409
|
+
/**
|
|
410
|
+
* This function makes one content from a list of contents, for a stage that shows them together.
|
|
411
|
+
* An example is a globe with a layer of labels, or a room with its marks.
|
|
412
|
+
*
|
|
413
|
+
* - The first content gives the extent: the views and the limits of the camera come from it.
|
|
414
|
+
* - The last content is on top. A pick asks the last content first, and it stops at the first record.
|
|
415
|
+
* - A record that two contents have is one record: each of them gets its highlight.
|
|
416
|
+
* - The stage disposes the stack, and the stack disposes each content.
|
|
417
|
+
*/
|
|
418
|
+
function stackContents(contents) {
|
|
419
|
+
const [first] = contents;
|
|
420
|
+
return {
|
|
421
|
+
objects: contents.flatMap((content) => content.objects),
|
|
422
|
+
get extent() {
|
|
423
|
+
return first.extent;
|
|
424
|
+
},
|
|
425
|
+
has: (record) => contents.some((content) => content.has(record)),
|
|
426
|
+
pick(ray) {
|
|
427
|
+
for (let at = contents.length - 1; at >= 0; at -= 1) {
|
|
428
|
+
const pick = contents[at]?.pick(ray);
|
|
429
|
+
if (pick !== void 0) return pick;
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
paint(theme, highlight) {
|
|
433
|
+
for (const content of contents) content.paint(theme, highlight);
|
|
434
|
+
},
|
|
435
|
+
step(frame) {
|
|
436
|
+
let active = false;
|
|
437
|
+
for (const content of contents) active = (content.step?.(frame) ?? false) || active;
|
|
438
|
+
return active;
|
|
439
|
+
},
|
|
440
|
+
dispose() {
|
|
441
|
+
for (const content of contents) content.dispose();
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
export { FIELD_OF_VIEW, STAGE_LIGHTS, createStage, stackContents };
|
|
409
446
|
|
|
410
447
|
//# sourceMappingURL=three.js.map
|