@skillre/dsh-plugin-sticky-wall 0.4.0 → 0.6.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/README.en.md +7 -4
- package/README.md +7 -4
- package/compatibility.json +11 -6
- package/docs/preview.svg +31 -24
- package/docs/preview.zh.svg +30 -23
- package/lib/client.js +514 -95
- package/lib/layout.d.ts +142 -24
- package/lib/layout.js +547 -121
- package/lib/store.d.ts +16 -4
- package/lib/store.js +32 -11
- package/lib/transport.d.ts +7 -2
- package/lib/transport.js +17 -3
- package/lib/types.d.ts +25 -6
- package/lib/types.js +13 -6
- package/package.json +1 -1
package/lib/layout.js
CHANGED
|
@@ -5,40 +5,69 @@
|
|
|
5
5
|
* rotation and a width preset. Two operations produce those numbers:
|
|
6
6
|
*
|
|
7
7
|
* - {@link arrangeLayouts} is the Host-side "tidy" pass. It packs the wall into
|
|
8
|
-
* a
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
8
|
+
* a compact cluster **centred on the world origin**, hugging the rectangle the
|
|
9
|
+
* centrepiece reserves: the Client opens looking at that middle, and a tidied
|
|
10
|
+
* wall reads as one composition rather than a list that happens to wrap. It is
|
|
11
|
+
* deterministic — the same notes always produce the same wall — and it is
|
|
12
|
+
* collision-checked rather than estimated, so two cards can never overlap even
|
|
13
|
+
* at the height cap.
|
|
14
14
|
* - {@link previewLayout} is the Client-side fallback for a note that was never
|
|
15
15
|
* placed (a wall written before the canvas existed, or a note created by the
|
|
16
|
-
* agent tool). It is
|
|
17
|
-
* note, index-stable, and centred, so the first render is already a
|
|
18
|
-
* wall around the middle instead of one tall stack.
|
|
16
|
+
* agent tool). It is a golden-angle spiral without the collision check: O(1)
|
|
17
|
+
* per note, index-stable, and centred, so the first render is already a
|
|
18
|
+
* scattered wall around the middle instead of one tall stack.
|
|
19
19
|
*
|
|
20
20
|
* Both halves import this module, so the geometry never drifts between the
|
|
21
21
|
* value the Host persists and the value the Client previews.
|
|
22
22
|
*/
|
|
23
|
-
import {
|
|
23
|
+
import { CENTRE_RESERVE, LAYOUT_LIMITS, WIDTHS, WIDTH_PX, } from './types.js';
|
|
24
24
|
/** Golden angle: the phyllotaxis constant that keeps a spiral from lining up. */
|
|
25
25
|
const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5));
|
|
26
26
|
/** Breathing room kept between two tidied cards, in world pixels. */
|
|
27
27
|
const TIDY_GAP = 26;
|
|
28
|
-
/** Hash-driven radial offset applied to a tidied card, in world pixels. */
|
|
29
|
-
const TIDY_JITTER = 5;
|
|
30
28
|
/** Maximum rotation the tidy pass adds, in degrees. */
|
|
31
29
|
const TIDY_TILT = 3.4;
|
|
32
30
|
/** Radial growth per derived (never-placed) note, in world pixels. */
|
|
33
31
|
const PREVIEW_STEP = 196;
|
|
34
32
|
/** Height assumed for a derived position, so the derived box looks centred. */
|
|
35
33
|
const PREVIEW_NOMINAL_HEIGHT = 200;
|
|
34
|
+
/**
|
|
35
|
+
* Horizontal stretch applied to the derived spiral, matching the reference
|
|
36
|
+
* viewport's aspect. A derived wall is never tidied, so its shape is the only
|
|
37
|
+
* thing keeping it framable at a legible zoom.
|
|
38
|
+
*/
|
|
39
|
+
const PREVIEW_SQUASH = 1.4;
|
|
40
|
+
/** Clearance the free-spot search keeps between a new card and its neighbours. */
|
|
41
|
+
const SPOT_GAP = 24;
|
|
42
|
+
/** Candidate positions the free-spot search tries before falling back. */
|
|
43
|
+
const MAX_SPOTS = 2048;
|
|
44
|
+
/**
|
|
45
|
+
* How far each successive contour grows past the reserved rectangle, in world
|
|
46
|
+
* pixels. A contour is only tried when every slot on the previous one is taken,
|
|
47
|
+
* so this is the radial grain of the packing: smaller packs tighter but costs
|
|
48
|
+
* more candidate tests.
|
|
49
|
+
*/
|
|
50
|
+
const FRAME_STEP = 20;
|
|
51
|
+
/** Spacing between two candidate slots along one contour, in world pixels. */
|
|
52
|
+
const CONTOUR_STEP = 14;
|
|
53
|
+
/** Contours walked outward before a card gives up and takes the safety spot. */
|
|
54
|
+
const MAX_FRAMES = 1024;
|
|
55
|
+
/** Upper bound on slots tested per contour, so a huge wall stays bounded. */
|
|
56
|
+
const MAX_SLOTS = 320;
|
|
57
|
+
/**
|
|
58
|
+
* The framing the tidy pass packs FOR: the usable canvas of a typical window, in
|
|
59
|
+
* world pixels at zoom 1. A candidate slot is scored by how much this rectangle
|
|
60
|
+
* would have to zoom out to hold the wall, which is the number the user actually
|
|
61
|
+
* feels. The exact window is unknown to the Host, so this is a shape, not a size.
|
|
62
|
+
*/
|
|
63
|
+
const FRAMING_WIDTH = 1064;
|
|
64
|
+
const FRAMING_HEIGHT = 664;
|
|
65
|
+
/** The reference viewport's own aspect; the contour is grown to match it. */
|
|
66
|
+
const FRAMING_ASPECT = 1.5;
|
|
36
67
|
/** Maximum rotation the preview fallback adds, in degrees. */
|
|
37
68
|
const PREVIEW_TILT = 4;
|
|
38
69
|
/** Spatial-grid cell; wider than the biggest rotated card plus the gap. */
|
|
39
70
|
const GRID_CELL = 430;
|
|
40
|
-
/** Candidate spiral positions tried per card before the safety fallback. */
|
|
41
|
-
const MAX_CANDIDATES = 4096;
|
|
42
71
|
/** Deterministic 32-bit FNV-1a hash; no crypto, identical on both halves. */
|
|
43
72
|
function hash(text) {
|
|
44
73
|
let value = 0x811c9dc5;
|
|
@@ -160,19 +189,32 @@ export function layoutOf(note, index) {
|
|
|
160
189
|
/**
|
|
161
190
|
* Derived position for a note the user never placed.
|
|
162
191
|
*
|
|
163
|
-
* Index-based so two never-placed notes cannot collide,
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
* a
|
|
192
|
+
* Index-based so two never-placed notes cannot collide, and laid out on a
|
|
193
|
+
* SQUASHED spiral — the same golden-angle walk with its radius stretched along
|
|
194
|
+
* the x axis — so the result reads as a wide cluster around the middle rather
|
|
195
|
+
* than a circle or a grid. The squash matters: a circle leaves the left and
|
|
196
|
+
* right of a wide window empty and the camera then has to zoom out to frame the
|
|
197
|
+
* empty corners, which is the same "everything looks small" failure the tidy
|
|
198
|
+
* pass fixes. The height used for centring is a nominal constant, not the real
|
|
199
|
+
* estimate: a derived position must not move when the user edits the text.
|
|
167
200
|
*/
|
|
168
201
|
export function previewLayout(note, index) {
|
|
169
202
|
const width = previewWidth(note.id);
|
|
170
203
|
// The derived wall leaves the same clear middle as the tidy pass: the centre
|
|
171
|
-
// is the wall's centrepiece, not another place for a card.
|
|
172
|
-
|
|
204
|
+
// is the wall's centrepiece, not another place for a card. The step out is
|
|
205
|
+
// derived from the reserve's own half-diagonal so the first derived card
|
|
206
|
+
// lands outside the rectangle at any reserve size.
|
|
207
|
+
const reserve = CENTRE_RESERVE;
|
|
208
|
+
// The reserve's half-diagonal is how far the RECTANGLE's corner is from the
|
|
209
|
+
// middle; the card's own half-diagonal is how far the CARD reaches back inward
|
|
210
|
+
// from its centre. Adding both is what guarantees the derived box cannot clip
|
|
211
|
+
// a corner of the reserved band.
|
|
212
|
+
const startRadius = Math.hypot(reserve.halfWidth, reserve.halfHeight) +
|
|
213
|
+
Math.hypot(WIDTH_PX[width], PREVIEW_NOMINAL_HEIGHT) / 2;
|
|
214
|
+
const radius = PREVIEW_STEP * Math.sqrt(Math.max(0, index) + (startRadius / PREVIEW_STEP) ** 2);
|
|
173
215
|
const theta = index * GOLDEN_ANGLE + (unit(`${note.id}:pa`) - 0.5) * 0.55;
|
|
174
216
|
return clampLayout({
|
|
175
|
-
x: Math.cos(theta) * radius - WIDTH_PX[width] / 2,
|
|
217
|
+
x: Math.cos(theta) * radius * PREVIEW_SQUASH - WIDTH_PX[width] / 2,
|
|
176
218
|
y: Math.sin(theta) * radius - PREVIEW_NOMINAL_HEIGHT / 2,
|
|
177
219
|
rotate: (unit(`${note.id}:pr`) - 0.5) * 2 * PREVIEW_TILT,
|
|
178
220
|
width,
|
|
@@ -193,18 +235,34 @@ export function rotatedExtent(width, height, rotate) {
|
|
|
193
235
|
return { width: width * cos + height * sin, height: height * cos + width * sin };
|
|
194
236
|
}
|
|
195
237
|
/**
|
|
196
|
-
*
|
|
238
|
+
* Whether a footprint stays out of a clear rectangle.
|
|
197
239
|
*
|
|
198
240
|
* The tidy pass keeps the wall's middle clear for the centrepiece, and "clear"
|
|
199
|
-
* has to mean the card's EDGE: measuring from the centre let a wide card
|
|
200
|
-
* over the capture field by half its own width.
|
|
241
|
+
* has to mean the card's EDGE: measuring from the card's centre let a wide card
|
|
242
|
+
* reach over the capture field by half its own width. A rectangle rather than a
|
|
243
|
+
* radius is the honest shape, because the centrepiece is a wide, short stack —
|
|
244
|
+
* a circle either wasted a tall column of paper above and below it or let wide
|
|
245
|
+
* cards in from the sides.
|
|
201
246
|
*/
|
|
202
|
-
function
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
247
|
+
export function intersectsReserve(box, reserve = CENTRE_RESERVE) {
|
|
248
|
+
const clear = clearsReserve(box, reserve);
|
|
249
|
+
return !clear;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Tolerance for the reserve test, in world pixels.
|
|
253
|
+
*
|
|
254
|
+
* Positions are rounded to one decimal for storage, so a card packed exactly on
|
|
255
|
+
* the boundary comes back a hundredth of a pixel inside it. Without a tolerance
|
|
256
|
+
* that rounding reads as a violation — and worse, `pushOutOfReserve` would then
|
|
257
|
+
* "correct" a card that was already in the right place. A twentieth of a pixel is
|
|
258
|
+
* far below anything visible and well above the rounding error.
|
|
259
|
+
*/
|
|
260
|
+
const RESERVE_EPSILON = 0.05;
|
|
261
|
+
function clearsReserve(box, reserve) {
|
|
262
|
+
return !(box.x < reserve.halfWidth - RESERVE_EPSILON &&
|
|
263
|
+
box.x + box.width > -reserve.halfWidth + RESERVE_EPSILON &&
|
|
264
|
+
box.y < reserve.halfHeight - RESERVE_EPSILON &&
|
|
265
|
+
box.y + box.height > -reserve.halfHeight + RESERVE_EPSILON);
|
|
208
266
|
}
|
|
209
267
|
/** Cell coordinate of one world value. */
|
|
210
268
|
function cellOf(value) {
|
|
@@ -234,6 +292,72 @@ function collides(box, grid) {
|
|
|
234
292
|
}
|
|
235
293
|
return false;
|
|
236
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* Distance from `box` to the nearest placed card, for tie-breaking between two
|
|
297
|
+
* candidates that both pass the collision test. Returns a large number when
|
|
298
|
+
* nothing is nearby.
|
|
299
|
+
*/
|
|
300
|
+
function nearestGap(box, grid) {
|
|
301
|
+
let nearest = Number.POSITIVE_INFINITY;
|
|
302
|
+
for (let cellX = cellOf(box.x - GRID_CELL); cellX <= cellOf(box.x + box.width + GRID_CELL); cellX += 1) {
|
|
303
|
+
for (let cellY = cellOf(box.y - GRID_CELL); cellY <= cellOf(box.y + box.height + GRID_CELL); cellY += 1) {
|
|
304
|
+
const bucket = grid.get(`${cellX}:${cellY}`);
|
|
305
|
+
if (bucket === undefined)
|
|
306
|
+
continue;
|
|
307
|
+
for (const other of bucket) {
|
|
308
|
+
const gapX = Math.max(other.x - (box.x + box.width), box.x - (other.x + other.width), 0);
|
|
309
|
+
const gapY = Math.max(other.y - (box.y + box.height), box.y - (other.y + other.height), 0);
|
|
310
|
+
nearest = Math.min(nearest, Math.hypot(gapX, gapY));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return nearest;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* The point at arc length `distance` around a rectangle's outline, with the
|
|
318
|
+
* outward normal there.
|
|
319
|
+
*
|
|
320
|
+
* This is the contour the tidy pass walks. Unlike a circle, a rectangle's
|
|
321
|
+
* outline stays a card's own half-size away from the reserve in every direction,
|
|
322
|
+
* which is what keeps the innermost cards as close to the middle as their size
|
|
323
|
+
* allows instead of at the rectangle's corner distance all the way round.
|
|
324
|
+
*
|
|
325
|
+
* `halfWidth`/`halfHeight` are the contour's own half-extents, so a contour is
|
|
326
|
+
* just "the reserve grown by one frame".
|
|
327
|
+
*/
|
|
328
|
+
function contourPoint(halfWidth, halfHeight, distance) {
|
|
329
|
+
const width = halfWidth * 2;
|
|
330
|
+
const height = halfHeight * 2;
|
|
331
|
+
const perimeter = 2 * (width + height);
|
|
332
|
+
let along = ((distance % perimeter) + perimeter) % perimeter;
|
|
333
|
+
// Clockwise from the top-left corner: top edge, right edge, bottom, left.
|
|
334
|
+
if (along < width)
|
|
335
|
+
return { x: -halfWidth + along, y: -halfHeight, nx: 0, ny: -1 };
|
|
336
|
+
along -= width;
|
|
337
|
+
if (along < height)
|
|
338
|
+
return { x: halfWidth, y: -halfHeight + along, nx: 1, ny: 0 };
|
|
339
|
+
along -= height;
|
|
340
|
+
if (along < width)
|
|
341
|
+
return { x: halfWidth - along, y: halfHeight, nx: 0, ny: 1 };
|
|
342
|
+
along -= width;
|
|
343
|
+
return { x: -halfWidth, y: halfHeight - along, nx: -1, ny: 0 };
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* The footprint of a card whose nearest edge sits on a contour point.
|
|
347
|
+
*
|
|
348
|
+
* The reach is half the card's extent MEASURED ALONG THE NORMAL, not half its
|
|
349
|
+
* width: on the top and bottom edges a tilted card reaches out by half its
|
|
350
|
+
* height, and charging it half its width there would let it clip the reserve.
|
|
351
|
+
*/
|
|
352
|
+
function boxOnContour(point, extent) {
|
|
353
|
+
const reach = (Math.abs(point.nx) * extent.width + Math.abs(point.ny) * extent.height) / 2;
|
|
354
|
+
return {
|
|
355
|
+
x: point.x + point.nx * reach - extent.width / 2,
|
|
356
|
+
y: point.y + point.ny * reach - extent.height / 2,
|
|
357
|
+
width: extent.width,
|
|
358
|
+
height: extent.height,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
237
361
|
/** Register one accepted footprint in every cell it touches. */
|
|
238
362
|
function remember(box, grid) {
|
|
239
363
|
for (let cellX = cellOf(box.x); cellX <= cellOf(box.x + box.width); cellX += 1) {
|
|
@@ -250,16 +374,45 @@ function remember(box, grid) {
|
|
|
250
374
|
/**
|
|
251
375
|
* Deterministic tidy arrangement for the whole wall.
|
|
252
376
|
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
377
|
+
* The wall is packed against a CONTOUR: the reserved rectangle's own outline,
|
|
378
|
+
* walked outward frame by frame. This is the third packer in this file's
|
|
379
|
+
* history and each replacement fixed a measured failure rather than a taste.
|
|
380
|
+
*
|
|
381
|
+
* 1. A phyllotaxis first-fit spiral had to keep every candidate clear of a
|
|
382
|
+
* card's full bounding circle, so it saturated at roughly a quarter of the
|
|
383
|
+
* available area and the only way to place the next card was to walk further
|
|
384
|
+
* out: fourteen cards spread over ~2700px of paper.
|
|
385
|
+
* 2. Concentric RINGS fixed that density but paid for it with distance. A ring
|
|
386
|
+
* is a circle, and the first ring's radius is the reserve's half-DIAGONAL —
|
|
387
|
+
* so the innermost cards sit at the rectangle's corner distance in every
|
|
388
|
+
* direction, including the four directions where the rectangle is only a
|
|
389
|
+
* card's width away. A fourteen-card wall then filled a 1645x1794 box, which
|
|
390
|
+
* the camera could only frame at 36%: the cards were individually packed
|
|
391
|
+
* tightly but the wall as a whole looked small and far away. That is the
|
|
392
|
+
* complaint this packer answers.
|
|
393
|
+
* 3. A contour hugs the rectangle instead: along the left and right edges a card
|
|
394
|
+
* sits one card-half-width from the reserve, and along the top and bottom one
|
|
395
|
+
* card-half-height from it. Every card is therefore as close to the middle as
|
|
396
|
+
* its own size allows, which is what makes the wall read as one composition.
|
|
397
|
+
*
|
|
398
|
+
* Two further decisions do the rest of the work:
|
|
259
399
|
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
400
|
+
* - **Score by how well the wall FRAMES, not by how free the slot is.** A
|
|
401
|
+
* candidate slot is judged by the zoom the whole wall would need in a
|
|
402
|
+
* reference viewport if the card went there. Filling a slot that widens the
|
|
403
|
+
* wall's box costs zoom, so the packer prefers slots that keep the box close
|
|
404
|
+
* to the window's own aspect — which is why the result is a wide, low cluster
|
|
405
|
+
* instead of a disc that leaves the left and right of the window empty.
|
|
406
|
+
* - **The first frame with room wins.** Walking outward frame by frame and
|
|
407
|
+
* taking the best-scoring slot on the first frame that has any is what makes
|
|
408
|
+
* every card land near the middle: an outer frame is only reached when the
|
|
409
|
+
* inner ones are genuinely full.
|
|
410
|
+
*
|
|
411
|
+
* A collision check still runs on every candidate rather than trusting the
|
|
412
|
+
* height estimate, because the estimate is a line count and a code block's
|
|
413
|
+
* padding is not something a line count can see. The packer never produces an
|
|
414
|
+
* overlap, and the final centring passes repair any card the centring shift
|
|
415
|
+
* nudged into the reserved rectangle.
|
|
263
416
|
*
|
|
264
417
|
* Returns a map from note id to its new layout, for every note that needs to
|
|
265
418
|
* move. Notes already at exactly that layout are omitted by the caller, so
|
|
@@ -269,8 +422,8 @@ export function arrangeLayouts(notes, options = {}) {
|
|
|
269
422
|
const placed = new Map();
|
|
270
423
|
if (notes.length === 0)
|
|
271
424
|
return placed;
|
|
272
|
-
// Decide each card's rotation and footprint up front: the
|
|
273
|
-
// collision test both work on the rotated box.
|
|
425
|
+
// Decide each card's rotation and footprint up front: the contour offsets and
|
|
426
|
+
// the collision test both work on the rotated box.
|
|
274
427
|
const items = notes.map((note) => {
|
|
275
428
|
const width = note.layout?.width ?? previewWidth(note.id);
|
|
276
429
|
const cardWidth = WIDTH_PX[width];
|
|
@@ -279,102 +432,203 @@ export function arrangeLayouts(notes, options = {}) {
|
|
|
279
432
|
const extent = rotatedExtent(cardWidth, cardHeight, rotate);
|
|
280
433
|
return { note, width, cardWidth, cardHeight, rotate, extent };
|
|
281
434
|
});
|
|
282
|
-
const
|
|
283
|
-
//
|
|
284
|
-
//
|
|
285
|
-
//
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const centreRadius = Math.max(0, options.centreRadius ?? CENTRE_RADIUS);
|
|
295
|
-
const startIndex = centreRadius > 0 ? (centreRadius / step) ** 2 : 0;
|
|
435
|
+
const reserve = normalizeReserve(options.centreReserve ?? CENTRE_RESERVE);
|
|
436
|
+
// Largest first: the classic first-fit-decreasing heuristic. Big cards claim
|
|
437
|
+
// the inner frames where there is less room to waste, and small cards fill the
|
|
438
|
+
// gaps they leave. Ties break on id so the result stays deterministic across
|
|
439
|
+
// two windows.
|
|
440
|
+
const ordered = [...items].sort((left, right) => {
|
|
441
|
+
const areaLeft = left.extent.width * left.extent.height;
|
|
442
|
+
const areaRight = right.extent.width * right.extent.height;
|
|
443
|
+
if (areaRight !== areaLeft)
|
|
444
|
+
return areaRight - areaLeft;
|
|
445
|
+
return left.note.id.localeCompare(right.note.id);
|
|
446
|
+
});
|
|
296
447
|
const grid = new Map();
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
448
|
+
// The wall's box so far, extended by each accepted card. The framing score
|
|
449
|
+
// needs it, and it is cheaper to maintain than to recompute per candidate.
|
|
450
|
+
let wall = null;
|
|
451
|
+
const extendWall = (box) => wall === null
|
|
452
|
+
? { x: box.x, y: box.y, width: box.width, height: box.height }
|
|
453
|
+
: {
|
|
454
|
+
x: Math.min(wall.x, box.x),
|
|
455
|
+
y: Math.min(wall.y, box.y),
|
|
456
|
+
width: Math.max(wall.x + wall.width, box.x + box.width) - Math.min(wall.x, box.x),
|
|
457
|
+
height: Math.max(wall.y + wall.height, box.y + box.height) - Math.min(wall.y, box.y),
|
|
458
|
+
};
|
|
459
|
+
/**
|
|
460
|
+
* The best slot for one card, or null when no contour has room.
|
|
461
|
+
*
|
|
462
|
+
* `soft` is used for the repair passes: a repair may not disturb a card that
|
|
463
|
+
* already sits legally, so it is asked to stay a hair further out, which keeps
|
|
464
|
+
* the rounding of a stored position from reading as a fresh violation.
|
|
465
|
+
*/
|
|
466
|
+
const pick = (item, settled, soft, seed) => {
|
|
300
467
|
const { extent } = item;
|
|
301
|
-
let
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
//
|
|
314
|
-
//
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
468
|
+
for (let frame = 0; frame < MAX_FRAMES; frame += 1) {
|
|
469
|
+
const halfWidth = soft.halfWidth + frame * FRAME_STEP;
|
|
470
|
+
// Grow the height FASTER than the width until the contour itself is as wide
|
|
471
|
+
// as the framing rectangle. Growing both equally makes the left and right
|
|
472
|
+
// edges (already the reserve's long ones) longer still, so every extra frame
|
|
473
|
+
// adds room for a vertical column and none for a horizontal row: the wall
|
|
474
|
+
// grew into an 1114x966 block that framed at 69% where a window-shaped one
|
|
475
|
+
// frames at 90%. A contour whose own aspect matches the window puts the
|
|
476
|
+
// spare slots on the short edges, which is where they are useful.
|
|
477
|
+
const halfHeight = Math.max(soft.halfHeight, halfWidth / FRAMING_ASPECT);
|
|
478
|
+
const perimeter = 4 * (halfWidth + halfHeight);
|
|
479
|
+
const slots = Math.min(MAX_SLOTS, Math.max(12, Math.round(perimeter / CONTOUR_STEP)));
|
|
480
|
+
// A deterministic phase per frame, so two frames' slots do not line up and
|
|
481
|
+
// the same wall always packs to the same coordinates.
|
|
482
|
+
const phase = unit(`${seed}:f${frame}`) * perimeter;
|
|
483
|
+
let best = null;
|
|
484
|
+
for (let slot = 0; slot < slots; slot += 1) {
|
|
485
|
+
const candidate = boxOnContour(contourPoint(halfWidth, halfHeight, phase + (slot / slots) * perimeter), extent);
|
|
486
|
+
if (!clearsReserve(candidate, reserve))
|
|
487
|
+
continue;
|
|
488
|
+
if (collides(candidate, settled))
|
|
489
|
+
continue;
|
|
490
|
+
const next = extendWall(candidate);
|
|
491
|
+
// How much this wall would have to shrink to fit the reference viewport.
|
|
492
|
+
// Higher is better; it is the number the camera will actually apply.
|
|
493
|
+
const score = Math.min(1, FRAMING_WIDTH / Math.max(1, next.width), FRAMING_HEIGHT / Math.max(1, next.height));
|
|
494
|
+
// HOW WINDOW-SHAPED the wall becomes, as a log ratio so being too wide
|
|
495
|
+
// and being too tall are penalised equally. This is the tie-break, and it
|
|
496
|
+
// is what actually produces a wide wall: for the first several cards
|
|
497
|
+
// almost every slot frames at the same zoom, because the wall is still
|
|
498
|
+
// smaller than the viewport either way. Without a shape preference the
|
|
499
|
+
// tie fell to placement noise, the wall grew equally in all four
|
|
500
|
+
// directions, and the result was a tall block that framed at 69% where a
|
|
501
|
+
// wide one frames at 90%.
|
|
502
|
+
const shape = Math.abs(Math.log((next.width + 1) / (next.height + 1) / FRAMING_ASPECT));
|
|
503
|
+
// How far this slot sits from the cards already placed. Even distribution
|
|
504
|
+
// is the point of a wall: a slot that hugs one neighbour leaves a hole
|
|
505
|
+
// elsewhere that the NEXT card then has to reach across.
|
|
506
|
+
const clearance = nearestGap(candidate, settled);
|
|
507
|
+
// Nearest-the-origin last, because that is the middle the camera frames.
|
|
508
|
+
const offset = Math.hypot(next.x + next.width / 2, next.y + next.height / 2);
|
|
509
|
+
const better = best === null ||
|
|
510
|
+
score > best.score + 1e-9 ||
|
|
511
|
+
(Math.abs(score - best.score) <= 1e-9 &&
|
|
512
|
+
(shape < best.shape - 1e-9 ||
|
|
513
|
+
(Math.abs(shape - best.shape) <= 1e-9 &&
|
|
514
|
+
(clearance > best.clearance + 1e-9 ||
|
|
515
|
+
(Math.abs(clearance - best.clearance) <= 1e-9 && offset < best.offset)))));
|
|
516
|
+
if (better)
|
|
517
|
+
best = { box: candidate, score, shape, clearance, offset };
|
|
322
518
|
}
|
|
519
|
+
// The first frame with ANY free slot wins. Walking further out would find
|
|
520
|
+
// roomier slots, and taking them is exactly how a wall grows into a sparse
|
|
521
|
+
// scatter instead of a composition.
|
|
522
|
+
if (best !== null)
|
|
523
|
+
return best.box;
|
|
323
524
|
}
|
|
324
|
-
|
|
525
|
+
return null;
|
|
526
|
+
};
|
|
527
|
+
for (const item of ordered) {
|
|
528
|
+
const box = pick(item, grid, reserve, item.note.id) ?? {
|
|
325
529
|
// Unreachable for a wall inside the note cap; the fallback exists so the
|
|
326
530
|
// function still returns a placement for every note instead of throwing.
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
531
|
+
x: -item.extent.width / 2,
|
|
532
|
+
y: -9000,
|
|
533
|
+
width: item.extent.width,
|
|
534
|
+
height: item.extent.height,
|
|
535
|
+
};
|
|
330
536
|
remember(box, grid);
|
|
537
|
+
wall = extendWall(box);
|
|
331
538
|
// The rotated footprint is centred on the card, so the card's own top-left
|
|
332
539
|
// is the footprint centre minus the UNROTATED half size.
|
|
333
540
|
placed.set(item.note.id, clampLayout({
|
|
334
|
-
x: box.x + extent.width / 2 - item.cardWidth / 2,
|
|
335
|
-
y: box.y + extent.height / 2 - item.cardHeight / 2,
|
|
541
|
+
x: box.x + item.extent.width / 2 - item.cardWidth / 2,
|
|
542
|
+
y: box.y + item.extent.height / 2 - item.cardHeight / 2,
|
|
336
543
|
rotate: item.rotate,
|
|
337
544
|
width: item.width,
|
|
338
545
|
}));
|
|
339
546
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
547
|
+
/**
|
|
548
|
+
* Centre the finished wall on the world origin.
|
|
549
|
+
*
|
|
550
|
+
* This is not cosmetic — it is what makes the reserved middle correct. The
|
|
551
|
+
* centrepiece is a SCREEN-space block at the viewport's middle, and the camera
|
|
552
|
+
* frames the wall's BOUNDING BOX centre at that same middle. So the world
|
|
553
|
+
* rectangle the packer keeps clear is only under the field if the wall's box is
|
|
554
|
+
* centred on the origin. Leave the offset in and a tidy wall's capture field
|
|
555
|
+
* drifts off its own reserve by however far the box was off.
|
|
556
|
+
*
|
|
557
|
+
* It alternates the shift with a repair, because a shift can push a card into
|
|
558
|
+
* the reserve: the repair displaces the wall again, so the shift has to be
|
|
559
|
+
* re-measured. A single pass left a 20-card wall ~56px off centre.
|
|
560
|
+
*/
|
|
561
|
+
const footprintOfId = (id, layout) => {
|
|
350
562
|
const note = notes.find((candidate) => candidate.id === id);
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
563
|
+
if (note === undefined)
|
|
564
|
+
return { x: layout.x, y: layout.y, width: 0, height: 0 };
|
|
565
|
+
return footprintOf(note, layout);
|
|
566
|
+
};
|
|
567
|
+
const wallBounds = () => {
|
|
568
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
569
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
570
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
571
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
572
|
+
for (const [id, layout] of placed) {
|
|
573
|
+
const box = footprintOfId(id, layout);
|
|
574
|
+
minX = Math.min(minX, box.x);
|
|
575
|
+
minY = Math.min(minY, box.y);
|
|
576
|
+
maxX = Math.max(maxX, box.x + box.width);
|
|
577
|
+
maxY = Math.max(maxY, box.y + box.height);
|
|
578
|
+
}
|
|
579
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
580
|
+
};
|
|
581
|
+
/** Repair every card that sits in the reserve, one at a time, against the rest. */
|
|
582
|
+
const repairReserve = (soft) => {
|
|
583
|
+
const violators = [...placed].filter(([id, layout]) => !clearsReserve(footprintOfId(id, layout), reserve));
|
|
584
|
+
if (violators.length === 0)
|
|
585
|
+
return;
|
|
586
|
+
const settled = new Map(placed);
|
|
587
|
+
for (const [id] of violators)
|
|
588
|
+
settled.delete(id);
|
|
589
|
+
const repairGrid = new Map();
|
|
590
|
+
for (const [id, layout] of settled)
|
|
591
|
+
remember(footprintOfId(id, layout), repairGrid);
|
|
592
|
+
for (const [id] of violators) {
|
|
593
|
+
const item = items.find((candidate) => candidate.note.id === id);
|
|
594
|
+
if (item === undefined)
|
|
595
|
+
continue;
|
|
596
|
+
const box = pick(item, repairGrid, soft, `${id}:repair`);
|
|
597
|
+
if (box === null)
|
|
598
|
+
continue;
|
|
599
|
+
remember(box, repairGrid);
|
|
600
|
+
placed.set(id, clampLayout({
|
|
601
|
+
x: box.x + item.extent.width / 2 - item.cardWidth / 2,
|
|
602
|
+
y: box.y + item.extent.height / 2 - item.cardHeight / 2,
|
|
603
|
+
rotate: item.rotate,
|
|
604
|
+
width: item.width,
|
|
605
|
+
}));
|
|
377
606
|
}
|
|
607
|
+
};
|
|
608
|
+
// Ask the repair to stay a hair clear, so a card packed exactly on the boundary
|
|
609
|
+
// is not re-placed by rounding noise.
|
|
610
|
+
const softReserve = { halfWidth: reserve.halfWidth + 1.5, halfHeight: reserve.halfHeight + 1.5 };
|
|
611
|
+
for (let pass = 0; pass < 6; pass += 1) {
|
|
612
|
+
const box = wallBounds();
|
|
613
|
+
const shiftX = box.x + box.width / 2;
|
|
614
|
+
const shiftY = box.y + box.height / 2;
|
|
615
|
+
if (Math.abs(shiftX) < 0.5 && Math.abs(shiftY) < 0.5)
|
|
616
|
+
break;
|
|
617
|
+
for (const [id, layout] of placed) {
|
|
618
|
+
placed.set(id, clampLayout({ ...layout, x: layout.x - shiftX, y: layout.y - shiftY }));
|
|
619
|
+
}
|
|
620
|
+
repairReserve(softReserve);
|
|
621
|
+
}
|
|
622
|
+
// The loop above can exit with violators left over: its last repair is itself
|
|
623
|
+
// displaced by the shift it was responding to. Settle them without shifting.
|
|
624
|
+
for (let pass = 0; pass < 24; pass += 1) {
|
|
625
|
+
const before = [...placed].filter(([id, layout]) => !clearsReserve(footprintOfId(id, layout), reserve)).length;
|
|
626
|
+
if (before === 0)
|
|
627
|
+
break;
|
|
628
|
+
repairReserve(softReserve);
|
|
629
|
+
const after = [...placed].filter(([id, layout]) => !clearsReserve(footprintOfId(id, layout), reserve)).length;
|
|
630
|
+
if (after >= before)
|
|
631
|
+
break;
|
|
378
632
|
}
|
|
379
633
|
return placed;
|
|
380
634
|
}
|
|
@@ -384,3 +638,175 @@ export function normalizeWidth(raw) {
|
|
|
384
638
|
const match = WIDTHS.find((width) => width === value);
|
|
385
639
|
return match ?? 'md';
|
|
386
640
|
}
|
|
641
|
+
/**
|
|
642
|
+
* Clamp a reserve into something the packer can actually honour.
|
|
643
|
+
*
|
|
644
|
+
* The Client sends a zoom-corrected reserve, so it can arrive as a huge number
|
|
645
|
+
* (a deeply zoomed-out wall) or as garbage. Both extremes are refused here: a
|
|
646
|
+
* non-finite or negative half-extent becomes zero, and the whole reserve is
|
|
647
|
+
* capped well inside the world bounds so the packer can never be asked to keep
|
|
648
|
+
* a middle clear that leaves no paper for the cards.
|
|
649
|
+
*/
|
|
650
|
+
export function normalizeReserve(raw) {
|
|
651
|
+
const cap = LAYOUT_LIMITS.coord / 4;
|
|
652
|
+
const clampHalf = (value) => {
|
|
653
|
+
const number = Number(value);
|
|
654
|
+
if (!Number.isFinite(number) || number <= 0)
|
|
655
|
+
return 0;
|
|
656
|
+
return Math.min(cap, number);
|
|
657
|
+
};
|
|
658
|
+
return { halfWidth: clampHalf(raw?.halfWidth), halfHeight: clampHalf(raw?.halfHeight) };
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* The nearest free spot for one new card, in world coordinates.
|
|
662
|
+
*
|
|
663
|
+
* Creating a note used to leave its placement to the Host, which stored `null`
|
|
664
|
+
* and let the Client derive a position from the note's index — and that derived
|
|
665
|
+
* position is a spiral that grows outward, so the tenth note could land a
|
|
666
|
+
* screen and a half away from where the user was looking. The card existed, but
|
|
667
|
+
* the user could not see it, which reads exactly like the capture silently
|
|
668
|
+
* failing.
|
|
669
|
+
*
|
|
670
|
+
* This searches outward from the middle instead: the spiral is walked from just
|
|
671
|
+
* outside the centrepiece's reserve, and the first candidate that clears every
|
|
672
|
+
* existing card (plus a gap) and stays out of the reserve is the answer. The
|
|
673
|
+
* result is therefore the gap nearest the centre of the wall, which is both
|
|
674
|
+
* where the user is looking and where the wall has room.
|
|
675
|
+
*
|
|
676
|
+
* Deterministic for the same inputs, so two windows creating at once cannot
|
|
677
|
+
* disagree about the free spot for the same wall.
|
|
678
|
+
*/
|
|
679
|
+
export function findFreeLayout(note, existing, options = {}) {
|
|
680
|
+
const width = note.layout?.width ?? previewWidth(note.id);
|
|
681
|
+
const cardWidth = WIDTH_PX[width];
|
|
682
|
+
const cardHeight = estimateHeight(note, width);
|
|
683
|
+
const rotate = (unit(`${note.id}:ar`) - 0.5) * 2 * TIDY_TILT;
|
|
684
|
+
const extent = rotatedExtent(cardWidth, cardHeight, rotate);
|
|
685
|
+
const reserve = normalizeReserve(options.centreReserve ?? CENTRE_RESERVE);
|
|
686
|
+
const grid = new Map();
|
|
687
|
+
for (const other of existing) {
|
|
688
|
+
// `layoutOf` with index 0 is enough here: a stored layout is used as-is, and
|
|
689
|
+
// a legacy note's derived position only has to be approximately right for
|
|
690
|
+
// the collision check to keep the new card off it.
|
|
691
|
+
const layout = other.layout ?? previewLayout(other, existing.indexOf(other));
|
|
692
|
+
const otherWidth = WIDTH_PX[layout.width];
|
|
693
|
+
const otherHeight = estimateHeight(other, layout.width);
|
|
694
|
+
const otherExtent = rotatedExtent(otherWidth, otherHeight, layout.rotate);
|
|
695
|
+
remember({
|
|
696
|
+
x: layout.x + otherWidth / 2 - otherExtent.width / 2,
|
|
697
|
+
y: layout.y + otherHeight / 2 - otherExtent.height / 2,
|
|
698
|
+
width: otherExtent.width,
|
|
699
|
+
height: otherExtent.height,
|
|
700
|
+
}, grid);
|
|
701
|
+
}
|
|
702
|
+
// Start just outside the reserve so the first candidate is the closest legal
|
|
703
|
+
// ring, then walk the same golden-angle spiral the tidy pass uses. A step near
|
|
704
|
+
// the card's own size keeps the search local: the answer is the first gap, not
|
|
705
|
+
// a far-away hole.
|
|
706
|
+
const step = Math.max(64, Math.sqrt(extent.width * extent.height) * 0.92);
|
|
707
|
+
const startRadius = Math.hypot(reserve.halfWidth, reserve.halfHeight);
|
|
708
|
+
const startIndex = (startRadius / step) ** 2;
|
|
709
|
+
for (let attempt = 0; attempt < MAX_SPOTS; attempt += 1) {
|
|
710
|
+
const index = startIndex + attempt;
|
|
711
|
+
const radius = step * Math.sqrt(index);
|
|
712
|
+
const theta = index * GOLDEN_ANGLE;
|
|
713
|
+
const candidate = {
|
|
714
|
+
x: Math.cos(theta) * radius - extent.width / 2,
|
|
715
|
+
y: Math.sin(theta) * radius - extent.height / 2,
|
|
716
|
+
width: extent.width,
|
|
717
|
+
height: extent.height,
|
|
718
|
+
};
|
|
719
|
+
if (!clearsReserve(candidate, reserve))
|
|
720
|
+
continue;
|
|
721
|
+
if (collides(candidate, grid))
|
|
722
|
+
continue;
|
|
723
|
+
return clampLayout({
|
|
724
|
+
x: candidate.x + extent.width / 2 - cardWidth / 2,
|
|
725
|
+
y: candidate.y + extent.height / 2 - cardHeight / 2,
|
|
726
|
+
rotate,
|
|
727
|
+
width,
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
// A wall this full is already at the note cap, so this is a safety net rather
|
|
731
|
+
// than a real path: place just outside the reserve on the first ring.
|
|
732
|
+
const fallbackRadius = startRadius + Math.max(extent.width, extent.height);
|
|
733
|
+
return clampLayout({
|
|
734
|
+
x: -cardWidth / 2,
|
|
735
|
+
y: -fallbackRadius - cardHeight / 2,
|
|
736
|
+
rotate,
|
|
737
|
+
width,
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* The footprint one note occupies on the wall, as an axis-aligned box.
|
|
742
|
+
*
|
|
743
|
+
* Exposed because the Client needs it for two things the packer already knows:
|
|
744
|
+
* testing whether a card is inside the current viewport, and drawing the reserve
|
|
745
|
+
* it must not cover.
|
|
746
|
+
*/
|
|
747
|
+
export function noteBox(note, index) {
|
|
748
|
+
return footprintOf(note, layoutOf(note, index));
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* The footprint a note occupies at a GIVEN layout.
|
|
752
|
+
*
|
|
753
|
+
* Split out from {@link noteBox} because the useful question is often about a
|
|
754
|
+
* layout that is not stored yet — a planned tidy position, or a candidate spot
|
|
755
|
+
* for a card that does not exist. `noteBox` answers "where is this note now";
|
|
756
|
+
* this answers "where would it be".
|
|
757
|
+
*/
|
|
758
|
+
/**
|
|
759
|
+
* Nudge a layout out of the centrepiece's reserved rectangle.
|
|
760
|
+
*
|
|
761
|
+
* The reserve is not only a packing rule: a user can drag a card straight over
|
|
762
|
+
* the quick-capture field, and a card parked there hides the wall's own controls
|
|
763
|
+
* behind itself. This returns the smallest translation that puts the card's
|
|
764
|
+
* footprint clear of the rectangle, which makes the reserve behave like a
|
|
765
|
+
* magnetic edge during a drag rather than a rule that is only enforced on tidy.
|
|
766
|
+
*/
|
|
767
|
+
/**
|
|
768
|
+
* Every way out of the reserve, shortest first.
|
|
769
|
+
*
|
|
770
|
+
* Exposed separately from {@link pushOutOfReserve} because the shortest escape is
|
|
771
|
+
* not always usable — it can land on a neighbour — so a caller that cares about
|
|
772
|
+
* collisions needs the whole ordered list, not just the first answer.
|
|
773
|
+
*/
|
|
774
|
+
export function escapesFromReserve(note, layout, reserve = CENTRE_RESERVE) {
|
|
775
|
+
const box = footprintOf(note, layout);
|
|
776
|
+
const moves = [
|
|
777
|
+
{ dx: -reserve.halfWidth - (box.x + box.width), dy: 0 },
|
|
778
|
+
{ dx: reserve.halfWidth - box.x, dy: 0 },
|
|
779
|
+
{ dx: 0, dy: -reserve.halfHeight - (box.y + box.height) },
|
|
780
|
+
{ dx: 0, dy: reserve.halfHeight - box.y },
|
|
781
|
+
];
|
|
782
|
+
return moves
|
|
783
|
+
.sort((left, right) => (Math.abs(left.dx) + Math.abs(left.dy)) - (Math.abs(right.dx) + Math.abs(right.dy)))
|
|
784
|
+
.map((move) => clampLayout({ ...layout, x: layout.x + move.dx, y: layout.y + move.dy }));
|
|
785
|
+
}
|
|
786
|
+
export function pushOutOfReserve(note, layout, reserve = CENTRE_RESERVE) {
|
|
787
|
+
const box = footprintOf(note, layout);
|
|
788
|
+
if (!intersectsReserve(box, reserve))
|
|
789
|
+
return layout;
|
|
790
|
+
// Four ways out, one per edge; the shortest wins so the card slides to the
|
|
791
|
+
// nearest side instead of jumping around the rectangle.
|
|
792
|
+
const moves = [
|
|
793
|
+
{ dx: -reserve.halfWidth - (box.x + box.width), dy: 0 },
|
|
794
|
+
{ dx: reserve.halfWidth - box.x, dy: 0 },
|
|
795
|
+
{ dx: 0, dy: -reserve.halfHeight - (box.y + box.height) },
|
|
796
|
+
{ dx: 0, dy: reserve.halfHeight - box.y },
|
|
797
|
+
];
|
|
798
|
+
let best = moves[0];
|
|
799
|
+
for (const move of moves) {
|
|
800
|
+
if (Math.abs(move.dx) + Math.abs(move.dy) < Math.abs(best.dx) + Math.abs(best.dy))
|
|
801
|
+
best = move;
|
|
802
|
+
}
|
|
803
|
+
return clampLayout({ ...layout, x: layout.x + best.dx, y: layout.y + best.dy });
|
|
804
|
+
}
|
|
805
|
+
export function footprintOf(note, layout) {
|
|
806
|
+
const cardWidth = WIDTH_PX[layout.width];
|
|
807
|
+
const cardHeight = estimateHeight(note, layout.width);
|
|
808
|
+
const extent = rotatedExtent(cardWidth, cardHeight, layout.rotate);
|
|
809
|
+
const centreX = layout.x + cardWidth / 2;
|
|
810
|
+
const centreY = layout.y + cardHeight / 2;
|
|
811
|
+
return { x: centreX - extent.width / 2, y: centreY - extent.height / 2, width: extent.width, height: extent.height };
|
|
812
|
+
}
|