@ultimat3/effects 0.0.0-stage → 31.52.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/CATALOG.md +169 -0
- package/CLAUDE.md +29 -0
- package/LICENSE +21 -0
- package/README.md +64 -2
- package/package.json +56 -4
- package/src/_effect.scss +53 -0
- package/src/aurora/Aurora.module.scss +56 -0
- package/src/aurora/Aurora.tsx +35 -0
- package/src/aurora/aurora-view.ts +32 -0
- package/src/backend.ts +34 -0
- package/src/canvas-mount.ts +61 -0
- package/src/count-up/CountUp.tsx +51 -0
- package/src/count-up/count-up-run.ts +59 -0
- package/src/count-up/count-up-view.ts +56 -0
- package/src/css-pause.ts +50 -0
- package/src/effect-canvas/EffectCanvas.module.scss +23 -0
- package/src/effect-canvas/EffectCanvas.tsx +51 -0
- package/src/effect-canvas/effect-canvas-view.ts +33 -0
- package/src/effect-host.ts +54 -0
- package/src/effect-mount.ts +114 -0
- package/src/errors.ts +33 -0
- package/src/glass-surface/GlassSurface.module.scss +25 -0
- package/src/glass-surface/GlassSurface.tsx +29 -0
- package/src/glass-surface/glass-surface-view.ts +33 -0
- package/src/gradient-orbs/GradientOrbs.module.scss +49 -0
- package/src/gradient-orbs/GradientOrbs.tsx +41 -0
- package/src/gradient-orbs/gradient-orbs-view.ts +77 -0
- package/src/grid-glow/GridGlow.module.scss +50 -0
- package/src/grid-glow/GridGlow.tsx +39 -0
- package/src/grid-glow/grid-glow-view.ts +40 -0
- package/src/index.ts +45 -0
- package/src/marquee/Marquee.module.scss +52 -0
- package/src/marquee/Marquee.tsx +31 -0
- package/src/marquee/marquee-view.ts +25 -0
- package/src/mosaic-grid/MosaicGrid.module.scss +39 -0
- package/src/mosaic-grid/MosaicGrid.tsx +59 -0
- package/src/mosaic-grid/mosaic-grid-view.ts +96 -0
- package/src/motion-port.ts +22 -0
- package/src/noise/Noise.module.scss +22 -0
- package/src/noise/Noise.tsx +49 -0
- package/src/noise/noise-view.ts +38 -0
- package/src/palette.ts +41 -0
- package/src/pixel-budget.ts +42 -0
- package/src/props.ts +61 -0
- package/src/shader-background/ShaderBackground.module.scss +70 -0
- package/src/shader-background/ShaderBackground.tsx +60 -0
- package/src/shader-background/load-preset.ts +19 -0
- package/src/shader-background/presets/dots.ts +59 -0
- package/src/shader-background/presets/grain.ts +59 -0
- package/src/shader-background/presets/mesh.ts +78 -0
- package/src/shader-background/shader-background-canvas.ts +116 -0
- package/src/shader-background/shader-background-view.ts +98 -0
- package/src/shader-background/uniform-pack.ts +11 -0
- package/src/shader-load.ts +17 -0
- package/src/shader-mount.ts +197 -0
- package/src/shimmer/Shimmer.module.scss +23 -0
- package/src/shimmer/Shimmer.tsx +19 -0
- package/src/spotlight-card/SpotlightCard.module.scss +29 -0
- package/src/spotlight-card/SpotlightCard.tsx +28 -0
- package/src/spotlight-card/spotlight-card-view.ts +37 -0
- package/src/spotlight-card/spotlight-wire.ts +34 -0
- package/src/text-reveal/TextReveal.module.scss +47 -0
- package/src/text-reveal/TextReveal.tsx +48 -0
- package/src/text-reveal/text-reveal-view.ts +38 -0
- package/src/tilt/Tilt.module.scss +18 -0
- package/src/tilt/Tilt.tsx +30 -0
- package/src/tilt/tilt-view.ts +64 -0
- package/src/tilt/tilt-wire.ts +50 -0
- package/src/use-css-pause.ts +35 -0
- package/src/use-effect-mount.ts +35 -0
- package/src/visibility.ts +26 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// A number that counts to its value. The server renders the FINAL text, so without script, under
|
|
2
|
+
// reduced motion and for a crawler the figure is already right; the tween is an enhancement.
|
|
3
|
+
|
|
4
|
+
import { solid, useUi } from '@ultimat3/ui';
|
|
5
|
+
import type { JSX } from 'solid-js';
|
|
6
|
+
import { browserHost } from '../effect-host';
|
|
7
|
+
import { runCountUp } from './count-up-run';
|
|
8
|
+
import {
|
|
9
|
+
COUNT_UP_BACKEND,
|
|
10
|
+
type CountTarget,
|
|
11
|
+
countFormatter,
|
|
12
|
+
countMs,
|
|
13
|
+
countScalar,
|
|
14
|
+
} from './count-up-view';
|
|
15
|
+
|
|
16
|
+
export interface CountUpProps {
|
|
17
|
+
/** The final value: a number, or a `Money` (counted in minor units, formatted by currency). */
|
|
18
|
+
value: CountTarget;
|
|
19
|
+
from?: number | undefined;
|
|
20
|
+
/** Tween length in ms; 0 shows the value at once. */
|
|
21
|
+
ms?: number | undefined;
|
|
22
|
+
/** `Intl.NumberFormat` options for a plain number. */
|
|
23
|
+
format?: Intl.NumberFormatOptions | undefined;
|
|
24
|
+
class?: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function CountUp(props: CountUpProps): JSX.Element {
|
|
28
|
+
const { locale } = useUi();
|
|
29
|
+
const to = countScalar(props.value);
|
|
30
|
+
const ms = countMs(props.ms);
|
|
31
|
+
const format = countFormatter(props.value, locale, props.format);
|
|
32
|
+
const rt = solid();
|
|
33
|
+
let el: HTMLSpanElement | undefined;
|
|
34
|
+
|
|
35
|
+
rt.createEffect(() => {
|
|
36
|
+
if (el === undefined) return;
|
|
37
|
+
rt.onCleanup(runCountUp(el, el, { from: props.from ?? 0, to, ms, format }, browserHost()));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<span
|
|
42
|
+
ref={(node) => {
|
|
43
|
+
el = node;
|
|
44
|
+
}}
|
|
45
|
+
class={props.class}
|
|
46
|
+
data-effect-backend={COUNT_UP_BACKEND}
|
|
47
|
+
>
|
|
48
|
+
{format(to)}
|
|
49
|
+
</span>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// The island half of CountUp: tween the text from `from` to the final value on the host's frame
|
|
2
|
+
// loop. Takes the host so a test drives frames by hand; the browser's is `browserHost()`.
|
|
3
|
+
|
|
4
|
+
import type { EffectHost } from '../effect-host';
|
|
5
|
+
import { countFrame } from './count-up-view';
|
|
6
|
+
|
|
7
|
+
/** The longest one frame may advance the tween: a stopped-and-resumed loop does not jump. */
|
|
8
|
+
const MAX_STEP_MS = 100;
|
|
9
|
+
|
|
10
|
+
export interface CountRun {
|
|
11
|
+
readonly from: number;
|
|
12
|
+
readonly to: number;
|
|
13
|
+
readonly ms: number;
|
|
14
|
+
readonly format: (scalar: number) => string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface CountNode {
|
|
18
|
+
textContent: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Returns the cleanup. Under reduced motion or `ms: 0` nothing runs and the server's final text
|
|
23
|
+
* stays. The loop stops itself offscreen, hidden and paused (the port's job); the cleanup always
|
|
24
|
+
* leaves the exact final text, so a stopped tween never strands a half-way figure.
|
|
25
|
+
*/
|
|
26
|
+
export function runCountUp(
|
|
27
|
+
node: CountNode,
|
|
28
|
+
el: Element,
|
|
29
|
+
run: CountRun,
|
|
30
|
+
host: EffectHost,
|
|
31
|
+
): () => void {
|
|
32
|
+
if (run.ms === 0 || !host.motion.allowed()) return () => {};
|
|
33
|
+
node.textContent = run.format(run.from);
|
|
34
|
+
// The clock starts on the first tick, not at mount: a counter below the fold is not frameLoop's
|
|
35
|
+
// to run until it scrolls in, and a clock started earlier would have spent the whole tween.
|
|
36
|
+
let elapsed = 0;
|
|
37
|
+
let last: number | undefined;
|
|
38
|
+
let stop: (() => void) | undefined;
|
|
39
|
+
const finish = (): void => {
|
|
40
|
+
stop?.();
|
|
41
|
+
node.textContent = run.format(run.to);
|
|
42
|
+
};
|
|
43
|
+
stop = host.motion.loop(() => {
|
|
44
|
+
const now = host.now();
|
|
45
|
+
// Accumulated, capped per frame: time spent paused or offscreen is not time spent counting.
|
|
46
|
+
elapsed += last === undefined ? 0 : Math.min(now - last, MAX_STEP_MS);
|
|
47
|
+
last = now;
|
|
48
|
+
node.textContent = run.format(countFrame(run.from, run.to, elapsed, run.ms));
|
|
49
|
+
if (elapsed >= run.ms) stop?.();
|
|
50
|
+
}, el);
|
|
51
|
+
// A pause (MotionToggle, reduced motion) stops the loop where it stands; the figure must not.
|
|
52
|
+
const unsubscribe = host.motion.onChange(() => {
|
|
53
|
+
if (!host.motion.allowed()) finish();
|
|
54
|
+
});
|
|
55
|
+
return () => {
|
|
56
|
+
unsubscribe();
|
|
57
|
+
finish();
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// CountUp's maths and formatting. The final value is what the server renders, through the same
|
|
2
|
+
// formatter every frame uses, so frame N of the tween and the static text can never disagree
|
|
3
|
+
// about grouping, decimals or where a currency symbol sits.
|
|
4
|
+
|
|
5
|
+
import { formatMoney, fromMinor, type Money } from '@ultimat3/money';
|
|
6
|
+
import { EffectsInvalidValueError } from '../errors';
|
|
7
|
+
|
|
8
|
+
export const COUNT_UP_BACKEND = 'static';
|
|
9
|
+
export const COUNT_UP_DEFAULT_MS = 1000;
|
|
10
|
+
|
|
11
|
+
export type CountTarget = number | Money;
|
|
12
|
+
|
|
13
|
+
export const isMoneyTarget = (target: CountTarget): target is Money => typeof target !== 'number';
|
|
14
|
+
|
|
15
|
+
/** The number the tween interpolates: the value itself, or a Money's integer minor units. */
|
|
16
|
+
export function countScalar(target: CountTarget): number {
|
|
17
|
+
return isMoneyTarget(target) ? target.minor : target;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function countMs(requested: number | undefined): number {
|
|
21
|
+
if (requested === undefined) return COUNT_UP_DEFAULT_MS;
|
|
22
|
+
if (!Number.isFinite(requested) || requested < 0) {
|
|
23
|
+
throw new EffectsInvalidValueError({
|
|
24
|
+
cause: `CountUp ms must be a finite number >= 0, got ${String(requested)}.`,
|
|
25
|
+
fix: `<CountUp value={1200} ms={${COUNT_UP_DEFAULT_MS}} /> // ms >= 0; 0 shows the final value at once, or omit ms`,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return requested;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A formatter for one target: Money through `formatMoney`, a number through `Intl.NumberFormat`. */
|
|
32
|
+
export function countFormatter(
|
|
33
|
+
target: CountTarget,
|
|
34
|
+
locale: string,
|
|
35
|
+
options?: Intl.NumberFormatOptions,
|
|
36
|
+
): (scalar: number) => string {
|
|
37
|
+
if (isMoneyTarget(target)) {
|
|
38
|
+
const { currency } = target;
|
|
39
|
+
return (scalar) => formatMoney(fromMinor(Math.round(scalar), currency), locale);
|
|
40
|
+
}
|
|
41
|
+
const format = new Intl.NumberFormat(locale, options);
|
|
42
|
+
// `Intl` shows up to three fraction digits, so 788.4 would flash by on the way to 1,000: an
|
|
43
|
+
// integer target with no fraction options counts in whole numbers.
|
|
44
|
+
const whole =
|
|
45
|
+
Number.isInteger(target) &&
|
|
46
|
+
options?.minimumFractionDigits === undefined &&
|
|
47
|
+
options?.maximumFractionDigits === undefined;
|
|
48
|
+
return (scalar) => format.format(whole ? Math.round(scalar) : scalar);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Ease-out cubic position between `from` and `to`; exactly `to` once `elapsed >= ms`. */
|
|
52
|
+
export function countFrame(from: number, to: number, elapsed: number, ms: number): number {
|
|
53
|
+
if (ms <= 0 || elapsed >= ms) return to;
|
|
54
|
+
const t = Math.max(elapsed, 0) / ms;
|
|
55
|
+
return from + (to - from) * (1 - (1 - t) ** 3);
|
|
56
|
+
}
|
package/src/css-pause.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The pause a CSS-animated entry gets in an island: `data-effect-paused` while its root is off
|
|
2
|
+
// screen or the tab is hidden (`_effect.scss` holds the animation on it), and the backend label
|
|
3
|
+
// from `pickBackend`. No canvas, no frame loop, no shader: an entry that only animates in CSS
|
|
4
|
+
// carries this and nothing else.
|
|
5
|
+
|
|
6
|
+
import { type EffectBackend, pickBackend } from './backend';
|
|
7
|
+
|
|
8
|
+
export interface CssPauseEnv {
|
|
9
|
+
readonly observeVisible: (el: Element, onVisible: (visible: boolean) => void) => () => void;
|
|
10
|
+
readonly allowed: () => boolean;
|
|
11
|
+
readonly onChange: (listener: () => void) => () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Writes `data-effect-paused` while `root` should not animate; the answer stops watching. */
|
|
15
|
+
export function pauseOffscreen(
|
|
16
|
+
root: HTMLElement,
|
|
17
|
+
observeVisible: CssPauseEnv['observeVisible'],
|
|
18
|
+
): () => void {
|
|
19
|
+
return observeVisible(root, (visible) => {
|
|
20
|
+
if (visible) root.removeAttribute('data-effect-paused');
|
|
21
|
+
else root.setAttribute('data-effect-paused', '');
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The offscreen pause plus the label (`css`, or `static` under reduced motion / a user pause). */
|
|
26
|
+
export function mountCssPause(
|
|
27
|
+
root: HTMLElement,
|
|
28
|
+
env: CssPauseEnv,
|
|
29
|
+
label: (backend: EffectBackend) => void,
|
|
30
|
+
): () => void {
|
|
31
|
+
const relabel = (): void =>
|
|
32
|
+
label(
|
|
33
|
+
pickBackend({
|
|
34
|
+
motion: env.allowed(),
|
|
35
|
+
hydrated: true,
|
|
36
|
+
css: true,
|
|
37
|
+
wants: 'css',
|
|
38
|
+
webgl2: false,
|
|
39
|
+
canvas: false,
|
|
40
|
+
lost: false,
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
const unpause = pauseOffscreen(root, env.observeVisible);
|
|
44
|
+
const unsubscribe = env.onChange(relabel);
|
|
45
|
+
relabel();
|
|
46
|
+
return () => {
|
|
47
|
+
unpause();
|
|
48
|
+
unsubscribe();
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
@use '../effect' as fx;
|
|
2
|
+
|
|
3
|
+
.root {
|
|
4
|
+
@include fx.root;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.layer {
|
|
8
|
+
@include fx.layer;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.content {
|
|
12
|
+
@include fx.content;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.frame {
|
|
16
|
+
@include fx.frame('.root');
|
|
17
|
+
position: absolute;
|
|
18
|
+
inset: 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.canvas {
|
|
22
|
+
@include fx.canvas('.root');
|
|
23
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// The escape hatch for an effect the catalogue does not have: an app's own Canvas2D `draw` or
|
|
2
|
+
// WebGL2 `shader`, mounted on the same budget, pause and lost-context rules as every built-in.
|
|
3
|
+
// `fallback` is REQUIRED by type — it is the server frame, the reduced-motion frame, the no-GPU frame.
|
|
4
|
+
|
|
5
|
+
import type { ColorRole } from '@ultimat3/ui';
|
|
6
|
+
import { cx } from '@ultimat3/ui';
|
|
7
|
+
import type { JSX } from 'solid-js';
|
|
8
|
+
import type { CanvasDraw } from '../canvas-mount';
|
|
9
|
+
import type { Rgb } from '../palette';
|
|
10
|
+
import type { ShaderProgram } from '../shader-mount';
|
|
11
|
+
import { useEffectMount } from '../use-effect-mount';
|
|
12
|
+
import styles from './EffectCanvas.module.scss';
|
|
13
|
+
import { EFFECT_CANVAS_BACKEND, effectCanvasSpec } from './effect-canvas-view';
|
|
14
|
+
|
|
15
|
+
interface EffectCanvasBase {
|
|
16
|
+
/** What shows before an island draws, under reduced motion, and with no usable context. */
|
|
17
|
+
fallback: JSX.Element;
|
|
18
|
+
/** Roles resolved to channels for `draw`/`shader`, 1..8. Default `['accent']`. */
|
|
19
|
+
roles?: readonly ColorRole[] | undefined;
|
|
20
|
+
/** Time multiplier, 0.25..4. Default 1. */
|
|
21
|
+
speed?: number | undefined;
|
|
22
|
+
children?: JSX.Element | undefined;
|
|
23
|
+
class?: string | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Canvas2D: `draw` builds the per-frame function from the resolved palette. */
|
|
27
|
+
export interface EffectCanvasDrawProps extends EffectCanvasBase {
|
|
28
|
+
draw: (colours: () => readonly Rgb[]) => CanvasDraw;
|
|
29
|
+
shader?: ((colours: () => readonly Rgb[]) => Promise<ShaderProgram>) | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** WebGL2: `shader` loads the program (lazily, so its GLSL is its own chunk); `draw` is the Canvas2D fallback. */
|
|
33
|
+
export interface EffectCanvasShaderProps extends EffectCanvasBase {
|
|
34
|
+
shader: (colours: () => readonly Rgb[]) => Promise<ShaderProgram>;
|
|
35
|
+
draw?: ((colours: () => readonly Rgb[]) => CanvasDraw) | undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type EffectCanvasProps = EffectCanvasDrawProps | EffectCanvasShaderProps;
|
|
39
|
+
|
|
40
|
+
export function EffectCanvas(props: EffectCanvasProps): JSX.Element {
|
|
41
|
+
const fx = useEffectMount(effectCanvasSpec(props), EFFECT_CANVAS_BACKEND);
|
|
42
|
+
return (
|
|
43
|
+
<div ref={fx.root} class={cx(styles['root'], props.class)} data-effect-backend={fx.backend()}>
|
|
44
|
+
<div class={styles['layer']} aria-hidden="true">
|
|
45
|
+
<div class={styles['frame']}>{props.fallback}</div>
|
|
46
|
+
<canvas ref={fx.canvas} class={styles['canvas']} />
|
|
47
|
+
</div>
|
|
48
|
+
<div class={styles['content']}>{props.children}</div>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// EffectCanvas's rules: an app's own drawing becomes the same `EffectSpec` every built-in entry
|
|
2
|
+
// mounts — the richest backend it supplied, its roles screened, its speed in range.
|
|
3
|
+
|
|
4
|
+
import type { ColorRole } from '@ultimat3/ui';
|
|
5
|
+
import type { EffectBackend } from '../backend';
|
|
6
|
+
import type { CanvasDraw } from '../canvas-mount';
|
|
7
|
+
import type { EffectSpec } from '../effect-mount';
|
|
8
|
+
import type { Rgb } from '../palette';
|
|
9
|
+
import { rangeProp, rolesProp } from '../props';
|
|
10
|
+
import { loadShader } from '../shader-load';
|
|
11
|
+
import type { ShaderProgram } from '../shader-mount';
|
|
12
|
+
|
|
13
|
+
/** The server renders the app's `fallback`: a still frame until an island draws. */
|
|
14
|
+
export const EFFECT_CANVAS_BACKEND: EffectBackend = 'static';
|
|
15
|
+
|
|
16
|
+
export interface EffectCanvasInput {
|
|
17
|
+
readonly draw?: ((colours: () => readonly Rgb[]) => CanvasDraw) | undefined;
|
|
18
|
+
readonly shader?: ((colours: () => readonly Rgb[]) => Promise<ShaderProgram>) | undefined;
|
|
19
|
+
readonly roles?: readonly ColorRole[] | undefined;
|
|
20
|
+
readonly speed?: number | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function effectCanvasSpec(input: EffectCanvasInput): EffectSpec {
|
|
24
|
+
const shader = input.shader;
|
|
25
|
+
return {
|
|
26
|
+
css: false,
|
|
27
|
+
wants: shader === undefined ? 'canvas' : 'webgl2',
|
|
28
|
+
speed: rangeProp('EffectCanvas', 'speed', input.speed, { min: 0.25, max: 4, fallback: 1 }),
|
|
29
|
+
roles: rolesProp('EffectCanvas', input.roles, ['accent'], { min: 1, max: 8 }),
|
|
30
|
+
shader: shader === undefined ? undefined : (colours) => loadShader(shader(colours)),
|
|
31
|
+
draw2d: input.draw,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Everything a mount asks the browser, as one injected value: the motion port, the clock, the
|
|
2
|
+
// device pixel ratio, box sizes, visibility and resolved custom properties. The browser's answer is
|
|
3
|
+
// `browserHost()`; a test hands its own and drives 1,000 frames with no DOM at all.
|
|
4
|
+
|
|
5
|
+
import { THEME_ATTRIBUTE } from '@ultimat3/ui';
|
|
6
|
+
import { browserMotion, type MotionPort } from './motion-port';
|
|
7
|
+
import { observeVisibility } from './visibility';
|
|
8
|
+
|
|
9
|
+
export interface EffectHost {
|
|
10
|
+
readonly motion: MotionPort;
|
|
11
|
+
/** Milliseconds, monotonic. */
|
|
12
|
+
readonly now: () => number;
|
|
13
|
+
readonly dpr: () => number;
|
|
14
|
+
/** Calls back with the CSS-pixel box now and on every resize; the answer stops observing. */
|
|
15
|
+
readonly observeSize: (
|
|
16
|
+
el: Element,
|
|
17
|
+
onSize: (width: number, height: number) => void,
|
|
18
|
+
) => () => void;
|
|
19
|
+
/** Calls back with whether `el` is on screen in a visible tab; the answer stops observing. */
|
|
20
|
+
readonly observeVisible: (el: Element, onVisible: (visible: boolean) => void) => () => void;
|
|
21
|
+
/** Calls back when the resolved roles may have changed: a theme toggle, the OS scheme. */
|
|
22
|
+
readonly observeTheme: (onChange: () => void) => () => void;
|
|
23
|
+
/** A resolved custom property on `el`, `''` when unset. */
|
|
24
|
+
readonly readVar: (el: Element, name: string) => string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function browserHost(): EffectHost {
|
|
28
|
+
return {
|
|
29
|
+
motion: browserMotion(),
|
|
30
|
+
now: () => performance.now(),
|
|
31
|
+
dpr: () => window.devicePixelRatio,
|
|
32
|
+
observeSize: (el, onSize) => {
|
|
33
|
+
const observer = new ResizeObserver((entries) => {
|
|
34
|
+
for (const entry of entries) onSize(entry.contentRect.width, entry.contentRect.height);
|
|
35
|
+
});
|
|
36
|
+
observer.observe(el);
|
|
37
|
+
const box = el.getBoundingClientRect();
|
|
38
|
+
onSize(box.width, box.height);
|
|
39
|
+
return () => observer.disconnect();
|
|
40
|
+
},
|
|
41
|
+
observeVisible: observeVisibility,
|
|
42
|
+
observeTheme: (onChange) => {
|
|
43
|
+
const observer = new MutationObserver(() => onChange());
|
|
44
|
+
observer.observe(document.documentElement, { attributeFilter: [THEME_ATTRIBUTE] });
|
|
45
|
+
const scheme = matchMedia('(prefers-color-scheme: dark)');
|
|
46
|
+
scheme.addEventListener('change', onChange);
|
|
47
|
+
return () => {
|
|
48
|
+
observer.disconnect();
|
|
49
|
+
scheme.removeEventListener('change', onChange);
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
readVar: (el, name) => getComputedStyle(el).getPropertyValue(name).trim(),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// What every animated entry's island effect calls: resolve the role palette, pick the backend,
|
|
2
|
+
// mount the richest context it got (WebGL2 → Canvas2D → the server frame), relabel on a pause or a
|
|
3
|
+
// lost context, and give everything back on unmount. The `.tsx` holds markup; this holds the rule.
|
|
4
|
+
|
|
5
|
+
import type { ColorRole } from '@ultimat3/ui';
|
|
6
|
+
import { type EffectBackend, type EffectWants, pickBackend } from './backend';
|
|
7
|
+
import { type CanvasDraw, mountCanvas } from './canvas-mount';
|
|
8
|
+
import { pauseOffscreen } from './css-pause';
|
|
9
|
+
import type { EffectHost } from './effect-host';
|
|
10
|
+
import { type Rgb, resolvePalette } from './palette';
|
|
11
|
+
import type { ShaderLoad } from './shader-load';
|
|
12
|
+
|
|
13
|
+
export interface EffectSpec {
|
|
14
|
+
/** Whether the entry's server frame animates in CSS. */
|
|
15
|
+
readonly css: boolean;
|
|
16
|
+
readonly wants: EffectWants;
|
|
17
|
+
readonly speed: number;
|
|
18
|
+
readonly roles: readonly ColorRole[];
|
|
19
|
+
/** Lazy (`loadShader`), so the GLSL and the WebGL2 mount reach only an island that draws them. */
|
|
20
|
+
readonly shader?: ((colours: () => readonly Rgb[]) => Promise<ShaderLoad>) | undefined;
|
|
21
|
+
readonly draw2d?: ((colours: () => readonly Rgb[]) => CanvasDraw) | undefined;
|
|
22
|
+
/** Pause the CSS animation offscreen and in a hidden tab (`data-effect-paused`). */
|
|
23
|
+
readonly pauseCss?: boolean | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Why a richer backend was not used, written to `data-effect-fallback` for whoever debugs it. */
|
|
27
|
+
type Fallback = 'compile' | 'link' | 'load';
|
|
28
|
+
|
|
29
|
+
export function mountEffect(
|
|
30
|
+
root: HTMLElement,
|
|
31
|
+
canvas: HTMLCanvasElement | undefined,
|
|
32
|
+
spec: EffectSpec,
|
|
33
|
+
host: EffectHost,
|
|
34
|
+
label: (backend: EffectBackend) => void,
|
|
35
|
+
): () => void {
|
|
36
|
+
let colours = resolvePalette(host, root, spec.roles);
|
|
37
|
+
const current = (): readonly Rgb[] => colours ?? [];
|
|
38
|
+
const state = { webgl2: false, canvas: false, lost: false, loading: false, done: false };
|
|
39
|
+
let release: (() => void) | undefined;
|
|
40
|
+
|
|
41
|
+
const relabel = (): void =>
|
|
42
|
+
label(
|
|
43
|
+
pickBackend({
|
|
44
|
+
motion: host.motion.allowed(),
|
|
45
|
+
hydrated: true,
|
|
46
|
+
css: spec.css,
|
|
47
|
+
wants: spec.wants,
|
|
48
|
+
webgl2: state.webgl2,
|
|
49
|
+
canvas: state.canvas,
|
|
50
|
+
lost: state.lost,
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
const fallBack = (why: Fallback): void => root.setAttribute('data-effect-fallback', why);
|
|
54
|
+
const tryCanvas = (target: HTMLCanvasElement): void => {
|
|
55
|
+
if (spec.draw2d === undefined) return;
|
|
56
|
+
release = mountCanvas(target, spec.draw2d(current), host, { speed: spec.speed });
|
|
57
|
+
state.canvas = release !== undefined;
|
|
58
|
+
};
|
|
59
|
+
const tryShader = (target: HTMLCanvasElement, load: NonNullable<EffectSpec['shader']>): void => {
|
|
60
|
+
state.loading = true;
|
|
61
|
+
load(current)
|
|
62
|
+
.then(({ program, mount }) => {
|
|
63
|
+
if (state.done) return;
|
|
64
|
+
const result = mount(target, program, host, {
|
|
65
|
+
speed: spec.speed,
|
|
66
|
+
onLost: () => {
|
|
67
|
+
state.lost = true;
|
|
68
|
+
relabel();
|
|
69
|
+
},
|
|
70
|
+
onRestored: () => {
|
|
71
|
+
state.lost = false;
|
|
72
|
+
relabel();
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
if (result.ok) {
|
|
76
|
+
state.webgl2 = true;
|
|
77
|
+
release = result.mount.dispose;
|
|
78
|
+
} else if (result.reason === 'no-context') tryCanvas(target);
|
|
79
|
+
else fallBack(result.reason);
|
|
80
|
+
})
|
|
81
|
+
.catch(() => fallBack('load'))
|
|
82
|
+
.finally(() => {
|
|
83
|
+
state.loading = false;
|
|
84
|
+
if (!state.done) relabel();
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
// Mounts at most once: a pause after that is `frameLoop`'s, and only the label changes.
|
|
88
|
+
const ensure = (): void => {
|
|
89
|
+
if (canvas === undefined || spec.wants === 'css' || release !== undefined) return;
|
|
90
|
+
if (state.loading || colours === undefined || !host.motion.allowed()) return;
|
|
91
|
+
if (spec.wants === 'webgl2' && spec.shader !== undefined) tryShader(canvas, spec.shader);
|
|
92
|
+
else tryCanvas(canvas);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const unsubscribe = host.motion.onChange(() => {
|
|
96
|
+
relabel();
|
|
97
|
+
ensure();
|
|
98
|
+
});
|
|
99
|
+
const unTheme = host.observeTheme(() => {
|
|
100
|
+
colours = resolvePalette(host, root, spec.roles) ?? colours;
|
|
101
|
+
ensure();
|
|
102
|
+
});
|
|
103
|
+
const unVisible = spec.pauseCss === true ? pauseOffscreen(root, host.observeVisible) : undefined;
|
|
104
|
+
ensure();
|
|
105
|
+
relabel();
|
|
106
|
+
|
|
107
|
+
return () => {
|
|
108
|
+
state.done = true;
|
|
109
|
+
release?.();
|
|
110
|
+
unsubscribe();
|
|
111
|
+
unTheme();
|
|
112
|
+
unVisible?.();
|
|
113
|
+
};
|
|
114
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The X_* codes owned by @ultimat3/effects. Each names the exact change that resolves it.
|
|
2
|
+
//
|
|
3
|
+
// No `docs:` line, and that is deliberate: `UltimateError`'s constructor resolves the registered
|
|
4
|
+
// descriptor, whose default is `ERROR_DOCS_URL` in @ultimat3/core. A URL written out here is a
|
|
5
|
+
// second answer to a question core already answers, and the last one went stale host and all.
|
|
6
|
+
import { registerErrorCodes, UltimateError } from '@ultimat3/core';
|
|
7
|
+
|
|
8
|
+
// The shape `bun run new-error-code` adds to: the owned array, the TITLES beside it, and the
|
|
9
|
+
// registration. A package written another way is refused by that generator, so it is written this
|
|
10
|
+
// way from the first file.
|
|
11
|
+
export const EFFECTS_ERROR_CODES = [
|
|
12
|
+
// Kept multi-line, a comment holding it open: the generator appends one line per code.
|
|
13
|
+
'X_EFFECTS_INVALID_VALUE',
|
|
14
|
+
] as const;
|
|
15
|
+
|
|
16
|
+
export type EffectsErrorCode = (typeof EFFECTS_ERROR_CODES)[number];
|
|
17
|
+
|
|
18
|
+
export const EFFECTS_ERROR_TITLES: Readonly<Record<EffectsErrorCode, string>> = {
|
|
19
|
+
X_EFFECTS_INVALID_VALUE: 'effect prop out of range',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
registerErrorCodes(
|
|
23
|
+
Object.fromEntries(
|
|
24
|
+
Object.entries(EFFECTS_ERROR_TITLES).map(([code, title]) => [code, { title }]),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
/** A prop an effect cannot draw: out of its range, not finite, or not one of its names. */
|
|
29
|
+
export class EffectsInvalidValueError extends UltimateError {
|
|
30
|
+
constructor(input: { cause: string; fix: string }) {
|
|
31
|
+
super({ code: 'X_EFFECTS_INVALID_VALUE', cause: input.cause, fix: input.fix });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
@use '@ultimat3/ui/tokens' as t;
|
|
2
|
+
|
|
3
|
+
// A pane the page shows through. `t.surface` supplies border, radius and elevation; the fill is
|
|
4
|
+
// the same `surface` role at a measured alpha (`GLASS_SURFACE_ALPHA`, held against the contrast
|
|
5
|
+
// pairs in `glass-surface-view.test.ts`), so text on it keeps the palette's contrast.
|
|
6
|
+
.glass {
|
|
7
|
+
@include t.surface;
|
|
8
|
+
background: t.role('surface', var(--glass-alpha, 0.82));
|
|
9
|
+
backdrop-filter: blur(var(--glass-blur)) saturate(140%);
|
|
10
|
+
|
|
11
|
+
// Blur needs the compositor and reads as noise to a user who asked for less transparency:
|
|
12
|
+
// they get the opaque surface the system already has.
|
|
13
|
+
@media (prefers-reduced-transparency: reduce), (prefers-contrast: more), (forced-colors: active) {
|
|
14
|
+
background: t.role('surface');
|
|
15
|
+
backdrop-filter: none;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// An engine without `backdrop-filter` shows the translucent fill over unblurred content, which
|
|
20
|
+
// is where legibility drops: it gets the opaque surface too.
|
|
21
|
+
@supports not (backdrop-filter: blur(1px)) {
|
|
22
|
+
.glass {
|
|
23
|
+
background: t.role('surface');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// A translucent pane over whatever is behind it, with the palette's text contrast kept (the alpha
|
|
2
|
+
// has a measured floor) and an opaque fill under `prefers-reduced-transparency`.
|
|
3
|
+
|
|
4
|
+
import { cx } from '@ultimat3/ui';
|
|
5
|
+
import type { JSX } from 'solid-js';
|
|
6
|
+
import styles from './GlassSurface.module.scss';
|
|
7
|
+
import { GLASS_BACKEND, GLASS_BLUR_PX, glassAlpha } from './glass-surface-view';
|
|
8
|
+
|
|
9
|
+
export interface GlassSurfaceProps {
|
|
10
|
+
/** Pane opacity 0-1; never below the contrast-measured floor. */
|
|
11
|
+
alpha?: number | undefined;
|
|
12
|
+
children?: JSX.Element | undefined;
|
|
13
|
+
class?: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function GlassSurface(props: GlassSurfaceProps): JSX.Element {
|
|
17
|
+
return (
|
|
18
|
+
<div
|
|
19
|
+
class={cx(styles['glass'], props.class)}
|
|
20
|
+
data-effect-backend={GLASS_BACKEND}
|
|
21
|
+
style={{
|
|
22
|
+
'--glass-alpha': String(glassAlpha(props.alpha)),
|
|
23
|
+
'--glass-blur': `${GLASS_BLUR_PX}px`,
|
|
24
|
+
}}
|
|
25
|
+
>
|
|
26
|
+
{props.children}
|
|
27
|
+
</div>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The glass recipe as numbers, so the contrast claim is testable: how translucent the pane is and
|
|
2
|
+
// what a given backdrop does to its colour. The SCSS reads the same alpha through one token.
|
|
3
|
+
|
|
4
|
+
export const GLASS_BACKEND = 'static';
|
|
5
|
+
/** How much of the surface role shows. Below this, text on a worst-case backdrop leaves AA. */
|
|
6
|
+
export const GLASS_SURFACE_ALPHA = 0.82;
|
|
7
|
+
export const GLASS_BLUR_PX = 12;
|
|
8
|
+
|
|
9
|
+
/** `"r g b"` channels (the token format) → numbers. */
|
|
10
|
+
const parse = (channels: string): [number, number, number] => {
|
|
11
|
+
const [r = 0, g = 0, b = 0] = channels.split(' ').map(Number);
|
|
12
|
+
return [r, g, b];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** The colour a translucent `top` lays over an opaque `under`: plain source-over, per channel. */
|
|
16
|
+
export function compositeOver(top: string, alpha: number, under: string): string {
|
|
17
|
+
const a = parse(top);
|
|
18
|
+
const b = parse(under);
|
|
19
|
+
return a.map((channel, i) => Math.round(channel * alpha + (b[i] ?? 0) * (1 - alpha))).join(' ');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* What glass is measured against: the roles a pane sits on inside one theme. Pure black or white
|
|
24
|
+
* behind it (a photograph) would need alpha ~0.98, which is no longer glass; an app with such a
|
|
25
|
+
* backdrop passes a higher `alpha`, and `glassAlpha` never goes below this floor.
|
|
26
|
+
*/
|
|
27
|
+
export const GLASS_BACKDROP_ROLES = ['bg', 'bg-soft', 'surface', 'surface-raised', 'line'] as const;
|
|
28
|
+
|
|
29
|
+
/** The requested opacity of the pane, held to [GLASS_SURFACE_ALPHA, 1]: translucency is a floor. */
|
|
30
|
+
export function glassAlpha(requested: number | undefined): number {
|
|
31
|
+
if (requested === undefined || !Number.isFinite(requested)) return GLASS_SURFACE_ALPHA;
|
|
32
|
+
return Math.min(Math.max(requested, GLASS_SURFACE_ALPHA), 1);
|
|
33
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
@use '@ultimat3/ui/tokens' as t;
|
|
2
|
+
@use '../effect' as fx;
|
|
3
|
+
|
|
4
|
+
// Transform and opacity only: the compositor moves the orbs, layout and paint never run.
|
|
5
|
+
@keyframes orbs-float {
|
|
6
|
+
33% {
|
|
7
|
+
transform: translate3d(12%, -10%, 0) scale(1.1);
|
|
8
|
+
opacity: 0.8;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
66% {
|
|
12
|
+
transform: translate3d(-10%, 8%, 0) scale(0.9);
|
|
13
|
+
opacity: 1;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.root {
|
|
18
|
+
@include fx.root;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.layer {
|
|
22
|
+
@include fx.layer;
|
|
23
|
+
opacity: var(--fx-intensity, 0.6);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.content {
|
|
27
|
+
@include fx.content;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.orb {
|
|
31
|
+
@include fx.pausable('.root');
|
|
32
|
+
position: absolute;
|
|
33
|
+
inset-inline-start: calc(var(--fx-x) - var(--fx-size) / 2);
|
|
34
|
+
inset-block-start: calc(var(--fx-y) - var(--fx-size) / 2);
|
|
35
|
+
inline-size: var(--fx-size);
|
|
36
|
+
aspect-ratio: 1;
|
|
37
|
+
border-radius: t.radius(full);
|
|
38
|
+
background: radial-gradient(circle, rgb(var(--fx-c)) 0, rgb(var(--fx-c) / 0) 70%);
|
|
39
|
+
animation: orbs-float calc(#{t.duration(slower)} * 25 / var(--fx-speed, 1)) t.easing(in-out)
|
|
40
|
+
infinite;
|
|
41
|
+
animation-delay: calc(#{t.duration(slower)} * -25 * var(--fx-phase, 0) / var(--fx-speed, 1));
|
|
42
|
+
will-change: transform, opacity;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@media (forced-colors: active) {
|
|
46
|
+
.layer {
|
|
47
|
+
display: none;
|
|
48
|
+
}
|
|
49
|
+
}
|