cseen 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 +161 -2
- package/dist/all.d.ts +8 -0
- package/dist/all.js +10 -0
- package/dist/all.js.map +1 -0
- package/dist/anim/mount.d.ts +69 -0
- package/dist/anim/mount.js +178 -0
- package/dist/anim/mount.js.map +1 -0
- package/dist/anim/orbit.d.ts +85 -0
- package/dist/anim/orbit.js +196 -0
- package/dist/anim/orbit.js.map +1 -0
- package/dist/anim/timeline.d.ts +287 -0
- package/dist/anim/timeline.js +379 -0
- package/dist/anim/timeline.js.map +1 -0
- package/dist/charts/charts.d.ts +216 -0
- package/dist/charts/charts.js +570 -0
- package/dist/charts/charts.js.map +1 -0
- package/dist/charts/index.d.ts +2 -0
- package/dist/charts/index.js +2 -0
- package/dist/charts/index.js.map +1 -0
- package/dist/construct/construct.d.ts +73 -0
- package/dist/construct/construct.js +96 -0
- package/dist/construct/construct.js.map +1 -0
- package/dist/construct/geom.d.ts +91 -0
- package/dist/construct/geom.js +118 -0
- package/dist/construct/geom.js.map +1 -0
- package/dist/core/animatable.d.ts +19 -0
- package/dist/core/animatable.js +41 -0
- package/dist/core/animatable.js.map +1 -0
- package/dist/core/camera.d.ts +48 -0
- package/dist/core/camera.js +95 -0
- package/dist/core/camera.js.map +1 -0
- package/dist/core/math.d.ts +39 -0
- package/dist/core/math.js +104 -0
- package/dist/core/math.js.map +1 -0
- package/dist/core/nodes.d.ts +322 -0
- package/dist/core/nodes.js +350 -0
- package/dist/core/nodes.js.map +1 -0
- package/dist/core/render.d.ts +86 -0
- package/dist/core/render.js +345 -0
- package/dist/core/render.js.map +1 -0
- package/dist/core/scene.d.ts +96 -0
- package/dist/core/scene.js +97 -0
- package/dist/core/scene.js.map +1 -0
- package/dist/core/sort.d.ts +40 -0
- package/dist/core/sort.js +181 -0
- package/dist/core/sort.js.map +1 -0
- package/dist/core/split.d.ts +47 -0
- package/dist/core/split.js +169 -0
- package/dist/core/split.js.map +1 -0
- package/dist/core/svg-import.d.ts +16 -0
- package/dist/core/svg-import.js +53 -0
- package/dist/core/svg-import.js.map +1 -0
- package/dist/cseen.css +104 -0
- package/dist/devices/devices.d.ts +307 -0
- package/dist/devices/devices.js +327 -0
- package/dist/devices/devices.js.map +1 -0
- package/dist/devices/index.d.ts +6 -0
- package/dist/devices/index.js +4 -0
- package/dist/devices/index.js.map +1 -0
- package/dist/devices/keyboard.d.ts +86 -0
- package/dist/devices/keyboard.js +186 -0
- package/dist/devices/keyboard.js.map +1 -0
- package/dist/devices/peripherals.d.ts +103 -0
- package/dist/devices/peripherals.js +171 -0
- package/dist/devices/peripherals.js.map +1 -0
- package/dist/draw/link.d.ts +76 -0
- package/dist/draw/link.js +102 -0
- package/dist/draw/link.js.map +1 -0
- package/dist/draw/pen.d.ts +41 -0
- package/dist/draw/pen.js +57 -0
- package/dist/draw/pen.js.map +1 -0
- package/dist/draw/shapes.d.ts +181 -0
- package/dist/draw/shapes.js +200 -0
- package/dist/draw/shapes.js.map +1 -0
- package/dist/draw/svgplane.d.ts +45 -0
- package/dist/draw/svgplane.js +63 -0
- package/dist/draw/svgplane.js.map +1 -0
- package/dist/draw/text.d.ts +111 -0
- package/dist/draw/text.js +204 -0
- package/dist/draw/text.js.map +1 -0
- package/dist/flow/flow.d.ts +198 -0
- package/dist/flow/flow.js +416 -0
- package/dist/flow/flow.js.map +1 -0
- package/dist/flow/index.d.ts +4 -0
- package/dist/flow/index.js +3 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/flow/layout.d.ts +66 -0
- package/dist/flow/layout.js +353 -0
- package/dist/flow/layout.js.map +1 -0
- package/dist/flow/sequence.d.ts +77 -0
- package/dist/flow/sequence.js +149 -0
- package/dist/flow/sequence.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/math/index.d.ts +2 -0
- package/dist/math/index.js +2 -0
- package/dist/math/index.js.map +1 -0
- package/dist/math/math.d.ts +238 -0
- package/dist/math/math.js +421 -0
- package/dist/math/math.js.map +1 -0
- package/dist/optics/index.d.ts +2 -0
- package/dist/optics/index.js +2 -0
- package/dist/optics/index.js.map +1 -0
- package/dist/optics/optics.d.ts +94 -0
- package/dist/optics/optics.js +173 -0
- package/dist/optics/optics.js.map +1 -0
- package/dist/ui/index.d.ts +2 -0
- package/dist/ui/index.js +2 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/ui/ui.d.ts +166 -0
- package/dist/ui/ui.js +255 -0
- package/dist/ui/ui.js.map +1 -0
- package/dist/waves/index.d.ts +2 -0
- package/dist/waves/index.js +2 -0
- package/dist/waves/index.js.map +1 -0
- package/dist/waves/waves.d.ts +93 -0
- package/dist/waves/waves.js +206 -0
- package/dist/waves/waves.js.map +1 -0
- package/package.json +81 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 M44F4
|
|
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,162 @@
|
|
|
1
|
-
#
|
|
1
|
+
# cseen
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Describe a scene in TypeScript and get SVG that follows your CSS theme, rendered on the server or played live in the browser.
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://m44f4.github.io/cseen/"><img alt="Website" src="https://img.shields.io/badge/Website-064E3B?style=for-the-badge&logoColor=white"></a>
|
|
7
|
+
<a href="https://m44f4.github.io/cseen/docs/"><img alt="Docs" src="https://img.shields.io/badge/Docs-064E3B?style=for-the-badge"></a>
|
|
8
|
+
<a href="https://m44f4.github.io/cseen/gallery/core/"><img alt="Gallery" src="https://img.shields.io/badge/Gallery-064E3B?style=for-the-badge"></a>
|
|
9
|
+
<a href="https://m44f4.github.io/cseen/playground/"><img alt="Playground" src="https://img.shields.io/badge/Playground-C4A052?style=for-the-badge"></a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p align="center">
|
|
13
|
+
<a href="https://www.npmjs.com/package/cseen"><img alt="npm version" src="https://img.shields.io/npm/v/cseen"></a>
|
|
14
|
+
<a href="LICENSE"><img alt="licence: MIT" src="https://img.shields.io/badge/licence-MIT-blue.svg"></a>
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
<p align="center">
|
|
18
|
+
<picture>
|
|
19
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/hero-dark.svg">
|
|
20
|
+
<img alt="The cseen mark laid on a plane, seen from an isometric camera" src="docs/img/hero-light.svg" width="640">
|
|
21
|
+
</picture>
|
|
22
|
+
</p>
|
|
23
|
+
|
|
24
|
+
cseen is for the figure in an article, a portfolio or documentation. You describe a scene in a few lines of TypeScript, animate it on a timeline, and put it on a page. The same scene renders to a plain SVG on the server and plays live in the browser.
|
|
25
|
+
|
|
26
|
+
The name is meant to be said aloud: the **c** is *see*, and *seen* sounds like *scene*, so **cseen** says **see scene**. Rearrange its letters and you get **scene** itself. You describe a scene, and you see it.
|
|
27
|
+
|
|
28
|
+
## What it does
|
|
29
|
+
|
|
30
|
+
- **Colours come from your CSS.** Set up to three variables (ink, background, accent) and everything else, charts included, is derived. Light, dark or a theme you have not made yet.
|
|
31
|
+
- **One coordinate system for 2D and 3D.** A flat diagram is a scene seen from the front.
|
|
32
|
+
- **Static first, scrubbable.** A scene is a function of time, so playing, pausing, stepping and rendering one frame all work the same way.
|
|
33
|
+
- **Small and dependency free**, split into packs: devices, charts, flowcharts, waves, optics, maths.
|
|
34
|
+
- **Not a 3D engine.** The output is SVG, so it suits figures and diagrams, not games, textured meshes or scenes with thousands of objects.
|
|
35
|
+
|
|
36
|
+
## Example
|
|
37
|
+
|
|
38
|
+
A terminal, a cloud and a browser, with a packet that travels from one to the other (the first figure below). This runs as pasted:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { Scene, Timeline, ground, dot, typeOn, moveTo, fadeIn, mount } from 'cseen';
|
|
42
|
+
import { terminal, cloud, browser } from 'cseen/devices';
|
|
43
|
+
import 'cseen/cseen.css';
|
|
44
|
+
|
|
45
|
+
const s = new Scene();
|
|
46
|
+
s.add(ground());
|
|
47
|
+
|
|
48
|
+
const term = s.add(terminal({ at: [-4, 0, 0], width: 3.6, title: 'zsh' }));
|
|
49
|
+
const cmd = term.write(['$ git push', 'deploying...', 'done.']);
|
|
50
|
+
s.add(cloud({ at: [0, 2.2, 2], size: 2.4 }));
|
|
51
|
+
const site = s.add(browser({ at: [4, 1, 0], width: 3.4, url: 'my.app' }));
|
|
52
|
+
const hello = site.write('Hello, world', { fontSize: 0.32 });
|
|
53
|
+
const packet = s.add(dot([-3, -0.2, 1.4], { role: 'accent', r: 3.6 }));
|
|
54
|
+
|
|
55
|
+
const tl = new Timeline();
|
|
56
|
+
tl.play(typeOn(cmd, 2.2));
|
|
57
|
+
tl.play(moveTo(packet, [-0.9, 2.2, 2.4], 1.3));
|
|
58
|
+
tl.play(moveTo(packet, [3.2, 0.9, 1.2], 1.3));
|
|
59
|
+
tl.play(fadeIn(hello, 0.8));
|
|
60
|
+
tl.lockFit(s);
|
|
61
|
+
|
|
62
|
+
mount(document.querySelector('#figure')!, s, { timeline: tl });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Without a browser, `renderStill(s, tl)` returns a standalone `<svg>` string.
|
|
66
|
+
|
|
67
|
+
## The packs
|
|
68
|
+
|
|
69
|
+
Each pack adds ready-made parts. A chart or a flowchart is one call, and it takes its colours from the same three CSS variables:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { bars } from 'cseen/charts';
|
|
73
|
+
import { flowchart } from 'cseen/flow';
|
|
74
|
+
|
|
75
|
+
s.add(bars({
|
|
76
|
+
data: { 2023: [12, 19, 7, 15], 2024: [14, 22, 11, 19] },
|
|
77
|
+
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
|
|
78
|
+
title: 'Revenue', size: [4.4, 2.8],
|
|
79
|
+
}));
|
|
80
|
+
|
|
81
|
+
s.add(flowchart([
|
|
82
|
+
{ label: 'start', kind: 'start' },
|
|
83
|
+
{ label: 'signed in?', kind: 'decision', to: [['home', 'yes'], ['sign in', 'no']] },
|
|
84
|
+
{ label: 'sign in', to: ['signed in?'] },
|
|
85
|
+
{ label: 'home', kind: 'end' },
|
|
86
|
+
]));
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The figures below are drawn with these packs.
|
|
90
|
+
|
|
91
|
+
<div align="center">
|
|
92
|
+
<table>
|
|
93
|
+
<tr>
|
|
94
|
+
<td width="50%" align="center">
|
|
95
|
+
<picture>
|
|
96
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/deploy-dark.svg">
|
|
97
|
+
<img alt="A terminal, a cloud and a browser with a packet travelling between them" src="docs/img/deploy-light.svg">
|
|
98
|
+
</picture>
|
|
99
|
+
<br><sub><code>/devices</code>: a terminal, a cloud and a browser, with a packet in flight</sub>
|
|
100
|
+
</td>
|
|
101
|
+
<td width="50%" align="center">
|
|
102
|
+
<picture>
|
|
103
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/chart-dark.svg">
|
|
104
|
+
<img alt="A bar chart of revenue by quarter for two years" src="docs/img/chart-light.svg">
|
|
105
|
+
</picture>
|
|
106
|
+
<br><sub><code>/charts</code>: a chart is a plane in the scene</sub>
|
|
107
|
+
</td>
|
|
108
|
+
</tr>
|
|
109
|
+
<tr>
|
|
110
|
+
<td width="50%" align="center">
|
|
111
|
+
<picture>
|
|
112
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/flow-dark.svg">
|
|
113
|
+
<img alt="A flowchart that asks whether the user is signed in" src="docs/img/flow-light.svg">
|
|
114
|
+
</picture>
|
|
115
|
+
<br><sub><code>/flow</code>: automatic layout, edges trace on</sub>
|
|
116
|
+
</td>
|
|
117
|
+
<td width="50%" align="center">
|
|
118
|
+
<picture>
|
|
119
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/img/stereo-dark.svg">
|
|
120
|
+
<img alt="A stereo rig: two cameras a baseline apart looking at one point" src="docs/img/stereo-light.svg">
|
|
121
|
+
</picture>
|
|
122
|
+
<br><sub><code>/optics</code>: depth from disparity</sub>
|
|
123
|
+
</td>
|
|
124
|
+
</tr>
|
|
125
|
+
</table>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
## Playground
|
|
129
|
+
|
|
130
|
+
The [playground](https://m44f4.github.io/cseen/playground/) lets you try cseen without installing anything. Pick any example from the list, or write your own scene in TypeScript, then run it with the button or Ctrl/Cmd+Enter. Drag the figure to turn it, and play or scrub its timeline. "Copy link" puts your code in the URL, so a link is a complete example you can send to someone.
|
|
131
|
+
|
|
132
|
+
## Install
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm install cseen
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The API may still change before 1.0. There are no runtime dependencies.
|
|
139
|
+
|
|
140
|
+
## Documentation
|
|
141
|
+
|
|
142
|
+
The docs site is at **<https://m44f4.github.io/cseen/>**, and its source lives in [`site/`](site/): written guides, a gallery where every figure opens in a window you can turn, edit and read the code of, an editable playground, and an API reference generated from the source. It runs against the current source:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
npm --prefix site install
|
|
146
|
+
npm run docs:dev # http://localhost:4321/cseen/
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Develop
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
npm test
|
|
153
|
+
npm run typecheck
|
|
154
|
+
npm run build
|
|
155
|
+
npm run readme:art # redraws the figures of this README into docs/img/
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
159
|
+
|
|
160
|
+
## Licence
|
|
161
|
+
|
|
162
|
+
MIT. The docs site bundles three fonts under the SIL Open Font License 1.1: Orbitron, Instrument Sans and JetBrains Mono (see [`site/public/fonts/NOTICE.txt`](site/public/fonts/NOTICE.txt)). The library itself ships no fonts.
|
package/dist/all.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './index.ts';
|
|
2
|
+
export * from './devices/index.ts';
|
|
3
|
+
export * from './ui/index.ts';
|
|
4
|
+
export * from './waves/index.ts';
|
|
5
|
+
export * from './optics/index.ts';
|
|
6
|
+
export * from './charts/index.ts';
|
|
7
|
+
export * from './math/index.ts';
|
|
8
|
+
export * from './flow/index.ts';
|
package/dist/all.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Everything in one place, for tests, the docs site and examples. Not a published entry point.
|
|
2
|
+
export * from "./index.js";
|
|
3
|
+
export * from "./devices/index.js";
|
|
4
|
+
export * from "./ui/index.js";
|
|
5
|
+
export * from "./waves/index.js";
|
|
6
|
+
export * from "./optics/index.js";
|
|
7
|
+
export * from "./charts/index.js";
|
|
8
|
+
export * from "./math/index.js";
|
|
9
|
+
export * from "./flow/index.js";
|
|
10
|
+
//# sourceMappingURL=all.js.map
|
package/dist/all.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all.js","sourceRoot":"","sources":["../src/all.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Scene } from '../core/scene.ts';
|
|
2
|
+
import type { Timeline } from './timeline.ts';
|
|
3
|
+
import { esc } from '../core/render.ts';
|
|
4
|
+
/** Options of `mount`. */
|
|
5
|
+
export interface MountOpts {
|
|
6
|
+
/** the timeline to play */
|
|
7
|
+
timeline?: Timeline;
|
|
8
|
+
/** play when mounted. Default true. */
|
|
9
|
+
autoplay?: boolean;
|
|
10
|
+
/** restart at the end. `true`, or `{ pause: seconds }` to hold the last frame first. Default true. */
|
|
11
|
+
loop?: boolean | {
|
|
12
|
+
pause: number;
|
|
13
|
+
};
|
|
14
|
+
/** playback speed, 1 by default */
|
|
15
|
+
speed?: number;
|
|
16
|
+
/** time to show for people who prefer reduced motion, and before play. Defaults to the end of the timeline. */
|
|
17
|
+
rest?: number;
|
|
18
|
+
/** only animate while on screen. Default true. */
|
|
19
|
+
onlyVisible?: boolean;
|
|
20
|
+
/** upper limit of redraws per second, 60 by default */
|
|
21
|
+
maxFps?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Controls a mounted scene: play, pause, seek by time or by chapter, and redraw after a change made by hand. */
|
|
24
|
+
export interface Player {
|
|
25
|
+
/** the current time in seconds */
|
|
26
|
+
readonly time: number;
|
|
27
|
+
/** the length of the timeline in seconds */
|
|
28
|
+
readonly duration: number;
|
|
29
|
+
/** starts or resumes playing */
|
|
30
|
+
play(): void;
|
|
31
|
+
/** stops playing, keeping the current frame */
|
|
32
|
+
pause(): void;
|
|
33
|
+
/** jumps to a time in seconds */
|
|
34
|
+
seek(t: number): void;
|
|
35
|
+
/** jumps to the start of the next (or previous) chapter */
|
|
36
|
+
step(dir?: 1 | -1): void;
|
|
37
|
+
/** stops and releases the scene */
|
|
38
|
+
destroy(): void;
|
|
39
|
+
/** whether it is playing */
|
|
40
|
+
readonly playing: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Redraws the scene as it is now, without moving the timeline. Call it after changing the camera or a
|
|
43
|
+
* node by hand (orbit controls do). Calls within one frame are merged, and `maxFps` still applies.
|
|
44
|
+
*/
|
|
45
|
+
invalidate(): void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Puts a scene in the page and plays it. The SVG is built once; each frame only the pieces that
|
|
49
|
+
* changed are replaced, and the drawing pauses when it is off screen.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* const s = new Scene({ view: 'iso' });
|
|
53
|
+
* s.add(ground(), box({ at: [0, 0, 0], size: 1.5, role: 'accent' }));
|
|
54
|
+
*
|
|
55
|
+
* const player = mount(document.querySelector('#figure')!, s);
|
|
56
|
+
* // player.invalidate() redraws after you change the scene yourself
|
|
57
|
+
*/
|
|
58
|
+
export declare function mount(target: Element, scene: Scene, o?: MountOpts): Player;
|
|
59
|
+
/**
|
|
60
|
+
* The static markup of a scene at one moment: render on the server, then `mount` in the browser to bring it to life.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const s = new Scene({ view: 'iso' });
|
|
64
|
+
* s.add(ground(), box({ at: [0, 0, 0], size: 1.5, role: 'accent' }));
|
|
65
|
+
*
|
|
66
|
+
* const svg = renderStill(s); // a standalone <svg> string: on the server, or anywhere without a browser
|
|
67
|
+
*/
|
|
68
|
+
export declare function renderStill(scene: Scene, timeline?: Timeline, t?: number): string;
|
|
69
|
+
export { esc };
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { esc } from "../core/render.js";
|
|
2
|
+
/** SVG namespace, for creating elements. */
|
|
3
|
+
const NS = 'http://www.w3.org/2000/svg';
|
|
4
|
+
/**
|
|
5
|
+
* Puts a scene in the page and plays it. The SVG is built once; each frame only the pieces that
|
|
6
|
+
* changed are replaced, and the drawing pauses when it is off screen.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const s = new Scene({ view: 'iso' });
|
|
10
|
+
* s.add(ground(), box({ at: [0, 0, 0], size: 1.5, role: 'accent' }));
|
|
11
|
+
*
|
|
12
|
+
* const player = mount(document.querySelector('#figure')!, s);
|
|
13
|
+
* // player.invalidate() redraws after you change the scene yourself
|
|
14
|
+
*/
|
|
15
|
+
export function mount(target, scene, o = {}) {
|
|
16
|
+
const tl = o.timeline;
|
|
17
|
+
const dur = tl?.duration ?? 0;
|
|
18
|
+
let svg = target.tagName.toLowerCase() === 'svg' ? target : target.querySelector('svg.cseen');
|
|
19
|
+
if (!svg) {
|
|
20
|
+
svg = document.createElementNS(NS, 'svg');
|
|
21
|
+
svg.setAttribute('class', 'cseen');
|
|
22
|
+
target.appendChild(svg);
|
|
23
|
+
}
|
|
24
|
+
svg.setAttribute('viewBox', `0 0 ${scene.size[0]} ${scene.size[1]}`);
|
|
25
|
+
svg.replaceChildren(); // take over from any server-rendered markup of the same scene
|
|
26
|
+
let head = null;
|
|
27
|
+
if (scene.title) {
|
|
28
|
+
svg.setAttribute('role', 'img');
|
|
29
|
+
svg.setAttribute('aria-label', scene.title);
|
|
30
|
+
const t = document.createElementNS(NS, 'title');
|
|
31
|
+
t.textContent = scene.title;
|
|
32
|
+
svg.appendChild(t);
|
|
33
|
+
head = t;
|
|
34
|
+
if (scene.desc) {
|
|
35
|
+
const d = document.createElementNS(NS, 'desc');
|
|
36
|
+
d.textContent = scene.desc;
|
|
37
|
+
svg.appendChild(d);
|
|
38
|
+
head = d;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
43
|
+
}
|
|
44
|
+
// Each drawn item keeps its own element. A frame only replaces the ones whose markup changed.
|
|
45
|
+
const els = new Map();
|
|
46
|
+
/** brings the page's elements in line with the scene's current frame, replacing only items whose markup changed */
|
|
47
|
+
const draw = () => {
|
|
48
|
+
const f = scene.frame();
|
|
49
|
+
const wanted = [];
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
for (const it of f.items) {
|
|
52
|
+
seen.add(it.key);
|
|
53
|
+
let rec = els.get(it.key);
|
|
54
|
+
if (!rec || rec.html !== it.html) {
|
|
55
|
+
rec?.el.remove();
|
|
56
|
+
const g = document.createElementNS(NS, 'g');
|
|
57
|
+
g.innerHTML = it.html;
|
|
58
|
+
rec = { html: it.html, el: g };
|
|
59
|
+
els.set(it.key, rec);
|
|
60
|
+
}
|
|
61
|
+
wanted.push(rec.el);
|
|
62
|
+
}
|
|
63
|
+
for (const [k, rec] of els)
|
|
64
|
+
if (!seen.has(k)) {
|
|
65
|
+
rec.el.remove();
|
|
66
|
+
els.delete(k);
|
|
67
|
+
}
|
|
68
|
+
let ref = head ? head.nextSibling : svg.firstChild;
|
|
69
|
+
for (const el of wanted) {
|
|
70
|
+
if (el === ref)
|
|
71
|
+
ref = ref.nextSibling;
|
|
72
|
+
else
|
|
73
|
+
svg.insertBefore(el, ref);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const reduce = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
77
|
+
const rest = o.rest ?? dur;
|
|
78
|
+
const speed = o.speed ?? 1;
|
|
79
|
+
const loop = o.loop ?? true;
|
|
80
|
+
const pause = typeof loop === 'object' ? loop.pause : 1.2;
|
|
81
|
+
const minGap = 1000 / (o.maxFps ?? 60);
|
|
82
|
+
// frame times come in a hair off the exact gap (16.66 ms, not 16.67), so a strict test would drop every other frame at 60 Hz
|
|
83
|
+
const tooSoon = (now, since) => now - since < minGap - 1.5;
|
|
84
|
+
let time = 0, playing = false, raf = 0, last = 0, hold = 0, visible = true;
|
|
85
|
+
let dirty = false, dirtyRaf = 0, dirtyAt = 0;
|
|
86
|
+
/** moves the timeline to `t` and draws it; clears any pending redraw, since this one covers it */
|
|
87
|
+
const show = (t) => { time = t; dirty = false; tl?.seek(t); draw(); };
|
|
88
|
+
/** draws a change made by hand (`invalidate`) while paused, no more often than `maxFps` */
|
|
89
|
+
const flush = (now) => {
|
|
90
|
+
dirtyRaf = 0;
|
|
91
|
+
if (!dirty)
|
|
92
|
+
return;
|
|
93
|
+
if (tooSoon(now, dirtyAt)) {
|
|
94
|
+
dirtyRaf = requestAnimationFrame(flush);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
dirty = false;
|
|
98
|
+
dirtyAt = now;
|
|
99
|
+
draw();
|
|
100
|
+
};
|
|
101
|
+
/** one animation frame: advance time by the real time passed (times `speed`), hold at the end, then loop or stop */
|
|
102
|
+
const tick = (now) => {
|
|
103
|
+
raf = requestAnimationFrame(tick);
|
|
104
|
+
// a frame that advances time redraws everything anyway; otherwise (held at the end, throttled) draw the pending change
|
|
105
|
+
if (dirty && visible && (hold > 0 || tooSoon(now, last)) && !tooSoon(now, dirtyAt)) {
|
|
106
|
+
dirty = false;
|
|
107
|
+
dirtyAt = now;
|
|
108
|
+
draw();
|
|
109
|
+
}
|
|
110
|
+
if (!visible || tooSoon(now, last))
|
|
111
|
+
return;
|
|
112
|
+
const dt = last ? (now - last) / 1000 : 0;
|
|
113
|
+
last = now;
|
|
114
|
+
if (hold > 0) {
|
|
115
|
+
hold -= dt;
|
|
116
|
+
if (hold <= 0)
|
|
117
|
+
show(0);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const t = time + dt * speed;
|
|
121
|
+
if (t >= dur) {
|
|
122
|
+
show(dur);
|
|
123
|
+
if (!loop)
|
|
124
|
+
p.pause();
|
|
125
|
+
else if (pause > 0)
|
|
126
|
+
hold = pause;
|
|
127
|
+
else
|
|
128
|
+
show(0); // no pause: restart now, or the next frame would show the last one again forever
|
|
129
|
+
}
|
|
130
|
+
else
|
|
131
|
+
show(t);
|
|
132
|
+
};
|
|
133
|
+
const p = {
|
|
134
|
+
get time() { return time; },
|
|
135
|
+
get duration() { return dur; },
|
|
136
|
+
get playing() { return playing; },
|
|
137
|
+
play() { if (playing || !dur)
|
|
138
|
+
return; playing = true; last = 0; raf = requestAnimationFrame(tick); },
|
|
139
|
+
pause() { playing = false; cancelAnimationFrame(raf); },
|
|
140
|
+
seek(t) { show(Math.min(Math.max(t, 0), dur)); },
|
|
141
|
+
step(dir = 1) {
|
|
142
|
+
const times = [0, ...(tl?.chapters.map((c) => c.time) ?? []), dur];
|
|
143
|
+
const next = dir > 0 ? times.find((x) => x > time + 1e-6) : [...times].reverse().find((x) => x < time - 1e-6);
|
|
144
|
+
show(next ?? (dir > 0 ? dur : 0));
|
|
145
|
+
},
|
|
146
|
+
invalidate() {
|
|
147
|
+
dirty = true;
|
|
148
|
+
if (!playing && !dirtyRaf)
|
|
149
|
+
dirtyRaf = requestAnimationFrame(flush);
|
|
150
|
+
},
|
|
151
|
+
destroy() { p.pause(); cancelAnimationFrame(dirtyRaf); dirty = false; io?.disconnect(); },
|
|
152
|
+
};
|
|
153
|
+
let io;
|
|
154
|
+
if ((o.onlyVisible ?? true) && typeof IntersectionObserver === 'function') {
|
|
155
|
+
io = new IntersectionObserver((e) => { visible = e[e.length - 1].isIntersecting; }, { rootMargin: '60px' });
|
|
156
|
+
io.observe(svg);
|
|
157
|
+
}
|
|
158
|
+
show(reduce || o.autoplay === false ? rest : 0);
|
|
159
|
+
if (!reduce && o.autoplay !== false)
|
|
160
|
+
p.play();
|
|
161
|
+
return p;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The static markup of a scene at one moment: render on the server, then `mount` in the browser to bring it to life.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* const s = new Scene({ view: 'iso' });
|
|
168
|
+
* s.add(ground(), box({ at: [0, 0, 0], size: 1.5, role: 'accent' }));
|
|
169
|
+
*
|
|
170
|
+
* const svg = renderStill(s); // a standalone <svg> string: on the server, or anywhere without a browser
|
|
171
|
+
*/
|
|
172
|
+
export function renderStill(scene, timeline, t) {
|
|
173
|
+
if (timeline)
|
|
174
|
+
timeline.seek(t ?? timeline.duration);
|
|
175
|
+
return scene.svg();
|
|
176
|
+
}
|
|
177
|
+
export { esc };
|
|
178
|
+
//# sourceMappingURL=mount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mount.js","sourceRoot":"","sources":["../../src/anim/mount.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AA6CxC,4CAA4C;AAC5C,MAAM,EAAE,GAAG,4BAA4B,CAAC;AAExC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,KAAK,CAAC,MAAe,EAAE,KAAY,EAAE,IAAe,EAAE;IACpE,MAAM,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC;IACtB,MAAM,GAAG,GAAG,EAAE,EAAE,QAAQ,IAAI,CAAC,CAAC;IAC9B,IAAI,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC,CAAC,CAAE,MAAwB,CAAC,CAAC,CAAE,MAAM,CAAC,aAAa,CAAC,WAAW,CAA0B,CAAC;IAC3I,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,GAAG,GAAG,QAAQ,CAAC,eAAe,CAAC,EAAE,EAAE,KAAK,CAAkB,CAAC;QAC3D,GAAG,CAAC,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC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,EAAE,CAAC;IAC9C,OAAO,CAAC,CAAC;AACX,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,KAAY,EAAE,QAAmB,EAAE,CAAU;IACvE,IAAI,QAAQ;QAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACpD,OAAO,KAAK,CAAC,GAAG,EAAE,CAAC;AACrB,CAAC;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Camera } from '../core/camera.ts';
|
|
2
|
+
import { type V3 } from '../core/math.ts';
|
|
3
|
+
import type { Scene } from '../core/scene.ts';
|
|
4
|
+
/** How far the camera may turn and zoom. Pitch and yaw are in degrees. */
|
|
5
|
+
export interface OrbitLimits {
|
|
6
|
+
/** degrees. Default [-89, 90]: from just below the ground to straight down. */
|
|
7
|
+
pitch?: [number, number];
|
|
8
|
+
/** degrees. Default: free. */
|
|
9
|
+
yaw?: [number, number];
|
|
10
|
+
/** magnification. Default [0.4, 6]. */
|
|
11
|
+
zoom?: [number, number];
|
|
12
|
+
}
|
|
13
|
+
/** Options of `orbit`. */
|
|
14
|
+
export interface OrbitOpts {
|
|
15
|
+
/** called after the camera changed. Pass `() => player.invalidate()` so the picture redraws. */
|
|
16
|
+
onChange?: () => void;
|
|
17
|
+
/** wheel and pinch zoom. Default true. */
|
|
18
|
+
zoom?: boolean;
|
|
19
|
+
/** how far the camera may turn and zoom */
|
|
20
|
+
limits?: OrbitLimits;
|
|
21
|
+
/** degrees per second of slow turning until the person touches the figure. Default off. Ignored for reduced motion. */
|
|
22
|
+
autoRotate?: number;
|
|
23
|
+
/** arrow keys turn, plus and minus zoom, 0 resets. Default true. */
|
|
24
|
+
keyboard?: boolean;
|
|
25
|
+
/** degrees per pixel of drag. Default 0.4. */
|
|
26
|
+
sensitivity?: number;
|
|
27
|
+
/** the camera state that reset returns to (a `camera.snapshot()`). Default: the camera as it is when orbit starts. */
|
|
28
|
+
home?: Record<string, number | number[]>;
|
|
29
|
+
}
|
|
30
|
+
/** Handle to a running orbit control. */
|
|
31
|
+
export interface Orbit {
|
|
32
|
+
/** back to the view the camera had when orbit was set up */
|
|
33
|
+
reset(): void;
|
|
34
|
+
/** removes the listeners and restores the element */
|
|
35
|
+
destroy(): void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* New angles after a drag of (dx, dy) pixels, in degrees. Dragging right turns the scene so its
|
|
39
|
+
* near side moves right; dragging down tips the top towards the viewer. Pitch is clamped.
|
|
40
|
+
*/
|
|
41
|
+
export declare function dragToAngles(a: {
|
|
42
|
+
yaw: number;
|
|
43
|
+
pitch: number;
|
|
44
|
+
}, dx: number, dy: number, o?: {
|
|
45
|
+
sensitivity?: number;
|
|
46
|
+
limits?: OrbitLimits;
|
|
47
|
+
}): {
|
|
48
|
+
yaw: number;
|
|
49
|
+
pitch: number;
|
|
50
|
+
};
|
|
51
|
+
/** Multiplies a zoom by a factor and keeps it inside the limits. */
|
|
52
|
+
export declare function clampZoom(zoom: number, factor: number, range?: [number, number]): number;
|
|
53
|
+
/** Where a perspective camera sits when it looks at `target` from `distance` along the given angles (same angles as the parallel camera). */
|
|
54
|
+
export declare function orbitPosition(target: V3, yaw: number, pitch: number, distance: number): V3;
|
|
55
|
+
/** The angles and distance of a perspective camera, the inverse of `orbitPosition`. */
|
|
56
|
+
export declare function anglesOf(position: V3, target: V3): {
|
|
57
|
+
yaw: number;
|
|
58
|
+
pitch: number;
|
|
59
|
+
distance: number;
|
|
60
|
+
};
|
|
61
|
+
/** Reads the current angles from either kind of camera. */
|
|
62
|
+
export declare function readAngles(cam: Camera): {
|
|
63
|
+
yaw: number;
|
|
64
|
+
pitch: number;
|
|
65
|
+
};
|
|
66
|
+
/** Points a camera along new angles. A perspective camera keeps its target and distance. */
|
|
67
|
+
export declare function writeAngles(cam: Camera, yaw: number, pitch: number): void;
|
|
68
|
+
/**
|
|
69
|
+
* Turns the camera of a scene with the pointer, wheel, touch and keyboard. The scene needs
|
|
70
|
+
* `fit: 'sphere'` (or a locked fit) so the object keeps its size while it turns.
|
|
71
|
+
*
|
|
72
|
+
* const player = mount(el, scene, { timeline });
|
|
73
|
+
* orbit(el, scene, { onChange: () => player.invalidate() });
|
|
74
|
+
*
|
|
75
|
+
* If a timeline animates the camera, the timeline wins while it plays.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* const s = new Scene({ fit: 'sphere' });
|
|
79
|
+
* s.add(ground(), box({ at: [0, 0, 0], size: 1.5, role: 'accent' }));
|
|
80
|
+
*
|
|
81
|
+
* const el = document.querySelector('#figure')!;
|
|
82
|
+
* const player = mount(el, s);
|
|
83
|
+
* orbit(el.querySelector('svg')!, s, { onChange: () => player.invalidate() }); // drag, wheel, pinch and arrow keys
|
|
84
|
+
*/
|
|
85
|
+
export declare function orbit(target: Element, scene: Scene, o?: OrbitOpts): Orbit;
|