Hiệu năng
Cần chú ýTốc độ khung hình: trung bình 60 fps, p5 59.9 · Tác vụ dài: 0 tác vụ, tổng 0 ms, dài nhất 0 ms · Mã nguồn gzip 8.9 KB · Khi chạy 158 KB
Glass bubbleMITHọa tiếtBencho
Component React Họa tiết của Lorenzo Cabra. Được thu thập nguyên vẹn từ Bencho theo giấy phép MIT và hiển thị trực tiếp tại đây.
Báo cáo chất lượng
Xếp loại và điểm số đến từ các kiểm tra tự động lúc dựng hoặc kiểm thử. Nếu một mục chưa chạy, nó được đánh dấu Chưa kiểm tra — không bao giờ bịa số liệu.
Tốc độ khung hình: trung bình 60 fps, p5 59.9 · Tác vụ dài: 0 tác vụ, tổng 0 ms, dài nhất 0 ms · Mã nguồn gzip 8.9 KB · Khi chạy 158 KB
Vấn đề 0 · Trình đọc màn hình: Đạt
Không có chuyển động liên tục
Không tràn
Số liệu tốc độ khung hình và tác vụ dài được đo trong Chromium không giao diện với WebGL phần mềm. Chúng chỉ nên được so sánh giữa các thành phần trên trang này, và không đại diện cho thiết bị thật.
Không áp dụng: không có hoạt ảnh nào thấy được khi đứng yên. Hoạt ảnh kích hoạt khi di chuột chưa được kiểm tra.
Không có vi phạm axe-core.

Kiểm tra ngày 15 thg 11, 2026 · Số liệu đến từ tập lệnh tự động, không phải mẫu viết tay.
Hiển thị đúng kích thước thật.
Bản sao bắt đầu bằng thông báo giấy phép. Hãy giữ lại khi dùng mã.
<div id="root"></div>:root{--bg:#f6f4f0;--ink:#17181a;--background:40 20% 96%;--foreground:24 10% 8%;--card:0 0% 100%}html,body{min-height:100%;margin:0;background:var(--bg);color:var(--ink)}html,body{height:100%;overflow:hidden}body{display:grid;place-items:center}#root{display:grid;place-items:center;max-width:100%;max-height:100%;box-sizing:border-box;padding:16px;color:hsl(var(--foreground))}#root.full{width:100%;height:100%;padding:0}@media(prefers-reduced-motion:reduce){*,*::before,*::after{animation-play-state:paused!important;transition-duration:0s!important}}
/*
MIT License
Copyright (c) 2026 Lorenzo Cabra
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
Not covered by this licence: the Bencho name and the goat mark, and the
photographs shown on bencho.dev (none are included here). Third-party
material keeps its own licence; see THIRD_PARTY.md.
Icon substitution (this copy only): lucide-react (ISC) was replaced with @tabler/icons-react (MIT).
Sample photographs were replaced with Incidental AI assets. The Bencho name and goat mark are not licensed for marketing use.
*/
/* ── Bencho tokens ─────────────────────────────────────────
The custom properties the free blocks read, at the values
bencho.dev draws them with. Light by default; dark under
[data-theme="dark"] on any ancestor, or automatically with
the system setting unless [data-theme="light"] says otherwise.
Map these to your own theme rather than keeping two: a block
only ever reads the names, never the values. */
:root {
--font-ui: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
--bg: #ffffff;
--board: #ffffff;
--card: #f7f7f6;
--surface: #f7f7f6;
--surface-2: #f1f1f0;
--surface-3: #ecebe7;
--ink: #17181a;
--ink-rgb: 23, 24, 26;
--ink-2: #3c3b37;
--ink-3: #5c5b56;
--ink-4: #6f6e68;
--ink-5: #85847e;
--on-ink: #ffffff;
--on-ink-rgb: 255, 255, 255;
--slab: #17181a;
--on-slab: #ffffff;
--slab-dark: #0e0f11;
--fill-slab: #ffffff;
--fill-on: #17181a;
--fill-on-rgb: 23, 24, 26;
--pane: var(--fill-slab);
--pane-edge: rgba(23, 24, 26, 0.08);
--pane-thumb: rgba(23, 24, 26, 0.07);
--inlay: rgba(255, 255, 255, 0.55);
--inlay-hi: rgba(23, 24, 26, 0.09);
--elev-rgb: 255, 255, 255;
--glass-rgb: 255, 255, 255;
--shadow-rgb: 23, 24, 26;
--wash: 0.06;
--pick: #0d99ff;
--signal: #2231dd;
--focus: #2231dd;
--ok: #15803d;
--ember: #b8431c;
color-scheme: light;
}
[data-theme="dark"] {
--bg: #000000;
--board: #1a1a1a;
--card: #0a0a0a;
--surface: #0f0f0f;
--surface-2: #181818;
--surface-3: #202020;
--ink: #ffffff;
--ink-rgb: 255, 255, 255;
--ink-2: #b4b4b4;
--ink-3: #9a9a9a;
--ink-4: #828282;
--ink-5: #6b6b6b;
--on-ink: #000000;
--on-ink-rgb: 0, 0, 0;
--slab: #1a1a1a;
--on-slab: #ffffff;
--slab-dark: #262626;
--fill-slab: #262626;
--fill-on: #ffffff;
--fill-on-rgb: 255, 255, 255;
--pane-edge: rgba(255, 255, 255, 0.08);
--pane-thumb: rgba(255, 255, 255, 0.07);
--inlay: rgba(255, 255, 255, 0.1);
--inlay-hi: rgba(255, 255, 255, 0.09);
--elev-rgb: 32, 32, 32;
--shadow-rgb: 0, 0, 0;
--wash: 0.09;
--pick: #3aa9ff;
--ok: #34d17a;
color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #000000;
--board: #1a1a1a;
--card: #0a0a0a;
--surface: #0f0f0f;
--surface-2: #181818;
--surface-3: #202020;
--ink: #ffffff;
--ink-rgb: 255, 255, 255;
--ink-2: #b4b4b4;
--ink-3: #9a9a9a;
--ink-4: #828282;
--ink-5: #6b6b6b;
--on-ink: #000000;
--on-ink-rgb: 0, 0, 0;
--slab: #1a1a1a;
--on-slab: #ffffff;
--slab-dark: #262626;
--fill-slab: #262626;
--fill-on: #ffffff;
--fill-on-rgb: 255, 255, 255;
--pane-edge: rgba(255, 255, 255, 0.08);
--pane-thumb: rgba(255, 255, 255, 0.07);
--inlay: rgba(255, 255, 255, 0.1);
--inlay-hi: rgba(255, 255, 255, 0.09);
--elev-rgb: 32, 32, 32;
--shadow-rgb: 0, 0, 0;
--wash: 0.09;
--pick: #3aa9ff;
--ok: #34d17a;
color-scheme: dark;
}
}
/* LiquidGlass.css */
/*
MIT License
Copyright (c) 2026 Lorenzo Cabra
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
Not covered by this licence: the Bencho name and the goat mark, and the
photographs shown on bencho.dev (none are included here). Third-party
material keeps its own licence; see THIRD_PARTY.md.
Icon substitution (this copy only): lucide-react (ISC) was replaced with @tabler/icons-react (MIT).
Sample photographs were replaced with Incidental AI assets. The Bencho name and goat mark are not licensed for marketing use.
*/
/* ══ Liquid glass ══════════════════════════════════════════
lab/LiquidGlass.tsx. The photograph, the margin round it and
the glass are one WebGL canvas; the margin is painted in the
card's colour, so the canvas has no edge of its own. */
.lqg {
position: relative;
display: grid;
place-items: center;}
.lqg-canvas {
display: block;
touch-action: none;
-webkit-user-select: none;
user-select: none;}
.lqg-canvas[data-over="true"] { cursor: grab; }
.lqg-canvas[data-over="true"]:active { cursor: grabbing; }
// bencho source (MIT). npm i motion react · Tailwind CSS v4
// ── blocks/glass-bubble/LiquidGlass.tsx (https://github.com/cabralorenzo/bencho/blob/c65992417427bb09e661674ffa800bef29adef49/blocks/glass-bubble/LiquidGlass.tsx)
/*
MIT License
Copyright (c) 2026 Lorenzo Cabra
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
Not covered by this licence: the Bencho name and the goat mark, and the
photographs shown on bencho.dev (none are included here). Third-party
material keeps its own licence; see THIRD_PARTY.md.
Icon substitution (this copy only): lucide-react (ISC) was replaced with @tabler/icons-react (MIT).
Sample photographs were replaced with Incidental AI assets. The Bencho name and goat mark are not licensed for marketing use.
*/
import { useEffect, useRef, useState } from "react";
/* ── inlined from lab/spring ──────────────────────── */
/* ── one spring, for everything that settles ───────────────
The maths was already on this bench twice, copied by hand:
Humidity's wheel and Brightness's column both accumulate
velocity toward a target, damp it, and snap when both the
delta and the velocity fall under 0.02. Two copies is a
coincidence; five would be a policy, so it comes out here
before the elastic blocks are written against it.
The two shipped copies are deliberately NOT refactored onto
this. They work, they are tuned, and rewriting the innards
of two live components to prove a point about duplication
is how a good afternoon becomes a bad one. This is the one
new code uses.
Frames, not milliseconds. `dt` is expressed in sixtieths of
a second and the damping is RAISED to it rather than
multiplied by it, so a dropped frame decays the same amount
of energy as the two frames it replaced. Multiplying is the
version that makes a spring behave differently on a busy
page, which is the hardest kind of bug to see.
The loop parks itself the moment the value has settled.
CLAUDE.md is not complimentary about the one permanent
requestAnimationFrame already on this bench and there is no
case for five more. */
/* Read once, the way the wheel and the pill nav do. A
preference, not a live input. */
const stillness = () =>
typeof window !== "undefined" &&
!!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
/* A drawn placeholder: Bencho's photographs are licensed to
the site and do not travel. Point this at your own. */
const COW: string = "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20800%22%20width%3D%22800%22%20height%3D%22800%22%20preserveAspectRatio%3D%22xMidYMid%20slice%22%3E%0A%3Cdefs%3E%3ClinearGradient%20id%3D%22s%22%20x1%3D%220%22%20y1%3D%220%22%20x2%3D%220%22%20y2%3D%221%22%3E%3Cstop%20offset%3D%220%22%20stop-color%3D%22rgb%28236%2C236%2C238%29%22%2F%3E%3Cstop%20offset%3D%221%22%20stop-color%3D%22rgb%28178%2C178%2C180%29%22%2F%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%0A%3Crect%20width%3D%22800%22%20height%3D%22800%22%20fill%3D%22url%28%23s%29%22%2F%3E%0A%3Ccircle%20cx%3D%22268%22%20cy%3D%22216%22%20r%3D%2272%22%20fill%3D%22rgb%28246%2C246%2C248%29%22%2F%3E%0A%3Cpath%20d%3D%22M0%2C800%20L0%2C503.7%20L100%2C461.7%20L200%2C424.0%20L300%2C461.1%20L400%2C503.6%20L500%2C472.1%20L600%2C425.4%20L700%2C450.8%20L800%2C500.8%20L800%2C800%20Z%22%20fill%3D%22rgb%28156%2C156%2C158%29%22%2F%3E%0A%3Cpath%20d%3D%22M0%2C800%20L0%2C606.1%20L100%2C599.8%20L200%2C568.5%20L300%2C545.1%20L400%2C554.1%20L500%2C586.1%20L600%2C607.5%20L700%2C595.8%20L800%2C563.4%20L800%2C800%20Z%22%20fill%3D%22rgb%28116%2C116%2C118%29%22%2F%3E%0A%3Cpath%20d%3D%22M0%2C800%20L0%2C694.1%20L100%2C711.8%20L200%2C688.4%20L300%2C664.3%20L400%2C681.2%20L500%2C709.9%20L600%2C700.7%20L700%2C669.6%20L800%2C670.3%20L800%2C800%20Z%22%20fill%3D%22rgb%2879%2C79%2C81%29%22%2F%3E%0A%3C%2Fsvg%3E";
/* ══ Glass bubble, liquid ═════════════════════════════════
A ball of glass you drag over a photograph. It bends what is
under it — a little bigger in the middle, hard at the rim —
splits the colour a hair at the very edge, catches the light
along its top, and can be carried off the picture onto the
card, where there is nothing to bend and it is only light and
shadow.
── A SHADER, NOT A FILTER ──────────────────────────────
The first Glass bubble was an SVG displacement map through a
backdrop filter, and it is still there for the Lens page.
What it could not do is know where its own edge was: a map
is a picture, drawn once, so the rim was a band of numbers
rather than a surface. Here the page is a texture and every
pixel asks a signed distance function how far it is from
the capsule's edge, which is what lets the bend, the rim
light and the shadow all follow the SAME edge exactly —
including while the capsule stretches under a fast drag.
── THE RIM READS OUTWARD ───────────────────────────────
Near the edge each pixel samples from further out along the
edge's normal, so the band round the rim shows what is just
outside it, gathered in. That is what makes text curve at
the edge of real glass. The middle only magnifies. */
/* ── the picture, and the room round it ─────────────────
The photograph is square and the ball has the run of a
margin on every side, so it can be carried half off the
picture onto the card — the edge is something you cross, not
a wall. The margin is part of the canvas and left clear, so
the card itself shows through it. */
const PIC = 440;
const PAD = 90;
const W = PIC + PAD * 2;
const H = W;
/* the photograph's corner at rest; the Corner knob sets it */
const PIC_R = 20;
const VERT = `
attribute vec2 pos;
varying vec2 vUv;
void main() {
vUv = vec2(pos.x * 0.5 + 0.5, 0.5 - pos.y * 0.5);
gl_Position = vec4(pos, 0.0, 1.0);
}`;
const FRAG = `
precision highp float;
uniform sampler2D tex;
uniform vec2 res;
uniform vec2 c;
uniform vec2 b;
uniform float r;
uniform float bend;
uniform float fringe;
uniform float dark;
varying vec2 vUv;
float sdf(vec2 p) {
vec2 q = abs(p - c) - b + vec2(r);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}
/* premultiplied: the margin round the picture is transparent,
so the real card shows through it and the glass over it bends
nothing — only its light and its shadow are left */
vec4 read(vec2 p) {
return texture2D(tex, clamp(p / res, 0.0, 1.0));
}
void main() {
vec2 p = vUv * res;
float d = sdf(p);
vec4 col = read(p);
/* what it casts: soft, a little below. It darkens the picture
where there is one and is a shadow of its own where there
is not. */
float ds = sdf(p - vec2(0.0, 9.0));
float sh = (1.0 - smoothstep(-8.0, 26.0, ds)) * mix(0.14, 0.3, dark);
if (d > 0.0) col = vec4(col.rgb * (1.0 - sh), col.a) + vec4(0.0, 0.0, 0.0, sh * (1.0 - col.a));
if (d < 1.0) {
float e = 1.5;
vec2 n = normalize(vec2(
sdf(p + vec2(e, 0.0)) - sdf(p - vec2(e, 0.0)),
sdf(p + vec2(0.0, e)) - sdf(p - vec2(0.0, e))
) + 1e-5);
/* the bending band scales with the ball, so a big one is
still a lens and a small one is not all rim */
float bevel = min(b.y, max(22.0, b.y * 0.36));
/* 0 at the rim, 1 once past the bevel */
float t = clamp(-d / bevel, 0.0, 1.0);
float edge = pow(1.0 - t, 2.4);
vec2 mag = c + (p - c) / 1.1;
vec2 off = n * edge * bevel * 1.1 * bend;
vec2 q = mag + off;
vec4 mid = read(q);
vec4 g = vec4(read(q + off * fringe).r, mid.g, read(q - off * fringe).b, mid.a);
/* the light: a thin lit band just inside the edge, bright
where it faces up-left, a weaker catch on the far side
where light has come through and out again */
vec2 L = normalize(vec2(-0.55, -0.85));
float rim = smoothstep(0.0, 1.6, -d) * (1.0 - smoothstep(1.6, 4.5, -d));
float lit = max(dot(n, L), 0.0);
float far = max(dot(n, -L), 0.0);
g.rgb *= mix(1.03, 1.08, dark);
/* light is added as light: colour and coverage together, so
it shows on the card as well as on the picture */
float light = rim * (0.18 + 0.75 * lit) + rim * far * 0.22 + edge * mix(0.05, 0.08, dark);
g += vec4(vec3(light), light);
g = clamp(g, 0.0, 1.0);
float a = 1.0 - smoothstep(-0.6, 0.6, d);
col = mix(col, g, a);
}
gl_FragColor = col;
}`;
const clamp = (v: number, a: number, b: number) => Math.min(b, Math.max(a, v));
/* ── light or dark, read off the page ────────────────────
The theme lives on <html data-theme>, so that is where this
looks — not at the site's store, which keeps the block whole
when it is copied out into somebody else's app. */
function useDark() {
const read = () => document.documentElement.dataset.theme === "dark";
const [dark, setDark] = useState(read);
useEffect(() => {
const mo = new MutationObserver(() => setDark(read()));
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
return () => mo.disconnect();
}, []);
return dark;
}
function compile(gl: WebGLRenderingContext) {
const sh = (type: number, src: string) => {
const s = gl.createShader(type)!;
gl.shaderSource(s, src);
gl.compileShader(s);
return s;
};
const prog = gl.createProgram()!;
gl.attachShader(prog, sh(gl.VERTEX_SHADER, VERT));
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FRAG));
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) return null;
return prog;
}
export function LiquidGlass({
/* the ball's diameter, px */
size = 180,
/* how hard the rim bends, 0..100 */
bend = 60,
/* how far the colours part at the edge, 0..40 */
fringe = 30,
/* the photograph's corner, px */
corner = PIC_R,
}: {
size?: number;
bend?: number;
fringe?: number;
corner?: number;
} = {}) {
const dark = useDark();
const still = stillness();
const cv = useRef<HTMLCanvasElement | null>(null);
const gl = useRef<{ g: WebGLRenderingContext; prog: WebGLProgram; tex: WebGLTexture } | null>(null);
const page = useRef<HTMLCanvasElement | null>(null);
const raf = useRef(0);
/* it starts on the picture, up and right of the middle */
const state = useRef({ x: W * 0.6, y: H * 0.42, vx: 0, vy: 0, tx: W * 0.6, ty: H * 0.42 });
const grab = useRef<{ dx: number; dy: number } | null>(null);
const knobs = useRef({ size, bend, fringe, dark });
knobs.current = { size, bend, fringe, dark };
/* ── one frame ─────────────────────────────────────────── */
const draw = () => {
const G = gl.current;
const c = cv.current;
if (!G || !c) return;
const { g, prog } = G;
const k = c.width / W;
const s = state.current;
const { size: sz, bend: bd, fringe: fr, dark } = knobs.current;
/* a fast drag stretches it along the way it is going and
thins it across — the same volume, moving */
const sp = Math.hypot(s.vx, s.vy);
const stretch = still ? 0 : Math.min(0.14, sp * 0.012);
const ax = sp > 0.01 ? Math.abs(s.vx) / sp : 1;
const rad = clamp(sz, 100, 280) / 2;
const bx = rad * (1 + stretch * ax - stretch * 0.5 * (1 - ax));
const by = rad * (1 + stretch * (1 - ax) - stretch * 0.5 * ax);
g.viewport(0, 0, c.width, c.height);
g.useProgram(prog);
const u = (n: string) => g.getUniformLocation(prog, n);
g.uniform2f(u("res"), c.width, c.height);
g.uniform2f(u("c"), s.x * k, s.y * k);
g.uniform2f(u("b"), bx * k, by * k);
g.uniform1f(u("r"), Math.min(bx, by) * k);
g.uniform1f(u("bend"), clamp(bd, 0, 100) / 100);
g.uniform1f(u("fringe"), clamp(fr, 0, 40) / 100);
g.uniform1f(u("dark"), dark ? 1 : 0);
g.drawArrays(g.TRIANGLE_STRIP, 0, 4);
};
/* ── the loop: a spring toward the hand, then stop ─────── */
const run = () => {
if (raf.current) return;
let prev = 0;
const tick = (t: number) => {
const dt = prev ? clamp((t - prev) / 16.67, 0, 2.5) : 1;
prev = t;
const s = state.current;
if (still) { s.x = s.tx; s.y = s.ty; s.vx = 0; s.vy = 0; }
else {
const k = 0.22;
const d = Math.pow(0.66, dt);
s.vx = (s.vx + (s.tx - s.x) * k * dt) * d;
s.vy = (s.vy + (s.ty - s.y) * k * dt) * d;
s.x += s.vx * dt;
s.y += s.vy * dt;
}
draw();
const moving = Math.abs(s.tx - s.x) > 0.05 || Math.abs(s.ty - s.y) > 0.05
|| Math.abs(s.vx) > 0.05 || Math.abs(s.vy) > 0.05 || grab.current;
raf.current = moving ? requestAnimationFrame(tick) : 0;
};
raf.current = requestAnimationFrame(tick);
};
/* ── the page under the glass ──────────────────────────── */
const paint = async () => {
const c = cv.current;
const G = gl.current;
if (!c || !G) return;
const pg = page.current ?? (page.current = document.createElement("canvas"));
pg.width = c.width;
pg.height = c.height;
const x = pg.getContext("2d")!;
const k = c.width / W;
/* the margin stays clear: the card behind the canvas is
the ground, whatever colour it is */
x.clearRect(0, 0, pg.width, pg.height);
const im = new Image();
im.src = COW;
await im.decode().catch(() => {});
/* the photograph, square and rounded, cropped to fill */
const px = PAD * k;
const ps = PIC * k;
const s = Math.max(ps / im.width, ps / im.height);
const iw = im.width * s;
const ih = im.height * s;
x.save();
x.beginPath();
x.roundRect(px, px, ps, ps, clamp(corner, 0, 60) * k);
x.clip();
x.drawImage(im, px + (ps - iw) / 2, px + (ps - ih) / 2, iw, ih);
x.restore();
const { g, tex } = G;
g.bindTexture(g.TEXTURE_2D, tex);
g.pixelStorei(g.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, g.RGBA, g.UNSIGNED_BYTE, pg);
draw();
};
/* ── set up once ───────────────────────────────────────── */
useEffect(() => {
const c = cv.current;
if (!c) return;
const dpr = Math.min(2, window.devicePixelRatio || 1);
c.width = Math.round(W * dpr);
c.height = Math.round(H * dpr);
const g = c.getContext("webgl", { antialias: true, premultipliedAlpha: true, alpha: true });
if (!g) return;
const prog = compile(g);
if (!prog) return;
const buf = g.createBuffer();
g.bindBuffer(g.ARRAY_BUFFER, buf);
g.bufferData(g.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), g.STATIC_DRAW);
const loc = g.getAttribLocation(prog, "pos");
g.enableVertexAttribArray(loc);
g.vertexAttribPointer(loc, 2, g.FLOAT, false, 0, 0);
const tex = g.createTexture()!;
g.bindTexture(g.TEXTURE_2D, tex);
g.texParameteri(g.TEXTURE_2D, g.TEXTURE_MIN_FILTER, g.LINEAR);
g.texParameteri(g.TEXTURE_2D, g.TEXTURE_MAG_FILTER, g.LINEAR);
g.texParameteri(g.TEXTURE_2D, g.TEXTURE_WRAP_S, g.CLAMP_TO_EDGE);
g.texParameteri(g.TEXTURE_2D, g.TEXTURE_WRAP_T, g.CLAMP_TO_EDGE);
gl.current = { g, prog, tex };
/* NOT loseContext() here. StrictMode runs this cleanup and
then the setup again on the SAME canvas, and a context
that has been lost comes straight back from getContext
still lost — a sad-face canvas. The browser frees it with
the element. */
return () => {
cancelAnimationFrame(raf.current);
raf.current = 0;
gl.current = null;
};
}, []);
/* the page repaints when what is on it changes */
useEffect(() => { paint(); }, [dark, corner]);
/* the glass redraws when a knob moves */
useEffect(() => { draw(); }, [size, bend, fringe]);
/* ── the hand ──────────────────────────────────────────── */
const at = (e: React.PointerEvent) => {
const r = e.currentTarget.getBoundingClientRect();
const k = r.width / W;
return { x: (e.clientX - r.left) / k, y: (e.clientY - r.top) / k };
};
const onGlass = (p: { x: number; y: number }) => {
const s = state.current;
return Math.hypot(p.x - s.x, p.y - s.y) <= clamp(size, 100, 280) / 2 + 4;
};
return (
<div className="lqg">
<canvas
ref={cv}
className="lqg-canvas"
style={{ width: W, height: H }}
aria-label="A ball of glass over a photograph. Drag it."
role="img"
onPointerDown={(e) => {
const p = at(e);
if (!onGlass(p)) return;
const s = state.current;
grab.current = { dx: p.x - s.x, dy: p.y - s.y };
try { e.currentTarget.setPointerCapture(e.pointerId); } catch { /* scripted */ }
run();
}}
onPointerMove={(e) => {
const p = at(e);
const gr = grab.current;
e.currentTarget.dataset.over = onGlass(p) || gr ? "true" : "false";
if (!gr) return;
const s = state.current;
s.tx = clamp(p.x - gr.dx, 0, W);
s.ty = clamp(p.y - gr.dy, 0, H);
run();
}}
onPointerUp={() => {
if (!grab.current) return;
grab.current = null;
run();
}}
onPointerCancel={() => { grab.current = null; run(); }}
/>
</div>
);
}Điều khiển · HTML · CSS · JS
Sửa mã và tinh chỉnh bằng các nút điều khiển. Bản xem trước cập nhật khi bạn gõ.

Thành phần này chạy từ bản đóng gói nên mã script chỉ xem được ở đây. Hãy sửa HTML và CSS; mã nguồn đầy đủ nằm trong thẻ Mã.
Sửa trực tiếp mọi thành phần, tinh chỉnh bằng nút điều khiển, tải mã nguồn đầy đủ và sao chép prompt nâng cao.
$49 một lầnGiá tạm thời
Chế độ thử: không thu tiền thật.
Miễn phí: xem trước, nút sao chép và ghi chú bài học.
Hoạ tiết lặp lại thêm chất liệu mà không tranh chỗ với chữ.
Nền phần mở đầu, vạch chia mục và vùng trống.
Đổi cỡ ô, giảm độ tương phản và chọn hai màu.
Thêm component "Glass bubble" từ Bencho (github.com/cabralorenzo/bencho) (MIT) vào ứng dụng React + Tailwind CSS của tôi. Tệp nguồn: https://github.com/cabralorenzo/bencho/blob/c65992417427bb09e661674ffa800bef29adef49/blocks/glass-bubble/LiquidGlass.tsx. Hoạ tiết lặp lại thêm chất liệu mà không tranh chỗ với chữ. Dùng cho: Nền phần mở đầu, vạch chia mục và vùng trống. Các thiết lập cần chỉnh: Đổi cỡ ô, giảm độ tương phản và chọn hai màu. Hỗ trợ giao diện sáng và tối, tôn trọng prefers-reduced-motion. Giữ thông báo giấy phép MIT.
Prompt dài hơn, có các bước tinh chỉnh, trợ năng và tích hợp.