@arraypress/effects-astro 0.9.0 → 0.10.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/AGENTS.md CHANGED
@@ -10,7 +10,7 @@ Pick by what is asked for:
10
10
 
11
11
  | Asked for | Use |
12
12
  |---|---|
13
- | An animated, colourful background for a page or hero | `<LightField>` (liquid light), `<FlutedGlass>` (light, gallery-white or dark), `<AsciiRain>` (techy, dark), `<DotGrid>` (quiet, minimal), `<SignalLines>` / `<WaveBars>` (audio) |
13
+ | An animated, colourful background for a page or hero | `<LightField>` (liquid light), `<FlutedGlass>` (light, gallery-white or dark), `<AsciiRain>` (techy, dark), `<AsciiNoise>` (drifting blobs of monospace type; techy, terminal, studio), `<DotGrid>` (quiet, minimal), `<SignalLines>` / `<WaveBars>` (audio) |
14
14
  | Ink in water, a lit fluid background, or a hero whose colours a palette switcher changes | `<InkField palette="…">` (Lumetri, Forest, Cadmium, Peacock, Vermillion, Barbie, Uranium, Azure, Ruby, Ultramarine, Saffron, Crimson); `setColors(el, INK_PALETTES[name].colors, INK_PALETTES[name].glint)` recolours it live |
15
15
  | Something to play with: ink, smoke, liquid, "interactive" | `<Ink>` over a section, or `<LightField>` (it has the fluid under it), or `<DotGrid>` (click ripples) |
16
16
  | A soft gradient that drifts (a card, a hero, a section) — "grainy gradient", "mesh", "aurora" | `<LiveGradient colors={[…]}>` (lazy by default; `liveGradient(colors)` for HTML made in a script) |
package/COMPONENTS.md CHANGED
@@ -4,6 +4,38 @@ Every component in `@arraypress/effects-astro`, with every prop. Generated from
4
4
 
5
5
  Import each from its own path: `import Name from '@arraypress/effects-astro/Name.astro'`.
6
6
 
7
+ ## `<AsciiNoise>`
8
+
9
+ AsciiNoise: a field of slow noise drawn as a grid of monospace characters, soft-edged blobs of type drifting and morphing, the quiet cells in one colour and the loud in another. See src/runtime/ascii-noise.js.
10
+
11
+ ```astro
12
+ <section class="hero" style="position: relative; background: #0b0b0b">
13
+ <AsciiNoise glyphs="shade" colors={['#2c3a0a', '#c8ff00']} quiet={{ x: 0.3, y: 0.5, radius: 0.6, depth: 0.9 }} />
14
+ <h1>…</h1>
15
+ </section>
16
+ ```
17
+
18
+ | Prop | Type | |
19
+ |---|---|---|
20
+ | `colors` | `string[]` | Two CSS hex colours: the quietest cells' and the loudest's. Default a dim grey and white. |
21
+ | `palette` | `PaletteName \| (string & {})` | A shared palette by name (`PALETTES`, any case), when there are no `colors`: its first and last colours. |
22
+ | `background` | `string` | A CSS hex colour to fill behind the type; none leaves the canvas clear there. |
23
+ | `glyphs` | `keyof typeof NOISE_RAMPS \| (string & {})` | A ramp (`ascii`, `shade`, `digits`, `rise`, `quads`, `bars`, `arrows`, `letters`, `dense`, or a set of `GLYPH_SETS`) or the characters, quietest first. Default `ascii`. |
24
+ | `cell` | `number` | A character cell's height in CSS px. Default 14. |
25
+ | `cols` | `number` | Cells across instead, whatever the width (overrides `cell`). |
26
+ | `aspect` | `number` | A cell's width over its height. Default 0.6. |
27
+ | `scale` | `number` | How many noise features span the width (higher is busier). Default 2.45. |
28
+ | `contrast` | `number` | How hard the edges are: higher pushes cells to empty or full. Default 6. |
29
+ | `brightness` | `number` | Shifts the whole field; lower leaves more cells empty. Default -0.1. |
30
+ | `drift` | `[number, number]` | How fast the sample window pans, widths a second, x and y. Default [0.036, 0.012]. |
31
+ | `evolve` | `number` | How fast the shapes morph. Default 0.24. |
32
+ | `offset` | `[number, number]` | Where the sample window starts, so two cards differ. Default [0, 0]. |
33
+ | `speed` | `number` | Plays every clock faster or slower; 0 for one still frame. Default 1. |
34
+ | `quiet` | `{ x?: number; y?: number; radius?: number; depth?: number } \| null` | An area kept quiet: centre (0–1 from the top left), radius (share of the height), depth (0–1). |
35
+ | `font` | `string` | The font family the glyphs are drawn in (a monospace stack by default); it is loaded first. |
36
+ | `fixed` | `boolean` | Fill the viewport (position: fixed). Default false. |
37
+ | `class` | `string` | Classes for the element. |
38
+
7
39
  ## `<AsciiRain>`
8
40
 
9
41
  AsciiRain: data rain on a rippling surface, drawn in text characters, with red and blue fringes at the glyph edges. The pointer ripples the surface and a click sends a ring out. See src/runtime/ascii-rain.js.
package/LICENSE CHANGED
@@ -78,8 +78,8 @@ SOFTWARE.
78
78
 
79
79
  ---
80
80
 
81
- The hash in src/runtime/shader-backgrounds/grain-gradient.js is Dave Hoskins'
82
- "Hash without Sine" (https://www.shadertoy.com/view/4djSRW):
81
+ The hashes in src/runtime/shader-backgrounds/grain-gradient.js and
82
+ src/runtime/ascii-noise.js are Dave Hoskins' "Hash without Sine" (https://www.shadertoy.com/view/4djSRW):
83
83
 
84
84
  Copyright (c) 2014 David Hoskins
85
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arraypress/effects-astro",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "GPU effects for Astro sites, as components: liquid light with a real fluid under it, ink in water, and outline text a spotlight fills. Plain WebGL, no third-party dependencies, with the fallbacks a theme needs (reduced motion, no WebGL, touch screens, off-screen pausing).",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,37 @@
1
+ ---
2
+ /**
3
+ * AsciiNoise: a field of slow noise drawn as a grid of monospace
4
+ * characters, soft-edged blobs of type drifting and morphing, the quiet
5
+ * cells in one colour and the loud in another. See
6
+ * src/runtime/ascii-noise.js.
7
+ *
8
+ * Fills its nearest positioned ancestor, or the viewport with `fixed`.
9
+ * Clear where the cells are empty (or `background`), so it lays over any
10
+ * ground. Without WebGL nothing is drawn.
11
+ *
12
+ * @example
13
+ * <section class="hero" style="position: relative; background: #0b0b0b">
14
+ * <AsciiNoise glyphs="shade" colors={['#2c3a0a', '#c8ff00']} quiet={{ x: 0.3, y: 0.5, radius: 0.6, depth: 0.9 }} />
15
+ * <h1>…</h1>
16
+ * </section>
17
+ */
18
+ import EffectCanvas from './EffectCanvas.astro';
19
+ import type { AsciiNoiseConfig } from '../runtime/ascii-noise.js';
20
+
21
+ interface Props extends AsciiNoiseConfig {
22
+ /** Fill the viewport (position: fixed). Default false. */
23
+ fixed?: boolean;
24
+ class?: string;
25
+ }
26
+
27
+ const { fixed = false, class: className, ...config } = Astro.props;
28
+ ---
29
+
30
+ <EffectCanvas name="ascii-noise" config={config} fixed={fixed} class={className} />
31
+
32
+ <script>
33
+ import { mount } from '../runtime/mount.js';
34
+ import { asciiNoise } from '../runtime/ascii-noise.js';
35
+
36
+ mount('[data-effect-ascii-noise]', asciiNoise);
37
+ </script>
package/src/index.d.ts CHANGED
@@ -6,6 +6,8 @@ export { spotlight } from './runtime/spotlight.js';
6
6
  export type { SpotlightConfig } from './runtime/spotlight.js';
7
7
  export { asciiRain } from './runtime/ascii-rain.js';
8
8
  export type { AsciiRainConfig } from './runtime/ascii-rain.js';
9
+ export { asciiNoise, asciiCells, noiseField, noiseColors, rampOf, NOISE_RAMPS, NOISE_DEFAULTS } from './runtime/ascii-noise.js';
10
+ export type { AsciiNoiseConfig } from './runtime/ascii-noise.js';
9
11
  export { glassGlyph } from './runtime/glass-glyph.js';
10
12
  export type { GlassGlyphConfig } from './runtime/glass-glyph.js';
11
13
  export { flutedGlass } from './runtime/fluted-glass.js';
package/src/index.js CHANGED
@@ -15,6 +15,7 @@ export { lightField } from './runtime/light-field.js';
15
15
  export { ink } from './runtime/ink.js';
16
16
  export { spotlight } from './runtime/spotlight.js';
17
17
  export { asciiRain } from './runtime/ascii-rain.js';
18
+ export { asciiNoise, asciiCells, noiseField, noiseColors, rampOf, NOISE_RAMPS, NOISE_DEFAULTS } from './runtime/ascii-noise.js';
18
19
  export { glassGlyph } from './runtime/glass-glyph.js';
19
20
  export { flutedGlass } from './runtime/fluted-glass.js';
20
21
  export { particleLogo } from './runtime/particle-logo.js';
@@ -0,0 +1,75 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface AsciiNoiseConfig {
4
+ /** Two CSS hex colours: the quietest cells' and the loudest's. Default a dim grey and white. */
5
+ colors?: string[];
6
+ /** A shared palette by name (`PALETTES`, any case), when there are no `colors`: its first and last colours. */
7
+ palette?: PaletteName | (string & {});
8
+ /** A CSS hex colour to fill behind the type; none leaves the canvas clear there. */
9
+ background?: string;
10
+ /** A ramp (`ascii`, `shade`, `digits`, `rise`, `quads`, `bars`, `arrows`, `letters`, `dense`, or a set of `GLYPH_SETS`) or the characters, quietest first. Default `ascii`. */
11
+ glyphs?: keyof typeof NOISE_RAMPS | (string & {});
12
+ /** A character cell's height in CSS px. Default 14. */
13
+ cell?: number;
14
+ /** Cells across instead, whatever the width (overrides `cell`). */
15
+ cols?: number;
16
+ /** A cell's width over its height. Default 0.6. */
17
+ aspect?: number;
18
+ /** How many noise features span the width (higher is busier). Default 2.45. */
19
+ scale?: number;
20
+ /** How hard the edges are: higher pushes cells to empty or full. Default 6. */
21
+ contrast?: number;
22
+ /** Shifts the whole field; lower leaves more cells empty. Default -0.1. */
23
+ brightness?: number;
24
+ /** How fast the sample window pans, widths a second, x and y. Default [0.036, 0.012]. */
25
+ drift?: [number, number];
26
+ /** How fast the shapes morph. Default 0.24. */
27
+ evolve?: number;
28
+ /** Where the sample window starts, so two cards differ. Default [0, 0]. */
29
+ offset?: [number, number];
30
+ /** Plays every clock faster or slower; 0 for one still frame. Default 1. */
31
+ speed?: number;
32
+ /** An area kept quiet: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
33
+ quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
34
+ /** The font family the glyphs are drawn in (a monospace stack by default); it is loaded first. */
35
+ font?: string;
36
+ }
37
+
38
+ /** The ramps, each quietest first. A ramp starting with a space leaves quiet cells empty. */
39
+ export declare const NOISE_RAMPS: {
40
+ readonly ascii: string;
41
+ readonly shade: string;
42
+ readonly digits: string;
43
+ readonly rise: string;
44
+ readonly quads: string;
45
+ readonly bars: string;
46
+ readonly arrows: string;
47
+ readonly letters: string;
48
+ readonly dense: string;
49
+ };
50
+
51
+ /** The settings' defaults, shared by the shader and `asciiCells`. */
52
+ export declare const NOISE_DEFAULTS: {
53
+ glyphs: string;
54
+ scale: number;
55
+ contrast: number;
56
+ brightness: number;
57
+ drift: [number, number];
58
+ evolve: number;
59
+ aspect: number;
60
+ };
61
+
62
+ /** A ramp's characters, quietest first (at least two). */
63
+ export declare function rampOf(glyphs?: string): string[];
64
+
65
+ /** The field: five octaves of gradient noise, each warped by the sum so far; 0–1. */
66
+ export declare function noiseField(x: number, y: number, evo: number, wt: number): number;
67
+
68
+ /** One frame as ramp indices: what the shader draws, cell for cell, on a target exactly `cols` by `rows` cells. */
69
+ export declare function asciiCells(config: AsciiNoiseConfig, cols: number, rows: number, time?: number): number[][];
70
+
71
+ /** The two colours as the shader takes them: the quietest cells' and the loudest's. */
72
+ export declare function noiseColors(config: AsciiNoiseConfig): [string, string];
73
+
74
+ /** Starts the noise on an element (its first canvas is drawn into). Returns a function that stops it. */
75
+ export declare function asciiNoise(el: HTMLElement, config: AsciiNoiseConfig): () => void;
@@ -0,0 +1,377 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/ascii-noise
3
+ *
4
+ * A field of slow noise drawn as a grid of monospace characters: each cell
5
+ * samples a five-octave gradient noise (quintic fade, a domain warp) at its
6
+ * centre, the value is pushed hard toward empty or full (`contrast`) and
7
+ * picks a character from a ramp, quietest first, so the field reads as
8
+ * soft-edged blobs of type drifting and morphing across the frame. The
9
+ * quiet cells take one colour and the loud ones another, blended in Oklab.
10
+ *
11
+ * Two clocks, both in seconds: the sample window pans (`drift`) and the
12
+ * shapes morph (`evolve`); the warp drifts on its own, more slowly. The
13
+ * same field is here in plain JavaScript (`noiseField`, `asciiCells`), so a
14
+ * page can lay its first frame as text before the shader starts, and the
15
+ * two agree.
16
+ *
17
+ * Clear where the cells are empty (or the `background` colour), so it lays
18
+ * over any ground. A `quiet` area thins the type behind copy. Without
19
+ * WebGL nothing is drawn; with prefers-reduced-motion it is one still frame.
20
+ *
21
+ * After `ascii_noise_bg`, a Processing sketch shared with the owner
22
+ * (2026-11-20): the noise, the warp, the tunings and the ramps are its;
23
+ * here it runs on the GPU, by the second rather than by the frame, with
24
+ * the width as its unit so a tall card and a wide hero sample alike, and
25
+ * its lattice hash is Dave Hoskins' Hash without Sine (MIT, © 2014 David
26
+ * Hoskins), which keeps the GPU and `asciiCells` in step.
27
+ */
28
+
29
+ import { createStage } from './stage.js';
30
+ import { GLSL_OKLAB, parseColor, toOklab } from './color.js';
31
+ import { GLSL_QUIET, quietUniform } from './glsl.js';
32
+ import { GLSL_GLYPHS, GLYPH_SETS, createGlyphs } from './glyphs.js';
33
+ import { watchColors } from './mount.js';
34
+ import { pickColors } from './palettes.js';
35
+
36
+ /**
37
+ * @typedef {object} AsciiNoiseConfig
38
+ * @property {string[]} [colors] Two CSS hex colours: the quietest cells' and the loudest's. Default a dim grey and white.
39
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: its first and last colours.
40
+ * @property {string} [background] A CSS hex colour to fill behind the type; none leaves the canvas clear there.
41
+ * @property {string} [glyphs='ascii'] A ramp (`ascii`, `shade`, `digits`, `rise`, `quads`, `bars`, `arrows`,
42
+ * `letters`, `dense`; or a set of GLYPH_SETS) or the characters themselves, quietest first.
43
+ * @property {number} [cell=14] A character cell's height in CSS px.
44
+ * @property {number} [cols] Cells across instead, whatever the width (overrides `cell`).
45
+ * @property {number} [aspect=0.6] A cell's width over its height.
46
+ * @property {number} [scale=2.45] How many noise features span the width (higher is busier).
47
+ * @property {number} [contrast=6] How hard the edges are: higher pushes cells to empty or full.
48
+ * @property {number} [brightness=-0.1] Shifts the whole field; lower leaves more cells empty.
49
+ * @property {[number, number]} [drift=[0.036, 0.012]] How fast the sample window pans, widths a second, x and y.
50
+ * @property {number} [evolve=0.24] How fast the shapes morph.
51
+ * @property {[number, number]} [offset=[0, 0]] Where the sample window starts, so two cards differ.
52
+ * @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
53
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
54
+ * An area kept quiet: centre (0–1 from the top left), radius (share of the height), depth (0–1).
55
+ * @property {string} [font] The font family the glyphs are drawn in (a monospace stack by default); it is loaded first.
56
+ */
57
+
58
+ /** The ramps, each quietest first. A ramp starting with a space leaves quiet cells empty. */
59
+ export const NOISE_RAMPS = {
60
+ /** Punctuation building to `@`. */
61
+ ascii: ' `^",:;!+*#%@',
62
+ /** Shade blocks, never empty: the whole frame is type. */
63
+ shade: '░▒▓█',
64
+ /** Numerals, 1 to 9. */
65
+ digits: ' 123456789',
66
+ /** Blocks rising from the baseline. */
67
+ rise: ' ▁▂▃▄▅▆▇█',
68
+ /** Quadrant blocks, from one corner to all four. */
69
+ quads: ' ▖▗▘▝▚▞▛▙▜▟█',
70
+ /** Bars filling from the left. */
71
+ bars: ' ▏▎▍▌▋▊▉█',
72
+ /** Arrows (the font must have them). */
73
+ arrows: ' ↑↗→↘↓↙←↖↕',
74
+ /** Letters, light to heavy. */
75
+ letters: ' iljtfrvxzcknyuUHPAOEGQDBMWR',
76
+ /** The long classic ramp. */
77
+ dense: " .'`^,:;I}{1)(|OZbkhao*#MW&8%B@$",
78
+ };
79
+
80
+ /** The settings' defaults, shared by the shader and `asciiCells`. */
81
+ export const NOISE_DEFAULTS = {
82
+ glyphs: 'ascii',
83
+ scale: 2.45,
84
+ contrast: 6,
85
+ brightness: -0.1,
86
+ drift: /** @type {[number, number]} */ ([0.036, 0.012]),
87
+ evolve: 0.24,
88
+ aspect: 0.6,
89
+ };
90
+
91
+ /** The default colours: a dim grey for the quiet cells, white for the loud. */
92
+ const GREY = ['#3a3a3a', '#ffffff'];
93
+
94
+ /**
95
+ * A ramp's characters.
96
+ *
97
+ * @param {string} [glyphs] A ramp's name (NOISE_RAMPS, then GLYPH_SETS) or the characters.
98
+ * @returns {string[]} The characters, quietest first (at least two).
99
+ */
100
+ export function rampOf(glyphs = NOISE_DEFAULTS.glyphs) {
101
+ const chars = [
102
+ ...(NOISE_RAMPS[/** @type {keyof typeof NOISE_RAMPS} */ (glyphs)] ??
103
+ GLYPH_SETS[/** @type {keyof typeof GLYPH_SETS} */ (glyphs)] ??
104
+ glyphs),
105
+ ];
106
+ return chars.length > 1 ? chars : [...NOISE_RAMPS.ascii];
107
+ }
108
+
109
+ /**
110
+ * GLSL (ES 1.00): `float noiseField(vec2 p, float evo, float wt)`, the
111
+ * warped five-octave gradient noise, 0–1. `noiseField` below is the same
112
+ * in JavaScript.
113
+ */
114
+ const GLSL_FIELD = `
115
+ vec2 grad(vec2 p) {
116
+ vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
117
+ p3 += dot(p3, p3.yzx + 33.33);
118
+ return -1.0 + 2.0 * fract((p3.xx + p3.yz) * p3.zy);
119
+ }
120
+ float gnoise(vec2 p) {
121
+ vec2 i = floor(p);
122
+ vec2 f = p - i;
123
+ vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
124
+ float a = dot(grad(i), f);
125
+ float b = dot(grad(i + vec2(1.0, 0.0)), f - vec2(1.0, 0.0));
126
+ float c = dot(grad(i + vec2(0.0, 1.0)), f - vec2(0.0, 1.0));
127
+ float d = dot(grad(i + vec2(1.0, 1.0)), f - vec2(1.0, 1.0));
128
+ return 0.5 + 0.5 * mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
129
+ }
130
+ float noiseField(vec2 p, float evo, float wt) {
131
+ float warp = gnoise(p + wt * 0.04);
132
+ float v = 0.0;
133
+ float amp = 0.5;
134
+ float freq = 1.0;
135
+ for (int i = 0; i < 5; i++) {
136
+ v += amp * gnoise(p * freq + vec2(warp * 0.3, warp * 0.25) + evo * 0.06);
137
+ amp *= 0.5;
138
+ freq *= 2.07;
139
+ warp = v;
140
+ }
141
+ return v;
142
+ }
143
+ `;
144
+
145
+ const FRAG = `
146
+ precision highp float;
147
+ uniform vec2 uRes;
148
+ uniform vec2 uCell;
149
+ uniform float uTime;
150
+ uniform float uScale;
151
+ uniform float uContrast;
152
+ uniform float uBrightness;
153
+ uniform vec2 uDrift;
154
+ uniform float uEvolve;
155
+ uniform vec2 uOffset;
156
+ uniform vec3 uLo;
157
+ uniform vec3 uHi;
158
+ uniform vec4 uBg;
159
+ ${GLSL_OKLAB}
160
+ ${GLSL_QUIET}
161
+ ${GLSL_GLYPHS}
162
+ ${GLSL_FIELD}
163
+
164
+ void main() {
165
+ // Cells counted from the top left, as the text still lays them.
166
+ vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
167
+ vec2 cell = floor(px / uCell);
168
+ vec2 centre = (cell + 0.5) * uCell;
169
+ // The field in widths, so its features keep their size whatever the shape.
170
+ vec2 p = (centre / uRes.x) * uScale + uOffset + uDrift * uTime;
171
+ float v = noiseField(p, uEvolve * uTime, uTime);
172
+ v = clamp((v - 0.5) * uContrast + 0.5 + uBrightness, 0.0, 1.0);
173
+ vec2 q = vec2(centre.x - 0.5 * uRes.x, 0.5 * uRes.y - centre.y) / uRes.y;
174
+ v *= mix(1.0 - uQuiet.w, 1.0, quietOpen(q));
175
+ float last = uGlyphCount - 1.0;
176
+ float index = min(floor(v * uGlyphCount), last);
177
+ float k = index / last;
178
+ float a = glyph(vec2(fract(px.x / uCell.x), 1.0 - fract(px.y / uCell.y)), k);
179
+ vec3 col = fromOklab(mix(uLo, uHi, k));
180
+ gl_FragColor = vec4(col * a + uBg.rgb * uBg.a * (1.0 - a), a + uBg.a * (1.0 - a));
181
+ }
182
+ `;
183
+
184
+ /**
185
+ * One octave's gradient at a lattice point (the shader's `grad`): Dave
186
+ * Hoskins' Hash without Sine, which brings its input under 1 before it
187
+ * multiplies, so the GPU's 32-bit floats and these 64-bit ones agree.
188
+ * (The sketch's own hash multiplied values in the thousands, where the
189
+ * two drift apart and the text still would not be the shader's frame.)
190
+ *
191
+ * @param {number} x The point's x.
192
+ * @param {number} y The point's y.
193
+ * @returns {[number, number]} A gradient, each part −1 to 1.
194
+ */
195
+ function grad(x, y) {
196
+ const fract = (/** @type {number} */ n) => n - Math.floor(n);
197
+ let a = fract(x * 0.1031);
198
+ let b = fract(y * 0.103);
199
+ let c = fract(x * 0.0973);
200
+ const d = a * (b + 33.33) + b * (c + 33.33) + c * (a + 33.33);
201
+ a += d;
202
+ b += d;
203
+ c += d;
204
+ return [-1 + 2 * fract((a + b) * c), -1 + 2 * fract((a + c) * b)];
205
+ }
206
+
207
+ /**
208
+ * Gradient noise with a quintic fade (the shader's `gnoise`).
209
+ *
210
+ * @param {number} x Where, x.
211
+ * @param {number} y Where, y.
212
+ * @returns {number} 0–1.
213
+ */
214
+ function gnoise(x, y) {
215
+ const ix = Math.floor(x);
216
+ const iy = Math.floor(y);
217
+ const fx = x - ix;
218
+ const fy = y - iy;
219
+ const ux = fx * fx * fx * (fx * (fx * 6 - 15) + 10);
220
+ const uy = fy * fy * fy * (fy * (fy * 6 - 15) + 10);
221
+ const dot = (/** @type {[number, number]} */ g, /** @type {number} */ dx, /** @type {number} */ dy) => g[0] * dx + g[1] * dy;
222
+ const a = dot(grad(ix, iy), fx, fy);
223
+ const b = dot(grad(ix + 1, iy), fx - 1, fy);
224
+ const c = dot(grad(ix, iy + 1), fx, fy - 1);
225
+ const d = dot(grad(ix + 1, iy + 1), fx - 1, fy - 1);
226
+ const ab = a + (b - a) * ux;
227
+ const cd = c + (d - c) * ux;
228
+ return 0.5 + 0.5 * (ab + (cd - ab) * uy);
229
+ }
230
+
231
+ /**
232
+ * The field: five octaves of gradient noise, each warped by the sum so far.
233
+ *
234
+ * @param {number} x Where, x.
235
+ * @param {number} y Where, y.
236
+ * @param {number} evo The morphing clock.
237
+ * @param {number} wt The warp's clock (seconds).
238
+ * @returns {number} 0–1, near 0.5 on average.
239
+ */
240
+ export function noiseField(x, y, evo, wt) {
241
+ let warp = gnoise(x + wt * 0.04, y + wt * 0.04);
242
+ let v = 0;
243
+ let amp = 0.5;
244
+ let freq = 1;
245
+ for (let i = 0; i < 5; i++) {
246
+ v += amp * gnoise(x * freq + warp * 0.3 + evo * 0.06, y * freq + warp * 0.25 + evo * 0.06);
247
+ amp *= 0.5;
248
+ freq *= 2.07;
249
+ warp = v;
250
+ }
251
+ return v;
252
+ }
253
+
254
+ /**
255
+ * One frame as ramp indices: what the shader draws, cell for cell, on a
256
+ * target exactly `cols` by `rows` cells.
257
+ *
258
+ * @param {AsciiNoiseConfig} config The settings (the same as the shader's).
259
+ * @param {number} cols Cells across.
260
+ * @param {number} rows Cells down.
261
+ * @param {number} [time=0] Seconds, as the shader's clock (already at its speed).
262
+ * @returns {number[][]} Each row's indices into `rampOf(config.glyphs)`.
263
+ */
264
+ export function asciiCells(config, cols, rows, time = 0) {
265
+ const { scale, contrast, brightness, drift, evolve, aspect } = { ...NOISE_DEFAULTS, ...config };
266
+ const n = rampOf(config.glyphs).length;
267
+ const [ox, oy] = config.offset ?? [0, 0];
268
+ // a cell is 1 wide and 1 / aspect tall; the quiet area is measured as the shader measures it
269
+ const height = rows / aspect;
270
+ const [qx, qy, radius, depth] = quietUniform(config.quiet, cols / height);
271
+ const smooth = (/** @type {number} */ e0, /** @type {number} */ e1, /** @type {number} */ x) => {
272
+ const t = Math.min(1, Math.max(0, (x - e0) / (e1 - e0)));
273
+ return t * t * (3 - 2 * t);
274
+ };
275
+ return Array.from({ length: rows }, (_, r) =>
276
+ Array.from({ length: cols }, (_, c) => {
277
+ const cx = c + 0.5;
278
+ const cy = (r + 0.5) / aspect;
279
+ let v = noiseField((cx / cols) * scale + ox + drift[0] * time, (cy / cols) * scale + oy + drift[1] * time, evolve * time, time);
280
+ v = Math.min(1, Math.max(0, (v - 0.5) * contrast + 0.5 + brightness));
281
+ if (radius > 0) {
282
+ const d = Math.hypot((cx - cols / 2) / height - qx, (height / 2 - cy) / height - qy);
283
+ v *= 1 - depth + depth * smooth(radius * 0.06, radius, d);
284
+ }
285
+ return Math.min(Math.floor(v * n), n - 1);
286
+ }),
287
+ );
288
+ }
289
+
290
+ /**
291
+ * The two colours as the shader takes them.
292
+ *
293
+ * @param {AsciiNoiseConfig} config The settings.
294
+ * @returns {[string, string]} The quietest cells' colour and the loudest's.
295
+ */
296
+ export function noiseColors(config) {
297
+ const colors = pickColors(config, GREY);
298
+ return [colors[0], colors[colors.length - 1]];
299
+ }
300
+
301
+ /**
302
+ * Starts the noise on an element: a canvas inside it (its first), filling
303
+ * it. With a `font`, the font is loaded before the glyphs are drawn.
304
+ *
305
+ * @param {HTMLElement} el The element.
306
+ * @param {AsciiNoiseConfig} config The settings.
307
+ * @returns {() => void} Stops it.
308
+ */
309
+ export function asciiNoise(el, config) {
310
+ let stop = () => {};
311
+ let stopped = false;
312
+ const ramp = rampOf(config.glyphs);
313
+ /** Starts the stage, unless it was stopped while the font loaded. */
314
+ const begin = () => {
315
+ if (!stopped) stop = run(el, config, ramp);
316
+ };
317
+ if (config.font && typeof document !== 'undefined' && document.fonts) document.fonts.load(`48px ${config.font}`, ramp.join('')).then(begin, begin);
318
+ else begin();
319
+ return () => {
320
+ stopped = true;
321
+ stop();
322
+ };
323
+ }
324
+
325
+ /**
326
+ * The stage itself, once the font is ready.
327
+ *
328
+ * @param {HTMLElement} el The element.
329
+ * @param {AsciiNoiseConfig} config The settings.
330
+ * @param {string[]} ramp The characters.
331
+ * @returns {() => void} Stops it.
332
+ */
333
+ function run(el, config, ramp) {
334
+ const o = { ...NOISE_DEFAULTS, ...config };
335
+ const speed = config.speed ?? 1;
336
+ const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
337
+ let [lo, hi] = noiseColors(config).map(lab);
338
+ const bg = config.background ? [...parseColor(config.background), 1] : [0, 0, 0, 0];
339
+ const start = performance.now();
340
+
341
+ const { stage, stop } = createStage(el, {
342
+ fragment: FRAG,
343
+ still: speed <= 0,
344
+ setup({ gl, uniform }) {
345
+ const { count } = createGlyphs(gl, ramp.join(''), { unit: 0, font: config.font, aspect: o.aspect });
346
+ gl.uniform1i(uniform('uGlyphs'), 0);
347
+ gl.uniform1f(uniform('uGlyphCount'), count);
348
+ gl.uniform1f(uniform('uScale'), o.scale);
349
+ gl.uniform1f(uniform('uContrast'), o.contrast);
350
+ gl.uniform1f(uniform('uBrightness'), o.brightness);
351
+ gl.uniform2f(uniform('uDrift'), o.drift[0], o.drift[1]);
352
+ gl.uniform1f(uniform('uEvolve'), o.evolve);
353
+ gl.uniform2f(uniform('uOffset'), ...(config.offset ?? [0, 0]));
354
+ gl.uniform4f(uniform('uBg'), bg[0], bg[1], bg[2], bg[3]);
355
+ },
356
+ resize({ gl, uniform, canvas, ratio }) {
357
+ const w = config.cols ? canvas.width / config.cols : (config.cell ?? 14) * ratio * o.aspect;
358
+ gl.uniform2f(uniform('uCell'), w, w / o.aspect);
359
+ gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, canvas.width / canvas.height));
360
+ },
361
+ uniforms({ gl, uniform }, now) {
362
+ gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, speed));
363
+ gl.uniform3f(uniform('uLo'), lo[0], lo[1], lo[2]);
364
+ gl.uniform3f(uniform('uHi'), hi[0], hi[1], hi[2]);
365
+ },
366
+ });
367
+ if (!stage) return stop;
368
+
369
+ const unwatch = watchColors(el, (next) => {
370
+ [lo, hi] = noiseColors({ colors: next.colors }).map(lab);
371
+ if (stage.still) stage.redraw();
372
+ });
373
+ return () => {
374
+ stop();
375
+ unwatch();
376
+ };
377
+ }