@zakisheriff/liquid-glass 0.2.2 → 0.2.3

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
@@ -488,7 +488,6 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
488
488
  const rimA = new Float32Array(cw * ch);
489
489
  const rimHue = new Float32Array(cw * ch);
490
490
  const inside = new Float32Array(cw * ch);
491
- const depth = new Float32Array(cw * ch);
492
491
  let max = 1e-4;
493
492
  const hw = width / 2;
494
493
  const hh = height / 2;
@@ -528,15 +527,13 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
528
527
  const m = Math.max(Math.abs(vx), Math.abs(vy));
529
528
  if (m > max) max = m;
530
529
  inside[i] = clamp(d / px1 + 0.5, 0, 1);
531
- const t = clamp(d / (shape.bezel * 0.9), 0, 1);
532
- depth[i] = t * t * (3 - 2 * t);
533
530
  if (d >= -px1) {
534
531
  const facing = -(nx + ny) * Math.SQRT1_2;
535
532
  const light = 0.22 + 0.78 * Math.abs(facing) ** 2.2 * (facing > 0 ? 1 : 0.62);
536
533
  const line = Math.exp(-(((d - 0.55) / 0.55) ** 2));
537
- const inner2 = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
534
+ const inner = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
538
535
  const edgeAA = clamp(d / px1 + 1, 0, 1);
539
- rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner2) * light * edgeAA, 0, 1);
536
+ rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner) * light * edgeAA, 0, 1);
540
537
  rimHue[i] = Math.atan2(ny, nx);
541
538
  }
542
539
  }
@@ -601,9 +598,6 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
601
598
  }
602
599
  ctx.putImageData(shapeMask, 0, 0);
603
600
  const mask = canvas.toDataURL();
604
- for (let i = 0; i < cw * ch; i++) shapeMask.data[i * 4 + 3] = Math.round(depth[i] * 255);
605
- ctx.putImageData(shapeMask, 0, 0);
606
- const inner = canvas.toDataURL();
607
601
  const id = `lg-refract-${++counter}`;
608
602
  const box = { x: 0, y: 0, width: W, height: H };
609
603
  const filter = el("filter", {
@@ -673,7 +667,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
673
667
  el("feComposite", { in: "bent", in2: "shape", operator: "in" })
674
668
  );
675
669
  container().append(filter);
676
- return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${inner}")` };
670
+ return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${mask}")` };
677
671
  }
678
672
  function resolve(shape, width, height) {
679
673
  const kind = shape.kind ?? "surface";
@@ -1103,11 +1097,13 @@ function useLensTrack(refs, options) {
1103
1097
  const tint = refs.tint.current;
1104
1098
  const row = refs.row.current;
1105
1099
  const glass = refs.glass.current;
1106
- if (!bar || !lens || !tint || !row || !glass) return;
1100
+ const select = refs.select.current;
1101
+ if (!bar || !lens || !tint || !row || !glass || !select) return;
1107
1102
  let slot = 0;
1108
1103
  let height = 0;
1109
1104
  let pad = 0;
1110
- let barHeight = 0;
1105
+ let W = 0;
1106
+ let H = 0;
1111
1107
  const state = { x: 0, lift: 0, swell: 0, squash: 0 };
1112
1108
  let current = latest.current.index;
1113
1109
  let gesture = null;
@@ -1116,19 +1112,32 @@ function useLensTrack(refs, options) {
1116
1112
  const style = getComputedStyle(bar);
1117
1113
  pad = parseFloat(style.paddingLeft) || 0;
1118
1114
  slot = (bar.clientWidth - pad - (parseFloat(style.paddingRight) || 0)) / latest.current.count;
1119
- height = lens.offsetHeight;
1120
- barHeight = bar.offsetHeight;
1115
+ height = bar.clientHeight - (parseFloat(style.paddingTop) || 0) - (parseFloat(style.paddingBottom) || 0);
1116
+ const { overhang, spread } = latest.current;
1117
+ W = Math.round(slot * (1 + spread));
1118
+ H = Math.round(bar.offsetHeight + overhang);
1119
+ for (const el2 of [lens, tint]) {
1120
+ el2.style.width = `${W}px`;
1121
+ el2.style.height = `${H}px`;
1122
+ el2.style.left = `${pad + (slot - W) / 2}px`;
1123
+ el2.style.top = `${pad + (height - H) / 2}px`;
1124
+ }
1125
+ select.style.width = `${slot}px`;
1126
+ row.style.width = `${slot * latest.current.count}px`;
1127
+ row.style.height = `${height}px`;
1121
1128
  };
1122
1129
  const paint = () => {
1123
- const { x: x2, lift: lift2, swell: swell2, squash } = state;
1124
- const { overhang, spread } = latest.current;
1125
- const liftH = barHeight + overhang - height;
1126
- const sx = 1 + spread * lift2 + squash;
1127
- const sy = 1 + liftH / height * lift2 - squash * 0.6;
1128
- const transform = `translateX(${x2}px) scale(${sx}, ${sy})`;
1130
+ const { x: x2, swell: swell2, squash } = state;
1131
+ const lift2 = clamp(state.lift, 0, 1.08);
1132
+ if (!W || !H) return;
1133
+ const kx = (slot + (W - slot) * lift2) / W * (1 + squash);
1134
+ const ky = (height + (H - height) * lift2) / H * (1 - squash * 0.6);
1135
+ const transform = `translateX(${x2}px) scale(${kx}, ${ky})`;
1129
1136
  lens.style.transform = transform;
1130
1137
  tint.style.transform = transform;
1131
- row.style.transform = `scale(${1 / sx}, ${1 / sy}) translate(${(sx - 1) * slot / 2 - x2}px, ${(sy - 1) * height / 2}px)`;
1138
+ row.style.transform = `translate(${W / 2 + (-slot / 2 - x2) / kx}px, ${H / 2 - height / (2 * ky)}px) scale(${1 / kx}, ${1 / ky})`;
1139
+ select.style.transform = `translateX(${x2}px)`;
1140
+ lens.style.opacity = String(clamp(lift2 * 4, 0, 1));
1132
1141
  const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1133
1142
  bar.style.scale = `${grow}`;
1134
1143
  lens.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
@@ -1137,7 +1146,7 @@ function useLensTrack(refs, options) {
1137
1146
  };
1138
1147
  const x = createSpring(current * slot, SPRINGS.snap, (value, velocity) => {
1139
1148
  state.x = value;
1140
- state.squash = clamp(Math.abs(velocity) / 4200, 0, 0.16);
1149
+ state.squash = clamp(Math.abs(velocity) / 6e3, 0, 0.08);
1141
1150
  paint();
1142
1151
  });
1143
1152
  const lift = createSpring(0, SPRINGS.lift, (value) => {
@@ -1314,12 +1323,13 @@ function LensTrack({
1314
1323
  const lens = useRef6(null);
1315
1324
  const tint = useRef6(null);
1316
1325
  const row = useRef6(null);
1326
+ const select = useRef6(null);
1317
1327
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1318
1328
  useRefraction(glass, { kind: "surface", strength });
1319
1329
  useRefraction(lens, { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 });
1320
1330
  const tabbar = variant === "tabbar";
1321
1331
  useLensTrack(
1322
- { bar, glass, lens, tint, row },
1332
+ { bar, glass, lens, tint, row, select },
1323
1333
  {
1324
1334
  count: items.length,
1325
1335
  index,
@@ -1340,6 +1350,7 @@ function LensTrack({
1340
1350
  style: { ...style, "--lg-count": items.length, "--lg-index": index },
1341
1351
  children: [
1342
1352
  /* @__PURE__ */ jsx5("span", { ref: glass, className: "lg-glass lg-track__glass", "aria-hidden": "true" }),
1353
+ /* @__PURE__ */ jsx5("span", { ref: select, className: "lg-track__select", "aria-hidden": "true" }),
1343
1354
  items.map((item, i) => /* @__PURE__ */ jsx5(
1344
1355
  "button",
1345
1356
  {
@@ -1355,7 +1366,7 @@ function LensTrack({
1355
1366
  i
1356
1367
  )),
1357
1368
  /* @__PURE__ */ jsx5("span", { ref: tint, className: "lg-track__tint", "aria-hidden": "true", children: /* @__PURE__ */ jsx5("span", { ref: row, className: "lg-track__row", children: items.map((item, i) => /* @__PURE__ */ jsx5("span", { className: "lg-track__copy", children: item }, i)) }) }),
1358
- /* @__PURE__ */ jsx5("span", { ref: lens, className: "lg-glass lg-track__lens", "aria-hidden": "true", children: /* @__PURE__ */ jsx5("span", { className: "lg-track__pill" }) })
1369
+ /* @__PURE__ */ jsx5("span", { ref: lens, className: "lg-glass lg-track__lens", "aria-hidden": "true" })
1359
1370
  ]
1360
1371
  }
1361
1372
  );