@sessile/dom 0.0.0-stage → 0.2.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 +373 -0
- package/dist/capabilities.d.ts +79 -0
- package/dist/capabilities.js +365 -0
- package/dist/cssSurface.d.ts +65 -0
- package/dist/cssSurface.js +348 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/refractFilter.d.ts +71 -0
- package/dist/refractFilter.js +622 -0
- package/dist/surfaceController.d.ts +146 -0
- package/dist/surfaceController.js +693 -0
- package/package.json +33 -3
- package/README.md +0 -3
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic headless 3-layer DOM surface controller for Sessile.
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates three sibling layers around live interactive DOM content:
|
|
5
|
+
* 1. `refractBackdrop` (unclipped Route B3 Chromium SVG `backdrop-filter: url(#id)` layer)
|
|
6
|
+
* 2. `veil` (SDF contour-clipped tint/frosted layer, also serving Safari/Firefox/solid fallback)
|
|
7
|
+
* 3. `overlayCanvas` (transparent WebGL2/2D Canvas perimeter highlight and outer shadow layer)
|
|
8
|
+
*
|
|
9
|
+
* Keeps live DOM interactive text/controls positioned cleanly between `veil` and `overlayCanvas`,
|
|
10
|
+
* applies lazy SVG filter mounting, and reuses preallocated structures on the 60/120fps hot path.
|
|
11
|
+
*
|
|
12
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
13
|
+
*/
|
|
14
|
+
import { DEFAULT_OPTICAL_MATERIALS, clamp, computeFieldBounds, createGlassOverlayRenderer, extractFieldContourPath, } from '@sessile/core';
|
|
15
|
+
import { createFrameLoop, detectSurfaceCapabilities, measureElementBorderBox, observeElementResize, observeReducedMotion, resolveEffectiveRenderMode, } from "./capabilities.js";
|
|
16
|
+
import { buildBackdropFilterCss, formatOpaqueSolidCss, formatRgbaCss, resolveElementTheme, } from "./cssSurface.js";
|
|
17
|
+
import { createRefractFilterController, } from "./refractFilter.js";
|
|
18
|
+
const MIN_ACTIVE_SIZE = 0.01;
|
|
19
|
+
const MIN_VISIBLE_OPACITY = 0.005;
|
|
20
|
+
const REFRACT_MARGIN_PX = 6;
|
|
21
|
+
const DEFAULT_VEIL_OPTICS = {
|
|
22
|
+
brightness: 1,
|
|
23
|
+
contrast: 1,
|
|
24
|
+
contrastFirst: false,
|
|
25
|
+
};
|
|
26
|
+
const EMPTY_OVERLAY_CAPABILITIES = {
|
|
27
|
+
webgl2: false,
|
|
28
|
+
floatFbo: false,
|
|
29
|
+
maxTextureSize: 0,
|
|
30
|
+
};
|
|
31
|
+
let nextAutoFilterId = 1;
|
|
32
|
+
function hasActivePrimitive(field) {
|
|
33
|
+
for (let i = 0; i < field.primitives.length; i++) {
|
|
34
|
+
const p = field.primitives[i];
|
|
35
|
+
if (p.size.width > MIN_ACTIVE_SIZE && p.size.height > MIN_ACTIVE_SIZE) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function resolveCurrentDpr(explicitDpr) {
|
|
42
|
+
if (explicitDpr !== undefined && Number.isFinite(explicitDpr) && explicitDpr > 0) {
|
|
43
|
+
return explicitDpr;
|
|
44
|
+
}
|
|
45
|
+
if (typeof window !== 'undefined' && Number.isFinite(window.devicePixelRatio) && window.devicePixelRatio > 0) {
|
|
46
|
+
return window.devicePixelRatio;
|
|
47
|
+
}
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
function setVisibilityCached(el, cache, value) {
|
|
51
|
+
if (!el || cache.visibility === value)
|
|
52
|
+
return;
|
|
53
|
+
el.style.visibility = value;
|
|
54
|
+
cache.visibility = value;
|
|
55
|
+
}
|
|
56
|
+
function setTransformSizeCached(el, cache, bounds) {
|
|
57
|
+
const transform = `translate3d(${bounds.x}px, ${bounds.y}px, 0)`;
|
|
58
|
+
const width = `${bounds.width}px`;
|
|
59
|
+
const height = `${bounds.height}px`;
|
|
60
|
+
if (cache.transform !== transform) {
|
|
61
|
+
el.style.transform = transform;
|
|
62
|
+
cache.transform = transform;
|
|
63
|
+
}
|
|
64
|
+
if (cache.width !== width) {
|
|
65
|
+
el.style.width = width;
|
|
66
|
+
cache.width = width;
|
|
67
|
+
}
|
|
68
|
+
if (cache.height !== height) {
|
|
69
|
+
el.style.height = height;
|
|
70
|
+
cache.height = height;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function setBackdropFilterCached(el, cache, value) {
|
|
74
|
+
if (!el || cache.backdropFilter === value)
|
|
75
|
+
return;
|
|
76
|
+
el.style.backdropFilter = value;
|
|
77
|
+
if (typeof el.style.setProperty === 'function') {
|
|
78
|
+
el.style.setProperty('-webkit-backdrop-filter', value);
|
|
79
|
+
}
|
|
80
|
+
cache.backdropFilter = value;
|
|
81
|
+
}
|
|
82
|
+
/** A backdrop-filter that changes nothing but keeps the veil's filtered compositing state. */
|
|
83
|
+
const VEIL_IDENTITY_BACKDROP_FILTER = 'blur(0px)';
|
|
84
|
+
/**
|
|
85
|
+
* Chromium (seen in 156) loses a composited element's `clip-path: path()` mask when its
|
|
86
|
+
* backdrop-filter is removed and clips it to the path's bounding box instead, so a
|
|
87
|
+
* frosted -> refract/solid switch left the veil tint as a rectangle. Once the veil has carried a
|
|
88
|
+
* filter it keeps an identity one instead of `none`; a veil that never had one stays filter-free.
|
|
89
|
+
*/
|
|
90
|
+
function setVeilBackdropFilterCached(el, cache, value) {
|
|
91
|
+
const hadFilter = cache.backdropFilter !== undefined && cache.backdropFilter !== 'none';
|
|
92
|
+
setBackdropFilterCached(el, cache, value === 'none' && hadFilter ? VEIL_IDENTITY_BACKDROP_FILTER : value);
|
|
93
|
+
}
|
|
94
|
+
function setClipPathCached(el, cache, value) {
|
|
95
|
+
if (cache.clipPath === value)
|
|
96
|
+
return;
|
|
97
|
+
el.style.clipPath = value;
|
|
98
|
+
cache.clipPath = value;
|
|
99
|
+
}
|
|
100
|
+
function setBackgroundCached(el, cache, value) {
|
|
101
|
+
if (cache.background === value)
|
|
102
|
+
return;
|
|
103
|
+
el.style.background = value;
|
|
104
|
+
cache.background = value;
|
|
105
|
+
}
|
|
106
|
+
function setOpacityCached(el, cache, value) {
|
|
107
|
+
if (cache.opacity === value)
|
|
108
|
+
return;
|
|
109
|
+
el.style.opacity = value;
|
|
110
|
+
cache.opacity = value;
|
|
111
|
+
}
|
|
112
|
+
function applyManagedBaseLayerStyles(el, positioning, zIndex, isolate) {
|
|
113
|
+
el.setAttribute('aria-hidden', 'true');
|
|
114
|
+
el.style.position = positioning;
|
|
115
|
+
el.style.top = '0';
|
|
116
|
+
el.style.left = '0';
|
|
117
|
+
el.style.pointerEvents = 'none';
|
|
118
|
+
el.style.visibility = 'hidden';
|
|
119
|
+
if (zIndex !== undefined) {
|
|
120
|
+
el.style.zIndex = String(zIndex);
|
|
121
|
+
}
|
|
122
|
+
if (isolate) {
|
|
123
|
+
el.style.isolation = 'isolate';
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Computes integer-snapped surface overlay bounds expanding the active primitives by
|
|
128
|
+
* outer shadow blur/offset and `smin` blend radius so contours and shadows never clip.
|
|
129
|
+
*/
|
|
130
|
+
export function computeAutoSurfaceBounds(field, material) {
|
|
131
|
+
const shadowPad = material.shadowAlpha > 0.001
|
|
132
|
+
? Math.ceil(material.shadowBlur * 1.5 + Math.abs(material.shadowOffsetY) + 8)
|
|
133
|
+
: 0;
|
|
134
|
+
const pad = Math.max(24, Math.ceil(Math.max(0, field.blendK)) + Math.max(8, shadowPad));
|
|
135
|
+
const raw = computeFieldBounds(field, pad);
|
|
136
|
+
if (raw.width <= 0 || raw.height <= 0) {
|
|
137
|
+
return { x: 0, y: 0, width: 0, height: 0 };
|
|
138
|
+
}
|
|
139
|
+
const minX = Math.floor(raw.x);
|
|
140
|
+
const minY = Math.floor(raw.y);
|
|
141
|
+
const maxX = Math.ceil(raw.x + raw.width);
|
|
142
|
+
const maxY = Math.ceil(raw.y + raw.height);
|
|
143
|
+
return {
|
|
144
|
+
x: minX,
|
|
145
|
+
y: minY,
|
|
146
|
+
width: Math.max(1, maxX - minX),
|
|
147
|
+
height: Math.max(1, maxY - minY),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Computes tight Route B3 refraction backdrop bounds clamped inside `surfaceBounds`
|
|
152
|
+
* while preserving enough padding for multi-primitive `smin` liquid bridges.
|
|
153
|
+
*/
|
|
154
|
+
export function computeClampedRefractBounds(field, surfaceBounds) {
|
|
155
|
+
const sMinX = Math.floor(surfaceBounds.x);
|
|
156
|
+
const sMinY = Math.floor(surfaceBounds.y);
|
|
157
|
+
const sMaxX = Math.ceil(surfaceBounds.x + surfaceBounds.width);
|
|
158
|
+
const sMaxY = Math.ceil(surfaceBounds.y + surfaceBounds.height);
|
|
159
|
+
const fallbackSurface = {
|
|
160
|
+
x: sMinX,
|
|
161
|
+
y: sMinY,
|
|
162
|
+
width: Math.max(1, sMaxX - sMinX),
|
|
163
|
+
height: Math.max(1, sMaxY - sMinY),
|
|
164
|
+
};
|
|
165
|
+
const margin = Math.ceil(Math.max(0, field.blendK)) + REFRACT_MARGIN_PX;
|
|
166
|
+
const raw = computeFieldBounds(field, margin);
|
|
167
|
+
if (raw.width <= 0 || raw.height <= 0) {
|
|
168
|
+
return fallbackSurface;
|
|
169
|
+
}
|
|
170
|
+
const minX = Math.max(sMinX, Math.floor(raw.x));
|
|
171
|
+
const minY = Math.max(sMinY, Math.floor(raw.y));
|
|
172
|
+
const maxX = Math.min(sMaxX, Math.ceil(raw.x + raw.width));
|
|
173
|
+
const maxY = Math.min(sMaxY, Math.ceil(raw.y + raw.height));
|
|
174
|
+
if (maxX <= minX || maxY <= minY) {
|
|
175
|
+
return fallbackSurface;
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
x: minX,
|
|
179
|
+
y: minY,
|
|
180
|
+
width: maxX - minX,
|
|
181
|
+
height: maxY - minY,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Stateful headless controller coordinating the 3-layer DOM glass presentation,
|
|
186
|
+
* Route B3 SVG refraction filter, progressive fallback, resize observation, and rAF loop.
|
|
187
|
+
*/
|
|
188
|
+
export class GlassSurfaceController {
|
|
189
|
+
doc;
|
|
190
|
+
container;
|
|
191
|
+
svgMountTarget;
|
|
192
|
+
filterId;
|
|
193
|
+
baseRefraction;
|
|
194
|
+
defaultMaterial;
|
|
195
|
+
disableRefractOnReducedMotion;
|
|
196
|
+
onResizeCallback;
|
|
197
|
+
onCapabilitiesChangeCallback;
|
|
198
|
+
refractBackdropEl;
|
|
199
|
+
veilEl;
|
|
200
|
+
overlayCanvasEl;
|
|
201
|
+
ownsRefractBackdrop;
|
|
202
|
+
ownsVeil;
|
|
203
|
+
ownsOverlayCanvas;
|
|
204
|
+
prevContainerPosition = null;
|
|
205
|
+
prevContainerZIndex = null;
|
|
206
|
+
overlayRenderer;
|
|
207
|
+
overlayCapabilities;
|
|
208
|
+
surfaceCapabilities;
|
|
209
|
+
requestedMode;
|
|
210
|
+
effectiveMode;
|
|
211
|
+
activeTheme;
|
|
212
|
+
containerSize = null;
|
|
213
|
+
lastRenderedFrame = null;
|
|
214
|
+
disposed = false;
|
|
215
|
+
refractFilterController = null;
|
|
216
|
+
stopReducedMotionObserver = null;
|
|
217
|
+
stopResizeObserver = null;
|
|
218
|
+
frameLoop = null;
|
|
219
|
+
loopScheduler;
|
|
220
|
+
refractCache = {};
|
|
221
|
+
veilCache = {};
|
|
222
|
+
overlayCache = {};
|
|
223
|
+
// Preallocated shifted-primitive pool for zero-GC overlay renderer frames.
|
|
224
|
+
shiftedPrimitivesPool = [];
|
|
225
|
+
shiftedField = {
|
|
226
|
+
primitives: [],
|
|
227
|
+
blendK: 0,
|
|
228
|
+
rimOpacity: undefined,
|
|
229
|
+
styleMix: undefined,
|
|
230
|
+
};
|
|
231
|
+
constructor(options = {}) {
|
|
232
|
+
this.doc =
|
|
233
|
+
options.document ??
|
|
234
|
+
options.container?.ownerDocument ??
|
|
235
|
+
(typeof document !== 'undefined' ? document : null);
|
|
236
|
+
this.container = options.container ?? null;
|
|
237
|
+
this.svgMountTarget = options.svgMountTarget;
|
|
238
|
+
this.filterId = options.filterId ?? `sessile-refract-${nextAutoFilterId++}`;
|
|
239
|
+
this.baseRefraction = options.refraction;
|
|
240
|
+
this.defaultMaterial = options.material;
|
|
241
|
+
this.disableRefractOnReducedMotion = options.disableRefractOnReducedMotion ?? false;
|
|
242
|
+
this.onResizeCallback = options.onResize;
|
|
243
|
+
this.onCapabilitiesChangeCallback = options.onCapabilitiesChange;
|
|
244
|
+
this.loopScheduler = options.scheduler;
|
|
245
|
+
const elements = options.elements ?? {};
|
|
246
|
+
const positioning = options.positioning ?? 'absolute';
|
|
247
|
+
const zIndex = options.zIndex;
|
|
248
|
+
const hasCustomRefractSlot = 'refractBackdrop' in elements;
|
|
249
|
+
const hasCustomVeilSlot = 'veil' in elements;
|
|
250
|
+
const hasCustomOverlaySlot = 'overlayCanvas' in elements;
|
|
251
|
+
const defaultRefractZ = !hasCustomRefractSlot ? -2 : undefined;
|
|
252
|
+
const defaultVeilZ = !hasCustomVeilSlot ? -1 : undefined;
|
|
253
|
+
const defaultOverlayZ = !hasCustomOverlaySlot ? 1 : undefined;
|
|
254
|
+
let refractEl = null;
|
|
255
|
+
let ownsRefract = false;
|
|
256
|
+
if (hasCustomRefractSlot) {
|
|
257
|
+
refractEl = elements.refractBackdrop ?? null;
|
|
258
|
+
}
|
|
259
|
+
else if (this.container && this.doc) {
|
|
260
|
+
refractEl = this.doc.createElement('div');
|
|
261
|
+
refractEl.setAttribute('data-sessile-layer', 'refract');
|
|
262
|
+
applyManagedBaseLayerStyles(refractEl, positioning, zIndex?.refract ?? defaultRefractZ);
|
|
263
|
+
ownsRefract = true;
|
|
264
|
+
}
|
|
265
|
+
let veilEl = null;
|
|
266
|
+
let ownsVeil = false;
|
|
267
|
+
if (hasCustomVeilSlot) {
|
|
268
|
+
veilEl = elements.veil ?? null;
|
|
269
|
+
}
|
|
270
|
+
else if (this.container && this.doc) {
|
|
271
|
+
veilEl = this.doc.createElement('div');
|
|
272
|
+
veilEl.setAttribute('data-sessile-layer', 'veil');
|
|
273
|
+
applyManagedBaseLayerStyles(veilEl, positioning, zIndex?.veil ?? defaultVeilZ, true);
|
|
274
|
+
ownsVeil = true;
|
|
275
|
+
}
|
|
276
|
+
let canvasEl = null;
|
|
277
|
+
let ownsCanvas = false;
|
|
278
|
+
if (hasCustomOverlaySlot) {
|
|
279
|
+
canvasEl = elements.overlayCanvas ?? null;
|
|
280
|
+
}
|
|
281
|
+
else if (this.container && this.doc) {
|
|
282
|
+
canvasEl = this.doc.createElement('canvas');
|
|
283
|
+
canvasEl.setAttribute('data-sessile-layer', 'overlay');
|
|
284
|
+
applyManagedBaseLayerStyles(canvasEl, positioning, zIndex?.overlay ?? defaultOverlayZ);
|
|
285
|
+
ownsCanvas = true;
|
|
286
|
+
}
|
|
287
|
+
this.refractBackdropEl = refractEl;
|
|
288
|
+
this.veilEl = veilEl;
|
|
289
|
+
this.overlayCanvasEl = canvasEl;
|
|
290
|
+
this.ownsRefractBackdrop = ownsRefract;
|
|
291
|
+
this.ownsVeil = ownsVeil;
|
|
292
|
+
this.ownsOverlayCanvas = ownsCanvas;
|
|
293
|
+
if (this.container && (ownsRefract || ownsVeil || ownsCanvas)) {
|
|
294
|
+
// Establish a positioning + z-index stacking context on the host container WITHOUT
|
|
295
|
+
// `isolation: isolate` (which would form a CSS Backdrop Root and block exterior refraction)
|
|
296
|
+
// so negative-zIndex backdrops paint behind normal-flow DOM content and overlayCanvas paints on top.
|
|
297
|
+
const computed = typeof globalThis.getComputedStyle === 'function'
|
|
298
|
+
? globalThis.getComputedStyle(this.container)
|
|
299
|
+
: null;
|
|
300
|
+
const effectivePosition = computed?.position || this.container.style.position || 'static';
|
|
301
|
+
const effectiveZIndex = computed?.zIndex || this.container.style.zIndex || 'auto';
|
|
302
|
+
if (effectivePosition === 'static') {
|
|
303
|
+
this.prevContainerPosition = this.container.style.position;
|
|
304
|
+
this.container.style.position = 'relative';
|
|
305
|
+
}
|
|
306
|
+
if (effectiveZIndex === 'auto') {
|
|
307
|
+
this.prevContainerZIndex = this.container.style.zIndex;
|
|
308
|
+
this.container.style.zIndex = '0';
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (this.container) {
|
|
312
|
+
const refNode = 'insertBackdropBefore' in options
|
|
313
|
+
? (options.insertBackdropBefore ?? null)
|
|
314
|
+
: this.container.firstChild;
|
|
315
|
+
const validRefNode = refNode && refNode.parentNode === this.container ? refNode : null;
|
|
316
|
+
if (ownsRefract && refractEl) {
|
|
317
|
+
if (validRefNode) {
|
|
318
|
+
this.container.insertBefore(refractEl, validRefNode);
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
this.container.appendChild(refractEl);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (ownsVeil && veilEl) {
|
|
325
|
+
if (validRefNode) {
|
|
326
|
+
this.container.insertBefore(veilEl, validRefNode);
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
this.container.appendChild(veilEl);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (ownsCanvas && canvasEl) {
|
|
333
|
+
this.container.appendChild(canvasEl);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (this.overlayCanvasEl) {
|
|
337
|
+
this.overlayRenderer = options.overlayRenderer ?? createGlassOverlayRenderer();
|
|
338
|
+
this.overlayCapabilities = this.overlayRenderer.mount(this.overlayCanvasEl);
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
this.overlayRenderer = options.overlayRenderer ?? null;
|
|
342
|
+
this.overlayCapabilities = { ...EMPTY_OVERLAY_CAPABILITIES };
|
|
343
|
+
}
|
|
344
|
+
this.surfaceCapabilities = detectSurfaceCapabilities(options.env);
|
|
345
|
+
this.requestedMode = options.mode ?? 'refract';
|
|
346
|
+
this.effectiveMode = resolveEffectiveRenderMode(this.requestedMode, this.surfaceCapabilities, {
|
|
347
|
+
disableRefractOnReducedMotion: this.disableRefractOnReducedMotion,
|
|
348
|
+
});
|
|
349
|
+
if (options.theme !== undefined) {
|
|
350
|
+
this.activeTheme = options.theme;
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
const themeOpts = options.env && 'matchMedia' in options.env
|
|
354
|
+
? { matchMedia: options.env.matchMedia ?? null }
|
|
355
|
+
: undefined;
|
|
356
|
+
this.activeTheme = resolveElementTheme(this.container, themeOpts);
|
|
357
|
+
}
|
|
358
|
+
this.stopReducedMotionObserver = observeReducedMotion((reduced) => {
|
|
359
|
+
if (this.disposed || reduced === this.surfaceCapabilities.reducedMotion) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
this.surfaceCapabilities = {
|
|
363
|
+
...this.surfaceCapabilities,
|
|
364
|
+
reducedMotion: reduced,
|
|
365
|
+
};
|
|
366
|
+
const nextEffective = resolveEffectiveRenderMode(this.requestedMode, this.surfaceCapabilities, { disableRefractOnReducedMotion: this.disableRefractOnReducedMotion });
|
|
367
|
+
if (nextEffective !== this.effectiveMode) {
|
|
368
|
+
this.effectiveMode = nextEffective;
|
|
369
|
+
if (this.lastRenderedFrame) {
|
|
370
|
+
this.render(this.lastRenderedFrame);
|
|
371
|
+
}
|
|
372
|
+
else if (nextEffective !== 'refract') {
|
|
373
|
+
this.hideRefractBackdrop();
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
this.onCapabilitiesChangeCallback?.(this.getCapabilities());
|
|
377
|
+
}, options.env);
|
|
378
|
+
const resizeTarget = options.observeResize === true
|
|
379
|
+
? this.container
|
|
380
|
+
: options.observeResize && typeof options.observeResize === 'object'
|
|
381
|
+
? options.observeResize
|
|
382
|
+
: null;
|
|
383
|
+
if (resizeTarget) {
|
|
384
|
+
this.stopResizeObserver = observeElementResize(resizeTarget, (size) => {
|
|
385
|
+
if (this.disposed)
|
|
386
|
+
return;
|
|
387
|
+
this.containerSize = size;
|
|
388
|
+
this.onResizeCallback?.(size);
|
|
389
|
+
}, {
|
|
390
|
+
ResizeObserverCtor: options.ResizeObserverCtor,
|
|
391
|
+
emitInitial: true,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
else if (this.container) {
|
|
395
|
+
this.containerSize = measureElementBorderBox(this.container);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
hideRefractBackdrop() {
|
|
399
|
+
setVisibilityCached(this.refractBackdropEl, this.refractCache, 'hidden');
|
|
400
|
+
setBackdropFilterCached(this.refractBackdropEl, this.refractCache, 'none');
|
|
401
|
+
}
|
|
402
|
+
ensureRefractFilterController() {
|
|
403
|
+
if (this.disposed)
|
|
404
|
+
return null;
|
|
405
|
+
if (!this.refractFilterController) {
|
|
406
|
+
this.refractFilterController = createRefractFilterController({
|
|
407
|
+
id: this.filterId,
|
|
408
|
+
document: this.doc ?? undefined,
|
|
409
|
+
mountTarget: this.svgMountTarget,
|
|
410
|
+
...this.baseRefraction,
|
|
411
|
+
});
|
|
412
|
+
this.refractFilterController.mount(this.svgMountTarget);
|
|
413
|
+
}
|
|
414
|
+
return this.refractFilterController;
|
|
415
|
+
}
|
|
416
|
+
prepareShiftedField(source, ox, oy) {
|
|
417
|
+
const srcPrimitives = source.primitives;
|
|
418
|
+
const count = srcPrimitives.length;
|
|
419
|
+
while (this.shiftedPrimitivesPool.length < count) {
|
|
420
|
+
this.shiftedPrimitivesPool.push({
|
|
421
|
+
center: { x: 0, y: 0 },
|
|
422
|
+
size: { width: 0, height: 0 },
|
|
423
|
+
radius: 0,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
const dstPrimitives = this.shiftedField.primitives;
|
|
427
|
+
dstPrimitives.length = count;
|
|
428
|
+
for (let i = 0; i < count; i++) {
|
|
429
|
+
const src = srcPrimitives[i];
|
|
430
|
+
const dst = this.shiftedPrimitivesPool[i];
|
|
431
|
+
dst.id = src.id;
|
|
432
|
+
dst.center.x = src.center.x - ox;
|
|
433
|
+
dst.center.y = src.center.y - oy;
|
|
434
|
+
dst.size.width = src.size.width;
|
|
435
|
+
dst.size.height = src.size.height;
|
|
436
|
+
dst.radius = src.radius;
|
|
437
|
+
dst.weight = src.weight;
|
|
438
|
+
dstPrimitives[i] = dst;
|
|
439
|
+
}
|
|
440
|
+
this.shiftedField.blendK = source.blendK;
|
|
441
|
+
this.shiftedField.rimOpacity = source.rimOpacity;
|
|
442
|
+
this.shiftedField.styleMix = source.styleMix;
|
|
443
|
+
return this.shiftedField;
|
|
444
|
+
}
|
|
445
|
+
/** Renders one frame across the 3 glass sibling layers and updates overlay highlights. */
|
|
446
|
+
render(frame) {
|
|
447
|
+
if (this.disposed)
|
|
448
|
+
return;
|
|
449
|
+
const opacity = clamp(frame.opacity ?? 1, 0, 1);
|
|
450
|
+
if (opacity <= MIN_VISIBLE_OPACITY || !hasActivePrimitive(frame.field)) {
|
|
451
|
+
this.clear();
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const theme = frame.theme ?? this.activeTheme;
|
|
455
|
+
const material = frame.material ?? this.defaultMaterial ?? DEFAULT_OPTICAL_MATERIALS[theme].frosted;
|
|
456
|
+
const veilOptics = frame.veilOptics ?? DEFAULT_VEIL_OPTICS;
|
|
457
|
+
const blurBumpPx = frame.blurBumpPx ?? 0;
|
|
458
|
+
const activeBlur = Math.max(0, material.blur + blurBumpPx);
|
|
459
|
+
const bounds = frame.bounds ?? computeAutoSurfaceBounds(frame.field, material);
|
|
460
|
+
if (bounds.width <= 0 || bounds.height <= 0) {
|
|
461
|
+
this.clear();
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
this.lastRenderedFrame = frame;
|
|
465
|
+
const contourPath = extractFieldContourPath(frame.field, { x: bounds.x, y: bounds.y });
|
|
466
|
+
const opacityStr = `${opacity}`;
|
|
467
|
+
if (this.veilEl) {
|
|
468
|
+
setVisibilityCached(this.veilEl, this.veilCache, 'visible');
|
|
469
|
+
setTransformSizeCached(this.veilEl, this.veilCache, bounds);
|
|
470
|
+
setClipPathCached(this.veilEl, this.veilCache, contourPath ? `path("${contourPath}")` : 'none');
|
|
471
|
+
setOpacityCached(this.veilEl, this.veilCache, opacityStr);
|
|
472
|
+
}
|
|
473
|
+
if (this.effectiveMode === 'solid') {
|
|
474
|
+
this.hideRefractBackdrop();
|
|
475
|
+
if (this.veilEl) {
|
|
476
|
+
setVeilBackdropFilterCached(this.veilEl, this.veilCache, 'none');
|
|
477
|
+
setBackgroundCached(this.veilEl, this.veilCache, frame.solidBackground ?? formatOpaqueSolidCss(material.tintRgba, theme));
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
else if (this.effectiveMode === 'refract' && contourPath && this.refractBackdropEl) {
|
|
481
|
+
const filterController = this.ensureRefractFilterController();
|
|
482
|
+
if (filterController && filterController.mounted) {
|
|
483
|
+
const refractBounds = frame.refractBounds ?? computeClampedRefractBounds(frame.field, bounds);
|
|
484
|
+
const refCfg = frame.refraction;
|
|
485
|
+
const bezel = refCfg?.bezel ?? this.baseRefraction?.bezel ?? material.bevel;
|
|
486
|
+
const strength = refCfg?.strength ?? this.baseRefraction?.strength ?? clamp(material.refraction / 50, 0, 2.5);
|
|
487
|
+
const dispersion = refCfg?.dispersion ?? this.baseRefraction?.dispersion ?? material.dispersion;
|
|
488
|
+
const scaleMultiplier = refCfg?.scaleMultiplier ?? this.baseRefraction?.scaleMultiplier;
|
|
489
|
+
const mapScale = refCfg?.mapScale ?? this.baseRefraction?.mapScale;
|
|
490
|
+
const maxMapPixels = refCfg?.maxMapPixels ?? this.baseRefraction?.maxMapPixels;
|
|
491
|
+
const profile = refCfg?.profile ?? this.baseRefraction?.profile;
|
|
492
|
+
const ior = refCfg?.ior ?? this.baseRefraction?.ior;
|
|
493
|
+
const caustics = refCfg?.caustics ?? this.baseRefraction?.caustics;
|
|
494
|
+
const nineSlice = refCfg?.nineSlice ?? this.baseRefraction?.nineSlice;
|
|
495
|
+
filterController.updateFrame({
|
|
496
|
+
bounds: refractBounds,
|
|
497
|
+
field: frame.field,
|
|
498
|
+
blurSigma: activeBlur,
|
|
499
|
+
saturate: material.saturation,
|
|
500
|
+
brightness: veilOptics.brightness,
|
|
501
|
+
contrast: veilOptics.contrast,
|
|
502
|
+
contrastFirst: veilOptics.contrastFirst,
|
|
503
|
+
bezel,
|
|
504
|
+
strength,
|
|
505
|
+
dispersion,
|
|
506
|
+
scaleMultiplier,
|
|
507
|
+
mapScale,
|
|
508
|
+
maxMapPixels,
|
|
509
|
+
profile,
|
|
510
|
+
ior,
|
|
511
|
+
caustics,
|
|
512
|
+
nineSlice,
|
|
513
|
+
});
|
|
514
|
+
// Route B3: keep `refractBackdrop` unclipped so Chromium avoids rectangular backdrop leaks
|
|
515
|
+
// while the SVG filter self-masks output alpha to the displaced glass interior.
|
|
516
|
+
const urlRef = `url(#${this.filterId})`;
|
|
517
|
+
setVisibilityCached(this.refractBackdropEl, this.refractCache, 'visible');
|
|
518
|
+
setTransformSizeCached(this.refractBackdropEl, this.refractCache, refractBounds);
|
|
519
|
+
setOpacityCached(this.refractBackdropEl, this.refractCache, opacityStr);
|
|
520
|
+
setBackdropFilterCached(this.refractBackdropEl, this.refractCache, urlRef);
|
|
521
|
+
if (this.veilEl) {
|
|
522
|
+
setVeilBackdropFilterCached(this.veilEl, this.veilCache, 'none');
|
|
523
|
+
setBackgroundCached(this.veilEl, this.veilCache, frame.refractTintBackground ?? formatRgbaCss(material.tintRgba));
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
this.applyFrostedVeil(activeBlur, material, veilOptics, frame.frostedBackground);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
this.applyFrostedVeil(activeBlur, material, veilOptics, frame.frostedBackground);
|
|
532
|
+
}
|
|
533
|
+
if (this.overlayCanvasEl) {
|
|
534
|
+
setVisibilityCached(this.overlayCanvasEl, this.overlayCache, 'visible');
|
|
535
|
+
setTransformSizeCached(this.overlayCanvasEl, this.overlayCache, bounds);
|
|
536
|
+
}
|
|
537
|
+
if (this.overlayRenderer) {
|
|
538
|
+
const shifted = this.prepareShiftedField(frame.field, bounds.x, bounds.y);
|
|
539
|
+
this.overlayRenderer.render({
|
|
540
|
+
viewport: { width: bounds.width, height: bounds.height },
|
|
541
|
+
dpr: resolveCurrentDpr(frame.dpr),
|
|
542
|
+
theme,
|
|
543
|
+
field: shifted,
|
|
544
|
+
material,
|
|
545
|
+
opacity,
|
|
546
|
+
blurBumpPx,
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
applyFrostedVeil(activeBlur, material, veilOptics, frostedBackground) {
|
|
551
|
+
this.hideRefractBackdrop();
|
|
552
|
+
if (!this.veilEl)
|
|
553
|
+
return;
|
|
554
|
+
const bf = buildBackdropFilterCss({
|
|
555
|
+
blurPx: activeBlur,
|
|
556
|
+
saturation: material.saturation,
|
|
557
|
+
veil: veilOptics,
|
|
558
|
+
});
|
|
559
|
+
setVeilBackdropFilterCached(this.veilEl, this.veilCache, bf);
|
|
560
|
+
setBackgroundCached(this.veilEl, this.veilCache, frostedBackground ?? formatRgbaCss(material.tintRgba));
|
|
561
|
+
}
|
|
562
|
+
/** Hides all surface layers and clears the overlay canvas without detaching DOM nodes. */
|
|
563
|
+
clear() {
|
|
564
|
+
this.lastRenderedFrame = null;
|
|
565
|
+
this.hideRefractBackdrop();
|
|
566
|
+
setVisibilityCached(this.veilEl, this.veilCache, 'hidden');
|
|
567
|
+
setVeilBackdropFilterCached(this.veilEl, this.veilCache, 'none');
|
|
568
|
+
setVisibilityCached(this.overlayCanvasEl, this.overlayCache, 'hidden');
|
|
569
|
+
this.overlayRenderer?.clear();
|
|
570
|
+
}
|
|
571
|
+
/** Changes the requested render mode and recomputes the effective downgraded mode. */
|
|
572
|
+
setMode(mode) {
|
|
573
|
+
const prevEffective = this.effectiveMode;
|
|
574
|
+
this.requestedMode = mode;
|
|
575
|
+
this.effectiveMode = resolveEffectiveRenderMode(mode, this.surfaceCapabilities, {
|
|
576
|
+
disableRefractOnReducedMotion: this.disableRefractOnReducedMotion,
|
|
577
|
+
});
|
|
578
|
+
if (this.effectiveMode !== prevEffective) {
|
|
579
|
+
if (this.lastRenderedFrame) {
|
|
580
|
+
this.render(this.lastRenderedFrame);
|
|
581
|
+
}
|
|
582
|
+
else if (this.effectiveMode !== 'refract') {
|
|
583
|
+
this.hideRefractBackdrop();
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return this.effectiveMode;
|
|
587
|
+
}
|
|
588
|
+
getMode() {
|
|
589
|
+
return this.requestedMode;
|
|
590
|
+
}
|
|
591
|
+
getEffectiveMode() {
|
|
592
|
+
return this.effectiveMode;
|
|
593
|
+
}
|
|
594
|
+
setTheme(theme) {
|
|
595
|
+
if (theme === this.activeTheme)
|
|
596
|
+
return;
|
|
597
|
+
this.activeTheme = theme;
|
|
598
|
+
if (this.lastRenderedFrame) {
|
|
599
|
+
this.render(this.lastRenderedFrame);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
getTheme() {
|
|
603
|
+
return this.activeTheme;
|
|
604
|
+
}
|
|
605
|
+
getCapabilities() {
|
|
606
|
+
return {
|
|
607
|
+
...this.surfaceCapabilities,
|
|
608
|
+
overlay: { ...this.overlayCapabilities },
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
getContainerSize() {
|
|
612
|
+
if (this.containerSize) {
|
|
613
|
+
return { ...this.containerSize };
|
|
614
|
+
}
|
|
615
|
+
if (this.container) {
|
|
616
|
+
return measureElementBorderBox(this.container);
|
|
617
|
+
}
|
|
618
|
+
return null;
|
|
619
|
+
}
|
|
620
|
+
getElements() {
|
|
621
|
+
return {
|
|
622
|
+
refractBackdrop: this.refractBackdropEl,
|
|
623
|
+
veil: this.veilEl,
|
|
624
|
+
overlayCanvas: this.overlayCanvasEl,
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
/** Starts a clamped `requestAnimationFrame` loop driving frames via `produceFrame`. */
|
|
628
|
+
startLoop(produceFrame) {
|
|
629
|
+
if (this.disposed)
|
|
630
|
+
return;
|
|
631
|
+
this.stopLoop();
|
|
632
|
+
this.frameLoop = createFrameLoop((dtSec, nowMs) => {
|
|
633
|
+
if (this.disposed)
|
|
634
|
+
return false;
|
|
635
|
+
const out = produceFrame(dtSec, nowMs);
|
|
636
|
+
if (out === false) {
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
if (out && typeof out === 'object') {
|
|
640
|
+
this.render(out);
|
|
641
|
+
}
|
|
642
|
+
return true;
|
|
643
|
+
}, { scheduler: this.loopScheduler });
|
|
644
|
+
this.frameLoop.start();
|
|
645
|
+
}
|
|
646
|
+
stopLoop() {
|
|
647
|
+
if (this.frameLoop) {
|
|
648
|
+
this.frameLoop.stop();
|
|
649
|
+
this.frameLoop = null;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
isLoopRunning() {
|
|
653
|
+
return this.frameLoop?.isRunning() ?? false;
|
|
654
|
+
}
|
|
655
|
+
/** Disposes observers, rAF loop, SVG filter, overlay renderer, and any managed sibling nodes. */
|
|
656
|
+
dispose() {
|
|
657
|
+
if (this.disposed)
|
|
658
|
+
return;
|
|
659
|
+
this.disposed = true;
|
|
660
|
+
this.stopLoop();
|
|
661
|
+
this.stopReducedMotionObserver?.();
|
|
662
|
+
this.stopReducedMotionObserver = null;
|
|
663
|
+
this.stopResizeObserver?.();
|
|
664
|
+
this.stopResizeObserver = null;
|
|
665
|
+
this.clear();
|
|
666
|
+
this.overlayRenderer?.dispose();
|
|
667
|
+
this.refractFilterController?.dispose();
|
|
668
|
+
this.refractFilterController = null;
|
|
669
|
+
if (this.ownsRefractBackdrop && this.refractBackdropEl?.parentNode) {
|
|
670
|
+
this.refractBackdropEl.parentNode.removeChild(this.refractBackdropEl);
|
|
671
|
+
}
|
|
672
|
+
if (this.ownsVeil && this.veilEl?.parentNode) {
|
|
673
|
+
this.veilEl.parentNode.removeChild(this.veilEl);
|
|
674
|
+
}
|
|
675
|
+
if (this.ownsOverlayCanvas && this.overlayCanvasEl?.parentNode) {
|
|
676
|
+
this.overlayCanvasEl.parentNode.removeChild(this.overlayCanvasEl);
|
|
677
|
+
}
|
|
678
|
+
if (this.container) {
|
|
679
|
+
if (this.prevContainerPosition !== null) {
|
|
680
|
+
this.container.style.position = this.prevContainerPosition;
|
|
681
|
+
this.prevContainerPosition = null;
|
|
682
|
+
}
|
|
683
|
+
if (this.prevContainerZIndex !== null) {
|
|
684
|
+
this.container.style.zIndex = this.prevContainerZIndex;
|
|
685
|
+
this.prevContainerZIndex = null;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
/** Factory helper creating a configured `GlassSurfaceController`. */
|
|
691
|
+
export function createGlassSurfaceController(options) {
|
|
692
|
+
return new GlassSurfaceController(options);
|
|
693
|
+
}
|