Shape-aware refractive glass component

Photorealistic
Optical Refraction
in React.

Built with Three.js, WebGL2 fragment shaders, and real-time Snell's Law physics. One shared GL context. Zero per-lens overhead.

Drag the lens →

Features

Shared Canvas Architecture

🔮

Physical Optical Refraction

Implements real-time refraction using Snell's law physics, displacement mapping, and customizable index of refraction (IOR).

Shared WebGL Renderer

A single shared WebGL2 context and unified render loop (LiquidGlassProvider) powers multiple glass panels concurrently without context loss or GPU thrashing.

🖼️

Dynamic DOM Background Sampling

Uses html2canvas-pro to capture and refract behind-the-glass page content in real-time.

🎯

Physics Springs & Micro-Interactions

Hover and press spring physics drive responsive refraction scale and scaling transformations.

🎨

Fresnel & Specular Lighting

Edge highlighting, specular lighting rings, and smooth edge anti-aliasing for a photorealistic glass surface.

📐

6 Lens Profiles

Convex Squircle, Convex Circle, Concave Circle, Concave Squircle, Lip Circle, and Lip Squircle — each with distinct optical characteristics.

Architecture Overview

Shared Canvas Provider Pattern

The component is architected around a Shared Canvas Provider pattern to scale efficiently — many glass panels sharing one WebGL2 context, one canvas, and one render loop.

01LiquidGlassProvider

Owns the single fixed full-viewport WebGL renderer, orthographic camera, and rAF loop. Mount once at the root.

02LiquidGlass

Light container that tracks DOM position, captures backdrop snapshots, manages hover/press spring physics, and registers with the provider.

03engine.ts

Pure optical physics pipeline: 1D Snell's Law refraction raytracing, 2D displacement & specular map synthesis, and spring physics engine.

04shaders.ts

High-precision GLSL vertex and fragment shaders for optical UV refraction, shape SDF masking, and Fresnel rim lighting.

05types.ts

Shared TypeScript definitions and the LensType enumeration.

6 Lens Profiles

All instances share one GL context via LiquidGlassProvider

No. 01Convex Circle

Standard spherical convex lens magnification.

No. 02Concave Circle

Inward curving circle lens displacement.

No. 03Lip Circle

S-curve lip edge profile on circular geometry.

No. 04Convex Squircle

Smooth squircle profile tapering outward.

No. 05Concave Squircle

Inward curving squircle profile.

No. 06Lip Squircle

S-curve lip edge profile on squircle geometry.

How It Works

Rendering Pipeline

01

Shared Canvas Overlay

LiquidGlassProvider initializes a single full-viewport WebGL context and orthographic camera aligned to document coordinates.

02

DOM Snapshotting

When backgroundRef is supplied, html2canvas-pro captures the underlying DOM region into a shared canvas texture.

03

1D Optical Physics

engine.ts calculates displacement vector arrays across 128 samples using Snell's Law (η = n₁/n₂) over the chosen 2D lens profile function f(x).

04

2D Texture Mapping

Synthesizes RGBA displacement normal maps (R offset, G offset) and specular reflection maps from the physics output.

05

Single-Pass WebGL Shader

The GLSL fragment shader in shaders.ts shifts UV sampling coordinates in document-space, adding Fresnel rim lighting and specular highlights.