How FluxFX Works
A simple, clear look at how FluxFX runs effects smoothly — from HTML attribute scanning and a shared animation loop to background GPU workers and automatic FPS protection.
Architecture Pipeline
1. FluxEngine & CentralizedTicker
- Plugin Registry:
FluxEngineholds aMap<string, FXPlugin>. Each effect implementsmount(el, options) => FXInstance. - Multiple Effects per Element: Active instances are tracked in a
WeakMap<HTMLElement, Map<string, ActiveEntry>>, allowing WebGL + DOM combinations likedata-fx="liquid-metal tilt-3d". - Single Shared Loop:
CentralizedTickerruns at most onerequestAnimationFrameloop across the entire page. Whendocument.hiddenis true or every mounted effect reportsisIdle() === true, the loop shuts off completely (0% CPU). - Error Isolation: Any exception thrown inside an effect's
update()setsentry.faulted = trueand pauses only that effect without crashing sibling effects.
2. OffscreenCanvas Worker vs Main-Thread Fallback
WebGLQuad renders every shader onto a single fullscreen triangle ([-1,-1, 3,-1, -1,3]) in WebGL2 (#version 300 es):
- Worker Path: When
offscreen !== falseandsupportsOffscreenCanvas()passes (non-WebKit/Safari browsers withWorker+transferControlToOffscreen),WebGLQuadspawns a Blob Web Worker (OffscreenWorker.ts) and transfers canvas control. Pointer, scroll, resize, and uniform updates stream viapostMessage. - Main-Thread Fallback: On Safari/iOS or if worker initialization emits an error,
fallbackToMainThread()clones a fresh canvas and runs on the main thread usingShaderCache(hashing vertex/fragment sources with FNV-1a and caching uniform locations) plus a recycled context pool.
3. Ping-Pong FBO Simulation
fluid, ink, and aetheria use two paired offscreen framebuffers (pingPong[0] and pingPong[1]):
- Feedback Pass: Renders
feedbackShaderinto the write FBO while sampling the previous frame fromuPrevFrame(advecting velocity in RG channels and dye/ink in BA channels). - Swap: Swaps
readIdxandwriteIdx. - Display Pass: Renders the final shaded frame to screen sampling
uFeedback.
FBOs prefer RGBA16F (HALF_FLOAT) when EXT_color_buffer_float or EXT_color_buffer_half_float is available, automatically falling back to RGBA8. FBO dimensions scale with quality (0.35 low, 0.5 medium, 0.65 high) and cap the longest edge (512px / 896px / 1152px).
4. Dual-Axis Efficiency & 30 ↔ 60 FPS Hover Benchmark
FluxFX coordinates work across the main thread and, when supported, an OffscreenCanvas worker. Its scheduler uses lower ambient and higher interaction frame-rate targets, then returns to the ambient target after activity. Actual frame rate and CPU/GPU cost vary by browser, device, effect, and page workload; the targets shown here are not performance guarantees. Settled DOM effects can stop requesting ticker updates until interaction wakes them.
Zero Main-Thread DOM Bottlenecks
WebGL2 shaders execute inside an OffscreenCanvas Web Worker wherever supported, while DOM interactions share one rAF ticker and sleep at 0% CPU once damped.
Thermal & Battery GPU Guardrails
Frame-rate targets, DynamicResolution DPR scaling, viewport pause (IntersectionObserver), and maxActiveContexts help manage rendering cost. They cannot prevent thermal throttling on every device.
5. Frame Pacing, DynamicResolution & PerformanceGuard
| Subsystem | Behavior |
|---|---|
| Adaptive FPS Pacing | Idles at 30 FPS ambient and boosts to 60 FPS for 1800ms on pointer, touch, scroll, or option updates using exponential dt smoothing. |
| DynamicResolution | Samples a 30-frame ring buffer; drops DPR by 0.15 if average frame time > 20.5ms, and restores by +0.10 after two stable windows < 17.2ms. |
| PerformanceGuard | Escalates across 4 tiers (0: normal, 1: reduced-res, 2: low-fps, 3: survival), progressively lowering DPR, uQuality, FPS (20/30 → 15/30), and FBO size, suspending non-interacted effects in survival mode with a 6s recovery probe. |
| Context Budget | Enforces maxActiveContexts (6 on Safari/mobile, 8 on desktop). Off-viewport or idle canvases are evicted to CSS radial-gradient placeholders and restored on scroll-in or hover. |
6. Theme, Surface Adaptation & Accessibility
- Auto Dark/Light Detection & ACD:
color.tschecksdata-theme,data-mode,data-bs-theme,.dark/.lightclasses, computed ancestorbackgroundColorWCAG luminance, andprefers-color-scheme, smoothly tweeninguThemeanduColor1..3viaMutationObserverwhile shaders combine backdrops and palettes with Additive Color Blending (ACD). - Surface Adaptation: Setting
adaptToSurfaceinspects the hovered child element's background color and rotates clashing hues so overlay effects stay legible. - Reduced Motion: When
prefers-reduced-motion: reducematches (andrespectReducedMotion !== false), WebGL effects render one static frame and halt the loop.