@tahoangphuc111/liquid-glass 0.1.0 → 0.2.1

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 CHANGED
@@ -1 +1,177 @@
1
- liquid glass optical refraction for vanilla js, react, and vue using wasm svg filters on desktop chromium and webgl scene capture on mobile and safari
1
+ # liquid-glass
2
+
3
+ Optical refraction and glass displacement for Vanilla JS, React, and Vue 3.
4
+
5
+ Uses WebAssembly (WASM SIMD) displacement maps on Chromium desktop, and falls back to WebGL scene capture or CSS backdrop filters on mobile and Safari.
6
+
7
+ ![liquid-glass interactive playground demo](./assets/demo.gif)
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @tahoangphuc111/liquid-glass
13
+ ```
14
+
15
+ Import base styles once in your root stylesheet or entry file:
16
+
17
+ ```ts
18
+ import "@tahoangphuc111/liquid-glass/styles.css";
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ### Vanilla JavaScript
24
+
25
+ ```ts
26
+ import { createLiquidGlass } from "@tahoangphuc111/liquid-glass";
27
+
28
+ const element = document.querySelector<HTMLElement>(".glass-card")!;
29
+
30
+ const glass = createLiquidGlass(element, {
31
+ surface: "convex_squircle",
32
+ thickness: 40,
33
+ bezel: 24,
34
+ ior: 1.45,
35
+ blur: 2,
36
+ interactive: true
37
+ });
38
+
39
+ // Update options at runtime
40
+ glass.update({ thickness: 50 });
41
+
42
+ // Clean up when removing the element
43
+ glass.destroy();
44
+ ```
45
+
46
+ ### React
47
+
48
+ Wrap components with `<LiquidGlass>` or use the `useLiquidGlass` hook:
49
+
50
+ ```tsx
51
+ import { LiquidGlass } from "@tahoangphuc111/liquid-glass/react";
52
+
53
+ export function GlassCard() {
54
+ return (
55
+ <LiquidGlass
56
+ surface="convex_squircle"
57
+ thickness={40}
58
+ bezel={24}
59
+ ior={1.45}
60
+ interactive
61
+ className="p-6 rounded-2xl"
62
+ >
63
+ <h2>Refracted Card</h2>
64
+ <p>Content rendered inside liquid glass container.</p>
65
+ </LiquidGlass>
66
+ );
67
+ }
68
+ ```
69
+
70
+ Or using the hook directly:
71
+
72
+ ```tsx
73
+ import { useRef } from "react";
74
+ import { useLiquidGlass } from "@tahoangphuc111/liquid-glass/react";
75
+
76
+ export function CustomCard() {
77
+ const ref = useRef<HTMLDivElement>(null);
78
+
79
+ useLiquidGlass(ref, {
80
+ surface: "convex_circle",
81
+ pill: true,
82
+ interactive: true
83
+ });
84
+
85
+ return <div ref={ref} className="p-4">Pill button</div>;
86
+ }
87
+ ```
88
+
89
+ ### Vue 3
90
+
91
+ Register as a global plugin or import components directly:
92
+
93
+ ```vue
94
+ <script setup lang="ts">
95
+ import { LiquidGlass } from "@tahoangphuc111/liquid-glass/vue";
96
+ </script>
97
+
98
+ <template>
99
+ <LiquidGlass
100
+ surface="convex_squircle"
101
+ :thickness="40"
102
+ :bezel="24"
103
+ :interactive="true"
104
+ class="glass-panel"
105
+ >
106
+ <span>Vue Liquid Glass Panel</span>
107
+ </LiquidGlass>
108
+ </template>
109
+ ```
110
+
111
+ Or with the composable:
112
+
113
+ ```vue
114
+ <script setup lang="ts">
115
+ import { ref } from "vue";
116
+ import { useLiquidGlass } from "@tahoangphuc111/liquid-glass/vue";
117
+
118
+ const panelRef = ref<HTMLElement | null>(null);
119
+
120
+ useLiquidGlass(panelRef, {
121
+ surface: "convex_squircle",
122
+ thickness: 35
123
+ });
124
+ </script>
125
+
126
+ <template>
127
+ <div ref="panelRef" class="glass-box">Content</div>
128
+ </template>
129
+ ```
130
+
131
+ ## How It Works
132
+
133
+ 1. **Chromium Desktop (`wasm-svg`)**: Calculates refraction displacement using a WebAssembly SIMD kernel and outputs dynamic displacement maps directly into SVG `feDisplacementMap` filters.
134
+ 2. **Mobile and Safari (`webgl`)**: When full SVG refraction is unsupported, samples background DOM elements onto a canvas or offscreen buffer and applies real-time GLSL fragment shaders.
135
+ 3. **Low-power or fallback (`css`)**: Automatically steps down to CSS `backdrop-filter` if the GPU drops frames or battery-saving mode is active.
136
+
137
+ ## Configuration Options
138
+
139
+ | Option | Type | Default | Description |
140
+ | :--- | :--- | :--- | :--- |
141
+ | `surface` | `"convex_squircle" \| "convex_circle" \| "linear" \| "lip"` | `"convex_squircle"` | Geometric refraction profile. |
142
+ | `engine` | `"auto" \| "wasm-svg" \| "webgl" \| "css"` | `"auto"` | Execution engine. `auto` picks the best supported engine. |
143
+ | `thickness` | `number` | `40` | Virtual lens thickness in pixels. |
144
+ | `bezel` | `number` | `24` | Width of the refraction boundary edge in pixels. |
145
+ | `ior` | `number` | `1.45` | Index of refraction. 1.0 is air, 1.33 is water, 1.5 is glass. |
146
+ | `blur` | `number` | `0` | Gaussian blur radius applied behind the displacement filter. |
147
+ | `radius` | `number` | `24` | Corner radius override in pixels. Defaults to element border radius. |
148
+ | `pill` | `boolean` | `false` | Locks corners to full pill or circular shape. |
149
+ | `hollow` | `boolean` | `false` | Keeps inner area flat and limits refraction only to bezel edges. |
150
+ | `interactive` | `boolean` | `false` | Enables spring physics on cursor hover and click. |
151
+ | `tintColor` | `string` | `undefined` | Overlay tint color in hex or rgb format. |
152
+ | `tintOpacity` | `number` | `0` | Opacity of the tint layer (0 to 1). |
153
+ | `sceneElement` | `HTMLElement` | `null` | Target DOM element to sample for WebGL refraction mode. |
154
+ | `onEngineChange` | `(engine: GlassEngine) => void` | `undefined` | Callback fired when engine resolves or falls back. |
155
+
156
+ ## Development
157
+
158
+ ```bash
159
+ # Install dependencies
160
+ npm install
161
+
162
+ # Start local interactive playground
163
+ npm run dev
164
+
165
+ # Run unit tests
166
+ npm test
167
+
168
+ # Rebuild the Rust WASM SIMD kernel and update src/core/wasm.ts (requires rustup + wasm32-unknown-unknown)
169
+ npm run build:wasm
170
+
171
+ # Build package
172
+ npm run build
173
+ ```
174
+
175
+ ## License
176
+
177
+ MIT