@ikeofficial/liquid-glass-js 0.3.0 → 0.3.2
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/README.md +27 -21
- package/build/core/liquid-glass.css +1 -1
- package/build/core/liquid-glass.d.ts +10 -6
- package/build/core/liquid-glass.js +1 -1
- package/build/core/optics.js +1 -1
- package/build/core/options.js +1 -1
- package/build/core/renderers/sampled.js +1 -0
- package/build/core/renderers/viewport.js +1 -1
- package/build/core/renderers/webgl.js +39 -0
- package/package.json +1 -1
- package/skills/liquid-glass-js/SKILL.md +7 -5
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# Liquid Glass JS
|
|
2
2
|
|
|
3
|
-
A dependency-free glass material for ordinary HTML elements. It can refract
|
|
3
|
+
A dependency-free glass material for ordinary HTML elements. It can blur a live backdrop, refract a declared media source, or apply SVG displacement to a small HTML scene. Angular, React and Vue bind the same JavaScript core to their lifecycle.
|
|
4
4
|
|
|
5
5
|
## Playground
|
|
6
6
|
|
|
7
7
|
Try the [interactive demo on GitHub Pages](https://bigimattia.github.io/liquidglass-js/).
|
|
8
8
|
|
|
9
|
-

|
|
10
10
|
|
|
11
11
|
To run the playground locally:
|
|
12
12
|
|
|
@@ -16,11 +16,11 @@ mise install
|
|
|
16
16
|
mise run dev
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
Open [the local playground](http://127.0.0.1:4173/demo/) to
|
|
19
|
+
Open [the local playground](http://127.0.0.1:4173/demo/) to drag a glass lens over an image and see live content beneath a pinned glass bar. **Automatic** chooses native backdrop refraction in Chromium and CSS frost in Firefox and Safari. Select **Viewport SVG** to try displacement on the declared HTML source. **Advanced** adds radius, bevel, curvature, chroma, blur, and tint opacity.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Expand **Usage** beneath the playground and choose a language to copy the live page example with the current mode and settings. The **Viewport SVG** example identifies the HTML element behind the glass with `source`.
|
|
22
22
|
|
|
23
|
-
The native-controls showcase has its own renderer picker. Select Button, Switch, Slider, Segmented, or Media and copy the Tailwind markup or the matching JavaScript, Angular, Vue, or React configuration. The Media
|
|
23
|
+
The native-controls showcase has its own renderer picker. Select Button, Switch, Slider, Segmented, or Media and copy the Tailwind markup or the matching JavaScript, Angular, Vue, or React configuration. The Media preview uses a shared WebGL canvas for the video and its controls. The copyable configurations use the library's sampled-media API for individual lenses.
|
|
24
24
|
|
|
25
25
|
## Install
|
|
26
26
|
|
|
@@ -46,10 +46,9 @@ Update the version in `package.json`, then run **Publish to npm** from the Actio
|
|
|
46
46
|
import { liquidGlass } from "@ikeofficial/liquid-glass-js";
|
|
47
47
|
import "@ikeofficial/liquid-glass-js/style.css";
|
|
48
48
|
|
|
49
|
-
const
|
|
50
|
-
const glass = liquidGlass(item, { mode: "auto", viewportSource: "#feed", radius: 30 });
|
|
49
|
+
const glass = liquidGlass("item", { options: { radius: 30 } });
|
|
51
50
|
|
|
52
|
-
glass.update({
|
|
51
|
+
glass.update({ options: { blur: 6 } });
|
|
53
52
|
glass.destroy();
|
|
54
53
|
```
|
|
55
54
|
|
|
@@ -69,9 +68,9 @@ import { LiquidGlassDirective } from "@ikeofficial/liquid-glass-js/angular";
|
|
|
69
68
|
imports: [LiquidGlassDirective],
|
|
70
69
|
template: `
|
|
71
70
|
<div class="glass-shell">
|
|
72
|
-
<main
|
|
71
|
+
<main class="glass-source"><div #feed>Scrollable content</div></main>
|
|
73
72
|
<button class="glass-bar"
|
|
74
|
-
[liquidGlass]="{ mode: 'viewport',
|
|
73
|
+
[liquidGlass]="{ mode: 'viewport', source: feed, options: { radius: 24 } }">
|
|
75
74
|
Continue
|
|
76
75
|
</button>
|
|
77
76
|
</div>
|
|
@@ -88,7 +87,7 @@ import { LiquidGlassDirective } from "@ikeofficial/liquid-glass-js/angular";
|
|
|
88
87
|
export class Action {}
|
|
89
88
|
```
|
|
90
89
|
|
|
91
|
-
|
|
90
|
+
An element ID or selector such as `source: '#feed'` also works.
|
|
92
91
|
|
|
93
92
|
## React
|
|
94
93
|
|
|
@@ -97,7 +96,7 @@ import { useLiquidGlass } from "@ikeofficial/liquid-glass-js/react";
|
|
|
97
96
|
import "@ikeofficial/liquid-glass-js/style.css";
|
|
98
97
|
|
|
99
98
|
export function Action() {
|
|
100
|
-
const ref = useLiquidGlass({
|
|
99
|
+
const ref = useLiquidGlass({ options: { radius: 22 } });
|
|
101
100
|
return <button ref={ref}>Continue</button>;
|
|
102
101
|
}
|
|
103
102
|
```
|
|
@@ -113,7 +112,7 @@ import "@ikeofficial/liquid-glass-js/style.css";
|
|
|
113
112
|
</script>
|
|
114
113
|
|
|
115
114
|
<template>
|
|
116
|
-
<button v-liquid-glass="{
|
|
115
|
+
<button v-liquid-glass="{ options: { radius: 22 } }">Continue</button>
|
|
117
116
|
</template>
|
|
118
117
|
```
|
|
119
118
|
|
|
@@ -123,28 +122,35 @@ The directive can also be registered globally with `app.directive("liquid-glass"
|
|
|
123
122
|
|
|
124
123
|
| Mode | Behavior |
|
|
125
124
|
| --- | --- |
|
|
126
|
-
| `auto` (default when `mode` is omitted) |
|
|
127
|
-
| `viewport` | SVG
|
|
128
|
-
| `
|
|
125
|
+
| `auto` (default when `mode` is omitted) | Sampled WebGL for an explicit, usable media source. Otherwise native backdrop displacement in Chromium or CSS frost in Firefox and Safari |
|
|
126
|
+
| `viewport` | Explicit SVG displacement on a bounded HTML `source`. Use for small, controlled scenes |
|
|
127
|
+
| `sampled` | Sample an explicit image, video, or canvas with WebGL. Use frost if sampling is unavailable |
|
|
128
|
+
| `backdrop` | Use native backdrop displacement where supported, with CSS frost as fallback |
|
|
129
129
|
| `frost` | CSS blur, tint, and highlights |
|
|
130
130
|
| `off` | Disables glass and restores author styling |
|
|
131
131
|
|
|
132
|
-
`
|
|
132
|
+
`source` accepts an HTML element, an ID, or a selector resolved in the lens’s document or shadow root. Keep the source axis-aligned and place the lens in an overlapping layer. The source and lens must be in separate subtrees for viewport mode. For an explicit viewport scene that scrolls in Safari, declare the content wrapper inside the scroll container as the source. Put `overflow: auto` on the outer container and place the pinned lens outside it. Call `glass.refresh()` after changing the relative position or replacing a selector target without an options update.
|
|
133
133
|
|
|
134
134
|
Viewport SVG applies a CSS filter to the source, which creates a stacking context and can change positioning for fixed descendants. Keep fixed overlays outside that source. The source may contain ordinary text, images, and interactive HTML. Native video compositing varies by browser. Use an ordinary lens host with available `::before` and `::after` pseudo-elements.
|
|
135
135
|
|
|
136
|
+
On WebKit, Viewport SVG positions its filter relative to the source's bounding box and limits the displacement passes to the lens with room for the warp. It uses frost when the source's estimated raster exceeds 8,388,608 device pixels or 4,096 device pixels on either side. The estimate uses the larger of the layout and displayed dimensions, scaled by the device pixel ratio.
|
|
137
|
+
|
|
136
138
|
The SVG renderers use an internally generated PNG data URL and fall back to frost if it cannot load. Reduced motion disables pointer tracking. Reduced transparency and forced colors use a solid treatment. The `liquid-glass` class with the shared stylesheet gives you a CSS-only treatment.
|
|
137
139
|
|
|
138
|
-
|
|
140
|
+
For automatic media refraction, place a same-origin or CORS-enabled image, video, or canvas behind the lens and call `liquidGlass("play-glass", { source: "player-video" })`. Use a borderless, axis-aligned source with `object-fit: fill`, centered `cover`, or centered `contain`. Video repaints with its frames. Call `glass.refresh()` after redrawing a canvas or repositioning the media with a transform. The native-control examples use Tailwind CSS v4 and the shared glass stylesheet.
|
|
139
141
|
|
|
140
142
|
## Options
|
|
141
143
|
|
|
142
144
|
| Option | Default | Effect |
|
|
143
145
|
| --- | --- | --- |
|
|
144
|
-
| `mode` | `"auto"` |
|
|
145
|
-
| `
|
|
146
|
+
| `mode` | `"auto"` | Renderer selection from the table above |
|
|
147
|
+
| `source` | `null` | ID, selector, or element behind the glass |
|
|
148
|
+
| `options` | — | Optional optical adjustments listed below |
|
|
149
|
+
|
|
150
|
+
| Optical option | Default | Effect |
|
|
151
|
+
| --- | --- | --- |
|
|
146
152
|
| `radius` | 24 | Corner radius in CSS pixels. Set size with your CSS |
|
|
147
|
-
| `refraction` | 18 | Requested edge displacement in CSS pixels. Zero disables distortion |
|
|
153
|
+
| `refraction` | 18 | Requested edge displacement in CSS pixels. The bevel limits the effective displacement. Zero disables distortion |
|
|
148
154
|
| `bevel` | 24 | Width of the refracted edge in CSS pixels |
|
|
149
155
|
| `curvature` | 2 | Shape of the optical bevel, 0.25–8 |
|
|
150
156
|
| `chroma` | 0 | Color-channel spread, 0–1 |
|
|
@@ -1 +1 @@
|
|
|
1
|
-
:where(.liquid-glass){--liquid-glass-lens: ;--liquid-glass-overlay-filter: var(--liquid-glass-lens) blur(var(--liquid-glass-blur, 2px)) saturate(var(--liquid-glass-saturation, 1.35));--liquid-glass-light-x: 25%;--liquid-glass-light-y: 0%;position:relative;z-index:0;border-radius:var(--liquid-glass-radius, 24px)}:where(.liquid-glass):before,:where(.liquid-glass):after{content:"";position:absolute;inset:0;border-radius:inherit;pointer-events:none}:where(.liquid-glass):before{z-index:-1;background:radial-gradient(ellipse at var(--liquid-glass-light-x) var(--liquid-glass-light-y),rgb(255 255 255 / var(--liquid-glass-glow, .23)),transparent 72%),rgb(var(--liquid-glass-tint-rgb, 255 255 255) / var(--liquid-glass-tint, .14));-webkit-backdrop-filter:var(--liquid-glass-overlay-filter);backdrop-filter:var(--liquid-glass-overlay-filter)}:where(.liquid-glass):after{padding:1px;opacity:var(--liquid-glass-edge, .85);background:radial-gradient(ellipse at var(--liquid-glass-light-x) var(--liquid-glass-light-y),rgb(255 255 255 / .95),transparent 64%),linear-gradient(var(--liquid-glass-angle, 145deg),rgb(255 255 255 / .55),rgb(255 255 255 / .11) 50%,rgb(255 255 255 / .42));mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0);mask-composite:exclude}:where(.liquid-glass:active):before{box-shadow:inset 0 0 18px #ffffff2b}@media(prefers-reduced-transparency:reduce),(forced-colors:active){:where(.liquid-glass):before{background:var(--liquid-glass-solid, Canvas);-webkit-backdrop-filter:none;backdrop-filter:none}:where(.liquid-glass):after{background:currentColor}}
|
|
1
|
+
:where(.liquid-glass){--liquid-glass-lens: ;--liquid-glass-overlay-filter: var(--liquid-glass-lens) blur(var(--liquid-glass-blur, 2px)) saturate(var(--liquid-glass-saturation, 1.35));--liquid-glass-light-x: 25%;--liquid-glass-light-y: 0%;position:relative;z-index:0;border-radius:var(--liquid-glass-radius, 24px)}:where(.liquid-glass):before,:where(.liquid-glass):after{content:"";position:absolute;inset:0;border-radius:inherit;pointer-events:none}:where(.liquid-glass):before{z-index:-1;background:radial-gradient(ellipse at var(--liquid-glass-light-x) var(--liquid-glass-light-y),rgb(255 255 255 / var(--liquid-glass-glow, .23)),transparent 72%),rgb(var(--liquid-glass-tint-rgb, 255 255 255) / var(--liquid-glass-tint, .14));-webkit-backdrop-filter:var(--liquid-glass-overlay-filter);backdrop-filter:var(--liquid-glass-overlay-filter)}:where(.liquid-glass):after{padding:1px;opacity:var(--liquid-glass-edge, .85);background:radial-gradient(ellipse at var(--liquid-glass-light-x) var(--liquid-glass-light-y),rgb(255 255 255 / .95),transparent 64%),linear-gradient(var(--liquid-glass-angle, 145deg),rgb(255 255 255 / .55),rgb(255 255 255 / .11) 50%,rgb(255 255 255 / .42));mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0);mask-composite:exclude}:where(.liquid-glass:active):before{box-shadow:inset 0 0 18px #ffffff2b}@media(prefers-reduced-transparency:reduce),(forced-colors:active){:where(.liquid-glass):before{background:var(--liquid-glass-solid, Canvas);-webkit-backdrop-filter:none;backdrop-filter:none}:where(.liquid-glass):after{background:currentColor}}:where(.liquid-glass-sampled){position:absolute;inset:0;width:100%;height:100%;pointer-events:none;border-radius:inherit;z-index:-2}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
export type
|
|
2
|
-
export type
|
|
1
|
+
export type LiquidGlassRenderer = "viewport" | "backdrop" | "sampled" | "frost" | "off";
|
|
2
|
+
export type LiquidGlassMode = "auto" | LiquidGlassRenderer;
|
|
3
3
|
|
|
4
4
|
export interface LiquidGlassOptions {
|
|
5
|
-
/** Auto (default)
|
|
5
|
+
/** Auto (default) prefers sampled media, then native displacement where supported, then CSS frost. */
|
|
6
6
|
mode?: LiquidGlassMode;
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/** Element behind the lens. IDs and selectors resolve in the lens's document or shadow root. */
|
|
8
|
+
source?: string | HTMLElement | null;
|
|
9
|
+
options?: LiquidGlassOptics;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface LiquidGlassOptics {
|
|
9
13
|
/** Uniform corner radius in CSS pixels. Defaults to 24. */
|
|
10
14
|
radius?: number;
|
|
11
15
|
/** Blur in CSS pixels. Defaults to 2. */
|
|
@@ -42,4 +46,4 @@ export interface LiquidGlassController {
|
|
|
42
46
|
destroy(): void;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
export function liquidGlass(element: HTMLElement, options?: LiquidGlassOptions): LiquidGlassController;
|
|
49
|
+
export function liquidGlass(element: string | HTMLElement, options?: LiquidGlassOptions): LiquidGlassController;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{lensGeometry as Y}from"./optics.js";import{resolveOptions as
|
|
1
|
+
import{lensGeometry as Y}from"./optics.js";import{resolveOptions as j}from"./options.js";import{browserEngine as ee,makeSvgLens as te}from"./renderers/svg.js";import{makeViewportLens as ne}from"./renderers/viewport.js";import{makeSampledLens as re}from"./renderers/sampled.js";const H=new WeakMap,S="liquid-glass";function J(t,d){if(typeof d!="string")return d;const x=t?.getElementById?.(d);if(x)return x;try{return t?.querySelector(d)}catch{return null}}function le(t,d={}){t=J(globalThis.document,t);const i=t?.ownerDocument?.defaultView;if(!i||!(t instanceof i.HTMLElement))throw new TypeError("liquidGlass requires an HTML element");const A=H.get(t);if(A)return A.update(d),A;let n=j(i,d);const O=ee(i),K=O==="blink"&&i.CSS?.supports("backdrop-filter",'url("#glass")'),C=i.matchMedia("(prefers-reduced-motion: reduce)"),P=i.matchMedia("(prefers-reduced-transparency: reduce), (forced-colors: active)"),b=new Map,Q=t.classList.contains(S),k=new i.ResizeObserver(a);let q,F,v,R,M,T,u=null,c=null,y=null,m,f="off",g=!1;function s(e,o){const r="--liquid-glass-"+e;t.style.getPropertyValue(r)!==String(o)&&(b.has(r)||b.set(r,[t.style.getPropertyValue(r),t.style.getPropertyPriority(r)]),t.style.setProperty(r,o))}function G(){for(const[e,[o,r]]of b)t.style.setProperty(e,o,r);b.clear()}function a(){!g&&n.mode!=="off"&&c===null&&(c=i.requestAnimationFrame(B))}function U(){g||n.mode==="off"||(c!==null&&i.cancelAnimationFrame(c),B())}function l(e,o=""){e!=="off"&&s("lens",e==="backdrop"?o:""),f!==e&&(f=e,i.queueMicrotask(()=>{!g&&f===e&&t.dispatchEvent(new i.CustomEvent("liquidglass:rendererchange",{detail:{renderer:e}}))}))}function L(){q?.dispose(),q=void 0}function h(){v&&(v.dispose(),v=R=M=void 0)}function N(e){if(!e)return null;const o=t.getRootNode();return e=J(o,e),e instanceof i.HTMLElement&&e.getRootNode()===o?e:null}function Z(e,o){const r=N(n.source??n.backdrop??(n.mode==="auto"?n.viewportSource:null));return!r?.isConnected||!["img","video","canvas"].includes(r.localName)||t.contains(r)?(h(),!1):(r!==R&&(h(),R=r,v=re(t,r,a,U)),M=Y(e,o,n,M,"viewport"),v.render(M,n)?(p(null),l("sampled"),!0):(v.hide(),!1))}function z(e){(!(e.target instanceof i.Node)||!u?.contains(e.target))&&a()}function p(e){e!==u&&(F?.dispose(),F=void 0,u&&k.unobserve(u),!u&&e&&(i.addEventListener("scroll",z,!0),i.addEventListener("resize",a)),u=e,u&&k.observe(u),u||(i.removeEventListener("scroll",z,!0),i.removeEventListener("resize",a)))}function _(){const e=N(n.source??n.viewportSource);return!e||e.contains(t)||t.contains(e)?null:e}function $(){const e=_();if(p(e?.isConnected?e:null),!u){l("frost");return}F??=ne(t,u,O),f!=="viewport"&&l("frost");const o=n;F.render(n,r=>{n===o&&l(r?"viewport":"frost")})}function B(){c=null;const{mode:e,refraction:o}=n,{clientWidth:r,clientHeight:E}=t;if(P.matches||e==="frost"||!o||!t.isConnected||!r||!E){L(),h(),p(null),l("frost");return}if(e==="sampled"||e==="auto"){if(Z(r,E)){L();return}}else h();if(K&&(e==="auto"||e==="backdrop")){p(null),f!=="backdrop"&&l("frost"),T=Y(r,E,n,T),q??=te(t);const V=n;q.render(T,n,X=>{n===V&&l(X?"backdrop":"frost",X)});return}L(),e==="viewport"?$():(p(null),l("frost"))}function w(){m=void 0,n.mode!=="off"&&(s("light-x",""),s("light-y",""))}function W(e){n.mode==="off"||!n.interactive||C.matches||e.pointerType==="touch"||(m={x:e.clientX,y:e.clientY},y===null&&(y=i.requestAnimationFrame(()=>{if(y=null,!m)return;const{width:o,height:r,left:E,top:V}=t.getBoundingClientRect();!o||!r||(s("light-x",(m.x-E)/o*100+"%"),s("light-y",(m.y-V)/r*100+"%"))})))}function D(){if(n.mode==="off"){c!==null&&i.cancelAnimationFrame(c),c=null,m=void 0,L(),h(),p(null),G(),t.classList.remove(S),l("off");return}t.classList.add(S),s("radius",n.radius+"px"),s("blur",n.blur+"px"),s("saturation",n.saturation),s("tint",n.tint),s("glow",n.glow),s("edge",n.edgeHighlight),s("angle",n.lightAngle+"deg"),n.interactive||w(),f==="off"&&l("frost"),a()}const I={get renderer(){return f},update(e,{replace:o=!1}={}){g||(n=j(i,e,o?void 0:n),D())},refresh:a,destroy(){g||(g=!0,c!==null&&i.cancelAnimationFrame(c),y!==null&&i.cancelAnimationFrame(y),L(),h(),p(null),k.disconnect(),t.removeEventListener("pointermove",W),t.removeEventListener("pointerleave",w),C.removeEventListener("change",w),P.removeEventListener("change",a),G(),Q?t.classList.add(S):t.classList.remove(S),H.delete(t),f="off")}};return H.set(t,I),t.addEventListener("pointermove",W,{passive:!0}),t.addEventListener("pointerleave",w),C.addEventListener("change",w),P.addEventListener("change",a),k.observe(t),D(),I}export{le as liquidGlass};
|
package/build/core/optics.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
function L(a){const t=Math.max(0,Math.min(1,a));return t*t*(3-2*t)}function $(a,t,{radius:C,refraction:N,bevel:D,curvature:P},X,B="backdrop"){const i=Math.min(C,a/2,t/2),u=B==="backdrop",E=Math.min(a,t),Y=Math.min(D,E*(u?.24:.5)),r=Y*(.45+.55*Math.min(1,.625*(N/Y))),j=Math.min(i,r),S=r-j,z=B==="sampled",p=z?.14+.035*P:.34+.03*P,G=.8*(1-p),I=p*(u?.8:1),H=(z?G:Math.min(I,G))/1.5,M=Math.min(N,r*H),W=[B,a,t,i,r,p,M].join("/");if(X?.key===W)return X;const J=u?Math.min(1,1.5*t/a):1,K=u?Math.min(1,1.5*a/t):1,q=Math.min(1,512/Math.max(a,t)),m=Math.max(1,Math.round(a*q)),l=Math.max(1,Math.round(t*q)),A=new Float32Array(m*l*2),O=a/m,Q=t/l;for(let n=0;n<Math.ceil(l/2);n++){const s=i-(n+.5)*Q,c=l-n-1;for(let o=0;o<Math.ceil(m/2);o++){let d=function(U,V,Z,_){const v=2*(U*m+V);A[v]=Z*F*J,A[v+1]=_*F*K};const e=i-(o+.5)*O,b=Math.max(0,e),k=Math.max(0,s),x=b&&k?Math.hypot(b,k):b+k,T=i-x-Math.min(Math.max(e,s),0),F=R(T,Math.max(-e,-s));if(!F)continue;const f=m-o-1,h=f===o?0:x?b/x:+(e>s),y=c===n?0:x?k/x:+(s>e);d(n,o,h,y),f!==o&&d(n,f,-h,y),c!==n&&d(c,o,h,-y),c!==n&&f!==o&&d(c,f,-h,-y)}}function R(n,s){if(n<=0||n>=r||!M)return 0;const c=S?j+S*L(s/(2*S)):r;if(n>=c)return 0;const o=c*p;return Math.min(M,c*H)/M*L(n/o)*(1-L((n-o)/(c-o)))}return{width:a,height:t,columns:m,rows:l,offsets:A,strength:M,key:W}}export{$ as lensGeometry};
|
package/build/core/options.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const
|
|
1
|
+
const f=Object.freeze({mode:"auto",source:null,viewportSource:null,backdrop:null,radius:24,blur:2,refraction:18,bevel:24,curvature:2,chroma:0,saturation:1.35,tint:.14,glow:.23,edgeHighlight:.85,lightAngle:145,interactive:!0}),a={radius:[0,1/0],blur:[0,1/0],refraction:[0,1/0],bevel:[.5,1/0],curvature:[.25,8],chroma:[0,1],saturation:[0,1/0],tint:[0,1],glow:[0,1],edgeHighlight:[0,1],lightAngle:[-1/0,1/0]};function d(c,r,s=f){const o={...s},n=r.options;if(n!==void 0&&(!n||typeof n!="object"||Array.isArray(n)))throw new TypeError("options must be an object");for(const e of["mode","source","viewportSource","backdrop"])r[e]!==void 0&&(o[e]=r[e]);r.source!==void 0&&(o.viewportSource=o.backdrop=null);for(const e of[...Object.keys(a),"interactive"]){const t=n?.[e]!==void 0?n[e]:r[e];t!==void 0&&(o[e]=t)}for(const[e,[t,l]]of Object.entries(a)){const u=o[e];if(!Number.isFinite(u)||u<t||u>l)throw new RangeError("Invalid liquidGlass option: "+e)}if(!["auto","viewport","backdrop","sampled","frost","off"].includes(o.mode))throw new TypeError('mode must be "auto", "viewport", "backdrop", "sampled", "frost", or "off"');if(typeof o.interactive!="boolean")throw new TypeError("interactive must be a boolean");for(const e of["source","viewportSource"]){const t=o[e];if(t!==null&&typeof t!="string"&&(!(t instanceof c.HTMLElement)||t.ownerDocument!==c.document))throw new TypeError(e+" must be an ID, selector, same-document HTML element, or null")}const i=o.backdrop;if(i!==null&&typeof i!="string"&&(!(i instanceof c.HTMLElement)||i.ownerDocument!==c.document||!["img","video","canvas"].includes(i.localName)))throw new TypeError("backdrop must be a selector, a same-document image, video, canvas, or null");return o}export{d as resolveOptions};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{drawWebGl as O}from"./webgl.js";function W(s,e,t){if(s.localName==="canvas")return t;const u=e.videoWidth??e.naturalWidth,d=e.videoHeight??e.naturalHeight;if(!u||!d)return null;const a=s.ownerDocument.defaultView.getComputedStyle(s).objectFit||"fill";if(a==="fill")return t;if(a!=="cover"&&a!=="contain")return null;const i=Math[a==="cover"?"max":"min"](t.width/u,t.height/d),g=u*i,l=d*i;return{left:t.left+(t.width-g)/2,top:t.top+(t.height-l)/2,width:g,height:l}}function H(s,e,t,u){const d=s.ownerDocument,a=d.defaultView,i=d.createElement("canvas");i.className="liquid-glass-sampled",i.setAttribute("aria-hidden","true");const g=i.getContext("2d"),l=d.createElement("canvas"),c=l.getContext("2d"),k=new a.AbortController,{signal:f}=k,n=e.localName==="video"?e:null;let h,p,v;const R=new a.ResizeObserver(t);R.observe(e);function E(){f.aborted||n.paused||n.ended||p!==void 0||v!==void 0||(n.requestVideoFrameCallback?p=n.requestVideoFrameCallback(()=>{p=void 0,u(),E()}):v=a.requestAnimationFrame(()=>{v=void 0,u(),E()}))}if(n){for(const b of["loadeddata","seeked","resize","emptied","pause","ended"])n.addEventListener(b,t,{signal:f});n.addEventListener("play",E,{signal:f}),E()}else e.addEventListener("load",t,{signal:f});const y=e.getRootNode();y.addEventListener("scroll",t,{capture:!0,passive:!0,signal:f}),y!==d&&d.addEventListener("scroll",t,{capture:!0,passive:!0,signal:f}),a.addEventListener("resize",t,{signal:f});function V(){return!n||n.readyState>=2?e:n.poster?(h?.src!==n.poster&&(h=new a.Image,n.crossOrigin!==null&&(h.crossOrigin=n.crossOrigin),h.addEventListener("load",t,{signal:f}),h.src=n.poster),h.complete?h:null):null}return{render(b,q){if(!g||!c||!e.isConnected)return!1;const N=V();if(!N||e.localName==="img"&&!e.complete||e.localName==="canvas"&&(!e.width||!e.height))return!1;const r=s.getBoundingClientRect(),o=e.getBoundingClientRect();if(!r.width||!r.height||!o.width||!o.height||r.right<=o.left||r.bottom<=o.top||r.left>=o.right||r.top>=o.bottom)return!1;const w=W(e,N,o);if(!w)return!1;const{columns:m,rows:C,strength:A}=b;(i.width!==m||i.height!==C||l.width!==m||l.height!==C)&&(i.width=l.width=m,i.height=l.height=C);const F=m/r.width,L=C/r.height;c.clearRect(0,0,m,C),c.save(),c.beginPath(),c.rect((o.left-r.left)*F,(o.top-r.top)*L,o.width*F,o.height*L),c.clip();try{c.drawImage(N,(w.left-r.left)*F,(w.top-r.top)*L,w.width*F,w.height*L)}catch(S){if(S.name!=="InvalidStateError")throw S;return!1}finally{c.restore()}return O(d,l,b,A,q,g)?(i.parentNode!==s&&s.prepend(i),!0):(l.width=m,!1)},hide(){i.remove()},dispose(){k.abort(),R.disconnect(),p!==void 0&&n.cancelVideoFrameCallback?.(p),v!==void 0&&a.cancelAnimationFrame(v),i.remove()}}}export{H as makeSampledLens};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{lensGeometry as
|
|
1
|
+
import{lensGeometry as J}from"../optics.js";import{appendRefraction as K,attrs as k,filterId as D,filterRoot as N,makeMapRequest as Q,setDisplacement as X,svg as g}from"./svg.js";const R=new WeakMap,Y=8*1024*1024,I=4096;function O(i,n,o){let e=R.get(i);if(!e){const{style:t}=i,r=i.ownerDocument.defaultView.getComputedStyle(i).filter;e={value:t.getPropertyValue("filter"),priority:t.getPropertyPriority("filter"),base:r&&r!=="none"?r:"",lenses:new Map},R.set(i,e)}e.lenses.set(n,'url("#'+o+'")'),U(i,e)}function U(i,{base:n,lenses:o,priority:e}){const t=[n,...o.values()].filter(Boolean).join(" ");i.style.getPropertyValue("filter")!==t&&i.style.setProperty("filter",t,e)}function W(i,n){const o=R.get(i);o?.lenses.delete(n)&&(o.lenses.size?U(i,o):(i.style.setProperty("filter",o.value,o.priority),R.delete(i)))}function H(i,n,o){const e=i.getBoundingClientRect(),t=n.getBoundingClientRect();if(e.right<=t.left||e.bottom<=t.top||e.left>=t.right||e.top>=t.bottom)return null;const{offsetWidth:r,offsetHeight:f}=n;if(!r||!f||!t.width||!t.height||!e.width||!e.height)return null;if(o){const a=n.ownerDocument.defaultView.devicePixelRatio||1,w=Math.ceil(Math.max(r,t.width)*a),u=Math.ceil(Math.max(f,t.height)*a);if(w>I||u>I||w*u>Y)return null}const d=r/t.width,l=f/t.height;return{viewport:{x:0,y:0,width:r,height:f},lens:{x:(e.left-t.left)*d,y:(e.top-t.top)*l,width:e.width*d,height:e.height*l}}}function Z({width:i,height:n},o,e){const t=Math.ceil(e+2),r=Math.max(0,Math.floor(o.x-t)),f=Math.max(0,Math.floor(o.y-t)),d=Math.min(i,Math.ceil(o.x+o.width+t)),l=Math.min(n,Math.ceil(o.y+o.height+t));return{x:r/i,y:f/n,width:(d-r)/i,height:(l-f)/n}}function tt(i,n,o){const e={},t=n.ownerDocument,r=t.defaultView,{root:f,defs:d}=N(n),l=Q(t,T);let a,w,u,s,x,C,F,S,P,M;const c=o==="webkit";function T(y=H(i,n,c)){const{options:B,applied:p}=w;if(!y||l.failed||!n.isConnected||!i.isConnected){r.clearTimeout(M),W(n,e),u=void 0,p(!1);return}if(!l.url||l.key!==a.key)return;const{viewport:m,lens:h}=y,{chroma:v}=B,q=[...Object.values(m),...Object.values(h),a.key,v].join("/");if(q===u){p(!0);return}if(u=q,P!==!!v){P=!!v;const b=c?"objectBoundingBox":"userSpaceOnUse";s=g(t,"filter",{id:D(),filterUnits:b,primitiveUnits:b,"color-interpolation-filters":"sRGB"}),C=g(t,"feFlood",{"flood-color":"#808000",result:"neutral"}),x=g(t,"feImage",{preserveAspectRatio:"none",result:"lens"}),s.append(C,x,g(t,"feComposite",{in:"lens",in2:"neutral",operator:"over",result:"offsets"}));const L=s.childElementCount-1;F=K(s,P),c&&(S=[...s.children].slice(L),s.append(g(t,"feComposite",{in:"SourceGraphic",in2:"neutral",operator:"out",result:"outside"}),g(t,"feComposite",{in:"warped",in2:"outside",operator:"arithmetic",k2:1,k3:1}))),d.replaceChildren(s)}const z=x.getAttribute("href")!==l.url,{width:V,height:j}=m,A=c?{x:0,y:0,width:1,height:1}:m,E=c?{x:h.x/V,y:h.y/j,width:h.width/V,height:h.height/j}:h;k(s,A);const G=c?Z(m,h,a.strength):A;if(k(C,G),S)for(const b of S)k(b,G);k(x,{...E,href:l.url}),X(F,a.strength/(c?Math.max(V,j):1),v),c&&(s.id=D()),O(n,e,s.id),c&&z&&(r.clearTimeout(M),M=r.setTimeout(()=>{s.id=D(),O(n,e,s.id)},94)),p(!0)}return{render(y,B){w={options:y,applied:B};const p=H(i,n,c);if(p){const{width:m,height:h}=p.lens;a=J(m,h,y,a,"viewport"),l.requestMap(a)}T(p)},dispose(){l.cancel(),r.clearTimeout(M),W(n,e),f.remove()}}}export{tt as makeViewportLens};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const x=new WeakMap,D=`
|
|
2
|
+
attribute vec2 position;
|
|
3
|
+
varying vec2 uv;
|
|
4
|
+
void main() {
|
|
5
|
+
gl_Position = vec4(position, 0.0, 1.0);
|
|
6
|
+
uv = vec2(position.x + 1.0, 1.0 - position.y) * 0.5;
|
|
7
|
+
}
|
|
8
|
+
`,v=`
|
|
9
|
+
#ifdef GL_FRAGMENT_PRECISION_HIGH
|
|
10
|
+
precision highp float;
|
|
11
|
+
#else
|
|
12
|
+
precision mediump float;
|
|
13
|
+
#endif
|
|
14
|
+
varying vec2 uv;
|
|
15
|
+
uniform sampler2D plate;
|
|
16
|
+
uniform sampler2D lens;
|
|
17
|
+
uniform vec2 strength;
|
|
18
|
+
uniform float chroma;
|
|
19
|
+
uniform vec4 plateRect;
|
|
20
|
+
uniform vec3 rounded;
|
|
21
|
+
|
|
22
|
+
void main() {
|
|
23
|
+
if (rounded.z > 0.0) {
|
|
24
|
+
vec2 halfSize = rounded.xy * 0.5;
|
|
25
|
+
vec2 q = abs((uv - 0.5) * rounded.xy) - halfSize + rounded.z;
|
|
26
|
+
if (length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) > rounded.z) discard;
|
|
27
|
+
}
|
|
28
|
+
vec4 map = texture2D(lens, uv);
|
|
29
|
+
vec2 offset = (map.rg * 255.0 - 128.0) / 127.0 * strength;
|
|
30
|
+
vec2 point = plateRect.xy + uv * plateRect.zw;
|
|
31
|
+
offset *= plateRect.zw;
|
|
32
|
+
vec4 color = texture2D(plate, point + offset);
|
|
33
|
+
if (chroma > 0.0) {
|
|
34
|
+
color.r = texture2D(plate, point + offset * (1.0 - chroma)).r;
|
|
35
|
+
color.b = texture2D(plate, point + offset * (1.0 + chroma)).b;
|
|
36
|
+
}
|
|
37
|
+
gl_FragColor = color;
|
|
38
|
+
}
|
|
39
|
+
`;function A(t){const e=t.createProgram();for(const[n,s]of[[t.VERTEX_SHADER,D],[t.FRAGMENT_SHADER,v]]){const i=t.createShader(n);t.shaderSource(i,s),t.compileShader(i),t.attachShader(e,i),t.deleteShader(i)}if(t.linkProgram(e),!t.getProgramParameter(e,t.LINK_STATUS))return t.deleteProgram(e),null;t.useProgram(e);const r=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,r),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,3,-1,-1,3]),t.STATIC_DRAW);const a=t.getAttribLocation(e,"position");t.enableVertexAttribArray(a),t.vertexAttribPointer(a,2,t.FLOAT,!1,0,0);for(const[n,s]of["plate","lens"].entries())t.activeTexture(t.TEXTURE0+n),t.bindTexture(t.TEXTURE_2D,t.createTexture()),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.uniform1i(t.getUniformLocation(e,s),n);t.pixelStorei(t.UNPACK_COLORSPACE_CONVERSION_WEBGL,t.NONE);const o=t.getUniformLocation(e,"plateRect");return t.uniform4f(o,0,0,1,1),{strength:t.getUniformLocation(e,"strength"),chroma:t.getUniformLocation(e,"chroma"),plateRect:o,rounded:t.getUniformLocation(e,"rounded")}}const U=new WeakMap;function l(t){let e=U.get(t);if(e)return e;const{columns:r,rows:a,offsets:o}=t;e=new Uint8ClampedArray(r*a*4);for(let n=0;n<r*a;n++)e[4*n]=128+127*o[2*n],e[4*n+1]=128+127*o[2*n+1],e[4*n+3]=255;return U.set(t,e),e}function w(t){const e=t.createElement("canvas"),r=e.getContext("webgl",{antialias:!1,depth:!1,stencil:!1,premultipliedAlpha:!1});if(!r)return null;let a=A(r),o;return e.addEventListener("webglcontextlost",n=>{n.preventDefault(),a=null}),e.addEventListener("webglcontextrestored",()=>{a=A(r),o=void 0}),function(s,i,_,{chroma:E},m){if(!a||r.isContextLost())return!1;const{width:c,height:p,columns:f,rows:T}=i;(e.width!==f||e.height!==T)&&(e.width=f,e.height=T),r.viewport(0,0,f,T),r.activeTexture(r.TEXTURE0);try{r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,s)}catch(R){if(R.name!=="SecurityError")throw R;return!1}o!==i.key&&(r.activeTexture(r.TEXTURE1),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,f,T,0,r.RGBA,r.UNSIGNED_BYTE,l(i)),o=i.key);const u=_/(1+E);return r.uniform2f(a.strength,u/c,u/p),r.uniform1f(a.chroma,E),r.drawArrays(r.TRIANGLES,0,3),m.clearRect(0,0,f,T),m.drawImage(e,0,0),!0}}function X(t,e,r,a,o,n){return x.has(t)||x.set(t,w(t)),x.get(t)?.(e,r,a,o,n)??!1}function I(t){const e=t.getContext("webgl",{alpha:!1,antialias:!1,depth:!1}),r=e&&A(e);if(!r)return null;const a=new WeakMap,o=e.createTexture();return e.activeTexture(e.TEXTURE1),e.bindTexture(e.TEXTURE_2D,o),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,1,1,0,e.RGBA,e.UNSIGNED_BYTE,new Uint8Array([128,128,0,255])),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),function(s,i,_){if(e.isContextLost())return!1;e.activeTexture(e.TEXTURE0);try{e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,s)}catch(E){if(E.name==="SecurityError"||E.name==="InvalidStateError")return!1;throw E}e.activeTexture(e.TEXTURE1),e.bindTexture(e.TEXTURE_2D,o),e.viewport(0,0,t.width,t.height),e.uniform4fv(r.plateRect,i),e.uniform2f(r.strength,0,0),e.uniform3f(r.rounded,0,0,0),e.drawArrays(e.TRIANGLES,0,3);for(const{viewport:E,plateRect:m,geometry:c,radius:p}of _){const{columns:f,rows:T,strength:u,width:R,height:h}=c;let d=a.get(c);d?e.bindTexture(e.TEXTURE_2D,d):(d=e.createTexture(),a.set(c,d),e.bindTexture(e.TEXTURE_2D,d),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,f,T,0,e.RGBA,e.UNSIGNED_BYTE,l(c))),e.viewport(...E),e.uniform4fv(r.plateRect,m),e.uniform2f(r.strength,u/R,u/h),e.uniform3f(r.rounded,R,h,p),e.drawArrays(e.TRIANGLES,0,3)}return!0}}export{X as drawWebGl,I as makeVideoCompositor};
|
package/package.json
CHANGED
|
@@ -17,9 +17,11 @@ description: Install and integrate the Liquid Glass JS library with plain JavaSc
|
|
|
17
17
|
|
|
18
18
|
Use an ordinary HTML host with geometry supplied by the app’s CSS and available `::before` and `::after` pseudo-elements. The adapters clean themselves up and treat a new options object as the complete configuration.
|
|
19
19
|
|
|
20
|
-
## Choose the
|
|
20
|
+
## Choose the source
|
|
21
21
|
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
22
|
+
- `liquidGlass("lens-id", { source: "source-id" })` uses automatic renderer selection. An HTML element or a selector also works. Source selectors resolve in the lens's document or shadow root.
|
|
23
|
+
- For regular page HTML, start with `liquidGlass("lens-id")`. Automatic mode uses native backdrop displacement in Chromium and CSS backdrop blur, saturation, tint, and highlights in Firefox and Safari.
|
|
24
|
+
- For targeted refraction, pass a same-origin or CORS-enabled image, video, or canvas as `source`. Keep it borderless and axis-aligned with `object-fit: fill` or centered `cover` or `contain`. Automatic mode tries sampled WebGL first. Use `mode: "sampled"` to select this path explicitly with frost as fallback.
|
|
25
|
+
- Use `{ mode: "viewport", source: "feed" }` to refract a small HTML scene explicitly. Put the lens above the bounded source in a separate layer. With Angular, pass a template reference such as `{ mode: "viewport", source: feed }`.
|
|
26
|
+
- Set `mode: "frost"` for CSS frost or `mode: "off"` to disable glass. Set optional optical adjustments under `options`, for example `{ source: "feed", options: { radius: 24 } }`.
|
|
27
|
+
- Call `glass.refresh()` after moving the lens relative to the source, redrawing a source canvas, or replacing its selector target. Run `mise run dev` and use **Usage** for live HTML examples or **Controls** for explicit media and matching JavaScript, Angular, React, and Vue examples.
|