Community图像github.com

GaoYangHeng/liquid-glass-craft

From-scratch method for building real Apple Liquid Glass on the web: SVG-filter refraction driven by a rounded-rect SDF, Snell's law displacement maps, cursor-tracked Fresnel specular glare, transparency calibration and 3D depth. Use when asked for 液态玻璃 / Liquid Glass / Apple Liquid Glass / 玻璃拟态 / glassmorphism / 磨砂玻璃 / 毛玻璃 / frosted glass / acrylic 半透明面板 / 玻璃按钮、玻璃播放条、玻璃侧栏、玻璃弹层, when an existing glass UI looks like frosted plastic, has a glued-on white halo or a fixed white edge, is not transparent enough, or lacks 3D depth, and when tuning refraction strength, glass transparency, edge refraction or highlight intensity.

liquid-glass-craft 是什么?

liquid-glass-craft is a Cursor agent skill that from-scratch method for building real Apple Liquid Glass on the web: SVG-filter refraction driven by a rounded-rect SDF, Snell's law displacement maps, cursor-tracked Fresnel specular glare, transparency calibration and 3D depth. Use when asked for 液态玻璃 / Liquid Glass / Apple Liquid Glass / 玻璃拟态 / glassmorphism / 磨砂玻璃 / 毛玻璃 / frosted glass / acrylic 半透明面板 / 玻璃按钮、玻璃播放条、玻璃侧栏、玻璃弹层, when an existing glass UI looks like frosted plastic, has a glued-on white halo or a fixed white edge, is not transparent enough, or lacks 3D depth, and when tuning refraction strength, glass transparency, edge refraction or highlight intensity.

兼容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/GaoYangHeng/liquid-glass-craft/tree/HEAD

Installed? Explore more 图像 skills: steipete/songsee, affaan-m/frontend-design, mattpocock/improve-codebase-architecture · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Liquid Glass Craft

A complete, from-scratch method for building real Liquid Glass. Not a CSS recipe collection, but a physical model + numerical implementation + tuning manual + pitfall log.

Reference implementation (index.html in the repo root, double-click to run): A dark music player interface with four glass panels — two capsules at the top, a playback bar at the bottom, and a queue overlay. Content scrolls under the glass, and the image inside the glass is recalculated every frame.


0. Read This First: Why Your Previous Attempt Wasn't Glass

Most "Liquid Glass" is actually frosted plastic. The one deciding criterion:

Real glass displaces what's behind it. Fake glass only blurs what's behind it.

  • Blur is scattering: light directions are randomized, the image degrades into a low-frequency color patch field.
  • Refraction is displacement: each pixel is moved elsewhere, the image structure is fully preserved, but geometry is altered.
  • Frosted plastic = strong scattering + zero displacement. That's why it "looks like a film" rather than "like a material."
  • Liquid Glass = weak scattering + strong edge displacement. The image barely moves in the center, and bends more sharply toward the edges.

So an executable acceptance test:

TestFrosted PlasticReal Glass
Align a straight line (table line, text stroke, cover edge) inside the glass with the same line outsideAlignable, just blurryDoes not connect — it is broken and shifted upon entry
Set blur to 0Becomes sharp but completely flatStill looks like glass because the bend remains
Move the mouse over the glassNo reaction, or a uniform white glowA small patch of light follows the cursor, deformed when near the edge

The second row is the most aggressive test. If your glass fails at blur: 0, you've actually implemented blur, and your refraction parameters are effectively zero.


1. Four-Layer Structure

A glass that can fool the eye is composed of four independent layers stacked together. Missing any one collapses the illusion.

┌─ ① Refraction Layer  backdrop-filter: url(#svg-filter)
│     Displacement map = rounded rect SDF → edge profile → Snell → RGB encoding
│     Responsible for: displacing the background. This layer determines "is it glass?"
├─ ② Material Layer  Thin white gradient fill (alpha 0.015–0.055)
│     Responsible for: glass being half a stop brighter than air. The difference between medium and air.
├─ ③ Reflection Layer  Pointer highlight (canvas per-pixel) + optional fixed edge highlight
│     Responsible for: the surface is alive, has light. This layer determines "is it expensive?"
└─ ④ Depth Layer  Outer drop shadow + edge darkening + inner fill gradient
      Responsible for: the glass floats, has thickness. This layer determines "is it three-dimensional?"

Full breakdown of the four layers in docs/01-what-makes-it-glass.md.


2. Mandatory Workflow (Do Not Skip Steps)

Do it in order. Each step has a verifiable output. If you skip step 2, all subsequent parameters have no reference frame.

  1. Prepare a background "worth refracting" (docs/10-workflow.md §1) When glass sits on a solid color or uniform gradient, refraction is physically invisible. You need hard-edge structures: text, covers, grid lines, light-dark boundaries. → Acceptance: Screenshot the background and identify at least 5 high-frequency edges.

  2. First, do only displacement — no blur, no decoration (docs/03-displacement-map.md) blur = 0, fill 0, shadow 0. At this point the glass should look "a bit dirty, a bit wobbly," but edges are clearly bending. → Acceptance: With blur: 0, straight lines inside and outside the glass do not connect. Do not proceed if this step fails.

  3. Add material fill (docs/05-transparency.md) Start from 0.03 and increase slowly until you "can tell there's something there." Above 0.06 it becomes a plastic plate. → Acceptance: The boundary between glass and background is visible, but you can still recognize the content behind it.

  4. Add blur (docs/04-parameters.md — blur section) Start from 0.11 (5% of the max value of 2.2). This amount is so small it's almost unmeasurable; its function is to soften pixel-level hard edges, not to "blur." In most cases, this is enough — transparency is more important than "thickness." Max value is 2.2; above 0.2, you begin sacrificing transparency. → Acceptance: Text behind the glass is clearly readable but does not compete with foreground text for attention.

  5. Add depth (docs/08-3d-depth.md) Drop shadow, edge darkening, fill gradient. No white lines. → Acceptance: The glass looks "closer" than the background by one stop.

  6. Finally, add highlights (docs/07-specular-highlight.md) This layer is the easiest to mess up. It must be done last, or you will use highlights to hide defects from previous layers. → Acceptance: The light is a small patch, so subtle you "feel it but can't pinpoint it," and it deforms near the edges.

  7. Run the gates — all green means done (docs/14-gates.md)

    node scripts/gate.mjs --url file:///your/index.html --sel ".glass"
    

    All nine must PASS to ship. If any FAIL, fix according to the chapter indicated by the error. Do not lower the thresholds. → Acceptance: The terminal's last line reads "All gates passed."


3. Acceptance Gates (Hard Thresholds)

Whether the glass is correct is not decided by your eyes at this moment, but by nine numbers.

This step is not a "suggested self-check" — it is the delivery standard. Because backdrop-filter can fail silently: a single leftover animation on an ancestor makes the whole pipeline sample an empty background, the page renders fine, the console is silent, and the screenshot looks decent. You will then waste three hours tuning parameters on a broken setup.

GateWhat it checksThresholdFinal version measurement
G1Refraction channel aliveColored pixels ≥ 2% of area96.6%
G2Samples the real DOM layer (color probe)Redness delta ≥ 6165.7
G3Only edges bend (read displacement field)Edge ≥ 55/127, center ≤ 32/127120 / 18
G4No bright edge linesPeak 0–4px must not exceed interior by 843.6 vs interior 50.6
G5Transparent, not a whiteboardCenter brightness Δ ≤ 14, interior std ≥ 128.2 / 63.2
G6Glare cappedPeak alpha 5–52, zero saturated pixels, zero hard edges15 / 0 / 0
G7Glare does not overflowBright pixels outside boundary (8px) ≤ 60
G8Text readableLarge text 3.0:1, rest 2.8:13.08
G93D depth (outer drop shadow)Far region brighter than immediately below ≥ 1.52.6

Three Non-Negotiable Rules

  1. All nine must PASS to ship. One FAIL means it's not done. There is no "close enough because the rest passed."
  2. Do not lower thresholds to pass. Thresholds are reverse-engineered from real user feedback (glare alone went through five rounds of rework; gnear dropped from 1.72 to 0.18). Changing them changes the requirements.
  3. SKIP is not PASS. G6 SKIPs if no .glare canvas is found — that means the check isn't covering that element. It must be manually verified, and the reason documented in the delivery notes.

Do the Gates Actually Catch Errors?

After passing on the final version, we intentionally created three broken copies to verify:

FaultInjectionResult
WhiteboardWhite fill raised to 0.26G5 FAIL, brightness Δ = 43.3
White edgeinset 0 1px 0 rgba(255,255,255,.85)G4 FAIL, peak = 232.4
Broken pipelinebackdrop-filter: blur(9px) overriding url()G3 FAIL, reports "no url(#...) reference"

All three were caught, and the error pointed directly to the root cause. A gate that hasn't been negatively validated is as good as unwritten — you must do this step when adding your own gates.

For each gate's motivation, principle, threshold origin, FAIL remedy, and the gate's own limitations, see docs/14-gates.md.


4. Three Untouchable Red Lines

Violating any one will cause the glass to fail completely, with no error reported. All three are fundamentally in the causes of G1 failure.

  1. No ancestor of the glass element may have transform / filter / opacity < 1, including animation residues. If any does, Blink treats it as a backdrop root, and backdrop-filter samples an empty background. Even an entrance animation that settles at opacity: 1 still sets the root — once an animation is declared, it remains a backdrop root. See docs/09-pitfalls.md §1. This is the most expensive pitfall; I wasted two hours on it.

  2. backdrop-filter can only have a single filter function. url(#x) blur(4px) will be discarded entirely. All blur/saturate/color adjustments must be moved inside the SVG filter.

  3. The SVG filter region for the highlight layer must be locked to 0/0/100%/100%. An element's overflow: hidden does not constrain filter output. If the region expands, displacement pushes the light outside the glass.


5. Parameter Quick Reference

All parameters are passed via <html data-*>, with defaults on the JS side.

AttributeDefaultEffectCommon RangeConsequence of Increasing
data-edge52Maximum edge refraction strength (CSS px)40–80Edge looks cracked, like a spherical lens
data-mag8Overall magnification parallax (CSS px)6–13Glass becomes a convex lens, content shrinks and expands outward
data-thick0.32Refraction band width = half short side × factor0.28–0.42Bend zone expands, center begins to bend
data-thickmax22Max refraction band width (px)18–30Edge bend on large elements too thick, looks like a droplet
data-blur0.11Glass haze (max 2.2, i.e., 5%)0–0.15Becomes frosted, transparency disappears
data-sat1.32Saturation of content seen through glass1.2–1.7Colors too vivid, looks like a filter
data-lift1.04Contrast slope0.80–0.90 (to recede) / 1.0 (neutral)Background becomes more prominent, fights foreground
data-lift00.008Contrast intercept (raises black)0.03–0.06Overall grayish
data-spec1Fixed edge specular highlight0 or 1Uniform white edge appears (source of cheap look)
data-gnear0.18Cursor glare intensity0.18–0.25Above 0.5 becomes a blob of white
data-gfar0Backside secondary image intensity0 (default off)A second patch of light appears on the opposite side
data-glag0.20Following lag0.12–0.30Light cannot keep up with cursor

The only one sentence you need to remember from this table: data-edge decides "how glassy it is," data-gnear decides "how cheap it looks." These are the main knobs; everything else is supporting.

For each parameter's physical meaning, magnitude derivation, and failure symptoms, see docs/04-parameters.md.


6. Table of Contents

Core

  • docs/01-what-makes-it-glass.md — The essence of glass. Four physical quantities, visual criteria, why blur is not refraction.
  • docs/02-backdrop-pipeline.md — Web implementation pipeline. Capabilities and limits of backdrop-filter, full filter chain derivation.
  • docs/03-displacement-map.md — Displacement map generation. SDF, Snell derivation, numerical details, range, encoding. The longest chapter.
  • docs/04-parameters.md — Full parameter guide. Physical meaning and failure symptoms of each knob.

Material

  • docs/05-transparency.md — How transparent? Fill gradient, contrast compression, pitfalls in light mode.
  • docs/06-edge-refraction.md — Edge special topic. Why white edges are inevitably cheap, and what communicates boundaries.
  • docs/07-specular-highlight.md — Glare (not a glow halo). Per-pixel algorithm, size, presence grading, secondary images.
  • docs/08-3d-depth.md — 3D depth. Five contributors, shadow grading, light source consistency.

Engineering

  • docs/09-pitfalls.md — All pitfalls, sorted by cost. The first is the most expensive (cost two hours).
  • docs/10-workflow.md — Full from-scratch workflow, including acceptance criteria for each step.
  • docs/11-verification.md — How to verify. Pixel difference method, headless screenshot, self-check checklist.
  • docs/12-apple-principles.md — Apple's design principles for glass. Glass only for control layers, no stacked glass.
  • docs/14-gates.md — Nine acceptance gates. Each gate's motivation, threshold origin, FAIL remedy, and the gate's own limitations.

Recipes

  • docs/13-recipes.md — Scene presets. Toolbar / floating bar / sidebar / modal / round button, each with full parameter set + CSS. See this first when unsure what values to use.

Tools

  • scripts/gate.mjs — Gate script. Launches headless browser, takes screenshot, assesses each item, outputs report. Exit code 0 = all passed.
  • scripts/README.md — Script usage and parameters.

Examples

  • index.html + styles.css + glass.js — Complete runnable reference implementation, open and use (dark mode).
  • EXAMPLE.md — File structure, key code locations, how to adapt for your own use.

7. Quick Start (Copy-Paste Route)

Don't write from scratch. Copy the four files in the root (index.html / styles.css / glass.js / app.js), then modify in this order:

  1. styles.css — the .glass block. Define your shape and material. Change border-radius to decide capsule vs. card.
  2. index.html — the data-* attributes. Adjust edge and mag per §5.
  3. glass.js — No changes needed. It's pure geometric computation, unrelated to your design.
  4. app.js — only contains press, playback, progress interactions. Delete if not needed.
  5. If supporting light mode, read the last section of docs/05-transparency.md. Do not simply invert the background.
  6. Run the gates — all green before shipping: node scripts/gate.mjs --url file:///your/index.html --sel ".glass".

8. One-Sentence Summary

Glass is not a visual effect; it is a displacement field. What you tune is not "transparency" — it is "how many pixels, and where, the background is bent."

相关技能