@ultimat3/effects 0.0.0-stage → 31.53.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 (71) hide show
  1. package/CATALOG.md +169 -0
  2. package/CLAUDE.md +29 -0
  3. package/LICENSE +21 -0
  4. package/README.md +64 -2
  5. package/package.json +56 -4
  6. package/src/_effect.scss +53 -0
  7. package/src/aurora/Aurora.module.scss +56 -0
  8. package/src/aurora/Aurora.tsx +35 -0
  9. package/src/aurora/aurora-view.ts +32 -0
  10. package/src/backend.ts +34 -0
  11. package/src/canvas-mount.ts +61 -0
  12. package/src/count-up/CountUp.tsx +51 -0
  13. package/src/count-up/count-up-run.ts +59 -0
  14. package/src/count-up/count-up-view.ts +56 -0
  15. package/src/css-pause.ts +50 -0
  16. package/src/effect-canvas/EffectCanvas.module.scss +23 -0
  17. package/src/effect-canvas/EffectCanvas.tsx +51 -0
  18. package/src/effect-canvas/effect-canvas-view.ts +33 -0
  19. package/src/effect-host.ts +54 -0
  20. package/src/effect-mount.ts +114 -0
  21. package/src/errors.ts +33 -0
  22. package/src/glass-surface/GlassSurface.module.scss +25 -0
  23. package/src/glass-surface/GlassSurface.tsx +29 -0
  24. package/src/glass-surface/glass-surface-view.ts +33 -0
  25. package/src/gradient-orbs/GradientOrbs.module.scss +49 -0
  26. package/src/gradient-orbs/GradientOrbs.tsx +41 -0
  27. package/src/gradient-orbs/gradient-orbs-view.ts +77 -0
  28. package/src/grid-glow/GridGlow.module.scss +50 -0
  29. package/src/grid-glow/GridGlow.tsx +39 -0
  30. package/src/grid-glow/grid-glow-view.ts +40 -0
  31. package/src/index.ts +45 -0
  32. package/src/marquee/Marquee.module.scss +52 -0
  33. package/src/marquee/Marquee.tsx +31 -0
  34. package/src/marquee/marquee-view.ts +25 -0
  35. package/src/mosaic-grid/MosaicGrid.module.scss +39 -0
  36. package/src/mosaic-grid/MosaicGrid.tsx +59 -0
  37. package/src/mosaic-grid/mosaic-grid-view.ts +96 -0
  38. package/src/motion-port.ts +22 -0
  39. package/src/noise/Noise.module.scss +22 -0
  40. package/src/noise/Noise.tsx +49 -0
  41. package/src/noise/noise-view.ts +38 -0
  42. package/src/palette.ts +41 -0
  43. package/src/pixel-budget.ts +42 -0
  44. package/src/props.ts +61 -0
  45. package/src/shader-background/ShaderBackground.module.scss +70 -0
  46. package/src/shader-background/ShaderBackground.tsx +60 -0
  47. package/src/shader-background/load-preset.ts +19 -0
  48. package/src/shader-background/presets/dots.ts +59 -0
  49. package/src/shader-background/presets/grain.ts +59 -0
  50. package/src/shader-background/presets/mesh.ts +78 -0
  51. package/src/shader-background/shader-background-canvas.ts +116 -0
  52. package/src/shader-background/shader-background-view.ts +98 -0
  53. package/src/shader-background/uniform-pack.ts +11 -0
  54. package/src/shader-load.ts +17 -0
  55. package/src/shader-mount.ts +197 -0
  56. package/src/shimmer/Shimmer.module.scss +23 -0
  57. package/src/shimmer/Shimmer.tsx +19 -0
  58. package/src/spotlight-card/SpotlightCard.module.scss +29 -0
  59. package/src/spotlight-card/SpotlightCard.tsx +28 -0
  60. package/src/spotlight-card/spotlight-card-view.ts +37 -0
  61. package/src/spotlight-card/spotlight-wire.ts +34 -0
  62. package/src/text-reveal/TextReveal.module.scss +47 -0
  63. package/src/text-reveal/TextReveal.tsx +48 -0
  64. package/src/text-reveal/text-reveal-view.ts +38 -0
  65. package/src/tilt/Tilt.module.scss +18 -0
  66. package/src/tilt/Tilt.tsx +30 -0
  67. package/src/tilt/tilt-view.ts +64 -0
  68. package/src/tilt/tilt-wire.ts +50 -0
  69. package/src/use-css-pause.ts +35 -0
  70. package/src/use-effect-mount.ts +35 -0
  71. package/src/visibility.ts +26 -0
@@ -0,0 +1,116 @@
1
+ // ShaderBackground on Canvas2D, for an island without WebGL2: the same `meshPoints`, `grainAngle`
2
+ // and `dotsRamp` the GLSL reads, drawn with gradients and arcs. Grain's film texture stays the
3
+ // server's SVG overlay here; the shader draws its own.
4
+
5
+ import type { CanvasDraw } from '../canvas-mount';
6
+ import { cssRgb, type Rgb } from '../palette';
7
+ import {
8
+ DOT_PITCH,
9
+ DOT_RADIUS,
10
+ dotsRamp,
11
+ grainAngle,
12
+ MESH_PEAK,
13
+ MESH_RADIUS,
14
+ meshPoints,
15
+ type ShaderPreset,
16
+ } from './shader-background-view';
17
+
18
+ /** `1 - smoothstep` sampled at quarters: the stops the server's `radial-gradient`s use too. */
19
+ export const MESH_STOPS: readonly (readonly [number, number])[] = [0, 0.25, 0.5, 0.75, 1].map(
20
+ (x) => [x, MESH_PEAK * (1 - x * x * (3 - 2 * x))] as const,
21
+ );
22
+
23
+ const lerp = (a: Rgb, b: Rgb, t: number): Rgb => [
24
+ a[0] + (b[0] - a[0]) * t,
25
+ a[1] + (b[1] - a[1]) * t,
26
+ a[2] + (b[2] - a[2]) * t,
27
+ ];
28
+
29
+ const drawMesh =
30
+ (colours: () => readonly Rgb[]): CanvasDraw =>
31
+ (ctx, frame) => {
32
+ const palette = colours();
33
+ const ground = palette[0];
34
+ if (ground === undefined) return;
35
+ ctx.fillStyle = cssRgb(ground);
36
+ ctx.fillRect(0, 0, frame.width, frame.height);
37
+ meshPoints(frame.time).forEach(([x, y], index) => {
38
+ const colour = palette[index + 1];
39
+ if (colour === undefined) return;
40
+ // A unit circle stretched to the ellipse: `ellipse 60% 60%` is 60% of EACH axis.
41
+ ctx.setTransform(
42
+ frame.scale * MESH_RADIUS * frame.width,
43
+ 0,
44
+ 0,
45
+ frame.scale * MESH_RADIUS * frame.height,
46
+ frame.scale * x * frame.width,
47
+ frame.scale * y * frame.height,
48
+ );
49
+ const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, 1);
50
+ for (const [offset, alpha] of MESH_STOPS)
51
+ gradient.addColorStop(offset, cssRgb(colour, alpha));
52
+ ctx.fillStyle = gradient;
53
+ ctx.fillRect(-1, -1, 2, 2);
54
+ });
55
+ ctx.setTransform(frame.scale, 0, 0, frame.scale, 0, 0);
56
+ };
57
+
58
+ const drawGrain =
59
+ (colours: () => readonly Rgb[]): CanvasDraw =>
60
+ (ctx, frame) => {
61
+ const [, a, b] = colours();
62
+ if (a === undefined || b === undefined) return;
63
+ const angle = (grainAngle(frame.time) * Math.PI) / 180;
64
+ const dx = Math.sin(angle);
65
+ const dy = -Math.cos(angle);
66
+ // CSS's gradient line: through the centre, long enough to reach both far corners.
67
+ const half = (Math.abs(frame.width * dx) + Math.abs(frame.height * dy)) / 2;
68
+ const cx = frame.width / 2;
69
+ const cy = frame.height / 2;
70
+ const gradient = ctx.createLinearGradient(
71
+ cx - dx * half,
72
+ cy - dy * half,
73
+ cx + dx * half,
74
+ cy + dy * half,
75
+ );
76
+ gradient.addColorStop(0, cssRgb(a));
77
+ gradient.addColorStop(1, cssRgb(b));
78
+ ctx.fillStyle = gradient;
79
+ ctx.fillRect(0, 0, frame.width, frame.height);
80
+ };
81
+
82
+ const drawDots =
83
+ (colours: () => readonly Rgb[]): CanvasDraw =>
84
+ (ctx, frame) => {
85
+ const [ground, a, b] = colours();
86
+ if (ground === undefined || a === undefined || b === undefined) return;
87
+ ctx.fillStyle = cssRgb(ground);
88
+ ctx.fillRect(0, 0, frame.width, frame.height);
89
+ const ramp = dotsRamp(frame.time);
90
+ const cols = Math.ceil(frame.width / DOT_PITCH);
91
+ const rows = Math.ceil(frame.height / DOT_PITCH);
92
+ const centreX = frame.width / 2 / DOT_PITCH;
93
+ const centreY = frame.height / 2 / DOT_PITCH;
94
+ for (let row = 0; row < rows; row++) {
95
+ for (let col = 0; col < cols; col++) {
96
+ const wave = Math.sin(Math.hypot(col - centreX, row - centreY) * 0.35 - frame.time * 1.6);
97
+ const hue = ramp * (0.5 + 0.5 * Math.sin(col * 0.3 + row * 0.2 + frame.time * 0.8));
98
+ ctx.fillStyle = cssRgb(lerp(a, b, hue));
99
+ ctx.beginPath();
100
+ ctx.arc(
101
+ (col + 0.5) * DOT_PITCH,
102
+ (row + 0.5) * DOT_PITCH,
103
+ DOT_RADIUS * (1 + ramp * 0.7 * wave),
104
+ 0,
105
+ Math.PI * 2,
106
+ );
107
+ ctx.fill();
108
+ }
109
+ }
110
+ };
111
+
112
+ export function presetCanvas(preset: ShaderPreset, colours: () => readonly Rgb[]): CanvasDraw {
113
+ if (preset === 'mesh') return drawMesh(colours);
114
+ if (preset === 'grain') return drawGrain(colours);
115
+ return drawDots(colours);
116
+ }
@@ -0,0 +1,98 @@
1
+ // ShaderBackground's rules, shared by the server frame, the Canvas2D fallback and the GLSL's
2
+ // uniforms: where the mesh's colour points are at time t, the grain gradient's angle, the dot
3
+ // grid's pitch. Time 0 of each is what the server paints, so no backend flashes in. No GLSL here.
4
+
5
+ import type { ColorRole } from '@ultimat3/ui';
6
+ import type { EffectBackend } from '../backend';
7
+ import { EffectsInvalidValueError } from '../errors';
8
+ import { rangeProp, rolesProp, roleVars } from '../props';
9
+
10
+ export const SHADER_PRESETS = ['mesh', 'grain', 'dots'] as const;
11
+ export type ShaderPreset = (typeof SHADER_PRESETS)[number];
12
+
13
+ /** The server frame is a still picture: frame 0, until an island draws. */
14
+ export const SHADER_BACKGROUND_BACKEND: EffectBackend = 'static';
15
+
16
+ /** `[base, a, b, c]`: the ground, then the colours drawn over it. */
17
+ export const SHADER_BACKGROUND_ROLES: readonly ColorRole[] = [
18
+ 'bg-soft',
19
+ 'accent',
20
+ 'info',
21
+ 'chart-3',
22
+ ];
23
+
24
+ /** A mesh point's reach: its ellipse spans this fraction of each axis (CSS `ellipse 60% 60%`). */
25
+ export const MESH_RADIUS = 0.6;
26
+ /** A mesh point's peak opacity. */
27
+ export const MESH_PEAK = 0.9;
28
+ /** The dot grid's pitch and dot radius, in CSS pixels. */
29
+ export const DOT_PITCH = 16;
30
+ export const DOT_RADIUS = DOT_PITCH * 0.18;
31
+
32
+ const MESH_PATHS = [
33
+ { ax: 0.31, ay: 0.27, wx: 0.21, wy: 0.17, px: 2.1, py: 0.4 },
34
+ { ax: 0.28, ay: 0.3, wx: 0.15, wy: 0.23, px: 4.4, py: 2.6 },
35
+ { ax: 0.33, ay: 0.25, wx: 0.19, wy: 0.13, px: 0.3, py: 4.9 },
36
+ ] as const;
37
+
38
+ /** The mesh's three colour points at `time` seconds, in 0..1 box units (y down, as CSS). */
39
+ export function meshPoints(time: number): readonly (readonly [number, number])[] {
40
+ return MESH_PATHS.map((path) => [
41
+ 0.5 + path.ax * Math.sin(time * path.wx + path.px),
42
+ 0.5 + path.ay * Math.cos(time * path.wy + path.py),
43
+ ]);
44
+ }
45
+
46
+ /** The grain gradient's CSS angle, in degrees, at `time` seconds. */
47
+ export function grainAngle(time: number): number {
48
+ return 135 + 20 * Math.sin(time * 0.25);
49
+ }
50
+
51
+ /** 0 at frame 0, 1 after two seconds: how much of the dots' wave and hue drift is shown. */
52
+ export function dotsRamp(time: number): number {
53
+ const x = Math.min(1, Math.max(0, time / 2));
54
+ return x * x * (3 - 2 * x);
55
+ }
56
+
57
+ export interface ShaderBackgroundInput {
58
+ readonly preset?: ShaderPreset | undefined;
59
+ readonly roles?: readonly ColorRole[] | undefined;
60
+ readonly speed?: number | undefined;
61
+ }
62
+
63
+ export interface ShaderBackgroundView {
64
+ readonly preset: ShaderPreset;
65
+ readonly roles: readonly ColorRole[];
66
+ readonly speed: number;
67
+ readonly vars: Record<string, string>;
68
+ }
69
+
70
+ const percent = (value: number): string => `${Math.round(value * 10_000) / 100}%`;
71
+
72
+ export function shaderBackgroundView(input: ShaderBackgroundInput): ShaderBackgroundView {
73
+ const preset = input.preset ?? 'mesh';
74
+ if (!SHADER_PRESETS.includes(preset)) {
75
+ throw new EffectsInvalidValueError({
76
+ cause: `<ShaderBackground preset> is '${String(preset)}', not one of ${SHADER_PRESETS.join(', ')}`,
77
+ fix: `<ShaderBackground preset="mesh" /> // or "grain", "dots"`,
78
+ });
79
+ }
80
+ const roles = rolesProp('ShaderBackground', input.roles, SHADER_BACKGROUND_ROLES, {
81
+ min: 4,
82
+ max: 4,
83
+ });
84
+ const speed = rangeProp('ShaderBackground', 'speed', input.speed, {
85
+ min: 0.25,
86
+ max: 4,
87
+ fallback: 1,
88
+ });
89
+ const vars: Record<string, string> = { ...roleVars(roles) };
90
+ meshPoints(0).forEach(([x, y], index) => {
91
+ vars[`--fx-p${index}x`] = percent(x);
92
+ vars[`--fx-p${index}y`] = percent(y);
93
+ });
94
+ vars['--fx-angle'] = `${grainAngle(0)}deg`;
95
+ vars['--fx-pitch'] = `${DOT_PITCH}px`;
96
+ vars['--fx-dot'] = `${DOT_RADIUS}px`;
97
+ return { preset, roles, speed, vars };
98
+ }
@@ -0,0 +1,11 @@
1
+ // Packs a palette into the flat `vec3[]` a shader takes, into an array allocated once per mount:
2
+ // a frame loop that allocated per frame would hand the garbage collector 60 arrays a second.
3
+
4
+ import type { Rgb } from '../palette';
5
+
6
+ export function packColours(colours: readonly Rgb[], into: Float32Array): Float32Array {
7
+ colours.forEach((colour, index) => {
8
+ into.set(colour, index * 3);
9
+ });
10
+ return into;
11
+ }
@@ -0,0 +1,17 @@
1
+ // A program and the mount that draws it, fetched together and lazily: only an entry that can draw
2
+ // a shader names this module, so a CSS or Canvas2D entry never carries the WebGL2 mount — not even
3
+ // in a one-island build, where a bundler inlines every dynamic `import()` it can reach.
4
+
5
+ import type { mountShader, ShaderProgram } from './shader-mount';
6
+
7
+ export interface ShaderLoad {
8
+ readonly program: ShaderProgram;
9
+ readonly mount: typeof mountShader;
10
+ }
11
+
12
+ export function loadShader(program: Promise<ShaderProgram>): Promise<ShaderLoad> {
13
+ return Promise.all([program, import('./shader-mount')]).then(([loaded, module]) => ({
14
+ program: loaded,
15
+ mount: module.mountShader,
16
+ }));
17
+ }
@@ -0,0 +1,197 @@
1
+ // Our own WebGL2 mount: compile and link one program, feed it uniforms each frame, draw one
2
+ // full-screen triangle, survive a lost context, and give everything back on dispose. Techniques
3
+ // studied (IntersectionObserver/visibility pause, pixel cap, DPR floor, resize, dispose) — the
4
+ // code is written here. Pausing is `frameLoop`'s through the host; this file holds no GLSL.
5
+
6
+ import type { EffectHost } from './effect-host';
7
+ import { backingSize, EFFECT_PIXELS, type PixelBudget } from './pixel-budget';
8
+
9
+ /** One uniform: a float, or a float vector / vector array uploaded with the matching `*fv`. */
10
+ export type Uniform = readonly ['1f', number] | readonly ['2fv' | '3fv' | '4fv', Float32Array];
11
+
12
+ export interface ShaderFrame {
13
+ /** Seconds of animation since frame 0, already scaled by speed. */
14
+ readonly time: number;
15
+ /** Backing-store pixels. */
16
+ readonly width: number;
17
+ readonly height: number;
18
+ /** Backing pixels per CSS pixel: a shader sizing in CSS pixels multiplies by it. */
19
+ readonly scale: number;
20
+ }
21
+
22
+ export interface ShaderProgram {
23
+ /** GLSL ES 3.00. Drawn as `drawArrays(TRIANGLES, 0, 3)` with no attributes: use `gl_VertexID`. */
24
+ readonly vertex: string;
25
+ readonly fragment: string;
26
+ readonly uniforms: (frame: ShaderFrame) => Readonly<Record<string, Uniform>>;
27
+ }
28
+
29
+ export interface ShaderMountOptions {
30
+ readonly speed: number;
31
+ readonly onLost: () => void;
32
+ readonly onRestored: () => void;
33
+ readonly budget?: PixelBudget | undefined;
34
+ }
35
+
36
+ export interface ShaderMount {
37
+ readonly dispose: () => void;
38
+ }
39
+
40
+ export type ShaderMountResult =
41
+ | { readonly ok: true; readonly mount: ShaderMount }
42
+ | { readonly ok: false; readonly reason: 'no-context' | 'compile' | 'link' };
43
+
44
+ /** The longest step one frame may advance: a resumed tab continues, it does not leap. */
45
+ const MAX_STEP_MS = 100;
46
+
47
+ interface Linked {
48
+ readonly program: WebGLProgram;
49
+ readonly shaders: readonly WebGLShader[];
50
+ }
51
+
52
+ type Built = Linked | 'compile' | 'link';
53
+
54
+ function build(gl: WebGL2RenderingContext, source: ShaderProgram): Built {
55
+ const shaders: WebGLShader[] = [];
56
+ const release = (): void => {
57
+ for (const shader of shaders) gl.deleteShader(shader);
58
+ };
59
+ for (const [type, text] of [
60
+ [gl.VERTEX_SHADER, source.vertex],
61
+ [gl.FRAGMENT_SHADER, source.fragment],
62
+ ] as const) {
63
+ const shader = gl.createShader(type);
64
+ if (shader !== null) {
65
+ shaders.push(shader);
66
+ gl.shaderSource(shader, text);
67
+ gl.compileShader(shader);
68
+ }
69
+ if (shader === null || gl.getShaderParameter(shader, gl.COMPILE_STATUS) !== true) {
70
+ release();
71
+ return 'compile';
72
+ }
73
+ }
74
+ const program = gl.createProgram();
75
+ for (const shader of shaders) gl.attachShader(program, shader);
76
+ gl.linkProgram(program);
77
+ if (gl.getProgramParameter(program, gl.LINK_STATUS) !== true) {
78
+ gl.deleteProgram(program);
79
+ release();
80
+ return 'link';
81
+ }
82
+ return { program, shaders };
83
+ }
84
+
85
+ function upload(gl: WebGL2RenderingContext, at: WebGLUniformLocation, uniform: Uniform): void {
86
+ if (uniform[0] === '1f') gl.uniform1f(at, uniform[1]);
87
+ else if (uniform[0] === '2fv') gl.uniform2fv(at, uniform[1]);
88
+ else if (uniform[0] === '3fv') gl.uniform3fv(at, uniform[1]);
89
+ else gl.uniform4fv(at, uniform[1]);
90
+ }
91
+
92
+ export function mountShader(
93
+ canvas: HTMLCanvasElement,
94
+ source: ShaderProgram,
95
+ host: EffectHost,
96
+ options: ShaderMountOptions,
97
+ ): ShaderMountResult {
98
+ const gl = canvas.getContext('webgl2');
99
+ if (gl === null) return { ok: false, reason: 'no-context' };
100
+ // `null` while there is no program: before the first build, and while the context is lost.
101
+ let built: Linked | null = null;
102
+
103
+ const budget = options.budget ?? EFFECT_PIXELS;
104
+ const locations = new Map<string, WebGLUniformLocation | null>();
105
+ let box: readonly [number, number] = [0, 0];
106
+ let elapsed = 0;
107
+ let last: number | undefined;
108
+ let stopLoop: (() => void) | undefined;
109
+
110
+ const draw = (): void => {
111
+ if (built === null) return;
112
+ const now = host.now();
113
+ elapsed += last === undefined ? 0 : Math.min(now - last, MAX_STEP_MS) * options.speed;
114
+ last = now;
115
+ const size = backingSize(box[0], box[1], host.dpr(), budget);
116
+ if (canvas.width !== size.width) canvas.width = size.width;
117
+ if (canvas.height !== size.height) canvas.height = size.height;
118
+ gl.viewport(0, 0, size.width, size.height);
119
+ gl.useProgram(built.program);
120
+ const frame = {
121
+ time: elapsed / 1000,
122
+ width: size.width,
123
+ height: size.height,
124
+ scale: size.scale,
125
+ };
126
+ for (const [name, uniform] of Object.entries(source.uniforms(frame))) {
127
+ if (!locations.has(name)) locations.set(name, gl.getUniformLocation(built.program, name));
128
+ const at = locations.get(name);
129
+ if (at !== null && at !== undefined) upload(gl, at, uniform);
130
+ }
131
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
132
+ };
133
+ const start = (): void => {
134
+ last = undefined;
135
+ stopLoop = host.motion.loop(draw, canvas);
136
+ };
137
+ const stop = (): void => {
138
+ stopLoop?.();
139
+ stopLoop = undefined;
140
+ };
141
+ const onLost = (event: Event): void => {
142
+ // Without `preventDefault` the browser never offers the context back.
143
+ event.preventDefault();
144
+ stop();
145
+ built = null;
146
+ locations.clear();
147
+ options.onLost();
148
+ };
149
+ const onRestored = (): void => {
150
+ const rebuilt = build(gl, source);
151
+ if (typeof rebuilt === 'string') return;
152
+ built = rebuilt;
153
+ start();
154
+ options.onRestored();
155
+ };
156
+
157
+ // Listening BEFORE the first build: a context lost in between would otherwise read as a broken
158
+ // shader, with no restore listener to bring it back.
159
+ canvas.addEventListener('webglcontextlost', onLost);
160
+ canvas.addEventListener('webglcontextrestored', onRestored);
161
+ const first = gl.isContextLost() ? 'lost' : build(gl, source);
162
+ // A lost context fails every compile, so a failure is the source's only on a live context.
163
+ const outcome = typeof first === 'string' && gl.isContextLost() ? 'lost' : first;
164
+ if (outcome === 'compile' || outcome === 'link') {
165
+ canvas.removeEventListener('webglcontextlost', onLost);
166
+ canvas.removeEventListener('webglcontextrestored', onRestored);
167
+ // Nothing will draw on it: hand the context back now, not whenever GC finds it.
168
+ gl.getExtension('WEBGL_lose_context')?.loseContext();
169
+ return { ok: false, reason: outcome };
170
+ }
171
+ const unobserve = host.observeSize(canvas, (width, height) => {
172
+ box = [width, height];
173
+ });
174
+ if (outcome === 'lost') options.onLost();
175
+ else {
176
+ built = outcome;
177
+ start();
178
+ }
179
+
180
+ return {
181
+ ok: true,
182
+ mount: {
183
+ dispose: () => {
184
+ stop();
185
+ unobserve();
186
+ canvas.removeEventListener('webglcontextlost', onLost);
187
+ canvas.removeEventListener('webglcontextrestored', onRestored);
188
+ if (built !== null) {
189
+ gl.deleteProgram(built.program);
190
+ for (const shader of built.shaders) gl.deleteShader(shader);
191
+ }
192
+ // A page holds a handful of live contexts at most; hand this one back now, not at GC.
193
+ gl.getExtension('WEBGL_lose_context')?.loseContext();
194
+ },
195
+ },
196
+ };
197
+ }
@@ -0,0 +1,23 @@
1
+ @use '@ultimat3/ui/tokens' as t;
2
+
3
+ // Text lit by the shared sweep (`t.shimmer` is ui's, the same one Skeleton paints). Clipped to the
4
+ // glyphs, so the base role keeps the text's own contrast and the glint only brightens it.
5
+ .shimmer {
6
+ @include t.shimmer('fg-muted', 'fg');
7
+ background-clip: text;
8
+ -webkit-background-clip: text;
9
+ color: transparent;
10
+ }
11
+
12
+ :global(html[data-motion='paused']) .shimmer {
13
+ animation: none;
14
+ }
15
+
16
+ // No clip support, or forced colours (which would paint the transparent text invisible).
17
+ @media (forced-colors: active) {
18
+ .shimmer {
19
+ background: none;
20
+ color: CanvasText;
21
+ animation: none;
22
+ }
23
+ }
@@ -0,0 +1,19 @@
1
+ // Text with the shared highlight sweep. The sweep is ui's `@mixin shimmer` (the one Skeleton
2
+ // paints); this component only clips it to the glyphs.
3
+
4
+ import { cx } from '@ultimat3/ui';
5
+ import type { JSX } from 'solid-js';
6
+ import styles from './Shimmer.module.scss';
7
+
8
+ export interface ShimmerProps {
9
+ children?: JSX.Element | undefined;
10
+ class?: string | undefined;
11
+ }
12
+
13
+ export function Shimmer(props: ShimmerProps): JSX.Element {
14
+ return (
15
+ <span class={cx(styles['shimmer'], props.class)} data-effect-backend="css">
16
+ {props.children}
17
+ </span>
18
+ );
19
+ }
@@ -0,0 +1,29 @@
1
+ @use '@ultimat3/ui/tokens' as t;
2
+
3
+ .card {
4
+ @include t.surface;
5
+ position: relative;
6
+ isolation: isolate;
7
+ overflow: hidden;
8
+
9
+ // The highlight is a background layer ABOVE the fill and below the content: no overlay element,
10
+ // so no stacking order to manage and text contrast stays the surface's own. `--spot-alpha` is 0
11
+ // until the pointer or keyboard focus arrives.
12
+ --spot-alpha: 0;
13
+ background-image: radial-gradient(
14
+ var(--spot-size, 20rem) circle at var(--spot-x, 50%) var(--spot-y, 50%),
15
+ t.role('accent', var(--spot-alpha)),
16
+ transparent 70%
17
+ );
18
+
19
+ // The pointer and keyboard focus light the same highlight. `:focus-visible` is the keyboard's;
20
+ // a mouse click on a button inside does not light it a second time.
21
+ &:hover,
22
+ &:has(:focus-visible) {
23
+ --spot-alpha: 0.16;
24
+ }
25
+
26
+ @media (prefers-contrast: more), (forced-colors: active) {
27
+ background-image: none;
28
+ }
29
+ }
@@ -0,0 +1,28 @@
1
+ // A surface with a highlight that follows the pointer and, for a keyboard user, the focused
2
+ // control. The highlight is CSS; the script only writes two custom properties.
3
+
4
+ import { cx } from '@ultimat3/ui';
5
+ import type { JSX } from 'solid-js';
6
+ import styles from './SpotlightCard.module.scss';
7
+ import { SPOTLIGHT_BACKEND } from './spotlight-card-view';
8
+ import { spotlightFocus, spotlightPointer } from './spotlight-wire';
9
+
10
+ export interface SpotlightCardProps {
11
+ children?: JSX.Element | undefined;
12
+ class?: string | undefined;
13
+ }
14
+
15
+ export function SpotlightCard(props: SpotlightCardProps): JSX.Element {
16
+ return (
17
+ <div
18
+ class={cx(styles['card'], props.class)}
19
+ data-effect-backend={SPOTLIGHT_BACKEND}
20
+ onPointerMove={(event) => spotlightPointer(event.currentTarget, event.clientX, event.clientY)}
21
+ onFocusIn={(event) => {
22
+ if (event.target instanceof HTMLElement) spotlightFocus(event.currentTarget, event.target);
23
+ }}
24
+ >
25
+ {props.children}
26
+ </div>
27
+ );
28
+ }
@@ -0,0 +1,37 @@
1
+ // Where the spotlight sits: the pointer inside the card, or the focused control's centre for a
2
+ // keyboard user, so both inputs light the same highlight. Pure geometry; the DOM wiring is the
3
+ // component's.
4
+
5
+ export const SPOTLIGHT_BACKEND = 'static';
6
+
7
+ export interface SpotlightBox {
8
+ readonly left: number;
9
+ readonly top: number;
10
+ readonly width: number;
11
+ readonly height: number;
12
+ }
13
+
14
+ export interface SpotlightPoint {
15
+ readonly x: number;
16
+ readonly y: number;
17
+ }
18
+
19
+ const clamp = (value: number, max: number): number => Math.min(Math.max(value, 0), max);
20
+
21
+ /** The pointer relative to the card's box, held inside it (a drag can leave the card). */
22
+ export function spotlightPoint(
23
+ box: SpotlightBox,
24
+ clientX: number,
25
+ clientY: number,
26
+ ): SpotlightPoint {
27
+ return { x: clamp(clientX - box.left, box.width), y: clamp(clientY - box.top, box.height) };
28
+ }
29
+
30
+ /** The centre of `target` expressed in the card's own coordinates: what keyboard focus lights. */
31
+ export function spotlightFocusPoint(card: SpotlightBox, target: SpotlightBox): SpotlightPoint {
32
+ return spotlightPoint(card, target.left + target.width / 2, target.top + target.height / 2);
33
+ }
34
+
35
+ export function spotlightVars(point: SpotlightPoint): Readonly<Record<string, string>> {
36
+ return { '--spot-x': `${Math.round(point.x)}px`, '--spot-y': `${Math.round(point.y)}px` };
37
+ }
@@ -0,0 +1,34 @@
1
+ // SpotlightCard's two inputs over a card element: the pointer and keyboard focus.
2
+
3
+ import {
4
+ type SpotlightBox,
5
+ spotlightFocusPoint,
6
+ spotlightPoint,
7
+ spotlightVars,
8
+ } from './spotlight-card-view';
9
+
10
+ export interface SpotlightCardEl {
11
+ getBoundingClientRect(): SpotlightBox;
12
+ readonly style: { setProperty(name: string, value: string): void };
13
+ }
14
+
15
+ export interface SpotlightFocusEl {
16
+ getBoundingClientRect(): SpotlightBox;
17
+ matches(selector: string): boolean;
18
+ }
19
+
20
+ const aim = (card: SpotlightCardEl, point: { x: number; y: number }): void => {
21
+ for (const [name, value] of Object.entries(spotlightVars(point))) {
22
+ card.style.setProperty(name, value);
23
+ }
24
+ };
25
+
26
+ export function spotlightPointer(card: SpotlightCardEl, clientX: number, clientY: number): void {
27
+ aim(card, spotlightPoint(card.getBoundingClientRect(), clientX, clientY));
28
+ }
29
+
30
+ /** Only a keyboard focus (`:focus-visible`) aims; a mouse click inside is the pointer's. */
31
+ export function spotlightFocus(card: SpotlightCardEl, target: SpotlightFocusEl): void {
32
+ if (!target.matches(':focus-visible')) return;
33
+ aim(card, spotlightFocusPoint(card.getBoundingClientRect(), target.getBoundingClientRect()));
34
+ }
@@ -0,0 +1,47 @@
1
+ @use '@ultimat3/ui/tokens' as t;
2
+
3
+ @keyframes reveal-in {
4
+ from {
5
+ opacity: 0;
6
+ filter: blur(0.25em);
7
+ transform: translateY(0.4em);
8
+ }
9
+ }
10
+
11
+ .root {
12
+ display: inline-block;
13
+ position: relative;
14
+ }
15
+
16
+ // The one node assistive tech reads; the split below is its visual twin and `aria-hidden`.
17
+ .text {
18
+ @include t.visually-hidden;
19
+ }
20
+
21
+ .piece {
22
+ display: inline-block;
23
+ white-space: pre;
24
+ animation: reveal-in t.duration(slow) t.easing(out) both;
25
+ animation-delay: calc(var(--i) * var(--reveal-step, 40ms));
26
+ }
27
+
28
+ .rest {
29
+ white-space: pre-wrap;
30
+ }
31
+
32
+ .line {
33
+ display: block;
34
+ }
35
+
36
+ // A paused page shows the finished text, not a half-faded one.
37
+ :global(html[data-motion='paused']) .piece {
38
+ animation: none;
39
+ }
40
+
41
+ // Reduced motion collapses durations but keeps `animation-delay`, so pieces would stay hidden for
42
+ // i x step and then pop. No animation at all: the finished text, at once.
43
+ @media (prefers-reduced-motion: reduce) {
44
+ .piece {
45
+ animation: none;
46
+ }
47
+ }