@mark1russell7/visuals 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +129 -2
- package/build/index.d.ts +64 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1 -0
- package/build/motion-Ba4IIfAV.js +338 -0
- package/build/motion-Ba4IIfAV.js.map +1 -0
- package/build/theme-eOmfx75p.d.ts +177 -0
- package/build/theme-eOmfx75p.d.ts.map +1 -0
- package/build/three.d.ts +126 -0
- package/build/three.d.ts.map +1 -0
- package/build/three.js +397 -0
- package/build/three.js.map +1 -0
- package/package.json +77 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Russell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,130 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mark1russell7/visuals
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
3D stages for data. A stage is one canvas of three.js with a camera that a person moves. The content of the stage is your data.
|
|
4
|
+
|
|
5
|
+
The package has two entries:
|
|
6
|
+
|
|
7
|
+
- The entry `.` has the camera maths: views, places, moves, keys and marks. It needs no browser, and it does not need `three`.
|
|
8
|
+
- The entry `/three` has the stage. It needs the package `three`, version 0.187.0 or later.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @mark1russell7/visuals three
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## A stage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { fitView } from "@mark1russell7/visuals";
|
|
20
|
+
import { createStage, type StageContent } from "@mark1russell7/visuals/three";
|
|
21
|
+
import { BoxGeometry, Mesh, MeshStandardMaterial } from "three";
|
|
22
|
+
|
|
23
|
+
const material = new MeshStandardMaterial();
|
|
24
|
+
const box = new Mesh(new BoxGeometry(2, 1, 3), material);
|
|
25
|
+
|
|
26
|
+
const content: StageContent<string> = {
|
|
27
|
+
objects: [box],
|
|
28
|
+
extent: {
|
|
29
|
+
center: { x: 0, y: 0, z: 0 },
|
|
30
|
+
corners: [-1, 1].flatMap((x) => [-0.5, 0.5].flatMap((y) => [-1.5, 1.5].map((z) => ({ x, y, z })))),
|
|
31
|
+
},
|
|
32
|
+
has: (record) => record === "box",
|
|
33
|
+
pick: (ray) => (ray.intersectObject(box).length > 0 ? { record: "box", value: "the box" } : undefined),
|
|
34
|
+
paint: (_theme, mark) => void material.color.set(mark("box") === 0 ? "#4f7cac" : "#e0a03a"),
|
|
35
|
+
dispose: () => {
|
|
36
|
+
box.geometry.dispose();
|
|
37
|
+
material.dispose();
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const stage = createStage(document.body, {
|
|
42
|
+
content,
|
|
43
|
+
theme: { sky: "#ffffff", ground: "#8a8f98" },
|
|
44
|
+
label: "A box that turns",
|
|
45
|
+
views: { home: fitView({ polar: 0.9, azimuth: 0.6 }), top: fitView({ polar: 0, azimuth: 0 }) },
|
|
46
|
+
home: "home",
|
|
47
|
+
keys: { t: "top" },
|
|
48
|
+
onSelect: (value) => console.log(value), // => "the box" after a click on the box
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`createStage` gives `undefined` for a browser with no WebGL 2. It does not throw. Then show a different picture of the same data.
|
|
53
|
+
|
|
54
|
+
## What a stage does
|
|
55
|
+
|
|
56
|
+
- A drag turns the content. The wheel changes the distance after a click in the picture, so the page scrolls until then.
|
|
57
|
+
- The arrow keys turn the content, and the keys `+` and `-` change the distance. The key `Home` shows the first view.
|
|
58
|
+
- A click selects the record under the pointer. The stage tells the page with `onSelect` and `onHover`.
|
|
59
|
+
- A view has a name. `setView` moves the camera to a view, with a short move.
|
|
60
|
+
- The stage makes a picture only after a change. A page with no motion uses no processor time.
|
|
61
|
+
- A new size of the container keeps the view, until a person moves the camera.
|
|
62
|
+
- The stage makes no move for a person who asks for less motion.
|
|
63
|
+
|
|
64
|
+
## The content
|
|
65
|
+
|
|
66
|
+
A stage knows nothing about the data. The content gives it these parts:
|
|
67
|
+
|
|
68
|
+
| Part | What it gives |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `objects` | The three.js objects of the content. |
|
|
71
|
+
| `extent` | The space that the content fills: the point that the camera looks at, and the corners that a view shows. |
|
|
72
|
+
| `pick` | The record that a ray from the camera finds first. |
|
|
73
|
+
| `paint` | The colors of each part, from the theme and the mark of its record. |
|
|
74
|
+
| `step` | The parts that change with the camera or with the time, before each picture. It is optional. |
|
|
75
|
+
| `dispose` | The release of the memory of the graphics card. |
|
|
76
|
+
|
|
77
|
+
A record has a name. The mark of a record is 0, 1 or 2: no mark, a record under the pointer, the selected record. `highlight` and `select` set the marks from the page.
|
|
78
|
+
|
|
79
|
+
## Views
|
|
80
|
+
|
|
81
|
+
A view is a function that gives the place of the camera for a picture. So a view fits each size of the picture.
|
|
82
|
+
|
|
83
|
+
- `fitView(angles)` shows each corner of the content from one direction.
|
|
84
|
+
- `capView(angles, angle)` shows a part of a ball: a cap around the point below the camera.
|
|
85
|
+
- Your own function gives a place from the extent, the angle of the picture and its shape.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { fitView, type StageView } from "@mark1russell7/visuals";
|
|
89
|
+
|
|
90
|
+
const front: StageView = fitView({ polar: Math.PI / 2, azimuth: 0 });
|
|
91
|
+
const near: StageView = (frame) => ({ ...front(frame), distance: 4 });
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## A ball
|
|
95
|
+
|
|
96
|
+
An extent with the field `surface` has a ball around its center, for example a globe. The camera stays out of the ball.
|
|
97
|
+
|
|
98
|
+
- The turn and the zoom become slower near the surface. So a small region does not go out of the picture with one drag.
|
|
99
|
+
- A move between two views changes the height above the surface by a constant factor in each step.
|
|
100
|
+
- `capView` shows a cap of the ball. A cap of half of the ball shows all of the ball.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { capView, type StageExtent } from "@mark1russell7/visuals";
|
|
104
|
+
|
|
105
|
+
const globe: StageExtent = { center: { x: 0, y: 0, z: 0 }, corners: [], surface: 1 };
|
|
106
|
+
const all = capView({ polar: 1, azimuth: 0.4 }, Math.PI / 2);
|
|
107
|
+
const region = capView({ polar: 0.8, azimuth: -1.3 }, 0.05);
|
|
108
|
+
|
|
109
|
+
all({ extent: globe, fieldOfView: 32, aspect: 1.5 }).distance; // => 3.79: the outline of the ball fills the height
|
|
110
|
+
region({ extent: globe, fieldOfView: 32, aspect: 1.5 }).distance; // => 1.18: a cap of 0.05 radians fills the height
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## The camera maths
|
|
114
|
+
|
|
115
|
+
The entry `.` has pure functions. A page with no WebGL 2 and a test in Node.js use the same functions as the stage.
|
|
116
|
+
|
|
117
|
+
| Function | What it gives |
|
|
118
|
+
| --- | --- |
|
|
119
|
+
| `viewAxes` | The three axes of the camera of a view. |
|
|
120
|
+
| `fitDistance`, `capDistance` | The distance at which the camera shows points, or a cap of a ball. |
|
|
121
|
+
| `cameraPosition`, `viewOf` | The position of a camera from its place, and the place from its position. |
|
|
122
|
+
| `placeBetween`, `moveAt`, `easeInOut` | A place on the way between two places, and the place at a time of a move. |
|
|
123
|
+
| `clampPlace`, `stageLimits` | A place in the limits of a stage, and the limits from the content. |
|
|
124
|
+
| `keyAction` | The action of a key. |
|
|
125
|
+
| `markOf`, `isClick` | The mark of a record, and the difference between a click and a drag. |
|
|
126
|
+
| `approach` | One step of a value to its goal, for a part that changes with time. |
|
|
127
|
+
|
|
128
|
+
The space of a stage has the y axis up. A place has a target, a polar angle from the y axis, an azimuth around it and a distance.
|
|
129
|
+
|
|
130
|
+
The license is MIT.
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { C as clampPlace, D as turnBetween, E as speedAt, O as viewAxes, S as capDistance, T as placeBetween, _ as Place, a as NO_MARKS, b as ViewAxes, c as FIT_MARGIN, d as StageView, f as ViewFrame, g as surfaceOf, h as stageLimits, i as Marks, k as viewOf, l as StageExtent, m as fitView, n as CLICK_DISTANCE, o as isClick, p as capView, r as Mark, s as markOf, t as StageTheme, u as StageLimits, v as PlaceLimits, w as fitDistance, x as cameraPosition, y as ViewAngles } from "./theme-eOmfx75p.js";
|
|
2
|
+
/** The angle of one step of an arrow key, in radians, for a camera far from the surface. */
|
|
3
|
+
export declare const KEY_TURN: number;
|
|
4
|
+
/** The factor of the height of a camera above the surface for one step of the key `+`. */
|
|
5
|
+
export declare const KEY_ZOOM = .85;
|
|
6
|
+
/** What a key does on a stage: it moves the camera to a place, or it shows a view. */
|
|
7
|
+
type KeyAction = {
|
|
8
|
+
readonly type: "place";
|
|
9
|
+
readonly place: Place;
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: "view";
|
|
12
|
+
readonly view: string;
|
|
13
|
+
};
|
|
14
|
+
/** The keys of a stage. */
|
|
15
|
+
interface KeyOptions {
|
|
16
|
+
/** The name of the view of the keys `Home` and `0`. */
|
|
17
|
+
readonly home: string;
|
|
18
|
+
/** The name of the view of each other key, for example `{ t: "top" }`. A letter has no case. */
|
|
19
|
+
readonly views?: Readonly<Record<string, string>> | undefined;
|
|
20
|
+
/** The radius of the ball around the target that the camera stays out of, or 0 for no ball. */
|
|
21
|
+
readonly surface?: number | undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* This function gives the action of a key on a stage, or `undefined` for a key with no action.
|
|
25
|
+
*
|
|
26
|
+
* The arrow keys turn the camera around the target. The keys `+` and `-` change the height above the surface.
|
|
27
|
+
* The keys `Home` and `0` show the home view. Near the surface, a step of an arrow key is smaller.
|
|
28
|
+
* The result has no limits: the stage puts the place in its limits.
|
|
29
|
+
*/
|
|
30
|
+
export declare function keyAction(key: string, now: Place, options: KeyOptions): KeyAction | undefined;
|
|
31
|
+
/** A move of a camera from one place to a different place. */
|
|
32
|
+
interface Move {
|
|
33
|
+
readonly from: Place;
|
|
34
|
+
readonly to: Place;
|
|
35
|
+
/** The time of the start of the move, in milliseconds. */
|
|
36
|
+
readonly start: number;
|
|
37
|
+
/** The time that the move takes, in milliseconds. */
|
|
38
|
+
readonly duration: number;
|
|
39
|
+
}
|
|
40
|
+
/** The place of a camera at one time of a move. */
|
|
41
|
+
interface MoveStep {
|
|
42
|
+
readonly place: Place;
|
|
43
|
+
/** This value is true at the end of the move, and after it. */
|
|
44
|
+
readonly done: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* This function gives a part of a move with a slow start and a slow end. `part` goes from 0 to 1.
|
|
48
|
+
* The result goes from 0 to 1, and it is 0.5 at the middle.
|
|
49
|
+
*/
|
|
50
|
+
export declare function easeInOut(part: number): number;
|
|
51
|
+
/**
|
|
52
|
+
* This function gives the place of a camera at the time `now` of a move, in milliseconds.
|
|
53
|
+
* A move with no duration is at its end immediately. `surface` is the radius of the ball around the target, or 0 for no ball.
|
|
54
|
+
*/
|
|
55
|
+
export declare function moveAt(move: Move, now: number, surface?: number): MoveStep;
|
|
56
|
+
/**
|
|
57
|
+
* This function moves a value one step to its goal. Each step goes a constant part of the way that remains.
|
|
58
|
+
*
|
|
59
|
+
* `seconds` is the time of the step, and `time` is the time in which the value goes most of the way, in seconds.
|
|
60
|
+
* A step with no time gives the goal. A value nearer to the goal than `snap` becomes the goal, so the steps have an end.
|
|
61
|
+
*/
|
|
62
|
+
export declare function approach(value: number, goal: number, seconds: number, time: number, snap?: number): number;
|
|
63
|
+
export { CLICK_DISTANCE, FIT_MARGIN, type KeyAction, type KeyOptions, type Mark, type Marks, type Move, type MoveStep, NO_MARKS, type Place, type PlaceLimits, type StageExtent, type StageLimits, type StageTheme, type StageView, type ViewAngles, type ViewAxes, type ViewFrame, cameraPosition, capDistance, capView, clampPlace, fitDistance, fitView, isClick, markOf, placeBetween, speedAt, stageLimits, surfaceOf, turnBetween, viewAxes, viewOf };
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/keys.ts","../src/motion.ts"],"mappings":";;qBAGa;;qBAGA;;KAGD;WAAuB;WAAwB,OAAO;;WAAqB;WAAuB;;;UAG7F;;WAEN;;WAEA,QAAQ,SAAS;;WAEjB;;;;;;;;;wBAUK,UAAU,aAAa,KAAK,OAAO,SAAS,aAAa;;UCzBxD;WACN,MAAM;WACN,IAAI;;WAEJ;;WAEA;;;UAIM;WACN,OAAO;;WAEP;;;;;;wBAOK,UAAU;;;;;wBASV,OAAO,MAAM,MAAM,aAAa,mBAAsB;;;;;;;wBAYtD,SAAS,eAAe,cAAc,iBAAiB,cAAc"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { i as CLICK_DISTANCE, d as FIT_MARGIN, c as KEY_TURN, l as KEY_ZOOM, a as NO_MARKS, t as approach, g as cameraPosition, _ as capDistance, f as capView, v as clampPlace, n as easeInOut, y as fitDistance, p as fitView, o as isClick, u as keyAction, s as markOf, r as moveAt, b as placeBetween, x as speedAt, m as stageLimits, h as surfaceOf, S as turnBetween, C as viewAxes, w as viewOf } from "./motion-Ba4IIfAV.js";
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// src/view.ts
|
|
2
|
+
/** This function gives the axes of the camera of a view. At a polar angle of 0, the top of the picture is the negative z axis. */
|
|
3
|
+
function viewAxes(view) {
|
|
4
|
+
const sinPolar = Math.sin(view.polar);
|
|
5
|
+
const cosPolar = Math.cos(view.polar);
|
|
6
|
+
const sinAzimuth = Math.sin(view.azimuth);
|
|
7
|
+
const cosAzimuth = Math.cos(view.azimuth);
|
|
8
|
+
return {
|
|
9
|
+
back: {
|
|
10
|
+
x: sinPolar * sinAzimuth,
|
|
11
|
+
y: cosPolar,
|
|
12
|
+
z: sinPolar * cosAzimuth
|
|
13
|
+
},
|
|
14
|
+
right: {
|
|
15
|
+
x: cosAzimuth,
|
|
16
|
+
y: 0,
|
|
17
|
+
z: 0 - sinAzimuth
|
|
18
|
+
},
|
|
19
|
+
up: {
|
|
20
|
+
x: 0 - cosPolar * sinAzimuth,
|
|
21
|
+
y: sinPolar,
|
|
22
|
+
z: 0 - cosPolar * cosAzimuth
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** This function gives the dot product of the vector from `origin` to `point` with `axis`. */
|
|
27
|
+
function along(point, origin, axis) {
|
|
28
|
+
return (point.x - origin.x) * axis.x + (point.y - origin.y) * axis.y + (point.z - origin.z) * axis.z;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* This function gives the smallest distance from `target` at which a camera shows each point.
|
|
32
|
+
*
|
|
33
|
+
* The camera looks at `target` along the direction of `view`. `fov` is the vertical angle of the picture in degrees,
|
|
34
|
+
* and `aspect` is the width of the picture divided by its height.
|
|
35
|
+
* The function gives 0 for no points, and for an angle or an aspect that makes no picture.
|
|
36
|
+
*/
|
|
37
|
+
function fitDistance(points, target, view, fov, aspect) {
|
|
38
|
+
const tanUp = Math.tan(fov * Math.PI / 360);
|
|
39
|
+
const tanRight = tanUp * aspect;
|
|
40
|
+
if (!(tanUp > 0) || !(tanRight > 0) || !Number.isFinite(tanRight)) return 0;
|
|
41
|
+
const axes = viewAxes(view);
|
|
42
|
+
let distance = 0;
|
|
43
|
+
for (const point of points) {
|
|
44
|
+
const depth = along(point, target, axes.back);
|
|
45
|
+
const needed = Math.max(Math.abs(along(point, target, axes.right)) / tanRight, Math.abs(along(point, target, axes.up)) / tanUp);
|
|
46
|
+
distance = Math.max(distance, depth + needed);
|
|
47
|
+
}
|
|
48
|
+
return distance;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* This function gives the smallest distance from the center of a ball at which a camera shows a cap of the ball.
|
|
52
|
+
*
|
|
53
|
+
* The camera looks at the center of the ball, and the center of the cap is on the line to the camera.
|
|
54
|
+
* `radius` is the radius of the ball. `angle` is the angle of the cap at the center of the ball, in radians, from 0 to π / 2.
|
|
55
|
+
* `fov` is the vertical angle of the picture in degrees, and `aspect` is the width of the picture divided by its height.
|
|
56
|
+
* The function gives 0 for an angle or an aspect that makes no picture.
|
|
57
|
+
*/
|
|
58
|
+
function capDistance(radius, angle, fov, aspect) {
|
|
59
|
+
const tanUp = Math.tan(fov * Math.PI / 360);
|
|
60
|
+
const tan = Math.min(tanUp, tanUp * aspect);
|
|
61
|
+
if (!(tan > 0) || !Number.isFinite(tan) || !(radius > 0)) return 0;
|
|
62
|
+
const cap = Math.min(Math.max(angle, 0), Math.PI / 2);
|
|
63
|
+
const half = Math.atan(tan);
|
|
64
|
+
if (cap >= Math.PI / 2 - half) return radius / Math.sin(half);
|
|
65
|
+
return radius * Math.cos(cap) + radius * Math.sin(cap) / tan;
|
|
66
|
+
}
|
|
67
|
+
/** This function gives the position of a camera at a distance from `target`, in the direction of `view`. */
|
|
68
|
+
function cameraPosition(target, view, distance) {
|
|
69
|
+
const { back } = viewAxes(view);
|
|
70
|
+
return {
|
|
71
|
+
x: target.x + back.x * distance,
|
|
72
|
+
y: target.y + back.y * distance,
|
|
73
|
+
z: target.z + back.z * distance
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** This function gives the angles of the view from `target` to a camera, and the distance between them. A camera at the target gives the angles 0. */
|
|
77
|
+
function viewOf(camera, target) {
|
|
78
|
+
const x = camera.x - target.x;
|
|
79
|
+
const y = camera.y - target.y;
|
|
80
|
+
const z = camera.z - target.z;
|
|
81
|
+
const distance = Math.hypot(x, y, z);
|
|
82
|
+
if (!(distance > 0)) return {
|
|
83
|
+
polar: 0,
|
|
84
|
+
azimuth: 0,
|
|
85
|
+
distance: 0
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
polar: Math.acos(Math.min(Math.max(y / distance, -1), 1)),
|
|
89
|
+
azimuth: Math.atan2(x, z),
|
|
90
|
+
distance
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** This function gives the angle between two azimuths along the shortest way, from −π to π. */
|
|
94
|
+
function turnBetween(from, to) {
|
|
95
|
+
const full = 2 * Math.PI;
|
|
96
|
+
const turn = ((to - from) % full + full) % full;
|
|
97
|
+
return turn > Math.PI ? turn - full : turn;
|
|
98
|
+
}
|
|
99
|
+
/** This function gives a place in its limits. The target and the azimuth stay as they are. */
|
|
100
|
+
function clampPlace(place, limits) {
|
|
101
|
+
return {
|
|
102
|
+
target: place.target,
|
|
103
|
+
azimuth: place.azimuth,
|
|
104
|
+
polar: Math.min(Math.max(place.polar, limits.minPolar), limits.maxPolar),
|
|
105
|
+
distance: Math.min(Math.max(place.distance, limits.minDistance), limits.maxDistance)
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* This function gives the height of a camera above the surface of a stage, as a part of its distance from the target.
|
|
110
|
+
*
|
|
111
|
+
* `surface` is the radius of the ball around the target that the camera stays out of. A stage with no ball has the value 0.
|
|
112
|
+
* The result is the factor of the turn speed and the zoom speed of a camera: near the surface, a camera moves slowly.
|
|
113
|
+
* The result is 0.02 at least, so a camera on the surface can move away.
|
|
114
|
+
*/
|
|
115
|
+
function speedAt(distance, surface) {
|
|
116
|
+
if (!(distance > 0)) return 1;
|
|
117
|
+
return Math.min(Math.max((distance - surface) / distance, .02), 1);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* This function gives the place at a part of the way between two places. `part` goes from 0 to 1.
|
|
121
|
+
*
|
|
122
|
+
* The azimuth goes along the shortest way. The height above the surface changes by an equal factor for each equal step,
|
|
123
|
+
* so a zoom has one speed in the picture. `surface` is the radius of the ball around the target, or 0 for no ball.
|
|
124
|
+
*/
|
|
125
|
+
function placeBetween(from, to, part, surface = 0) {
|
|
126
|
+
const between = (a, b) => a + (b - a) * part;
|
|
127
|
+
const low = from.distance - surface;
|
|
128
|
+
const high = to.distance - surface;
|
|
129
|
+
const height = low > 0 && high > 0 ? low * (high / low) ** part : between(low, high);
|
|
130
|
+
return {
|
|
131
|
+
target: {
|
|
132
|
+
x: between(from.target.x, to.target.x),
|
|
133
|
+
y: between(from.target.y, to.target.y),
|
|
134
|
+
z: between(from.target.z, to.target.z)
|
|
135
|
+
},
|
|
136
|
+
polar: between(from.polar, to.polar),
|
|
137
|
+
azimuth: from.azimuth + turnBetween(from.azimuth, to.azimuth) * part,
|
|
138
|
+
distance: surface + height
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// src/extent.ts
|
|
142
|
+
/** The space around the content in a view that shows all of it, as a factor of the distance. */
|
|
143
|
+
const FIT_MARGIN = 1.06;
|
|
144
|
+
/** This function gives the radius of the ball around the center of an extent, or 0 for no ball. */
|
|
145
|
+
function surfaceOf(extent) {
|
|
146
|
+
const surface = extent.surface ?? 0;
|
|
147
|
+
return surface > 0 ? surface : 0;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* This function makes a view that shows each corner of the content from one direction.
|
|
151
|
+
* `margin` is the space around the content, as a factor of the distance. An extent with no corners gives the distance 1.
|
|
152
|
+
*/
|
|
153
|
+
function fitView(angles, margin = FIT_MARGIN) {
|
|
154
|
+
return ({ extent, fieldOfView, aspect }) => {
|
|
155
|
+
const distance = fitDistance(extent.corners, extent.center, angles, fieldOfView, aspect) * margin;
|
|
156
|
+
return {
|
|
157
|
+
target: extent.center,
|
|
158
|
+
polar: angles.polar,
|
|
159
|
+
azimuth: angles.azimuth,
|
|
160
|
+
distance: distance > 0 ? distance : 1
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* This function makes a view of a part of the ball of the content: a cap around the point of the ball below the camera.
|
|
166
|
+
*
|
|
167
|
+
* `angles` is the direction from the center of the ball to the camera. `angle` is the angle of the cap at the center of the ball, in radians.
|
|
168
|
+
* `margin` is the space around the cap, as a factor of the height above the ball. An extent with no ball gives the distance 1.
|
|
169
|
+
*/
|
|
170
|
+
function capView(angles, angle, margin = FIT_MARGIN) {
|
|
171
|
+
return ({ extent, fieldOfView, aspect }) => {
|
|
172
|
+
const surface = surfaceOf(extent);
|
|
173
|
+
const height = Math.max(capDistance(surface, angle, fieldOfView, aspect) - surface, 0);
|
|
174
|
+
return {
|
|
175
|
+
target: extent.center,
|
|
176
|
+
polar: angles.polar,
|
|
177
|
+
azimuth: angles.azimuth,
|
|
178
|
+
distance: surface > 0 ? surface + height * margin : 1
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* This function gives the limits of the camera of a stage for a picture.
|
|
184
|
+
*
|
|
185
|
+
* The largest distance of the views is the base. The camera goes out to 3 times this distance, and in to 3 % of the height of that view.
|
|
186
|
+
* The extent can give each distance. The nearest depth is small against the height of the nearest camera, and the farthest depth is behind all the content.
|
|
187
|
+
*/
|
|
188
|
+
function stageLimits(frame, views, polar) {
|
|
189
|
+
const { extent } = frame;
|
|
190
|
+
const surface = surfaceOf(extent);
|
|
191
|
+
let widest = 0;
|
|
192
|
+
for (const view of views) widest = Math.max(widest, view(frame).distance);
|
|
193
|
+
if (!(widest > surface)) widest = surface + 1;
|
|
194
|
+
let reach = surface;
|
|
195
|
+
for (const corner of extent.corners) reach = Math.max(reach, Math.hypot(corner.x - extent.center.x, corner.y - extent.center.y, corner.z - extent.center.z));
|
|
196
|
+
const minDistance = extent.minDistance ?? surface + (widest - surface) * .03;
|
|
197
|
+
const maxDistance = Math.max(extent.maxDistance ?? surface + (widest - surface) * 3, minDistance);
|
|
198
|
+
const height = Math.max(minDistance - surface, 1e-6);
|
|
199
|
+
return {
|
|
200
|
+
minPolar: polar.min,
|
|
201
|
+
maxPolar: polar.max,
|
|
202
|
+
minDistance,
|
|
203
|
+
maxDistance,
|
|
204
|
+
near: height / 8,
|
|
205
|
+
far: (maxDistance + reach) * 1.5
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
// src/keys.ts
|
|
209
|
+
/** The angle of one step of an arrow key, in radians, for a camera far from the surface. */
|
|
210
|
+
const KEY_TURN = Math.PI / 24;
|
|
211
|
+
/** The factor of the height of a camera above the surface for one step of the key `+`. */
|
|
212
|
+
const KEY_ZOOM = .85;
|
|
213
|
+
/**
|
|
214
|
+
* This function gives the action of a key on a stage, or `undefined` for a key with no action.
|
|
215
|
+
*
|
|
216
|
+
* The arrow keys turn the camera around the target. The keys `+` and `-` change the height above the surface.
|
|
217
|
+
* The keys `Home` and `0` show the home view. Near the surface, a step of an arrow key is smaller.
|
|
218
|
+
* The result has no limits: the stage puts the place in its limits.
|
|
219
|
+
*/
|
|
220
|
+
function keyAction(key, now, options) {
|
|
221
|
+
const surface = options.surface ?? 0;
|
|
222
|
+
const turn = KEY_TURN * speedAt(now.distance, surface);
|
|
223
|
+
const zoom = (factor) => ({
|
|
224
|
+
...now,
|
|
225
|
+
distance: surface + (now.distance - surface) * factor
|
|
226
|
+
});
|
|
227
|
+
switch (key) {
|
|
228
|
+
case "ArrowLeft": return {
|
|
229
|
+
type: "place",
|
|
230
|
+
place: {
|
|
231
|
+
...now,
|
|
232
|
+
azimuth: now.azimuth - turn
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
case "ArrowRight": return {
|
|
236
|
+
type: "place",
|
|
237
|
+
place: {
|
|
238
|
+
...now,
|
|
239
|
+
azimuth: now.azimuth + turn
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
case "ArrowUp": return {
|
|
243
|
+
type: "place",
|
|
244
|
+
place: {
|
|
245
|
+
...now,
|
|
246
|
+
polar: now.polar - turn
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
case "ArrowDown": return {
|
|
250
|
+
type: "place",
|
|
251
|
+
place: {
|
|
252
|
+
...now,
|
|
253
|
+
polar: now.polar + turn
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
case "+":
|
|
257
|
+
case "=": return {
|
|
258
|
+
type: "place",
|
|
259
|
+
place: zoom(KEY_ZOOM)
|
|
260
|
+
};
|
|
261
|
+
case "-":
|
|
262
|
+
case "_": return {
|
|
263
|
+
type: "place",
|
|
264
|
+
place: zoom(1 / KEY_ZOOM)
|
|
265
|
+
};
|
|
266
|
+
case "Home":
|
|
267
|
+
case "0": return {
|
|
268
|
+
type: "view",
|
|
269
|
+
view: options.home
|
|
270
|
+
};
|
|
271
|
+
default: {
|
|
272
|
+
const view = key.length === 1 ? options.views?.[key.toLowerCase()] : void 0;
|
|
273
|
+
return view === void 0 ? void 0 : {
|
|
274
|
+
type: "view",
|
|
275
|
+
view
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// src/marks.ts
|
|
281
|
+
/** The marks of a stage with no pointer on it and no selected record. */
|
|
282
|
+
const NO_MARKS = {
|
|
283
|
+
hovered: void 0,
|
|
284
|
+
selected: void 0,
|
|
285
|
+
marked: /* @__PURE__ */ new Set()
|
|
286
|
+
};
|
|
287
|
+
/** This function gives the mark of a record. The selected record has the highest mark. */
|
|
288
|
+
function markOf(marks, record) {
|
|
289
|
+
if (record === marks.selected) return 2;
|
|
290
|
+
return record === marks.hovered || marks.marked.has(record) ? 1 : 0;
|
|
291
|
+
}
|
|
292
|
+
/** A pointer that moves this distance or less between down and up makes a click, in CSS pixels. */
|
|
293
|
+
const CLICK_DISTANCE = 4;
|
|
294
|
+
/**
|
|
295
|
+
* This function tells if a pointer that went down and up made a click. A pointer that moved more than `limit` made a drag.
|
|
296
|
+
* The positions and the limit are in CSS pixels.
|
|
297
|
+
*/
|
|
298
|
+
function isClick(down, up, limit = 4) {
|
|
299
|
+
return Math.hypot(up.x - down.x, up.y - down.y) <= limit;
|
|
300
|
+
}
|
|
301
|
+
// src/motion.ts
|
|
302
|
+
/**
|
|
303
|
+
* This function gives a part of a move with a slow start and a slow end. `part` goes from 0 to 1.
|
|
304
|
+
* The result goes from 0 to 1, and it is 0.5 at the middle.
|
|
305
|
+
*/
|
|
306
|
+
function easeInOut(part) {
|
|
307
|
+
const clamped = Math.min(Math.max(part, 0), 1);
|
|
308
|
+
return clamped < .5 ? 4 * clamped * clamped * clamped : 1 - (-2 * clamped + 2) ** 3 / 2;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* This function gives the place of a camera at the time `now` of a move, in milliseconds.
|
|
312
|
+
* A move with no duration is at its end immediately. `surface` is the radius of the ball around the target, or 0 for no ball.
|
|
313
|
+
*/
|
|
314
|
+
function moveAt(move, now, surface = 0) {
|
|
315
|
+
const part = move.duration > 0 ? Math.min(Math.max((now - move.start) / move.duration, 0), 1) : 1;
|
|
316
|
+
if (part >= 1) return {
|
|
317
|
+
place: move.to,
|
|
318
|
+
done: true
|
|
319
|
+
};
|
|
320
|
+
return {
|
|
321
|
+
place: placeBetween(move.from, move.to, easeInOut(part), surface),
|
|
322
|
+
done: false
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* This function moves a value one step to its goal. Each step goes a constant part of the way that remains.
|
|
327
|
+
*
|
|
328
|
+
* `seconds` is the time of the step, and `time` is the time in which the value goes most of the way, in seconds.
|
|
329
|
+
* A step with no time gives the goal. A value nearer to the goal than `snap` becomes the goal, so the steps have an end.
|
|
330
|
+
*/
|
|
331
|
+
function approach(value, goal, seconds, time, snap = .01) {
|
|
332
|
+
if (!(seconds > 0) || !(time > 0)) return goal;
|
|
333
|
+
const next = value + (goal - value) * (1 - Math.exp(0 - seconds / time));
|
|
334
|
+
return Math.abs(goal - next) < snap ? goal : next;
|
|
335
|
+
}
|
|
336
|
+
export { viewAxes as C, turnBetween as S, capDistance as _, NO_MARKS as a, placeBetween as b, KEY_TURN as c, FIT_MARGIN as d, capView as f, cameraPosition as g, surfaceOf as h, CLICK_DISTANCE as i, KEY_ZOOM as l, stageLimits as m, easeInOut as n, isClick as o, fitView as p, moveAt as r, markOf as s, approach as t, keyAction as u, clampPlace as v, viewOf as w, speedAt as x, fitDistance as y };
|
|
337
|
+
|
|
338
|
+
//# sourceMappingURL=motion-Ba4IIfAV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"motion-Ba4IIfAV.js","names":[],"sources":["../src/view.ts","../src/extent.ts","../src/keys.ts","../src/marks.ts","../src/motion.ts"],"sourcesContent":["import type { Point3 } from \"@mark1russell7/layout-contract\";\n\n/**\n * The direction of a view: where the camera is, as seen from the point that it looks at.\n * The space of a stage has the y axis up.\n */\nexport interface ViewAngles {\n /** The angle from the up axis to the camera, in radians. At 0, the camera looks down from above. */\n readonly polar: number;\n /** The angle around the up axis, in radians. At 0, the camera is on the positive z side of the point. */\n readonly azimuth: number;\n}\n\n/** A place of a camera: the point that it looks at, the angles of its view and its distance from the point. */\nexport interface Place extends ViewAngles {\n readonly target: Point3;\n readonly distance: number;\n}\n\n/** The three axes of a camera, as unit vectors in the space of the stage. */\nexport interface ViewAxes {\n /** The vector from the point that the camera looks at to the camera. */\n readonly back: Point3;\n /** The vector to the right side of the picture. */\n readonly right: Point3;\n /** The vector to the top of the picture. */\n readonly up: Point3;\n}\n\n/** The limits of the place of a camera. */\nexport interface PlaceLimits {\n /** The smallest polar angle. At 0, the azimuth of a camera has no meaning. */\n readonly minPolar: number;\n /** The largest polar angle. */\n readonly maxPolar: number;\n /** The smallest distance from the target. */\n readonly minDistance: number;\n /** The largest distance from the target. */\n readonly maxDistance: number;\n}\n\n/** This function gives the axes of the camera of a view. At a polar angle of 0, the top of the picture is the negative z axis. */\nexport function viewAxes(view: ViewAngles): ViewAxes {\n const sinPolar = Math.sin(view.polar);\n const cosPolar = Math.cos(view.polar);\n const sinAzimuth = Math.sin(view.azimuth);\n const cosAzimuth = Math.cos(view.azimuth);\n return {\n back: { x: sinPolar * sinAzimuth, y: cosPolar, z: sinPolar * cosAzimuth },\n right: { x: cosAzimuth, y: 0, z: 0 - sinAzimuth },\n up: { x: 0 - cosPolar * sinAzimuth, y: sinPolar, z: 0 - cosPolar * cosAzimuth },\n };\n}\n\n/** This function gives the dot product of the vector from `origin` to `point` with `axis`. */\nfunction along(point: Point3, origin: Point3, axis: Point3): number {\n return (point.x - origin.x) * axis.x + (point.y - origin.y) * axis.y + (point.z - origin.z) * axis.z;\n}\n\n/**\n * This function gives the smallest distance from `target` at which a camera shows each point.\n *\n * The camera looks at `target` along the direction of `view`. `fov` is the vertical angle of the picture in degrees,\n * and `aspect` is the width of the picture divided by its height.\n * The function gives 0 for no points, and for an angle or an aspect that makes no picture.\n */\nexport function fitDistance(points: readonly Point3[], target: Point3, view: ViewAngles, fov: number, aspect: number): number {\n const tanUp = Math.tan((fov * Math.PI) / 360);\n const tanRight = tanUp * aspect;\n if (!(tanUp > 0) || !(tanRight > 0) || !Number.isFinite(tanRight)) return 0;\n const axes = viewAxes(view);\n let distance = 0;\n for (const point of points) {\n const depth = along(point, target, axes.back);\n const needed = Math.max(Math.abs(along(point, target, axes.right)) / tanRight, Math.abs(along(point, target, axes.up)) / tanUp);\n distance = Math.max(distance, depth + needed);\n }\n return distance;\n}\n\n/**\n * This function gives the smallest distance from the center of a ball at which a camera shows a cap of the ball.\n *\n * The camera looks at the center of the ball, and the center of the cap is on the line to the camera.\n * `radius` is the radius of the ball. `angle` is the angle of the cap at the center of the ball, in radians, from 0 to π / 2.\n * `fov` is the vertical angle of the picture in degrees, and `aspect` is the width of the picture divided by its height.\n * The function gives 0 for an angle or an aspect that makes no picture.\n */\nexport function capDistance(radius: number, angle: number, fov: number, aspect: number): number {\n const tanUp = Math.tan((fov * Math.PI) / 360);\n const tan = Math.min(tanUp, tanUp * aspect);\n if (!(tan > 0) || !Number.isFinite(tan) || !(radius > 0)) return 0;\n const cap = Math.min(Math.max(angle, 0), Math.PI / 2);\n const half = Math.atan(tan);\n // A cap this large has its edge behind the outline of the ball. Then the outline of the ball is the limit.\n if (cap >= Math.PI / 2 - half) return radius / Math.sin(half);\n // The edge of the cap is at the height r sin(a) from the line, and at the depth r cos(a) from the center.\n return radius * Math.cos(cap) + (radius * Math.sin(cap)) / tan;\n}\n\n/** This function gives the position of a camera at a distance from `target`, in the direction of `view`. */\nexport function cameraPosition(target: Point3, view: ViewAngles, distance: number): Point3 {\n const { back } = viewAxes(view);\n return { x: target.x + back.x * distance, y: target.y + back.y * distance, z: target.z + back.z * distance };\n}\n\n/** This function gives the angles of the view from `target` to a camera, and the distance between them. A camera at the target gives the angles 0. */\nexport function viewOf(camera: Point3, target: Point3): ViewAngles & { readonly distance: number } {\n const x = camera.x - target.x;\n const y = camera.y - target.y;\n const z = camera.z - target.z;\n const distance = Math.hypot(x, y, z);\n if (!(distance > 0)) return { polar: 0, azimuth: 0, distance: 0 };\n return { polar: Math.acos(Math.min(Math.max(y / distance, -1), 1)), azimuth: Math.atan2(x, z), distance };\n}\n\n/** This function gives the angle between two azimuths along the shortest way, from −π to π. */\nexport function turnBetween(from: number, to: number): number {\n const full = 2 * Math.PI;\n const turn = (((to - from) % full) + full) % full;\n return turn > Math.PI ? turn - full : turn;\n}\n\n/** This function gives a place in its limits. The target and the azimuth stay as they are. */\nexport function clampPlace(place: Place, limits: PlaceLimits): Place {\n return {\n target: place.target,\n azimuth: place.azimuth,\n polar: Math.min(Math.max(place.polar, limits.minPolar), limits.maxPolar),\n distance: Math.min(Math.max(place.distance, limits.minDistance), limits.maxDistance),\n };\n}\n\n/**\n * This function gives the height of a camera above the surface of a stage, as a part of its distance from the target.\n *\n * `surface` is the radius of the ball around the target that the camera stays out of. A stage with no ball has the value 0.\n * The result is the factor of the turn speed and the zoom speed of a camera: near the surface, a camera moves slowly.\n * The result is 0.02 at least, so a camera on the surface can move away.\n */\nexport function speedAt(distance: number, surface: number): number {\n if (!(distance > 0)) return 1;\n return Math.min(Math.max((distance - surface) / distance, 0.02), 1);\n}\n\n/**\n * This function gives the place at a part of the way between two places. `part` goes from 0 to 1.\n *\n * The azimuth goes along the shortest way. The height above the surface changes by an equal factor for each equal step,\n * so a zoom has one speed in the picture. `surface` is the radius of the ball around the target, or 0 for no ball.\n */\nexport function placeBetween(from: Place, to: Place, part: number, surface: number = 0): Place {\n const between = (a: number, b: number): number => a + (b - a) * part;\n const low = from.distance - surface;\n const high = to.distance - surface;\n const height = low > 0 && high > 0 ? low * (high / low) ** part : between(low, high);\n return {\n target: { x: between(from.target.x, to.target.x), y: between(from.target.y, to.target.y), z: between(from.target.z, to.target.z) },\n polar: between(from.polar, to.polar),\n azimuth: from.azimuth + turnBetween(from.azimuth, to.azimuth) * part,\n distance: surface + height,\n };\n}\n","import type { Point3 } from \"@mark1russell7/layout-contract\";\nimport { capDistance, fitDistance, type Place, type PlaceLimits, type ViewAngles } from \"./view.ts\";\n\n/** The space that the content of a stage fills. A view and the limits of the camera come from it. */\nexport interface StageExtent {\n /** The points that a view of all the content shows, for example each corner of each solid. */\n readonly corners: readonly Point3[];\n /** The point that the camera looks at. */\n readonly center: Point3;\n /**\n * The radius of the ball around the center that the camera stays out of, for example the radius of a globe.\n * The default is 0: the content has no ball.\n */\n readonly surface?: number | undefined;\n /** The smallest distance of the camera from the center. The default comes from the views of the stage. */\n readonly minDistance?: number | undefined;\n /** The largest distance of the camera from the center. The default comes from the views of the stage. */\n readonly maxDistance?: number | undefined;\n}\n\n/** The picture that a view is for: the content, and the angle and the shape of the picture. */\nexport interface ViewFrame {\n readonly extent: StageExtent;\n /** The vertical angle of the picture, in degrees. */\n readonly fieldOfView: number;\n /** The width of the picture divided by its height. */\n readonly aspect: number;\n}\n\n/** A view of a stage: a function that gives the place of the camera for a picture. A new size of the picture gives a new place. */\nexport type StageView = (frame: ViewFrame) => Place;\n\n/** The limits of the camera of a stage: the limits of its place, and the nearest and the farthest depth that it draws. */\nexport interface StageLimits extends PlaceLimits {\n readonly near: number;\n readonly far: number;\n}\n\n/** The space around the content in a view that shows all of it, as a factor of the distance. */\nexport const FIT_MARGIN = 1.06;\n\n/** This function gives the radius of the ball around the center of an extent, or 0 for no ball. */\nexport function surfaceOf(extent: StageExtent): number {\n const surface = extent.surface ?? 0;\n return surface > 0 ? surface : 0;\n}\n\n/**\n * This function makes a view that shows each corner of the content from one direction.\n * `margin` is the space around the content, as a factor of the distance. An extent with no corners gives the distance 1.\n */\nexport function fitView(angles: ViewAngles, margin: number = FIT_MARGIN): StageView {\n return ({ extent, fieldOfView, aspect }) => {\n const distance = fitDistance(extent.corners, extent.center, angles, fieldOfView, aspect) * margin;\n return { target: extent.center, polar: angles.polar, azimuth: angles.azimuth, distance: distance > 0 ? distance : 1 };\n };\n}\n\n/**\n * This function makes a view of a part of the ball of the content: a cap around the point of the ball below the camera.\n *\n * `angles` is the direction from the center of the ball to the camera. `angle` is the angle of the cap at the center of the ball, in radians.\n * `margin` is the space around the cap, as a factor of the height above the ball. An extent with no ball gives the distance 1.\n */\nexport function capView(angles: ViewAngles, angle: number, margin: number = FIT_MARGIN): StageView {\n return ({ extent, fieldOfView, aspect }) => {\n const surface = surfaceOf(extent);\n const height = Math.max(capDistance(surface, angle, fieldOfView, aspect) - surface, 0);\n return { target: extent.center, polar: angles.polar, azimuth: angles.azimuth, distance: surface > 0 ? surface + height * margin : 1 };\n };\n}\n\n/**\n * This function gives the limits of the camera of a stage for a picture.\n *\n * The largest distance of the views is the base. The camera goes out to 3 times this distance, and in to 3 % of the height of that view.\n * The extent can give each distance. The nearest depth is small against the height of the nearest camera, and the farthest depth is behind all the content.\n */\nexport function stageLimits(frame: ViewFrame, views: readonly StageView[], polar: { readonly min: number; readonly max: number }): StageLimits {\n const { extent } = frame;\n const surface = surfaceOf(extent);\n let widest = 0;\n for (const view of views) widest = Math.max(widest, view(frame).distance);\n if (!(widest > surface)) widest = surface + 1;\n let reach = surface;\n for (const corner of extent.corners) reach = Math.max(reach, Math.hypot(corner.x - extent.center.x, corner.y - extent.center.y, corner.z - extent.center.z));\n const minDistance = extent.minDistance ?? surface + (widest - surface) * 0.03;\n const maxDistance = Math.max(extent.maxDistance ?? surface + (widest - surface) * 3, minDistance);\n const height = Math.max(minDistance - surface, 1e-6);\n return { minPolar: polar.min, maxPolar: polar.max, minDistance, maxDistance, near: height / 8, far: (maxDistance + reach) * 1.5 };\n}\n","import { speedAt, type Place } from \"./view.ts\";\n\n/** The angle of one step of an arrow key, in radians, for a camera far from the surface. */\nexport const KEY_TURN: number = Math.PI / 24;\n\n/** The factor of the height of a camera above the surface for one step of the key `+`. */\nexport const KEY_ZOOM = 0.85;\n\n/** What a key does on a stage: it moves the camera to a place, or it shows a view. */\nexport type KeyAction = { readonly type: \"place\"; readonly place: Place } | { readonly type: \"view\"; readonly view: string };\n\n/** The keys of a stage. */\nexport interface KeyOptions {\n /** The name of the view of the keys `Home` and `0`. */\n readonly home: string;\n /** The name of the view of each other key, for example `{ t: \"top\" }`. A letter has no case. */\n readonly views?: Readonly<Record<string, string>> | undefined;\n /** The radius of the ball around the target that the camera stays out of, or 0 for no ball. */\n readonly surface?: number | undefined;\n}\n\n/**\n * This function gives the action of a key on a stage, or `undefined` for a key with no action.\n *\n * The arrow keys turn the camera around the target. The keys `+` and `-` change the height above the surface.\n * The keys `Home` and `0` show the home view. Near the surface, a step of an arrow key is smaller.\n * The result has no limits: the stage puts the place in its limits.\n */\nexport function keyAction(key: string, now: Place, options: KeyOptions): KeyAction | undefined {\n const surface = options.surface ?? 0;\n const turn = KEY_TURN * speedAt(now.distance, surface);\n const zoom = (factor: number): Place => ({ ...now, distance: surface + (now.distance - surface) * factor });\n switch (key) {\n case \"ArrowLeft\":\n return { type: \"place\", place: { ...now, azimuth: now.azimuth - turn } };\n case \"ArrowRight\":\n return { type: \"place\", place: { ...now, azimuth: now.azimuth + turn } };\n case \"ArrowUp\":\n return { type: \"place\", place: { ...now, polar: now.polar - turn } };\n case \"ArrowDown\":\n return { type: \"place\", place: { ...now, polar: now.polar + turn } };\n case \"+\":\n case \"=\":\n return { type: \"place\", place: zoom(KEY_ZOOM) };\n case \"-\":\n case \"_\":\n return { type: \"place\", place: zoom(1 / KEY_ZOOM) };\n case \"Home\":\n case \"0\":\n return { type: \"view\", view: options.home };\n default: {\n const view = key.length === 1 ? options.views?.[key.toLowerCase()] : undefined;\n return view === undefined ? undefined : { type: \"view\", view };\n }\n }\n}\n","/** The mark of a record. 0 is no mark. 1 is a record under the pointer or in the marked list. 2 is the selected record. */\nexport type Mark = 0 | 1 | 2;\n\n/** The records of a stage that have a mark. Each record has a name. */\nexport interface Marks {\n /** The name of the record under the pointer. */\n readonly hovered: string | undefined;\n /** The name of the selected record. */\n readonly selected: string | undefined;\n /** The names of the records that the page marks. */\n readonly marked: ReadonlySet<string>;\n}\n\n/** The marks of a stage with no pointer on it and no selected record. */\nexport const NO_MARKS: Marks = { hovered: undefined, selected: undefined, marked: new Set() };\n\n/** This function gives the mark of a record. The selected record has the highest mark. */\nexport function markOf(marks: Marks, record: string): Mark {\n if (record === marks.selected) return 2;\n return record === marks.hovered || marks.marked.has(record) ? 1 : 0;\n}\n\n/** A pointer that moves this distance or less between down and up makes a click, in CSS pixels. */\nexport const CLICK_DISTANCE = 4;\n\n/**\n * This function tells if a pointer that went down and up made a click. A pointer that moved more than `limit` made a drag.\n * The positions and the limit are in CSS pixels.\n */\nexport function isClick(down: { readonly x: number; readonly y: number }, up: { readonly x: number; readonly y: number }, limit: number = CLICK_DISTANCE): boolean {\n return Math.hypot(up.x - down.x, up.y - down.y) <= limit;\n}\n","import { placeBetween, type Place } from \"./view.ts\";\n\n/** A move of a camera from one place to a different place. */\nexport interface Move {\n readonly from: Place;\n readonly to: Place;\n /** The time of the start of the move, in milliseconds. */\n readonly start: number;\n /** The time that the move takes, in milliseconds. */\n readonly duration: number;\n}\n\n/** The place of a camera at one time of a move. */\nexport interface MoveStep {\n readonly place: Place;\n /** This value is true at the end of the move, and after it. */\n readonly done: boolean;\n}\n\n/**\n * This function gives a part of a move with a slow start and a slow end. `part` goes from 0 to 1.\n * The result goes from 0 to 1, and it is 0.5 at the middle.\n */\nexport function easeInOut(part: number): number {\n const clamped = Math.min(Math.max(part, 0), 1);\n return clamped < 0.5 ? 4 * clamped * clamped * clamped : 1 - (-2 * clamped + 2) ** 3 / 2;\n}\n\n/**\n * This function gives the place of a camera at the time `now` of a move, in milliseconds.\n * A move with no duration is at its end immediately. `surface` is the radius of the ball around the target, or 0 for no ball.\n */\nexport function moveAt(move: Move, now: number, surface: number = 0): MoveStep {\n const part = move.duration > 0 ? Math.min(Math.max((now - move.start) / move.duration, 0), 1) : 1;\n if (part >= 1) return { place: move.to, done: true };\n return { place: placeBetween(move.from, move.to, easeInOut(part), surface), done: false };\n}\n\n/**\n * This function moves a value one step to its goal. Each step goes a constant part of the way that remains.\n *\n * `seconds` is the time of the step, and `time` is the time in which the value goes most of the way, in seconds.\n * A step with no time gives the goal. A value nearer to the goal than `snap` becomes the goal, so the steps have an end.\n */\nexport function approach(value: number, goal: number, seconds: number, time: number, snap: number = 0.01): number {\n if (!(seconds > 0) || !(time > 0)) return goal;\n const next = value + (goal - value) * (1 - Math.exp(0 - seconds / time));\n return Math.abs(goal - next) < snap ? goal : next;\n}\n"],"mappings":";;AA0CA,SAAgB,SAAS,MAA4B;CACnD,MAAM,WAAW,KAAK,IAAI,KAAK,KAAK;CACpC,MAAM,WAAW,KAAK,IAAI,KAAK,KAAK;CACpC,MAAM,aAAa,KAAK,IAAI,KAAK,OAAO;CACxC,MAAM,aAAa,KAAK,IAAI,KAAK,OAAO;CACxC,OAAO;EACL,MAAM;GAAE,GAAG,WAAW;GAAY,GAAG;GAAU,GAAG,WAAW;EAAW;EACxE,OAAO;GAAE,GAAG;GAAY,GAAG;GAAG,GAAG,IAAI;EAAW;EAChD,IAAI;GAAE,GAAG,IAAI,WAAW;GAAY,GAAG;GAAU,GAAG,IAAI,WAAW;EAAW;CAChF;AACF;;AAGA,SAAS,MAAM,OAAe,QAAgB,MAAsB;CAClE,QAAQ,MAAM,IAAI,OAAO,KAAK,KAAK,KAAK,MAAM,IAAI,OAAO,KAAK,KAAK,KAAK,MAAM,IAAI,OAAO,KAAK,KAAK;AACrG;;;;;;;;AASA,SAAgB,YAAY,QAA2B,QAAgB,MAAkB,KAAa,QAAwB;CAC5H,MAAM,QAAQ,KAAK,IAAK,MAAM,KAAK,KAAM,GAAG;CAC5C,MAAM,WAAW,QAAQ;CACzB,IAAI,EAAE,QAAQ,MAAM,EAAE,WAAW,MAAM,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO;CAC1E,MAAM,OAAO,SAAS,IAAI;CAC1B,IAAI,WAAW;CACf,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,QAAQ,MAAM,OAAO,QAAQ,KAAK,IAAI;EAC5C,MAAM,SAAS,KAAK,IAAI,KAAK,IAAI,MAAM,OAAO,QAAQ,KAAK,KAAK,CAAC,IAAI,UAAU,KAAK,IAAI,MAAM,OAAO,QAAQ,KAAK,EAAE,CAAC,IAAI,KAAK;EAC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