Compiling Liquid Glass to SVG and CSS

macOS Tahoe light cyan and sunlit cream wave
    The same rounded box rendered three ways over a live backdrop. Switch between Chromium SVG refraction, the zero-child-DOM Safari specular host, and standard CSS backdrop blur.

    Most liquid glass libraries on the web spin up a WebGL canvas and redraw a copy of your background behind a fragment shader. That falls apart the moment live HTML text, scrolling containers, video, or selection highlights move underneath the glass. Put glass on more than a handful of controls and browsers begin dropping WebGL contexts outright.

    Glascii takes the DOM route instead. Everything a curved glass rim does is a pure function of the element's geometry and seven optical numbers, independent of whatever sits behind it. A Web Worker computes the signed-distance field once per shape, packs it into displacement and rim-light maps, and mounts a standard SVG <filter> referenced by CSS backdrop-filter: url(#id). Zero WebGL contexts, zero background screenshots.

    1. 1. BakeA Web Worker evaluates the signed-distance lens field off the main thread
    2. 2. PackGrey and alpha channels protect displacement coordinates from Display-P3 drift
    3. 3. SliceAnalytical 9-slicing shrinks a full-box map to a tiny corner atlas
    4. 4. CompositeSVG refraction in Chromium, zero-child-DOM specular bevels elsewhere
    Color management

    The Display-P3 displacement trap

    Every textbook SVG refraction demo packs horizontal displacement X into the red channel and vertical displacement Y into green. On an sRGB display, neutral grey (127, 127) bends nothing and the math looks right.

    On a wide-gamut Display-P3 MacBook, Studio Display, or Android phone, Chromium converts a resolved <feImage> from sRGB into the display color space before feDisplacementMap samples it. Because the sRGB-to-P3 matrix mixes red and green, a strong vertical offset leaks sideways into horizontal offset. Measured on a Display-P3 MacBook at scale 100, a neutral X coordinate shifts by 9.7 pixels wherever Y is 230.

    RGB packing on Display-P3 +9.7 px X drift
    R=X, G=Y (channel bleed)
    Grey + Alpha packing 0.0 px drift
    R=G=B=X, A=Y ∈ [127..255]
    Neutral vertical lines under a pure vertical displacement gradient on a Display-P3 MacBook. Packing X into achromatic grey and Y into alpha eliminates cross-channel bleed.

    The fix rests on one invariant of color management: only grey and alpha survive Display-P3 conversion untouched. An achromatic pixel where red, green, and blue are identical stays identical after the gamut matrix, and the alpha channel never enters the color matrix at all.

    Glascii encodes X in grey across [0..254] so neutral 127 maps cleanly to 0.5, and encodes Y in alpha across [127..255] so premultiplication never collapses X to zero when Y sits at its negative extreme. One feColorMatrix unpacks both channels back to R and G with opaque alpha right before displacement runs.

    Exact zero-bias unpack matrix

    Scaling grey by 255 / 254 and alpha by 255 / 128 places neutral (127, 191) at exact 0.5 with zero subpixel creep. Note color-interpolation-filters="sRGB" on the <filter>: SVG defaults to linearRGB, which would gamma-decode 127/255 down to ~0.212 before the matrix runs.

    <filter id="lens" color-interpolation-filters="sRGB">
      <!-- Unpack Grey [0..254] -> R and Alpha [127..255] -> G with A = 1 -->
      <feColorMatrix in="bendRaw" type="matrix"
        values="1.003937 0 0 0 0
                0 0 0 1.9921875 -0.9921875
                0 0 0 0 0
                0 0 0 0 1"
        result="bend" />
    </filter>
    Dispersion

    Five taps instead of eighteen

    When glass bends light strongly along a curved edge, different wavelengths bend by slightly different amounts. Running three feDisplacementMap passes for red, green, and blue works at low dispersion, but once separation crosses 1.5 pixels the image splits into three hard, detached ghosts.

    A smooth chromatic smear normally takes a 6-tap tent filter per channel, which costs 18 displacement passes per frame. Glascii shares five fixed displacement taps across all three channels, spaced at multiples of the dispersion spread and blended through normalized tent weights.

    -0.75 -0.375 0 +0.375 +0.75 Blue Green Red
    Normalized tent weights per channel evaluated at five shared displacement offsets. Each tap multiplies its RGB weights inside one feColorMatrix while holding alpha at 1.

    Below 1.5 pixels of peak separation, 3-tap and 5-tap output look identical, so the compiler drops down to three taps automatically.

    Backdrop blur (frost) stays outside the <filter> altogether, running first as a native CSS blur() in the same backdrop-filter chain. Blurring inside SVG with feGaussianBlur averages transparent black beyond the element's bounds and leaves a dark halo; CSS blur() mirrors edge pixels cleanly before refraction runs.

    Compositor performance

    Why full-box maps stall the compositor

    Baking a displacement map once sounds free until you profile Chromium's compositor. Every time a page submits a frame that draws an SVG filter, Chromium's security-constrained paint serializer copies each <feImage> bitmap inline across IPC to the GPU process. A blinking text caret or a hover highlight in a sidebar forces a full copy of every on-screen lens map.

    Measured on a full-page layout with four large lenses, serializing full-resolution maps cost roughly 5 ms per million texels, pushing frame times from 28 ms to 137–186 ms.

    When splay is 0, the lens field only varies inside the four corners and the rim band (depth). Along every straight edge between corners, adjacent texel rows or columns are byte-identical. Instead of allocating a full-element buffer, Glascii bakes just the four corners, plus two guard lines for bilinear filtering, into a tiny 9-slice atlas.

    Joining nine <feImage> tiles with <feMerge> introduces one catch: at fractional browser zoom, semi-transparent alpha edges bleed together along tile seams. To keep seams invisible, 9-sliced lenses split X and Y into two opaque greyscale atlases (A = 255) and recombine them inside the filter before the Display-P3 unpack matrix runs. Full-box lenses (splay > 0) do not merge tiles, so they stick to the single Grey+Alpha map to halve IPC bytes.

    macOS Sonoma emerald, coral, and cobalt hills
    Box 360 × 184 px Atlas 148 × 148 px Corner pitch 1:1 fixed Release rebakes 0
    Drag the bottom-right corner to resize this lens live. The four corner tiles stay pinned at their baked pixel pitch while only the straight center strip stretches.
    Property Full-box map Analytical 9-slice
    1440 × 900 bake CPU Full 5.2 Mpx SDF pass ~36× faster
    Map heap allocation Full device-pixel grid ~256× smaller
    Live resize corner pitch Stretches until rebake Exact 1:1
    Rebake on pointer release Required 0 (same filter node)
    Measured savings from analytical 9-slice baking when splay is 0.
    Cross-engine

    Safari and Firefox without touching your DOM

    Chromium honors backdrop-filter: url(#id) today. Safari and Firefox parse the declaration and ignore the SVG reference, leaving naive liquid glass demos looking like flat grey rectangles.

    The obvious workaround is to inject overlay wrapper <div> children inside the glass element. In React, Next.js, and Astro apps, mutating child DOM nodes on mount breaks hydration and collides with layout selectors. Glascii leaves your element's children completely untouched.

    On those engines, Glascii attaches a single shared Constructable Stylesheet via document.adoptedStyleSheets, passing strict Content Security Policies without inline style hashes, and tags the host element with data attributes so it paints entirely through pseudo-elements:

    • ::before applies a pow(ramp, 3.5) feathered bevel mask with boosted backdrop vibrancy and directional shading.
    • ::after composites the baked 1 px specular rim onto the backdrop using mix-blend-mode: plus-lighter.
    SDF refraction field
    5-tap spectral split
    Additive specular rim
    Zero-DOM Safari host
    The four optical layers behind every glascii surface, degraded cleanly without injecting wrapper elements or touching WebGL.
    API

    Two lines in React, or vanilla DOM

    The entire engine compiles to zero runtime dependencies. Use the React component when you want automatic box measurement and cleanup, or mount directly on any HTML element in Vue, Svelte, Astro, or plain TypeScript.

    import { Glass } from 'glascii'
    
    export function Panel({ children }) {
      return (
        <Glass refraction={0.9} depth={28} dispersion={0.7} frost={2}>
          {children}
        </Glass>
      )
    }

    The baker clamps and normalizes every input up front, so out-of-range props never produce NaN maps or clipped filters. It also reads computed CSS border-radius per corner, so pills, circles, and asymmetric shapes like 16px 16px 0 0 bottom sheets work out of the box.

    macOS Tahoe dark cobalt and indigo wave
    Drag lens
    1.00
    30 px
    0.95
    0
    -45°
    0.90
    0.00
    Drag any slider or move the lens directly. Crossing 1.5 px peak spread promotes dispersion to the 5-tap spectral tent; raising splay above 0 switches out of the 9-slice corner atlas.
    Open the playground Drag, resize, and inspect the live SVG filter.