snell-glass 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md ADDED
@@ -0,0 +1,103 @@
1
+ # snell-glass: guide for coding agents
2
+
3
+ Liquid glass (real refraction, dispersion, rim light) for any element, in any browser with WebGL2. This file is the
4
+ rule-first summary for agents adding it to a project. Human docs: README.md.
5
+
6
+ Use it when someone asks for liquid glass, Apple / iOS 26 style glass, a glass lens or magnifier, or refraction
7
+ that `backdrop-filter` can't do.
8
+
9
+ ## The one rule that matters
10
+
11
+ **The glass only shows what snell-glass knows how to draw.** It can't read the browser's rendering, so it repaints
12
+ the content behind each glass itself. Plain DOM is invisible to it unless you tag it. If you add a glass and don't
13
+ tag what's behind it, the glass will show only the page background colour. That's not a bug.
14
+
15
+ Before you finish, look at what sits behind every glass you add, and handle each thing with the table below.
16
+
17
+ | Content behind the glass | Action |
18
+ | ------------------------------------------------- | ----------------------------------------------------------------------- |
19
+ | `<img>` same-origin / `blob:` / `data:` | none |
20
+ | `<img>` cross-origin | add `crossorigin="anonymous"` (host must allow CORS), else it's skipped |
21
+ | `<canvas>` 2D, `<video>` | none (re-captured live) |
22
+ | `<canvas>` WebGL | create the context with `{ preserveDrawingBuffer: true }` |
23
+ | element background colour / `linear-gradient()` | add `data-glass-box` to that element |
24
+ | text | add `data-glass-text` to the text element or a container of it |
25
+ | `background-image: url()`, radial/conic gradient | not drawn: use `<img>`/`<canvas>` or `registerPainter` |
26
+ | borders, shadows, SVG, icon fonts | not drawn: `registerPainter`, or accept it |
27
+ | DOM in 3D perspective | not drawn: render it to a `<canvas>` |
28
+ | content that must NOT show through | add `data-glass-ignore` (skips the element and its subtree) |
29
+
30
+ A glass never captures its own element or children (they sit on top of it), so tagging them only matters to other glasses.
31
+
32
+ ## Install and use
33
+
34
+ ```bash
35
+ npm i snell-glass
36
+ ```
37
+
38
+ React (`react` >= 17 is an optional peer dependency):
39
+
40
+ ```tsx
41
+ import { SnellGlass } from 'snell-glass/react';
42
+
43
+ <SnellGlass className="nav" wash={0.85} radius={999}>…children render on top…</SnellGlass>;
44
+ // props: every option below, plus `as` (element type, default 'div') and normal HTML attributes.
45
+ // ref gives the Glass handle: { element, ready, set, render, destroy }.
46
+ ```
47
+
48
+ Vanilla:
49
+
50
+ ```ts
51
+ import { createGlass } from 'snell-glass';
52
+
53
+ const glass = createGlass(element, { ior: 1.5 }); // element keeps its children; the glass is drawn behind them
54
+ glass.set({ frost: 1 }); // any option, any time
55
+ glass.destroy(); // on unmount
56
+ ```
57
+
58
+ The element needs a size (width/height from CSS or content). Its own CSS `border-radius` is used unless you pass
59
+ `radius`. Don't give it an opaque background: the glass is the background. SSR is safe: nothing runs until mount.
60
+
61
+ ## Options (all optional)
62
+
63
+ `tone` 'light' | 'dark' · `radius` px · `ior` 1.5 (1 = no bending) · `depth` 50 · `bezel` 36 · `dispersion` 0.14 ·
64
+ `frost` 0..1 (animatable) · `blur` 9 · `wash` 0.35 (raise to ~0.85 for readable labels on busy content) ·
65
+ `saturation` 1.1 · `shine` 1 · `lightFollow` 0.8 · `paused` false · `backdrop` element (default `document.body`;
66
+ only content inside it is drawn) · `background` colour under everything (default: the page background) ·
67
+ `liveInterval` 33 ms.
68
+
69
+ Starting points: navigation bar / pill `wash: 0.85, depth: 44, bezel: 30`; floating card `tone: 'dark', wash: 0.95,
70
+ radius: 32`; magnifier lens `wash: 0.05, depth: 90, bezel: 110, dispersion: 0.24, radius: 999`; frosted panel
71
+ `frost: 0.85, blur: 12, wash: 0.4`.
72
+
73
+ ## Moving things
74
+
75
+ - The glass follows its element every frame by itself (CSS transitions and animations included).
76
+ - If you move the element (or content behind it) from your own `requestAnimationFrame` loop, call `glass.render()`
77
+ right after the move, and start your loop before the glass mounts, so both land in the same frame.
78
+ - Text or boxes animated with CSS/Web Animations inside a tagged element are re-drawn every frame while they run.
79
+
80
+ ## Custom content
81
+
82
+ ```ts
83
+ import { registerPainter, invalidateBackdrop } from 'snell-glass';
84
+
85
+ registerPainter({
86
+ selector: '.chart',
87
+ live: true, // re-capture on the live clock (for animated content); or use `signature`
88
+ signature: (el) => el.dataset.version ?? '', // redraw only when this string changes
89
+ paint: ({ ctx, element, rect }) => {
90
+ /* Canvas 2D, viewport CSS px coordinates */
91
+ }
92
+ });
93
+ invalidateBackdrop(); // force a re-capture after a change the glass can't detect
94
+ ```
95
+
96
+ ## Checklist before you're done
97
+
98
+ 1. Every glass has a size and no opaque background.
99
+ 2. Everything that should show through it is an image, a canvas, a video, or tagged (`data-glass-box`,
100
+ `data-glass-text`), or has a painter.
101
+ 3. Cross-origin images have `crossorigin="anonymous"`, and WebGL canvases have `preserveDrawingBuffer`.
102
+ 4. In React, the component unmounts cleanly (SnellGlass handles `destroy`); in vanilla, call `destroy()`.
103
+ 5. Without WebGL2 the element falls back to a CSS `backdrop-filter` glass, so text on it should still be legible.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mason Wong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,169 @@
1
- # Temporary Holding Version
1
+ # snell-glass
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Apple-style **liquid glass effect** for the web, in React or plain JS: real refraction through a curved bezel (Snell's law), chromatic dispersion and a rim
4
+ light that follows the pointer, **over content that moves**, in every browser with WebGL2, **Safari included**.
5
+
6
+ - Not a blur: `backdrop-filter` can only blur and tint.
7
+ - Not Chrome-only: SVG displacement inside `backdrop-filter` only works in Chromium.
8
+ - Not a snapshot: html2canvas-style captures go stale the moment anything behind the glass moves.
9
+
10
+ ```bash
11
+ npm i snell-glass
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ### Vanilla
17
+
18
+ ```ts
19
+ import { createGlass } from 'snell-glass';
20
+
21
+ const glass = createGlass(document.querySelector('.pill')!, { ior: 1.5, depth: 50 });
22
+
23
+ glass.set({ frost: 1 }); // every option can change at any time, and frost animates
24
+ glass.destroy();
25
+ ```
26
+
27
+ ### React
28
+
29
+ ```tsx
30
+ import { SnellGlass } from 'snell-glass/react';
31
+
32
+ export const Island = () => (
33
+ <SnellGlass className="island" wash={0.85} radius={999}>
34
+ Now playing
35
+ </SnellGlass>
36
+ );
37
+ ```
38
+
39
+ The element keeps its children on top; the glass is drawn behind them.
40
+
41
+ ## Options
42
+
43
+ | Option | Default | What it does |
44
+ | -------------- | ---------------- | --------------------------------------------------------------------- |
45
+ | `tone` | `'light'` | `'light'` washes the top with white, `'dark'` with a smoky tint |
46
+ | `radius` | CSS radius | Corner radius in px; omit to follow `border-radius` (animations too) |
47
+ | `ior` | `1.5` | Index of refraction. `1` = no bending |
48
+ | `depth` | `50` | Slab thickness in px: how far the edge reaches for what it shows |
49
+ | `bezel` | `36` | Width of the rounded edge in px, where all the bending happens |
50
+ | `dispersion` | `0.14` | Red/blue split at the edge |
51
+ | `frost` | `0` | `0` clear → `1` frosted; fractions blend, so it animates |
52
+ | `blur` | `9` | Frost blur radius in px |
53
+ | `wash` | `0.35` | Strength of the top-down tint, for labels on the glass |
54
+ | `saturation` | `1.1` | Saturation of what's seen through the glass |
55
+ | `shine` | `1` | Rim light and sheen strength |
56
+ | `lightFollow` | `0.8` | How much the light turns toward the pointer (0 with reduced motion) |
57
+ | `paused` | `false` | Stop rendering, keep the last frame |
58
+ | `backdrop` | `document.body` | Only content inside this element is seen through the glass |
59
+ | `background` | page background | Colour under everything |
60
+ | `liveInterval` | `33` | ms between re-captures of live sources (canvas, video) |
61
+
62
+ ## What the glass can see
63
+
64
+ **Read this before you ship.** WebGL can't read what the browser has already drawn, so snell-glass repaints what's
65
+ behind each glass itself, into a texture, only when it changes. It does **not** see arbitrary DOM: an element that
66
+ isn't on the list below is invisible through the glass, and you'll see only the page background where it should
67
+ be.
68
+
69
+ | Behind the glass | Seen? | What to do |
70
+ | --------------------------------------------------------- | ------------------------- | -------------------------------------------------------------------------- |
71
+ | `<img>`, same-origin or `blob:` / `data:` | Automatically | Nothing |
72
+ | `<img>` from another origin | Only with CORS | Add `crossorigin="anonymous"`; the host must send `Access-Control-Allow-Origin` |
73
+ | `<canvas>` (2D) | Automatically, live | Nothing |
74
+ | `<canvas>` (WebGL / WebGL2) | Automatically, live | Create the context with `preserveDrawingBuffer: true` |
75
+ | `<video>` | Automatically, live | Same-origin, or CORS as for images |
76
+ | An element's background colour or `linear-gradient()` | When tagged | Add `data-glass-box` to that element |
77
+ | Text | When tagged | Add `data-glass-text` to the element (or a container) holding it |
78
+ | `background-image: url(…)`, radial/conic gradients | No | Use an `<img>` / `<canvas>`, or a [custom painter](#custom-painters) |
79
+ | Borders, `box-shadow`, SVG, icon fonts | No | A custom painter, or accept that they don't show |
80
+ | CSS filters other than `brightness()`, blend modes | Ignored | Drawn unfiltered |
81
+ | DOM in 3D perspective (`rotateX` / `rotateY` beyond a tilt) | No | Render it to a `<canvas>` instead (the demo's tilted wall does this) |
82
+ | Anything else (plain `div`s, buttons, form fields…) | No | Tag it, paint it, or leave it out |
83
+
84
+ Everything that is drawn lands at its real on-screen position: 2D transforms (rotate, scale, skew), a gentle 3D
85
+ tilt, opacity, `brightness()`, its own `border-radius` and any ancestor's `overflow` clipping are all applied, and
86
+ CSS or Web Animations running inside a tagged element are followed every frame.
87
+
88
+ Mark anything that shouldn't show through with `data-glass-ignore` (it skips the element and all of its children).
89
+ Only content inside the `backdrop` option (default `document.body`) is considered.
90
+
91
+ ### Typical setup
92
+
93
+ ```html
94
+ <!-- a hero behind a glass nav -->
95
+ <section data-glass-box class="hero"> <!-- its background colour / gradient -->
96
+ <h1 data-glass-text>Glass that bends light.</h1>
97
+ <p data-glass-text>Real refraction over content that moves.</p>
98
+ <img src="/poster.jpg" alt="" /> <!-- same-origin: works as is -->
99
+ <canvas id="particles"></canvas> <!-- live, works as is -->
100
+ </section>
101
+ ```
102
+
103
+ `data-glass-text` on a container draws all the text inside it, so tag a section's heading block once rather than
104
+ every line. Glass over a big, mostly-plain page costs little; the price is paid per tagged element that sits
105
+ behind a glass.
106
+
107
+ ### Troubleshooting
108
+
109
+ - **The glass shows only the page colour.** Whatever is behind it isn't on the list above. Tag it
110
+ (`data-glass-box`, `data-glass-text`), or check the browser console for CORS errors on images.
111
+ - **Something is drawn in the wrong place for one frame while it moves.** You're moving it (or the glass) from your
112
+ own `requestAnimationFrame`; call `glass.render()` right after, and register your loop before the glass mounts.
113
+ - **A WebGL canvas shows up blank.** Create its context with `preserveDrawingBuffer: true`.
114
+ - **No refraction at all, just a frosted CSS panel.** WebGL2 isn't available (or the context was lost); this is the
115
+ built-in fallback.
116
+
117
+ ### Custom painters
118
+
119
+ ```ts
120
+ import { registerPainter } from 'snell-glass';
121
+
122
+ const unregister = registerPainter({
123
+ selector: '.chart',
124
+ live: true, // it animates: re-capture on the live clock
125
+ signature: (el) => el.dataset.version ?? '', // or: redraw when this string changes
126
+ paint: ({ ctx, element, rect }) => {
127
+ // ctx is already in page (viewport CSS px) coordinates
128
+ drawMyChart(ctx, element, rect);
129
+ }
130
+ });
131
+ ```
132
+
133
+ ## Animating the element
134
+
135
+ The glass follows its element every frame. If you move or resize the element yourself in a
136
+ `requestAnimationFrame` callback, call `glass.render()` right after, so the glass lands in the same frame instead
137
+ of the next one.
138
+
139
+ ## Performance
140
+
141
+ - The page structure is indexed once (and on DOM changes), not per frame.
142
+ - Each frame reads each element's computed style at most once.
143
+ - Nothing is repainted, uploaded or blurred unless what's behind the glass changed.
144
+ - Blur passes are scissored to the glass; clear glass skips them.
145
+ - GPU buffers grow and shrink in steps, so resizing glass doesn't reallocate every frame (important in Safari).
146
+
147
+ ## Support
148
+
149
+ Any browser with WebGL2: Safari 15+, Chrome, Edge, Firefox. Without WebGL2, the element gets a CSS glass
150
+ (`backdrop-filter` blur and tint) instead.
151
+
152
+ ## For AI agents
153
+
154
+ [AGENTS.md](./AGENTS.md) is a compact, rule-first version of this page for coding agents adding snell-glass to a
155
+ project. It ships in the npm package, so it's also at `node_modules/snell-glass/AGENTS.md`.
156
+
157
+ ## Demo
158
+
159
+ Source and issues: [github.com/masonwongcs/snell-glass](https://github.com/masonwongcs/snell-glass)
160
+
161
+ ```bash
162
+ pnpm install
163
+ pnpm dev # http://localhost:5199
164
+ pnpm build:demo # static site in demo/dist
165
+ ```
166
+
167
+ ## License
168
+
169
+ MIT