@sessile/core 0.5.0 → 0.5.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/contour.d.ts CHANGED
@@ -7,12 +7,8 @@
7
7
  * SPDX-License-Identifier: MPL-2.0
8
8
  */
9
9
  import type { GlassFieldState, GlassPrimitive, Point } from './contracts.ts';
10
- /**
11
- * Computes the tight upper bound `u_n` on cumulative quadratic `smin` outward dilation
12
- * (in multiples of `blendK`) for `count` primitives via the recurrence
13
- * `u_1 = 0`, `u_{n+1} = (1 + u_n)^2 / 4 < 1`.
14
- */
15
- export declare function computeMaxSminDilationFactor(count: number): number;
10
+ import { computeMaxSminDilationFactor } from './sdf.ts';
11
+ export { computeMaxSminDilationFactor };
16
12
  /**
17
13
  * Formats a single rounded-rectangle primitive as an analytic SVG path (`M...A...Z`),
18
14
  * falling back to sharp corners for degenerate radii or `''` for inactive sizes.
package/dist/contour.js CHANGED
@@ -6,7 +6,8 @@
6
6
  *
7
7
  * SPDX-License-Identifier: MPL-2.0
8
8
  */
9
- import { createPreparedField, evaluateFieldDistance, prepareField } from "./sdf.js";
9
+ import { computeMaxSminDilationFactor, createPreparedField, evaluateFieldDistance, findShapeDominantPrimitive, prepareField, } from "./sdf.js";
10
+ export { computeMaxSminDilationFactor };
10
11
  const MAX_GRID_STEPS = 128;
11
12
  const MIN_GRID_STEPS = 24;
12
13
  const MAX_GRID_NODES = MAX_GRID_STEPS + 1;
@@ -79,21 +80,6 @@ function packClusterPreparedField(prepared, activeCount, clusterTag) {
79
80
  dst.blendK = prepared.blendK;
80
81
  return dst;
81
82
  }
82
- /**
83
- * Computes the tight upper bound `u_n` on cumulative quadratic `smin` outward dilation
84
- * (in multiples of `blendK`) for `count` primitives via the recurrence
85
- * `u_1 = 0`, `u_{n+1} = (1 + u_n)^2 / 4 < 1`.
86
- */
87
- export function computeMaxSminDilationFactor(count) {
88
- if (count <= 1) {
89
- return 0;
90
- }
91
- let u = 0;
92
- for (let i = 1; i < count; i++) {
93
- u = ((1 + u) * (1 + u)) * 0.25;
94
- }
95
- return u;
96
- }
97
83
  const ZERO_POINT = { x: 0, y: 0 };
98
84
  function fmt(value) {
99
85
  const rounded = Math.round(value * 100) / 100;
@@ -193,15 +179,12 @@ export function extractFieldContourPath(field, offset = ZERO_POINT) {
193
179
  if (activeCount === 0) {
194
180
  return '';
195
181
  }
196
- if (activeCount === 1) {
197
- const primitives = field.primitives;
198
- for (let i = 0; i < primitives.length; i++) {
199
- const p = primitives[i];
200
- if (isActivePrimitive(p)) {
201
- return formatRoundedRectPath(p, offset);
202
- }
182
+ if (activeCount <= 2) {
183
+ // One primitive, or one whose partner is absorbed too deep to blend: exact analytic outline.
184
+ const dominant = findShapeDominantPrimitive(field);
185
+ if (dominant) {
186
+ return formatRoundedRectPath(dominant, offset);
203
187
  }
204
- return '';
205
188
  }
206
189
  const blendK = field.blendK;
207
190
  if (blendK <= 0.001) {
package/dist/index.d.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  * SPDX-License-Identifier: MPL-2.0
12
12
  */
13
13
  export { DEFAULT_OPTICAL_MATERIALS, DEFAULT_SPRINGS, MAX_GPU_PRIMITIVES, type EdgeInsets, type GlassFieldState, type GlassPrimitive, type IGlassOverlayRenderer, type OpticalMaterial, type OpticalPresetVariant, type OverlayRenderFrame, type Point, type Rect, type RendererCapabilities, type RimMode, type SdfSample, type Size, type SminSample, type SpringParams, type ThemeMode, } from './contracts.ts';
14
- export { computeFieldBounds, computePairMergeStatus, computeRoundedBoxDistance, computeRoundedBoxSdf, computeSmin, computeSminDistance, createPreparedField, evaluateFieldDistance, evaluateFieldSdf, prepareField, type PairMergeStatus, type PreparedField, } from './sdf.ts';
14
+ export { computeFieldBounds, computeFieldShapeBounds, computePairMergeStatus, computeRoundedBoxDistance, computeRoundedBoxSdf, computeSmin, computeSminDistance, createPreparedField, evaluateFieldDistance, evaluateFieldSdf, findShapeDominantPrimitive, prepareField, type PairMergeStatus, type PreparedField, } from './sdf.ts';
15
15
  export { extractFieldContourPath, formatRoundedRectPath, } from './contour.ts';
16
16
  export type { BezelHeightProfileName, RefractionProfileName } from './refractProfile.ts';
17
17
  export { clamp, computeAreaPreservingScale, createElasticPressController, createMorphTimeline, createSpringChannel, cubicBezier, deformSizePreservingArea, easeInOutCubic, easeInQuad, easeOutCubic, evaluateSpring, evaluateSpringState, lerp, riseAndFall, smoothstep, type AreaPreservingScale, type ElasticPressController, type ElasticPressOptions, type ElasticPressState, type MorphTimeline, type MorphTimelineOptions, type MorphTimelinePhase, type MorphTimelineState, type SpringChannel, type SpringChannelOptions, type SpringChannelState, type SpringSample, } from './physics.ts';
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@
11
11
  * SPDX-License-Identifier: MPL-2.0
12
12
  */
13
13
  export { DEFAULT_OPTICAL_MATERIALS, DEFAULT_SPRINGS, MAX_GPU_PRIMITIVES, } from "./contracts.js";
14
- export { computeFieldBounds, computePairMergeStatus, computeRoundedBoxDistance, computeRoundedBoxSdf, computeSmin, computeSminDistance, createPreparedField, evaluateFieldDistance, evaluateFieldSdf, prepareField, } from "./sdf.js";
14
+ export { computeFieldBounds, computeFieldShapeBounds, computePairMergeStatus, computeRoundedBoxDistance, computeRoundedBoxSdf, computeSmin, computeSminDistance, createPreparedField, evaluateFieldDistance, evaluateFieldSdf, findShapeDominantPrimitive, prepareField, } from "./sdf.js";
15
15
  export { extractFieldContourPath, formatRoundedRectPath, } from "./contour.js";
16
16
  export { clamp, computeAreaPreservingScale, createElasticPressController, createMorphTimeline, createSpringChannel, cubicBezier, deformSizePreservingArea, easeInOutCubic, easeInQuad, easeOutCubic, evaluateSpring, evaluateSpringState, lerp, riseAndFall, smoothstep, } from "./physics.js";
17
17
  export { interpolateCssGlassSurfaceToMaterial, interpolateOpticalMaterial, interpolateVeilOptics, parseCssBoxShadow, parseCssFilterOptics, parseCssGlassSurface, parseCssRgba, tryParseCssRgba, resolveOpticalMaterial, } from "./optics.js";
package/dist/sdf.d.ts CHANGED
@@ -65,6 +65,40 @@ export declare function computeSmin(distA: number, distB: number, normalA: Point
65
65
  * Returns `{ x: 0, y: 0, width: 0, height: 0 }` when no primitive is active.
66
66
  */
67
67
  export declare function computeFieldBounds(field: GlassFieldState, padding?: number): Rect;
68
+ /**
69
+ * Computes the tight upper bound `u_n` on cumulative quadratic `smin` outward dilation
70
+ * (in multiples of `blendK`) for `count` primitives via the recurrence
71
+ * `u_1 = 0`, `u_{n+1} = (1 + u_n)^2 / 4 < 1`.
72
+ */
73
+ export declare function computeMaxSminDilationFactor(count: number): number;
74
+ /**
75
+ * Computes a tight axis-aligned box around the field's glass shape (its zero isosurface):
76
+ * the active primitives' union box, extended per side only by the `smin` bulge that can
77
+ * actually reach past it. Unlike `computeFieldBounds(field, blendK)`, a single primitive, or
78
+ * one primitive with the others blended in well inside it, yields its own border box exactly.
79
+ * Two primitives too far apart to blend yield their plain union box. With three or more the
80
+ * per-side bulge is a conservative bound that ignores separation on the other axis.
81
+ * Returns `{ x: 0, y: 0, width: 0, height: 0 }` when no primitive is active.
82
+ */
83
+ export declare function computeFieldShapeBounds(field: GlassFieldState): Rect;
84
+ /**
85
+ * Returns the one primitive that alone defines the field wherever its signed distance is at
86
+ * least `-depth`: the glass shape itself plus a `depth` px band inside its edge. That is the
87
+ * single active primitive, or, for exactly two, the outer one when the inner one sits so deep
88
+ * inside it that `smin` never blends there. Returns `null` otherwise.
89
+ *
90
+ * Renderers use it to treat a primitive absorbed into another (a popover's droplet resting
91
+ * inside its panel) exactly like the panel alone: the same analytic contour and the same lens
92
+ * encoding a resting single-rect surface uses, so the last frame of a morph and the resting
93
+ * glass cannot differ.
94
+ *
95
+ * With `m` the smallest inset of the inner box `B` from the outer box `A`, a point at depth
96
+ * `delta <= m` behind a straight edge of `A` has `dB >= m - delta` and `dA = -delta`, so
97
+ * `dB - dA >= m`; behind a corner of radius `r` the bound loosens by `CORNER_INSET * r`. `smin`
98
+ * returns `dA` unchanged when `dB - dA >= k`, outside `A` included, and the straight-edge bound
99
+ * holds across the whole band while `m` also covers its depth at edges and corners.
100
+ */
101
+ export declare function findShapeDominantPrimitive(field: GlassFieldState, depth?: number): GlassPrimitive | null;
68
102
  /**
69
103
  * Detects full absorption of primitive `b` into `a` by probing `b`'s four corners
70
104
  * against `a`'s SDF, returning the merge depth and rim opacity weight.
package/dist/sdf.js CHANGED
@@ -265,6 +265,199 @@ export function computeFieldBounds(field, padding = 0) {
265
265
  height: Math.max(0, maxY - minY + padding * 2),
266
266
  };
267
267
  }
268
+ /**
269
+ * Computes the tight upper bound `u_n` on cumulative quadratic `smin` outward dilation
270
+ * (in multiples of `blendK`) for `count` primitives via the recurrence
271
+ * `u_1 = 0`, `u_{n+1} = (1 + u_n)^2 / 4 < 1`.
272
+ */
273
+ export function computeMaxSminDilationFactor(count) {
274
+ if (count <= 1) {
275
+ return 0;
276
+ }
277
+ let u = 0;
278
+ for (let i = 1; i < count; i++) {
279
+ u = (1 + u) * (1 + u) * 0.25;
280
+ }
281
+ return u;
282
+ }
283
+ /**
284
+ * Outward reach of the `smin` bulge past one side of the active primitives' union box, given
285
+ * `gap`: how far the second-outermost primitive edge on that side lies inside the outermost one.
286
+ *
287
+ * Every primitive distance at a point `d` px beyond that side is at least `d`, and at least
288
+ * `gap + d` for all primitives but the outermost. For two primitives the quadratic `smin`
289
+ * subtracts `(k - |dA - dB|)^2 / 4k`, which bounds the bulge by `(k - gap)^2 / 4k`: continuous
290
+ * in `gap` and exactly zero once the second edge lies `k` inside, so a primitive absorbed deep
291
+ * inside another adds nothing. For more primitives the cumulative dilation `u_n * k` applies
292
+ * whenever any other edge lies within `(1 + u_n + u_{n-1}) * k`, past which neither a direct
293
+ * blend with the outermost primitive nor the others' own merged bulge can reach the side.
294
+ */
295
+ function sminSideBulge(gap, count, k) {
296
+ if (count === 2) {
297
+ return gap < k ? ((k - gap) * (k - gap)) / (4 * k) : 0;
298
+ }
299
+ const un = computeMaxSminDilationFactor(count);
300
+ const reach = (1 + un + computeMaxSminDilationFactor(count - 1)) * k;
301
+ return gap < reach ? un * k : 0;
302
+ }
303
+ /**
304
+ * Euclidean distance between two primitives' border boxes. Outside both, `smin` subtracts
305
+ * `(k - d)^2 / 4k` from `min(dA, dB) >= (s - d) / 2`, where `d = |dA - dB|` and `s` is this
306
+ * separation; that stays non-negative for every `d` once `s >= k`, so such a pair never bridges
307
+ * or bulges.
308
+ */
309
+ function boxSeparation(a, b) {
310
+ const dx = Math.abs(a.center.x - b.center.x) - (a.size.width + b.size.width) * 0.5;
311
+ const dy = Math.abs(a.center.y - b.center.y) - (a.size.height + b.size.height) * 0.5;
312
+ return Math.hypot(dx > 0 ? dx : 0, dy > 0 ? dy : 0);
313
+ }
314
+ /**
315
+ * Computes a tight axis-aligned box around the field's glass shape (its zero isosurface):
316
+ * the active primitives' union box, extended per side only by the `smin` bulge that can
317
+ * actually reach past it. Unlike `computeFieldBounds(field, blendK)`, a single primitive, or
318
+ * one primitive with the others blended in well inside it, yields its own border box exactly.
319
+ * Two primitives too far apart to blend yield their plain union box. With three or more the
320
+ * per-side bulge is a conservative bound that ignores separation on the other axis.
321
+ * Returns `{ x: 0, y: 0, width: 0, height: 0 }` when no primitive is active.
322
+ */
323
+ export function computeFieldShapeBounds(field) {
324
+ let count = 0;
325
+ let first = null;
326
+ let second = null;
327
+ // Outermost and second-outermost edge per side, as outward coordinates (-x, +x, -y, +y).
328
+ let l1 = -Infinity;
329
+ let l2 = -Infinity;
330
+ let r1 = -Infinity;
331
+ let r2 = -Infinity;
332
+ let t1 = -Infinity;
333
+ let t2 = -Infinity;
334
+ let b1 = -Infinity;
335
+ let b2 = -Infinity;
336
+ const primitives = field.primitives;
337
+ for (let i = 0; i < primitives.length; i++) {
338
+ const p = primitives[i];
339
+ if (!isActivePrimitive(p)) {
340
+ continue;
341
+ }
342
+ count++;
343
+ if (first === null) {
344
+ first = p;
345
+ }
346
+ else if (second === null) {
347
+ second = p;
348
+ }
349
+ const halfW = p.size.width * 0.5;
350
+ const halfH = p.size.height * 0.5;
351
+ const left = -(p.center.x - halfW);
352
+ const right = p.center.x + halfW;
353
+ const top = -(p.center.y - halfH);
354
+ const bottom = p.center.y + halfH;
355
+ if (left > l1) {
356
+ l2 = l1;
357
+ l1 = left;
358
+ }
359
+ else if (left > l2) {
360
+ l2 = left;
361
+ }
362
+ if (right > r1) {
363
+ r2 = r1;
364
+ r1 = right;
365
+ }
366
+ else if (right > r2) {
367
+ r2 = right;
368
+ }
369
+ if (top > t1) {
370
+ t2 = t1;
371
+ t1 = top;
372
+ }
373
+ else if (top > t2) {
374
+ t2 = top;
375
+ }
376
+ if (bottom > b1) {
377
+ b2 = b1;
378
+ b1 = bottom;
379
+ }
380
+ else if (bottom > b2) {
381
+ b2 = bottom;
382
+ }
383
+ }
384
+ if (count === 0) {
385
+ return { x: 0, y: 0, width: 0, height: 0 };
386
+ }
387
+ const k = field.blendK;
388
+ const blended = count >= 2 && k > EPSILON && !(count === 2 && boxSeparation(first, second) >= k);
389
+ const padL = blended ? sminSideBulge(l1 - l2, count, k) : 0;
390
+ const padR = blended ? sminSideBulge(r1 - r2, count, k) : 0;
391
+ const padT = blended ? sminSideBulge(t1 - t2, count, k) : 0;
392
+ const padB = blended ? sminSideBulge(b1 - b2, count, k) : 0;
393
+ const minX = -l1 - padL;
394
+ const minY = -t1 - padT;
395
+ return {
396
+ x: minX,
397
+ y: minY,
398
+ width: Math.max(0, r1 + padR - minX),
399
+ height: Math.max(0, b1 + padB - minY),
400
+ };
401
+ }
402
+ /** `1 - 1/sqrt(2)`: how far a rounded corner's 45 degree point sits inside its box, per unit radius. */
403
+ const CORNER_INSET = 1 - Math.SQRT1_2;
404
+ /**
405
+ * Returns the one primitive that alone defines the field wherever its signed distance is at
406
+ * least `-depth`: the glass shape itself plus a `depth` px band inside its edge. That is the
407
+ * single active primitive, or, for exactly two, the outer one when the inner one sits so deep
408
+ * inside it that `smin` never blends there. Returns `null` otherwise.
409
+ *
410
+ * Renderers use it to treat a primitive absorbed into another (a popover's droplet resting
411
+ * inside its panel) exactly like the panel alone: the same analytic contour and the same lens
412
+ * encoding a resting single-rect surface uses, so the last frame of a morph and the resting
413
+ * glass cannot differ.
414
+ *
415
+ * With `m` the smallest inset of the inner box `B` from the outer box `A`, a point at depth
416
+ * `delta <= m` behind a straight edge of `A` has `dB >= m - delta` and `dA = -delta`, so
417
+ * `dB - dA >= m`; behind a corner of radius `r` the bound loosens by `CORNER_INSET * r`. `smin`
418
+ * returns `dA` unchanged when `dB - dA >= k`, outside `A` included, and the straight-edge bound
419
+ * holds across the whole band while `m` also covers its depth at edges and corners.
420
+ */
421
+ export function findShapeDominantPrimitive(field, depth = 0) {
422
+ let first = null;
423
+ let second = null;
424
+ const primitives = field.primitives;
425
+ for (let i = 0; i < primitives.length; i++) {
426
+ const p = primitives[i];
427
+ if (!isActivePrimitive(p)) {
428
+ continue;
429
+ }
430
+ if (first === null) {
431
+ first = p;
432
+ }
433
+ else if (second === null) {
434
+ second = p;
435
+ }
436
+ else {
437
+ return null;
438
+ }
439
+ }
440
+ if (first === null || second === null) {
441
+ return first;
442
+ }
443
+ const k = Math.max(0, field.blendK);
444
+ const band = Math.max(0, depth);
445
+ if (absorbs(first, second, k, band))
446
+ return first;
447
+ if (absorbs(second, first, k, band))
448
+ return second;
449
+ return null;
450
+ }
451
+ function absorbs(outer, inner, k, band) {
452
+ const outerHalfW = outer.size.width * 0.5;
453
+ const outerHalfH = outer.size.height * 0.5;
454
+ const innerHalfW = inner.size.width * 0.5;
455
+ const innerHalfH = inner.size.height * 0.5;
456
+ const inset = Math.min(inner.center.x - innerHalfW - (outer.center.x - outerHalfW), outer.center.x + outerHalfW - (inner.center.x + innerHalfW), inner.center.y - innerHalfH - (outer.center.y - outerHalfH), outer.center.y + outerHalfH - (inner.center.y + innerHalfH));
457
+ const radius = Math.max(0, Math.min(outer.radius, outerHalfW, outerHalfH));
458
+ const cornerReach = CORNER_INSET * radius;
459
+ return inset >= band && inset >= cornerReach + Math.SQRT1_2 * band && inset >= k + cornerReach;
460
+ }
268
461
  /**
269
462
  * Detects full absorption of primitive `b` into `a` by probing `b`'s four corners
270
463
  * against `a`'s SDF, returning the merge depth and rim opacity weight.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sessile/core",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "type": "module",
5
5
  "license": "MPL-2.0",
6
6
  "description": "Zero-dependency pure TypeScript kernel for live-DOM continuous glass morphing.",