vireglass 2.0.0 → 2.1.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/dist/dom.cjs CHANGED
@@ -438,36 +438,34 @@ function smin(a, b, k) {
438
438
  const h = Math.min(Math.max(0.5 + 0.5 * (b - a) / k, 0), 1);
439
439
  return b + (a - b) * h - k * h * (1 - h);
440
440
  }
441
- function sceneDistance(x, y, w, h, r, smoothing = 0, b, c) {
442
- const a = sdfRoundedRect(x, y, w, h, r);
443
- if (smoothing <= 0 || !b || b.width <= 0) return a;
444
- const db = sdfRoundedRect(x - b.offsetX, y - b.offsetY, b.width, b.height, b.cornerRadius);
445
- const ab = smin(a, db, smoothing);
446
- if (!c || c.width <= 0) return ab;
447
- const dc = sdfRoundedRect(x - c.offsetX, y - c.offsetY, c.width, c.height, c.cornerRadius);
448
- return smin(ab, dc, smoothing);
449
- }
450
- function sceneGradient(x, y, w, h, r, smoothing = 0, b, c) {
451
- const na = sdfRoundedRectGradient(x, y, w, h, r);
452
- if (smoothing <= 0 || !b || b.width <= 0) return na;
453
- const bx = x - b.offsetX;
454
- const by = y - b.offsetY;
455
- const da = sdfRoundedRect(x, y, w, h, r);
456
- const db = sdfRoundedRect(bx, by, b.width, b.height, b.cornerRadius);
457
- const hb = Math.min(Math.max(0.5 + 0.5 * (db - da) / smoothing, 0), 1);
458
- const nb = sdfRoundedRectGradient(bx, by, b.width, b.height, b.cornerRadius);
459
- let gx = nb[0] + (na[0] - nb[0]) * hb;
460
- let gy = nb[1] + (na[1] - nb[1]) * hb;
461
- if (c && c.width > 0) {
462
- const cx = x - c.offsetX;
463
- const cy = y - c.offsetY;
464
- const dab = smin(da, db, smoothing);
465
- const dc = sdfRoundedRect(cx, cy, c.width, c.height, c.cornerRadius);
466
- const hc = Math.min(Math.max(0.5 + 0.5 * (dc - dab) / smoothing, 0), 1);
467
- const nc = sdfRoundedRectGradient(cx, cy, c.width, c.height, c.cornerRadius);
468
- gx = nc[0] + (gx - nc[0]) * hc;
469
- gy = nc[1] + (gy - nc[1]) * hc;
441
+ function sceneDistance(x, y, w, h, r, smoothing = 0, ...shapes) {
442
+ let d = sdfRoundedRect(x, y, w, h, r);
443
+ if (smoothing <= 0) return d;
444
+ for (const s of shapes) {
445
+ if (!s || s.width <= 0) continue;
446
+ d = smin(d, sdfRoundedRect(x - s.offsetX, y - s.offsetY, s.width, s.height, s.cornerRadius), smoothing);
470
447
  }
448
+ return d;
449
+ }
450
+ function sceneGradient(x, y, w, h, r, smoothing = 0, ...shapes) {
451
+ let d = sdfRoundedRect(x, y, w, h, r);
452
+ let [gx, gy] = sdfRoundedRectGradient(x, y, w, h, r);
453
+ let blended = false;
454
+ if (smoothing > 0) {
455
+ for (const s of shapes) {
456
+ if (!s || s.width <= 0) continue;
457
+ const sx = x - s.offsetX;
458
+ const sy = y - s.offsetY;
459
+ const ds = sdfRoundedRect(sx, sy, s.width, s.height, s.cornerRadius);
460
+ const weight = Math.min(Math.max(0.5 + 0.5 * (ds - d) / smoothing, 0), 1);
461
+ const [nx, ny] = sdfRoundedRectGradient(sx, sy, s.width, s.height, s.cornerRadius);
462
+ gx = nx + (gx - nx) * weight;
463
+ gy = ny + (gy - ny) * weight;
464
+ d = smin(d, ds, smoothing);
465
+ blended = true;
466
+ }
467
+ }
468
+ if (!blended) return [gx, gy];
471
469
  gx += EPSILON;
472
470
  gy += EPSILON;
473
471
  const len = Math.hypot(gx, gy) || 1;
@@ -523,6 +521,7 @@ function renderDisplacementPixels(optics, geometry, dpr, state = {}) {
523
521
  const rawScale = optics.refraction * thicknessDp(geometry, optics) * SCALE_GAIN;
524
522
  const scale = Math.min(rawScale, MAX_SCALE);
525
523
  const smoothing = state.smoothing ?? 0;
524
+ const shapes = [state.morph, state.morph2, ...state.morphs ?? []];
526
525
  const data = new Uint8ClampedArray(width * height * 4);
527
526
  const halfW = geometry.width / 2;
528
527
  const halfH = geometry.height / 2;
@@ -532,7 +531,7 @@ function renderDisplacementPixels(optics, geometry, dpr, state = {}) {
532
531
  const x = (px + 0.5) / density - halfW;
533
532
  const i = (py * width + px) * 4;
534
533
  const [wx, wy] = state.touch ? touchWarp(x, y, state.touch) : [x, y];
535
- const sd = sceneDistance(wx, wy, geometry.width, geometry.height, geometry.cornerRadius, smoothing, state.morph, state.morph2);
534
+ const sd = sceneDistance(wx, wy, geometry.width, geometry.height, geometry.cornerRadius, smoothing, ...shapes);
536
535
  const t = Math.min(Math.max((sd + bevel) / bevel, 0), 1);
537
536
  if (t <= 0 || scale <= 0) {
538
537
  data[i] = 128;
@@ -541,7 +540,7 @@ function renderDisplacementPixels(optics, geometry, dpr, state = {}) {
541
540
  data[i + 3] = 255;
542
541
  continue;
543
542
  }
544
- const [gx, gy] = sceneGradient(wx, wy, geometry.width, geometry.height, geometry.cornerRadius, smoothing, state.morph, state.morph2);
543
+ const [gx, gy] = sceneGradient(wx, wy, geometry.width, geometry.height, geometry.cornerRadius, smoothing, ...shapes);
545
544
  const magnitude = bevelProfile(t) * (scale / 2);
546
545
  const dx = -gx * magnitude;
547
546
  const dy = -gy * magnitude;
@@ -1777,7 +1776,7 @@ function attachGlass(el, opts = {}) {
1777
1776
  }
1778
1777
  function morphState() {
1779
1778
  if (!morph) return {};
1780
- return { smoothing: morph.smoothing, morph: morph.shape, morph2: morph.shape2 };
1779
+ return { smoothing: morph.smoothing, morph: morph.shape, morph2: morph.shape2, morphs: morph.shapes };
1781
1780
  }
1782
1781
  function morphKey() {
1783
1782
  if (!morph) return "0";