@arraypress/effects-astro 0.11.0 → 0.13.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 +2 -2
- package/COMPONENTS.md +175 -10
- package/package.json +1 -1
- package/src/components/DitherGradient.astro +41 -0
- package/src/components/FlutedGlow.astro +39 -0
- package/src/components/GlassDrop.astro +40 -0
- package/src/components/GlassStack.astro +41 -0
- package/src/components/PixelFountain.astro +20 -7
- package/src/components/PixelGlitchText.astro +4 -3
- package/src/components/PixelHorizon.astro +40 -0
- package/src/components/TruchetField.astro +40 -0
- package/src/index.d.ts +19 -1
- package/src/index.js +11 -1
- package/src/runtime/bayer.d.ts +5 -0
- package/src/runtime/bayer.js +48 -0
- package/src/runtime/dither-field.js +7 -33
- package/src/runtime/dither-gradient.d.ts +91 -0
- package/src/runtime/dither-gradient.js +410 -0
- package/src/runtime/fluted-glow.d.ts +79 -0
- package/src/runtime/fluted-glow.js +312 -0
- package/src/runtime/glass-drop-plan.d.ts +59 -0
- package/src/runtime/glass-drop-plan.js +133 -0
- package/src/runtime/glass-drop.d.ts +61 -0
- package/src/runtime/glass-drop.js +331 -0
- package/src/runtime/glass-optics.d.ts +5 -0
- package/src/runtime/glass-optics.js +49 -0
- package/src/runtime/glass-stack.d.ts +65 -0
- package/src/runtime/glass-stack.js +315 -0
- package/src/runtime/pixel-fountain.d.ts +29 -13
- package/src/runtime/pixel-fountain.js +183 -42
- package/src/runtime/pixel-glitch.d.ts +2 -0
- package/src/runtime/pixel-glitch.js +8 -2
- package/src/runtime/pixel-horizon.d.ts +60 -0
- package/src/runtime/pixel-horizon.js +274 -0
- package/src/runtime/slab-runs.d.ts +18 -0
- package/src/runtime/slab-runs.js +112 -0
- package/src/runtime/truchet-field.d.ts +60 -0
- package/src/runtime/truchet-field.js +272 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/fluted-glow
|
|
3
|
+
*
|
|
4
|
+
* A soft light seen through reeded glass: behind upright flutes, a glowing
|
|
5
|
+
* ring, a round orb or a light rising from the bottom, ramped from the
|
|
6
|
+
* ground through a deep and a mid colour to a hot one (in Oklab), breathing
|
|
7
|
+
* and drifting slowly. Each flute shows a narrow slice of what is behind it
|
|
8
|
+
* stretched across its width, so the light breaks into vertical strips,
|
|
9
|
+
* with a faint shadow and highlight at each flute's seams and a fine grain
|
|
10
|
+
* over all of it. Unlike `<FlutedGlass>` (slanted flutes over pointer-led
|
|
11
|
+
* blooms) it is made to sit behind copy: no pointer, the light framing a
|
|
12
|
+
* quiet middle, the copy sharp in front of the glass.
|
|
13
|
+
*
|
|
14
|
+
* Every clock is in seconds and nothing is kept between frames; the light
|
|
15
|
+
* and the flutes' slicing are here in plain JavaScript too (`glowAt`,
|
|
16
|
+
* `fluteSample`).
|
|
17
|
+
*
|
|
18
|
+
* Opaque: it paints its `background` (default near-black) everywhere.
|
|
19
|
+
* Without WebGL nothing is drawn; with prefers-reduced-motion it is one
|
|
20
|
+
* still frame.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createStage } from './stage.js';
|
|
24
|
+
import { GLSL_HASH33 } from './hash-noise.js';
|
|
25
|
+
import { GLSL_OKLAB, parseColor, toOklab, toHex, mixOklab } from './color.js';
|
|
26
|
+
import { GLSL_QUIET, quietUniform } from './glsl.js';
|
|
27
|
+
import { watchColors } from './mount.js';
|
|
28
|
+
import { pickColors } from './palettes.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {'ring' | 'orb' | 'rise'} GlowShape
|
|
32
|
+
*
|
|
33
|
+
* @typedef {object} FlutedGlowConfig
|
|
34
|
+
* @property {string[]} [colors] Three CSS hex colours, the light from its edge in: deep, mid and hot. Default a violet ring's.
|
|
35
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
|
|
36
|
+
* @property {string} [background='#08070c'] The ground, painted everywhere.
|
|
37
|
+
* @property {GlowShape} [shape='ring'] The light behind the glass.
|
|
38
|
+
* @property {number} [flutes=14] Flutes across the width.
|
|
39
|
+
* @property {number} [squeeze=0.35] How much of what is behind a flute it shows across its width (lower stretches more).
|
|
40
|
+
* @property {number} [size=0.3] The ring's radius or the orb's, a share of the height; how high the rise reaches.
|
|
41
|
+
* @property {number} [thickness=0.09] The ring's soft width, a share of the height.
|
|
42
|
+
* @property {number} [glow=1] How bright the light is.
|
|
43
|
+
* @property {number} [breathe=0.06] How far the light swells and shrinks, a share of its size.
|
|
44
|
+
* @property {number} [drift=0.04] How far its centre wanders, a share of the height.
|
|
45
|
+
* @property {number} [rate=0.05] How fast it breathes and wanders, cycles a second.
|
|
46
|
+
* @property {number} [seams=0.35] How strong the flutes' shadows and highlights are; 0 for none.
|
|
47
|
+
* @property {number} [grain=0.05] Fine grain over all of it; 0 for none.
|
|
48
|
+
* @property {[number, number]} [centre=[0.5, 0.5]] The ring's or orb's centre, 0–1 from the top left.
|
|
49
|
+
* @property {number} [seed=0] Where its wandering starts, so two cards differ.
|
|
50
|
+
* @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
|
|
51
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
52
|
+
* An area dimmed: centre (0–1 from the top left), radius (share of the height), depth (0–1).
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
/** The shapes, in the order the shader numbers them. */
|
|
56
|
+
export const GLOW_SHAPES = /** @type {const} */ (['ring', 'orb', 'rise']);
|
|
57
|
+
|
|
58
|
+
/** The settings' defaults, shared by the shader and the JS twin. */
|
|
59
|
+
export const GLOW_DEFAULTS = {
|
|
60
|
+
background: '#08070c',
|
|
61
|
+
shape: /** @type {GlowShape} */ ('ring'),
|
|
62
|
+
flutes: 14,
|
|
63
|
+
squeeze: 0.35,
|
|
64
|
+
size: 0.3,
|
|
65
|
+
thickness: 0.09,
|
|
66
|
+
glow: 1,
|
|
67
|
+
breathe: 0.06,
|
|
68
|
+
drift: 0.04,
|
|
69
|
+
rate: 0.05,
|
|
70
|
+
seams: 0.35,
|
|
71
|
+
grain: 0.05,
|
|
72
|
+
centre: /** @type {[number, number]} */ ([0.5, 0.5]),
|
|
73
|
+
seed: 0,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** The default colours: a violet ring's. */
|
|
77
|
+
const VIOLET = ['#3a1a7a', '#8f7cff', '#f2eeff'];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* GLSL: `float glowAt(vec2 p, float t)`, how bright the light behind the
|
|
81
|
+
* glass is at a point (heights from the top left), 0–1 and beyond at its
|
|
82
|
+
* hottest. `glowAt` below is the same in JavaScript.
|
|
83
|
+
*/
|
|
84
|
+
const GLSL_GLOW = `
|
|
85
|
+
uniform float uShape;
|
|
86
|
+
uniform float uSizeL;
|
|
87
|
+
uniform float uThick;
|
|
88
|
+
uniform float uGlow;
|
|
89
|
+
uniform float uBreathe;
|
|
90
|
+
uniform float uDrift;
|
|
91
|
+
uniform float uRate;
|
|
92
|
+
uniform vec2 uCentre;
|
|
93
|
+
uniform float uSeed;
|
|
94
|
+
float glowAt(vec2 p, float t) {
|
|
95
|
+
float ph = 6.2831853 * t * uRate;
|
|
96
|
+
float grow = 1.0 + uBreathe * sin(ph + uSeed);
|
|
97
|
+
vec2 c = uCentre + uDrift * vec2(sin(ph * 0.7 + uSeed * 1.3), cos(ph * 0.53 + uSeed * 0.7));
|
|
98
|
+
if (uShape < 0.5) {
|
|
99
|
+
float d = (length(p - c) - uSizeL * grow) / max(uThick, 0.01);
|
|
100
|
+
return uGlow * exp(-d * d);
|
|
101
|
+
}
|
|
102
|
+
if (uShape < 1.5) {
|
|
103
|
+
float d = length(p - c) / max(uSizeL * grow, 0.01);
|
|
104
|
+
return uGlow * exp(-d * d * 1.6);
|
|
105
|
+
}
|
|
106
|
+
float top = uCentre.y * 2.0 - uSizeL * grow;
|
|
107
|
+
float sway = 0.06 * sin(p.x * 5.0 + ph + uSeed);
|
|
108
|
+
return uGlow * smoothstep(top - 0.25, top + 0.55, p.y + sway);
|
|
109
|
+
}
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
const FRAG = `
|
|
113
|
+
precision highp float;
|
|
114
|
+
uniform vec2 uRes;
|
|
115
|
+
uniform float uTime;
|
|
116
|
+
uniform float uFlutes;
|
|
117
|
+
uniform float uSqueeze;
|
|
118
|
+
uniform float uSeams;
|
|
119
|
+
uniform float uGrain;
|
|
120
|
+
uniform vec3 uBg;
|
|
121
|
+
uniform vec3 uRamp[3];
|
|
122
|
+
${GLSL_OKLAB}
|
|
123
|
+
${GLSL_QUIET}
|
|
124
|
+
${GLSL_HASH33}
|
|
125
|
+
${GLSL_GLOW}
|
|
126
|
+
|
|
127
|
+
void main() {
|
|
128
|
+
vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
|
|
129
|
+
float aspect = uRes.x / uRes.y;
|
|
130
|
+
// Heights from the top left; the flutes measured in widths.
|
|
131
|
+
vec2 p = px / uRes.y;
|
|
132
|
+
float fw = 1.0 / uFlutes;
|
|
133
|
+
float fx = px.x / uRes.x;
|
|
134
|
+
float idx = floor(fx / fw);
|
|
135
|
+
float local = (fx / fw - idx) * 2.0 - 1.0;
|
|
136
|
+
float sx = ((idx + 0.5) * fw + local * 0.5 * fw * uSqueeze) * aspect;
|
|
137
|
+
float i = glowAt(vec2(sx, p.y), uTime);
|
|
138
|
+
|
|
139
|
+
vec2 q = vec2(p.x - 0.5 * aspect, 0.5 - p.y);
|
|
140
|
+
i *= mix(1.0 - uQuiet.w, 1.0, quietOpen(q));
|
|
141
|
+
|
|
142
|
+
// The ramp: ground, deep, mid, hot, by how bright.
|
|
143
|
+
vec3 lab = mix(uBg, uRamp[0], smoothstep(0.0, 0.35, i));
|
|
144
|
+
lab = mix(lab, uRamp[1], smoothstep(0.3, 0.75, i));
|
|
145
|
+
lab = mix(lab, uRamp[2], smoothstep(0.8, 1.2, i));
|
|
146
|
+
vec3 col = fromOklab(lab);
|
|
147
|
+
|
|
148
|
+
// Each flute a little darker toward its seams, a highlight on its left seam, a shadow on its right.
|
|
149
|
+
float seam = 1.0 - uSeams * 0.35 * (1.0 - cos(local * 1.5707963));
|
|
150
|
+
seam += uSeams * 0.25 * smoothstep(-0.86, -1.0, local) * (0.3 + i);
|
|
151
|
+
seam -= uSeams * 0.3 * smoothstep(0.86, 1.0, local);
|
|
152
|
+
col *= seam;
|
|
153
|
+
col += (hash33(vec3(px, floor(uTime * 24.0))).x - 0.5) * uGrain;
|
|
154
|
+
gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
|
155
|
+
}
|
|
156
|
+
`;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The settings, filled with the defaults, with the shape's number.
|
|
160
|
+
*
|
|
161
|
+
* @param {FlutedGlowConfig} config The settings.
|
|
162
|
+
* @returns {typeof GLOW_DEFAULTS & { shapeIndex: number, speed: number }} The settings.
|
|
163
|
+
*/
|
|
164
|
+
function settings(config) {
|
|
165
|
+
const o = { ...GLOW_DEFAULTS, speed: 1, ...config };
|
|
166
|
+
return { ...o, shapeIndex: Math.max(0, GLOW_SHAPES.indexOf(o.shape)) };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* How bright the light behind the glass is at a point (the shader's
|
|
171
|
+
* `glowAt`), before the flutes and the quiet area.
|
|
172
|
+
*
|
|
173
|
+
* @param {FlutedGlowConfig} config The settings.
|
|
174
|
+
* @param {number} x Heights from the left.
|
|
175
|
+
* @param {number} y Heights from the top.
|
|
176
|
+
* @param {number} aspect Width over height (the centre is a share of each).
|
|
177
|
+
* @param {number} [time=0] Seconds (already at its speed).
|
|
178
|
+
* @returns {number} 0 and up; about `glow` at its hottest.
|
|
179
|
+
*/
|
|
180
|
+
export function glowAt(config, x, y, aspect, time = 0) {
|
|
181
|
+
const o = settings(config);
|
|
182
|
+
const ph = 2 * Math.PI * time * o.rate;
|
|
183
|
+
const grow = 1 + o.breathe * Math.sin(ph + o.seed);
|
|
184
|
+
const cx = o.centre[0] * aspect + o.drift * Math.sin(ph * 0.7 + o.seed * 1.3);
|
|
185
|
+
const cy = o.centre[1] + o.drift * Math.cos(ph * 0.53 + o.seed * 0.7);
|
|
186
|
+
if (o.shapeIndex === 0) {
|
|
187
|
+
const d = (Math.hypot(x - cx, y - cy) - o.size * grow) / Math.max(o.thickness, 0.01);
|
|
188
|
+
return o.glow * Math.exp(-d * d);
|
|
189
|
+
}
|
|
190
|
+
if (o.shapeIndex === 1) {
|
|
191
|
+
const d = Math.hypot(x - cx, y - cy) / Math.max(o.size * grow, 0.01);
|
|
192
|
+
return o.glow * Math.exp(-d * d * 1.6);
|
|
193
|
+
}
|
|
194
|
+
const top = o.centre[1] * 2 - o.size * grow;
|
|
195
|
+
const sway = 0.06 * Math.sin(x * 5 + ph + o.seed);
|
|
196
|
+
const t = Math.min(1, Math.max(0, (y + sway - (top - 0.25)) / 0.8));
|
|
197
|
+
return o.glow * t * t * (3 - 2 * t);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Where behind the glass a point looks (the shader's slicing): each flute
|
|
202
|
+
* shows a slice `squeeze` of its own width, stretched across it.
|
|
203
|
+
*
|
|
204
|
+
* @param {number} fx The point, a share of the width (0–1).
|
|
205
|
+
* @param {number} flutes Flutes across the width.
|
|
206
|
+
* @param {number} squeeze The share of a flute's width it shows.
|
|
207
|
+
* @returns {{ x: number, local: number }} The share of the width it shows, and where in its flute it is (−1 to 1).
|
|
208
|
+
*/
|
|
209
|
+
export function fluteSample(fx, flutes, squeeze) {
|
|
210
|
+
const fw = 1 / flutes;
|
|
211
|
+
const idx = Math.floor(fx / fw);
|
|
212
|
+
const local = (fx / fw - idx) * 2 - 1;
|
|
213
|
+
return { x: (idx + 0.5) * fw + local * 0.5 * fw * squeeze, local };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The light's three colours as the shader takes them (fewer repeat).
|
|
218
|
+
*
|
|
219
|
+
* @param {FlutedGlowConfig} config The settings.
|
|
220
|
+
* @returns {[string, string, string]} Deep, mid and hot.
|
|
221
|
+
*/
|
|
222
|
+
export function glowColors(config) {
|
|
223
|
+
const c = pickColors(config, VIOLET);
|
|
224
|
+
return [c[0], c[Math.min(1, c.length - 1)], c[c.length - 1]];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* A CSS `background` that stands in for the light before the shader starts
|
|
229
|
+
* and without WebGL: the ring, orb or rise as a CSS gradient, under upright
|
|
230
|
+
* stripes for the flutes.
|
|
231
|
+
*
|
|
232
|
+
* @param {FlutedGlowConfig} config The settings (`colors`/`palette`, `background`, `shape`, `flutes`, `size`, `thickness`, `centre`).
|
|
233
|
+
* @param {number} [aspect=0.8] The element's width over its height, to size the ring and orb as the shader does (a 4:5 card by default).
|
|
234
|
+
* @returns {string} The CSS background.
|
|
235
|
+
*/
|
|
236
|
+
export function glowStill(config, aspect = 0.8) {
|
|
237
|
+
const o = settings(config);
|
|
238
|
+
// A circle's stops run along the ray to the farthest corner; the shader measures in heights.
|
|
239
|
+
const [cx, cy] = o.centre;
|
|
240
|
+
const ray = Math.hypot(Math.max(cx, 1 - cx) * aspect, Math.max(cy, 1 - cy));
|
|
241
|
+
const [deep, mid, hot] = glowColors(config).map((c) => toHex(parseColor(c)));
|
|
242
|
+
const ground = o.background;
|
|
243
|
+
const fade = toHex(mixOklab(parseColor(ground), parseColor(deep), 0.5));
|
|
244
|
+
const at = `${Math.round(o.centre[0] * 100)}% ${Math.round(o.centre[1] * 100)}%`;
|
|
245
|
+
const r = Math.round((o.size / ray) * 100);
|
|
246
|
+
const w = Math.round((o.thickness / ray) * 100);
|
|
247
|
+
const flute = `repeating-linear-gradient(90deg, rgba(255,255,255,0.06) 0, transparent ${(100 / o.flutes) * 0.12}%, transparent ${(100 / o.flutes) * 0.8}%, rgba(0,0,0,0.25) ${100 / o.flutes}%)`;
|
|
248
|
+
const light =
|
|
249
|
+
o.shapeIndex === 0
|
|
250
|
+
? `radial-gradient(circle at ${at}, ${ground} ${Math.max(0, r - 2 * w)}%, ${deep} ${r - w}%, ${hot} ${r}%, ${mid} ${r + w * 0.6}%, ${fade} ${r + w * 1.4}%, ${ground} ${r + w * 2.4}%)`
|
|
251
|
+
: o.shapeIndex === 1
|
|
252
|
+
? `radial-gradient(circle at ${at}, ${hot} 0%, ${mid} ${Math.round(r * 0.5)}%, ${deep} ${r}%, ${ground} ${Math.round(r * 1.6)}%)`
|
|
253
|
+
: `linear-gradient(to top, ${hot} 0%, ${mid} ${Math.round(o.size * 60)}%, ${deep} ${Math.round(o.size * 100)}%, ${ground} ${Math.round(o.size * 160)}%)`;
|
|
254
|
+
return [flute, light, ground].join(', ');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Starts the light on an element: a canvas inside it (its first), at the
|
|
259
|
+
* stage's capped pixel ratio.
|
|
260
|
+
*
|
|
261
|
+
* @param {HTMLElement} el The element.
|
|
262
|
+
* @param {FlutedGlowConfig} config The settings.
|
|
263
|
+
* @returns {() => void} Stops it.
|
|
264
|
+
*/
|
|
265
|
+
export function flutedGlow(el, config) {
|
|
266
|
+
const o = settings(config);
|
|
267
|
+
const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
|
|
268
|
+
let ramp = glowColors(config).map(lab);
|
|
269
|
+
const ground = lab(o.background);
|
|
270
|
+
const start = performance.now();
|
|
271
|
+
|
|
272
|
+
const { stage, stop } = createStage(el, {
|
|
273
|
+
fragment: FRAG,
|
|
274
|
+
alpha: false,
|
|
275
|
+
still: o.speed <= 0,
|
|
276
|
+
setup({ gl, uniform }) {
|
|
277
|
+
gl.uniform1f(uniform('uFlutes'), Math.max(1, o.flutes));
|
|
278
|
+
gl.uniform1f(uniform('uSqueeze'), o.squeeze);
|
|
279
|
+
gl.uniform1f(uniform('uSeams'), o.seams);
|
|
280
|
+
gl.uniform1f(uniform('uGrain'), o.grain);
|
|
281
|
+
gl.uniform3f(uniform('uBg'), ground[0], ground[1], ground[2]);
|
|
282
|
+
gl.uniform1f(uniform('uShape'), o.shapeIndex);
|
|
283
|
+
gl.uniform1f(uniform('uSizeL'), o.size);
|
|
284
|
+
gl.uniform1f(uniform('uThick'), o.thickness);
|
|
285
|
+
gl.uniform1f(uniform('uGlow'), o.glow);
|
|
286
|
+
gl.uniform1f(uniform('uBreathe'), o.breathe);
|
|
287
|
+
gl.uniform1f(uniform('uDrift'), o.drift);
|
|
288
|
+
gl.uniform1f(uniform('uRate'), o.rate);
|
|
289
|
+
gl.uniform1f(uniform('uSeed'), o.seed);
|
|
290
|
+
},
|
|
291
|
+
resize({ gl, uniform }) {
|
|
292
|
+
const w = Math.max(1, el.clientWidth);
|
|
293
|
+
const h = Math.max(1, el.clientHeight);
|
|
294
|
+
gl.uniform2f(uniform('uCentre'), o.centre[0] * (w / h), o.centre[1]);
|
|
295
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, w / h));
|
|
296
|
+
},
|
|
297
|
+
uniforms({ gl, uniform }, now) {
|
|
298
|
+
gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, o.speed));
|
|
299
|
+
gl.uniform3fv(uniform('uRamp[0]'), new Float32Array(ramp.flat()));
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
if (!stage) return stop;
|
|
303
|
+
|
|
304
|
+
const unwatch = watchColors(el, (next) => {
|
|
305
|
+
ramp = glowColors({ colors: next.colors }).map(lab);
|
|
306
|
+
if (stage.still) stage.redraw();
|
|
307
|
+
});
|
|
308
|
+
return () => {
|
|
309
|
+
stop();
|
|
310
|
+
unwatch();
|
|
311
|
+
};
|
|
312
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** The sequence's parts, in seconds at speed 1: the drop forming, spreading into its shape, and melting away. */
|
|
2
|
+
export declare const DROP_PHASES: Readonly<{ form: number; spread: number; melt: number }>;
|
|
3
|
+
|
|
4
|
+
/** The drop's radius when it lands, a share of the tile's edge. */
|
|
5
|
+
export declare const DROP_SIZE: number;
|
|
6
|
+
|
|
7
|
+
export interface DropLayout {
|
|
8
|
+
/** The glass's middle, each 0–1 from the top left. */
|
|
9
|
+
centre: { x: number; y: number };
|
|
10
|
+
/** The tile's edge, a share of the element's height. */
|
|
11
|
+
tile: number;
|
|
12
|
+
/** The mark's box under the glass, a share of the height. */
|
|
13
|
+
mark: number;
|
|
14
|
+
/** The mark's box at rest, a share of the height. */
|
|
15
|
+
rest: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface DropState {
|
|
19
|
+
/** How far the drop has formed, 0–1. */
|
|
20
|
+
drop: number;
|
|
21
|
+
/** From the drop (0) to the tile or mark (1); it overshoots a little as the glass settles. */
|
|
22
|
+
morph: number;
|
|
23
|
+
/** How much the glass's edge still ripples, 0–1. */
|
|
24
|
+
wobble: number;
|
|
25
|
+
/** Seconds since the drop landed, or −1 before. */
|
|
26
|
+
ripple: number;
|
|
27
|
+
/** How far the mark has risen under the glass (or, as the logo's own glass melts, come through flat), 0–1. */
|
|
28
|
+
markIn: number;
|
|
29
|
+
/** How much glass there is, 1 whole to 0 melted. */
|
|
30
|
+
glass: number;
|
|
31
|
+
/** The mark's size, 1 under the glass to `restScale` at rest. */
|
|
32
|
+
scale: number;
|
|
33
|
+
/** Whether the sequence has finished and nothing more moves. */
|
|
34
|
+
done: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The settings the plan reads. */
|
|
38
|
+
export interface DropPlanConfig {
|
|
39
|
+
area?: { x: number; y: number; width: number; height: number };
|
|
40
|
+
size?: number;
|
|
41
|
+
markScale?: number;
|
|
42
|
+
restScale?: number;
|
|
43
|
+
hold?: number;
|
|
44
|
+
loopDelay?: number;
|
|
45
|
+
loop?: boolean;
|
|
46
|
+
rest?: 'flat' | 'glass';
|
|
47
|
+
delay?: number;
|
|
48
|
+
speed?: number;
|
|
49
|
+
form?: 'tile' | 'mark';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Where the glass and the mark sit in an element (`aspect`, its width over its height): a square tile `size` of the area's shorter side, the mark's box `markScale` of it, `restScale` times that at rest. */
|
|
53
|
+
export declare function dropLayout(config: DropPlanConfig, aspect: number): DropLayout;
|
|
54
|
+
|
|
55
|
+
/** The cycle's length in seconds at speed 1: the sequence, then the rest before it plays again (none without `loop`). */
|
|
56
|
+
export declare function dropCycle(config: DropPlanConfig): number;
|
|
57
|
+
|
|
58
|
+
/** What the sequence is doing `seconds` after it started (`Infinity`: the final state). */
|
|
59
|
+
export declare function dropAt(seconds: number, config: DropPlanConfig): DropState;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/glass-drop-plan
|
|
3
|
+
*
|
|
4
|
+
* GlassDrop's plan, apart from its shader so it can be tested and so a page
|
|
5
|
+
* can lay a still of the resting mark exactly where the effect leaves it:
|
|
6
|
+
* where the glass and the mark sit (`dropLayout`), and what the sequence
|
|
7
|
+
* is doing at a moment (`dropAt`): a drop of liquid glass forms, spreads
|
|
8
|
+
* into a tile (or into the mark's own shape) with the mark rising under it,
|
|
9
|
+
* holds, then melts away and leaves the mark flat on the ground. On a loop,
|
|
10
|
+
* each round starts from the resting mark, which fades as the drop forms, so
|
|
11
|
+
* a page can show the resting mark as the effect's still and the effect
|
|
12
|
+
* takes over from it without a jump.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** The sequence's parts, in seconds at speed 1: the drop forming, spreading into its shape, and melting away. */
|
|
16
|
+
export const DROP_PHASES = Object.freeze({ form: 0.7, spread: 1.3, melt: 1.1 });
|
|
17
|
+
|
|
18
|
+
/** The drop's radius when it lands, a share of the tile's edge. */
|
|
19
|
+
export const DROP_SIZE = 0.17;
|
|
20
|
+
|
|
21
|
+
/** The settings the plan reads, with their defaults. */
|
|
22
|
+
const DEFAULTS = { size: 0.7, markScale: 0.62, restScale: 1, hold: 2.4, loopDelay: 1.6, loop: false, rest: 'flat', delay: 0, speed: 1 };
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {object} DropLayout
|
|
26
|
+
* @property {{ x: number, y: number }} centre The glass's middle, each 0–1 from the top left.
|
|
27
|
+
* @property {number} tile The tile's edge, a share of the element's height.
|
|
28
|
+
* @property {number} mark The mark's box under the glass, a share of the height.
|
|
29
|
+
* @property {number} rest The mark's box at rest, a share of the height.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Where the glass and the mark sit in an element: the tile is a square
|
|
34
|
+
* `size` of the area's shorter side, centred in it; the mark's box is
|
|
35
|
+
* `markScale` of the tile under the glass, and `restScale` times that once
|
|
36
|
+
* the glass has gone. A square box holds the whole image (an SVG logo's
|
|
37
|
+
* viewBox), so a still drawn in the same box lines up exactly.
|
|
38
|
+
*
|
|
39
|
+
* @param {{ area?: { x: number, y: number, width: number, height: number }, size?: number, markScale?: number, restScale?: number }} config The settings.
|
|
40
|
+
* @param {number} aspect The element's width over its height.
|
|
41
|
+
* @returns {DropLayout} The layout.
|
|
42
|
+
*/
|
|
43
|
+
export function dropLayout(config, aspect) {
|
|
44
|
+
const area = config.area ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
45
|
+
const size = config.size ?? DEFAULTS.size;
|
|
46
|
+
const markScale = config.markScale ?? DEFAULTS.markScale;
|
|
47
|
+
const restScale = config.restScale ?? DEFAULTS.restScale;
|
|
48
|
+
const tile = Math.min(area.width * aspect, area.height) * size;
|
|
49
|
+
return {
|
|
50
|
+
centre: { x: area.x + area.width / 2, y: area.y + area.height / 2 },
|
|
51
|
+
tile,
|
|
52
|
+
mark: tile * markScale,
|
|
53
|
+
rest: tile * markScale * restScale,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @typedef {object} DropState
|
|
59
|
+
* @property {number} drop How far the drop has formed, 0–1 (its radius is `drop × DROP_SIZE` of the tile).
|
|
60
|
+
* @property {number} morph From the drop (0) to the tile or mark (1); it overshoots a little as the glass settles.
|
|
61
|
+
* @property {number} wobble How much the glass's edge still ripples, 0–1.
|
|
62
|
+
* @property {number} ripple Seconds since the drop landed (the rings on the ground), or −1 before.
|
|
63
|
+
* @property {number} markIn How far the mark has risen under the glass, 0–1.
|
|
64
|
+
* @property {number} glass How much glass there is, 1 whole to 0 melted.
|
|
65
|
+
* @property {number} scale The mark's size, 1 under the glass to `restScale` at rest.
|
|
66
|
+
* @property {boolean} done Whether the sequence has finished and nothing more moves.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/** Hermite ease, 0–1. */
|
|
70
|
+
const ease = (x) => {
|
|
71
|
+
const t = Math.min(1, Math.max(0, x));
|
|
72
|
+
return t * t * (3 - 2 * t);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The cycle's length in seconds at speed 1: the sequence, then the rest
|
|
77
|
+
* before it plays again (none when it does not loop).
|
|
78
|
+
*
|
|
79
|
+
* @param {{ hold?: number, loopDelay?: number, loop?: boolean, rest?: 'flat' | 'glass' }} config The settings.
|
|
80
|
+
* @returns {number} Seconds.
|
|
81
|
+
*/
|
|
82
|
+
export function dropCycle(config) {
|
|
83
|
+
const hold = config.hold ?? DEFAULTS.hold;
|
|
84
|
+
if ((config.rest ?? DEFAULTS.rest) === 'glass') return DROP_PHASES.form + DROP_PHASES.spread;
|
|
85
|
+
const end = DROP_PHASES.form + DROP_PHASES.spread + hold + DROP_PHASES.melt;
|
|
86
|
+
return (config.loop ?? DEFAULTS.loop) ? end + (config.loopDelay ?? DEFAULTS.loopDelay) : end;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* What the sequence is doing at a moment. `Infinity` gives the final state:
|
|
91
|
+
* the mark flat at its rest size (or, with `rest: 'glass'`, the glass held).
|
|
92
|
+
*
|
|
93
|
+
* @param {number} seconds Seconds since it started, before `delay` and `speed` are applied.
|
|
94
|
+
* @param {{ hold?: number, loopDelay?: number, loop?: boolean, rest?: 'flat' | 'glass', delay?: number, speed?: number, restScale?: number, form?: 'tile' | 'mark' }} config The settings.
|
|
95
|
+
* @returns {DropState} The state.
|
|
96
|
+
*/
|
|
97
|
+
export function dropAt(seconds, config) {
|
|
98
|
+
const hold = config.hold ?? DEFAULTS.hold;
|
|
99
|
+
const restScale = config.restScale ?? DEFAULTS.restScale;
|
|
100
|
+
const keep = (config.rest ?? DEFAULTS.rest) === 'glass';
|
|
101
|
+
const looping = !keep && (config.loop ?? DEFAULTS.loop);
|
|
102
|
+
const speed = Math.max(0.05, config.speed ?? DEFAULTS.speed);
|
|
103
|
+
let t = (seconds - (config.delay ?? DEFAULTS.delay)) * speed;
|
|
104
|
+
const cycle = dropCycle(config);
|
|
105
|
+
if (!Number.isFinite(t)) t = keep ? cycle : cycle + hold + DROP_PHASES.melt + 1;
|
|
106
|
+
else if (looping && t > 0) t %= cycle;
|
|
107
|
+
const { form, spread, melt } = DROP_PHASES;
|
|
108
|
+
const glassOnly = (config.form ?? 'tile') === 'mark';
|
|
109
|
+
|
|
110
|
+
const drop = t <= 0 ? 0 : t >= form ? 1 : 1 - (1 - t / form) ** 3;
|
|
111
|
+
const u = (t - form) / spread;
|
|
112
|
+
// A damped spring from the drop to the shape: past it a little, then settling, landing on exactly 1.
|
|
113
|
+
const spring = u <= 0 ? 0 : 1 - Math.exp(-5 * u) * Math.cos(9 * u);
|
|
114
|
+
const morph = u <= 0 ? 0 : u >= 1 ? 1 : spring + (1 - spring) * ease((u - 0.75) / 0.25);
|
|
115
|
+
const wobble = t <= 0 ? 0 : u <= 0 ? Math.min(1, t / form) * 0.6 : u >= 1 ? 0 : Math.exp(-4 * u);
|
|
116
|
+
const ripple = t >= form ? t - form : -1;
|
|
117
|
+
const markIn = glassOnly ? 0 : ease((u - 0.2) / 0.6);
|
|
118
|
+
const v = keep ? 0 : ease((t - form - spread - hold) / melt);
|
|
119
|
+
const end = form + spread + (keep ? 0 : hold + melt);
|
|
120
|
+
// On a loop every round starts from where the last one rested: the flat mark, fading as the new drop forms.
|
|
121
|
+
if (looping && t < form)
|
|
122
|
+
return { drop, morph, wobble, ripple, markIn: 1 - drop, glass: 1, scale: restScale, done: false };
|
|
123
|
+
return {
|
|
124
|
+
drop,
|
|
125
|
+
morph,
|
|
126
|
+
wobble,
|
|
127
|
+
ripple,
|
|
128
|
+
markIn: glassOnly ? v : markIn,
|
|
129
|
+
glass: 1 - v,
|
|
130
|
+
scale: 1 + (restScale - 1) * v,
|
|
131
|
+
done: !looping && t >= end + 2,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export interface GlassDropConfig {
|
|
2
|
+
/** An image (an SVG logo) to make the glass of. */
|
|
3
|
+
src?: string;
|
|
4
|
+
/** A character or word instead of an image. */
|
|
5
|
+
text?: string;
|
|
6
|
+
/** Its CSS font, without the size. Default: the page's, bold. */
|
|
7
|
+
font?: string;
|
|
8
|
+
/** Where the glass sits, each 0–1 from the top left. Default all of the element. */
|
|
9
|
+
area?: { x: number; y: number; width: number; height: number };
|
|
10
|
+
/** What the drop becomes: `tile`, a glass tile with the logo under it, or `mark`, the logo's own shape in glass. Default `tile`. */
|
|
11
|
+
form?: 'tile' | 'mark';
|
|
12
|
+
/** The tile's edge, a share of the area's shorter side, 0.2–1. Default 0.7. */
|
|
13
|
+
size?: number;
|
|
14
|
+
/** The logo's size under the glass, a share of the tile, 0.2–1. Default 0.62. */
|
|
15
|
+
markScale?: number;
|
|
16
|
+
/** The logo's size once the glass has melted, times its size under the glass, 0.5–2. Default 1. */
|
|
17
|
+
restScale?: number;
|
|
18
|
+
/** The tile's corner radius, a share of its edge, 0–0.5. Default 0.24. */
|
|
19
|
+
radius?: number;
|
|
20
|
+
/** The ground. Default `#000000`. */
|
|
21
|
+
background?: string;
|
|
22
|
+
/** The logo's colour. Default `#ffffff`. */
|
|
23
|
+
color?: string;
|
|
24
|
+
/** The tile's surface under the logo. Default `#0a1020`. */
|
|
25
|
+
plate?: string;
|
|
26
|
+
/** The studio light the glass mirrors. Default `#ffffff`. */
|
|
27
|
+
light?: string;
|
|
28
|
+
/** How hard the glass bends what is under it, 0–2. Default 1. */
|
|
29
|
+
depth?: number;
|
|
30
|
+
/** How far the colours part at the rim, 0–2; 0 for none. Default 1. */
|
|
31
|
+
dispersion?: number;
|
|
32
|
+
/** How wide the rounded rim is, a share of the tile (or the logo), 0.02–0.3. Default 0.1. */
|
|
33
|
+
bevel?: number;
|
|
34
|
+
/** The lip and the studio's reflections, 0–2. Default 1. */
|
|
35
|
+
highlights?: number;
|
|
36
|
+
/** Light focused at the glass's edge and onto the ground, 0–2. Default 1. */
|
|
37
|
+
caustics?: number;
|
|
38
|
+
/** A soft glow round the glass, 0–1. Default 0.5. */
|
|
39
|
+
bloom?: number;
|
|
40
|
+
/** The rings the drop sends across the ground, 0–2; 0 for none. Default 1. */
|
|
41
|
+
ripples?: number;
|
|
42
|
+
/** With `form: 'mark'`: how strongly the glass takes the logo's colour, 0–1. Default 0.88. */
|
|
43
|
+
tint?: number;
|
|
44
|
+
/** Where the light comes from, degrees, 0–360 (0 right, 90 top). Default 125. */
|
|
45
|
+
lightAngle?: number;
|
|
46
|
+
/** How fast the sequence plays, 0.25–3. Default 1. */
|
|
47
|
+
speed?: number;
|
|
48
|
+
/** Seconds to wait before it starts, 0–10. Default 0. */
|
|
49
|
+
delay?: number;
|
|
50
|
+
/** Seconds the glass holds before it melts, 0–10. Default 2.4. */
|
|
51
|
+
hold?: number;
|
|
52
|
+
/** Play it again after a rest; each round starts from the resting logo, which fades as the new drop forms. Default false. */
|
|
53
|
+
loop?: boolean;
|
|
54
|
+
/** With `loop`: seconds the logo rests before the next drop, 0–10. Default 1.6. */
|
|
55
|
+
loopDelay?: number;
|
|
56
|
+
/** How it ends: `flat`, the glass melted and the logo left on the ground, or `glass`, the glass held. Default `flat`. */
|
|
57
|
+
rest?: 'flat' | 'glass';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Starts the glass drop on an element (its first canvas). Returns a function that stops it. */
|
|
61
|
+
export declare function glassDrop(el: HTMLElement, config: GlassDropConfig): () => void;
|