@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,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/glass-stack
|
|
3
|
+
*
|
|
4
|
+
* A logo as a stack of tinted glass discs: the logo is cut into horizontal
|
|
5
|
+
* slices, and every stretch of logo in a slice becomes a flat disc of
|
|
6
|
+
* glass, seen a little from below so its underside and its front edge
|
|
7
|
+
* show, in front of a lit card. The discs are tinted along a gradient from
|
|
8
|
+
* the top slice to the bottom one; through them the card is bent at their
|
|
9
|
+
* rounded rims, darkened where the glass is thickest, and their edges
|
|
10
|
+
* mirror a small studio (./glass-optics.js). The gaps in the logo stay
|
|
11
|
+
* open. On first show the discs grow from the bottom up; after that the
|
|
12
|
+
* stack leans gently on its own and toward the pointer.
|
|
13
|
+
*
|
|
14
|
+
* Our own construction after the idea of a logo as stacked glass slabs
|
|
15
|
+
* (see docs/credits.md): a 2.5D stack drawn in one pass, the slices'
|
|
16
|
+
* runs read from a small texture (./slab-runs.js), nearer discs drawn over
|
|
17
|
+
* farther ones.
|
|
18
|
+
*
|
|
19
|
+
* Opaque: it paints its `background` everywhere. Without WebGL the canvas
|
|
20
|
+
* goes and the element's background shows. With reduced motion it is one
|
|
21
|
+
* still frame, every disc grown.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { createStage } from './stage.js';
|
|
25
|
+
import { drawShape } from './shape.js';
|
|
26
|
+
import { parseColor, mixOklab } from './color.js';
|
|
27
|
+
import { canHover } from './device.js';
|
|
28
|
+
import { pointerIn } from './pointer.js';
|
|
29
|
+
import { GLSL_STUDIO, lightVector } from './glass-optics.js';
|
|
30
|
+
import { sliceRuns, runsTexture, slabGrowth } from './slab-runs.js';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {import('./glass-stack.js').GlassStackConfig} GlassStackConfig
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
const FRAG = `
|
|
37
|
+
precision highp float;
|
|
38
|
+
uniform vec2 uRes;
|
|
39
|
+
uniform sampler2D uRuns;
|
|
40
|
+
uniform sampler2D uTints;
|
|
41
|
+
uniform vec2 uCentre;
|
|
42
|
+
uniform float uEdge;
|
|
43
|
+
uniform float uSlices;
|
|
44
|
+
uniform float uFace;
|
|
45
|
+
uniform float uWall;
|
|
46
|
+
uniform vec2 uTilt;
|
|
47
|
+
uniform vec3 uGround;
|
|
48
|
+
uniform vec3 uLight;
|
|
49
|
+
uniform float uCardSize;
|
|
50
|
+
uniform float uExposure;
|
|
51
|
+
uniform float uAmbient;
|
|
52
|
+
uniform float uIor;
|
|
53
|
+
uniform float uDensity;
|
|
54
|
+
uniform float uReflect;
|
|
55
|
+
uniform float uOpacity;
|
|
56
|
+
uniform vec2 uLightDir;
|
|
57
|
+
${GLSL_STUDIO}
|
|
58
|
+
|
|
59
|
+
// The lit card behind the stack, and the ground round it.
|
|
60
|
+
vec3 backdrop(vec2 p) {
|
|
61
|
+
vec2 hs = vec2(uEdge * 0.5 * uCardSize);
|
|
62
|
+
vec2 q = abs(p - uCentre) - hs + uEdge * 0.06;
|
|
63
|
+
float d = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - uEdge * 0.06;
|
|
64
|
+
float on = smoothstep(uEdge * 0.012, -uEdge * 0.012, d);
|
|
65
|
+
// Brighter toward its top, as a card lit from above is.
|
|
66
|
+
float fall = 0.94 + 0.06 * clamp((p.y - uCentre.y) / (uEdge * uCardSize) + 0.5, 0.0, 1.0);
|
|
67
|
+
vec3 card = uLight * uExposure * fall;
|
|
68
|
+
vec3 ground = uGround + uLight * uAmbient * exp(-max(d, 0.0) / (uEdge * 0.12));
|
|
69
|
+
return mix(ground, card, on);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A slice's run at x: where along it x is (−1 left edge … 1 right edge), and whether x is in one.
|
|
73
|
+
vec2 runAt(float x, float slice) {
|
|
74
|
+
float u = (x - (uCentre.x - uEdge * 0.5)) / uEdge;
|
|
75
|
+
if (u < 0.0 || u > 1.0) return vec2(0.0, 0.0);
|
|
76
|
+
vec4 t = texture2D(uRuns, vec2(u, (slice + 0.5) / uSlices));
|
|
77
|
+
float along = (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0;
|
|
78
|
+
return vec2(along * 2.0 - 1.0, step(0.5, t.b));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
void main() {
|
|
82
|
+
vec2 p = gl_FragCoord.xy / uRes.y;
|
|
83
|
+
float px = 1.0 / uRes.y;
|
|
84
|
+
vec3 col = backdrop(p);
|
|
85
|
+
float pitch = uEdge / uSlices;
|
|
86
|
+
float top = uCentre.y + uEdge * 0.5;
|
|
87
|
+
float here = floor((top - p.y) / pitch);
|
|
88
|
+
vec3 view = vec3(0.0, 0.0, -1.0);
|
|
89
|
+
|
|
90
|
+
// The slices that can reach this pixel, the farthest (highest) first, so nearer discs cover them.
|
|
91
|
+
for (int k = -2; k <= 2; k++) {
|
|
92
|
+
float slice = here + float(k);
|
|
93
|
+
if (slice < 0.0 || slice >= uSlices) continue;
|
|
94
|
+
vec4 tint = texture2D(uTints, vec2((slice + 0.5) / uSlices, 0.5));
|
|
95
|
+
float grow = tint.a * 1.25;
|
|
96
|
+
if (grow <= 0.001) continue;
|
|
97
|
+
float yc = top - (slice + 0.5) * pitch;
|
|
98
|
+
// The stack leans: each slice slides with its height, and the discs open or close a little.
|
|
99
|
+
float shift = uTilt.x * (yc - uCentre.y) * 0.45;
|
|
100
|
+
vec2 run = runAt(p.x - shift, slice);
|
|
101
|
+
if (run.y < 0.5) continue;
|
|
102
|
+
float q = run.x / grow;
|
|
103
|
+
if (abs(q) >= 1.0) continue;
|
|
104
|
+
float vr = pitch * uFace * (1.0 + uTilt.y * 0.3) * min(grow, 1.0);
|
|
105
|
+
float wall = pitch * uWall * min(grow, 1.0);
|
|
106
|
+
float halfH = vr * sqrt(1.0 - q * q);
|
|
107
|
+
float dy = p.y - yc;
|
|
108
|
+
|
|
109
|
+
vec3 c = tint.rgb;
|
|
110
|
+
if (abs(dy) <= halfH + px) {
|
|
111
|
+
// The underside: a flat disc whose rim rounds over, so the card bends at its edge.
|
|
112
|
+
float r = sqrt(q * q + (dy * dy) / (vr * vr));
|
|
113
|
+
float roll = pow(clamp(r, 0.0, 1.0), 4.0);
|
|
114
|
+
vec3 n = normalize(vec3(vec2(q, dy / vr) * roll * 1.6, 1.0));
|
|
115
|
+
vec2 bend = n.xy * (uIor - 1.0) * pitch * 1.4;
|
|
116
|
+
float path = 1.0 + 1.1 * roll;
|
|
117
|
+
vec3 through = backdrop(p - bend) * pow(c, vec3(uDensity * path));
|
|
118
|
+
float fres = schlick(n.z, 0.04);
|
|
119
|
+
vec3 mirrored = studio(reflect(view, n), uLightDir, uLight);
|
|
120
|
+
vec3 face = through * (1.0 - fres * 0.5) + mirrored * (fres * 1.4 + 0.05) * (0.4 + uReflect);
|
|
121
|
+
// A darker band just inside the rim gives the disc its edge; a bright hairline at the very lip.
|
|
122
|
+
face *= 1.0 - 0.16 * smoothstep(0.8, 0.96, r) * (1.0 - smoothstep(0.96, 1.0, r));
|
|
123
|
+
face += uLight * 0.35 * smoothstep(0.955, 0.995, r) * (0.5 + uReflect);
|
|
124
|
+
// Light carried through the disc gathers toward its far (lower) edge.
|
|
125
|
+
face += through * 0.22 * smoothstep(0.1, 0.85, -dy / vr) * (1.0 - q * q);
|
|
126
|
+
float cover = clamp((halfH - abs(dy)) / px + 0.5, 0.0, 1.0);
|
|
127
|
+
col = mix(col, face, cover * uOpacity);
|
|
128
|
+
} else if (dy > halfH && dy <= halfH + wall + px) {
|
|
129
|
+
// The front edge: glass seen through its thickness, deeper in colour, rounder toward the middle,
|
|
130
|
+
// with light caught along the line where it meets the top.
|
|
131
|
+
float h = (dy - halfH) / max(wall, 1e-5);
|
|
132
|
+
float shade = 0.5 + 0.5 * (1.0 - q * q);
|
|
133
|
+
vec3 edge = backdrop(p) * pow(c, vec3(uDensity * 2.6)) * shade;
|
|
134
|
+
edge += uLight * (0.45 * smoothstep(0.65, 1.0, h) + 0.12 * smoothstep(0.35, 0.0, h)) * (0.5 + uReflect);
|
|
135
|
+
float cover = clamp((halfH + wall - dy) / px + 0.5, 0.0, 1.0);
|
|
136
|
+
col = mix(col, edge, cover * min(1.0, uOpacity + 0.1));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
gl_FragColor = vec4(col, 1.0);
|
|
140
|
+
}
|
|
141
|
+
`;
|
|
142
|
+
|
|
143
|
+
/** The defaults: our "lagoon" glass, aqua to deep blue, on white. */
|
|
144
|
+
const DEFAULTS = Object.freeze({
|
|
145
|
+
colors: ['#bff3ff', '#5ad1f0', '#1f8fe0', '#2140c8'],
|
|
146
|
+
background: '#ffffff',
|
|
147
|
+
light: '#ffffff',
|
|
148
|
+
slabs: 22,
|
|
149
|
+
phase: 0.5,
|
|
150
|
+
band: 0.35,
|
|
151
|
+
depth: 0.55,
|
|
152
|
+
thickness: 0.35,
|
|
153
|
+
ior: 1.45,
|
|
154
|
+
reflectivity: 0.3,
|
|
155
|
+
density: 0.9,
|
|
156
|
+
opacity: 0.92,
|
|
157
|
+
exposure: 1.1,
|
|
158
|
+
ambient: 0.05,
|
|
159
|
+
cardSize: 1.15,
|
|
160
|
+
logoScale: 0.73,
|
|
161
|
+
lightAngle: 115,
|
|
162
|
+
tilt: 1,
|
|
163
|
+
sway: 0.5,
|
|
164
|
+
intro: true,
|
|
165
|
+
introDuration: 1.6,
|
|
166
|
+
delay: 0,
|
|
167
|
+
speed: 1,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
/** The finest the logo is sliced, in columns. */
|
|
171
|
+
const COLUMNS = 1024;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The tint of each slice: the colours as a gradient from the top slice to the bottom one, blended in Oklab.
|
|
175
|
+
*
|
|
176
|
+
* @param {string[]} colors Two to six colours, top first.
|
|
177
|
+
* @param {number} slices How many slices.
|
|
178
|
+
* @returns {Array<[number, number, number]>} RGB 0–1, a slice each.
|
|
179
|
+
*/
|
|
180
|
+
export function sliceTints(colors, slices) {
|
|
181
|
+
const rgb = colors.map(parseColor);
|
|
182
|
+
return Array.from({ length: slices }, (_, i) => {
|
|
183
|
+
const x = (slices > 1 ? i / (slices - 1) : 0) * (rgb.length - 1);
|
|
184
|
+
const a = Math.min(Math.floor(x), rgb.length - 1);
|
|
185
|
+
const b = Math.min(a + 1, rgb.length - 1);
|
|
186
|
+
return /** @type {[number, number, number]} */ (mixOklab(rgb[a], rgb[b], x - a));
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Where the stack sits: a square box `logoScale` of the area's shorter
|
|
192
|
+
* side, centred in it. The logo fills the box as an image fills a square
|
|
193
|
+
* (an SVG logo's viewBox), so a still drawn in the same box lines up.
|
|
194
|
+
*
|
|
195
|
+
* @param {{ area?: { x: number, y: number, width: number, height: number }, logoScale?: number }} config The settings.
|
|
196
|
+
* @param {number} aspect The element's width over its height.
|
|
197
|
+
* @returns {{ centre: { x: number, y: number }, edge: number }} Its middle (0–1 from the top left) and its edge, a share of the height.
|
|
198
|
+
*/
|
|
199
|
+
export function stackLayout(config, aspect) {
|
|
200
|
+
const area = config.area ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
201
|
+
return {
|
|
202
|
+
centre: { x: area.x + area.width / 2, y: area.y + area.height / 2 },
|
|
203
|
+
edge: Math.min(area.width * aspect, area.height) * (config.logoScale ?? DEFAULTS.logoScale),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Starts the glass stack on an element: an opaque canvas inside it, filling it.
|
|
209
|
+
*
|
|
210
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
211
|
+
* @param {GlassStackConfig} config The settings.
|
|
212
|
+
* @returns {() => void} Stops it.
|
|
213
|
+
*/
|
|
214
|
+
export function glassStack(el, config) {
|
|
215
|
+
const c = { ...DEFAULTS, ...config };
|
|
216
|
+
const slices = Math.max(4, Math.min(64, Math.round(c.slabs)));
|
|
217
|
+
const colors = c.colors?.length >= 2 ? c.colors : c.colors?.length === 1 ? [c.colors[0], c.colors[0]] : DEFAULTS.colors;
|
|
218
|
+
const tints = sliceTints(colors, slices);
|
|
219
|
+
const tintBytes = new Uint8Array(slices * 4);
|
|
220
|
+
tints.forEach((t, i) => tintBytes.set([t[0] * 255, t[1] * 255, t[2] * 255, 0], i * 4));
|
|
221
|
+
const start = performance.now();
|
|
222
|
+
const tilt = { x: 0, y: 0 };
|
|
223
|
+
let generation = 0;
|
|
224
|
+
let runsTex = /** @type {WebGLTexture | null} */ (null);
|
|
225
|
+
let tintTex = /** @type {WebGLTexture | null} */ (null);
|
|
226
|
+
/** The last growth written, so the tints are uploaded only while the discs grow. */
|
|
227
|
+
let grown = -1;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A nearest-filtered, edge-clamped texture on a unit.
|
|
231
|
+
*
|
|
232
|
+
* @param {WebGLRenderingContext | WebGL2RenderingContext} gl The context.
|
|
233
|
+
* @param {number} unit The texture unit.
|
|
234
|
+
* @returns {WebGLTexture | null} The texture.
|
|
235
|
+
*/
|
|
236
|
+
const nearest = (gl, unit) => {
|
|
237
|
+
const t = gl.createTexture();
|
|
238
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
239
|
+
gl.bindTexture(gl.TEXTURE_2D, t);
|
|
240
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
241
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
242
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
243
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
244
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(4));
|
|
245
|
+
return t;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const { stage, stop } = createStage(el, {
|
|
249
|
+
fragment: FRAG,
|
|
250
|
+
alpha: false,
|
|
251
|
+
setup({ gl, uniform }) {
|
|
252
|
+
runsTex = nearest(gl, 0);
|
|
253
|
+
tintTex = nearest(gl, 1);
|
|
254
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, slices, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, tintBytes);
|
|
255
|
+
gl.uniform1i(uniform('uRuns'), 0);
|
|
256
|
+
gl.uniform1i(uniform('uTints'), 1);
|
|
257
|
+
gl.uniform1f(uniform('uSlices'), slices);
|
|
258
|
+
// The camera's height sets how open the discs are and how much of their front edge shows.
|
|
259
|
+
gl.uniform1f(uniform('uFace'), Math.max(0.05, Math.min(1.4, c.depth)));
|
|
260
|
+
gl.uniform1f(uniform('uWall'), Math.max(0, Math.min(1, c.thickness)));
|
|
261
|
+
gl.uniform3fv(uniform('uGround'), parseColor(c.background));
|
|
262
|
+
gl.uniform3fv(uniform('uLight'), parseColor(c.light));
|
|
263
|
+
gl.uniform1f(uniform('uCardSize'), c.cardSize);
|
|
264
|
+
gl.uniform1f(uniform('uExposure'), c.exposure);
|
|
265
|
+
gl.uniform1f(uniform('uAmbient'), c.ambient);
|
|
266
|
+
gl.uniform1f(uniform('uIor'), c.ior);
|
|
267
|
+
gl.uniform1f(uniform('uDensity'), c.density);
|
|
268
|
+
gl.uniform1f(uniform('uReflect'), c.reflectivity);
|
|
269
|
+
gl.uniform1f(uniform('uOpacity'), Math.max(0, Math.min(1, c.opacity)));
|
|
270
|
+
gl.uniform2fv(uniform('uLightDir'), lightVector(c.lightAngle));
|
|
271
|
+
},
|
|
272
|
+
resize({ gl, uniform, canvas }) {
|
|
273
|
+
const aspect = canvas.width / canvas.height;
|
|
274
|
+
const layout = stackLayout(c, aspect);
|
|
275
|
+
gl.uniform2f(uniform('uCentre'), layout.centre.x * aspect, 1 - layout.centre.y);
|
|
276
|
+
gl.uniform1f(uniform('uEdge'), layout.edge);
|
|
277
|
+
const size = Math.max(64, Math.min(COLUMNS, Math.round(layout.edge * canvas.height)));
|
|
278
|
+
const mine = ++generation;
|
|
279
|
+
drawShape({ text: c.text, font: c.font, src: c.src, padding: 0 }, size, size)
|
|
280
|
+
.then((mask) => {
|
|
281
|
+
if (mine !== generation) return;
|
|
282
|
+
const runs = sliceRuns(mask, slices, { phase: c.phase, band: c.band, min: Math.max(2, Math.round(size * 0.025)) });
|
|
283
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
284
|
+
gl.bindTexture(gl.TEXTURE_2D, runsTex);
|
|
285
|
+
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
|
|
286
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size, slices, 0, gl.RGBA, gl.UNSIGNED_BYTE, runsTexture(runs, size));
|
|
287
|
+
if (stage?.still) stage.redraw();
|
|
288
|
+
})
|
|
289
|
+
.catch((error) => console.warn('[effects-astro]', error));
|
|
290
|
+
},
|
|
291
|
+
uniforms({ gl, uniform, still }, now) {
|
|
292
|
+
const seconds = (now - start) / 1000;
|
|
293
|
+
const t = (seconds - c.delay) * c.speed;
|
|
294
|
+
// The discs grow from the bottom up; the tints texture carries each one's size.
|
|
295
|
+
const done = still || !c.intro || t >= c.introDuration + 0.1;
|
|
296
|
+
if (!(done && grown === 1)) {
|
|
297
|
+
for (let i = 0; i < slices; i++) tintBytes[i * 4 + 3] = Math.round((done ? 1 : Math.max(0, Math.min(1.25, slabGrowth(i, slices, t, c.introDuration)))) * 204);
|
|
298
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
299
|
+
gl.bindTexture(gl.TEXTURE_2D, tintTex);
|
|
300
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, slices, 1, gl.RGBA, gl.UNSIGNED_BYTE, tintBytes);
|
|
301
|
+
grown = done ? 1 : 0;
|
|
302
|
+
}
|
|
303
|
+
// It leans on its own, slowly, and toward the pointer.
|
|
304
|
+
const p = !still && canHover() ? pointerIn(el) : null;
|
|
305
|
+
const lean = p?.inside ? { x: (p.x - 0.5) * 2, y: (p.y - 0.5) * 2 } : { x: 0, y: 0 };
|
|
306
|
+
const drift = still ? { x: 0.35, y: 0 } : { x: Math.sin(seconds * 0.37) * 0.8 + Math.sin(seconds * 0.17 + 1) * 0.2, y: Math.sin(seconds * 0.29 + 2) };
|
|
307
|
+
const goal = { x: (drift.x * c.sway + lean.x * c.tilt) * 0.12, y: (drift.y * c.sway + lean.y * c.tilt) * 0.12 };
|
|
308
|
+
const ease = still ? 1 : 0.05;
|
|
309
|
+
tilt.x += (goal.x - tilt.x) * ease;
|
|
310
|
+
tilt.y += (goal.y - tilt.y) * ease;
|
|
311
|
+
gl.uniform2f(uniform('uTilt'), tilt.x, tilt.y);
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
return stop;
|
|
315
|
+
}
|
|
@@ -1,32 +1,36 @@
|
|
|
1
1
|
export interface PixelFountainConfig {
|
|
2
|
-
/** The pixels' colour when they fire; the darker steps they fade through are made from it. Default `#c8ff00
|
|
2
|
+
/** The pixels' colour when they fire; the darker steps they fade through are made from it. With `source="pointer"`, the one ink every block is drawn in. Default `#c8ff00` (from the pointer, the text's colour, `currentColor`). */
|
|
3
3
|
color?: string;
|
|
4
|
-
/** The steps a pixel fades through, first to last (overrides `color
|
|
4
|
+
/** The steps a pixel fades through, first to last (overrides `color`; from the pointer, only the first is used). Default made from `color`. */
|
|
5
5
|
colors?: string[];
|
|
6
6
|
/** A shared palette by name, its colours as the steps, when there are no `colors`. Default none. */
|
|
7
7
|
palette?: string;
|
|
8
8
|
/** A pixel's size, CSS px (a power-up's biggest blocks are two). Default 8. */
|
|
9
9
|
cell?: number;
|
|
10
|
-
/** How far above the edge the pixels can fly, CSS px. Default 240. */
|
|
10
|
+
/** How far above the edge the pixels can fly, CSS px (from the pointer: how far above the host the canvas reaches). Default 240 (160 from the pointer). */
|
|
11
11
|
rise?: number;
|
|
12
|
-
/** Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none. Default 14. */
|
|
12
|
+
/** Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none (always none from the pointer). Default 14. */
|
|
13
13
|
rate?: number;
|
|
14
|
-
/** Pixels a click or tap fires. Default 40. */
|
|
14
|
+
/** Pixels a click or tap fires. Default 40 (24 from the pointer). */
|
|
15
15
|
burst?: number;
|
|
16
|
+
/** From the pointer: blocks fired for every 100 CSS px it moves, at most 12 a move. Default 10. */
|
|
17
|
+
trail?: number;
|
|
18
|
+
/** Whether a mouse click fires a burst (a tap always does). Default true (false from the pointer). */
|
|
19
|
+
press?: boolean;
|
|
16
20
|
/** How hard they are pulled back, 0–2. Default 1. */
|
|
17
21
|
gravity?: number;
|
|
18
22
|
/** How widely they fan out, 0–1. Default 0.5. */
|
|
19
23
|
spread?: number;
|
|
20
24
|
/** How much a wind pushes them sideways, 0–1. Default 0.3. */
|
|
21
25
|
drift?: number;
|
|
22
|
-
/** The longest a pixel lives, seconds. Default 1.4. */
|
|
26
|
+
/** The longest a pixel lives, seconds. Default 1.4 (0.9 from the pointer). */
|
|
23
27
|
life?: number;
|
|
24
|
-
/** The chance a pixel splits into smaller ones rather than shrinking, 0–1. Default 0.4. */
|
|
28
|
+
/** The chance a pixel splits into smaller ones rather than shrinking, 0–1. Default 0.4 (0.2 from the pointer). */
|
|
25
29
|
split?: number;
|
|
26
|
-
/** The chance a pixel flickers out on and off before it goes, 0–1. Default 0.35. */
|
|
30
|
+
/** The chance a pixel flickers out on and off before it goes, 0–1. Default 0.35 (0.5 from the pointer). */
|
|
27
31
|
flicker?: number;
|
|
28
|
-
/** Fire from the host's top edge
|
|
29
|
-
source?: 'edge' | 'text';
|
|
32
|
+
/** Fire from the host's top edge; from the filled pixels of its text; or only from the pointer, where it moves over the host (a block-dust trail in one ink, nothing on its own). Default `edge`. */
|
|
33
|
+
source?: 'edge' | 'text' | 'pointer';
|
|
30
34
|
/** A selector for the element the pixels fire from and the pointer is followed over: the nearest ancestor that matches, else the first on the page. Default the element's positioned parent. */
|
|
31
35
|
host?: string;
|
|
32
36
|
/** Seeds the randomness. Default 1. */
|
|
@@ -61,8 +65,8 @@ export interface Pixel {
|
|
|
61
65
|
wobble: number;
|
|
62
66
|
}
|
|
63
67
|
|
|
64
|
-
/** What fired a pixel: the ambient trickle, the pointer moving, or a press (a power-up). */
|
|
65
|
-
export type EmitKind = 'ambient' | 'move' | 'burst';
|
|
68
|
+
/** What fired a pixel: the ambient trickle, the pointer moving over the edge or letters, or a press (a power-up); from the pointer, its trail and a tap. */
|
|
69
|
+
export type EmitKind = 'ambient' | 'move' | 'burst' | 'trail' | 'tap';
|
|
66
70
|
|
|
67
71
|
/** A fountain's settings: the canvas's size and the effect's physics. */
|
|
68
72
|
export interface FountainOptions {
|
|
@@ -80,6 +84,8 @@ export interface FountainOptions {
|
|
|
80
84
|
life?: number;
|
|
81
85
|
split?: number;
|
|
82
86
|
flicker?: number;
|
|
87
|
+
/** Blocks a pointer's move fires for every 100 CSS px it travels. Default 10. */
|
|
88
|
+
trail?: number;
|
|
83
89
|
seed?: number;
|
|
84
90
|
}
|
|
85
91
|
|
|
@@ -100,6 +106,10 @@ export interface Fountain {
|
|
|
100
106
|
emitEdge(x: number, n: number, kind?: EmitKind): number;
|
|
101
107
|
/** Fires `n` pixels from where the ambient ones fire. */
|
|
102
108
|
emitAmbient(n: number, kind?: EmitKind): number;
|
|
109
|
+
/** The pointer moved to a point: fires `trail` blocks per 100 px travelled (at most `TRAIL_CAP`), spaced along the way it came and ending there; none on the first point after a lift or for no move. Returns how many fired. */
|
|
110
|
+
moveTo(x: number, y: number): number;
|
|
111
|
+
/** Forgets where the pointer was (it left, or lifted). */
|
|
112
|
+
lift(): void;
|
|
103
113
|
/** One step (default 1/60 s): the trickle, then every pixel ages, breaks down, moves and flickers. */
|
|
104
114
|
step(dt?: number): void;
|
|
105
115
|
/** Empties the pool. */
|
|
@@ -112,6 +122,12 @@ export const MAX_PIXELS: number;
|
|
|
112
122
|
/** The simulation's fixed step, seconds (1/60). */
|
|
113
123
|
export const STEP: number;
|
|
114
124
|
|
|
125
|
+
/** The most blocks one pointer move fires (12). */
|
|
126
|
+
export const TRAIL_CAP: number;
|
|
127
|
+
|
|
128
|
+
/** The settings that change with `source: 'pointer'`. */
|
|
129
|
+
export const POINTER_DEFAULTS: { rate: number; life: number; split: number; flicker: number; rise: number; burst: number; trail: number };
|
|
130
|
+
|
|
115
131
|
/** The steps a pixel fades through, made from one colour in Oklab: darker and less saturated each step, turning a little towards olive. */
|
|
116
132
|
export function fadeSteps(color: string, count?: number): string[];
|
|
117
133
|
|
|
@@ -130,5 +146,5 @@ export function inkedCells(rgba: Uint8ClampedArray, width: number, height: numbe
|
|
|
130
146
|
/** Makes a seeded fountain of pixels. */
|
|
131
147
|
export function createFountain(options: FountainOptions): Fountain;
|
|
132
148
|
|
|
133
|
-
/** Starts the pixel fountain on an element placed over its host's top edge. Returns a function that stops it. */
|
|
149
|
+
/** Starts the pixel fountain on an element placed over its host's top edge (over the host and above it, from the text or the pointer). Returns a function that stops it. */
|
|
134
150
|
export function pixelFountain(el: HTMLElement, config: PixelFountainConfig): () => void;
|