@margoengine/viewcube 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +130 -2
- package/dist/axes.d.ts +22 -0
- package/dist/axes.js +72 -0
- package/dist/compass.d.ts +62 -0
- package/dist/compass.js +204 -0
- package/dist/fit.d.ts +48 -0
- package/dist/fit.js +84 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.js +528 -0
- package/dist/up.d.ts +29 -0
- package/dist/up.js +71 -0
- package/package.json +64 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
var t = {
|
|
7
|
+
light: {
|
|
8
|
+
background: "rgba(236,238,242,.92)",
|
|
9
|
+
text: "#262a31",
|
|
10
|
+
line: "#6b7280",
|
|
11
|
+
hover: "rgba(52,128,246,.45)",
|
|
12
|
+
hoverText: "#262a31",
|
|
13
|
+
focus: "#0b3d91"
|
|
14
|
+
},
|
|
15
|
+
dark: {
|
|
16
|
+
background: "rgba(36,40,47,.92)",
|
|
17
|
+
text: "#e6e9ee",
|
|
18
|
+
line: "#8b939f",
|
|
19
|
+
hover: "rgba(90,160,255,.4)",
|
|
20
|
+
hoverText: "#fff",
|
|
21
|
+
focus: "#9cc7ff"
|
|
22
|
+
},
|
|
23
|
+
"high-contrast": {
|
|
24
|
+
background: "#000",
|
|
25
|
+
text: "#fff",
|
|
26
|
+
line: "#fff",
|
|
27
|
+
hover: "#ff0",
|
|
28
|
+
hoverText: "#000",
|
|
29
|
+
focus: "#000"
|
|
30
|
+
}
|
|
31
|
+
}, n = Object.keys(t.light), r = (e) => "--mvc-" + e.replace(/[A-Z]/g, (e) => "-" + e.toLowerCase()), i = (e) => n.map((t) => e[t] ? r(t) + ":" + e[t] : "").join(";"), a = {
|
|
32
|
+
front: [
|
|
33
|
+
[
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
1
|
|
37
|
+
],
|
|
38
|
+
[
|
|
39
|
+
1,
|
|
40
|
+
0,
|
|
41
|
+
0
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
0,
|
|
45
|
+
1,
|
|
46
|
+
0
|
|
47
|
+
]
|
|
48
|
+
],
|
|
49
|
+
back: [
|
|
50
|
+
[
|
|
51
|
+
0,
|
|
52
|
+
0,
|
|
53
|
+
-1
|
|
54
|
+
],
|
|
55
|
+
[
|
|
56
|
+
-1,
|
|
57
|
+
0,
|
|
58
|
+
0
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
0,
|
|
62
|
+
1,
|
|
63
|
+
0
|
|
64
|
+
]
|
|
65
|
+
],
|
|
66
|
+
right: [
|
|
67
|
+
[
|
|
68
|
+
1,
|
|
69
|
+
0,
|
|
70
|
+
0
|
|
71
|
+
],
|
|
72
|
+
[
|
|
73
|
+
0,
|
|
74
|
+
0,
|
|
75
|
+
-1
|
|
76
|
+
],
|
|
77
|
+
[
|
|
78
|
+
0,
|
|
79
|
+
1,
|
|
80
|
+
0
|
|
81
|
+
]
|
|
82
|
+
],
|
|
83
|
+
left: [
|
|
84
|
+
[
|
|
85
|
+
-1,
|
|
86
|
+
0,
|
|
87
|
+
0
|
|
88
|
+
],
|
|
89
|
+
[
|
|
90
|
+
0,
|
|
91
|
+
0,
|
|
92
|
+
1
|
|
93
|
+
],
|
|
94
|
+
[
|
|
95
|
+
0,
|
|
96
|
+
1,
|
|
97
|
+
0
|
|
98
|
+
]
|
|
99
|
+
],
|
|
100
|
+
top: [
|
|
101
|
+
[
|
|
102
|
+
0,
|
|
103
|
+
1,
|
|
104
|
+
0
|
|
105
|
+
],
|
|
106
|
+
[
|
|
107
|
+
1,
|
|
108
|
+
0,
|
|
109
|
+
0
|
|
110
|
+
],
|
|
111
|
+
[
|
|
112
|
+
0,
|
|
113
|
+
0,
|
|
114
|
+
-1
|
|
115
|
+
]
|
|
116
|
+
],
|
|
117
|
+
bottom: [
|
|
118
|
+
[
|
|
119
|
+
0,
|
|
120
|
+
-1,
|
|
121
|
+
0
|
|
122
|
+
],
|
|
123
|
+
[
|
|
124
|
+
1,
|
|
125
|
+
0,
|
|
126
|
+
0
|
|
127
|
+
],
|
|
128
|
+
[
|
|
129
|
+
0,
|
|
130
|
+
0,
|
|
131
|
+
1
|
|
132
|
+
]
|
|
133
|
+
]
|
|
134
|
+
}, o = Math.PI / 4, s = 1e-9, c = `.mvc{${i(t.light)}}
|
|
135
|
+
@media(prefers-color-scheme:dark){.mvc-auto{${i(t.dark)}}}
|
|
136
|
+
@media(prefers-contrast:more){.mvc-auto{${i(t["high-contrast"])}}}
|
|
137
|
+
.mvc{position:absolute;z-index:1;touch-action:none;user-select:none;-webkit-user-select:none;font:600 11px system-ui,sans-serif}
|
|
138
|
+
.mvc-cube{position:absolute;inset:0;transform-style:preserve-3d;pointer-events:none}
|
|
139
|
+
.mvc-face{position:absolute;inset:0;display:grid;grid-template:20% 60% 20%/20% 60% 20%;backface-visibility:hidden;-webkit-backface-visibility:hidden;pointer-events:auto;box-sizing:border-box;border:1px solid var(--mvc-line);background:var(--mvc-background);color:var(--mvc-text)}
|
|
140
|
+
.mvc-face>*{border:0;margin:0;padding:0;background:none;color:inherit;font:inherit;text-transform:uppercase;cursor:pointer;outline:none}
|
|
141
|
+
.mvc-face>:hover,.mvc-face>:focus-visible,.mvc>button:hover,.mvc>button:focus-visible{background:var(--mvc-hover);color:var(--mvc-hover-text)}
|
|
142
|
+
.mvc button:focus-visible{box-shadow:inset 0 0 0 2px var(--mvc-focus)}
|
|
143
|
+
.mvc-home,.mvc-roll{position:absolute;top:-26px;width:20px;height:20px;padding:0;border:1px solid var(--mvc-line);border-radius:50%;background:var(--mvc-background);color:var(--mvc-text);font:14px/1 system-ui,sans-serif;cursor:pointer}
|
|
144
|
+
.mvc-home{left:0;font-size:15px}
|
|
145
|
+
.mvc-projection{position:absolute;left:0;right:0;bottom:-26px;height:22px;border:1px solid var(--mvc-line);border-radius:4px;background:var(--mvc-background);color:var(--mvc-text);font:inherit;cursor:pointer}`, l = (e) => {
|
|
146
|
+
let t = Math.hypot(e[0], e[1], e[2]);
|
|
147
|
+
return [Math.hypot(e[0], e[2]) > 1e-9 * t ? Math.atan2(e[0], e[2]) : 0, Math.acos(Math.max(-1, Math.min(1, e[1] / t)))];
|
|
148
|
+
}, u = (e, t) => [
|
|
149
|
+
e[1] > 0 ? t.top : e[1] < 0 ? t.bottom : "",
|
|
150
|
+
e[2] > 0 ? t.front : e[2] < 0 ? t.back : "",
|
|
151
|
+
e[0] > 0 ? t.right : e[0] < 0 ? t.left : ""
|
|
152
|
+
].filter(Boolean).join(" "), d = (e, t, n, r) => {
|
|
153
|
+
let i = Math.hypot(n[0], n[1], n[2]), a = n[0] / i, o = n[1] / i, s = n[2] / i, c = Math.cos(r), l = Math.sin(r), u = (a * t[0] + o * t[1] + s * t[2]) * (1 - c);
|
|
154
|
+
e[0] = t[0] * c + (o * t[2] - s * t[1]) * l + a * u, e[1] = t[1] * c + (s * t[0] - a * t[2]) * l + o * u, e[2] = t[2] * c + (a * t[1] - o * t[0]) * l + s * u;
|
|
155
|
+
}, f = (e) => `matrix3d(${e.map((e) => +e.toFixed(6)).join(",")})`, p = 0, m = class i {
|
|
156
|
+
local(e) {
|
|
157
|
+
return e;
|
|
158
|
+
}
|
|
159
|
+
world(e) {
|
|
160
|
+
return e;
|
|
161
|
+
}
|
|
162
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
163
|
+
return new i(e, t);
|
|
164
|
+
}
|
|
165
|
+
constructor(e, { container: t = e.canvas.parentElement, corner: n = "top-right", size: r = 80, duration: i = .4, projection: a, labels: s = {}, flip: l = !1, drag: u = !0, home: d, roll: f = !1, theme: m, view: h } = {}) {
|
|
166
|
+
if (this.engine = e, this.name = "margoengine-viewcube:" + p++, this.buttons = [], this.last = "", this.disposed = !1, this.twist = 0, this.az = 0, this.dragged = !1, this.spin = !1, this.onClick = (e) => {
|
|
167
|
+
let t = e.target?.closest?.("[data-view],[data-projection],[data-home],[data-roll]");
|
|
168
|
+
if (this.dragged) return void (this.dragged = !1);
|
|
169
|
+
if (t) {
|
|
170
|
+
if (t.dataset.view) {
|
|
171
|
+
let e = t.dataset.view.split(",").map(Number), n = this.camera, r = this.local(n.position.map((e, t) => e - n.target[t])), i = this.flip && !this.animation && (r[0] * e[0] + r[1] * e[1] + r[2] * e[2]) / Math.hypot(...r) / Math.hypot(...e) > 1 - 1e-9;
|
|
172
|
+
this.view(i ? e.map((e) => -e) : e);
|
|
173
|
+
} else t.dataset.home === void 0 ? t.dataset.roll ? this.roll(+t.dataset.roll) : this.setProjection(this.projection.mode === "orthographic" ? "perspective" : "orthographic") : this.home();
|
|
174
|
+
}
|
|
175
|
+
}, this.onPointer = (e) => {
|
|
176
|
+
let t = this.grab, n = this.camera;
|
|
177
|
+
if (e.type === "pointerdown") {
|
|
178
|
+
if (this.dragged = !1, this.drag && !e.button && e.target.dataset?.view !== void 0) {
|
|
179
|
+
let [t, n] = this.current();
|
|
180
|
+
this.grab = {
|
|
181
|
+
id: e.pointerId,
|
|
182
|
+
x: e.clientX,
|
|
183
|
+
y: e.clientY,
|
|
184
|
+
theta: t,
|
|
185
|
+
phi: n
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (!t || e.pointerId !== t.id) return;
|
|
191
|
+
if (e.type !== "pointermove") {
|
|
192
|
+
this.grab = void 0;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
let r = e.clientX - t.x, i = e.clientY - t.y, a = Math.cos(this.twist), o = Math.sin(this.twist), s = r * a - i * o, c = r * o + i * a;
|
|
196
|
+
if (!this.dragged) {
|
|
197
|
+
if (Math.hypot(r, i) < 3) return;
|
|
198
|
+
this.dragged = !0, this.animation = void 0, n.cancelFit?.();
|
|
199
|
+
try {
|
|
200
|
+
this.element.setPointerCapture(t.id);
|
|
201
|
+
} catch {}
|
|
202
|
+
}
|
|
203
|
+
let l = t.theta - s * this.speed;
|
|
204
|
+
this.place(l, Math.min(Math.PI, Math.max(0, t.phi - c * this.speed)), Math.hypot(...n.position.map((e, t) => e - n.target[t]))), this.spin = !0, this.pole(l), this.spin = !1, e.preventDefault();
|
|
205
|
+
}, this.onKey = (e) => {
|
|
206
|
+
this.dragged = !1;
|
|
207
|
+
let [t, n] = this.current(), r = (e) => Math.round(e / o) * o;
|
|
208
|
+
switch (e.key) {
|
|
209
|
+
case "ArrowLeft":
|
|
210
|
+
this.orbit(r(t) - o, n, this.duration);
|
|
211
|
+
break;
|
|
212
|
+
case "ArrowRight":
|
|
213
|
+
this.orbit(r(t) + o, n, this.duration);
|
|
214
|
+
break;
|
|
215
|
+
case "ArrowUp":
|
|
216
|
+
this.orbit(t, Math.max(0, r(n) - o), this.duration);
|
|
217
|
+
break;
|
|
218
|
+
case "ArrowDown":
|
|
219
|
+
this.orbit(t, Math.min(Math.PI, r(n) + o), this.duration);
|
|
220
|
+
break;
|
|
221
|
+
case "Home":
|
|
222
|
+
this.home();
|
|
223
|
+
break;
|
|
224
|
+
default: return;
|
|
225
|
+
}
|
|
226
|
+
e.preventDefault();
|
|
227
|
+
}, !t) throw TypeError("ViewCube needs a container; pass options.container");
|
|
228
|
+
if (!(r > 0) || !(i >= 0)) throw RangeError("Invalid size or duration; use size > 0, duration >= 0");
|
|
229
|
+
let g = e.plugins?.get("cameras");
|
|
230
|
+
a ??= g?.mode ? g : void 0, this.camera = h?.camera ?? e.camera, this.duration = i, this.flip = l, this.drag = u, this.speed = 2 / r, this.projection = a, this.container = t;
|
|
231
|
+
let _ = {
|
|
232
|
+
top: "Top",
|
|
233
|
+
bottom: "Bottom",
|
|
234
|
+
front: "Front",
|
|
235
|
+
back: "Back",
|
|
236
|
+
left: "Left",
|
|
237
|
+
right: "Right",
|
|
238
|
+
...s
|
|
239
|
+
}, v = t.ownerDocument, y = (e, t, n) => {
|
|
240
|
+
let r = v.createElement(e);
|
|
241
|
+
return n && (r.className = n), t.appendChild(r), r;
|
|
242
|
+
}, b = this.element = y("div", t, "mvc");
|
|
243
|
+
b.setAttribute("role", "group"), b.setAttribute("aria-label", "View cube");
|
|
244
|
+
let [x, S] = n.split("-"), [C, w, T, E] = h?.viewport ?? [
|
|
245
|
+
0,
|
|
246
|
+
0,
|
|
247
|
+
1,
|
|
248
|
+
1
|
|
249
|
+
], [D, O] = h?.pixels ? [e.canvas.width, e.canvas.height] : [1, 1], k = {
|
|
250
|
+
left: C / D,
|
|
251
|
+
top: w / O,
|
|
252
|
+
right: 1 - (C + T) / D,
|
|
253
|
+
bottom: 1 - (w + E) / O
|
|
254
|
+
}, A = (e, t) => h ? `calc(${k[e] * 100}% + ${t}px)` : t + "px";
|
|
255
|
+
if (b.style[x] = A(x, x === "bottom" && a ? 38 : 12), this.lift = () => void (x === "top" && (b.style.top = A(x, 38))), b.style[S] = A(S, 12), b.style.width = b.style.height = r + "px", y("style", b).textContent = c, m && this.setTheme(m), this.cube = y("div", b, "mvc-cube"), this.build(this.cube, _, r), a) {
|
|
256
|
+
let e = this.toggle = y("button", b, "mvc-projection");
|
|
257
|
+
e.type = "button", e.dataset.projection = "", e.setAttribute("aria-label", "Orthographic projection"), this.showProjection();
|
|
258
|
+
}
|
|
259
|
+
if (d && this.setHome(d === !0 ? void 0 : d), f) {
|
|
260
|
+
this.lift();
|
|
261
|
+
for (let [e, t, n, r] of [[
|
|
262
|
+
1,
|
|
263
|
+
"↶",
|
|
264
|
+
"Roll the view counter-clockwise",
|
|
265
|
+
22
|
|
266
|
+
], [
|
|
267
|
+
-1,
|
|
268
|
+
"↷",
|
|
269
|
+
"Roll the view clockwise",
|
|
270
|
+
0
|
|
271
|
+
]]) {
|
|
272
|
+
let i = y("button", b, "mvc-roll");
|
|
273
|
+
i.type = "button", i.dataset.roll = String(e), i.title = n, i.setAttribute("aria-label", n), i.textContent = t, i.style.right = r + "px";
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
b.addEventListener("click", this.onClick), b.addEventListener("keydown", this.onKey);
|
|
277
|
+
for (let e of [
|
|
278
|
+
"pointerdown",
|
|
279
|
+
"pointermove",
|
|
280
|
+
"pointerup",
|
|
281
|
+
"pointercancel"
|
|
282
|
+
]) b.addEventListener(e, this.onPointer);
|
|
283
|
+
this.position = t.style.position, !this.position && v.defaultView?.getComputedStyle(t).position === "static" && (t.style.position = "relative"), e.use(this.plugin = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
|
|
284
|
+
name: this.name,
|
|
285
|
+
setup() {},
|
|
286
|
+
onPreUpdate: (e) => this.update(e),
|
|
287
|
+
registerPasses: (e) => {
|
|
288
|
+
e.add({
|
|
289
|
+
name: this.name + ":sync",
|
|
290
|
+
order: 1e4,
|
|
291
|
+
execute: () => this.sync()
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
dispose: () => this.dispose()
|
|
295
|
+
}), this.sync();
|
|
296
|
+
}
|
|
297
|
+
build(e, t, n) {
|
|
298
|
+
let r = (t, n, r) => {
|
|
299
|
+
let i = e.ownerDocument.createElement(t);
|
|
300
|
+
return r && (i.className = r), n.appendChild(i), i;
|
|
301
|
+
};
|
|
302
|
+
for (let i of Object.keys(a)) {
|
|
303
|
+
let [o, s, c] = a[i], l = r("div", e, "mvc-face"), d = n / 2;
|
|
304
|
+
l.style.transform = f([
|
|
305
|
+
...s,
|
|
306
|
+
0,
|
|
307
|
+
...c.map((e) => -e),
|
|
308
|
+
0,
|
|
309
|
+
...o,
|
|
310
|
+
0,
|
|
311
|
+
...o.map((e) => e * d),
|
|
312
|
+
1
|
|
313
|
+
]);
|
|
314
|
+
for (let e = 0; e < 9; e++) {
|
|
315
|
+
let n = 1 - Math.floor(e / 3), a = e % 3 - 1, d = o.map((e, t) => e + a * s[t] + n * c[t]), f = u(d, t), p = e === 4, m = r(p ? "button" : "div", l);
|
|
316
|
+
m.dataset.view = d.join(), m.title = f, p ? (m.type = "button", this.buttons.push([m, o]), m.textContent = t[i], m.setAttribute("aria-label", f + " view")) : m.setAttribute("aria-hidden", "true");
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
draw(e, t, n, r) {
|
|
321
|
+
this.cube.style.transform = r;
|
|
322
|
+
for (let [e, t] of this.buttons) e.tabIndex = t[0] * n[0] + t[1] * n[1] + t[2] * n[2] > .05 ? 0 : -1;
|
|
323
|
+
}
|
|
324
|
+
view(e, t = this.duration) {
|
|
325
|
+
let n = typeof e == "string" ? e === "iso" ? [
|
|
326
|
+
1,
|
|
327
|
+
1,
|
|
328
|
+
1
|
|
329
|
+
] : a[e]?.[0] : e;
|
|
330
|
+
if (!n || n.length !== 3 || !n.every(Number.isFinite) || !Math.hypot(...n)) throw RangeError("Invalid view; use a face, 'iso' or [x, y, z]");
|
|
331
|
+
this.orbit(...l(n), t);
|
|
332
|
+
}
|
|
333
|
+
setHome(e = this.camera) {
|
|
334
|
+
let t = [...e.position], n = [...e.target];
|
|
335
|
+
if (t.length !== 3 || n.length !== 3 || ![...t, ...n].every(Number.isFinite) || !Math.hypot(...t.map((e, t) => e - n[t]))) throw RangeError("Invalid home; use a position and a target apart, three finite numbers each");
|
|
336
|
+
if (this.homeView = {
|
|
337
|
+
position: t,
|
|
338
|
+
target: n
|
|
339
|
+
}, this.homeButton) return;
|
|
340
|
+
let r = this.homeButton = this.element.ownerDocument.createElement("button");
|
|
341
|
+
r.type = "button", r.className = "mvc-home", r.dataset.home = "", r.title = "Home", r.setAttribute("aria-label", "Home view"), r.textContent = "⌂", this.element.appendChild(r), this.lift();
|
|
342
|
+
}
|
|
343
|
+
getHome() {
|
|
344
|
+
let e = this.homeView;
|
|
345
|
+
return e && {
|
|
346
|
+
position: [...e.position],
|
|
347
|
+
target: [...e.target]
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
home(e = this.duration) {
|
|
351
|
+
let t = this.homeView;
|
|
352
|
+
if (!t) return this.view("iso", e);
|
|
353
|
+
let n = this.local(t.position.map((e, n) => e - t.target[n]));
|
|
354
|
+
this.orbit(...l(n), e, t.target, Math.hypot(...n));
|
|
355
|
+
}
|
|
356
|
+
roll(e = 1, t = this.duration) {
|
|
357
|
+
if (!Number.isFinite(e)) throw RangeError("Invalid roll; use a number of quarter turns");
|
|
358
|
+
this.orbit(...this.current(), t, void 0, void 0, this.twist + e * Math.PI / 2);
|
|
359
|
+
}
|
|
360
|
+
get turns() {
|
|
361
|
+
return this.twist / (Math.PI / 2);
|
|
362
|
+
}
|
|
363
|
+
setTheme(e) {
|
|
364
|
+
let i = typeof e == "string" ? e === "auto" ? {} : t[e] : e;
|
|
365
|
+
if (!i || typeof i != "object" || n.some((e) => i[e] !== void 0 && typeof i[e] != "string")) throw RangeError("Invalid theme; use 'light', 'dark', 'high-contrast', 'auto' or colour strings");
|
|
366
|
+
this.element.className = e === "auto" ? "mvc mvc-auto" : "mvc";
|
|
367
|
+
for (let e of n) i[e] ? this.element.style.setProperty(r(e), i[e]) : this.element.style.removeProperty(r(e));
|
|
368
|
+
}
|
|
369
|
+
getProjection() {
|
|
370
|
+
return this.projection?.mode;
|
|
371
|
+
}
|
|
372
|
+
setProjection(t) {
|
|
373
|
+
if (!this.projection) throw new e("state", "ViewCube has no projection; pass options.projection");
|
|
374
|
+
if (t !== "orthographic" && t !== "perspective") throw RangeError("Invalid projection; use 'orthographic' or 'perspective'");
|
|
375
|
+
this.projection.mode = t, this.showProjection();
|
|
376
|
+
}
|
|
377
|
+
update(e) {
|
|
378
|
+
let t = this.animation, n = this.camera;
|
|
379
|
+
if (!t || t.at && Math.hypot(...n.position.map((e, n) => e - t.at[n])) > 1e-6 * t.radius) {
|
|
380
|
+
this.animation = void 0, this.pole();
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
let r = t.duration ? Math.min(1, (t.elapsed += Math.max(0, e)) / t.duration) : 1, i = r * r * (3 - 2 * r), a = t.theta + t.dTheta * i;
|
|
384
|
+
this.twist = r === 1 ? (Math.round((t.twist + t.dTwist) / (Math.PI / 2)) % 4 + 4) % 4 * (Math.PI / 2) : t.twist + t.dTwist * i, t.to && (n.target = t.from.map((e, n) => r === 1 ? t.to[n] : e + (t.to[n] - e) * i)), t.at = this.place(a, t.phi + t.dPhi * i, r === 1 ? t.radius + t.dRadius : t.radius + t.dRadius * i), this.pole(a), r === 1 && (this.animation = void 0);
|
|
385
|
+
}
|
|
386
|
+
get animating() {
|
|
387
|
+
return !!this.animation;
|
|
388
|
+
}
|
|
389
|
+
sync() {
|
|
390
|
+
if (this.disposed) return;
|
|
391
|
+
this.toggle && this.toggle.getAttribute("aria-pressed") !== String(this.projection.mode === "orthographic") && this.showProjection();
|
|
392
|
+
let e = this.camera, t = this.local(e.position.map((t, n) => t - e.target[n])), n = Math.hypot(...t);
|
|
393
|
+
if (!n) return;
|
|
394
|
+
let [r, i, a] = t.map((e) => e / n), o = this.local(e.up ?? [
|
|
395
|
+
0,
|
|
396
|
+
1,
|
|
397
|
+
0
|
|
398
|
+
]), s = [
|
|
399
|
+
o[1] * a - o[2] * i,
|
|
400
|
+
o[2] * r - o[0] * a,
|
|
401
|
+
o[0] * i - o[1] * r
|
|
402
|
+
], c = Math.hypot(...s), [l, u, d] = c ? s.map((e) => e / c) : [
|
|
403
|
+
1,
|
|
404
|
+
0,
|
|
405
|
+
0
|
|
406
|
+
], p = i * d - a * u, m = a * l - r * d, h = r * u - i * l, g = f([
|
|
407
|
+
l,
|
|
408
|
+
-p,
|
|
409
|
+
r,
|
|
410
|
+
0,
|
|
411
|
+
u,
|
|
412
|
+
-m,
|
|
413
|
+
i,
|
|
414
|
+
0,
|
|
415
|
+
d,
|
|
416
|
+
-h,
|
|
417
|
+
a,
|
|
418
|
+
0,
|
|
419
|
+
0,
|
|
420
|
+
0,
|
|
421
|
+
0,
|
|
422
|
+
1
|
|
423
|
+
]);
|
|
424
|
+
g !== this.last && this.draw([
|
|
425
|
+
l,
|
|
426
|
+
u,
|
|
427
|
+
d
|
|
428
|
+
], [
|
|
429
|
+
p,
|
|
430
|
+
m,
|
|
431
|
+
h
|
|
432
|
+
], [
|
|
433
|
+
r,
|
|
434
|
+
i,
|
|
435
|
+
a
|
|
436
|
+
], this.last = g);
|
|
437
|
+
}
|
|
438
|
+
dispose() {
|
|
439
|
+
if (this.disposed) return;
|
|
440
|
+
this.disposed = !0, this.animation = void 0;
|
|
441
|
+
let e = this.camera;
|
|
442
|
+
if (this.upSet && e.up === this.upSet) {
|
|
443
|
+
let [t, n] = this.current(), r = this.local(e.position.map((t, n) => t - e.target[n]));
|
|
444
|
+
e.up = this.upPrev, !r[0] && !r[2] && this.place(t, n ? Math.PI - 1e-6 : 1e-6, Math.hypot(...r));
|
|
445
|
+
}
|
|
446
|
+
this.element.removeEventListener("click", this.onClick), this.element.removeEventListener("keydown", this.onKey);
|
|
447
|
+
for (let e of [
|
|
448
|
+
"pointerdown",
|
|
449
|
+
"pointermove",
|
|
450
|
+
"pointerup",
|
|
451
|
+
"pointercancel"
|
|
452
|
+
]) this.element.removeEventListener(e, this.onPointer);
|
|
453
|
+
this.grab = void 0, this.element.remove(), this.container.style.position = this.position, !this.engine.disposed && this.plugin && this.engine.sceneOf(this.plugin)?.removePlugin(this.name);
|
|
454
|
+
}
|
|
455
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
456
|
+
this.dispose();
|
|
457
|
+
}
|
|
458
|
+
place(e, t, n) {
|
|
459
|
+
let r = this.camera.target, i = t < s ? 1 : t > Math.PI - s ? -1 : 0, a = this.world(i ? [
|
|
460
|
+
0,
|
|
461
|
+
i * n,
|
|
462
|
+
0
|
|
463
|
+
] : [
|
|
464
|
+
n * Math.sin(t) * Math.sin(e),
|
|
465
|
+
n * Math.cos(t),
|
|
466
|
+
n * Math.sin(t) * Math.cos(e)
|
|
467
|
+
]);
|
|
468
|
+
return this.camera.position = a.map((e, t) => r[t] + e);
|
|
469
|
+
}
|
|
470
|
+
pole(e) {
|
|
471
|
+
let t = this.camera, n = t.position.map((e, n) => e - t.target[n]), r = this.local(n);
|
|
472
|
+
this.upSet && t.up !== this.upSet && (this.upSet = void 0, this.twist = 0);
|
|
473
|
+
let i = this.upSet ? this.upPrev : t.up, a = this.local(i), o = a;
|
|
474
|
+
if (!r[0] && !r[2] && r[1]) {
|
|
475
|
+
if (e === void 0) return;
|
|
476
|
+
let t = Math.sign(r[1]);
|
|
477
|
+
if (this.upSet || !Math.hypot(a[0], a[2])) o = [
|
|
478
|
+
-t * Math.sin(e),
|
|
479
|
+
0,
|
|
480
|
+
-t * Math.cos(e)
|
|
481
|
+
], this.az = e;
|
|
482
|
+
else if (this.spin && this.projection?.up === i) {
|
|
483
|
+
d(this.projection.up = [
|
|
484
|
+
0,
|
|
485
|
+
0,
|
|
486
|
+
0
|
|
487
|
+
], this.world([
|
|
488
|
+
-t * Math.sin(e),
|
|
489
|
+
0,
|
|
490
|
+
-t * Math.cos(e)
|
|
491
|
+
]), n, -this.twist), this.az = e;
|
|
492
|
+
return;
|
|
493
|
+
} else this.az = Math.atan2(-t * a[0], -t * a[2]);
|
|
494
|
+
}
|
|
495
|
+
o === a && !this.twist ? this.upSet &&= (t.up = i, void 0) : (this.upSet || (this.upPrev = t.up, t.up = this.upSet = [
|
|
496
|
+
0,
|
|
497
|
+
0,
|
|
498
|
+
0
|
|
499
|
+
]), d(this.upSet, this.world(o), n, -this.twist));
|
|
500
|
+
}
|
|
501
|
+
current() {
|
|
502
|
+
let e = this.camera, t = this.local(e.position.map((t, n) => t - e.target[n])), n = this.local(e.up), r = l(t);
|
|
503
|
+
return !t[0] && !t[2] && t[1] && (this.upSet && e.up === this.upSet ? r[0] = this.az : Math.hypot(n[0], n[2]) && (r[0] = t[1] > 0 ? Math.atan2(-n[0], -n[2]) : Math.atan2(n[0], n[2]))), r;
|
|
504
|
+
}
|
|
505
|
+
orbit(e, t, n, r, i, a = 0) {
|
|
506
|
+
let o = this.camera, s = Math.hypot(...o.position.map((e, t) => e - o.target[t])), [c, l] = this.current();
|
|
507
|
+
o.cancelFit?.(), this.animation = {
|
|
508
|
+
theta: c,
|
|
509
|
+
phi: l,
|
|
510
|
+
dTheta: ((e - c) % (2 * Math.PI) + 3 * Math.PI) % (2 * Math.PI) - Math.PI,
|
|
511
|
+
dPhi: t - l,
|
|
512
|
+
radius: s,
|
|
513
|
+
dRadius: (i ?? s) - s,
|
|
514
|
+
from: [...o.target],
|
|
515
|
+
to: r && [...r],
|
|
516
|
+
twist: this.twist,
|
|
517
|
+
dTwist: a - this.twist,
|
|
518
|
+
elapsed: 0,
|
|
519
|
+
duration: n
|
|
520
|
+
}, n || this.update(0);
|
|
521
|
+
}
|
|
522
|
+
showProjection() {
|
|
523
|
+
let e = this.toggle, t = this.projection.mode === "orthographic";
|
|
524
|
+
e.textContent = t ? "Ortho" : "Persp", e.setAttribute("aria-pressed", String(t));
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
//#endregion
|
|
528
|
+
export { t as THEMES, m as ViewCube };
|
package/dist/up.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import { ViewCube, type ViewCubeOptions } from './index.js';
|
|
3
|
+
type Vector = readonly [number, number, number];
|
|
4
|
+
/** A positive world axis or any finite nonzero world direction. */
|
|
5
|
+
export type UpAxis = 'x' | 'y' | 'z' | Vector;
|
|
6
|
+
/** World frame and inherited appearance/navigation options for {@link UpViewCube}. */
|
|
7
|
+
export interface UpViewCubeOptions extends ViewCubeOptions {
|
|
8
|
+
/** Optional projection; a CameraProjection's pole up is aligned to the widget's front. */
|
|
9
|
+
projection?: {
|
|
10
|
+
mode: 'orthographic' | 'perspective';
|
|
11
|
+
up?: [number, number, number];
|
|
12
|
+
};
|
|
13
|
+
/** World up. Default 'z'. Named views and tuple views use the resulting widget frame. */
|
|
14
|
+
up?: UpAxis;
|
|
15
|
+
/** World direction toward the front camera, projected perpendicular to up.
|
|
16
|
+
* Default +Z, or -Y when up is nearly parallel to +Z. Must not be parallel to up. */
|
|
17
|
+
front?: Vector;
|
|
18
|
+
}
|
|
19
|
+
/** A ViewCube whose entire navigation and display use a configurable world frame. */
|
|
20
|
+
export declare class UpViewCube extends ViewCube {
|
|
21
|
+
private frame?;
|
|
22
|
+
static attach(engine: EngineKernel, options?: UpViewCubeOptions): UpViewCube;
|
|
23
|
+
constructor(engine: EngineKernel, { up, front, ...options }?: UpViewCubeOptions);
|
|
24
|
+
/** Maps a world direction into the cube's right, up and front coordinates. */
|
|
25
|
+
local(direction: readonly number[]): readonly number[];
|
|
26
|
+
/** Maps a cube direction to world space. */
|
|
27
|
+
world(direction: readonly number[]): readonly number[];
|
|
28
|
+
}
|
|
29
|
+
export {};
|
package/dist/up.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { ViewCube as e } from "./index.js";
|
|
5
|
+
//#region src/up.ts
|
|
6
|
+
var t = (e, t) => e.reduce((e, n, r) => e + n * t[r], 0), n = (e) => {
|
|
7
|
+
let t = Math.hypot(...e);
|
|
8
|
+
if (e.length !== 3 || !e.every(Number.isFinite) || !Number.isFinite(t) || !t) throw RangeError("View cube frame needs finite nonzero directions");
|
|
9
|
+
return e.map((e) => e / t);
|
|
10
|
+
}, r = class r extends e {
|
|
11
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
12
|
+
return new r(e, t);
|
|
13
|
+
}
|
|
14
|
+
constructor(e, { up: r = "z", front: i, ...a } = {}) {
|
|
15
|
+
let o = n(typeof r == "string" ? {
|
|
16
|
+
x: [
|
|
17
|
+
1,
|
|
18
|
+
0,
|
|
19
|
+
0
|
|
20
|
+
],
|
|
21
|
+
y: [
|
|
22
|
+
0,
|
|
23
|
+
1,
|
|
24
|
+
0
|
|
25
|
+
],
|
|
26
|
+
z: [
|
|
27
|
+
0,
|
|
28
|
+
0,
|
|
29
|
+
1
|
|
30
|
+
]
|
|
31
|
+
}[r] ?? [] : r), s = n(i ?? (Math.abs(o[2]) < .9 ? [
|
|
32
|
+
0,
|
|
33
|
+
0,
|
|
34
|
+
1
|
|
35
|
+
] : [
|
|
36
|
+
0,
|
|
37
|
+
-1,
|
|
38
|
+
0
|
|
39
|
+
])), c = s.map((e, n) => e - t(s, o) * o[n]);
|
|
40
|
+
if (Math.hypot(...c) < 1e-10) throw RangeError("Front must not be parallel to up");
|
|
41
|
+
let l = n(c), u = [
|
|
42
|
+
o[1] * l[2] - o[2] * l[1],
|
|
43
|
+
o[2] * l[0] - o[0] * l[2],
|
|
44
|
+
o[0] * l[1] - o[1] * l[0]
|
|
45
|
+
];
|
|
46
|
+
super(e, a), this.frame = [
|
|
47
|
+
u,
|
|
48
|
+
o,
|
|
49
|
+
l
|
|
50
|
+
];
|
|
51
|
+
let d = a.projection ?? e.plugins?.get("cameras");
|
|
52
|
+
d && "up" in d && (d.up = l.map((e) => -e)), this.camera.up = [...o], this.view(this.local(this.camera.position.map((e, t) => e - this.camera.target[t])), 0), this.sync();
|
|
53
|
+
}
|
|
54
|
+
local(e) {
|
|
55
|
+
if (!this.frame) return e;
|
|
56
|
+
let n = Math.hypot(...e);
|
|
57
|
+
return this.frame.map((r) => {
|
|
58
|
+
let i = t(r, e);
|
|
59
|
+
return Math.abs(i) < 1e-14 * n ? 0 : i;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
world(e) {
|
|
63
|
+
return this.frame ? [
|
|
64
|
+
0,
|
|
65
|
+
1,
|
|
66
|
+
2
|
|
67
|
+
].map((t) => this.frame.reduce((n, r, i) => n + r[t] * e[i], 0)) : e;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
export { r as UpViewCube };
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@margoengine/viewcube",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CAD view cube: shows the camera orientation and animates to top, front, side and isometric views, with a perspective/orthographic toggle.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"margoengine",
|
|
7
|
+
"webgpu",
|
|
8
|
+
"3d",
|
|
9
|
+
"viewcube"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://margoengine.com",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/scottcampbelldata/margoengine/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/scottcampbelldata/margoengine.git",
|
|
18
|
+
"directory": "packages/viewcube"
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"author": "Scott Campbell",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./axes": {
|
|
30
|
+
"types": "./dist/axes.d.ts",
|
|
31
|
+
"import": "./dist/axes.js"
|
|
32
|
+
},
|
|
33
|
+
"./up": {
|
|
34
|
+
"types": "./dist/up.d.ts",
|
|
35
|
+
"import": "./dist/up.js"
|
|
36
|
+
},
|
|
37
|
+
"./fit": {
|
|
38
|
+
"types": "./dist/fit.d.ts",
|
|
39
|
+
"import": "./dist/fit.js"
|
|
40
|
+
},
|
|
41
|
+
"./compass": {
|
|
42
|
+
"types": "./dist/compass.d.ts",
|
|
43
|
+
"import": "./dist/compass.js"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"LICENSE",
|
|
49
|
+
"NOTICE"
|
|
50
|
+
],
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@margoengine/core": "^1.0.0"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@margoengine/math": "^1.0.0"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "node ../../scripts/build-optional.js viewcube",
|
|
62
|
+
"test": "npm run build && node --test test/*.test.js",
|
|
63
|
+
"prepack": "npm run build",
|
|
64
|
+
"test:browser": "cd ../.. && node packages/viewcube/test/browser-check.mjs"
|
|
65
|
+
}
|
|
66
|
+
}
|