lensfx 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/LICENSE +21 -0
- package/README.md +183 -2
- package/dist/index.d.mts +468 -0
- package/dist/index.d.ts +468 -0
- package/dist/index.js +3722 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3680 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rudraksha132
|
|
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,184 @@
|
|
|
1
|
-
#
|
|
1
|
+
# LensFX (Aave Glass Optical Refraction Engine)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Optical glass simulation and refraction engine for modern web apps.
|
|
4
|
+
> Reverse-engineered from `https://aave.com/design/building-glass-for-the-web`.
|
|
5
|
+
> Stack: **React 18+**, **Framer Motion**, **SVG Filters**, **WebGL / WebGL 2**.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. Architectural Overview
|
|
10
|
+
|
|
11
|
+
LensFX brings real-time optical physics to the DOM and WebGL without resorting to expensive offscreen canvas rasterizers like `html2canvas`. Real DOM elements (text, links, buttons, inputs) remain fully interactive, selectable, and accessible behind the lens.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 2. Directory Structure
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
d:/cf/aave/
|
|
19
|
+
├── src/
|
|
20
|
+
│ ├── index.ts # Unified library entry point
|
|
21
|
+
│ ├── lib/
|
|
22
|
+
│ │ └── glass/
|
|
23
|
+
│ │ ├── types.ts # Lens parameters, theme overrides, slot interfaces
|
|
24
|
+
│ │ ├── math.ts # SDF, erf, dome constants, sync/async map generators
|
|
25
|
+
│ │ └── AaveGlass.tsx # Core <AaveGlass> component & multi-lens manager
|
|
26
|
+
│ ├── components/
|
|
27
|
+
│ │ ├── GlassSwitch.tsx # Switch with rubber-band overshoot & squish physics
|
|
28
|
+
│ │ ├── GlassSlider.tsx # Range slider with track fill refraction target
|
|
29
|
+
│ │ ├── GlassToggleGroup.tsx # Segmented control with spring-eased refraction pill
|
|
30
|
+
│ │ └── GlassPlayground.tsx # Live benchmark & parameter exploration stage
|
|
31
|
+
│ └── webgl/
|
|
32
|
+
│ ├── VideoRefractionRenderer.ts # WebGL video player glass engine & shaders
|
|
33
|
+
│ └── QRRefractionRenderer.ts # WebGL 2 interactive QR code refraction engine
|
|
34
|
+
└── README.md
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 3. Mathematical Foundations
|
|
40
|
+
|
|
41
|
+
### 3.1 Signed Distance Function (SDF) of Rounded Rectangle
|
|
42
|
+
The displacement map calculates pixel distance from the rounded rectangle lens:
|
|
43
|
+
$$\text{ox} = \frac{\text{col} + 0.5}{\text{size}} \cdot 2 \cdot \text{halfW} - \text{halfW}$$
|
|
44
|
+
$$\text{oy} = \frac{\text{row} + 0.5}{\text{size}} \cdot 2 \cdot \text{halfH} - \text{halfH}$$
|
|
45
|
+
$$d_x = \max(|\text{ox}| - \text{halfW} + R, 0), \quad d_y = \max(|\text{oy}| - \text{halfH} + R, 0)$$
|
|
46
|
+
$$\text{sdf} = \sqrt{d_x^2 + d_y^2} + \min(\max(|\text{ox}| - \text{halfW} + R, |\text{oy}| - \text{halfH} + R), 0) - R$$
|
|
47
|
+
|
|
48
|
+
### 3.2 Error Function (`erf`) Approximation
|
|
49
|
+
Used for smooth edge falloff without performance penalties:
|
|
50
|
+
$$\text{erf}(x) = \tanh(1.7724538509 \cdot x), \quad \text{where } \sqrt{\pi} \approx 1.7724538509$$
|
|
51
|
+
$$\text{falloff} = 0.5 \cdot \left(1 + \text{erf}\left(\text{innerSDF} \cdot \frac{1}{\text{depth} \cdot \sqrt{2}}\right)\right)$$
|
|
52
|
+
|
|
53
|
+
### 3.3 3D Dome Geometry Integration
|
|
54
|
+
For parabolic / spherical curvature (`domeDepth > 0`):
|
|
55
|
+
$$a = \max(0.01, \min(\text{domeDepth}, \min(\text{halfW}, \text{halfH}) - 1))$$
|
|
56
|
+
$$R_x = \frac{\text{halfW}^2 + a^2}{2a}, \quad R_y = \frac{\text{halfH}^2 + a^2}{2a}$$
|
|
57
|
+
Numerical integration over 200 samples computes normalisation factors `scaleX` and `scaleY` so the integral sums to $0.5$:
|
|
58
|
+
$$\nabla_{\text{dome}}(p, R) = \frac{\min(p, 0.999R)}{\sqrt{R^2 - \min(p, 0.999R)^2}} \cdot \text{scale}$$
|
|
59
|
+
|
|
60
|
+
### 3.4 Four-Fold Quadrant Symmetry
|
|
61
|
+
For dynamic lenses, the top-left quadrant $[0 \dots \frac{\text{size}}{2}, 0 \dots \frac{\text{size}}{2}]$ is computed once and written directly into all 4 quadrants of the `ImageData` buffer simultaneously:
|
|
62
|
+
```javascript
|
|
63
|
+
// Top-Left: (col, row)
|
|
64
|
+
// Top-Right: (size - 1 - col, row) [negate R displacement]
|
|
65
|
+
// Bottom-Left: (col, size - 1 - row) [negate G displacement]
|
|
66
|
+
// Bottom-Right: (size - 1 - col, size - 1 - row) [negate R & G displacement]
|
|
67
|
+
```
|
|
68
|
+
This achieves sub-2ms generation times on standard hardware.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 4. SVG Filter Pipeline
|
|
73
|
+
|
|
74
|
+
The dynamic SVG filter rendered in `<AaveGlass>`:
|
|
75
|
+
|
|
76
|
+
```xml
|
|
77
|
+
<filter filterUnits="objectBoundingBox" primitiveUnits="objectBoundingBox"
|
|
78
|
+
color-interpolation-filters="sRGB" x="0" y="0" width="1" height="1">
|
|
79
|
+
<!-- 1. Neutral gray background (128, 128, 128) -->
|
|
80
|
+
<feFlood floodColor="rgb(128,128,128)" floodOpacity="1" result="mapBg" />
|
|
81
|
+
|
|
82
|
+
<!-- 2. Dynamic Displacement Map -->
|
|
83
|
+
<feImage href="{dataUrl}" preserveAspectRatio="none" result="rawMap"
|
|
84
|
+
x="{lensLeft}" y="{lensTop}" width="{lensW}" height="{lensH}" />
|
|
85
|
+
|
|
86
|
+
<!-- 3. Composite over neutral gray outside lens -->
|
|
87
|
+
<feComposite in="rawMap" in2="mapBg" operator="over" result="map" />
|
|
88
|
+
|
|
89
|
+
<!-- 4. Aspect-ratio scaling feColorMatrix -->
|
|
90
|
+
<feColorMatrix in="map" type="matrix"
|
|
91
|
+
values="{Sx} 0 0 0 {0.5*(1-Sx)} 0 {Sy} 0 0 {0.5*(1-Sy)} 0 0 1 0 0 0 0 0 1 0"
|
|
92
|
+
result="scaledMap" />
|
|
93
|
+
|
|
94
|
+
<!-- 5. Chromatic Aberration 3-Pass Displacement -->
|
|
95
|
+
<feDisplacementMap in="SourceGraphic" in2="scaledMap"
|
|
96
|
+
scale="{maxScale * (1 + 0.2 * chroma)}" xChannelSelector="R" yChannelSelector="G" />
|
|
97
|
+
<feColorMatrix type="matrix" values="1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0" result="dispR" />
|
|
98
|
+
|
|
99
|
+
<feDisplacementMap in="SourceGraphic" in2="scaledMap"
|
|
100
|
+
scale="{maxScale * (1 + 0.1 * chroma)}" xChannelSelector="R" yChannelSelector="G" />
|
|
101
|
+
<feColorMatrix type="matrix" values="0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0" result="dispG" />
|
|
102
|
+
|
|
103
|
+
<feDisplacementMap in="SourceGraphic" in2="scaledMap"
|
|
104
|
+
scale="{maxScale}" xChannelSelector="R" yChannelSelector="G" />
|
|
105
|
+
<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0" result="dispB" />
|
|
106
|
+
|
|
107
|
+
<feComposite in="dispR" in2="dispG" operator="arithmetic" k1="0" k2="1" k3="1" k4="0" />
|
|
108
|
+
<feComposite in2="dispB" operator="arithmetic" k1="0" k2="1" k3="1" k4="0" result="lensResult" />
|
|
109
|
+
|
|
110
|
+
<!-- 6. Specular Highlight Pass -->
|
|
111
|
+
<!-- Light Theme: Additive Specular -->
|
|
112
|
+
<feColorMatrix in="{isSafari ? 'rawMap' : 'map'}" type="matrix"
|
|
113
|
+
values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 0 {-128/255}" result="specMask" />
|
|
114
|
+
<feComposite in="specMask" in2="lensResult" operator="arithmetic"
|
|
115
|
+
k1="0" k2="{specularStrength}" k3="1" k4="0" result="lensResult" />
|
|
116
|
+
|
|
117
|
+
<!-- 7. Hole Cutout & Over Composite -->
|
|
118
|
+
<feFlood floodColor="black" floodOpacity="1" result="lensMask" />
|
|
119
|
+
<feComposite in="SourceGraphic" in2="lensMask" operator="out" result="holedSG" />
|
|
120
|
+
<feComposite in="lensResult" in2="holedSG" operator="over" />
|
|
121
|
+
</filter>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## 5. WebGL Fallback Pipelines
|
|
127
|
+
|
|
128
|
+
### 5.1 Video Player Refraction (`VideoRefractionRenderer.ts`)
|
|
129
|
+
* **Analytical SDF Evaluation**: Evaluates 3 circle controls and 1 rounded-rectangle seek bar analytically in the fragment shader.
|
|
130
|
+
* **Separable 9-Tap Blur**: Runs a half-resolution 2-pass blur using kernel:
|
|
131
|
+
`[0.0276, 0.0663, 0.1240, 0.1801, 0.2042, 0.1801, 0.1240, 0.0663, 0.0276]`
|
|
132
|
+
* **Apple-Style Vibrancy**: Dynamically balances specular highlight between additive (dark background) and multiplicative (bright background) based on perceptual luminance:
|
|
133
|
+
$$\text{luma} = 0.299R + 0.587G + 0.114B$$
|
|
134
|
+
$$\text{correction} = (0.5 - \text{luma}) \cdot \text{adaptStrength} \cdot \text{mask}$$
|
|
135
|
+
|
|
136
|
+
### 5.2 QR Code Refraction (`QRRefractionRenderer.ts`)
|
|
137
|
+
* **WebGL 2 (`#version 300 es`)**: Uses integer texelFetch into an `R8` occupancy texture for $O(1)$ grid lookup.
|
|
138
|
+
* **Analytical Finder Eye SDFs**: Renders three corner detection eyes with animated hover scales and attenuated refraction displacement.
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 6. Component Physics & Interaction Mechanics
|
|
143
|
+
|
|
144
|
+
### Rubber-Band Overshoot Formula
|
|
145
|
+
Used across `<GlassSwitch>` and `<GlassSlider>`:
|
|
146
|
+
$$f(x) = \text{maxOvershoot} \cdot \left(1 - \left(1 - \min\left(1, \frac{x}{\text{dampening}}\right)\right)^3\right)$$
|
|
147
|
+
|
|
148
|
+
### Perpendicular Squish Simulation Loop
|
|
149
|
+
Models velocity-induced lens deformation with a damped harmonic oscillator:
|
|
150
|
+
$$\text{targetSquish} = \min\left(0.35, \max(0.012 \cdot |\text{velocity}|^{0.75}, \text{hold})\right)$$
|
|
151
|
+
$$a = -180 \cdot (\text{squish} - \text{targetSquish}) - 14 \cdot \text{squishVelocity}$$
|
|
152
|
+
$$\text{lensWidth} = W_{\text{base}} \cdot (1 - 0.2 \cdot \text{squish})$$
|
|
153
|
+
$$\text{lensHeight} = H_{\text{base}} \cdot (1 + 0.4 \cdot \text{squish})$$
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## 7. Quick Usage Example
|
|
158
|
+
|
|
159
|
+
```tsx
|
|
160
|
+
import { GlassSwitch, GlassSlider, GlassToggleGroup } from "./src";
|
|
161
|
+
|
|
162
|
+
export function Demo() {
|
|
163
|
+
return (
|
|
164
|
+
<div style={{ display: "flex", flexDirection: "column", gap: "24px", padding: "40px" }}>
|
|
165
|
+
{/* Switch */}
|
|
166
|
+
<GlassSwitch defaultChecked={true} theme="dark" />
|
|
167
|
+
|
|
168
|
+
{/* Slider */}
|
|
169
|
+
<GlassSlider defaultValue={50} min={0} max={100} theme="dark" />
|
|
170
|
+
|
|
171
|
+
{/* Toggle Group */}
|
|
172
|
+
<GlassToggleGroup
|
|
173
|
+
options={[
|
|
174
|
+
{ id: "hubs", label: "Hubs" },
|
|
175
|
+
{ id: "spokes", label: "Spokes" },
|
|
176
|
+
{ id: "reserves", label: "Reserves" },
|
|
177
|
+
]}
|
|
178
|
+
defaultValue="hubs"
|
|
179
|
+
theme="dark"
|
|
180
|
+
/>
|
|
181
|
+
</div>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
```
|