@arraypress/effects-astro 0.4.0 → 0.5.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 +4 -2
- package/COMPONENTS.md +34 -0
- package/README.md +4 -2
- package/package.json +5 -2
- package/src/components/Scene.astro +89 -0
- package/src/index.d.ts +3 -1
- package/src/index.js +2 -1
- package/src/runtime/scene.d.ts +58 -0
- package/src/runtime/scene.js +80 -0
package/AGENTS.md
CHANGED
|
@@ -17,6 +17,7 @@ Pick by what is asked for:
|
|
|
17
17
|
| Smoke or ink on a light page | `<Ink dark colors={[…]}>` (dark dye alone barely shows) |
|
|
18
18
|
| Panels of colour fanning out of the dark, a bold launch or fintech hero with depth | `<ColorPanels>` (Opening by default; `preset="Glass"`, `"Default"` or `"Gradient"`; `palette` or `colors`, `quiet` behind copy) |
|
|
19
19
|
| A gradient, noise, halftone or liquid-metal look by name | `<PaperShader shader="…" preset="…">`; the demo has a hero for every shader, its presets in the tag |
|
|
20
|
+
| A glossy, glassy or studio-lit background a palette recolours: liquid marble, frosted or ribbed glass, glossy tiles, chrome, glitter, holographic foil, a neon ribbon, pastel spheres or a blob, glass rings or strands, ink in water, topographic lines | `<Scene name="…" palette="…">` (`marble`, `frosted`, `ribs`, `tiles`, `blob`, `chrome`, `glitter`, `foil`, `ribbon`, `spiral`, `swirl`, `arches`, `spheres`, `spheres2`, `strands`, `rings`, `ink-water`, `topo`); `params` reshapes it (`PARAMS[name]` in `@arraypress/shaders` lists each scene's); lazy by default, so a wall of cards can each have one |
|
|
20
21
|
| A themed hero background by name (mesh gradient, grainy gradient, silk, light bands, synthwave grid, neon waves), tinted from the site's accent | `<ShaderBackground effect="…">`; `static` when it sits behind body copy or is `fixed` |
|
|
21
22
|
| A launch, waitlist or coming-soon hero: light around centred copy | `<HaloRing>` |
|
|
22
23
|
| A dark, premium product or smart-home hero: frosted glass lit from within, copy beside it | `<RadiantGlass>` (`side="right"` mirrors it; `layout`, `shape`) |
|
|
@@ -59,6 +60,7 @@ Things that trip people up:
|
|
|
59
60
|
- **Clicks:** effects that react to clicks ignore links, buttons and form fields. Add `data-effect-ignore` to anything else whose clicks are its own.
|
|
60
61
|
- **`<SpotlightText fill="dodge">`** blends with what is behind it: no stacking context may sit between them (no `z-index` on a positioned ancestor, no `opacity`, `transform`, `filter` or `isolation`). Use `fill="solid"` where that cannot be arranged.
|
|
61
62
|
- **Styling a component's element** from the page needs `:global(.its-class)`, since the element belongs to the component.
|
|
63
|
+
- **`<Scene>` takes a palette by name** (`palette="Barbie"`); `@arraypress/shaders` itself takes the palette object (`PALETTES.Barbie`), never the name. Its `lazy` is the scene's own (a context held near the screen, given back four seconds after leaving on a fresh canvas), not `mount()`'s.
|
|
62
64
|
- **`<PaperShader>` needs WebGL 2** and shows `background` without it. Its picture shaders take `image` (same origin, or served with CORS).
|
|
63
65
|
- **Colour presets:** any effect that takes `colors` takes `palette="…"` (`PALETTES`: Lumetri, Forest, Cadmium, Peacock, Vermillion, Barbie, Uranium, Azure, Ruby, Ultramarine, Saffron, Crimson, Ember, Lagoon, Citrus, Glacier, Rosewater, Mono). Reach for a palette before inventing colours, and avoid violet unless the brand is violet.
|
|
64
66
|
- **Changing colours at runtime:** `setColors(el, colors, glint?)` from `@arraypress/effects-astro`.
|
|
@@ -66,7 +68,7 @@ Things that trip people up:
|
|
|
66
68
|
|
|
67
69
|
## Working on this package
|
|
68
70
|
|
|
69
|
-
Plain ES modules in `src/` with hand-written `.d.ts` files beside them: components in `src/components/`, the runtime in `src/runtime/` (one concern a file), stylesheets in `src/styles/`. No build step and
|
|
71
|
+
Plain ES modules in `src/` with hand-written `.d.ts` files beside them: components in `src/components/`, the runtime in `src/runtime/` (one concern a file), stylesheets in `src/styles/`. No build step, and one dependency: `@arraypress/shaders` (the owner's own scene shaders, for `<Scene>`). `demo/` is an Astro site (not published): one gallery page of every effect as a working hero, a file of heroes per group in `demo/src/heroes/` (listed in `demo/src/tour/groups.ts`), shown one at a time and filtered by type (`src/scripts/viewer.js`; only the hero on show has its effect stamped in). Each hero's Customise panel is generated from the components' prop docs by `npm run docs`, so every prop needs a doc comment with its default and, for numbers, a range written "0–1".
|
|
70
72
|
|
|
71
73
|
| | |
|
|
72
74
|
|---|---|
|
|
@@ -85,7 +87,7 @@ The shaders and canvases cannot run under Node. After changing an effect, check
|
|
|
85
87
|
- **No repetition.** Anything two effects need goes in its own runtime module and is imported.
|
|
86
88
|
- **JSDoc on every function**, nested ones included: a short description, `@param` for each parameter, `@returns`, `@throws` where it applies. Every prop has a doc comment with its default (COMPONENTS.md is built from them). Keep the `.d.ts` files in step. Plain, concise voice: what a thing does and why.
|
|
87
89
|
- **Colour in Oklab** (`mixOklab`, `paletteAt`, `GLSL_PALETTE`), never mixed in RGB.
|
|
88
|
-
- **No telemetry, no network, no dependencies.** Effects ship inside sites for other people's visitors.
|
|
90
|
+
- **No telemetry, no network, no third-party dependencies.** Effects ship inside sites for other people's visitors. The one dependency is `@arraypress/shaders`, the owner's own; a scene's shader belongs there, and `<Scene>` only wraps it.
|
|
89
91
|
- **`src/vendor/paper/` is Paper's code, unchanged but for dropped source-map comments** (Apache-2.0). Update it by re-vendoring a release and regenerating `presets.js`; never edit it by hand.
|
|
90
92
|
- **Credit what is adapted**, in the module's header, the README's Credits and LICENSE, and only from MIT-compatible sources. shaders.com's open-source components (MIT) may be adapted; its presets and sections are licensed content and may not be copied, even modified.
|
|
91
93
|
- **Every change goes in CHANGELOG.md** (Keep a Changelog, SemVer). Commit and push only with the owner's go-ahead; the owner publishes to npm.
|
package/COMPONENTS.md
CHANGED
|
@@ -1013,6 +1013,40 @@ Reveal: content that rises, slides, zooms or un-blurs into place as it scrolls i
|
|
|
1013
1013
|
| `distance` | `number` | How far it travels, px. Default 24. |
|
|
1014
1014
|
| `class` | `string` | Classes for the element. |
|
|
1015
1015
|
|
|
1016
|
+
## `<Scene>`
|
|
1017
|
+
|
|
1018
|
+
Scene: one of the scenes from `@arraypress/shaders` as a background: liquid marble, frosted glass, ribbed light, glossy tiles, a pastel blob, chrome liquid, a glitter galaxy, holographic foil, a neon ribbon, spiral trails, a glass swirl, arches, pastel spheres, glass strands, glass rings, ink in water and topographic lines. Any palette recolours it, and its own settings (`params`) reshape it. See src/runtime/scene.js.
|
|
1019
|
+
|
|
1020
|
+
```astro
|
|
1021
|
+
<Scene name="marble" palette="Barbie" params={{ scale: 1.2 }} />
|
|
1022
|
+
<Scene name="ink-water" colors={['#0d1461', '#990f61', '#050514']} speed={0.6} />
|
|
1023
|
+
<Scene name="foil" fixed opacity={0.5} />
|
|
1024
|
+
```
|
|
1025
|
+
|
|
1026
|
+
| Prop | Type | |
|
|
1027
|
+
|---|---|---|
|
|
1028
|
+
| `name` (required) | `SceneName` | The scene: marble, frosted, ribs, tiles, blob, chrome, glitter, foil, ribbon, spiral, swirl, arches, spheres, spheres2, strands, rings, ink-water or topo. Required. |
|
|
1029
|
+
| `palette` | `PaletteName` | A named palette (PALETTES): its three colours and its glint. Default the scene's own colours. |
|
|
1030
|
+
| `colors` | `string[]` | One to three colours (hex), over the palette's. Default the palette's, or the scene's own. |
|
|
1031
|
+
| `glint` | `string` | The highlight colour (hex), over the palette's. Default the palette's, or the scene's own. |
|
|
1032
|
+
| `params` | `Record<string, number>` | The scene's own settings by key (`PARAMS[name]` in `@arraypress/shaders`; marble takes `scale`, `sharpness`, `warp` and `relief`). Default the scene's. |
|
|
1033
|
+
| `speed` | `number` | How fast it moves, 0–3 (0 still). Default 1. |
|
|
1034
|
+
| `hue` | `number` | Hue turned round the colour wheel, -180–180 degrees. Default 0. |
|
|
1035
|
+
| `sat` | `number` | Saturation, 0–2 (0 grey). Default 1. |
|
|
1036
|
+
| `exposure` | `number` | Brightness, 0.3–2. Default 1. |
|
|
1037
|
+
| `tint` | `string` | A colour to lean toward (hex), keeping the light and dark, by `tintAmount`. Default `#d4ff3a`. |
|
|
1038
|
+
| `tintAmount` | `number` | How far toward `tint`, 0–1. Default 0. |
|
|
1039
|
+
| `zoom` | `number` | Zoom, 0.5–2.5. Default 1. |
|
|
1040
|
+
| `angle` | `number` | Rotation, -180–180 degrees. Default 0. |
|
|
1041
|
+
| `grain` | `number` | Film grain, 0–3. Default 1. |
|
|
1042
|
+
| `fps` | `number` | Frames a second, 1–60. Default 30. |
|
|
1043
|
+
| `scale` | `number` | Share of CSS pixels drawn at, 0.1–1 (lowered while frames come late). Default the scene's own, 0.45–0.85. |
|
|
1044
|
+
| `lazy` | `boolean \| string` | Hold a WebGL context only while near the screen (true, or how near as a CSS length), giving it back four seconds after leaving; false holds one from the start. Default true. |
|
|
1045
|
+
| `fixed` | `boolean` | Fill the viewport, behind the page (position: fixed, z-index -1). Default false. |
|
|
1046
|
+
| `background` | `string` | What shows before it draws, and without WebGL 2 (a CSS background). Default the scene's colours as soft blooms over its ground. |
|
|
1047
|
+
| `opacity` | `number` | Its opacity, 0–1, for backdrops under copy. Default 1. |
|
|
1048
|
+
| `class` | `string` | Classes for the element. |
|
|
1049
|
+
|
|
1016
1050
|
## `<ScrambleText>`
|
|
1017
1051
|
|
|
1018
1052
|
ScrambleText: text that resolves out of random characters, left to right, when it scrolls into view, and again on hover if asked. For a label, a stat, a short headline. Screen readers and search get the real text; the scramble is drawn over it. See src/runtime/text-motion.js.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @arraypress/effects-astro
|
|
2
2
|
|
|
3
|
-
Effects for Astro sites, as components: shader backgrounds, a real fluid, logos made of glass, particles or cubes, text that glitches, scrambles or types itself, glass panels and marquees: what makes a page look alive. Small UI pieces (countdowns, sliders, drop zones, tooltips) live in [`@arraypress/ui-astro`](https://github.com/LumiThemes/ui-astro); counters and drag-to-scroll in [`@arraypress/behaviors-astro`](https://github.com/arraypress/behaviors-astro). Plain WebGL, Canvas and CSS with no dependencies, and with the fallbacks a theme needs built in.
|
|
3
|
+
Effects for Astro sites, as components: shader backgrounds, a real fluid, logos made of glass, particles or cubes, text that glitches, scrambles or types itself, glass panels and marquees: what makes a page look alive. Small UI pieces (countdowns, sliders, drop zones, tooltips) live in [`@arraypress/ui-astro`](https://github.com/LumiThemes/ui-astro); counters and drag-to-scroll in [`@arraypress/behaviors-astro`](https://github.com/arraypress/behaviors-astro). Plain WebGL, Canvas and CSS with no third-party dependencies (`<Scene>`'s shaders come from the owner's own [`@arraypress/shaders`](https://github.com/arraypress/shaders)), and with the fallbacks a theme needs built in.
|
|
4
4
|
|
|
5
5
|
```astro
|
|
6
6
|
---
|
|
@@ -55,6 +55,7 @@ Astro 6 or 7. The runtime underneath (`@arraypress/effects-astro`) works without
|
|
|
55
55
|
| `<IsoCity>` | A tiny isometric low-poly city with traffic keeping its lanes and the brand's taxis; click a street and the nearest one drives round to pick you up; `night` lights the windows and headlights | WebGL |
|
|
56
56
|
| `<ColorPanels>` | Translucent panels of colour swinging open out of the dark and sweeping past like turning pages: Paper's color-panels shader with plain props (`density`, `skewLeft`, `length`, `edges`, `blur`, `blend`…), its Opening preset by default (`preset` for the others) | WebGL 2 |
|
|
57
57
|
| `<Galaxy>` | Deep space in a palette's colours: soft nebula clouds with dust lanes, shimmering glitter and twinkling stars, drifting slowly | Canvas |
|
|
58
|
+
| `<Scene>` | One of twenty studio-lit scenes from [`@arraypress/shaders`](https://github.com/arraypress/shaders) by `name`: liquid marble, frosted or ribbed glass, glossy tiles, chrome, glitter, holographic foil, a neon ribbon, pastel spheres, glass rings, ink in water, oil bubbles, a melting hologram, topographic lines and more; any `palette` recolours it, `params` reshapes it, lazy by default | WebGL 2 |
|
|
58
59
|
| `<WorldRoutes>` | A dotted world map with comets of light arcing between cities: a home hub, per-route colours and Oklab gradients, labels, regions, great-circle arcs, hover | Canvas |
|
|
59
60
|
| `<GlowField>` | A light to lay over a background: a bloom, a swaying stage spotlight, a horizon or a wash, breathing gently | CSS |
|
|
60
61
|
| `<Grain>` | Flickering film grain, to dither smooth gradients and make a picture read as film | CSS |
|
|
@@ -142,7 +143,7 @@ Every effect that takes `colors` also takes `palette`, one of eighteen named set
|
|
|
142
143
|
|
|
143
144
|
## Changing colours
|
|
144
145
|
|
|
145
|
-
The WebGL effects (`<LightField>`, `<InkField>`, `<Ink>`, `<AsciiRain>`, `<FlutedGlass>`, `<GlassGlyph>`, `<LiquidMetal>`, `<ParticleLogo>`, `<NeonSwarm>`, `<NebulaLogo>`, `<PointCloud>`, `<VoxelLogo>`, `<SignalLines>`, `<WaveBars>`, `<DotSurface>`, `<HaloRing>`, `<SilkRibbon>`, `<RadiantGlass>`, `<Topography>`, `<Caustics>`, `<RainOnGlass>`, `<MosaicReveal>`, `<Ocean>`, `<IsoCity>`) and `<Galaxy>` read a `data-colors` attribute; `setColors` writes it. A light or an ink field eases to the new palette in about a second; ink, rain and the rest take it from their next frame. Set before an effect starts, it is its colours from the first frame.
|
|
146
|
+
The WebGL effects (`<LightField>`, `<InkField>`, `<Ink>`, `<AsciiRain>`, `<FlutedGlass>`, `<GlassGlyph>`, `<LiquidMetal>`, `<ParticleLogo>`, `<NeonSwarm>`, `<NebulaLogo>`, `<PointCloud>`, `<VoxelLogo>`, `<SignalLines>`, `<WaveBars>`, `<DotSurface>`, `<HaloRing>`, `<SilkRibbon>`, `<RadiantGlass>`, `<Topography>`, `<Caustics>`, `<RainOnGlass>`, `<MosaicReveal>`, `<Ocean>`, `<IsoCity>`, `<Scene>`) and `<Galaxy>` read a `data-colors` attribute; `setColors` writes it. A light or an ink field eases to the new palette in about a second; ink, rain and the rest take it from their next frame. Set before an effect starts, it is its colours from the first frame.
|
|
146
147
|
|
|
147
148
|
```js
|
|
148
149
|
import { setColors } from '@arraypress/effects-astro';
|
|
@@ -157,6 +158,7 @@ The package's main entry is the runtime the components are built on, for use wit
|
|
|
157
158
|
| Export | |
|
|
158
159
|
|---|---|
|
|
159
160
|
| `paperShader(el, config)`, `paperSettings`, `paperUniforms` | Paper's shaders, and the settings and uniforms for a shader and preset |
|
|
161
|
+
| `scene(el, config)`, `sceneSettings` | `<Scene>`: an `@arraypress/shaders` scene on an element's canvas, and its props as that package's options |
|
|
160
162
|
| `paperLook(el, config)`, `paperLookSettings`, `PAPER_LOOKS` | The Paper looks (`<ColorPanels>`…): a look's plain props as a Paper shader's settings, and the looks by name |
|
|
161
163
|
| `createStage(el, options)` | A full-canvas WebGL effect: context, program, sizing, the loop, reduced motion, clean-up |
|
|
162
164
|
| `createCanvas2D(el, options)`, `grainPattern`, `grainOnto` | The same for Canvas 2D effects, and grain laid onto strokes only |
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arraypress/effects-astro",
|
|
3
|
-
"version": "0.
|
|
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 dependencies, with the fallbacks a theme needs (reduced motion, no WebGL, touch screens, off-screen pausing).",
|
|
3
|
+
"version": "0.5.0",
|
|
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",
|
|
7
7
|
"types": "src/index.d.ts",
|
|
@@ -43,6 +43,9 @@
|
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=20"
|
|
45
45
|
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@arraypress/shaders": "^0.1.0"
|
|
48
|
+
},
|
|
46
49
|
"peerDependencies": {
|
|
47
50
|
"astro": "^6 || ^7"
|
|
48
51
|
},
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Scene: one of the scenes from `@arraypress/shaders` as a
|
|
4
|
+
* background: liquid marble, frosted glass, ribbed light, glossy tiles, a
|
|
5
|
+
* pastel blob, chrome liquid, a glitter galaxy, holographic foil, a neon
|
|
6
|
+
* ribbon, spiral trails, a glass swirl, arches, pastel spheres, glass
|
|
7
|
+
* strands, glass rings, ink in water and topographic lines. Any palette
|
|
8
|
+
* recolours it, and its own settings (`params`) reshape it. See
|
|
9
|
+
* src/runtime/scene.js.
|
|
10
|
+
*
|
|
11
|
+
* Fills its nearest positioned ancestor, or the viewport with `fixed`.
|
|
12
|
+
* Lazy by default: it holds a WebGL context only while near the screen
|
|
13
|
+
* and gives it back four seconds after it leaves, so a page can carry
|
|
14
|
+
* many. It draws at 30 frames a second, at a share of the screen's
|
|
15
|
+
* resolution that drops on slow devices, pauses off screen, and draws one
|
|
16
|
+
* still frame with reduced motion. Without WebGL 2, its colours show as
|
|
17
|
+
* soft blooms over its ground. `setColors` recolours it live.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* <Scene name="marble" palette="Barbie" params={{ scale: 1.2 }} />
|
|
21
|
+
* <Scene name="ink-water" colors={['#0d1461', '#990f61', '#050514']} speed={0.6} />
|
|
22
|
+
* <Scene name="foil" fixed opacity={0.5} />
|
|
23
|
+
*/
|
|
24
|
+
import EffectCanvas from './EffectCanvas.astro';
|
|
25
|
+
import { SCENES, fallback, validateOptions } from '@arraypress/shaders';
|
|
26
|
+
import { sceneSettings } from '../runtime/scene.js';
|
|
27
|
+
import type { SceneName } from '../runtime/scene.js';
|
|
28
|
+
import type { PaletteName } from '../runtime/palettes.js';
|
|
29
|
+
|
|
30
|
+
interface Props {
|
|
31
|
+
/** The scene: marble, frosted, ribs, tiles, blob, chrome, glitter, foil, ribbon, spiral, swirl, arches, spheres, spheres2, strands, rings, ink-water or topo. Required. */
|
|
32
|
+
name: SceneName;
|
|
33
|
+
/** A named palette (PALETTES): its three colours and its glint. Default the scene's own colours. */
|
|
34
|
+
palette?: PaletteName;
|
|
35
|
+
/** One to three colours (hex), over the palette's. Default the palette's, or the scene's own. */
|
|
36
|
+
colors?: string[];
|
|
37
|
+
/** The highlight colour (hex), over the palette's. Default the palette's, or the scene's own. */
|
|
38
|
+
glint?: string;
|
|
39
|
+
/** The scene's own settings by key (`PARAMS[name]` in `@arraypress/shaders`; marble takes `scale`, `sharpness`, `warp` and `relief`). Default the scene's. */
|
|
40
|
+
params?: Record<string, number>;
|
|
41
|
+
/** How fast it moves, 0–3 (0 still). Default 1. */
|
|
42
|
+
speed?: number;
|
|
43
|
+
/** Hue turned round the colour wheel, -180–180 degrees. Default 0. */
|
|
44
|
+
hue?: number;
|
|
45
|
+
/** Saturation, 0–2 (0 grey). Default 1. */
|
|
46
|
+
sat?: number;
|
|
47
|
+
/** Brightness, 0.3–2. Default 1. */
|
|
48
|
+
exposure?: number;
|
|
49
|
+
/** A colour to lean toward (hex), keeping the light and dark, by `tintAmount`. Default `#d4ff3a`. */
|
|
50
|
+
tint?: string;
|
|
51
|
+
/** How far toward `tint`, 0–1. Default 0. */
|
|
52
|
+
tintAmount?: number;
|
|
53
|
+
/** Zoom, 0.5–2.5. Default 1. */
|
|
54
|
+
zoom?: number;
|
|
55
|
+
/** Rotation, -180–180 degrees. Default 0. */
|
|
56
|
+
angle?: number;
|
|
57
|
+
/** Film grain, 0–3. Default 1. */
|
|
58
|
+
grain?: number;
|
|
59
|
+
/** Frames a second, 1–60. Default 30. */
|
|
60
|
+
fps?: number;
|
|
61
|
+
/** Share of CSS pixels drawn at, 0.1–1 (lowered while frames come late). Default the scene's own, 0.45–0.85. */
|
|
62
|
+
scale?: number;
|
|
63
|
+
/** Hold a WebGL context only while near the screen (true, or how near as a CSS length), giving it back four seconds after leaving; false holds one from the start. Default true. */
|
|
64
|
+
lazy?: boolean | string;
|
|
65
|
+
/** Fill the viewport, behind the page (position: fixed, z-index -1). Default false. */
|
|
66
|
+
fixed?: boolean;
|
|
67
|
+
/** What shows before it draws, and without WebGL 2 (a CSS background). Default the scene's colours as soft blooms over its ground. */
|
|
68
|
+
background?: string;
|
|
69
|
+
/** Its opacity, 0–1, for backdrops under copy. Default 1. */
|
|
70
|
+
opacity?: number;
|
|
71
|
+
class?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { fixed = false, background, opacity = 1, lazy = true, class: className, ...config } = Astro.props;
|
|
75
|
+
if (!Object.hasOwn(SCENES, config.name)) throw new Error(`<Scene>: no scene "${config.name}" (there are ${Object.keys(SCENES).join(', ')})`);
|
|
76
|
+
// `lazy` reaches the scene as `near`: under its own name, mount() would start and stop the scene itself, without the scene's own grace period and fresh canvas.
|
|
77
|
+
Object.assign(config, { near: lazy });
|
|
78
|
+
const { options } = sceneSettings(config);
|
|
79
|
+
validateOptions(config.name, options); // a bad param or colour fails the build, not the page
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
<EffectCanvas name="scene" config={config} fixed={fixed} behind background={background ?? fallback(config.name, options)} opacity={opacity} class={className} />
|
|
83
|
+
|
|
84
|
+
<script>
|
|
85
|
+
import { mount } from '../runtime/mount.js';
|
|
86
|
+
import { scene } from '../runtime/scene.js';
|
|
87
|
+
|
|
88
|
+
mount('[data-effect-scene]', scene);
|
|
89
|
+
</script>
|
package/src/index.d.ts
CHANGED
|
@@ -101,6 +101,8 @@ export type { LiveGradientOptions } from './runtime/live-gradient.js';
|
|
|
101
101
|
export type { PaperConfig, PaperShaderName } from './runtime/paper.js';
|
|
102
102
|
export { paperLook, paperLookSettings } from './runtime/paper-look.js';
|
|
103
103
|
export type { PaperLookConfig, PaperLookOptions } from './runtime/paper-look.js';
|
|
104
|
+
export { scene, sceneSettings } from './runtime/scene.js';
|
|
105
|
+
export type { SceneConfig, SceneName } from './runtime/scene.js';
|
|
104
106
|
export { PAPER_LOOKS } from './runtime/paper-looks.js';
|
|
105
107
|
export type { PaperLook, PaperLookName } from './runtime/paper-looks.js';
|
|
106
108
|
export { createStage } from './runtime/stage.js';
|
|
@@ -142,7 +144,7 @@ export { mount, readConfig, watchColors } from './runtime/mount.js';
|
|
|
142
144
|
/**
|
|
143
145
|
* Changes the colours of a WebGL effect (`<LightField>`, `<Ink>`, `<AsciiRain>`,
|
|
144
146
|
* `<FlutedGlass>`, `<GlassGlyph>`, `<LiquidMetal>`, `<ParticleLogo>`, `<PointCloud>`,
|
|
145
|
-
* `<VoxelLogo>`, `<HaloRing>`, `<SilkRibbon>`, `<RadiantGlass>`, `<Topography>`, `<Caustics>`, `<RainOnGlass>`, `<MosaicReveal>`, `<Ocean>`, `<IsoCity>`),
|
|
147
|
+
* `<VoxelLogo>`, `<HaloRing>`, `<SilkRibbon>`, `<RadiantGlass>`, `<Topography>`, `<Caustics>`, `<RainOnGlass>`, `<MosaicReveal>`, `<Ocean>`, `<IsoCity>`, `<Scene>`),
|
|
146
148
|
* in the order its `colors` takes. A light eases to them; the others take them next frame.
|
|
147
149
|
*/
|
|
148
150
|
export function setColors(el: Element | null, colors: string[], glint?: string): void;
|
package/src/index.js
CHANGED
|
@@ -69,6 +69,7 @@ export { paperShader, paperSettings, paperUniforms } from './runtime/paper.js';
|
|
|
69
69
|
export { whenNear, lazyMargin } from './runtime/near.js';
|
|
70
70
|
export { liveGradient } from './runtime/live-gradient.js';
|
|
71
71
|
export { paperLook, paperLookSettings } from './runtime/paper-look.js';
|
|
72
|
+
export { scene, sceneSettings } from './runtime/scene.js';
|
|
72
73
|
export { PAPER_LOOKS } from './runtime/paper-looks.js';
|
|
73
74
|
export { createStage } from './runtime/stage.js';
|
|
74
75
|
export { createMesh, STRIDE } from './runtime/mesh.js';
|
|
@@ -95,7 +96,7 @@ export { mount, readConfig, watchColors } from './runtime/mount.js';
|
|
|
95
96
|
/**
|
|
96
97
|
* Changes the colours of a WebGL effect (`<LightField>`, `<Ink>`,
|
|
97
98
|
* `<AsciiRain>`, `<FlutedGlass>`, `<GlassGlyph>`, `<LiquidMetal>`,
|
|
98
|
-
* `<ParticleLogo>`, `<PointCloud>`, `<VoxelLogo>`, `<HaloRing>`, `<SilkRibbon>`, `<RadiantGlass>`, `<Topography>`, `<Caustics>`, `<RainOnGlass>`, `<MosaicReveal>`, `<Ocean>`, `<IsoCity>`): a light
|
|
99
|
+
* `<ParticleLogo>`, `<PointCloud>`, `<VoxelLogo>`, `<HaloRing>`, `<SilkRibbon>`, `<RadiantGlass>`, `<Topography>`, `<Caustics>`, `<RainOnGlass>`, `<MosaicReveal>`, `<Ocean>`, `<IsoCity>`, `<Scene>`): a light
|
|
99
100
|
* eases to them in about a second, the others take them from their next frame.
|
|
100
101
|
*
|
|
101
102
|
* @param {Element | null} el The effect's element (`[data-effect-light-field]` and so on).
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SceneOptions } from '@arraypress/shaders';
|
|
2
|
+
import type { PaletteName } from './palettes.js';
|
|
3
|
+
|
|
4
|
+
/** A scene's name: one of `@arraypress/shaders`' scenes. */
|
|
5
|
+
export type SceneName =
|
|
6
|
+
| 'marble'
|
|
7
|
+
| 'frosted'
|
|
8
|
+
| 'ribs'
|
|
9
|
+
| 'tiles'
|
|
10
|
+
| 'blob'
|
|
11
|
+
| 'chrome'
|
|
12
|
+
| 'glitter'
|
|
13
|
+
| 'foil'
|
|
14
|
+
| 'ribbon'
|
|
15
|
+
| 'spiral'
|
|
16
|
+
| 'swirl'
|
|
17
|
+
| 'arches'
|
|
18
|
+
| 'spheres'
|
|
19
|
+
| 'spheres2'
|
|
20
|
+
| 'strands'
|
|
21
|
+
| 'rings'
|
|
22
|
+
| 'ink-water'
|
|
23
|
+
| 'topo'
|
|
24
|
+
| 'oil-bubbles'
|
|
25
|
+
| 'melting-hologram';
|
|
26
|
+
|
|
27
|
+
/** A `<Scene>`'s settings, as its element carries them. */
|
|
28
|
+
export interface SceneConfig {
|
|
29
|
+
/** The scene. */
|
|
30
|
+
name: SceneName;
|
|
31
|
+
/** A named palette (PALETTES). */
|
|
32
|
+
palette?: PaletteName | (string & {});
|
|
33
|
+
/** One to three colours (hex), over the palette's. */
|
|
34
|
+
colors?: string[];
|
|
35
|
+
/** The highlight colour (hex). */
|
|
36
|
+
glint?: string;
|
|
37
|
+
/** The scene's own settings by key. */
|
|
38
|
+
params?: Record<string, number>;
|
|
39
|
+
speed?: number;
|
|
40
|
+
hue?: number;
|
|
41
|
+
sat?: number;
|
|
42
|
+
exposure?: number;
|
|
43
|
+
tint?: string;
|
|
44
|
+
tintAmount?: number;
|
|
45
|
+
zoom?: number;
|
|
46
|
+
angle?: number;
|
|
47
|
+
grain?: number;
|
|
48
|
+
fps?: number;
|
|
49
|
+
scale?: number;
|
|
50
|
+
/** The component's `lazy`: hold a context only while near the screen (true, or a CSS length). Default true. */
|
|
51
|
+
near?: boolean | string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A `<Scene>`'s settings as `@arraypress/shaders`' options: the palette found by name, `near` as `lazy`. */
|
|
55
|
+
export function sceneSettings(config: SceneConfig): { name: SceneName; options: SceneOptions };
|
|
56
|
+
|
|
57
|
+
/** Starts a scene on an element's canvas; returns a function that stops it. */
|
|
58
|
+
export function scene(el: HTMLElement, config: SceneConfig): () => void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/scene
|
|
3
|
+
*
|
|
4
|
+
* A scene from `@arraypress/shaders` on an effect's canvas: liquid marble,
|
|
5
|
+
* frosted glass, glossy tiles, chrome, glitter, holographic foil, ink in
|
|
6
|
+
* water and the rest, recoloured by a palette and shaped by the scene's own
|
|
7
|
+
* settings. `<Scene>` is the component.
|
|
8
|
+
*
|
|
9
|
+
* The scene is loaded by name, each in its own chunk, so a page ships only
|
|
10
|
+
* the shaders it shows. Its laziness is the package's own, not mount()'s:
|
|
11
|
+
* it holds a WebGL context only while near the screen and gives it back
|
|
12
|
+
* four seconds after it leaves, on a fresh canvas, and it caps its frame
|
|
13
|
+
* rate, adapts its resolution, pauses off screen and draws one still frame
|
|
14
|
+
* with reduced motion. `setColors` recolours it live.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { scene as run, LOOK } from '@arraypress/shaders/core';
|
|
18
|
+
import { load } from '@arraypress/shaders/load';
|
|
19
|
+
import { findPalette } from './palettes.js';
|
|
20
|
+
import { watchColors } from './mount.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {import('./scene.js').SceneConfig} SceneConfig
|
|
24
|
+
* @typedef {import('@arraypress/shaders').SceneOptions} SceneOptions
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** The settings a `<Scene>` hands to its scene as they are: the look, the colours, the params, the frame rate and resolution. */
|
|
28
|
+
const OPTIONS = /** @type {(keyof SceneConfig)[]} */ ([...Object.keys(LOOK), 'colors', 'glint', 'params', 'fps', 'scale']);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A `<Scene>`'s settings as the package's options: its palette found by
|
|
32
|
+
* name (an unknown one is warned about and the scene's own colours kept),
|
|
33
|
+
* `near` as `lazy`, and the rest as they are.
|
|
34
|
+
*
|
|
35
|
+
* @param {SceneConfig} config The component's settings.
|
|
36
|
+
* @returns {{ name: SceneConfig['name'], options: SceneOptions }} The scene and its options.
|
|
37
|
+
*/
|
|
38
|
+
export function sceneSettings(config) {
|
|
39
|
+
const { name, palette, near = true } = config;
|
|
40
|
+
// Only the scene's own options: anything else a component is handed (Astro's `slot`, say) is not the scene's.
|
|
41
|
+
/** @type {Record<string, unknown>} */
|
|
42
|
+
const options = Object.fromEntries(OPTIONS.filter((k) => config[k] !== undefined).map((k) => [k, config[k]]));
|
|
43
|
+
if (palette) {
|
|
44
|
+
const found = findPalette(palette);
|
|
45
|
+
if (found) options.palette = found;
|
|
46
|
+
else console.warn('[effects-astro]', `No palette "${palette}"`);
|
|
47
|
+
}
|
|
48
|
+
options.lazy = near;
|
|
49
|
+
return { name, options: /** @type {SceneOptions} */ (options) };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Starts a scene on an element's canvas.
|
|
54
|
+
*
|
|
55
|
+
* @param {HTMLElement} el The element (its first canvas is drawn on).
|
|
56
|
+
* @param {SceneConfig} config The scene and its settings.
|
|
57
|
+
* @returns {() => void} Stops it.
|
|
58
|
+
*/
|
|
59
|
+
export function scene(el, config) {
|
|
60
|
+
const canvas = el.querySelector('canvas');
|
|
61
|
+
if (!canvas) return () => {};
|
|
62
|
+
const { name, options } = sceneSettings(config);
|
|
63
|
+
let stopped = false;
|
|
64
|
+
/** @type {import('@arraypress/shaders').SceneHandle | null} */
|
|
65
|
+
let handle = null;
|
|
66
|
+
let unwatch = () => {};
|
|
67
|
+
load(name)
|
|
68
|
+
.then((def) => {
|
|
69
|
+
if (stopped) return;
|
|
70
|
+
const h = run(canvas, def, options);
|
|
71
|
+
handle = h;
|
|
72
|
+
unwatch = watchColors(el, (next) => h.set({ colors: next.colors.slice(0, 3), ...(next.glint ? { glint: next.glint } : {}) }));
|
|
73
|
+
})
|
|
74
|
+
.catch((error) => console.warn('[effects-astro]', error?.message ?? error));
|
|
75
|
+
return () => {
|
|
76
|
+
stopped = true;
|
|
77
|
+
unwatch();
|
|
78
|
+
handle?.stop();
|
|
79
|
+
};
|
|
80
|
+
}
|