@zakisheriff/liquid-glass 0.3.9 → 0.3.11
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/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +49 -34
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -34
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -251,6 +251,12 @@ interface GlassShape {
|
|
|
251
251
|
dispersion?: number;
|
|
252
252
|
/** Overall displacement multiplier. */
|
|
253
253
|
strength?: number;
|
|
254
|
+
/**
|
|
255
|
+
* Rim stretch, 0–1 (lens only). Instead of pulling in light from past the
|
|
256
|
+
* edge, the rim samples inward ever more steeply, so whatever sits under it
|
|
257
|
+
* smears out to the edge in streaks: the thick-droplet look of iOS tab bars.
|
|
258
|
+
*/
|
|
259
|
+
stretch?: number;
|
|
254
260
|
/**
|
|
255
261
|
* Margin (px) along the top and bottom of a lens that stays clear and
|
|
256
262
|
* unbent, e.g. where a tab-bar lens rises past its bar: only the part over
|
package/dist/index.d.ts
CHANGED
|
@@ -251,6 +251,12 @@ interface GlassShape {
|
|
|
251
251
|
dispersion?: number;
|
|
252
252
|
/** Overall displacement multiplier. */
|
|
253
253
|
strength?: number;
|
|
254
|
+
/**
|
|
255
|
+
* Rim stretch, 0–1 (lens only). Instead of pulling in light from past the
|
|
256
|
+
* edge, the rim samples inward ever more steeply, so whatever sits under it
|
|
257
|
+
* smears out to the edge in streaks: the thick-droplet look of iOS tab bars.
|
|
258
|
+
*/
|
|
259
|
+
stretch?: number;
|
|
254
260
|
/**
|
|
255
261
|
* Margin (px) along the top and bottom of a lens that stays clear and
|
|
256
262
|
* unbent, e.g. where a tab-bar lens rises past its bar: only the part over
|
package/dist/index.js
CHANGED
|
@@ -611,7 +611,7 @@ function bevelProfile(bezel, thickness, ior) {
|
|
|
611
611
|
return a + (b - a) * (f - i);
|
|
612
612
|
};
|
|
613
613
|
}
|
|
614
|
-
function bake(width, height, radius, shape, pad = 0, simple = false, fringe = false) {
|
|
614
|
+
function bake(width, height, radius, shape, pad = 0, simple = false, fringe = false, box = false) {
|
|
615
615
|
const W = width + pad * 2;
|
|
616
616
|
const H = height + pad * 2;
|
|
617
617
|
let density = Math.min(typeof devicePixelRatio === "number" ? devicePixelRatio : 1, 2);
|
|
@@ -629,6 +629,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
629
629
|
const inside = new Float32Array(cw * ch);
|
|
630
630
|
const zone = new Float32Array(cw * ch);
|
|
631
631
|
let max = 1e-4;
|
|
632
|
+
let maxX = 1e-4;
|
|
633
|
+
let maxY = 1e-4;
|
|
632
634
|
const hw = width / 2;
|
|
633
635
|
const hh = height / 2;
|
|
634
636
|
const px1 = 1 / density;
|
|
@@ -661,7 +663,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
661
663
|
ny = Math.sign(py);
|
|
662
664
|
}
|
|
663
665
|
const i = y * cw + x;
|
|
664
|
-
const
|
|
666
|
+
const u = d / shape.bezel;
|
|
667
|
+
const s = (lens && shape.stretch > 0 ? d >= 0 && u < 1 ? -shape.bezel * shape.stretch * 0.5 * (1 - u) ** 2 : 0 : shift(d) * direction) * shape.strength * band(py);
|
|
665
668
|
let vx = nx * s;
|
|
666
669
|
let vy = ny * s;
|
|
667
670
|
if (simple && magnify) {
|
|
@@ -672,6 +675,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
672
675
|
dy[i] = vy;
|
|
673
676
|
const m = Math.max(Math.abs(vx), Math.abs(vy));
|
|
674
677
|
if (m > max) max = m;
|
|
678
|
+
if (Math.abs(vx) > maxX) maxX = Math.abs(vx);
|
|
679
|
+
if (Math.abs(vy) > maxY) maxY = Math.abs(vy);
|
|
675
680
|
inside[i] = clamp(d / px1 + 0.5, 0, 1);
|
|
676
681
|
zone[i] = (d <= 0 ? 1 : clamp(1 - d / Math.max(1, shape.bezel), 0, 1) ** 1.3) * band(py);
|
|
677
682
|
if (d >= -px1) {
|
|
@@ -689,11 +694,14 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
689
694
|
canvas.width = cw;
|
|
690
695
|
canvas.height = ch;
|
|
691
696
|
const ctx = canvas.getContext("2d");
|
|
697
|
+
const scale = box ? 2 * Math.max(maxX / W, maxY / H) : max * 2;
|
|
698
|
+
const spanX = box ? scale * W / 2 : max;
|
|
699
|
+
const spanY = box ? scale * H / 2 : max;
|
|
692
700
|
const field = ctx.createImageData(cw, ch);
|
|
693
701
|
for (let i = 0; i < cw * ch; i++) {
|
|
694
702
|
const o = i * 4;
|
|
695
|
-
field.data[o] = Math.round(127.5 + dx[i] /
|
|
696
|
-
field.data[o + 1] = Math.round(127.5 + dy[i] /
|
|
703
|
+
field.data[o] = Math.round(127.5 + dx[i] / spanX * 127.5);
|
|
704
|
+
field.data[o + 1] = Math.round(127.5 + dy[i] / spanY * 127.5);
|
|
697
705
|
field.data[o + 2] = 128;
|
|
698
706
|
field.data[o + 3] = 255;
|
|
699
707
|
}
|
|
@@ -709,8 +717,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
709
717
|
const px = (x + 0.5) * W / cw - W / 2;
|
|
710
718
|
const py = (y + 0.5) * H / ch - H / 2;
|
|
711
719
|
const k = band(py);
|
|
712
|
-
ramp.data[o] = Math.round(127.5 - px / reach * 127.5 * k);
|
|
713
|
-
ramp.data[o + 1] = Math.round(127.5 - py / reach * 127.5 * k);
|
|
720
|
+
ramp.data[o] = Math.round(127.5 - px / (box ? W / 2 : reach) * 127.5 * k);
|
|
721
|
+
ramp.data[o + 1] = Math.round(127.5 - py / (box ? H / 2 : reach) * 127.5 * k);
|
|
714
722
|
ramp.data[o + 2] = 128;
|
|
715
723
|
ramp.data[o + 3] = 255;
|
|
716
724
|
}
|
|
@@ -758,25 +766,28 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
758
766
|
smooth = canvas.toDataURL();
|
|
759
767
|
}
|
|
760
768
|
const id = `lg-refract-${++counter}`;
|
|
761
|
-
const
|
|
762
|
-
const filter = el(
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
769
|
+
const area = box ? { x: 0, y: 0, width: 1, height: 1 } : { x: 0, y: 0, width: W, height: H };
|
|
770
|
+
const filter = el(
|
|
771
|
+
"filter",
|
|
772
|
+
box ? { id, filterUnits: "objectBoundingBox", primitiveUnits: "objectBoundingBox", x: 0, y: 0, width: 1, height: 1, "color-interpolation-filters": "sRGB" } : {
|
|
773
|
+
id,
|
|
774
|
+
// Generous region: the rim samples content from beyond the glass edge.
|
|
775
|
+
x: -0.25,
|
|
776
|
+
y: -0.25,
|
|
777
|
+
width: 1.5,
|
|
778
|
+
height: 1.5,
|
|
779
|
+
"color-interpolation-filters": "sRGB"
|
|
780
|
+
}
|
|
781
|
+
);
|
|
782
|
+
filter.append(el("feImage", { href: map, ...area, preserveAspectRatio: "none", result: "field" }));
|
|
772
783
|
let source = "SourceGraphic";
|
|
773
784
|
if (zoomMap) {
|
|
774
785
|
filter.append(
|
|
775
|
-
el("feImage", { href: zoomMap, ...
|
|
786
|
+
el("feImage", { href: zoomMap, ...area, preserveAspectRatio: "none", result: "zoom" }),
|
|
776
787
|
el("feDisplacementMap", {
|
|
777
788
|
in: "SourceGraphic",
|
|
778
789
|
in2: "zoom",
|
|
779
|
-
scale: 2 * reach * magnify,
|
|
790
|
+
scale: box ? magnify : 2 * reach * magnify,
|
|
780
791
|
xChannelSelector: "R",
|
|
781
792
|
yChannelSelector: "G",
|
|
782
793
|
result: "magnified"
|
|
@@ -784,9 +795,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
784
795
|
);
|
|
785
796
|
source = "magnified";
|
|
786
797
|
}
|
|
787
|
-
const scale = max * 2;
|
|
788
798
|
if (shape.dispersion > 0 && !simple && fringe) {
|
|
789
|
-
const spread = 0.3 * shape.dispersion;
|
|
799
|
+
const spread = (shape.stretch > 0 ? 0.12 : 0.3) * shape.dispersion;
|
|
790
800
|
for (const [name, factor] of [["r", 1 + spread], ["g", 1], ["b", 1 - spread]]) {
|
|
791
801
|
filter.append(
|
|
792
802
|
el("feDisplacementMap", {
|
|
@@ -848,8 +858,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
848
858
|
let out = "bent";
|
|
849
859
|
if (smooth) {
|
|
850
860
|
filter.append(
|
|
851
|
-
el("feGaussianBlur", { in: "bent", stdDeviation: 0.65, result: "bent-soft" }),
|
|
852
|
-
el("feImage", { href: smooth, ...
|
|
861
|
+
el("feGaussianBlur", { in: "bent", stdDeviation: box ? `${0.65 / W} ${0.65 / H}` : 0.65, result: "bent-soft" }),
|
|
862
|
+
el("feImage", { href: smooth, ...area, preserveAspectRatio: "none", result: "rim-zone" }),
|
|
853
863
|
el("feComposite", { in: "bent-soft", in2: "rim-zone", operator: "in", result: "rim-soft" }),
|
|
854
864
|
el("feComposite", { in: "bent", in2: "rim-zone", operator: "out", result: "core" }),
|
|
855
865
|
el("feComposite", { in: "rim-soft", in2: "core", operator: "arithmetic", k2: 1, k3: 1, result: "smoothed" })
|
|
@@ -857,7 +867,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
|
|
|
857
867
|
out = "smoothed";
|
|
858
868
|
}
|
|
859
869
|
filter.append(
|
|
860
|
-
el("feImage", { href: mask, ...
|
|
870
|
+
el("feImage", { href: mask, ...area, preserveAspectRatio: "none", result: "shape" }),
|
|
861
871
|
el("feComposite", { in: out, in2: "shape", operator: "in" })
|
|
862
872
|
);
|
|
863
873
|
container().append(filter);
|
|
@@ -877,18 +887,19 @@ function resolve(shape, width, height) {
|
|
|
877
887
|
dispersion: shape.dispersion ?? (lens ? 1 : 0),
|
|
878
888
|
strength: shape.strength ?? 1,
|
|
879
889
|
inset: shape.inset ?? 0,
|
|
890
|
+
stretch: shape.stretch ?? 0,
|
|
880
891
|
copy: shape.copy ?? false
|
|
881
892
|
};
|
|
882
893
|
}
|
|
883
|
-
function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fringe = false) {
|
|
894
|
+
function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fringe = false, box = false) {
|
|
884
895
|
const w = Math.round(width);
|
|
885
896
|
const h = Math.round(height);
|
|
886
897
|
const r = Math.min(w / 2, h / 2, Math.max(0, radius));
|
|
887
898
|
const full = resolve(shape, w, h);
|
|
888
|
-
const key = [w, h, Math.round(r * 2) / 2, full.kind, full.bezel.toFixed(1), full.thickness.toFixed(1), full.ior, full.zoom, full.dispersion, full.strength, full.inset, pad, simple, fringe].join("|");
|
|
899
|
+
const key = [w, h, Math.round(r * 2) / 2, full.kind, full.bezel.toFixed(1), full.thickness.toFixed(1), full.ior, full.zoom, full.dispersion, full.strength, full.inset, full.stretch, pad, simple, fringe, box].join("|");
|
|
889
900
|
let baked = cache.get(key);
|
|
890
901
|
if (!baked) {
|
|
891
|
-
baked = bake(w, h, r, full, pad, simple, fringe);
|
|
902
|
+
baked = bake(w, h, r, full, pad, simple, fringe, box);
|
|
892
903
|
cache.set(key, baked);
|
|
893
904
|
}
|
|
894
905
|
return baked;
|
|
@@ -918,7 +929,8 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
|
|
|
918
929
|
let pad = 0;
|
|
919
930
|
let zoom = 1;
|
|
920
931
|
let zoomX = 1;
|
|
921
|
-
const
|
|
932
|
+
const chromium = canRefractBackdrop();
|
|
933
|
+
const bendCopy = !!mirror && (shape.copy === "refract" || !chromium);
|
|
922
934
|
let copyFilter = "";
|
|
923
935
|
const applyCopyFilter = () => {
|
|
924
936
|
const viewport = element.querySelector(":scope > .lg-mirror > .lg-mirror__viewport");
|
|
@@ -936,11 +948,11 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
|
|
|
936
948
|
({ zoom, zoomX } = resolve(shape, width, height));
|
|
937
949
|
element.style.setProperty("--lg-inner-mask", baked.inner);
|
|
938
950
|
if (bendCopy) {
|
|
939
|
-
copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true).filter;
|
|
951
|
+
copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium).filter;
|
|
940
952
|
applyCopyFilter();
|
|
941
953
|
}
|
|
942
954
|
}
|
|
943
|
-
if (live || bendCopy) {
|
|
955
|
+
if (live || bendCopy && chromium) {
|
|
944
956
|
const backdrop = live ? baked.filter : glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }).filter;
|
|
945
957
|
element.style.setProperty("--lg-filter", backdrop);
|
|
946
958
|
element.setAttribute("data-lg-refract", "");
|
|
@@ -949,7 +961,10 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
|
|
|
949
961
|
};
|
|
950
962
|
const observer = new ResizeObserver(() => {
|
|
951
963
|
cancel?.();
|
|
952
|
-
|
|
964
|
+
const timer = setTimeout(() => {
|
|
965
|
+
cancel = idle(fit);
|
|
966
|
+
}, 140);
|
|
967
|
+
cancel = () => clearTimeout(timer);
|
|
953
968
|
});
|
|
954
969
|
observer.observe(element);
|
|
955
970
|
fit();
|
|
@@ -1949,12 +1964,12 @@ function LensTrack({
|
|
|
1949
1964
|
const select = (0, import_react9.useRef)(null);
|
|
1950
1965
|
const strength = Math.max(1, Math.min(10, intensity)) / 5;
|
|
1951
1966
|
const tabbar = variant === "tabbar";
|
|
1952
|
-
const zoom = tabbar ? 1.
|
|
1967
|
+
const zoom = tabbar ? 1.04 : 1.1;
|
|
1953
1968
|
useRefraction(glass, { kind: "surface", strength });
|
|
1954
1969
|
const overhang = tabbar ? 14 : 12;
|
|
1955
1970
|
useRefraction(
|
|
1956
1971
|
lens,
|
|
1957
|
-
{ kind: "lens", strength: 1
|
|
1972
|
+
{ kind: "lens", strength: 1, stretch: 1, bezel: tabbar ? 24 : 16, zoom, dispersion: 1, copy: "refract" },
|
|
1958
1973
|
true,
|
|
1959
1974
|
bar,
|
|
1960
1975
|
() => bar.current?.hasAttribute("data-lifted") ?? false
|
|
@@ -2417,7 +2432,7 @@ var GlassSwitch = import_react13.default.forwardRef(
|
|
|
2417
2432
|
const sync = (0, import_react13.useRef)(null);
|
|
2418
2433
|
useRefraction(
|
|
2419
2434
|
thumbRef,
|
|
2420
|
-
{ kind: "lens", zoom: 0.72, zoomX: 1, dispersion: 1, copy: "refract", strength: Math.max(1, Math.min(10, intensity)) / 5 },
|
|
2435
|
+
{ kind: "lens", zoom: 0.72, zoomX: 1, dispersion: 1, stretch: 1, copy: "refract", strength: Math.max(1, Math.min(10, intensity)) / 5 },
|
|
2421
2436
|
true,
|
|
2422
2437
|
ref,
|
|
2423
2438
|
() => parseFloat(ref.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
|