physics-glass-effects
Version:
Physics-based glass effects using WebGL with real optical science - works with React, Vue, Next.js, and vanilla JS
159 lines (155 loc) • 4.99 kB
TypeScript
/**
* Glass shape types available in the physics engine
*/
type GlassShape = 'sphere' | 'cylinder' | 'lens' | 'prism' | 'flat';
/**
* Background pattern types for demonstrating distortion effects
*/
type BackgroundPattern = 'stripes' | 'grid' | 'circles' | 'texture';
/**
* Glass material presets with realistic optical properties
*/
interface GlassMaterial {
name: string;
refractionIndex: number;
dispersion: number;
description: string;
}
/**
* Mouse interaction configuration
*/
interface MouseConfig {
enabled: boolean;
followCursor: boolean;
centerX?: number;
centerY?: number;
}
/**
* Rendering performance configuration
*/
interface PerformanceConfig {
pixelRatio?: number;
antialias?: boolean;
preserveDrawingBuffer?: boolean;
premultipliedAlpha?: boolean;
}
/**
* Animation configuration
*/
interface AnimationConfig {
enabled: boolean;
speed: number;
surfaceRipples: boolean;
}
/**
* Complete configuration object for PhysicsGlass
*/
interface PhysicsGlassConfig {
shape?: GlassShape;
size?: number;
refractionIndex?: number;
dispersion?: number;
thickness?: number;
backgroundPattern?: BackgroundPattern;
backgroundTexture?: HTMLImageElement | HTMLCanvasElement | string;
mouse?: MouseConfig;
animation?: AnimationConfig;
performance?: PerformanceConfig;
onReady?: () => void;
onError?: (error: Error) => void;
onShapeChange?: (shape: GlassShape) => void;
onMaterialChange?: (material: GlassMaterial) => void;
}
/**
* Public API methods for the PhysicsGlass instance
*/
interface PhysicsGlassAPI {
setShape(shape: GlassShape): void;
setSize(size: number): void;
setRefractionIndex(index: number): void;
setDispersion(dispersion: number): void;
setThickness(thickness: number): void;
setBackgroundPattern(pattern: BackgroundPattern): void;
setBackgroundTexture(texture: HTMLImageElement | HTMLCanvasElement | string): Promise<void>;
setMaterial(material: GlassMaterial | string): void;
getMaterials(): GlassMaterial[];
setMousePosition(x: number, y: number): void;
enableMouseTracking(enabled: boolean): void;
startAnimation(): void;
stopAnimation(): void;
setAnimationSpeed(speed: number): void;
destroy(): void;
resize(): void;
getConfig(): PhysicsGlassConfig;
getCanvas(): HTMLCanvasElement;
isWebGLSupported(): boolean;
}
/**
* Predefined glass materials with realistic properties
*/
declare const GLASS_MATERIALS: Record<string, GlassMaterial>;
/**
* Error types that can be thrown by the library
*/
declare class PhysicsGlassError extends Error {
code: string;
constructor(message: string, code: string);
}
declare const ERROR_CODES: {
readonly WEBGL_NOT_SUPPORTED: "WEBGL_NOT_SUPPORTED";
readonly CANVAS_NOT_FOUND: "CANVAS_NOT_FOUND";
readonly SHADER_COMPILATION_FAILED: "SHADER_COMPILATION_FAILED";
readonly TEXTURE_LOAD_FAILED: "TEXTURE_LOAD_FAILED";
readonly INVALID_CONFIGURATION: "INVALID_CONFIGURATION";
};
declare class PhysicsGlass implements PhysicsGlassAPI {
private canvas;
private config;
private webglState;
private animationId;
private isDestroyed;
private time;
private mousePos;
private uniforms;
private cleanupContextLoss?;
private vertexBuffer?;
constructor(canvas: HTMLCanvasElement | string, config?: PhysicsGlassConfig);
private validateConfig;
private init;
private setupWebGL;
private setupCanvas;
private setupEventListeners;
private handleMouseMove;
private handleMouseLeave;
private handleResize;
private handleContextLoss;
private handleContextRestored;
private getShapeIndex;
private getPatternIndex;
private updateUniforms;
private render;
setShape(shape: GlassShape): void;
setSize(size: number): void;
setRefractionIndex(index: number): void;
setDispersion(dispersion: number): void;
setThickness(thickness: number): void;
setBackgroundPattern(pattern: BackgroundPattern): void;
setBackgroundTexture(texture: HTMLImageElement | HTMLCanvasElement | string): Promise<void>;
setMaterial(material: GlassMaterial | string): void;
getMaterials(): GlassMaterial[];
setMousePosition(x: number, y: number): void;
enableMouseTracking(enabled: boolean): void;
startAnimation(): void;
stopAnimation(): void;
setAnimationSpeed(speed: number): void;
resize(): void;
destroy(): void;
getConfig(): PhysicsGlassConfig;
getCanvas(): HTMLCanvasElement;
isWebGLSupported(): boolean;
}
/**
* Check if WebGL is supported in the current browser
*/
declare function isWebGLSupported(): boolean;
export { AnimationConfig, BackgroundPattern, ERROR_CODES, GLASS_MATERIALS, GlassMaterial, GlassShape, MouseConfig, PerformanceConfig, PhysicsGlass, PhysicsGlassAPI, PhysicsGlassConfig, PhysicsGlassError, PhysicsGlass as default, isWebGLSupported };