@alexpeng/dsh-custom-plugin 0.9.3 → 0.11.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +1730 -866
  2. package/NOTICE +71 -0
  3. package/README.i18n.yaml +7 -7
  4. package/README.md +344 -335
  5. package/README.zh.md +309 -302
  6. package/lib/client.js +10260 -2063
  7. package/lib/client.js.map +1 -1
  8. package/lib/index.js +235 -99
  9. package/lib/types/balance.d.ts +44 -0
  10. package/lib/types/client/api.d.ts +13 -8
  11. package/lib/types/client/bg-ascii.d.ts +405 -0
  12. package/lib/types/client/bg-field.d.ts +62 -0
  13. package/lib/types/client/bg-focus.d.ts +63 -0
  14. package/lib/types/client/bg-jelly.d.ts +53 -0
  15. package/lib/types/client/bg-layer.d.ts +152 -0
  16. package/lib/types/client/bg-ocean.d.ts +31 -0
  17. package/lib/types/client/bg-params.d.ts +142 -0
  18. package/lib/types/client/bg-pointer.d.ts +107 -0
  19. package/lib/types/client/bg-shader.d.ts +329 -0
  20. package/lib/types/client/bg-wake.d.ts +57 -0
  21. package/lib/types/client/bg-whale.d.ts +56 -0
  22. package/lib/types/client/custom.d.ts +4 -1
  23. package/lib/types/client/index.d.ts +1 -1
  24. package/lib/types/client/palette.d.ts +100 -0
  25. package/lib/types/client/panel-bounds.d.ts +65 -0
  26. package/lib/types/client/styles.d.ts +9 -4
  27. package/lib/types/client/ui/anchor.d.ts +55 -0
  28. package/lib/types/client/ui/appearance-controls.d.ts +24 -0
  29. package/lib/types/client/ui/command-model.d.ts +28 -0
  30. package/lib/types/client/ui/folders-model.d.ts +31 -0
  31. package/lib/types/client/ui/index-bridge.d.ts +6 -0
  32. package/lib/types/client/ui/overlays.d.ts +66 -0
  33. package/lib/types/client/ui/primitives.d.ts +93 -0
  34. package/lib/types/client/ui/prompt-model.d.ts +37 -0
  35. package/lib/types/host-service.d.ts +68 -15
  36. package/lib/types/pricing.d.ts +8 -4
  37. package/lib/types/protocol.d.ts +48 -2
  38. package/lib/types/usage.d.ts +8 -4
  39. package/locale/en.json +1 -1
  40. package/locale/zh.json +1 -1
  41. package/package.json +106 -105
@@ -0,0 +1,405 @@
1
+ /**
2
+ * ASCII character backdrop: the renderer that lives inside `div#vx-bg`.
3
+ *
4
+ * The hot loop never builds strings. Each piece writes one ramp level per
5
+ * cell into a reused `Uint8Array`, and the renderer blits a pre-baked glyph
6
+ * atlas with `drawImage` only — one atlas per (theme, font, dpr, tile size),
7
+ * rebuilt on demand and never per frame. That is what keeps a 15 fps field of
8
+ * ~10k cells free of per-frame allocations and `fillText` metric work.
9
+ *
10
+ * Motion is read from the layer's `data-vx-bg-motion` attribute and the
11
+ * surface from `--vx-bg-tint`; page visibility and the reduced-motion media
12
+ * query are observed here as well. Degradation follows a fixed ladder —
13
+ * frame rate → density → mono → dither → static frame → off — and never
14
+ * climbs back on its own inside one session.
15
+ *
16
+ * Phase 3a ships the ASCII system presets — `flow`, `rain`, `ocean`,
17
+ * `starfield`, `ripple`, plus the zero-rAF `static` tier. Each preset is its
18
+ * own piece (falling trails, horizontal swells, sparse stars, concentric
19
+ * rings), not the flow field with different knobs. Switching presets reuses
20
+ * the canvas and the grid buffer; the atlas is rebuilt only when the ramp,
21
+ * ink, theme or cell geometry changes.
22
+ * @module @alexpeng/dsh-custom-plugin/client/bg-ascii
23
+ */
24
+ import { type BgFocusRect } from './bg-focus.ts';
25
+ import { type AsciiPiece } from './bg-field.ts';
26
+ import { type BgPointerEventLike } from './bg-pointer.ts';
27
+ export { FLOW_LEVEL_FLOOR, quantizeLevel } from './bg-field.ts';
28
+ export type { AsciiInput, AsciiPiece } from './bg-field.ts';
29
+ export { createOceanPiece } from './bg-ocean.ts';
30
+ export { createWhalePiece } from './bg-whale.ts';
31
+ export { createJellyPiece } from './bg-jelly.ts';
32
+ /** Upper bound for the backing-store scale. */
33
+ export declare const DPR_CAP = 2;
34
+ /** Hard grid caps: beyond these the cell size grows instead of the grid. */
35
+ export declare const MAX_COLS = 240;
36
+ /** Hard grid caps: beyond these the cell size grows instead of the grid. */
37
+ export declare const MAX_ROWS = 96;
38
+ /** Monospace stack; `ui-monospace` resolves locally, so nothing is fetched. */
39
+ export declare const MONO_STACK = "ui-monospace, SFMono-Regular, Menlo, Consolas, \"Cascadia Mono\", \"DejaVu Sans Mono\", monospace";
40
+ /** Glyph ramps. Only guaranteed-monospace ASCII; CJK and box drawing are
41
+ * deliberately excluded because their advance is not 0.6 em in every face. */
42
+ export type AsciiCharset = 'classic' | 'blocks' | 'dots';
43
+ /** Glyph ramps ordered by increasing ink density. */
44
+ export declare const CHARSET: Record<AsciiCharset, string>;
45
+ /** Every ASCII piece id. `static` is the zero-rAF tier. */
46
+ export type AsciiPreset = 'flow' | 'rain' | 'ocean' | 'whale' | 'jelly' | 'starfield' | 'ripple' | 'static';
47
+ /** Inclusive numeric bounds a future settings panel may write. */
48
+ export interface AsciiRange {
49
+ readonly min: number;
50
+ readonly max: number;
51
+ }
52
+ /** Writable bounds per preset; defaults above sit inside these. */
53
+ export interface AsciiPresetRanges {
54
+ readonly density: AsciiRange;
55
+ readonly speed: AsciiRange;
56
+ readonly fps: AsciiRange;
57
+ readonly fontPx: AsciiRange;
58
+ readonly opacity: AsciiRange;
59
+ }
60
+ /** Tunables of one preset. */
61
+ export interface AsciiPresetSpec {
62
+ readonly density: number;
63
+ readonly speed: number;
64
+ readonly fps: number;
65
+ readonly charset: AsciiCharset;
66
+ readonly dither: boolean;
67
+ readonly opacity: number;
68
+ readonly fontPx: number;
69
+ /** Per-piece ink multiplier on the theme's base ΔL. The dense `blocks`
70
+ * pieces need none (1.0); the `dots` pieces carry far less ink per cell, so
71
+ * they lift the ink to reach the same perceived strength. Also multiplied by
72
+ * the user's soft/standard/strong strength choice. */
73
+ readonly inkScale: number;
74
+ /** The piece consumes pointer input and may mark interaction cells in the
75
+ * emphasis buffer. `flow` and `ocean` opt in today; `static` deliberately
76
+ * does not, so its one frame stays deterministic. */
77
+ readonly interactive?: boolean;
78
+ /** Picture presets keep the full marked ink inside the calm zone. The
79
+ * reading-column mask exists to thin a texture, but a preset whose subject
80
+ * is a drawn animal has no texture to thin: at the 0.65 emphasis floor the
81
+ * whale measured as a ghost behind the content surfaces (Desktop pass,
82
+ * 2026-12-03), which defeats the preset. Off for every texture preset. */
83
+ readonly emphasisFull?: boolean;
84
+ /** Shape-aware rendering: a cell on a contour swaps its ramp glyph for a
85
+ * directional stroke of similar ink weight (the MIT `glyph-graphics` idea).
86
+ * The `dots` pieces and `rain` keep their plain ramp. */
87
+ readonly shapeAware?: boolean;
88
+ readonly ranges: AsciiPresetRanges;
89
+ }
90
+ /**
91
+ * System presets. `flow` is the default animated tier; `static` draws one
92
+ * deterministic flow frame (zero rAF). The other four are genuinely different
93
+ * pieces: vertical falling trails, horizontal swells, sparse parallax stars
94
+ * and concentric rings. `density` varies the cell size, so only `flow` and
95
+ * `static` share one baked atlas today.
96
+ *
97
+ * `inkScale` is the per-piece visibility calibration (measurements in
98
+ * AGENTS.md, 2026-11-08, 1280×822 dpr 1.5, reading column over the composited
99
+ * page): a dot glyph carries ~1/6 of a block glyph's ink, so the dots pieces
100
+ * need a bigger per-cell step to be seen at all, and the sparse starfield
101
+ * needs a brighter point rather than more area. These numbers are measured,
102
+ * not taste: `flow` at 1.0 reads 0.15% mean ink mass / 2.99% peak; `ocean` at
103
+ * 1.4 with density 0.65 reads 0.17% / 4.37%; `ripple` at 1.25 reads 0.11% /
104
+ * 3.11%; `starfield` at 1.6 reads a 5.15% star peak while staying <0.4%
105
+ * covered; `rain` stays at 1.1 because its 13.3% inter-frame change (the
106
+ * highest of the six) is what carries it, not its ink mass.
107
+ */
108
+ export declare const ASCII_PRESETS: Record<AsciiPreset, AsciiPresetSpec>;
109
+ /** Resolve a stored preset id to an ASCII piece. A shader-tier id (or any
110
+ * unknown string) is a stale combination and falls back to `flow`. */
111
+ export declare function resolveAsciiPreset(preset: string | undefined): AsciiPreset;
112
+ /** Panel overrides for one ASCII preset. Every value is clamped into the
113
+ * preset's own `ranges` before it is used, so an out-of-range value (a stale
114
+ * state file, a slider crossing a preset switch) never reaches the grid.
115
+ * `strength` is the user's soft/standard/strong multiplier, applied on top of
116
+ * the piece's own `inkScale`; it is clamped to the ink scale band, not to a
117
+ * preset range. */
118
+ export type AsciiParams = Partial<Record<'density' | 'speed' | 'fps' | 'fontPx' | 'opacity' | 'strength', number>>;
119
+ /**
120
+ * Resolve overrides against one preset. Missing or invalid keys fall back to
121
+ * the preset's own default, so a preset switch never inherits a value the new
122
+ * piece cannot honour. The returned `inkScale` is the piece's calibrated value
123
+ * times the user's strength multiplier. Returns the base spec object itself
124
+ * when nothing is overridden (no allocation until the user actually moves a
125
+ * slider).
126
+ */
127
+ export declare function resolveAsciiSpec(preset: AsciiPreset, params: AsciiParams): AsciiPresetSpec;
128
+ /** Resize debounce before the grid is rebuilt. */
129
+ export declare const RESIZE_DEBOUNCE_MS = 150;
130
+ /** Frames per degradation statistics window. */
131
+ export declare const LADDER_WINDOW = 60;
132
+ /** A window is "over budget" above this multiple of the frame budget. */
133
+ export declare const LADDER_OVER_BUDGET = 1.6;
134
+ /** Consecutive bad windows before one ladder step. */
135
+ export declare const LADDER_BAD_WINDOWS = 2;
136
+ /** Last rung: 0 full … 5 static frame, 6 no glyphs (surface only). */
137
+ export declare const DEGRADE_MAX = 6;
138
+ /** Inside the calm zone, cells an interactive piece marks as part of the
139
+ * effect (a ripple crest, a fish) keep this fraction of the ramp instead of
140
+ * the zone's 0.15 floor. Raised 0.5 → 0.65 on 2026-11-11: the user asked for
141
+ * the pointer response to read more clearly, and a user-caused ripple may sit
142
+ * nearer the column's own contrast than the ambient texture — the ambient
143
+ * stays at the 0.15 floor, so only what the hand just did gets brighter. */
144
+ export declare const EFFECT_FOCUS_FLOOR = 0.65;
145
+ /**
146
+ * Measure the cell in CSS pixels. `measureText` ignores the current
147
+ * transform, so the probe needs no dpr scaling: 50 glyphs cancel the
148
+ * sub-pixel rounding of a single advance. Row height is 1.15 em, leaving
149
+ * roughly 0.18 em above and below the ink so rows never touch.
150
+ */
151
+ export declare function measureCell(ctx: AsciiCtxLike, fontPx: number): {
152
+ cellW: number;
153
+ cellH: number;
154
+ };
155
+ /** A surface counts as dark below this weighted brightness (0..255). */
156
+ export declare function isDarkSurface(hex: string): boolean;
157
+ /**
158
+ * The `flow` piece: a slowly drifting value-noise field. `t` is already
159
+ * scaled by the preset speed. Dithering only makes sense on short ramps;
160
+ * at 16 levels it adds noise instead of removing banding.
161
+ *
162
+ * The field is lifted by {@link FLOW_LEVEL_FLOOR} before quantisation. That
163
+ * lift is the round-1 visibility fix, and it is a *coverage* fix, not an ink
164
+ * fix: value noise spends most of its area near 0.5, which quantises onto the
165
+ * `blocks` ramp's mid glyphs (`+` / `*`) — measured at 12 px those carry only
166
+ * 5.5% / 4.4% of a cell's ink area, against 12% for `#`, 17% for `%` and 23%
167
+ * for `@`. Raising the ink ΔL further does nothing to that (the ink is already
168
+ * near-black and the surface halves it), while the lift moves the typical cell
169
+ * from `+` to `#`-`%`, i.e. 2–3× the ink mass on screen.
170
+ */
171
+ export declare function createFlowPiece(levels: number, dither: boolean, interactive?: boolean): AsciiPiece;
172
+ /**
173
+ * The `rain` piece: falling columns. Each column owns a phase, a fall speed
174
+ * and a trail length derived from its index; roughly one column in seven is
175
+ * dormant, so the rain reads as patchy weather rather than a curtain. The
176
+ * head is the brightest cell and the trail fades quadratically behind it.
177
+ *
178
+ * The pointer bends whole columns sideways instead of editing individual
179
+ * drops: a drag is wind (displacement scaled by pointer speed and falling off
180
+ * with distance), and simply resting on the page parts the curtain into a
181
+ * tunnel around the cursor. A press pushes the nearby columns away and
182
+ * brightens only their heads for a moment — the trail itself is untouched.
183
+ * With no input every weight stays at zero, so the frame is bit-identical to
184
+ * the pre-interaction rain.
185
+ */
186
+ export declare function createRainPiece(levels: number, dither: boolean): AsciiPiece;
187
+ /**
188
+ * The `starfield` piece: sparse stars on a mostly blank field, drifting and
189
+ * twinkling slowly. The cells below the star thresholds stay at level 0, so
190
+ * the blit loop skips them and the tier is the cheapest. The thresholds are
191
+ * calibrated (see `ASCII_PRESETS.starfield`), and the field is split into
192
+ * three depth layers that drift at different speeds — far 0.05 rows/s, mid
193
+ * 0.10, near 0.18 — so the picture has parallax without a second canvas.
194
+ * The pointer shifts those layers by up to 15 px (≈1 cell horizontally) in
195
+ * proportion to its distance from the page centre, eased at 0.12 per frame.
196
+ *
197
+ * The pointer cannot make a sparse field denser without breaking its
198
+ * character, so direct interaction is light: a press scatters a burst of live
199
+ * stars and a drag drops a thin trail. Dwelling adds the slower cue — stars
200
+ * inside a 150 px ellipse brighten, and after about a second a small cluster
201
+ * of near-miss cells has accumulated; both fade out in roughly a second once
202
+ * the pointer leaves, leaving the baseline bit-identical.
203
+ */
204
+ export declare function createStarfieldPiece(levels: number, _dither: boolean): AsciiPiece;
205
+ /**
206
+ * The `ripple` piece: concentric rings from three fixed origins, plus rings
207
+ * the pointer drops. Each ring decays with distance from its origin, keeping
208
+ * the field low-contrast — crests brighten, troughs fall back toward the base,
209
+ * nothing saturates. Dithering stays off: the stipple would break the contour
210
+ * lines the rings are read through.
211
+ *
212
+ * A press drops one strong ring where it lands; its amplitude and speed grow
213
+ * with the pointer velocity, so a flick throws a harder splash than a resting
214
+ * click. A drag leaves a trail of weak ones about every 0.22 s and every five
215
+ * cells, so the pointer moves like a fingertip across water. Slots are a fixed
216
+ * ring buffer — no allocation per drop, no growth with drag length.
217
+ */
218
+ export declare function createRipplePiece(levels: number, dither: boolean): AsciiPiece;
219
+ /** Pick the piece for one preset. `static` reuses the flow field at t=0. */
220
+ export declare function createPresetPiece(preset: AsciiPreset, levels: number, dither: boolean): AsciiPiece;
221
+ /** Structural canvas surface needed by the renderer and by tests. */
222
+ export interface AsciiCtxLike {
223
+ font: string;
224
+ textAlign: string;
225
+ textBaseline: string;
226
+ globalAlpha: number;
227
+ fillStyle: string;
228
+ imageSmoothingEnabled: boolean;
229
+ measureText(text: string): {
230
+ width: number;
231
+ };
232
+ fillText(text: string, x: number, y: number): void;
233
+ fillRect(x: number, y: number, width: number, height: number): void;
234
+ setTransform(a: number, b: number, c: number, d: number, e: number, f: number): void;
235
+ drawImage(image: AsciiCanvasLike, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
236
+ }
237
+ /** Structural canvas surface needed by the renderer and by tests. */
238
+ export interface AsciiCanvasLike {
239
+ width: number;
240
+ height: number;
241
+ className: string;
242
+ style: {
243
+ width: string;
244
+ height: string;
245
+ };
246
+ getContext(contextId: '2d'): AsciiCtxLike | null;
247
+ remove(): void;
248
+ }
249
+ /** The host element (`div#vx-bg`) as the renderer sees it. */
250
+ export interface AsciiHostLike {
251
+ readonly clientWidth: number;
252
+ readonly clientHeight: number;
253
+ getAttribute(name: string): string | null;
254
+ appendChild(node: AsciiCanvasLike): unknown;
255
+ style: {
256
+ getPropertyValue(name: string): string;
257
+ };
258
+ /** Optional viewport offset. The page host sits at 0,0; the conversation
259
+ * host is a fixed layer over the column, so pointer CSS px must be shifted
260
+ * into host-local coordinates before they become grid cells. */
261
+ getBoundingClientRect?(): {
262
+ readonly left: number;
263
+ readonly top: number;
264
+ };
265
+ }
266
+ /** The slice of `Document` this renderer touches. */
267
+ export interface AsciiDocLike {
268
+ createElement(tag: string): AsciiCanvasLike;
269
+ addEventListener(type: 'visibilitychange', listener: () => void): void;
270
+ removeEventListener(type: 'visibilitychange', listener: () => void): void;
271
+ readonly hidden: boolean;
272
+ }
273
+ /** The slice of `MediaQueryList` this renderer touches. */
274
+ export interface AsciiMediaQueryLike {
275
+ readonly matches: boolean;
276
+ addEventListener(type: 'change', listener: () => void): void;
277
+ removeEventListener(type: 'change', listener: () => void): void;
278
+ }
279
+ /** The slice of `ResizeObserver` this renderer touches. */
280
+ export interface AsciiResizeObserverLike {
281
+ observe(target: unknown): void;
282
+ disconnect(): void;
283
+ }
284
+ /** The slice of `Window` this renderer touches. The pointer methods are
285
+ * optional so a minimal fake window (and a host without PointerEvent) simply
286
+ * leaves interaction off instead of failing to boot. */
287
+ export interface AsciiWinLike {
288
+ readonly devicePixelRatio: number;
289
+ readonly innerWidth?: number;
290
+ readonly innerHeight?: number;
291
+ requestAnimationFrame(callback: (now: number) => void): number;
292
+ cancelAnimationFrame(handle: number): void;
293
+ matchMedia(query: string): AsciiMediaQueryLike;
294
+ ResizeObserver?: new (callback: () => void) => AsciiResizeObserverLike;
295
+ addEventListener?: (type: string, listener: (event: BgPointerEventLike) => void, options?: {
296
+ passive?: boolean;
297
+ }) => void;
298
+ removeEventListener?: (type: string, listener: (event: BgPointerEventLike) => void, options?: {
299
+ passive?: boolean;
300
+ }) => void;
301
+ }
302
+ /** One baked glyph atlas: the ramp tiles followed by optional directional
303
+ * extras, already coloured and faded. */
304
+ export interface Atlas {
305
+ canvas: AsciiCanvasLike;
306
+ /** Tile size in CSS px (the destination unit). */
307
+ tileW: number;
308
+ tileH: number;
309
+ /** Tile size in backing px (the source unit). */
310
+ pxW: number;
311
+ pxH: number;
312
+ glyphs: string[];
313
+ /** 1 when the glyph is a space; the draw loop skips those tiles. */
314
+ empty: Uint8Array;
315
+ /** (level × 5 + direction) → tile index, or null for a plain ramp atlas. */
316
+ chart: Uint8Array | null;
317
+ }
318
+ /** Inputs for {@link buildAtlas}; the canvas factory keeps tests DOM-free. */
319
+ export interface AtlasOptions {
320
+ createCanvas: () => AsciiCanvasLike;
321
+ chars: string;
322
+ ink: string;
323
+ dark: boolean;
324
+ dpr: number;
325
+ tileW: number;
326
+ tileH: number;
327
+ fontPx: number;
328
+ opacity: number;
329
+ /** Which ramp this is; selects the directional extras. */
330
+ charset?: AsciiCharset;
331
+ /** Bake the directional extras and the chart. */
332
+ shape?: boolean;
333
+ }
334
+ /**
335
+ * Bake one coloured tile per ramp level, plus optional directional extras.
336
+ * Level 0 is the blank glyph and the last ramp level is the densest one in
337
+ * **both** themes — pieces write levels, not glyphs, so a level must mean the
338
+ * same thing whichever ink the theme picks. Empty tiles are marked for the
339
+ * draw loop. Per-level alpha is baked here too, which is why the hot loop
340
+ * never touches `globalAlpha`.
341
+ */
342
+ export declare function buildAtlas(options: AtlasOptions): Atlas | null;
343
+ /** Rolling degradation statistics. Fields stay mutable: no per-window alloc. */
344
+ export interface LadderState {
345
+ level: number;
346
+ badWindows: number;
347
+ sum: number;
348
+ count: number;
349
+ }
350
+ /** Fresh ladder pinned to rung 0. */
351
+ export declare function createLadder(): LadderState;
352
+ /**
353
+ * Feed one accepted draw interval. A window closes every
354
+ * {@link LADDER_WINDOW} frames; two consecutive over-budget windows step the
355
+ * ladder down one rung. A good window clears one bad window, so a single
356
+ * spike never degrades. Returns true when a step happened.
357
+ */
358
+ export declare function observeFrame(state: LadderState, interval: number, budget: number): boolean;
359
+ /** Observable renderer state; tests and the live probe read this. */
360
+ export interface AsciiBackdropStats {
361
+ level: number;
362
+ cols: number;
363
+ rows: number;
364
+ frames: number;
365
+ atlasBuilds: number;
366
+ /** Cells the last frame marked as interaction effects (ripple crests, fish). */
367
+ emphasisCells: number;
368
+ /** Cells the last frame drew as overlay glyphs (small fish directions). */
369
+ overlayCells: number;
370
+ alive: boolean;
371
+ }
372
+ /** Options accepted by {@link createAsciiBackdrop}. */
373
+ export interface AsciiBackdropOptions {
374
+ doc: AsciiDocLike;
375
+ win: AsciiWinLike;
376
+ host: AsciiHostLike;
377
+ preset?: AsciiPreset;
378
+ /** Panel overrides for the boot preset; clamps to its `ranges`. */
379
+ params?: AsciiParams;
380
+ }
381
+ /** Handle owned by the appearance painter. */
382
+ export interface AsciiBackdrop {
383
+ /** Re-read motion + tint and paint/resume/stop accordingly. */
384
+ sync(): void;
385
+ /** Switch piece; resets the ladder to rung 0 and repaints immediately. The
386
+ * canvas and level buffer survive; the atlas is reused when its key (ramp,
387
+ * ink, theme, dpr, cell size, font, opacity) is unchanged. */
388
+ setPreset(preset: AsciiPreset): void;
389
+ /** Replace the panel overrides for the current preset. Values are clamped
390
+ * into that preset's `ranges`; a preset switch keeps the overrides and
391
+ * re-clamps them. Repaints immediately so a dragging slider is visible. */
392
+ setParams(params: AsciiParams): void;
393
+ /** Move the calm zone (the reading column). `null` disables the mask. The
394
+ * per-cell caps are rebuilt only when the zone or the grid actually moves. */
395
+ setFocus(rect: BgFocusRect | null): void;
396
+ /** Stop everything and free the canvas, listeners, arrays and atlases. */
397
+ destroy(): void;
398
+ readonly stats: AsciiBackdropStats;
399
+ }
400
+ /**
401
+ * Create the renderer and attach its canvas to the layer. The caller mounts
402
+ * `div#vx-bg` first (so the motion attribute and tint already exist), then
403
+ * calls {@link AsciiBackdrop.sync}.
404
+ */
405
+ export declare function createAsciiBackdrop(options: AsciiBackdropOptions): AsciiBackdrop;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Shared field primitives for the ASCII backdrop pieces.
3
+ *
4
+ * A piece writes one ramp level per cell into a caller-owned `Uint8Array` and
5
+ * never allocates in its hot loop. Interaction is injected through
6
+ * {@link AsciiInput}; pieces that draw something other than water (fish
7
+ * glyphs) write a slot into the optional `overlay` buffer, and pieces whose
8
+ * cells must stay readable inside the calm zone mark them in `emphasis`.
9
+ *
10
+ * Keeping these types here (rather than in `bg-ascii.ts`) lets `bg-ocean.ts`
11
+ * depend on the contract without importing the renderer, so there is no
12
+ * module cycle between the piece and the canvas that hosts it.
13
+ * @module @alexpeng/dsh-custom-plugin/client/bg-field
14
+ */
15
+ /** One frame is one ramp-level index per cell. The extra arguments are
16
+ * optional so the four older pieces and their tests keep compiling. */
17
+ export type AsciiPiece = (t: number, out: Uint8Array, cols: number, rows: number, input?: AsciiInput, emphasis?: Uint8Array, overlay?: Uint8Array) => void;
18
+ /** Pointer state in grid coordinates. The renderer maps CSS pixels onto cells
19
+ * once per frame, so a piece never touches the DOM or the event object. */
20
+ export interface AsciiInput {
21
+ /** Pointer position in cells. */
22
+ x: number;
23
+ y: number;
24
+ /** The pointer has been seen and has not left the window. */
25
+ active: boolean;
26
+ /** Primary button held down. */
27
+ down: boolean;
28
+ /** Smoothed velocity in cells per second. */
29
+ vx: number;
30
+ vy: number;
31
+ /** Increments once per press; a piece reacts to a change. */
32
+ pulseId: number;
33
+ pulseX: number;
34
+ pulseY: number;
35
+ }
36
+ /** Glyphs a piece may place through the overlay buffer. Index 0 is "none";
37
+ * 1–8 are single-cell direction marks for the small fish, 9–13 are the body,
38
+ * edge, head and tail cells of a koi, 14–19 are the punctuation the `whale`
39
+ * piece needs for its spout and bubbles. Append-only: existing slots are
40
+ * persisted in no state, but the ocean piece indexes them by number. */
41
+ export declare const OVERLAY_GLYPHS: readonly string[];
42
+ /** Lowest field value the `flow` / `static` piece may emit, as a fraction of
43
+ * the ramp. 0.45 keeps the darkest cell around the middle of the ramp instead
44
+ * of the blank space glyph: the ink *area* per cell, not the ink darkness, is
45
+ * what the eye reads through the 0.5-alpha content surface. */
46
+ export declare const FLOW_LEVEL_FLOOR = 0.45;
47
+ /** Contrast and gamma stay internal: exposed knobs made every preset look
48
+ * worse. Values are deliberate no-ops until a preset needs them. */
49
+ export declare const CONTRAST = 1;
50
+ export declare const GAMMA = 1;
51
+ /** Bayer 4×4 ordered dither matrix (row-major). */
52
+ export declare const BAYER4: Uint8Array<ArrayBuffer>;
53
+ /** Quantize a 0..1 field value to a ramp index. Clamping lives here so a
54
+ * piece can never write outside the atlas. */
55
+ export declare function quantizeLevel(value: number, levels: number): number;
56
+ /** 32-bit integer hash → 0..1. `Math.imul` keeps the multiplications honest;
57
+ * plain `*` would lose the low bits once the product passes 2^53. */
58
+ export declare function hash2(x: number, y: number): number;
59
+ /** Smooth value noise; one octave is enough for a decorative drift field. */
60
+ export declare function valueNoise(x: number, y: number): number;
61
+ /** Shared tail of every piece: clamp, optional Bayer dither, quantize. */
62
+ export declare function shade(out: Uint8Array, i: number, v: number, levels: number, dither: boolean, row: number, col: number): void;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Spatial focus mask for the backdrop: the ink keeps full strength around the
3
+ * shell's edges and fades toward the reading column, so the texture never sits
4
+ * directly behind body copy. Geometry only — the DOM measurement lives in
5
+ * `custom.tsx`, so both renderer tiers and the unit tests share one rule.
6
+ *
7
+ * The mask is deliberately *not* a radial vignette: the fade follows the real
8
+ * content rectangle (feathered on every side), so a sidebar collapse or a
9
+ * window resize moves the calm zone with the text instead of leaving a
10
+ * mismatched circle on screen.
11
+ */
12
+ /** Rectangle in CSS pixels, top-left origin, viewport coordinates. */
13
+ export interface BgFocusRect {
14
+ x: number;
15
+ y: number;
16
+ w: number;
17
+ h: number;
18
+ }
19
+ /** Feather width of the fade, CSS px. 150 is kept from rounds 2–3, but for a
20
+ * different reason now: with the composer-span rect the visible gutter is only
21
+ * ~141 px wide (x 280..421 at 1280), so the ramp has to finish *inside* it — at
22
+ * 150 the weight reaches 0.97 at the gutter's outer edge and 1.0 a few px
23
+ * further out, which is the strongest "edge strong → body faint" the layout
24
+ * allows. Round 4 A/B'd 220 and rejected it: a band wider than the gutter
25
+ * dilutes the one region the user can actually see (canvas gutter mean 0.0141
26
+ * vs sidebar 0.0744, i.e. the gutter sat at 19% of full strength while the
27
+ * paragraph area was already down at 0.0060 — ratio 2.37×). */
28
+ export declare const FOCUS_FEATHER_PX = 150;
29
+ /** Ink retained at the middle of the reading column, as a fraction of full
30
+ * strength. Round 4 lowered this from 0.45 to 0.15: the canvas already showed
31
+ * a 3.96× mean ratio at 0.45 (column 0.0151 vs gutter 0.0599), yet the
32
+ * page-scale A/B read as one flat wash — a uniform 45% inside a zone that
33
+ * covers almost the whole content area has nothing to be compared against.
34
+ * 0.15 leaves a barely-there texture behind the copy while the gutter keeps
35
+ * full strength, and the text contrast measured 14.32:1 at 0.45, so it can
36
+ * only improve here. */
37
+ export declare const FOCUS_FLOOR = 0.15;
38
+ /** Reject geometry that cannot describe a real column. */
39
+ export declare function normalizeFocusRect(rect: BgFocusRect | null | undefined): BgFocusRect | null;
40
+ /**
41
+ * The "避让正文" gate: the rect the renderers get. `false` means the mask does
42
+ * not participate at all — `null` reaches `setFocus`, so the ASCII tier never
43
+ * allocates caps and the shader never uploads `u_focus`. This is deliberately
44
+ * not "weight every cell by 1": that would keep the whole pipeline running
45
+ * for a page the user asked to be uniform.
46
+ */
47
+ export declare function focusRectFor(bgFocus: boolean | undefined, rect: BgFocusRect | null): BgFocusRect | null;
48
+ /** True when two rects describe the same zone; used to skip mask rebuilds. */
49
+ export declare function sameFocusRect(a: BgFocusRect | null, b: BgFocusRect | null): boolean;
50
+ /**
51
+ * Ink weight (0..1) at a CSS point: `floor` inside the rect, rising to 1 at
52
+ * `feather` px outside it with a smoothstep ramp (C1-continuous, so the fade
53
+ * has no visible band at either end).
54
+ */
55
+ export declare function focusWeight(px: number, py: number, rect: BgFocusRect, feather: number, floor: number): number;
56
+ /**
57
+ * Per-cell level caps for the ASCII tier. A cell may not exceed its cap, which
58
+ * thins the glyph ramp toward the column centre; the ramp order is preserved
59
+ * so the piece keeps its own look. The cap is quantised anyway (a ramp has
60
+ * 9–15 steps), so a per-cell dither breaks the level boundaries into a soft
61
+ * grain instead of concentric rims.
62
+ */
63
+ export declare function buildFocusCaps(cols: number, rows: number, cellW: number, cellH: number, rect: BgFocusRect, feather: number, floor: number, lastLevel: number, out?: Uint8Array): Uint8Array;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * `jelly`: a jellyfish rising slowly through the page.
3
+ *
4
+ * The animal is three layers:
5
+ * 1. a bell rasterised once from a half-ellipse at module load — concentric
6
+ * tonal rings (rim level 9 down to a translucent level 4 centre) with a
7
+ * wavy skirt on the bottom row;
8
+ * 2. six parametric tendrils hanging from the skirt, each with its own
9
+ * phase, amplitude and length drawn as `| / \ ~` direction marks with a
10
+ * faint ramp cell underneath;
11
+ * 3. drifting plankton (sparse level 1–2 dots that wrap vertically) and click
12
+ * sparks (short-lived overlay points that spread out from the pointer).
13
+ *
14
+ * The bell pulses at 1.15 rad/s: the whole animal bobs one row and the
15
+ * tendrils shorten on the contraction, which is what makes it read as
16
+ * swimming rather than floating. The pointer is felt: inside 12 cells the
17
+ * jellyfish rises faster and contracts; a click adds a stronger contraction
18
+ * and lights a ring of sparks at the click.
19
+ *
20
+ * Every bell and tendril cell is marked for emphasis, and the preset carries
21
+ * `emphasisFull` (bg-ascii spec flag), so the animal keeps its own ink inside
22
+ * the calm zone instead of fading to the 0.65 floor — the same contract the
23
+ * whale uses. Plankton stays masked like any texture. Nothing allocates per
24
+ * frame: the bell is module-level, the spark pool is fixed, and the plankton
25
+ * positions are hashes of their index.
26
+ * @module @alexpeng/dsh-custom-plugin/client/bg-jelly
27
+ */
28
+ import { type AsciiPiece } from './bg-field.ts';
29
+ /** Bell size in grid cells; the tendrils add ~15 rows below it. */
30
+ export declare const JELLY_BELL_W = 24;
31
+ export declare const JELLY_BELL_H = 8;
32
+ /** One baked bell cell: offset from the bell's top-left, ramp level and
33
+ * whether it sits on the wavy skirt. */
34
+ export interface JellyBellCell {
35
+ readonly dx: number;
36
+ readonly dy: number;
37
+ readonly level: number;
38
+ readonly skirt: boolean;
39
+ }
40
+ /**
41
+ * Rasterise the bell. Runs once at module load. The tonal rings are what give
42
+ * the bell its dome: the rim is the darkest step, the interior steps down ring
43
+ * by ring, and the very centre stays translucent (level 4).
44
+ */
45
+ export declare function buildJellyBell(): JellyBellCell[];
46
+ /** The baked bell, shared by every instance. */
47
+ export declare const JELLY_BELL_CELLS: readonly JellyBellCell[];
48
+ /**
49
+ * Create the jellyfish piece. The returned function owns the bell position, the
50
+ * contraction state, the spark pool and the plankton hashes; all of it is
51
+ * reused for the piece's lifetime and reset only when the grid changes.
52
+ */
53
+ export declare function createJellyPiece(levels: number, dither: boolean): AsciiPiece;