@sessile/presets 0.1.0 → 0.3.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.
@@ -57,6 +57,10 @@ export interface DragMergeOptions {
57
57
  magneticRadiusPx?: number;
58
58
  magneticStrength?: number;
59
59
  neighborTugRatio?: number;
60
+ /** Peak area-preserving squash-and-stretch ratio driven by node spring velocity (`0` disables; defaults to `0.08`). */
61
+ velocityStretch?: number;
62
+ /** Speed (px/s) at which velocity-driven stretch reaches 50% of `velocityStretch` (defaults to `900`). */
63
+ velocityStretchReferencePxPerSec?: number;
60
64
  spring?: SpringParams;
61
65
  bounds?: Rect;
62
66
  returnToRestOnRelease?: boolean;
@@ -96,6 +100,8 @@ export declare const DRAG_MERGE_DEFAULTS: {
96
100
  readonly magneticRadiusPx: 48;
97
101
  readonly magneticStrength: 0.18;
98
102
  readonly neighborTugRatio: 0.14;
103
+ readonly velocityStretch: 0.08;
104
+ readonly velocityStretchReferencePxPerSec: 900;
99
105
  readonly spring: {
100
106
  readonly mass: 1;
101
107
  readonly stiffness: 280;
package/dist/dragMerge.js CHANGED
@@ -7,13 +7,15 @@
7
7
  *
8
8
  * SPDX-License-Identifier: MPL-2.0
9
9
  */
10
- import { clamp, computeRoundedBoxDistance, createSpringChannel, } from '@sessile/core';
10
+ import { clamp, computeRoundedBoxDistance, createSpringChannel, deformSizePreservingArea, } from '@sessile/core';
11
11
  /** Default smin blend, magnetic range, and spring constants for drag-merge fields. */
12
12
  export const DRAG_MERGE_DEFAULTS = {
13
13
  blendK: 32,
14
14
  magneticRadiusPx: 48,
15
15
  magneticStrength: 0.18,
16
16
  neighborTugRatio: 0.14,
17
+ velocityStretch: 0.08,
18
+ velocityStretchReferencePxPerSec: 900,
17
19
  spring: { mass: 1, stiffness: 280, dampingRatio: 0.72 },
18
20
  };
19
21
  function clampNodeSize(size) {
@@ -84,6 +86,9 @@ export function createDragMergeController(options = {}) {
84
86
  const magneticRadiusPx = Math.max(0, options.magneticRadiusPx ?? DRAG_MERGE_DEFAULTS.magneticRadiusPx);
85
87
  const magneticStrength = clamp(options.magneticStrength ?? DRAG_MERGE_DEFAULTS.magneticStrength, 0, 1);
86
88
  const neighborTugRatio = clamp(options.neighborTugRatio ?? DRAG_MERGE_DEFAULTS.neighborTugRatio, 0, 1);
89
+ const velocityStretch = clamp(options.velocityStretch ?? DRAG_MERGE_DEFAULTS.velocityStretch, 0, 0.35);
90
+ const velocityStretchRef = Math.max(1, options.velocityStretchReferencePxPerSec ??
91
+ DRAG_MERGE_DEFAULTS.velocityStretchReferencePxPerSec);
87
92
  const springParams = options.spring ?? DRAG_MERGE_DEFAULTS.spring;
88
93
  const returnToRestOnRelease = options.returnToRestOnRelease ?? false;
89
94
  const snapToNearestOnRelease = options.snapToNearestOnRelease ?? false;
@@ -257,32 +262,71 @@ export function createDragMergeController(options = {}) {
257
262
  }
258
263
  }
259
264
  }
265
+ function buildRenderedNodeSnapshot(node) {
266
+ const xState = node.x.getState();
267
+ const yState = node.y.getState();
268
+ const settled = xState.settled && yState.settled;
269
+ const center = { x: xState.value, y: yState.value };
270
+ let primitiveSize = { width: node.size.width, height: node.size.height };
271
+ let primitiveRadius = node.radius;
272
+ if (!reducedMotion && !node.pinned && !settled && velocityStretch > 0) {
273
+ const speed = Math.hypot(xState.velocity, yState.velocity);
274
+ if (speed > 1) {
275
+ const normSpeed = speed / (speed + velocityStretchRef);
276
+ let stretch = 1 + velocityStretch * normSpeed;
277
+ if (bounds) {
278
+ const lenSq = xState.velocity * xState.velocity + yState.velocity * yState.velocity;
279
+ const alpha = lenSq > 1e-12
280
+ ? (xState.velocity * xState.velocity - yState.velocity * yState.velocity) / lenSq
281
+ : 0;
282
+ if (alpha > 1e-6 && node.size.width > 0) {
283
+ const availHalfW = Math.max(0, Math.min(center.x - bounds.x, bounds.x + bounds.width - center.x));
284
+ const maxScaleX = (2 * availHalfW) / node.size.width;
285
+ stretch = maxScaleX <= 1 ? 1 : Math.min(stretch, Math.pow(maxScaleX, 1 / alpha));
286
+ }
287
+ else if (alpha < -1e-6 && node.size.height > 0) {
288
+ const availHalfH = Math.max(0, Math.min(center.y - bounds.y, bounds.y + bounds.height - center.y));
289
+ const maxScaleY = (2 * availHalfH) / node.size.height;
290
+ stretch = maxScaleY <= 1 ? 1 : Math.min(stretch, Math.pow(maxScaleY, 1 / -alpha));
291
+ }
292
+ }
293
+ if (stretch > 1 + 1e-9) {
294
+ primitiveSize = deformSizePreservingArea(node.size, stretch, xState.velocity, yState.velocity);
295
+ primitiveRadius = clampNodeRadius(node.radius, primitiveSize);
296
+ }
297
+ }
298
+ }
299
+ const primitive = {
300
+ id: node.id,
301
+ center,
302
+ size: primitiveSize,
303
+ radius: primitiveRadius,
304
+ weight: node.weight,
305
+ };
306
+ return { primitive, center, xState, yState, settled };
307
+ }
260
308
  function buildState() {
261
309
  const nodeStates = [];
262
310
  const primitives = [];
263
311
  let animating = false;
264
312
  for (const node of nodes) {
265
- const xState = node.x.getState();
266
- const yState = node.y.getState();
267
- const settled = xState.settled && yState.settled;
268
- if (!settled) {
313
+ const snap = buildRenderedNodeSnapshot(node);
314
+ if (!snap.settled) {
269
315
  animating = true;
270
316
  }
271
- const center = { x: xState.value, y: yState.value };
272
- const primitive = makePrimitiveFromCoords(node, center);
273
- primitives.push(primitive);
317
+ primitives.push(snap.primitive);
274
318
  nodeStates.push({
275
319
  id: node.id,
276
- primitive,
277
- center,
278
- targetCenter: { x: xState.target, y: yState.target },
320
+ primitive: snap.primitive,
321
+ center: { x: snap.center.x, y: snap.center.y },
322
+ targetCenter: { x: snap.xState.target, y: snap.yState.target },
279
323
  restCenter: { ...node.restCenter },
280
324
  size: { ...node.size },
281
325
  radius: node.radius,
282
326
  pinned: node.pinned,
283
327
  weight: node.weight,
284
328
  dragging: activeDragId === node.id,
285
- settled,
329
+ settled: snap.settled,
286
330
  });
287
331
  }
288
332
  const links = [];
@@ -338,11 +382,8 @@ export function createDragMergeController(options = {}) {
338
382
  hitTest(point) {
339
383
  for (let i = nodes.length - 1; i >= 0; i -= 1) {
340
384
  const node = nodes[i];
341
- const prim = makePrimitiveFromCoords(node, {
342
- x: node.x.getState().value,
343
- y: node.y.getState().value,
344
- });
345
- if (computeRoundedBoxDistance(point.x, point.y, prim) <= 0) {
385
+ const { primitive } = buildRenderedNodeSnapshot(node);
386
+ if (computeRoundedBoxDistance(point.x, point.y, primitive) <= 0) {
346
387
  return node.id;
347
388
  }
348
389
  }
@@ -24,6 +24,8 @@ export interface SegmentedPillOptions {
24
24
  blendK?: number;
25
25
  squeezeRatio?: number;
26
26
  maxStretchPx?: number;
27
+ /** Enforce exact 2D area conservation (`W' * H' === W * H`) on stretched lobes (defaults to `true`). */
28
+ preserveArea?: boolean;
27
29
  reducedMotion?: boolean;
28
30
  }
29
31
  /** Instantaneous snapshot of the segmented indicator pill and its SDF field. */
package/dist/segmented.js CHANGED
@@ -7,7 +7,7 @@
7
7
  *
8
8
  * SPDX-License-Identifier: MPL-2.0
9
9
  */
10
- import { clamp, createSpringChannel, lerp, } from '@sessile/core';
10
+ import { clamp, computeAreaPreservingScale, createSpringChannel, lerp, } from '@sessile/core';
11
11
  /** Default springs and deformation limits for the segmented pill preset. */
12
12
  export const SEGMENTED_PILL_DEFAULTS = {
13
13
  leadSpring: { mass: 1, stiffness: 380, dampingRatio: 0.78 },
@@ -48,13 +48,35 @@ function evalSegmentedBoxesGap(sep, ux, uy, leadW, leadH, trailW, trailH, radius
48
48
  const oy = Math.max(0, Math.abs(uy) * sep - innerSumH);
49
49
  return Math.hypot(ox, oy) - rLead - rTrail;
50
50
  }
51
- function computeConnectedStretchCap(maxStretchPx, ux, uy, lw, lh, tw, th, lr, baseBlendK, squeezeRatio) {
51
+ function computeLobeScale(stretchRatio, squeezeRatio, ux, uy, preserveArea, isLead) {
52
+ if (preserveArea) {
53
+ const stretchAmount = isLead
54
+ ? squeezeRatio * 0.5 * stretchRatio
55
+ : squeezeRatio * stretchRatio;
56
+ return computeAreaPreservingScale(1 + stretchAmount, ux, uy);
57
+ }
58
+ if (isLead) {
59
+ return {
60
+ scaleX: (1 + squeezeRatio * 0.35 * stretchRatio * Math.abs(ux)) *
61
+ (1 - squeezeRatio * 0.5 * stretchRatio * Math.abs(uy)),
62
+ scaleY: (1 + squeezeRatio * 0.35 * stretchRatio * Math.abs(uy)) *
63
+ (1 - squeezeRatio * 0.5 * stretchRatio * Math.abs(ux)),
64
+ };
65
+ }
66
+ return {
67
+ scaleX: 1 - squeezeRatio * stretchRatio * Math.abs(uy),
68
+ scaleY: 1 - squeezeRatio * stretchRatio * Math.abs(ux),
69
+ };
70
+ }
71
+ function computeConnectedStretchCap(maxStretchPx, ux, uy, lw, lh, tw, th, lr, baseBlendK, squeezeRatio, preserveArea) {
52
72
  if (maxStretchPx <= 0)
53
73
  return 0;
54
- const minLeadW = Math.max(0, lw * (1 + squeezeRatio * 0.35 * Math.abs(ux)) * (1 - squeezeRatio * 0.5 * Math.abs(uy)));
55
- const minLeadH = Math.max(0, lh * (1 + squeezeRatio * 0.35 * Math.abs(uy)) * (1 - squeezeRatio * 0.5 * Math.abs(ux)));
56
- const minTrailW = Math.max(0, tw * (1 - squeezeRatio * Math.abs(uy)));
57
- const minTrailH = Math.max(0, th * (1 - squeezeRatio * Math.abs(ux)));
74
+ const leadScale = computeLobeScale(1, squeezeRatio, ux, uy, preserveArea, true);
75
+ const trailScale = computeLobeScale(1, squeezeRatio, ux, uy, preserveArea, false);
76
+ const minLeadW = Math.max(0, lw * leadScale.scaleX);
77
+ const minLeadH = Math.max(0, lh * leadScale.scaleY);
78
+ const minTrailW = Math.max(0, tw * trailScale.scaleX);
79
+ const minTrailH = Math.max(0, th * trailScale.scaleY);
58
80
  const maxBridgeGap = baseBlendK * 0.4;
59
81
  if (evalSegmentedBoxesGap(maxStretchPx, ux, uy, minLeadW, minLeadH, minTrailW, minTrailH, lr) <= maxBridgeGap) {
60
82
  return maxStretchPx;
@@ -91,6 +113,7 @@ export function createSegmentedPillController(options) {
91
113
  const baseBlendK = Math.max(0, options.blendK ?? SEGMENTED_PILL_DEFAULTS.blendK);
92
114
  const squeezeRatio = clamp(options.squeezeRatio ?? SEGMENTED_PILL_DEFAULTS.squeezeRatio, 0, 0.45);
93
115
  const maxStretchPx = Math.max(0, options.maxStretchPx ?? SEGMENTED_PILL_DEFAULTS.maxStretchPx);
116
+ const preserveArea = options.preserveArea ?? true;
94
117
  const defaultRadius = options.defaultRadius;
95
118
  let reducedMotion = options.reducedMotion ?? false;
96
119
  let disposed = false;
@@ -202,19 +225,15 @@ export function createSegmentedPillController(options) {
202
225
  const ux = dx / rawLagDist;
203
226
  const uy = dy / rawLagDist;
204
227
  // Constrain maximum center separation by the actual rounded-box bridge gap so diagonal jumps never split into two lobes.
205
- const stretchCap = computeConnectedStretchCap(maxStretchPx, ux, uy, lw, lh, tw, th, lr, baseBlendK, squeezeRatio);
228
+ const stretchCap = computeConnectedStretchCap(maxStretchPx, ux, uy, lw, lh, tw, th, lr, baseBlendK, squeezeRatio, preserveArea);
206
229
  let clampedSep = stretchCap > 0 ? (stretchCap * rawLagDist) / (stretchCap + rawLagDist) : 0;
207
230
  const stretchRatio = stretchCap > 0 ? clamp(clampedSep / stretchCap, 0, 1) : 0;
208
- const leadSqueezeX = 1 - squeezeRatio * 0.5 * stretchRatio * Math.abs(uy);
209
- const leadSqueezeY = 1 - squeezeRatio * 0.5 * stretchRatio * Math.abs(ux);
210
- const leadStretchX = 1 + squeezeRatio * 0.35 * stretchRatio * Math.abs(ux);
211
- const leadStretchY = 1 + squeezeRatio * 0.35 * stretchRatio * Math.abs(uy);
212
- const trailSqueezeX = 1 - squeezeRatio * stretchRatio * Math.abs(uy);
213
- const trailSqueezeY = 1 - squeezeRatio * stretchRatio * Math.abs(ux);
214
- const outLeadW = Math.max(0, lw * leadStretchX * leadSqueezeX);
215
- const outLeadH = Math.max(0, lh * leadStretchY * leadSqueezeY);
216
- const outTrailW = Math.max(0, tw * trailSqueezeX);
217
- const outTrailH = Math.max(0, th * trailSqueezeY);
231
+ const leadScale = computeLobeScale(stretchRatio, squeezeRatio, ux, uy, preserveArea, true);
232
+ const trailScale = computeLobeScale(stretchRatio, squeezeRatio, ux, uy, preserveArea, false);
233
+ const outLeadW = Math.max(0, lw * leadScale.scaleX);
234
+ const outLeadH = Math.max(0, lh * leadScale.scaleY);
235
+ const outTrailW = Math.max(0, tw * trailScale.scaleX);
236
+ const outTrailH = Math.max(0, th * trailScale.scaleY);
218
237
  const activeBlendK = baseBlendK * clamp(lerp(0.25, 1, stretchRatio), 0, 1);
219
238
  const activeMaxGap = activeBlendK * 0.45;
220
239
  if (clampedSep > 0 &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sessile/presets",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "license": "MPL-2.0",
6
6
  "description": "Optional interaction presets (morphing menu, segmented pill, drag-merge) built on Sessile.",
@@ -26,7 +26,7 @@
26
26
  "access": "public"
27
27
  },
28
28
  "dependencies": {
29
- "@sessile/core": "0.1.0"
29
+ "@sessile/core": "0.3.0"
30
30
  },
31
31
  "scripts": {
32
32
  "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit",