@edinstvo/glass

中文文档
1.1.5发布于 2026/11/12MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2466

Liquid glass for the web: one WebGL material that looks the same on macOS and Windows.

原始使用文档
文档目录11 节
  1. Documentation
  2. npm
  3. Exports
  4. Contributing
  5. Setting up for development
  6. License
  7. Thickness
  8. Lighting
  9. Background adaptation
  10. Surface geometry
  11. Optical model
Edinstvo Glass

Edinstvo Glass is a simple library for real glass on the web.

At its core is a WebGL lens that bends light through a curved edge, splits colours and frosts what is behind it, the same on macOS and Windows. It comes as one web component with no dependencies.

Documentation

See the full documentation, with a playground and examples, at glass.makark.ru/docs.

npm

To install from npm:

npm i @edinstvo/glass
<script type="module">
  import "@edinstvo/glass/define";
</script>

<edinstvo-glass frost="0.4" refraction="0.5">Hello</edinstvo-glass>

Exports

  • @edinstvo/glass - attachGlass, the element classes, defaults and ranges of every setting.
  • @edinstvo/glass/define - Registers edinstvo-glass and edinstvo-glass-group.
  • @edinstvo/glass/glass.css - Base materials on backdrop-filter, for pages without JavaScript or a GPU.

Contributing

Edinstvo Glass is open source and we appreciate issue reports and pull requests.

Setting up for development

Initialize the repo:

git clone https://github.com/EdinstvoAI/edinstvo-glass.git
cd edinstvo-glass
npm ci

Run the bench:

npm run demo

Click a glass element to select it. The outline and inspector heading show the current target, and the controls read its settings. Merged shapes share one material: select Edit group material to edit that surface, or edit motion on the individual shape. Unmerged elements can have their own material settings.

Test:

npm test

License

MIT

Thickness

thickness sets the optical distance between the front and back surfaces in CSS pixels, from 0 to 96 (default 6). depth controls the curved surface profile. strokeThickness independently sets the visible rim width, from 0 to 8 CSS pixels (default 1).

<edinstvo-glass thickness="18" stroke-thickness="1" refraction="0.45">Content</edinstvo-glass>

A local material layer can override each property independently; inherit keeps the parent value and default restores the engine default. Refraction uses the travel distance through an approximated curved front surface and a flat back surface, with the configured refractive index. This is an orthographic screen-space model; it does not provide a rotatable 3D mesh or multiple internal reflections.

Lighting

Set the light once for the page. Any element can replace one setting and keep the rest.

import { setSceneLight } from "@edinstvo/glass";

setSceneLight({ direction: "top-left", intensity: "80%", color: "white", softness: 0.6 });
<edinstvo-glass light-color="blue" reflection="0.6" dynamic-refraction="0.5"></edinstvo-glass>

See Lighting.

Background adaptation

Adaptation is off by default. Enable it per element or once for the scene:

import { setSceneMaterial } from "@edinstvo/glass";
setSceneMaterial({ adaptation: "full", adaptationStrength: 1 });
<edinstvo-glass adaptation="contrast">Readable text</edinstvo-glass>
<edinstvo-glass adaptation="off" tint="#fff">Manual material</edinstvo-glass>
ModeWhat changes
offNo background analysis; all material values stay manual
tintAn automatic tint follows the local light/dark background; text stays under application control
contrastAutomatic tint, foreground and transparency adapt to sampled contrast
fullContrast adaptation also raises frost on visually complex backgrounds

adaptationStrength (0–1) blends the adjustment with your material. Explicit tint and foreground colors are retained. Set foreground="auto" for automatic black/white text. Modes contrast and full publish the chosen text color as --glass-foreground; web components apply it to their content. With attachGlass, apply color: var(--glass-foreground, inherit) to the text you want to adapt. Input tokens are --glass-adaptation, --glass-adaptation-strength and --glass-text-color.

Changes interpolate over time using adaptationDuration (0–1000 ms, default 300); this also applies when returning to a manual material. Set adaptation-duration="0" for immediate changes. Reduced-motion preferences skip interpolation. A settling transition schedules frames; once settled, it stops. The source is still sampled at most once per 150 ms, and the contrast target is cached between samples.

full favors a stable glass appearance: under ordinary accessibility settings it changes configured transparency by at most 0.18 and raises frost by at most 0.18. Choose contrast when the sampled contrast target should take priority over transparency. Increased system contrast or reduced transparency can override those visual bounds in full as well. data-glass-adapting marks a transition; the sampled contrast estimate describes its target rather than every intermediate frame.

The engine samples a 12×12 crop of the supplied backdrop at most once per 150 ms per active surface. Samples include local color, luminance and variation. Samples are cached and analysis is not run for off or strength 0. setSceneMaterial(null) clears scene material defaults; setSceneLight continues to override the lighting properties independently. Local inherit and default work per property.

Contrast modes target 4.5:1 for sampled colors, or 7:1 with prefers-contrast: more, using the W3C relative luminance and contrast definition. This is an estimate before optical distortion, lighting and final text rendering, not a WCAG compliance guarantee. Manual colors or reduced adaptation strength may prevent the target from being reached. data-glass-contrast reports the sampled estimate. Audit the rendered interface separately.

prefers-reduced-transparency: reduce lowers transparency in contrast modes. Forced colors hide the optical canvas and use system colors in web components. Neither preference starts a continuous animation loop.

Surface geometry

<edinstvo-glass shape="ellipse" style="width:240px;height:120px"></edinstvo-glass>
<edinstvo-glass shape="superellipse" curvature="5"></edinstvo-glass>
<edinstvo-glass shape="path" path="M50 0L100 100H0Z" view-box="0 0 100 100"></edinstvo-glass>
const surface = attachGlass(element, {
  geometry: { shape: "path", path: "M50 0L100 100H0Z", viewBox: [0, 0, 100, 100] },
  material: { refraction: 0.4, depth: 12 },
});
surface.set({ geometry: { shape: "ellipse" } });
ShapeGeometry
rounded-rectCSS corner radius; existing default
circleA circle inscribed in the shorter side of the element
ellipseAn ellipse spanning the element's width and height
superellipseA continuous profile; curvature ranges from 2 (ellipse) to 8
pathSVG path data mapped from viewBox into the element bounds

Geometry changes the shader outline and its normals, so refraction, dispersion, rim, light and reflection follow that shape. It also participates in the existing group merging and layer composition. Set a web component's .geometry property to a new object to update it; geometry does not add a second renderer or context.

Paths accept path data only, not SVG markup, scripts or external resources. Use fill-rule="evenodd" or geometry.fillRule for alternate hole filling. Limits are 64 KiB of path data, 4096 commands and finite coordinates within ±1,000,000. Invalid markup is rejected by the property API; invalid attributes use the default outline with a console warning. Invalid native path syntax uses the view box. Limits bound input size, not the runtime of every browser's native path rasterizer.

Each custom outline is baked into a 256×256 signed distance tile. Raw and smoothed distances use 16-bit packed channels. Tiles share a per-surface atlas; only geometry changes rebuild it, and normal interaction reuses its GPU texture. GPU atlas caching targets at most four atlases and 32 MiB, excluding one active atlas if necessary. The distance transform follows the linear-time Felzenszwalb–Huttenlocher method.

Ellipses and superellipses use distance approximations. Small SVG details, highly concave paths, large elements and extreme aspect changes may reveal sampling or normal-field artifacts. Geometry controls the optical canvas; HTML layout and hit testing stay rectangular, and custom HTML content is not automatically clipped to a path. Custom paths have no automatic box shadow. CSS fallback uses the ordinary rounded rectangle rather than reproducing arbitrary path optics.

Optical model

The screen-space lens uses Snell refraction with configurable ior (1–2.4, default 1.5). ior="1" is optically matched to air and produces no geometric refraction. The curved profile has a height bounded by half the material thickness. depth defines its width. The ray offset uses a shallow thickness correction at the bevel. At grazing angles the lens blends in a sharper backdrop sample, so the bend remains visible on frosted glass.

Enabled environment reflections use the unpolarized dielectric Fresnel equations to mix reflected and transmitted colors. The equations and IOR convention follow PBRT's dielectric interface description. reflection="0" still disables that contribution. The scene image is the available reflection source; this does not reconstruct a hidden 3D environment or simulate repeated internal reflections.

dispersion-mode="artistic" uses intentional RGB offsets. dispersion-mode="physical" uses a two-term Cauchy approximation at 610, 550 and 460 nm, with a generic coefficient of 0.004 µm² scaled by dispersion. This coefficient is illustrative, not a fitted optical glass catalog entry or a full spectral solution. Physical channel separation follows surface incidence and optical path, not the angle of the decorative directional light. dispersion="0" disables channel separation in both modes.

<edinstvo-glass ior="1.5" reflection="1" dispersion-mode="physical"
  dispersion="0.5" thickness="12" depth="12" frost="0.1">
  Content
</edinstvo-glass>

The implementation remains a 2D optical approximation with an orthographic view. Tint, lift, bloom, touch warping and contrast adaptation are appearance controls, so combining them does not constitute a fully energy-conserving spectral material. No 3D rotation or separate renderer is involved.

版本与发布记录

@edinstvo/glass 共发布 14 个版本,首次发布于 2026/11/04;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.5latest2026/11/121 天前
1.1.42026/11/112 天前
1.1.32026/11/058 天前
1.1.22026/11/058 天前
1.1.12026/11/058 天前
1.1.02026/11/058 天前

其他发布通道:next → 0.1.0-beta.1。

关于 @edinstvo/glass 的常见问题

@edinstvo/glass 是什么?

Liquid glass for the web: one WebGL material that looks the same on macOS and Windows.

如何安装 @edinstvo/glass?

在项目目录运行 npm install @edinstvo/glass,或使用 pnpm add / yarn add / bun add。

@edinstvo/glass 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@edinstvo/glass 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@edinstvo/glass 可以商用吗?

@edinstvo/glass 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@edinstvo/glass 的下载量有多少?

根据 npm 官方下载统计,@edinstvo/glass 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,466 次,日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@edinstvo/glass 还在维护吗?

最新版本 1.1.5 发布于 2026/11/12(1 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/11/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。