@zakisheriff/liquid-glass 4.0.1 → 4.0.2

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.mjs CHANGED
@@ -152,21 +152,21 @@ var clamp = (value, min, max) => Math.min(max, Math.max(min, value));
152
152
 
153
153
  // src/engine/selection.ts
154
154
  function guardSelection(el2) {
155
- let held = false;
155
+ let held2 = false;
156
156
  const block = (event) => {
157
- if (held) event.preventDefault();
157
+ if (held2) event.preventDefault();
158
158
  };
159
159
  const clear = () => {
160
160
  const selection = window.getSelection?.();
161
161
  if (selection && !selection.isCollapsed) selection.removeAllRanges();
162
162
  };
163
163
  const down = () => {
164
- held = true;
164
+ held2 = true;
165
165
  clear();
166
166
  };
167
167
  const up = () => {
168
- if (!held) return;
169
- held = false;
168
+ if (!held2) return;
169
+ held2 = false;
170
170
  clear();
171
171
  };
172
172
  el2.addEventListener("pointerdown", down, true);
@@ -616,6 +616,18 @@ function setGlassQuality(quality) {
616
616
  // src/engine/refraction.ts
617
617
  var SVG_NS = "http://www.w3.org/2000/svg";
618
618
  var cache = /* @__PURE__ */ new Map();
619
+ function encode(canvas) {
620
+ return new Promise((done) => {
621
+ if (typeof canvas.toBlob !== "function" || typeof FileReader !== "function") return done(canvas.toDataURL());
622
+ canvas.toBlob((blob) => {
623
+ if (!blob) return done(canvas.toDataURL());
624
+ const reader = new FileReader();
625
+ reader.onload = () => done(reader.result);
626
+ reader.onerror = () => done(canvas.toDataURL());
627
+ reader.readAsDataURL(blob);
628
+ });
629
+ });
630
+ }
619
631
  var defs = null;
620
632
  var counter = 0;
621
633
  function el(name, attrs) {
@@ -665,6 +677,10 @@ function bevelProfile(bezel, thickness, ior) {
665
677
  return a + (b - a) * (f - i);
666
678
  };
667
679
  }
680
+ function fillPixels(image, r, g, b, a) {
681
+ const word = new Uint32Array(new Uint8ClampedArray([r, g, b, a]).buffer)[0];
682
+ new Uint32Array(image.data.buffer, image.data.byteOffset, image.data.length / 4).fill(word);
683
+ }
668
684
  function bake(width, height, radius, shape, pad = 0, simple = false, fringe = false, box = false) {
669
685
  const W = width + pad * 2;
670
686
  const H = height + pad * 2;
@@ -682,6 +698,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
682
698
  const rimHue = new Float32Array(cw * ch);
683
699
  const inside = new Float32Array(cw * ch);
684
700
  const zone = new Float32Array(cw * ch);
701
+ const outer = new Int32Array(cw * ch);
702
+ let edges = 0;
685
703
  let max = 1e-4;
686
704
  let maxX = 1e-4;
687
705
  let maxY = 1e-4;
@@ -709,6 +727,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
709
727
  inside[y * cw + x] = 1;
710
728
  continue;
711
729
  }
730
+ outer[edges++] = y * cw + x;
712
731
  let nx;
713
732
  let ny;
714
733
  if (len > 0) {
@@ -752,12 +771,14 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
752
771
  const canvas = document.createElement("canvas");
753
772
  canvas.width = cw;
754
773
  canvas.height = ch;
755
- const ctx = canvas.getContext("2d");
774
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
756
775
  const scale = box ? 2 * Math.max(maxX / W, maxY / H) : max * 2;
757
776
  const spanX = box ? scale * W / 2 : max;
758
777
  const spanY = box ? scale * H / 2 : max;
759
778
  const field = ctx.createImageData(cw, ch);
760
- for (let i = 0; i < cw * ch; i++) {
779
+ fillPixels(field, 128, 128, 128, 255);
780
+ for (let e = 0; e < edges; e++) {
781
+ const i = outer[e];
761
782
  const o = i * 4;
762
783
  field.data[o] = Math.round(127.5 + dx[i] / spanX * 127.5);
763
784
  field.data[o + 1] = Math.round(127.5 + dy[i] / spanY * 127.5);
@@ -765,9 +786,9 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
765
786
  field.data[o + 3] = 255;
766
787
  }
767
788
  ctx.putImageData(field, 0, 0);
768
- const map = canvas.toDataURL();
789
+ const map = encode(canvas);
769
790
  const reach = Math.max(W, H) / 2;
770
- let zoomMap = "";
791
+ let zoomMap = Promise.resolve("");
771
792
  if (magnify && !simple) {
772
793
  const ramp = ctx.createImageData(cw, ch);
773
794
  for (let y = 0; y < ch; y++) {
@@ -783,10 +804,12 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
783
804
  }
784
805
  }
785
806
  ctx.putImageData(ramp, 0, 0);
786
- zoomMap = canvas.toDataURL();
807
+ zoomMap = encode(canvas);
787
808
  }
788
809
  const shine = ctx.createImageData(cw, ch);
789
- for (let i = 0; i < cw * ch; i++) {
810
+ fillPixels(shine, 255, 255, 255, 0);
811
+ for (let e = 0; e < edges; e++) {
812
+ const i = outer[e];
790
813
  const o = i * 4;
791
814
  let r = 255;
792
815
  let g = 255;
@@ -804,26 +827,35 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
804
827
  shine.data[o + 3] = Math.round(rimA[i] * 255);
805
828
  }
806
829
  ctx.putImageData(shine, 0, 0);
807
- const rim = canvas.toDataURL();
830
+ const rim = encode(canvas);
808
831
  const shapeMask = ctx.createImageData(cw, ch);
809
- for (let i = 0; i < cw * ch; i++) {
832
+ fillPixels(shapeMask, 255, 255, 255, 255);
833
+ for (let e = 0; e < edges; e++) {
834
+ const i = outer[e];
810
835
  const o = i * 4;
811
836
  shapeMask.data[o] = shapeMask.data[o + 1] = shapeMask.data[o + 2] = 255;
812
837
  shapeMask.data[o + 3] = Math.round(inside[i] * 255);
813
838
  }
814
839
  ctx.putImageData(shapeMask, 0, 0);
815
- const mask = canvas.toDataURL();
816
- let smooth = "";
840
+ const mask = encode(canvas);
841
+ let smooth = Promise.resolve("");
817
842
  if (lens && !simple) {
818
843
  const soft = ctx.createImageData(cw, ch);
819
- for (let i = 0; i < cw * ch; i++) {
844
+ fillPixels(soft, 255, 255, 255, 0);
845
+ for (let e = 0; e < edges; e++) {
846
+ const i = outer[e];
820
847
  const o = i * 4;
821
848
  soft.data[o] = soft.data[o + 1] = soft.data[o + 2] = 255;
822
849
  soft.data[o + 3] = Math.round(zone[i] * 255);
823
850
  }
824
851
  ctx.putImageData(soft, 0, 0);
825
- smooth = canvas.toDataURL();
852
+ smooth = encode(canvas);
826
853
  }
854
+ return Promise.all([map, zoomMap, rim, mask, smooth]).then(
855
+ ([map2, zoomMap2, rim2, mask2, smooth2]) => assemble(W, H, box, magnify, reach, scale, shape, simple, fringe, map2, zoomMap2, rim2, mask2, smooth2)
856
+ );
857
+ }
858
+ function assemble(W, H, box, magnify, reach, scale, shape, simple, fringe, map, zoomMap, rim, mask, smooth) {
827
859
  const id = `lg-refract-${++counter}`;
828
860
  const area = box ? { x: 0, y: 0, width: 1, height: 1 } : { x: 0, y: 0, width: W, height: H };
829
861
  const filter = el(
@@ -970,9 +1002,24 @@ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fr
970
1002
  var soon = [];
971
1003
  var later = [];
972
1004
  var pumping = false;
1005
+ var held = 0;
1006
+ function holdBakes() {
1007
+ held++;
1008
+ let released = false;
1009
+ return () => {
1010
+ if (released) return;
1011
+ released = true;
1012
+ held--;
1013
+ if (!held && !pumping && (soon.length || later.length)) pump();
1014
+ };
1015
+ }
973
1016
  function pump() {
974
1017
  pumping = true;
975
1018
  const run = (deadline) => {
1019
+ if (held) {
1020
+ pumping = false;
1021
+ return;
1022
+ }
976
1023
  const end = performance.now() + 8;
977
1024
  const left = () => deadline ? deadline.timeRemaining() > 4 : performance.now() < end;
978
1025
  let ran = 0;
@@ -1028,32 +1075,51 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1028
1075
  const viewport = element.querySelector(":scope > .lg-mirror > .lg-mirror__viewport");
1029
1076
  if (viewport && copyFilter) viewport.style.filter = copyFilter;
1030
1077
  };
1078
+ let fits = 0;
1079
+ let gone = false;
1031
1080
  const fit = () => {
1032
1081
  const width = element.offsetWidth;
1033
1082
  const height = element.offsetHeight;
1034
1083
  if (!width || !height) return;
1084
+ const run = ++fits;
1035
1085
  const radius = cornerRadius(element, width, height);
1036
- const baked = glassFor(width, height, radius, shape);
1037
- filter = baked.filter;
1038
- if (!live && mirror?.current) {
1086
+ const mirrored = !live && !!mirror?.current;
1087
+ if (mirrored) {
1039
1088
  pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
1040
1089
  ({ zoom, zoomX } = resolve(shape, width, height));
1041
1090
  if (tears) {
1042
1091
  zoom = Math.min(zoom, 1);
1043
1092
  zoomX = Math.min(zoomX, 1);
1044
1093
  }
1045
- element.style.setProperty("--lg-inner-mask", baked.inner);
1046
- if (bendCopy) {
1047
- copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium).filter;
1048
- applyCopyFilter();
1049
- }
1050
- }
1051
- if (live || bendCopy && chromium && !shape.copyOnly) {
1052
- const backdrop = live ? baked.filter : glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }).filter;
1053
- element.style.setProperty("--lg-refraction", backdrop);
1054
- element.setAttribute("data-lg-refract", shape.kind ?? "surface");
1055
1094
  }
1056
- element.style.setProperty("--lg-rim", baked.rim);
1095
+ const refractBackdrop = live || bendCopy && chromium && !shape.copyOnly;
1096
+ const none = Promise.resolve(null);
1097
+ Promise.all([
1098
+ glassFor(width, height, radius, shape),
1099
+ // The copy is magnified by CSS; the filter only bends the rim and
1100
+ // fringes what it bends with colour.
1101
+ mirrored && bendCopy ? glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium) : none,
1102
+ // Behind a bent copy the backdrop only carries the bar's glass and
1103
+ // edges: the copy already magnifies the content, so the backdrop isn't
1104
+ // zoomed (that pushed the bar's edge into a ring inside the lens) and
1105
+ // its rim bends gently, curling the bar's edge into thin arcs.
1106
+ refractBackdrop && !live ? glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }) : none
1107
+ ]).then(([baked, copy, behind]) => {
1108
+ if (run !== fits || gone) return;
1109
+ filter = baked.filter;
1110
+ if (mirrored) {
1111
+ element.style.setProperty("--lg-inner-mask", baked.inner);
1112
+ if (copy) {
1113
+ copyFilter = copy.filter;
1114
+ applyCopyFilter();
1115
+ }
1116
+ }
1117
+ if (refractBackdrop) {
1118
+ element.style.setProperty("--lg-refraction", live ? baked.filter : behind.filter);
1119
+ element.setAttribute("data-lg-refract", shape.kind ?? "surface");
1120
+ }
1121
+ element.style.setProperty("--lg-rim", baked.rim);
1122
+ });
1057
1123
  };
1058
1124
  let seen = typeof IntersectionObserver !== "function";
1059
1125
  const request = () => {
@@ -1082,6 +1148,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1082
1148
  applyCopyFilter();
1083
1149
  const ungovern = governRefraction(element);
1084
1150
  return () => {
1151
+ gone = true;
1085
1152
  cancel?.();
1086
1153
  unmirror?.();
1087
1154
  ungovern();
@@ -1360,11 +1427,20 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1360
1427
  if (wanted.length && sheet.hasAttribute("data-raised") !== raised) sheet.toggleAttribute("data-raised", raised);
1361
1428
  };
1362
1429
  let closing = false;
1363
- const position = createSpring(hidden, OPEN, (y) => {
1430
+ let release = null;
1431
+ const landed = () => {
1432
+ release?.();
1433
+ release = null;
1434
+ sheet.removeAttribute("data-lg-moving");
1435
+ };
1436
+ const position = createSpring(hidden, OPEN, (y, velocity) => {
1364
1437
  paint(y);
1365
1438
  if (closing && y >= hidden - 1) setMounted(false);
1439
+ if (release && Math.abs(y - position.target) < 1 && Math.abs(velocity) < 20) landed();
1366
1440
  });
1367
1441
  const go = (y, feel) => {
1442
+ release ?? (release = holdBakes());
1443
+ sheet.setAttribute("data-lg-moving", "");
1368
1444
  position.tune(feel);
1369
1445
  position.set(y);
1370
1446
  };
@@ -1388,6 +1464,8 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1388
1464
  if (raised && !target.closest(".lg-sheet__grabber-zone") && sheet.scrollTop > 0) return;
1389
1465
  if (target.closest('input, textarea, button, a, [role="slider"], [role="switch"]')) return;
1390
1466
  position.stop();
1467
+ landed();
1468
+ sheet.setAttribute("data-lg-moving", "");
1391
1469
  gesture = { id: event.pointerId, y: event.clientY, from: position.value, t: event.timeStamp, v: 0, last: position.value };
1392
1470
  sheet.setPointerCapture(event.pointerId);
1393
1471
  };
@@ -1438,6 +1516,7 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1438
1516
  sheet.focus({ preventScroll: true });
1439
1517
  return () => {
1440
1518
  position.stop();
1519
+ landed();
1441
1520
  api.current = null;
1442
1521
  observer.disconnect();
1443
1522
  sheet.removeEventListener("pointerdown", down);
@@ -1600,7 +1679,7 @@ var GlassMenu = ({
1600
1679
  useEffect4(() => {
1601
1680
  if (!mounted) return;
1602
1681
  const panel = surfaceRef.current;
1603
- let held = null;
1682
+ let held2 = null;
1604
1683
  let hot = null;
1605
1684
  const itemAt = (x, y) => document.elementFromPoint(x, y)?.closest(".lg-menu__item:not(:disabled)") ?? null;
1606
1685
  const light = (next) => {
@@ -1614,16 +1693,16 @@ var GlassMenu = ({
1614
1693
  };
1615
1694
  const down = (event) => {
1616
1695
  if (!event.isPrimary || event.button !== 0) return;
1617
- held = event.pointerId;
1696
+ held2 = event.pointerId;
1618
1697
  hot = null;
1619
1698
  light(itemAt(event.clientX, event.clientY));
1620
1699
  };
1621
1700
  const move = (event) => {
1622
- if (event.pointerId === held) light(itemAt(event.clientX, event.clientY));
1701
+ if (event.pointerId === held2) light(itemAt(event.clientX, event.clientY));
1623
1702
  };
1624
1703
  const up = (event) => {
1625
- if (event.pointerId !== held) return;
1626
- held = null;
1704
+ if (event.pointerId !== held2) return;
1705
+ held2 = null;
1627
1706
  const target = hot;
1628
1707
  light(null);
1629
1708
  const index = target ? Number(target.dataset.index) : -1;
@@ -1636,12 +1715,12 @@ var GlassMenu = ({
1636
1715
  }
1637
1716
  };
1638
1717
  const cancel = (event) => {
1639
- if (event.pointerId !== held) return;
1640
- held = null;
1718
+ if (event.pointerId !== held2) return;
1719
+ held2 = null;
1641
1720
  light(null);
1642
1721
  };
1643
1722
  const hold = (event) => {
1644
- if (held !== null && event.cancelable) event.preventDefault();
1723
+ if (held2 !== null && event.cancelable) event.preventDefault();
1645
1724
  };
1646
1725
  panel.addEventListener("pointerdown", down);
1647
1726
  window.addEventListener("pointermove", move);