@tahoangphuc111/liquid-glass 0.1.0 → 0.1.1

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.
@@ -208,6 +208,9 @@ function revokeBlobUrlSafely(url) {
208
208
  URL.revokeObjectURL(url);
209
209
  }
210
210
  }
211
+ if (typeof window !== "undefined") {
212
+ initWasmSimdModule();
213
+ }
211
214
 
212
215
  // src/core/telemetry.ts
213
216
  var JANK_WINDOW_MS = 1e3;
@@ -1839,12 +1842,20 @@ function applyLetterSpacing(ctx, letterSpacing) {
1839
1842
  if (!("letterSpacing" in target)) return;
1840
1843
  target.letterSpacing = letterSpacing === "normal" ? "0px" : letterSpacing;
1841
1844
  }
1845
+ var blurCache = /* @__PURE__ */ new Map();
1842
1846
  function parseCssBlurPx(filterStr) {
1843
1847
  if (!filterStr || filterStr === "none") return 0;
1848
+ const cached = blurCache.get(filterStr);
1849
+ if (cached !== void 0) return cached;
1844
1850
  const match = filterStr.match(/blur\(\s*([\d.]+)px\s*\)/i);
1845
- if (!match) return 0;
1846
- const value = Number.parseFloat(match[1]);
1847
- return Number.isFinite(value) && value > 0 ? Math.min(80, value) : 0;
1851
+ let value = 0;
1852
+ if (match) {
1853
+ const parsed = Number.parseFloat(match[1]);
1854
+ value = Number.isFinite(parsed) && parsed > 0 ? Math.min(80, parsed) : 0;
1855
+ }
1856
+ if (blurCache.size > 200) blurCache.clear();
1857
+ blurCache.set(filterStr, value);
1858
+ return value;
1848
1859
  }
1849
1860
  function splitTopLevelArgs(inner) {
1850
1861
  const parts = [];
@@ -2012,16 +2023,26 @@ function drawRadialGradientBox(ctx, backgroundImage, x, y, width, height, radius
2012
2023
  fillRoundedOrRect(ctx, x, y, width, height, radius);
2013
2024
  return true;
2014
2025
  }
2026
+ var gradientColorsCache = /* @__PURE__ */ new Map();
2015
2027
  function parseLinearGradientColors(backgroundImage) {
2028
+ const cached = gradientColorsCache.get(backgroundImage);
2029
+ if (cached !== void 0) return cached;
2016
2030
  const startIdx = backgroundImage.indexOf("linear-gradient(");
2017
- if (startIdx < 0) return [];
2031
+ if (startIdx < 0) {
2032
+ if (gradientColorsCache.size > 200) gradientColorsCache.clear();
2033
+ gradientColorsCache.set(backgroundImage, []);
2034
+ return [];
2035
+ }
2018
2036
  const inner = backgroundImage.slice(startIdx + 16);
2019
2037
  const tokens = inner.match(COLOR_TOKEN_PATTERN) ?? [];
2020
- return tokens.filter(
2038
+ const result = tokens.filter(
2021
2039
  (token) => !["to", "top", "bottom", "left", "right", "deg", "rad", "turn", "from", "at"].includes(
2022
2040
  token.toLowerCase()
2023
2041
  )
2024
2042
  );
2043
+ if (gradientColorsCache.size > 200) gradientColorsCache.clear();
2044
+ gradientColorsCache.set(backgroundImage, result);
2045
+ return result;
2025
2046
  }
2026
2047
  function resolveTextFill(ctx, style, rect, sourceRect) {
2027
2048
  const clip = style.backgroundClip || style.webkitBackgroundClip;
@@ -2483,6 +2504,11 @@ function captureDomToCanvas(source, canvas, range, pixelRatio, radius, backdropO
2483
2504
  function createSceneCaptureController(sourceElement, getQualityOptions) {
2484
2505
  const listeners = /* @__PURE__ */ new Set();
2485
2506
  let captureCanvas = typeof document !== "undefined" ? document.createElement("canvas") : null;
2507
+ if (captureCanvas) {
2508
+ captureCanvas.width = 0;
2509
+ captureCanvas.height = 0;
2510
+ }
2511
+ let hasCaptured = false;
2486
2512
  let captureFrame = null;
2487
2513
  let disposed = false;
2488
2514
  const range = typeof document !== "undefined" ? document.createRange() : null;
@@ -2493,6 +2519,7 @@ function createSceneCaptureController(sourceElement, getQualityOptions) {
2493
2519
  const pixelRatio = typeof resolved === "number" ? resolved : resolved?.pixelRatio;
2494
2520
  const radius = typeof resolved === "number" ? void 0 : resolved?.radius;
2495
2521
  if (captureDomToCanvas(sourceElement, captureCanvas, range, pixelRatio, radius)) {
2522
+ hasCaptured = true;
2496
2523
  for (const listener of listeners) {
2497
2524
  listener(captureCanvas, sourceElement);
2498
2525
  }
@@ -2561,14 +2588,14 @@ function createSceneCaptureController(sourceElement, getQualityOptions) {
2561
2588
  scheduleCapture();
2562
2589
  return {
2563
2590
  get canvas() {
2564
- return captureCanvas;
2591
+ return hasCaptured ? captureCanvas : null;
2565
2592
  },
2566
2593
  get source() {
2567
2594
  return sourceElement;
2568
2595
  },
2569
2596
  subscribe(listener) {
2570
2597
  listeners.add(listener);
2571
- if (captureCanvas && captureCanvas.width > 0 && sourceElement.isConnected) {
2598
+ if (hasCaptured && captureCanvas && captureCanvas.width > 0 && sourceElement.isConnected) {
2572
2599
  listener(captureCanvas, sourceElement);
2573
2600
  }
2574
2601
  return () => {
@@ -3296,16 +3323,16 @@ function createInteractiveSpring(onFrame, onRest) {
3296
3323
  }
3297
3324
 
3298
3325
  // src/core/controller.ts
3299
- function computeCornerRadiusPx(el, width, height, config) {
3326
+ function computeCornerRadiusPx(el, width, height, config, cachedRadiusStr) {
3300
3327
  const halfMin = Math.min(width, height) * 0.5;
3301
- if (config.pill) return halfMin;
3302
- const rawRadius = typeof window !== "undefined" && typeof window.getComputedStyle === "function" ? window.getComputedStyle(el).borderTopLeftRadius.trim() : "";
3328
+ if (config.pill) return { radius: halfMin, rawRadiusStr: "" };
3329
+ const rawRadius = cachedRadiusStr ?? (typeof window !== "undefined" && typeof window.getComputedStyle === "function" ? window.getComputedStyle(el).borderTopLeftRadius.trim() : "");
3303
3330
  const parsed = rawRadius.endsWith("%") ? parseFloat(rawRadius) / 100 * Math.min(width, height) : parseFloat(rawRadius);
3304
3331
  if (Number.isFinite(parsed)) {
3305
- return Math.min(Math.max(parsed, 0), halfMin);
3332
+ return { radius: Math.min(Math.max(parsed, 0), halfMin), rawRadiusStr: rawRadius };
3306
3333
  }
3307
3334
  const fallback = config.radius ?? Math.min(Math.round(height * 0.5), 24);
3308
- return Math.min(Math.max(fallback, 0), halfMin);
3335
+ return { radius: Math.min(Math.max(fallback, 0), halfMin), rawRadiusStr: rawRadius };
3309
3336
  }
3310
3337
  function createLiquidGlass(element, initialOptions = {}) {
3311
3338
  let options = { ...initialOptions };
@@ -3313,12 +3340,14 @@ function createLiquidGlass(element, initialOptions = {}) {
3313
3340
  let activeEngine = "css";
3314
3341
  let hasInitializedEngine = false;
3315
3342
  let destroyed = false;
3343
+ let cachedRadiusStr = null;
3316
3344
  let webglCanvas = null;
3317
3345
  let webglRenderer = null;
3318
3346
  let webglResizeObserver = null;
3319
3347
  let localSceneCapture = null;
3320
3348
  let unsubscribeCapture = null;
3321
- let sceneCanvas = options.sceneCanvas ?? null;
3349
+ let externalSceneCanvas = options.sceneCanvas ?? null;
3350
+ let capturedSceneCanvas = null;
3322
3351
  let sceneEl = options.sceneElement ?? null;
3323
3352
  let scrollFrameId = null;
3324
3353
  let renderFrameId = null;
@@ -3335,7 +3364,7 @@ function createLiquidGlass(element, initialOptions = {}) {
3335
3364
  let pressGlow = 0;
3336
3365
  element.setAttribute("data-lg-surface", "");
3337
3366
  element.classList.add("lg-surface");
3338
- const hasSceneInput = () => Boolean(sceneCanvas || sceneEl || options.sceneElement);
3367
+ const hasSceneInput = () => Boolean(externalSceneCanvas || sceneEl || options.sceneElement);
3339
3368
  const setEngine = (engine) => {
3340
3369
  const changed = !hasInitializedEngine || activeEngine !== engine;
3341
3370
  hasInitializedEngine = true;
@@ -3362,6 +3391,7 @@ function createLiquidGlass(element, initialOptions = {}) {
3362
3391
  } else {
3363
3392
  element.style.removeProperty("--lg-rim-opacity");
3364
3393
  }
3394
+ cachedRadiusStr = null;
3365
3395
  const existingState = elementStateMap.get(element);
3366
3396
  if (existingState) {
3367
3397
  existingState.cachedRadiusStr = null;
@@ -3386,14 +3416,17 @@ function createLiquidGlass(element, initialOptions = {}) {
3386
3416
  const width = element.offsetWidth || Math.round(element.getBoundingClientRect().width);
3387
3417
  const height = element.offsetHeight || Math.round(element.getBoundingClientRect().height);
3388
3418
  if (width < 2 || height < 2) return;
3389
- const radius = computeCornerRadiusPx(element, width, height, config);
3419
+ const radiusResult = computeCornerRadiusPx(element, width, height, config, cachedRadiusStr);
3420
+ cachedRadiusStr = radiusResult.rawRadiusStr;
3421
+ const radius = radiusResult.radius;
3390
3422
  const halfMin = Math.min(width, height) * 0.5;
3391
3423
  const bezel = Math.max(1, Math.min(config.bezel, halfMin - 1));
3392
3424
  const tintColor = parseCssColorToRgb(options.tintColor);
3393
3425
  const tintOpacity = options.tintOpacity ?? 0.12;
3394
3426
  const highlight = Math.min(1, (options.highlight ?? 0.72) + pressGlow);
3427
+ const activeSceneCanvas = externalSceneCanvas ?? capturedSceneCanvas;
3395
3428
  const sceneRect = computeSceneRect();
3396
- const hasSceneTex = Boolean(sceneCanvas && sceneCanvas.width > 0 && sceneRect);
3429
+ const hasSceneTex = Boolean(activeSceneCanvas && activeSceneCanvas.width > 0 && sceneRect);
3397
3430
  element.setAttribute("data-lg-webgl-scene", hasSceneTex ? "1" : "0");
3398
3431
  if (!hasSceneTex && config.coreFilter) {
3399
3432
  element.style.setProperty("--lg-filter", config.coreFilter);
@@ -3457,6 +3490,7 @@ function createLiquidGlass(element, initialOptions = {}) {
3457
3490
  unsubscribeCapture = null;
3458
3491
  localSceneCapture?.destroy();
3459
3492
  localSceneCapture = null;
3493
+ capturedSceneCanvas = null;
3460
3494
  webglRenderer?.dispose();
3461
3495
  webglRenderer = null;
3462
3496
  if (webglCanvas) {
@@ -3499,17 +3533,25 @@ function createLiquidGlass(element, initialOptions = {}) {
3499
3533
  return;
3500
3534
  }
3501
3535
  }
3502
- if (!sceneCanvas && (sceneEl || options.sceneElement)) {
3536
+ if (!externalSceneCanvas && (sceneEl || options.sceneElement)) {
3503
3537
  const targetSource = sceneEl ?? options.sceneElement ?? null;
3504
3538
  if (targetSource && (!localSceneCapture || localSceneCapture.source !== targetSource)) {
3505
3539
  unsubscribeCapture?.();
3506
3540
  localSceneCapture?.destroy();
3541
+ capturedSceneCanvas = null;
3507
3542
  localSceneCapture = createSceneCaptureController(
3508
3543
  targetSource,
3509
3544
  () => {
3510
3545
  const width = element.offsetWidth || Math.round(element.getBoundingClientRect().width);
3511
3546
  const height = element.offsetHeight || Math.round(element.getBoundingClientRect().height);
3512
- const radius = width >= 2 && height >= 2 ? computeCornerRadiusPx(element, width, height, config) : config.pill ? 9999 : config.radius;
3547
+ let radius;
3548
+ if (width >= 2 && height >= 2) {
3549
+ const res = computeCornerRadiusPx(element, width, height, config, cachedRadiusStr);
3550
+ cachedRadiusStr = res.rawRadiusStr;
3551
+ radius = res.radius;
3552
+ } else {
3553
+ radius = config.pill ? 9999 : config.radius;
3554
+ }
3513
3555
  return {
3514
3556
  pixelRatio: options.pixelRatio,
3515
3557
  radius
@@ -3517,19 +3559,29 @@ function createLiquidGlass(element, initialOptions = {}) {
3517
3559
  }
3518
3560
  );
3519
3561
  unsubscribeCapture = localSceneCapture.subscribe((canvas, source) => {
3520
- sceneCanvas = canvas;
3562
+ capturedSceneCanvas = canvas;
3521
3563
  sceneEl = source;
3522
3564
  lastSceneBackdropKey = getSceneBackdropKey();
3523
3565
  webglRenderer?.setSceneCanvas(canvas);
3524
3566
  scheduleWebglRender();
3525
3567
  });
3568
+ } else if (capturedSceneCanvas && capturedSceneCanvas.width > 0) {
3569
+ webglRenderer.setSceneCanvas(capturedSceneCanvas);
3526
3570
  }
3527
- } else if (sceneCanvas) {
3571
+ } else if (externalSceneCanvas) {
3528
3572
  unsubscribeCapture?.();
3529
3573
  unsubscribeCapture = null;
3530
3574
  localSceneCapture?.destroy();
3531
3575
  localSceneCapture = null;
3532
- webglRenderer.setSceneCanvas(sceneCanvas);
3576
+ capturedSceneCanvas = null;
3577
+ webglRenderer.setSceneCanvas(externalSceneCanvas);
3578
+ } else {
3579
+ unsubscribeCapture?.();
3580
+ unsubscribeCapture = null;
3581
+ localSceneCapture?.destroy();
3582
+ localSceneCapture = null;
3583
+ capturedSceneCanvas = null;
3584
+ webglRenderer.setSceneCanvas(null);
3533
3585
  }
3534
3586
  if (!webglResizeObserver && typeof ResizeObserver === "function") {
3535
3587
  webglResizeObserver = new ResizeObserver(scheduleWebglRender);
@@ -3659,7 +3711,8 @@ function createLiquidGlass(element, initialOptions = {}) {
3659
3711
  reducedTransparencyQuery2?.addEventListener?.("change", applyEngine);
3660
3712
  highContrastQuery2?.addEventListener?.("change", applyEngine);
3661
3713
  onWasmReady(() => {
3662
- if (!destroyed && (options.engine ?? "auto") === "auto" && canUseSvgGlass()) {
3714
+ const mode = options.engine ?? "auto";
3715
+ if (!destroyed && (mode === "auto" || mode === "wasm-svg") && canUseSvgGlass()) {
3663
3716
  applyEngine();
3664
3717
  }
3665
3718
  });
@@ -3677,12 +3730,12 @@ function createLiquidGlass(element, initialOptions = {}) {
3677
3730
  const prevPill = config.pill;
3678
3731
  options = { ...options, ...newOptions };
3679
3732
  if ("sceneCanvas" in newOptions) {
3680
- sceneCanvas = newOptions.sceneCanvas ?? null;
3733
+ externalSceneCanvas = newOptions.sceneCanvas ?? null;
3681
3734
  }
3682
3735
  if ("sceneElement" in newOptions) {
3683
3736
  const nextSource = newOptions.sceneElement ?? null;
3684
- if (nextSource !== sceneEl && !("sceneCanvas" in newOptions) && !options.sceneCanvas) {
3685
- sceneCanvas = null;
3737
+ if (nextSource !== sceneEl) {
3738
+ capturedSceneCanvas = null;
3686
3739
  }
3687
3740
  sceneEl = nextSource;
3688
3741
  }
@@ -3706,6 +3759,7 @@ function createLiquidGlass(element, initialOptions = {}) {
3706
3759
  }
3707
3760
  scheduleWebglRender();
3708
3761
  } else if (activeEngine === "wasm-svg") {
3762
+ cachedRadiusStr = null;
3709
3763
  const existingState = elementStateMap.get(element);
3710
3764
  if (existingState) existingState.cachedRadiusStr = null;
3711
3765
  registerManagedElement(element, config);
@@ -3715,8 +3769,11 @@ function createLiquidGlass(element, initialOptions = {}) {
3715
3769
  },
3716
3770
  setSceneCanvas(canvas, nextSceneEl) {
3717
3771
  if (destroyed) return;
3718
- sceneCanvas = canvas;
3772
+ externalSceneCanvas = canvas;
3719
3773
  if (nextSceneEl !== void 0) {
3774
+ if (nextSceneEl !== sceneEl) {
3775
+ capturedSceneCanvas = null;
3776
+ }
3720
3777
  sceneEl = nextSceneEl;
3721
3778
  }
3722
3779
  if (canvas) {
@@ -3724,6 +3781,7 @@ function createLiquidGlass(element, initialOptions = {}) {
3724
3781
  unsubscribeCapture = null;
3725
3782
  localSceneCapture?.destroy();
3726
3783
  localSceneCapture = null;
3784
+ capturedSceneCanvas = null;
3727
3785
  }
3728
3786
  if (activeEngine === "webgl" && webglRenderer) {
3729
3787
  if (!canvas && (sceneEl || options.sceneElement)) {
@@ -3769,5 +3827,5 @@ function createLiquidGlass(element, initialOptions = {}) {
3769
3827
  }
3770
3828
 
3771
3829
  export { BLUR_EDGE_MODE, CSS_FALLBACK_BACKDROP, DEFAULT_GLASS_CONFIG, GLASS_COLOR_MATRIX, GlassWebglRenderer, MAP_MAX_PIXELS, MAP_MAX_SCALE, MAP_MIN_SCALE, MAP_PRELOAD_TIMEOUT_MS, MAX_ELEMENT_DEVICE_AREA, MAX_TOTAL_DEVICE_AREA, PREBLUR_MODE, SVG_NS, USE_CSS_SATURATION, applyQualityTier, canUseSvgGlass, canUseWebglRefraction, captureDomToCanvas, checkIsChromium, createLiquidGlass, createSceneCaptureController, destroyLiquidGlass, downgradeLiquidGlass, downgradeLiquidGlassToCss, formatGlassFilterStyle, getLiquidGlassStats, getMediaQueries, getWasmDisplacementBmp, initLiquidGlass, initWasmSimdModule, isGlassDowngraded, isWasmReady, onWasmReady, parseCssColorToRgb, pickMapScale, qualityDowngrades, qualityTier, qualityUpgrades, rebuildAllLiquidGlassFilters, recordJankFrame, registerManagedElement, resolveGlassConfig, resolveGlassEngine, revokeBlobUrlSafely, unregisterManagedElement };
3772
- //# sourceMappingURL=chunk-AZ2YV2W4.js.map
3773
- //# sourceMappingURL=chunk-AZ2YV2W4.js.map
3830
+ //# sourceMappingURL=chunk-HL64SDJN.js.map
3831
+ //# sourceMappingURL=chunk-HL64SDJN.js.map