feat: split the glass into scene, page and bar containers; press, stretch and menu morph animations

The bar and its menu render on a small fixed canvas above the page glass, so neither trails the other while scrolling. Small glass skips blur and dispersion, and idle frames redraw at a low rate. Corners are superellipses; glare lifts chroma; pills swell when pressed and stretch along their motion; the language menu's glass grows out of its button with a sliding highlight.
This commit is contained in:
Flechazo committed 2026-10-25 09:54:35 +08:00
1 parent fb2fd370b2
commit f4017387b0
3 files changed
+251 -118

No files matched your search

+8 -2
View File
@@ -57,6 +57,9 @@ a { color: inherit; }
.glass-layer { position: absolute; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; z-index: -1; pointer-events: none; opacity: 0; transition: opacity 1.4s ease; }
.scene.gl.ready + .glass-layer { opacity: 1; }
.glass-layer canvas { position: absolute; left: 0; top: 0; display: block; will-change: transform; }
.glass-top { position: fixed; left: 0; top: 0; z-index: -1; pointer-events: none; opacity: 0; transition: opacity 1.4s ease; }
.scene.gl.ready ~ .glass-top { opacity: 1; }
.glass-top canvas { position: absolute; left: 0; top: 0; display: block; }
.scene.gl .art, .scene.gl .glow { display: none; }
html.gl .grade { background: radial-gradient(58% 48% at 22% 32%, rgba(18, 7, 36, .58), rgba(18, 7, 36, .22) 55%, rgba(18, 7, 36, 0) 100%), linear-gradient(180deg, rgba(20, 8, 40, .1) 0%, rgba(20, 8, 40, 0) 45%, rgba(14, 5, 28, .4) 100%), rgba(14, 5, 28, var(--dim, 0)); }
@@ -147,6 +150,9 @@ html.gl .grade { background: radial-gradient(58% 48% at 22% 32%, rgba(18, 7, 36,
.lang-menu li:hover, .lang-menu li.active { color: var(--text); background-color: rgba(255, 255, 255, .08); }
.lang-menu li[aria-selected="true"] { color: var(--text); background-color: rgba(255, 255, 255, .14); }
@keyframes menu-in { from { transform: scale(.6, .4); opacity: 0; } }
/* Liquid: the shader grows the menu's glass out of the button; the options fade in after it. */
@keyframes menu-fade { from { opacity: 0; transform: translateY(-6px); } }
html.liquid .lang-menu { animation: menu-fade .3s ease .08s both; }
@media (max-width: 640px) { .bar nav { display: none; } .lang { margin-left: auto; } }
main { width: min(1080px, calc(100% - 28px)); margin: 0 auto; }
@@ -296,7 +302,7 @@ html.liquid .stamp { color: rgba(246, 240, 255, .66); }
/* Liquid mode: the shader draws every glass surface; HTML keeps the content. */
html.liquid .glass, html.liquid .pill, html.liquid .file, html.liquid .shell pre, html.liquid .eyebrow,
html.liquid .lang-btn, html.liquid .filter .thumb, html.liquid .lang-menu li[aria-selected="true"] {
html.liquid .lang-btn, html.liquid .filter .thumb, html.liquid .lang-menu li {
background-color: transparent; background-image: none;
-webkit-backdrop-filter: none; backdrop-filter: none; box-shadow: none;
}
@@ -304,7 +310,7 @@ html.liquid .lang-btn { background-image: url("/assets/chevron.svg"); }
html.liquid .glass::before, html.liquid .glass::after { display: none; }
html.liquid .card.reveal, html.liquid .log.reveal, html.liquid .verify.reveal { opacity: 1; transform: none; }
html.liquid .pill:hover { transform: scale(1.06); }
html.liquid .pill:active { transform: scale(.93); }
html.liquid .pill:active { transform: scale(1.04); }
html.liquid .file { transition: transform .6s var(--spring); }
html.liquid .file:hover { transform: scale(1.012); }
html.liquid .filter { background: rgba(10, 4, 22, .35); }
+241 -114
View File
@@ -352,28 +352,37 @@ const PREFS = (() => {
// WebGL2 liquid glass, after Apple's Liquid Glass and the open recreations
// (liquid-glass-studio, ptu14/liquid-glass): the graded art is rendered to a
// texture, blurred at half resolution, and one pass draws every glass shape
// from its live element rectangle. Shapes are rounded-rect SDFs; a Snell
// refraction edge factor bends the background at the bezel, with dispersion,
// fresnel and a two-sided glare. The sticky bar fuses with any glass it
// touches while scrolling through a wide smooth minimum, with a brighter rim
// at the neck. Without WebGL2 the CSS glass remains.
// texture, blurred at half resolution, and a glass pass draws every shape
// from its live element rectangle. Shapes are rounded rects with
// superellipse corners; a Snell refraction edge factor bends the background
// at the bezel, with dispersion, fresnel and a glare that lifts lightness and
// chroma. Glass fuses through a smooth minimum.
//
// Like SwiftUI's GlassEffectContainer the glass is split into containers
// that render apart: the page container scrolls with the document, the bar
// container is a small fixed canvas around the bar and its menu, and the
// scene itself is a third canvas. Small glass skips blur and dispersion,
// and when nothing moves the containers redraw at a low rate.
// Without WebGL2 the CSS glass remains.
const MAX_GLASS = 40;
// Kinds: 0 panel, 1 clear pill, 2 accent pill, 3 inset row, 4 bar.
const GLASS = [
[".bar", 4], [".lang-menu", 0], [".quick", 0], [".card", 0], [".log", 0], [".verify", 0], [".file", 3], [".shell pre", 3],
[".eyebrow", 1], [".lang-btn", 1], [".lang-menu [aria-selected=\"true\"]", 1], [".filter .thumb", 1], [".ghost", 1], [".more", 1], [".dl", 2], [".toast.on", 1],
[".bar", 4], [".quick", 0], [".card", 0], [".log", 0], [".verify", 0], [".file", 3], [".shell pre", 3],
[".eyebrow", 1], [".lang-btn", 1], [".filter .thumb", 1], [".ghost", 1], [".more", 1], [".dl", 2], [".toast.on", 1],
];
const PRESSABLE = ".pill, .lang-btn, .filter button, .quick";
const VERTEX = `#version 300 es
in vec2 p;
void main() { gl_Position = vec4(p, 0.0, 1.0); }`;
// The illustration with its light, in CSS-pixel space (y down).
// The illustration with its light, in viewport pixels (y down).
const SCENE = `#version 300 es
precision highp float;
uniform sampler2D u_art;
uniform vec2 u_res;
uniform vec2 u_res; // target, px
uniform vec2 u_origin; // target's top-left in the viewport, px
uniform vec2 u_view; // viewport, px
uniform vec4 u_rect;
uniform float u_time;
uniform float u_dim;
@@ -381,7 +390,7 @@ uniform float u_px;
out vec4 color;
void main() {
vec2 px = vec2(gl_FragCoord.x, u_res.y - gl_FragCoord.y);
vec2 px = u_origin + vec2(gl_FragCoord.x, u_res.y - gl_FragCoord.y);
float scale = max(u_rect.z / (1000.0 * u_px), .4) * u_px;
vec3 col = texture(u_art, (px - u_rect.xy) / u_rect.zw).rgb;
float l = dot(col, vec3(.299, .587, .114));
@@ -403,7 +412,7 @@ void main() {
float flicker = .75 + .25 * sin(u_time * 1.7 + sin(u_time * .6) * 2.0);
vec2 e1 = u_rect.xy + vec2(.597, .392) * u_rect.zw, e2 = u_rect.xy + vec2(.957, .405) * u_rect.zw;
col += vec3(1.0, .2, .32) * (exp(-length(px - e1) / (38.0 * scale)) + exp(-length(px - e2) / (38.0 * scale))) * .32 * flicker;
vec2 q = gl_FragCoord.xy / u_res - .5;
vec2 q = px / u_view - .5;
col *= (1.0 - dot(q, q) * .35) * (1.0 - u_dim * .3);
color = vec4(col, 1.0);
}`;
@@ -426,31 +435,35 @@ void main() {
color = vec4(c / (w[0] + 2.0 * (w[1] + w[2] + w[3] + w[4] + w[5] + w[6])), 1.0);
}`;
// Output is premultiplied over whatever lies below the container's canvas.
const GLASS_SHADER = `#version 300 es
precision highp float;
#define MAX ${MAX_GLASS}
#define PI 3.14159265
uniform sampler2D u_bg;
uniform sampler2D u_blur;
uniform vec2 u_res; // canvas, px
uniform vec2 u_view; // viewport, px
uniform float u_off; // rows above the viewport (scrolling layer), px
uniform int u_overlay; // 1: transparent layer over the scene canvas
uniform vec2 u_origin; // canvas top-left in the viewport, px
uniform vec4 u_fbo; // viewport rect the scene textures cover, px
uniform int u_cutBar; // page container: the bar container draws the bar itself
uniform vec2 u_mouse;
uniform vec2 u_light; // the blade's light, px
uniform float u_px;
uniform float u_time;
uniform int u_count;
uniform vec4 u_glass[MAX];
uniform vec4 u_meta[MAX]; // radius, kind (5 = occluder), hover, layer
uniform vec4 u_meta[MAX]; // radius, kind, hover (+1 while pressed), layer
out vec4 color;
float hash(vec2 p) { return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }
float roundRect(vec2 p, vec4 r, float radius) {
// Rounded rect whose corners are superellipses (n = 2.6), close to Apple's
// continuous corners.
float shape(vec2 p, vec4 r, float radius) {
vec2 h = r.zw * .5;
vec2 q = abs(p - (r.xy + h)) - (h - radius);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius;
vec2 m = max(q, 0.0) / max(radius, 1e-3);
float corner = m.x > 0.0 && m.y > 0.0 ? pow(pow(m.x, 2.6) + pow(m.y, 2.6), 1.0 / 2.6) : m.x + m.y;
return corner * radius + min(max(q.x, q.y), 0.0) - radius;
}
// Inigo Quilez's polynomial smooth minimum.
@@ -459,41 +472,40 @@ float smin(float a, float b, float k) {
return mix(b, a, h) - k * h * (1.0 - h);
}
vec2 uvOf(vec2 p) { return vec2((p.x - u_fbo.x) / u_fbo.z, 1.0 - (p.y - u_fbo.y) / u_fbo.w); }
void main() {
vec2 frag = gl_FragCoord.xy;
vec2 px = vec2(frag.x, u_res.y - frag.y - u_off);
vec2 uv = vec2(px.x, u_view.y - px.y) / u_view;
vec2 px = u_origin + vec2(gl_FragCoord.x, u_res.y - gl_FragCoord.y);
vec2 uv = uvOf(px);
vec3 bg = texture(u_bg, uv).rgb, col = bg;
// Per-layer distances. Layer 0 holds panels and free pills; the bar is
// kept apart so it can fuse with them.
float d0 = 1e5, d1 = 1e5, d2 = 1e5, dBar = 1e5, dOcc = 1e5;
float d0 = 1e5, d1 = 1e5, d2 = 1e5, dBar = 1e5;
float k0 = 0.0, hov0 = 0.0, hov1 = 0.0, hov2 = 0.0, acc2 = 0.0, bar0 = 0.0;
float r0 = 0.0, r1 = 0.0, r2 = 0.0;
float kSelf = 6.0 * u_px;
for (int i = 0; i < MAX; i++) {
if (i >= u_count) break;
vec4 r = u_glass[i], m = u_meta[i];
float d = roundRect(px, r, min(m.x, min(r.z, r.w) * .5));
if (m.y == 5.0) { dOcc = min(dOcc, d); continue; }
float d = shape(px, r, min(m.x, min(r.z, r.w) * .5));
float near = 1.0 - smoothstep(0.0, 24.0 * u_px, d);
if (m.y == 4.0) { dBar = d; bar0 = max(bar0, near); r0 = max(r0, m.x * near); }
else if (m.w == 0.0) { d0 = smin(d0, d, kSelf); hov0 = max(hov0, m.z * near); r0 = max(r0, m.x * near); k0 = max(k0, (m.y == 0.0 ? 1.0 : 0.0) * near); }
else if (m.w == 1.0) { d1 = smin(d1, d, kSelf); hov1 = max(hov1, m.z * near); r1 = max(r1, m.x * near); }
else { d2 = smin(d2, d, kSelf); hov2 = max(hov2, m.z * near); acc2 = max(acc2, (m.y == 2.0 ? 1.0 : 0.0) * near); r2 = max(r2, m.x * near); }
if (m.y == 4.0) { dBar = min(dBar, d); bar0 = max(bar0, near); }
else if (m.w == 0.0) { d0 = smin(d0, d, kSelf); hov0 = max(hov0, m.z * near); k0 = max(k0, (m.y == 0.0 ? 1.0 : 0.0) * near); }
else if (m.w == 1.0) { d1 = smin(d1, d, kSelf); hov1 = max(hov1, m.z * near); }
else { d2 = smin(d2, d, kSelf); hov2 = max(hov2, m.z * near); acc2 = max(acc2, (m.y == 2.0 ? 1.0 : 0.0) * near); }
}
// Contact: within kBar the bar and the glass under it pull into one
// shape; neck marks the bridge between them.
float own = d0;
// Contact: within kBar the bar and the glass near it pull into one shape;
// neck marks the bridge between them.
float kBar = 30.0 * u_px;
float neck = clamp(1.0 - abs(d0 - dBar) / kBar, 0.0, 1.0) * (1.0 - smoothstep(0.0, kBar, max(d0, dBar)));
d0 = smin(d0, dBar, kBar);
float panels = d0;
// Gradients for all layers before any branching.
vec2 g0 = vec2(dFdx(d0), -dFdy(d0)), g1 = vec2(dFdx(d1), -dFdy(d1)), g2 = vec2(dFdx(d2), -dFdy(d2));
// Soft shadow under the base layer.
float shadow = .3 * (1.0 - smoothstep(0.0, 26.0 * u_px, panels)) * step(0.0, panels);
float shadow = .3 * (1.0 - smoothstep(0.0, 26.0 * u_px, d0)) * step(0.0, d0);
col *= 1.0 - shadow;
float cover = 0.0;
@@ -503,6 +515,8 @@ void main() {
vec2 g = l == 0 ? g0 : l == 1 ? g1 : g2;
vec2 n = g / max(length(g), 1e-4);
float hov = l == 0 ? hov0 : l == 1 ? hov1 : hov2;
float press = max(hov - 1.0, 0.0);
hov = min(hov, 1.0);
float accent = l == 2 ? acc2 : 0.0;
float panel = l == 0 ? max(k0, bar0) : 0.0;
// Bezel thickness: thick for panels, thin for pills and rows.
@@ -516,17 +530,21 @@ void main() {
float tt = asin(sin(ti) / 1.5);
edge = tan(ti - tt);
}
float reach = (l == 0 ? mix(46.0, 60.0, panel) : 26.0) * u_px;
float reach = (l == 0 ? mix(46.0, 60.0, panel) : 26.0) * u_px * (1.0 + .5 * press);
vec2 off = -n * edge * reach;
off.y = -off.y;
vec2 o = off / u_view;
// Dispersion: red bends least, blue most.
float spread = .1 * edge;
// Clear glass: barely frosted, so the art stays visible beneath.
float blurMix = l == 0 ? mix(.0, .16, panel) : l == 1 ? .1 : .0;
vec3 sharp = vec3(texture(u_bg, uv + o * (1.0 - spread)).r, texture(u_bg, uv + o).g, texture(u_bg, uv + o * (1.0 + spread)).b);
vec3 soft = vec3(texture(u_blur, uv + o * (1.0 - spread)).r, texture(u_blur, uv + o).g, texture(u_blur, uv + o * (1.0 + spread)).b);
vec3 g3 = mix(sharp, soft, blurMix);
vec2 o = off / u_fbo.zw;
vec3 g3;
if (l == 0) {
// Dispersion: red bends least, blue most; the bezel is a touch frosted.
float spread = .12 * edge;
vec3 sharp = vec3(texture(u_bg, uv + o * (1.0 - spread)).r, texture(u_bg, uv + o).g, texture(u_bg, uv + o * (1.0 + spread)).b);
vec3 soft = vec3(texture(u_blur, uv + o * (1.0 - spread)).r, texture(u_blur, uv + o).g, texture(u_blur, uv + o * (1.0 + spread)).b);
g3 = mix(sharp, soft, mix(.0, .16, panel) + .2 * (1.0 - x) * step(0.0, x) * panel);
} else {
// Small glass: one tap; dispersion and blur would not show.
g3 = texture(u_bg, uv + o).rgb;
}
// Vibrancy and a violet body that keeps text legible on panels.
float lum = dot(g3, vec3(.299, .587, .114));
g3 = mix(vec3(lum), g3, 1.15);
@@ -534,12 +552,13 @@ void main() {
float body = l == 0 ? mix(.04, .1, panel) : l == 1 ? .1 : mix(.04, .32, accent);
g3 = mix(g3, tint * (accent > .5 ? lum + .3 : 1.0), body);
g3 *= l == 0 ? mix(.96, .86, panel) : .94;
g3 += .04 * hov;
g3 += .04 * hov + .1 * press;
// Fresnel: the rim brightens toward white.
float fres = pow(clamp(1.0 - depth / (18.0 * u_px), 0.0, 1.0), 5.0);
float contact = l == 0 ? neck : 0.0;
g3 = mix(g3, vec3(1.0), fres * (.32 + .4 * contact));
// Glare: two opposite highlights, the brighter one facing the blade light.
g3 = mix(g3, vec3(1.0), fres * (.32 + .4 * contact + .3 * press));
// Glare: two opposite highlights, the brighter one facing the blade
// light. It lifts lightness and chroma of what is seen through the rim.
vec2 toLight = normalize(u_light - px);
float facing = dot(n, toLight);
float diag = .5 + .5 * cos(2.0 * (atan(n.y, n.x) - atan(toLight.y, toLight.x)));
@@ -548,25 +567,27 @@ void main() {
// The pointer is a second light on nearby rims.
vec2 toMouse = u_mouse - px;
float mouseGlare = pow(max(dot(n, normalize(toMouse)), 0.0), 3.0) * exp(-length(toMouse) / (160.0 * u_px)) * glareGeo;
g3 += (vec3(1.0, .82, .96) * glare * .85 + vec3(1.0) * mouseGlare * .8) * (1.0 + contact);
vec3 vivid = clamp(mix(vec3(lum), g3, 1.8) * 1.5 + .2, 0.0, 1.4);
g3 = mix(g3, vivid, clamp(glare * .9 * (1.0 + contact), 0.0, 1.0));
g3 += vec3(1.0, .86, .97) * glare * .35 + vec3(1.0) * mouseGlare * .8 * (1.0 + contact);
float alpha = 1.0 - smoothstep(-1.0 * u_px, 1.0 * u_px, d);
col = mix(col, g3, alpha);
cover = max(cover, alpha);
}
float grain = (hash(px + fract(u_time) * 100.0) - .5) * .022;
if (u_overlay == 0) { color = vec4(col + grain, 1.0); return; }
// Over the scene canvas: premultiplied, so that compositing onto the same
// scene gives col back. The bar, menu and toast live on the scene canvas
// and cut holes here.
col += grain * cover;
col += (hash(px + fract(u_time) * 100.0) - .5) * .022 * cover;
// Premultiplied, so that compositing onto the scene gives col back.
float a = 1.0 - (1.0 - cover) * (1.0 - shadow);
float keep = smoothstep(-1.0 * u_px, 1.0 * u_px, min(dOcc, dBar));
color = vec4(max(col - bg * (1.0 - a), 0.0), a) * keep;
vec4 outc = vec4(max(col - bg * (1.0 - a), 0.0), a);
// The page container leaves the bar's body to the bar container but keeps
// its own glass under it and the neck outside it.
if (u_cutBar == 1) outc *= max(1.0 - smoothstep(-1.0 * u_px, 1.0 * u_px, own), smoothstep(-1.0 * u_px, 1.0 * u_px, dBar));
color = outc;
}`;
// One WebGL2 context: its own copy of the scene, the blur and the glass pass.
function glassContext(canvas, art, overlay) {
const gl = canvas.getContext("webgl2", { alpha: overlay, premultipliedAlpha: true, antialias: false, depth: false, stencil: false });
// One container: its own WebGL2 context, scene copy, blur and glass pass.
// sceneOnly draws the scene straight to its canvas.
function glassContext(canvas, art, sceneOnly) {
const gl = canvas.getContext("webgl2", { alpha: !sceneOnly, premultipliedAlpha: true, antialias: false, depth: false, stencil: false });
if (!gl) return null;
const build = fragment => {
const program = gl.createProgram();
@@ -583,7 +604,7 @@ function glassContext(canvas, art, overlay) {
const cache = {};
return { program, u: name => (name in cache ? cache[name] : (cache[name] = gl.getUniformLocation(program, name))) };
};
const sceneP = build(SCENE), blurP = build(BLUR), glassP = build(GLASS_SHADER);
const sceneP = build(SCENE), blurP = sceneOnly ? null : build(BLUR), glassP = sceneOnly ? null : build(GLASS_SHADER);
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
@@ -610,32 +631,35 @@ function glassContext(canvas, art, overlay) {
};
let targets = null, size = "";
// f: ratio, vw/vh (viewport, CSS px), off (rows above it, CSS px), time,
// rect, dim, mouse, light and the glass list.
// f: ratio, view [w, h], canvas {x, y, w, h} and fbo {x, y, w, h} in
// viewport CSS px, time, art rect, dim, mouse, light, list, cutBar.
return function render(f) {
const k = f.ratio, r = f.rect;
const w = Math.round(f.vw * k), h = Math.round(f.vh * k), ch = Math.round((f.vh + 2 * f.off) * k);
if (size !== `${w}x${h}x${ch}`) {
size = `${w}x${h}x${ch}`;
canvas.width = w; canvas.height = ch;
const k = f.ratio, r = f.rect, c = f.canvas, b = f.fbo || f.canvas;
const cw = Math.max(1, Math.round(c.w * k)), ch = Math.max(1, Math.round(c.h * k));
const bw = Math.max(1, Math.round(b.w * k)), bh = Math.max(1, Math.round(b.h * k));
if (size !== `${cw}x${ch}/${bw}x${bh}`) {
size = `${cw}x${ch}/${bw}x${bh}`;
canvas.width = cw; canvas.height = ch;
if (targets) for (const t of Object.values(targets)) { gl.deleteTexture(t.tex); gl.deleteFramebuffer(t.fbo); }
const hw = Math.max(1, w >> 1), hh = Math.max(1, h >> 1);
targets = { bg: target(w, h), half: target(hw, hh), blur: target(hw, hh) };
targets = sceneOnly ? {} : { bg: target(bw, bh), half: target(Math.max(1, bw >> 1), Math.max(1, bh >> 1)), blur: target(Math.max(1, bw >> 1), Math.max(1, bh >> 1)) };
}
// 1. Scene.
gl.useProgram(sceneP.program);
gl.bindFramebuffer(gl.FRAMEBUFFER, targets.bg.fbo);
gl.viewport(0, 0, w, h);
gl.bindFramebuffer(gl.FRAMEBUFFER, sceneOnly ? null : targets.bg.fbo);
gl.viewport(0, 0, bw, bh);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, artTexture);
gl.uniform1i(sceneP.u("u_art"), 0);
gl.uniform2f(sceneP.u("u_res"), w, h);
gl.uniform2f(sceneP.u("u_res"), bw, bh);
gl.uniform2f(sceneP.u("u_origin"), b.x * k, b.y * k);
gl.uniform2f(sceneP.u("u_view"), f.view[0] * k, f.view[1] * k);
gl.uniform4f(sceneP.u("u_rect"), r.x * k, r.y * k, r.w * k, r.h * k);
gl.uniform1f(sceneP.u("u_time"), f.time);
gl.uniform1f(sceneP.u("u_dim"), f.dim);
gl.uniform1f(sceneP.u("u_px"), k);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
if (sceneOnly) return;
// 2. Blur at half resolution.
gl.useProgram(blurP.program);
@@ -653,7 +677,7 @@ function glassContext(canvas, art, overlay) {
// 3. Glass.
gl.useProgram(glassP.program);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, w, ch);
gl.viewport(0, 0, cw, ch);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, targets.bg.tex);
gl.activeTexture(gl.TEXTURE1);
@@ -662,10 +686,10 @@ function glassContext(canvas, art, overlay) {
const u = glassP.u;
gl.uniform1i(u("u_bg"), 0);
gl.uniform1i(u("u_blur"), 1);
gl.uniform2f(u("u_res"), w, ch);
gl.uniform2f(u("u_view"), w, h);
gl.uniform1f(u("u_off"), f.off * k);
gl.uniform1i(u("u_overlay"), overlay ? 1 : 0);
gl.uniform2f(u("u_res"), cw, ch);
gl.uniform2f(u("u_origin"), c.x * k, c.y * k);
gl.uniform4f(u("u_fbo"), b.x * k, b.y * k, bw, bh);
gl.uniform1i(u("u_cutBar"), f.cutBar ? 1 : 0);
gl.uniform2f(u("u_mouse"), f.mouse[0] * k, f.mouse[1] * k);
gl.uniform2f(u("u_light"), f.light[0] * k, f.light[1] * k);
gl.uniform1f(u("u_px"), k);
@@ -677,95 +701,198 @@ function glassContext(canvas, art, overlay) {
};
}
// Two canvases keep the glass glued to the page while scrolling. Browsers
// scroll on the compositor ahead of script, so glass drawn on a fixed canvas
// trails its element by a frame or more. The fixed scene canvas carries only
// the glass that is fixed as well (the bar, its menu, the toast); the rest is
// drawn on a transparent canvas placed in the document, which the compositor
// scrolls together with the content.
// A damped spring toward target; returns whether it is still moving.
function spring(s, target, stiffness = .16, damping = .72) {
s.v = (s.v + (target - s.x) * stiffness) * damping;
s.x += s.v;
if (Math.abs(s.v) < 1e-3 && Math.abs(target - s.x) < 1e-3) { s.x = target; s.v = 0; return false; }
return true;
}
// Three canvases. Browsers scroll on the compositor ahead of script, so
// glass drawn on a fixed canvas trails a scrolling element, and glass in the
// document trails a fixed one. The scene canvas is fixed; the page container
// is a transparent canvas placed in the document; the bar container is a
// small fixed canvas above it that holds the bar, its menu and the button.
function shaderScene(scene, art, state) {
const fixedCanvas = document.createElement("canvas");
const sceneCanvas = document.createElement("canvas");
const pageCanvas = document.createElement("canvas");
let fixedRender, pageRender;
const barCanvas = document.createElement("canvas");
let sceneRender, pageRender, barRender;
try {
fixedRender = glassContext(fixedCanvas, art, false);
pageRender = fixedRender && glassContext(pageCanvas, art, true);
sceneRender = glassContext(sceneCanvas, art, true);
pageRender = sceneRender && glassContext(pageCanvas, art, false);
barRender = pageRender && glassContext(barCanvas, art, false);
} catch (error) {
console.warn("liquid glass disabled:", error.message);
return false;
}
if (!fixedRender || !pageRender) return false;
if (!barRender) return false;
fixedCanvas.className = "shader";
scene.prepend(fixedCanvas);
sceneCanvas.className = "shader";
scene.prepend(sceneCanvas);
const pageLayer = el("div", { class: "glass-layer" });
const barLayer = el("div", { class: "glass-top" });
pageLayer.append(pageCanvas);
barLayer.append(barCanvas);
scene.after(pageLayer);
pageLayer.after(barLayer);
scene.classList.add("gl");
document.documentElement.classList.add("gl");
PREFS.apply();
const list = () => ({ rects: new Float32Array(MAX_GLASS * 4), metas: new Float32Array(MAX_GLASS * 4), count: 0 });
const fixedList = list(), pageList = list();
const hovered = new WeakMap(), fixedOf = new WeakMap();
const pageList = list(), barList = list();
const motion = new WeakMap(), inBar = new WeakMap();
let radii = new WeakMap();
addEventListener("resize", () => { radii = new WeakMap(); });
let last = 0, quality = 1, slow = 0;
const push = (into, r, meta, ratio) => {
// Activity: input keeps full frame rate for a moment; idle drops to ~20 fps.
let active = 0, pressed = null;
const wake = () => { active = performance.now(); };
for (const type of ["scroll", "wheel", "pointermove", "pointerdown", "pointerup", "keydown", "resize", "touchmove"]) addEventListener(type, wake, { passive: true });
addEventListener("pointerdown", e => { pressed = e.target.closest?.(PRESSABLE) || null; }, { passive: true });
for (const type of ["pointerup", "pointercancel", "blur"]) addEventListener(type, () => { pressed = null; }, { passive: true });
// The language menu's glass grows out of its button and retracts into it;
// a highlight springs between the options.
const button = $("#lang"), menu = $("#lang-menu");
const grow = { x: 0, v: 0 };
const glow = { x: 0, y: 0, w: 0, h: 0, vx: 0, vy: 0, vw: 0, vh: 0, on: false };
let menuBox = null;
const push = (into, x, y, w, h, meta) => {
if (into.count >= MAX_GLASS) return;
into.rects.set([r.left * ratio, r.top * ratio, r.width * ratio, r.height * ratio], into.count * 4);
into.rects.set([x, y, w, h], into.count * 4);
into.metas.set(meta, into.count * 4);
into.count++;
};
// Per element: hover and press levels on springs, and a stretch along its
// own motion (scrolling excluded), like a drop that lags behind.
function animate(node, r, fixed) {
let m = motion.get(node);
const cx = r.left + r.width / 2 + (fixed ? 0 : scrollX), cy = r.top + r.height / 2 + (fixed ? 0 : scrollY);
if (!m) motion.set(node, m = { cx, cy, sx: 0, sy: 0, hover: { x: 0, v: 0 }, press: { x: 0, v: 0 } });
const { tx, ty, active: on } = state.pointer;
const inside = on && tx >= r.left && tx <= r.right && ty >= r.top && ty <= r.bottom;
let moving = spring(m.hover, inside ? 1 : 0, .18, .7);
moving = spring(m.press, pressed === node || pressed?.closest(".filter") && node.matches(".thumb") ? 1 : 0, .22, .62) || moving;
m.sx += (Math.abs(cx - m.cx) - m.sx) * .3;
m.sy += (Math.abs(cy - m.cy) - m.sy) * .3;
m.cx = cx; m.cy = cy;
if (m.sx + m.sy > .05) moving = true;
return { m, moving };
}
function collect(ratio, vh, margin) {
fixedList.count = pageList.count = 0;
const { tx, ty, active } = state.pointer;
pageList.count = barList.count = 0;
let moving = false, barBox = null;
for (const [selector, kind] of GLASS) {
for (const node of $$(selector)) {
const r = node.getBoundingClientRect();
if (r.width < 2 || r.height < 2 || r.bottom < -margin - 60 || r.top > vh + margin + 60) continue;
if (!radii.has(node)) radii.set(node, parseFloat(getComputedStyle(node).borderTopLeftRadius) || 0);
if (!fixedOf.has(node)) fixedOf.set(node, !!node.closest(".bar, .toast"));
const inside = active && tx >= r.left && tx <= r.right && ty >= r.top && ty <= r.bottom;
const level = (hovered.get(node) || 0) + ((inside ? 1 : 0) - (hovered.get(node) || 0)) * .18;
hovered.set(node, level);
if (!inBar.has(node)) inBar.set(node, !!node.closest(".bar"));
const fixed = inBar.get(node) || node.matches(".toast");
const { m, moving: busy } = animate(node, r, fixed);
moving = moving || busy;
const pill = kind === 1 || kind === 2;
// Pills swell when pressed; moving pills stretch along their path.
const swell = 1 + (pill ? .12 : .02) * m.press.x;
const ax = pill ? Math.min(m.sx * .03, .4) : 0, ay = pill ? Math.min(m.sy * .03, .4) : 0;
const w = r.width * swell * (1 + ax - ay * .4), h = r.height * swell * (1 + ay - ax * .4);
const x = r.left + (r.width - w) / 2, y = r.top + (r.height - h) / 2;
const layer = kind === 0 || kind === 4 ? 0 : !node.parentElement.closest(".glass") ? 0 : kind === 3 ? 1 : 2;
const radius = Math.min(radii.get(node), r.width / 2, r.height / 2) * ratio;
if (!fixedOf.get(node)) { push(pageList, r, [radius, kind, level, layer], ratio); continue; }
push(fixedList, r, [radius, kind, level, layer], ratio);
// The page layer fuses with the bar and leaves holes for the menu and toast.
if (kind === 4) push(pageList, r, [radius, 4, 0, 0], ratio);
else if (layer === 0) push(pageList, r, [radius, 5, 0, 0], ratio);
const radius = Math.min(radii.get(node), w / 2, h / 2) * ratio;
const meta = [radius, kind, m.hover.x + m.press.x, layer];
if (kind === 4) barBox = r;
if (!inBar.get(node)) { push(pageList, x * ratio, y * ratio, w * ratio, h * ratio, meta); continue; }
push(barList, x * ratio, y * ratio, w * ratio, h * ratio, meta);
// The page container fuses with the bar, which its own container draws.
if (kind === 4) push(pageList, x * ratio, y * ratio, w * ratio, h * ratio, [radius, 4, 0, 0]);
}
}
// Menu glass: from the button to the open menu on a spring with overshoot.
const open = !menu.hidden;
if (open) { const r = menu.getBoundingClientRect(); menuBox = { x: r.left, y: r.top, w: r.width, h: r.height }; }
moving = spring(grow, open ? 1 : 0, .14, .7) || moving;
let region = barBox ? { x: barBox.left, y: barBox.top, w: barBox.width, h: barBox.height } : null;
if (menuBox && grow.x > .01) {
const b = button.getBoundingClientRect(), p = grow.x, q = Math.min(p, 1);
const x = b.left + (menuBox.x - b.left) * p, y = b.top + (menuBox.y - b.top) * p;
const w = b.width + (menuBox.w - b.width) * p, h = b.height + (menuBox.h - b.height) * p;
const radius = (b.height / 2 + (22 - b.height / 2) * q) * ratio;
push(barList, x * ratio, y * ratio, w * ratio, h * ratio, [radius, 0, 0, 0]);
region = region ? union(region, { x, y, w, h }) : { x, y, w, h };
// The highlight under the active option.
const li = open && (menu.querySelector("li.active") || menu.querySelector('li[aria-selected="true"]'));
if (li && p > .55) {
const r = li.getBoundingClientRect();
if (!glow.on) Object.assign(glow, { x: r.left, y: r.top, w: r.width, h: r.height, on: true });
for (const [key, value] of [["x", r.left], ["y", r.top], ["w", r.width], ["h", r.height]]) {
const s = { x: glow[key], v: glow["v" + key] };
moving = spring(s, value, .2, .66) || moving;
glow[key] = s.x; glow["v" + key] = s.v;
}
const stretch = Math.min(Math.abs(glow.vy) * .04, .3);
const gh = glow.h * (1 + stretch), gw = glow.w * (1 - stretch * .3);
push(barList, (glow.x + (glow.w - gw) / 2) * ratio, (glow.y + (glow.h - gh) / 2) * ratio, gw * ratio, gh * ratio, [gh / 2 * ratio, 1, 0, 2]);
} else glow.on = false;
} else glow.on = false;
return { moving, region };
}
const union = (a, b) => {
const x = Math.min(a.x, b.x), y = Math.min(a.y, b.y);
return { x, y, w: Math.max(a.x + a.w, b.x + b.w) - x, h: Math.max(a.y + a.h, b.y + b.h) - y };
};
let last = 0, lastDraw = 0, quality = 1, slow = 0, wasMoving = true;
function draw(now) {
requestAnimationFrame(draw);
if (document.hidden) { last = 0; return; }
if (last && now - last > 30) { if (++slow > 40 && quality > .55) { quality *= .85; slow = 0; } } else slow = Math.max(0, slow - 1);
last = now;
// Idle: nothing moves but the scene's slow light, so redraw sparingly.
const idle = !wasMoving && now - active > 700;
if (idle && now - lastDraw < 48) return;
if (!idle) {
if (last && now - last > 30) { if (++slow > 40 && quality > .55) { quality *= .85; slow = 0; } } else slow = Math.max(0, slow - 1);
}
last = now; lastDraw = now;
const ratio = Math.min(devicePixelRatio || 1, 1.5) * quality;
// The fixed scene excludes the scrollbar; innerWidth would stretch the canvas.
const vw = scene.clientWidth || innerWidth, vh = scene.clientHeight || innerHeight;
// Rows beyond the viewport cover the frames the compositor runs ahead.
const margin = Math.round(Math.min(260, vh * .3));
if (PREFS.solid) fixedList.count = pageList.count = 0;
else collect(ratio, vh, margin);
let region = null;
if (PREFS.solid) { pageList.count = barList.count = 0; wasMoving = false; }
else ({ moving: wasMoving, region } = collect(ratio, vh, margin));
const r = state.rect;
const view = { x: 0, y: 0, w: vw, h: vh };
const frame = {
ratio, vw, vh, off: 0, time: PREFS.calm ? 0 : now / 1000, rect: r, dim: state.dim,
mouse: [state.pointer.tx, state.pointer.ty], light: [r.x + .46 * r.w, r.y + .565 * r.h], list: fixedList,
ratio, view: [vw, vh], time: PREFS.calm ? 0 : now / 1000, rect: r, dim: state.dim,
mouse: [state.pointer.tx, state.pointer.ty], light: [r.x + .46 * r.w, r.y + .565 * r.h],
};
fixedRender(frame);
pageRender({ ...frame, off: margin, list: pageList });
sceneRender({ ...frame, canvas: view });
pageRender({ ...frame, canvas: { x: 0, y: -margin, w: vw, h: vh + 2 * margin }, fbo: view, list: pageList, cutBar: true });
const style = pageCanvas.style;
style.width = `${vw}px`;
style.height = `${vh + 2 * margin}px`;
// Measured, not scrollY: the layer's box may start below the page top (collapsed margins).
style.transform = `translate3d(0, ${-margin - pageLayer.getBoundingClientRect().top}px, 0)`;
// The bar container covers the bar, its menu and their shadow only.
if (region && barList.count) {
const pad = 28, reach = 64;
const box = { x: Math.floor(region.x - pad), y: Math.floor(Math.max(0, region.y - pad)), w: 0, h: 0 };
box.w = Math.ceil(region.w + 2 * pad); box.h = Math.ceil(region.y + region.h + pad - box.y);
barRender({ ...frame, canvas: box, fbo: { x: box.x - reach, y: box.y - reach, w: box.w + 2 * reach, h: box.h + 2 * reach }, list: barList, cutBar: false });
const s = barCanvas.style;
s.display = "";
s.width = `${box.w}px`; s.height = `${box.h}px`;
s.transform = `translate3d(${box.x}px, ${box.y}px, 0)`;
} else barCanvas.style.display = "none";
}
requestAnimationFrame(draw);
return true;
+2 -2
View File
@@ -9,8 +9,8 @@
<meta name="theme-color" content="#140a24">
<link rel="icon" href="/assets/icon.svg" type="image/svg+xml">
<link rel="preload" href="/assets/bg.webp" as="image" type="image/webp">
<link rel="stylesheet" href="/assets/app.css?v=9">
<script src="/assets/app.js?v=9" defer></script>
<link rel="stylesheet" href="/assets/app.css?v=10">
<script src="/assets/app.js?v=10" defer></script>
</head>
<body>
<div class="scene" aria-hidden="true">