@arraypress/effects-astro 0.12.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 +1 -1
- package/COMPONENTS.md +89 -0
- package/package.json +1 -1
- package/src/components/GlassDrop.astro +40 -0
- package/src/components/GlassStack.astro +41 -0
- package/src/index.d.ts +9 -0
- package/src/index.js +5 -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/slab-runs.d.ts +18 -0
- package/src/runtime/slab-runs.js +112 -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
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** A stretch of logo across a slice, as columns (both ends included). */
|
|
2
|
+
export interface Run {
|
|
3
|
+
from: number;
|
|
4
|
+
to: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** The runs of each slice (top first, each left to right): where a mask's logo crosses it. `phase` (0–1) is where in its slice each samples, `band` (0–1) how much of it, `threshold` the coverage that counts (0–255), `min` the narrowest run kept. */
|
|
8
|
+
export declare function sliceRuns(
|
|
9
|
+
mask: { alpha: Uint8ClampedArray | Uint8Array; width: number; height: number },
|
|
10
|
+
slices: number,
|
|
11
|
+
options?: { phase?: number; band?: number; threshold?: number; min?: number },
|
|
12
|
+
): Run[][];
|
|
13
|
+
|
|
14
|
+
/** The runs as the shader's RGBA texture (a row per slice, a texel per column): where along its run a column is in red and green (16 bits), 255 in blue, the run's width share in alpha; zero outside runs. */
|
|
15
|
+
export declare function runsTexture(runs: Run[][], width: number): Uint8Array;
|
|
16
|
+
|
|
17
|
+
/** A slice's disc size in the intro, `t` seconds in (0 none to 1 whole; a little over while settling): the bottom first, the top done by `duration`. */
|
|
18
|
+
export declare function slabGrowth(slice: number, slices: number, t: number, duration?: number): number;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/slab-runs
|
|
3
|
+
*
|
|
4
|
+
* GlassStack's slicing: a logo cut into horizontal slices, each slice's
|
|
5
|
+
* stretches of logo (its runs) becoming the glass discs of the stack. Pure,
|
|
6
|
+
* so it runs (and is tested) without a browser: it reads a mask's coverage
|
|
7
|
+
* and writes a small texture the shader reads, a row per slice.
|
|
8
|
+
*
|
|
9
|
+
* A slice samples a band of the logo, not one line, so a small part (a dot,
|
|
10
|
+
* a thin bar) is never lost between slices: `phase` says where in its
|
|
11
|
+
* slice the band sits (0 top, 1 bottom) and `band` how much of the slice it
|
|
12
|
+
* covers (0 a line, 1 all of it).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {object} Run
|
|
17
|
+
* @property {number} from Its first column.
|
|
18
|
+
* @property {number} to Its last column.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The runs of each slice: where the logo crosses it, as column spans.
|
|
23
|
+
*
|
|
24
|
+
* @param {{ alpha: Uint8ClampedArray | Uint8Array, width: number, height: number }} mask The logo's coverage, rows from the top.
|
|
25
|
+
* @param {number} slices How many slices, top to bottom.
|
|
26
|
+
* @param {{ phase?: number, band?: number, threshold?: number, min?: number }} [options] Where each slice samples
|
|
27
|
+
* (`phase`, 0–1, default 0.5), how much of it (`band`, 0–1, default 0.35), the coverage that counts (`threshold`,
|
|
28
|
+
* 0–255, default 110) and the narrowest run kept, in columns (`min`, default 2).
|
|
29
|
+
* @returns {Run[][]} A list of runs per slice, top first, each left to right.
|
|
30
|
+
*/
|
|
31
|
+
export function sliceRuns(mask, slices, options = {}) {
|
|
32
|
+
const { alpha, width, height } = mask;
|
|
33
|
+
const phase = options.phase ?? 0.5;
|
|
34
|
+
const band = options.band ?? 0.35;
|
|
35
|
+
const threshold = options.threshold ?? 110;
|
|
36
|
+
const min = options.min ?? 2;
|
|
37
|
+
const pitch = height / slices;
|
|
38
|
+
/** @type {Run[][]} */
|
|
39
|
+
const out = [];
|
|
40
|
+
for (let i = 0; i < slices; i++) {
|
|
41
|
+
const centre = (i + phase) * pitch;
|
|
42
|
+
const reach = Math.max(0.5, (band * pitch) / 2);
|
|
43
|
+
const top = Math.max(0, Math.floor(centre - reach));
|
|
44
|
+
const bottom = Math.min(height - 1, Math.ceil(centre + reach));
|
|
45
|
+
/** @type {Run[]} */
|
|
46
|
+
const runs = [];
|
|
47
|
+
let from = -1;
|
|
48
|
+
for (let x = 0; x <= width; x++) {
|
|
49
|
+
let on = false;
|
|
50
|
+
if (x < width) for (let y = top; y <= bottom && !on; y++) on = alpha[y * width + x] > threshold;
|
|
51
|
+
if (on && from < 0) from = x;
|
|
52
|
+
if (!on && from >= 0) {
|
|
53
|
+
if (x - from >= min) runs.push({ from, to: x - 1 });
|
|
54
|
+
from = -1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
out.push(runs);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The runs as the shader's texture: a row per slice (top first), a texel
|
|
64
|
+
* per column. Inside a run, red and green hold where the column is along
|
|
65
|
+
* it (0 its left edge, 1 its right) in sixteen bits, blue is 255, and alpha
|
|
66
|
+
* the run's width as a share of the row; outside, all four are 0.
|
|
67
|
+
*
|
|
68
|
+
* @param {Run[][]} runs The runs per slice.
|
|
69
|
+
* @param {number} width The row's width in columns.
|
|
70
|
+
* @returns {Uint8Array} RGBA bytes, `width × runs.length` texels.
|
|
71
|
+
*/
|
|
72
|
+
export function runsTexture(runs, width) {
|
|
73
|
+
const out = new Uint8Array(width * runs.length * 4);
|
|
74
|
+
runs.forEach((row, i) => {
|
|
75
|
+
for (const { from, to } of row) {
|
|
76
|
+
const span = to - from + 1;
|
|
77
|
+
const share = Math.min(255, Math.round((span / width) * 255));
|
|
78
|
+
for (let x = from; x <= to; x++) {
|
|
79
|
+
const along = Math.round(((x - from + 0.5) / span) * 65535);
|
|
80
|
+
const o = (i * width + x) * 4;
|
|
81
|
+
out[o] = along >> 8;
|
|
82
|
+
out[o + 1] = along & 255;
|
|
83
|
+
out[o + 2] = 255;
|
|
84
|
+
out[o + 3] = share;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* When each slice's disc has grown, in the intro: the bottom first, the
|
|
93
|
+
* top last, each over a share of the whole, eased out with a slight
|
|
94
|
+
* overshoot so the glass settles.
|
|
95
|
+
*
|
|
96
|
+
* @param {number} slice Which slice, 0 the top.
|
|
97
|
+
* @param {number} slices How many.
|
|
98
|
+
* @param {number} t Seconds since the intro began.
|
|
99
|
+
* @param {number} [duration=1.6] Seconds until the top disc has grown.
|
|
100
|
+
* @returns {number} Its size, 0 none to 1 whole (a little over while settling).
|
|
101
|
+
*/
|
|
102
|
+
export function slabGrowth(slice, slices, t, duration = 1.6) {
|
|
103
|
+
if (duration <= 0) return 1;
|
|
104
|
+
const grow = duration * 0.38;
|
|
105
|
+
const from = slices > 1 ? ((slices - 1 - slice) / (slices - 1)) * (duration - grow) : 0;
|
|
106
|
+
const x = (t - from) / grow;
|
|
107
|
+
if (x <= 0) return 0;
|
|
108
|
+
if (x >= 1) return 1;
|
|
109
|
+
// easeOutBack: past 1 a little, back to exactly 1
|
|
110
|
+
const c = 1.4;
|
|
111
|
+
return 1 + (c + 1) * (x - 1) ** 3 + c * (x - 1) ** 2;
|
|
112
|
+
}
|