@pencil-lang/figure 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,6 +8,7 @@ compiles is only read by the WASM built from the same source.
8
8
  | `@pencil-lang/figure` | Defines `<pencil-figure>`, and exports `PencilFigure`. |
9
9
  | `@pencil-lang/figure/host` | The WASM as a page calls it: `ready()`, `Player`, `gather`, `uses`, `inspect`, `from_latex` and the rest. |
10
10
  | `@pencil-lang/figure/pack` | `pack` and `unpack`: a figure's sources as a string for a link's `#pen=`, which the playground opens. |
11
+ | `@pencil-lang/figure/controls` | What writes the default controls (`markup`, `declarative`, `CONTROLS_CSS`), and `readoutHtml`, which sets a readout in a page's own box. |
11
12
  | `@pencil-lang/figure/astro` | The Astro integration (the default export), `build()` and `alternates()`. |
12
13
  | `@pencil-lang/figure/Figure.astro` | The Astro component. |
13
14
 
@@ -29,6 +30,20 @@ It owns the canvas, the labels over it, the GPU and the clock. It starts the GPU
29
30
  under `prefers-reduced-motion`, and every handle and beat can be worked from the keyboard. Its attributes, methods and
30
31
  events are described at the top of [`src/element.js`](src/element.js).
31
32
 
33
+ A figure's shaders are compiled after its sources, while the figure before it, or its poster, stays on screen and draws
34
+ on. Its pipelines are made with wgpu's `create_render_pipeline_async`, which on WebGPU is `createRenderPipelineAsync`,
35
+ so the browser compiles them off the thread that composites the page. Chrome compiles a pipeline made with
36
+ `createRenderPipeline` on that thread, and a page of figures stopped for 150 to 800 ms as each one started. `load` and
37
+ `loadCompiled` throw a compile error at once, and resolve with `true` once the figure is shown, however long the GPU
38
+ takes to start, or with `false`: one asked for while another is compiling takes its place, one the GPU can't make is
39
+ an `error`, and one whose GPU can't start, or whose figure leaves the page, isn't shown, though it's still what the
40
+ figure starts from if it does. The `error` has `detail.kept`: `true` when the figure before stays on screen, and
41
+ `false` when there was none on the player, as for a first figure, or after the GPU started again, which then shows the
42
+ figure that drew before. A figure far off screen has no GPU yet, so its `load` resolves only once it comes near, and a
43
+ page that awaits several figures' loads in turn waits at the first one off screen. A figure's handles and keys wait for
44
+ it too. WebGL2 compiles a pipeline where it's made, as before. A page that drives a `Player` itself does
45
+ `player.install(await player.prepare(doc, sheets, names, files))` ([`src/host.js`](src/host.js)).
46
+
32
47
  A param with a binding goes back to it when a reader sets a param that binding reads, as θ in Bounds does when `q` or
33
48
  the case is set after a drag: the core says which params each binding reads (`info.params[].reads`), and the element
34
49
  hands them back, so a page needs no `release` of its own for it. Params held together, by the `params` attribute or
@@ -53,6 +68,109 @@ a handle's reach drags the handle and doesn't scroll the page. One anywhere else
53
68
  page, as it would over an image, so a figure never traps a reader's scroll. A drag never selects text, in the figure or
54
69
  on the page around it, and a long press shows no callout.
55
70
 
71
+ ## Controls
72
+
73
+ ```html
74
+ <pencil-figure controls src="tube.pen" sheets="theme.style tube.style"></pencil-figure>
75
+ ```
76
+
77
+ `controls` asks for the default controls, as `<video controls>` does: a play button and time when anything follows time,
78
+ beat stepping with each beat's caption, a control for each param a handle doesn't set, and the readouts, each label the
79
+ document writes without `at`. Each param's control says its caption, `param q "arms" = 8 in …`, or its name, and is the
80
+ kind the figure and its sheets give it: a slider with its value after it for a range, radios for a choice of four options
81
+ or fewer, a `<select>` for more, and a checkbox for a choice of off and on (`docs/language.md`, Controls and readouts).
82
+
83
+ - **They're keyboard controls,** native ones: Tab reaches each, the arrows move a slider and pick within a group of radios,
84
+ Space and Enter press the play button, and each has a focus ring.
85
+ - **A screen reader gets each control's caption as its name** and its value as its text (`aria-valuetext` on a slider), the
86
+ radios as a group named for the caption, and each readout's words, with its mathematics in words ("z squared = 1").
87
+ - **They're `<pencil-controls slot="controls">`,** a child of the figure whose shadow root holds them. The element writes
88
+ one when the figure loads, if the page has none. The Astro component writes it at build, with `controls`, as a
89
+ declarative shadow root, disabled until the figure loads and with every size reserved, so nothing moves. One whose
90
+ declarative shadow root wasn't read, as in HTML a script put in the page, gets the controls written when the figure
91
+ loads. Without `controls` the defaults are hidden, and whatever the page put in `<pencil-controls>` stays.
92
+ - **Nothing in them moves** when the figure loads, as a value changes or as time plays. An output is as wide as the widest
93
+ value its param's range shows, every beat's caption lies in one place, the play button is as wide as the wider of its
94
+ words, and a readout's box is as big as the most it says over the moments a reader can reach, from the one the figure
95
+ starts at, which the core tries (`readout_room`). Each sits in a contained box. Every slider is in one grid, so each
96
+ starts and ends where the others do, and a long caption wraps rather than squeezing its slider.
97
+ - **On a phone,** or a screen with little height, one row stays, on one line, in a bar of fixed height, and the rest drop
98
+ down from a `⋯` button in a menu over the page below, as wide as the controls. The figure keeps the height. The bar
99
+ keeps the playback, which is the time or else the beats, or else the readouts, or else a figure's one param. It never
100
+ splits a figure's params: one with more than one and nothing else has a bar of only the button, `Controls ⋯`, and its
101
+ params together in the menu. A row that doesn't fit the bar's line, as radios with long words, goes behind the menu
102
+ too, and the button says "Controls", so nothing in the bar is cut off. A row that comes to fit again, as when a phone
103
+ turns, comes back to the bar. The beats' caption is a line of its own under the bar, wherever the stepper is, as tall
104
+ as the longest caption at that width, so stepping moves nothing, and the menu drops from under it.
105
+ - **The menu scrolls with the page,** under its bar, and follows the bar wherever else it goes: when something above it
106
+ grows, or a sticky or fixed box carries it, and a frame behind when a scrolling box it's in scrolls. It closes when
107
+ its controls leave the screen, so it never stays over another figure. Opening one low on the screen scrolls the page
108
+ just enough to show it, never so far that the bar leaves the top, and opening another figure's closes it. It's at most
109
+ 60% of the screen's height, and scrolls within that. Escape closes it and gives the focus back to `⋯`, and controls
110
+ written again, as for another look, keep an open menu open where it was, and the focus where it was, without
111
+ scrolling. The button is called "More controls" beside the bar's row and "Controls" alone, and a build writes it
112
+ disabled, as it does the rest, until the figure loads.
113
+ - **A new beat is said,** in a polite live region: which beat it is and its caption, `2 / 3: Then as many…`, whether
114
+ a reader stepped to it or time brought it.
115
+ - **Anything a page puts in `<pencil-controls>`,** as a choice of looks, is shown after the defaults, and on a phone in
116
+ their menu. With the Astro component it goes in the `controls` slot: `<Figure …><label slot="controls">…</label></Figure>`.
117
+
118
+ **Styling.** The defaults are styled through `::part`, as `pencil-controls::part(slider)`, and these custom properties,
119
+ set on the figure or anything around it:
120
+
121
+ | Custom property | What it is |
122
+ |---|---|
123
+ | `--pencil-controls-font` | Their text, as the `font` shorthand. |
124
+ | `--pencil-ink` | Values, a param's name and readouts. |
125
+ | `--pencil-muted` | What a control says it is, and captions. |
126
+ | `--pencil-accent` | Sliders, radios and checkboxes. |
127
+ | `--pencil-rule`, `--pencil-surface` | A button's border and background, and the rule is the menu's border on a phone. |
128
+ | `--pencil-surface-menu` | The menu's background on a phone. |
129
+ | `--pencil-math-font` | A param's name, as the `font` shorthand. |
130
+ | `--pencil-focus` | The focus ring, as on the figure's handles. |
131
+ | `--pencil-readout-size` | A readout's text size. |
132
+
133
+ | Part | What it is |
134
+ |---|---|
135
+ | `controls`, `bar`, `rest` | All of them, the row a phone's bar keeps with the menu's button, and the rest, which on a phone is the menu. |
136
+ | `row` | Each row, with `time`, `beats`, `param`, `choice` or `readouts` as well. |
137
+ | `label` | What a control says it is: a `<label>`, or a group's `<legend>`. |
138
+ | `slider`, `select`, `radio`, `checkbox`, `option` | Each control, and a radio's label. |
139
+ | `value` | A slider's value, the time, or the beat (with `time` or `beat` as well). |
140
+ | `button` | Each button, with `play`, `step` or `more` as well. |
141
+ | `captions`, `caption` | The beats' captions, and each. |
142
+ | `readout` | Each readout's box. |
143
+
144
+ The canvas and the poster are the figure's `stage` part, so a page's background or focus ring for the figure goes there,
145
+ `pencil-figure::part(stage)`, and not on the controls.
146
+
147
+ **A page's own controls.** A page that wants its own puts them in the `controls` slot, marked with what each is for, and
148
+ the element sets each one's range, step and value from the figure, follows them every frame, and holds a param when a
149
+ reader sets one, with or without the `controls` attribute:
150
+
151
+ ```html
152
+ <pencil-figure src="tube.pen" sheets="theme.style tube.style">
153
+ <div slot="controls">
154
+ <label>Arms <input type="range" data-param="p"></label> <output data-param="p"></output>
155
+ <span data-readout="ratio" style="display: inline-block; width: 8em; height: 1.4em; contain: strict"></span>
156
+ </div>
157
+ </pencil-figure>
158
+ ```
159
+
160
+ | Marked | What the element does |
161
+ |---|---|
162
+ | `input[type=range][data-param="q"]` | Sets its `min`, `max`, `step` and value from `q`, and its `aria-valuetext`. Moving it pauses and holds `q`. |
163
+ | `select[data-param="along"]` | Fills in the choice's options if it has none, and holds which one is picked. A choice is named by its value, `along`, or its param, `along.index`. |
164
+ | `input[type=radio][data-param="along"][value="1"]` | Checks the one whose value is the option's place, from 0. |
165
+ | `input[type=checkbox][data-param="glow"]` | Checked for the second option, on. |
166
+ | `output[data-param="q"]` | Says `q`'s value, as a control does. |
167
+ | `[data-readout="ratio"]` | Says what the readout `ratio` says, laid out as the figure lays it out, with its words for a screen reader (`readoutHtml`). A page gives it its size. |
168
+ | `button[data-play]`, `input[type=range][data-time]`, `output[data-time]` | Play and pause, and the time. |
169
+ | `button[data-step="1"]`, `button[data-step="-1"]`, `output[data-beat]`, `[data-captions]` | Beats: next, previous, which one, and its caption, the child of `[data-captions]` in its place shown with `data-on`. |
170
+
171
+ The figure's `readouts` is what its readouts say this frame, each `{ id, text, words, math, width, height, laid }`, in
172
+ ems, for a page that sets them itself.
173
+
56
174
  Labels are set in the package's own fonts, in [`fonts/`](fonts/README.md): Libertinus, under the SIL Open Font License.
57
175
  They're the files the `pencil` command draws a poster with, so the figure that takes over from a poster sets the same
58
176
  glyphs in the same places. The element fetches the two text faces when it loads, and the math font the first time a
@@ -118,13 +236,17 @@ import Figure from '@pencil-lang/figure/Figure.astro';
118
236
  is built again, with a `pencil` from the same source as the package. A build names the folders nothing asked for,
119
237
  and leaves them: one that skips drafts asks for fewer. A figure is built elsewhere and put in the folder only once
120
238
  it has built, so a build that fails, or has no `pencil`, never removes what's kept.
239
+ - **`controls` writes the default controls,** at the poster's moment and disabled, with every size reserved, so nothing
240
+ moves when the figure loads (see Controls). What a page puts in the component's `controls` slot comes after them.
121
241
  - **`alt` is required.** It's the poster's text and the figure's name.
122
242
  - Anything else, such as `id`, `class`, `params`, `paused` or `data-*`, goes on `<pencil-figure>`.
123
243
 
124
244
  `build({ src, sheets, time, params, size, card })` is what the component calls. A page can call it too, for a
125
- figure's `info` (its size, params with their ranges, beats and captions, and the colors its sheets name) and its
126
- `moment` (the time, and every param's value and range, where the poster shows it), to write its controls in their
127
- first state before any script runs, or with `card: true` for a 1200×630 image for sharing.
245
+ figure's `info` (its size, params with their ranges, captions and controls, beats and captions, readouts with the room
246
+ each needs, and the colors its sheets name) and its `moment` (the time, every param's value and range, and what each
247
+ readout says, where the poster shows it), to write controls of its own in their first state before any script runs, or
248
+ with `card: true` for a 1200×630 image for sharing. `declarative({ info, moment, paused, disabled })` from
249
+ `@pencil-lang/figure/controls` is the defaults' shadow root, as the component writes it.
128
250
 
129
251
  `alternates({ sheets, dark, narrow, narrowMedia })` gives the sets of sheets the component builds a figure under: its
130
252
  own as `sheets`, and as `others` each `{ media, sheets }` it writes a `<source>` for. A page that swaps one figure
@@ -157,7 +279,8 @@ pnpm test
157
279
 
158
280
  Playwright's, in [`tests/`](tests), against the corpus in this repo, headless, on its own Chromium and WebKit: each at
159
281
  a desktop size and as a phone (a Pixel 7 and an iPhone 15, emulated). They cover drawing, every key on a handle and on
160
- beats, a mouse drag and a touch drag, a swipe that scrolls past a figure, a drag that selects no text, a range that follows another param, reduced motion, sheets picked by theme, the
282
+ beats, the default controls and a page's own, a phone's bar and where its menu opens as the page scrolls, readouts, and that no control moves as the figure loads, plays or is
283
+ dragged, a mouse drag and a touch drag, a swipe that scrolls past a figure, a drag that selects no text, a range that follows another param, reduced motion, sheets picked by theme, the
161
284
  poster's swap, and a compiled figure. `PENCIL_GL=1 pnpm test` runs them all on WebGL2 in place of WebGPU, and `PENCIL_MIN=1 pnpm test` on the minified bundle. They serve the repo on
162
285
  port 8743, and reuse a server already there, so a second checkout runs them on its own with `PENCIL_PORT=8751 pnpm test`.
163
286
  [`playwright.config.js`](playwright.config.js) says which GPU path each browser has headless. An emulated phone is a
@@ -21,9 +21,15 @@
21
21
  // WASM, so a line of script in the page starts on them as it's read, behind the poster.
22
22
  // A page that pins a theme over the reader's system says so with `color-scheme` in its CSS, and
23
23
  // a figure with `dark` sheets follows it (`src/scheme.js`), from the first paint.
24
+ // `controls` writes the default controls into the page, from what the build says of the figure, as
25
+ // it will stand when it starts and disabled, with every size reserved, so they have their place
26
+ // before any script runs and nothing moves when the figure loads. They're `<pencil-controls>`, with
27
+ // a declarative shadow root, and the element turns them on. Anything the page puts in the
28
+ // component's `controls` slot is shown after them, and on a phone in their menu.
24
29
  // Anything else (`id`, `class`, `params`, `paused`, `gl`, `data-*`) goes on `<pencil-figure>`.
25
30
  import { alternates, build } from './build.js';
26
31
  import { settle } from '../src/scheme.js';
32
+ import { declarative, hasControls, shape as controlsShape } from '../src/controls.js';
27
33
  import wasm from '../pkg/pencil_web_bg.wasm?url';
28
34
 
29
35
  interface Props {
@@ -37,10 +43,13 @@ interface Props {
37
43
  poster?: { time?: number; params?: Record<string, number> };
38
44
  /** The figure is in view when the page opens. */
39
45
  priority?: boolean;
46
+ /** The default controls, written at build: play and time, beats, a control for each param a
47
+ * handle doesn't set, and the readouts. */
48
+ controls?: boolean;
40
49
  [attribute: string]: unknown;
41
50
  }
42
51
 
43
- const { src, sheets, dark, narrow, narrowMedia, alt, poster = {}, priority = false, ...attributes } = Astro.props;
52
+ const { src, sheets, dark, narrow, narrowMedia, alt, poster = {}, priority = false, controls = false, ...attributes } = Astro.props;
44
53
  // As in `<picture>`, the first choice that matches wins, so the narrowest case comes first.
45
54
  const { sheets: always, others: choices } = alternates({ sheets, dark, narrow, narrowMedia });
46
55
 
@@ -79,12 +88,22 @@ const start = (themed || early)
79
88
  ? `((f)=>{${themed ? `(${String(settle).replace(/\n\s+/g, '\n')})(f);` : ''}${early ? fetchAhead : ''}})(document.currentScript.parentElement)`
80
89
  : undefined;
81
90
  const mark = reshaped.length ? main.hash : undefined;
91
+ // The controls as the figure starts: at the poster's moment, paused if the page starts it paused,
92
+ // and disabled until it loads. A figure that doesn't compile, in `astro dev`, gets its controls
93
+ // from the element once it does.
94
+ // What the page put in the `controls` slot, which can be nothing: a slot written with a condition is
95
+ // there whether or not the condition holds.
96
+ const own = Astro.slots.has('controls') ? (await Astro.slots.render('controls')).trim() : '';
97
+ const extra = own !== '';
98
+ const written = controls && main.info && (hasControls(main.info) || extra)
99
+ ? declarative({ info: main.info, moment: main.moment, paused: Boolean(attributes.paused), disabled: true, extra })
100
+ : null;
82
101
  const rules = reshaped
83
102
  .map((each) => `@media ${each.media}{[data-pencil-poster="${mark}"] img{--pencil-shape:${each.shape}}}`)
84
103
  .join('');
85
104
  ---
86
105
  {mark && <style is:inline set:html={rules}></style>}
87
- <pencil-figure {...attributes} {...from(main)} aria-label={alt} data-pencil-poster={mark}>
106
+ <pencil-figure {...attributes} {...from(main)} aria-label={alt} data-pencil-poster={mark} controls={controls || undefined}>
88
107
  {others.map((built, i) => <source media={choices[i].media} {...from(built)} />)}
89
108
  {main.poster && (
90
109
  <picture style={shape(main) && `--pencil-shape: ${shape(main)}`}>
@@ -109,6 +128,12 @@ const rules = reshaped
109
128
  </picture>
110
129
  )}
111
130
  {start && <script is:inline set:html={start}></script>}
131
+ {(written || extra) && (
132
+ <pencil-controls slot="controls" data-shape={written ? controlsShape(main.info) : undefined}>
133
+ {written && <Fragment set:html={written} />}
134
+ {extra && <Fragment set:html={own} />}
135
+ </pencil-controls>
136
+ )}
112
137
  </pencil-figure>
113
138
 
114
139
  <script>
package/astro/build.d.ts CHANGED
@@ -37,6 +37,20 @@ export declare function alternates(options: {
37
37
  narrowMedia?: string;
38
38
  }): { sheets: string[]; others: Choice[] };
39
39
 
40
+ /** What a readout says, as the element's `readouts` give it and `readoutHtml` sets it. */
41
+ export interface Readout {
42
+ id: string;
43
+ text: string;
44
+ words: string;
45
+ math: boolean;
46
+ width: number;
47
+ height: number;
48
+ laid: {
49
+ runs: { text: string; x: number; top: number; size: number; math: boolean; stretch: number }[];
50
+ rules: { x: number; y: number; width: number; height: number }[];
51
+ } | null;
52
+ }
53
+
40
54
  /** What a figure says of itself, as the element's `info` does. */
41
55
  export interface FigureInfo {
42
56
  /** Its design size, `[width, height]`. */
@@ -57,7 +71,15 @@ export interface FigureInfo {
57
71
  /** The other params its binding reads: a reader setting one of them hands it back to its binding. */
58
72
  reads: string[];
59
73
  choice: { of: string; options: string[] } | null;
74
+ /** What its control says, as the document writes it: `param q "arms" = …`. */
75
+ caption: string | null;
76
+ /** Its control, as the sheets say: its name, which `style q.control` reaches, its kind, and
77
+ * which parts of `caption`, `name` and `value` it says. */
78
+ control: { name: string; kind: 'slider' | 'radios' | 'select' | 'checkbox'; text: ('caption' | 'name' | 'expr' | 'value')[] };
60
79
  }[];
80
+ /** Each label with no place, said beside the figure, in the order it's written, with the room it
81
+ * needs, `[width, height]` in ems, so nothing beside it moves as it changes. */
82
+ readouts: { id: string; room?: [number, number] }[];
61
83
  /** Each mark a reader can move, in every panel: its panel, its mark, and the params it sets, one or
62
84
  * two (a point dragged in two dimensions). */
63
85
  handles: { panel: number; mark: string; params: string[] }[];
@@ -77,7 +99,13 @@ export interface Built {
77
99
  card: { src: string; width: number; height: number } | null;
78
100
  info: FigureInfo | null;
79
101
  /** The figure as its poster shows it: the time, and every param's value and range by name. */
80
- moment: { time: number; values: Record<string, number>; ranges: Record<string, [number, number]> } | null;
102
+ moment: {
103
+ time: number;
104
+ values: Record<string, number>;
105
+ ranges: Record<string, [number, number]>;
106
+ /** What each readout says there, laid out in ems, as the element's `readouts` are. */
107
+ readouts: Readout[];
108
+ } | null;
81
109
  /** A link that opens the figure's sources in a playground, when the integration has one. */
82
110
  playground: string | null;
83
111
  hash: string | null;
package/astro/build.js CHANGED
@@ -193,8 +193,9 @@ export async function replace(dir, built) {
193
193
  *
194
194
  * Gives `{ compiled, poster: { src, srcset, webp, width, height }, card, info, moment, hash }`,
195
195
  * with URLs from the site's root. `moment` is the figure as its poster shows it, which is how a
196
- * page that holds the same params starts it: `{ time, values, ranges }`, the last two for every
197
- * param by name, so a page can write its controls as they'll be before the figure runs.
196
+ * page that holds the same params starts it: `{ time, values, ranges, readouts }`, the middle two for
197
+ * every param by name, and what each readout says, so a page can write its controls as they'll be
198
+ * before the figure runs.
198
199
  * `playground` is a link that opens the figure's sources in a playground, when the integration
199
200
  * was told where one is: the page ships none of them, so the link carries them all. `webp` is a `srcset` of the same poster as WebP, where that's the smaller,
200
201
  * and in `astro dev` also `src` and `sheets`, the sources' URLs, which the
@@ -295,7 +296,12 @@ async function make(cfg, key, { src, sheets, time, params, size, card }) {
295
296
  },
296
297
  card: manifest.card ? { src: url(manifest.card.src), width: manifest.card.width, height: manifest.card.height } : null,
297
298
  info: manifest.info,
298
- moment: { time: manifest.poster.time, values: manifest.poster.values, ranges: manifest.poster.ranges },
299
+ moment: {
300
+ time: manifest.poster.time,
301
+ values: manifest.poster.values,
302
+ ranges: manifest.poster.ranges,
303
+ readouts: manifest.poster.readouts ?? [],
304
+ },
299
305
  playground: await playgroundLink(cfg, stem, src, sheets, manifest.sources),
300
306
  hash: manifest.hash,
301
307
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pencil-lang/figure",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Pencil figures on a page: the <pencil-figure> element, the WASM host it runs on, and an Astro component that builds a figure and its poster.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,6 +22,7 @@
22
22
  ".": "./src/element.js",
23
23
  "./host": "./src/host.js",
24
24
  "./pack": "./src/pack.js",
25
+ "./controls": "./src/controls.js",
25
26
  "./astro": {
26
27
  "types": "./astro/index.d.ts",
27
28
  "default": "./astro/index.js"