@arraypress/effects-astro 0.5.0 → 0.7.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.
@@ -43,6 +43,45 @@ import { PRESETS, ENUMS, RENAMES, FITS } from '../vendor/paper/presets.js';
43
43
  * @property {number} [scale=1] Share of the pixel ratio drawn at.
44
44
  */
45
45
 
46
+ /**
47
+ * Each shader's module, loaded on first use, in its own chunk. The paths
48
+ * are literals, not built from the name: a bundler gives a literal path its
49
+ * own chunk, but leaves a template path in a package as it is (Vite's
50
+ * dynamic-import-vars skips node_modules), and the built site 404s on it.
51
+ */
52
+ const LOADERS = {
53
+ 'color-panels': () => import('../vendor/paper/shaders/color-panels.js'),
54
+ dithering: () => import('../vendor/paper/shaders/dithering.js'),
55
+ 'dot-grid': () => import('../vendor/paper/shaders/dot-grid.js'),
56
+ 'dot-orbit': () => import('../vendor/paper/shaders/dot-orbit.js'),
57
+ 'fluted-glass': () => import('../vendor/paper/shaders/fluted-glass.js'),
58
+ 'gem-smoke': () => import('../vendor/paper/shaders/gem-smoke.js'),
59
+ 'god-rays': () => import('../vendor/paper/shaders/god-rays.js'),
60
+ 'grain-gradient': () => import('../vendor/paper/shaders/grain-gradient.js'),
61
+ 'halftone-cmyk': () => import('../vendor/paper/shaders/halftone-cmyk.js'),
62
+ 'halftone-dots': () => import('../vendor/paper/shaders/halftone-dots.js'),
63
+ heatmap: () => import('../vendor/paper/shaders/heatmap.js'),
64
+ 'image-dithering': () => import('../vendor/paper/shaders/image-dithering.js'),
65
+ 'lens-distortion': () => import('../vendor/paper/shaders/lens-distortion.js'),
66
+ 'liquid-metal': () => import('../vendor/paper/shaders/liquid-metal.js'),
67
+ 'mesh-gradient': () => import('../vendor/paper/shaders/mesh-gradient.js'),
68
+ metaballs: () => import('../vendor/paper/shaders/metaballs.js'),
69
+ 'neuro-noise': () => import('../vendor/paper/shaders/neuro-noise.js'),
70
+ 'paper-texture': () => import('../vendor/paper/shaders/paper-texture.js'),
71
+ 'perlin-noise': () => import('../vendor/paper/shaders/perlin-noise.js'),
72
+ 'pulsing-border': () => import('../vendor/paper/shaders/pulsing-border.js'),
73
+ 'simplex-noise': () => import('../vendor/paper/shaders/simplex-noise.js'),
74
+ 'smoke-ring': () => import('../vendor/paper/shaders/smoke-ring.js'),
75
+ spiral: () => import('../vendor/paper/shaders/spiral.js'),
76
+ 'static-mesh-gradient': () => import('../vendor/paper/shaders/static-mesh-gradient.js'),
77
+ 'static-radial-gradient': () => import('../vendor/paper/shaders/static-radial-gradient.js'),
78
+ swirl: () => import('../vendor/paper/shaders/swirl.js'),
79
+ voronoi: () => import('../vendor/paper/shaders/voronoi.js'),
80
+ warp: () => import('../vendor/paper/shaders/warp.js'),
81
+ water: () => import('../vendor/paper/shaders/water.js'),
82
+ waves: () => import('../vendor/paper/shaders/waves.js'),
83
+ };
84
+
46
85
  /** The shaders that sample their picture blurred (mip levels), as Paper's React components set up. */
47
86
  const MIPMAPPED = new Set(['fluted-glass', 'gem-smoke', 'heatmap', 'liquid-metal', 'lens-distortion', 'paper-texture', 'water']);
48
87
 
@@ -60,6 +99,19 @@ const HOST = new Set(['speed', 'frame']);
60
99
  */
61
100
  const camel = (name) => name.replace(/-(\w)/g, (_, c) => c.toUpperCase());
62
101
 
102
+ /**
103
+ * A shader's module, by name.
104
+ *
105
+ * @param {string} name The shader (`mesh-gradient`, `gem-smoke`…).
106
+ * @returns {Promise<Record<string, any>>} Its module: the fragment shader and any picture helper.
107
+ * @throws {TypeError} When there is no shader by that name.
108
+ */
109
+ async function load(name) {
110
+ const loader = Object.hasOwn(LOADERS, name) ? LOADERS[/** @type {keyof typeof LOADERS} */ (name)] : undefined;
111
+ if (!loader) throw new TypeError(`No Paper shader "${name}" (there are ${Object.keys(LOADERS).join(', ')})`);
112
+ return loader();
113
+ }
114
+
63
115
  /**
64
116
  * The shader's settings: its preset (the named one or its first), with the
65
117
  * config's colours and settings over it.
@@ -202,8 +254,7 @@ export function paperShader(el, config) {
202
254
  * @returns {Promise<() => void>} Stops it.
203
255
  */
204
256
  async function start(el, config) {
205
- /** @type {Record<string, any>} */
206
- const module = await import(`../vendor/paper/shaders/${config.shader}.js`);
257
+ const module = await load(config.shader);
207
258
  const fragment = module[`${camel(config.shader)}FragmentShader`];
208
259
  if (!fragment) throw new Error(`No fragment shader in Paper's "${config.shader}"`);
209
260
  let settings = paperSettings(config);
@@ -0,0 +1,134 @@
1
+ export interface PixelFountainConfig {
2
+ /** The pixels' colour when they fire; the darker steps they fade through are made from it. Default `#c8ff00`. */
3
+ color?: string;
4
+ /** The steps a pixel fades through, first to last (overrides `color`). Default made from `color`. */
5
+ colors?: string[];
6
+ /** A shared palette by name, its colours as the steps, when there are no `colors`. Default none. */
7
+ palette?: string;
8
+ /** A pixel's size, CSS px (a power-up's biggest blocks are two). Default 8. */
9
+ cell?: number;
10
+ /** How far above the edge the pixels can fly, CSS px. Default 240. */
11
+ rise?: number;
12
+ /** Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none. Default 14. */
13
+ rate?: number;
14
+ /** Pixels a click or tap fires. Default 40. */
15
+ burst?: number;
16
+ /** How hard they are pulled back, 0–2. Default 1. */
17
+ gravity?: number;
18
+ /** How widely they fan out, 0–1. Default 0.5. */
19
+ spread?: number;
20
+ /** How much a wind pushes them sideways, 0–1. Default 0.3. */
21
+ drift?: number;
22
+ /** The longest a pixel lives, seconds. Default 1.4. */
23
+ life?: number;
24
+ /** The chance a pixel splits into smaller ones rather than shrinking, 0–1. Default 0.4. */
25
+ split?: number;
26
+ /** The chance a pixel flickers out on and off before it goes, 0–1. Default 0.35. */
27
+ flicker?: number;
28
+ /** Fire from the host's top edge, or from the filled pixels of its text. Default `edge`. */
29
+ source?: 'edge' | 'text';
30
+ /** A selector for the element the pixels fire from and the pointer is followed over: the nearest ancestor that matches, else the first on the page. Default the element's positioned parent. */
31
+ host?: string;
32
+ /** Seeds the randomness. Default 1. */
33
+ seed?: number;
34
+ }
35
+
36
+ /** One pixel of a fountain. */
37
+ export interface Pixel {
38
+ /** Centre, CSS px from the canvas's left and top. */
39
+ x: number;
40
+ y: number;
41
+ /** Velocity, CSS px/s (negative `vy` rises). */
42
+ vx: number;
43
+ vy: number;
44
+ /** Seconds lived, and seconds it lives. */
45
+ age: number;
46
+ life: number;
47
+ /** Its size: 0 a double block, 1 a block, 2 a half, 3 a quarter. */
48
+ tier: number;
49
+ /** The share of its life at which it next breaks down, and the share between breaks. */
50
+ next: number;
51
+ gap: number;
52
+ /** A power-up's pixel: its first frames burn white. */
53
+ hot: boolean;
54
+ /** The share of life from which it flickers (over 1 for never). */
55
+ flicker: number;
56
+ /** Whether it shows this frame. */
57
+ on: boolean;
58
+ /** Steps until it next flickers. */
59
+ blink: number;
60
+ /** Its own sideways push, CSS px/s². */
61
+ wobble: number;
62
+ }
63
+
64
+ /** What fired a pixel: the ambient trickle, the pointer moving, or a press (a power-up). */
65
+ export type EmitKind = 'ambient' | 'move' | 'burst';
66
+
67
+ /** A fountain's settings: the canvas's size and the effect's physics. */
68
+ export interface FountainOptions {
69
+ /** The canvas's size, CSS px. */
70
+ width: number;
71
+ height: number;
72
+ /** Where the edge is, CSS px down the canvas. Default its bottom. */
73
+ floor?: number;
74
+ cell?: number;
75
+ rise?: number;
76
+ rate?: number;
77
+ gravity?: number;
78
+ spread?: number;
79
+ drift?: number;
80
+ life?: number;
81
+ split?: number;
82
+ flicker?: number;
83
+ seed?: number;
84
+ }
85
+
86
+ /** A seeded pool of pixels, stepped at a fixed rate: `pixels[0…count)` are alive. */
87
+ export interface Fountain {
88
+ pixels: Pixel[];
89
+ readonly count: number;
90
+ /** Seconds stepped. */
91
+ readonly time: number;
92
+ resize(width: number, height: number, floor?: number): void;
93
+ /** Points (x, y pairs, tops first) the ambient pixels fire from instead of the edge, as `inkedCells` gives them. */
94
+ setOrigins(points: Float32Array | null, tops?: number): void;
95
+ /** Fires `n` pixels from a point; returns how many the pool had room for. */
96
+ emit(x: number, y: number, n: number, kind?: EmitKind): number;
97
+ /** Fires `n` pixels from the origins within `radius` of a point (a press with none near fires from the point). */
98
+ emitNear(x: number, y: number, n: number, kind?: EmitKind, radius?: number): number;
99
+ /** Fires `n` pixels from the edge, from the cell under `x`. */
100
+ emitEdge(x: number, n: number, kind?: EmitKind): number;
101
+ /** Fires `n` pixels from where the ambient ones fire. */
102
+ emitAmbient(n: number, kind?: EmitKind): number;
103
+ /** One step (default 1/60 s): the trickle, then every pixel ages, breaks down, moves and flickers. */
104
+ step(dt?: number): void;
105
+ /** Empties the pool. */
106
+ clear(): void;
107
+ }
108
+
109
+ /** The most pixels alive at once (400). */
110
+ export const MAX_PIXELS: number;
111
+
112
+ /** The simulation's fixed step, seconds (1/60). */
113
+ export const STEP: number;
114
+
115
+ /** The steps a pixel fades through, made from one colour in Oklab: darker and less saturated each step, turning a little towards olive. */
116
+ export function fadeSteps(color: string, count?: number): string[];
117
+
118
+ /** The colour step for a share of a pixel's life (one step per equal share), or -1 for a power-up's hot first frames. */
119
+ export function shadeAt(t: number, count: number, hot?: boolean): number;
120
+
121
+ /** Each tier's size in whole device pixels: the double block's, the block's, the half's and the quarter's. */
122
+ export function tierSizes(cell: number, dpr: number): number[];
123
+
124
+ /** Where a pixel is drawn (device px): its centre snapped to the grid of its own size, columns from the left and rows up from the floor. Returns its left and top. */
125
+ export function pixelRect(x: number, y: number, size: number, floor: number): [number, number];
126
+
127
+ /** The grid cells a picture of text fills (centre and most of four points round it inked), rows up from `floor`: centres as x, y pairs, tops first, and how many are tops. */
128
+ export function inkedCells(rgba: Uint8ClampedArray, width: number, height: number, cell: number, floor?: number): { points: Float32Array; tops: number };
129
+
130
+ /** Makes a seeded fountain of pixels. */
131
+ export function createFountain(options: FountainOptions): Fountain;
132
+
133
+ /** Starts the pixel fountain on an element placed over its host's top edge. Returns a function that stops it. */
134
+ export function pixelFountain(el: HTMLElement, config: PixelFountainConfig): () => void;