@johnmorrisdotca/fuda-cards 0.9.2 → 0.11.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.
Files changed (98) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE.md +14 -0
  3. package/README.md +466 -60
  4. package/dist/binder.d.ts +4 -6
  5. package/dist/binder.js +32 -23
  6. package/dist/card-markup.d.ts +2 -0
  7. package/dist/card-markup.js +59 -0
  8. package/dist/card-options.d.ts +131 -0
  9. package/dist/card-options.js +48 -0
  10. package/dist/card-styles.d.ts +2 -0
  11. package/dist/card-styles.js +314 -0
  12. package/dist/choice.d.ts +1 -1
  13. package/dist/choice.js +27 -23
  14. package/dist/collection-address.d.ts +8 -0
  15. package/dist/collection-address.js +52 -0
  16. package/dist/collection-binder-view.d.ts +9 -0
  17. package/dist/collection-binder-view.js +123 -0
  18. package/dist/collection-controls.d.ts +2 -0
  19. package/dist/collection-controls.js +143 -0
  20. package/dist/collection-deck.d.ts +11 -0
  21. package/dist/collection-deck.js +165 -0
  22. package/dist/collection-dialog.d.ts +2 -0
  23. package/dist/collection-dialog.js +28 -0
  24. package/dist/collection-element.d.ts +41 -63
  25. package/dist/collection-element.js +288 -472
  26. package/dist/collection-host.d.ts +5 -0
  27. package/dist/collection-host.js +1 -0
  28. package/dist/collection-icons.d.ts +3 -0
  29. package/dist/collection-icons.js +19 -0
  30. package/dist/collection-motion.d.ts +11 -0
  31. package/dist/collection-motion.js +72 -0
  32. package/dist/collection-styles.d.ts +4 -0
  33. package/dist/collection-styles.js +336 -0
  34. package/dist/collection-views.d.ts +8 -0
  35. package/dist/collection-views.js +8 -0
  36. package/dist/collection.d.ts +140 -11
  37. package/dist/collection.js +139 -33
  38. package/dist/element-define.d.ts +2 -0
  39. package/dist/element-define.js +3 -0
  40. package/dist/element.d.ts +20 -1
  41. package/dist/element.js +289 -98
  42. package/dist/index.d.ts +9 -52
  43. package/dist/index.js +5 -5
  44. package/dist/live-3d.d.ts +126 -0
  45. package/dist/live-3d.js +229 -0
  46. package/dist/materials.d.ts +117 -6
  47. package/dist/materials.js +191 -28
  48. package/dist/motion.d.ts +2 -0
  49. package/dist/motion.js +4 -0
  50. package/dist/pile-card.js +138 -91
  51. package/dist/pile.d.ts +61 -8
  52. package/dist/pile.js +89 -29
  53. package/dist/stack.d.ts +42 -6
  54. package/dist/stack.js +28 -3
  55. package/dist/version.d.ts +11 -0
  56. package/dist/version.js +11 -0
  57. package/dist/wear.d.ts +147 -9
  58. package/dist/wear.js +273 -89
  59. package/dist/zoom.d.ts +1 -1
  60. package/dist/zoom.js +73 -42
  61. package/docs/API.md +783 -0
  62. package/docs/collection-displays.md +1 -1
  63. package/docs/guides/accessibility-and-theming.md +62 -0
  64. package/docs/guides/architecture.md +53 -0
  65. package/docs/guides/binder-and-piles.md +83 -0
  66. package/docs/guides/bundlers.md +60 -0
  67. package/docs/guides/card-element.md +65 -0
  68. package/docs/guides/collections.md +86 -0
  69. package/docs/guides/embed-in-an-iframe.md +39 -0
  70. package/docs/guides/frameworks.md +132 -0
  71. package/docs/guides/getting-started.md +96 -0
  72. package/docs/guides/html-and-cdn.md +69 -0
  73. package/docs/guides/ja/accessibility-and-theming.md +62 -0
  74. package/docs/guides/ja/architecture.md +53 -0
  75. package/docs/guides/ja/binder-and-piles.md +83 -0
  76. package/docs/guides/ja/bundlers.md +60 -0
  77. package/docs/guides/ja/card-element.md +65 -0
  78. package/docs/guides/ja/collections.md +86 -0
  79. package/docs/guides/ja/embed-in-an-iframe.md +39 -0
  80. package/docs/guides/ja/frameworks.md +132 -0
  81. package/docs/guides/ja/getting-started.md +96 -0
  82. package/docs/guides/ja/html-and-cdn.md +69 -0
  83. package/docs/guides/ja/live-3d.md +75 -0
  84. package/docs/guides/ja/materials-and-stacks.md +73 -0
  85. package/docs/guides/ja/motion-and-spin.md +73 -0
  86. package/docs/guides/ja/troubleshooting.md +60 -0
  87. package/docs/guides/ja/typescript-and-node.md +45 -0
  88. package/docs/guides/ja/upgrading.md +56 -0
  89. package/docs/guides/ja/wear.md +63 -0
  90. package/docs/guides/live-3d.md +75 -0
  91. package/docs/guides/materials-and-stacks.md +73 -0
  92. package/docs/guides/motion-and-spin.md +73 -0
  93. package/docs/guides/troubleshooting.md +60 -0
  94. package/docs/guides/typescript-and-node.md +45 -0
  95. package/docs/guides/upgrading.md +56 -0
  96. package/docs/guides/wear.md +63 -0
  97. package/docs/live-3d.md +59 -0
  98. package/package.json +81 -11
package/dist/pile.d.ts CHANGED
@@ -2,15 +2,56 @@
2
2
  * How a card taken off a pile lands on the pile at its side: still facing up, or turned over sideways (as a person turns a card, like a
3
3
  * page) to show its back. `upside-down` was a 0.6.0 value that spun the picture half way round in its plane; it is still accepted and
4
4
  * means `face-down`, so nothing breaks. A card never ends with its picture on its head.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { normalizeLanding, type DeckLanding } from "@johnmorrisdotca/fuda-cards/collection";
9
+ *
10
+ * const old: DeckLanding = "upside-down";
11
+ * console.log(normalizeLanding(old)); // face-down
12
+ * ```
13
+ */
14
+ export type DeckLanding = "face-up" | "face-down" | "upside-down";
15
+ /**
16
+ * The two ways a card can land, with the label on each button: `face-up` (still facing up) and `face-down` (turned over sideways on the way).
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { deckLandings } from "@johnmorrisdotca/fuda-cards/collection";
21
+ *
22
+ * console.log(deckLandings.map((landing) => landing.id)); // [ 'face-up', 'face-down' ]
23
+ * ```
5
24
  */
6
- export type DeckLanding = 'face-up' | 'face-down' | 'upside-down';
7
25
  export declare const deckLandings: ReadonlyArray<{
8
- id: 'face-up' | 'face-down';
26
+ id: "face-up" | "face-down";
9
27
  label: string;
10
28
  }>;
11
- /** The landing a value means: `face-down` (also `upside-down` and `turned-over`) is turned over; anything else is still facing up. */
12
- export declare function normalizeLanding(value: string | null | undefined): 'face-up' | 'face-down';
13
- /** Where the cards of a fan lie, first card on the left when open and on top when closed. `open` is the fan spread like a hand of playing cards; `closed` is the squared-up deck it starts from and closes back to. */
29
+ /**
30
+ * The landing a value means: `face-down` (also `upside-down` and `turned-over`) is turned over; anything else is still facing up.
31
+ *
32
+ * @param value - The `deck-land` attribute's value, or nothing.
33
+ * @returns `face-down` or `face-up`.
34
+ * @example
35
+ * ```ts
36
+ * import { normalizeLanding } from "@johnmorrisdotca/fuda-cards/collection";
37
+ *
38
+ * console.log(normalizeLanding("turned-over"), normalizeLanding(null), normalizeLanding("sideways")); // face-down face-up face-up
39
+ * ```
40
+ */
41
+ export declare function normalizeLanding(value: string | null | undefined): "face-up" | "face-down";
42
+ /**
43
+ * Where one card of a fan lies, first card on the left when open and on top when closed. `angle`, `x` and `y` are the open fan, spread like a hand
44
+ * of playing cards (turn in degrees, across in card widths, down in card widths); the `closed` ones are the squared-up deck it starts from and
45
+ * closes back to.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * import { fanLayout, type FanPlace } from "@johnmorrisdotca/fuda-cards/collection";
50
+ *
51
+ * const first: FanPlace = fanLayout(5)[0];
52
+ * console.log(first.angle); // -22
53
+ * ```
54
+ */
14
55
  export interface FanPlace {
15
56
  angle: number;
16
57
  x: number;
@@ -19,7 +60,20 @@ export interface FanPlace {
19
60
  closedX: number;
20
61
  closedY: number;
21
62
  }
22
- /** `step` is how far apart neighbouring cards lie across, in card widths; the angle does the rest of the spreading. A phone uses a smaller one. */
63
+ /**
64
+ * Where the cards of a fan lie: each card turns eleven degrees more than the one before, up to 110 degrees from the first to the last, centred on the pile.
65
+ *
66
+ * @param count - How many cards; none gives an empty fan.
67
+ * @param seed - The seed that nudges the squared-up deck the fan closes to. Unless said, 1.
68
+ * @param step - How far apart neighbouring cards lie across, in card widths; the angle does the rest of the spreading. A phone uses a smaller one. Unless said, 0.34.
69
+ * @returns The places of the cards, first card first.
70
+ * @example
71
+ * ```ts
72
+ * import { fanLayout } from "@johnmorrisdotca/fuda-cards/collection";
73
+ *
74
+ * console.log(fanLayout(3).map((place) => place.angle)); // [ -11, 0, 11 ]
75
+ * ```
76
+ */
23
77
  export declare function fanLayout(count: number, seed?: number, step?: number): FanPlace[];
24
78
  /** The card's height over its width, and where a fan turns: 165% of a card's height down from its top. The CSS uses the same two numbers. */
25
79
  export declare const CARD_RATIO: number;
@@ -56,7 +110,6 @@ export interface Timing {
56
110
  export declare const SPEEDS: Record<string, number>;
57
111
  /** The base length of a move, 600 ms; `speed` stretches or shortens every move at once. */
58
112
  export declare const BASE_MS = 600;
59
- export declare function reducedMotion(): boolean;
60
113
  /**
61
114
  * `animate="off"` turns every move off; `animate="on"` turns them on; with no `animate` a move is on only where `dflt` says (the pieces that
62
115
  * already moved before the option existed). The reader's reduced-motion setting always wins.
@@ -75,4 +128,4 @@ export declare function flight(fly: HTMLElement, turn: HTMLElement, from: {
75
128
  x: number;
76
129
  y: number;
77
130
  z?: number;
78
- }, land: 'face-up' | 'face-down', direction: 'off' | 'back', ms: number, spin?: number): Animation[];
131
+ }, land: "face-up" | "face-down", direction: "off" | "back", ms: number, spin?: number): Animation[];
package/dist/pile.js CHANGED
@@ -4,20 +4,57 @@
4
4
  * `<fuda-collection>` (the Overlapping Deck and the other displays that draw a pile) and `<fuda-card stack>` both use this one
5
5
  * piece, so a pile fans open and closed, and a card comes off it and goes back, the same way everywhere.
6
6
  */
7
- import { stackLayout } from './stack.js';
7
+ import { reducedMotion } from "./motion.js";
8
+ import { stackLayout } from "./stack.js";
9
+ /**
10
+ * The two ways a card can land, with the label on each button: `face-up` (still facing up) and `face-down` (turned over sideways on the way).
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { deckLandings } from "@johnmorrisdotca/fuda-cards/collection";
15
+ *
16
+ * console.log(deckLandings.map((landing) => landing.id)); // [ 'face-up', 'face-down' ]
17
+ * ```
18
+ */
8
19
  export const deckLandings = [
9
- { id: 'face-up', label: 'Still facing up' }, { id: 'face-down', label: 'Turned over' }
20
+ { id: "face-up", label: "Still facing up" },
21
+ { id: "face-down", label: "Turned over" },
10
22
  ];
11
- /** The landing a value means: `face-down` (also `upside-down` and `turned-over`) is turned over; anything else is still facing up. */
23
+ /**
24
+ * The landing a value means: `face-down` (also `upside-down` and `turned-over`) is turned over; anything else is still facing up.
25
+ *
26
+ * @param value - The `deck-land` attribute's value, or nothing.
27
+ * @returns `face-down` or `face-up`.
28
+ * @example
29
+ * ```ts
30
+ * import { normalizeLanding } from "@johnmorrisdotca/fuda-cards/collection";
31
+ *
32
+ * console.log(normalizeLanding("turned-over"), normalizeLanding(null), normalizeLanding("sideways")); // face-down face-up face-up
33
+ * ```
34
+ */
12
35
  export function normalizeLanding(value) {
13
- return value === 'face-down' || value === 'upside-down' || value === 'turned-over' ? 'face-down' : 'face-up';
36
+ return value === "face-down" || value === "upside-down" || value === "turned-over" ? "face-down" : "face-up";
14
37
  }
15
- /** `step` is how far apart neighbouring cards lie across, in card widths; the angle does the rest of the spreading. A phone uses a smaller one. */
38
+ /**
39
+ * Where the cards of a fan lie: each card turns eleven degrees more than the one before, up to 110 degrees from the first to the last, centred on the pile.
40
+ *
41
+ * @param count - How many cards; none gives an empty fan.
42
+ * @param seed - The seed that nudges the squared-up deck the fan closes to. Unless said, 1.
43
+ * @param step - How far apart neighbouring cards lie across, in card widths; the angle does the rest of the spreading. A phone uses a smaller one. Unless said, 0.34.
44
+ * @returns The places of the cards, first card first.
45
+ * @example
46
+ * ```ts
47
+ * import { fanLayout } from "@johnmorrisdotca/fuda-cards/collection";
48
+ *
49
+ * console.log(fanLayout(3).map((place) => place.angle)); // [ -11, 0, 11 ]
50
+ * ```
51
+ */
16
52
  export function fanLayout(count, seed = 1, step = 0.34) {
17
53
  const n = Math.max(0, Math.floor(count));
18
54
  if (!n)
19
55
  return [];
20
- const spread = Math.min(110, 11 * (n - 1)), turn = n > 1 ? spread / (n - 1) : 0;
56
+ const spread = Math.min(110, 11 * (n - 1));
57
+ const turn = n > 1 ? spread / (n - 1) : 0;
21
58
  const nudge = stackLayout(n, { messiness: 0.25, seed, depth: n });
22
59
  return Array.from({ length: n }, (_, i) => {
23
60
  const under = i >= 1 ? nudge[i - 1] : undefined;
@@ -27,7 +64,7 @@ export function fanLayout(count, seed = 1, step = 0.34) {
27
64
  y: 0,
28
65
  closedAngle: under ? under.rotate / 3 : 0,
29
66
  closedX: under ? under.x : 0,
30
- closedY: under ? under.y : 0
67
+ closedY: under ? under.y : 0,
31
68
  };
32
69
  });
33
70
  }
@@ -39,20 +76,33 @@ export const FAN_PIVOT = 1.65;
39
76
  * `bottom` past its top and bottom (never negative). Pages use it to leave room and to fit the view.
40
77
  */
41
78
  export function fanBounds(places) {
42
- let x0 = 0, x1 = 1, y0 = 0, y1 = CARD_RATIO;
43
- const px = 0.5, py = FAN_PIVOT * CARD_RATIO;
79
+ let x0 = 0;
80
+ let x1 = 1;
81
+ let y0 = 0;
82
+ let y1 = CARD_RATIO;
83
+ const px = 0.5;
84
+ const py = FAN_PIVOT * CARD_RATIO;
44
85
  for (const p of places) {
45
- const a = (p.angle * Math.PI) / 180, c = Math.cos(a), s = Math.sin(a);
46
- for (const [cx, cy] of [[0, 0], [1, 0], [0, CARD_RATIO], [1, CARD_RATIO]]) {
47
- const dx = cx - px, dy = cy - py;
48
- const x = px + dx * c - dy * s + p.x, y = py + dx * s + dy * c;
86
+ const a = (p.angle * Math.PI) / 180;
87
+ const c = Math.cos(a);
88
+ const s = Math.sin(a);
89
+ for (const [cx, cy] of [
90
+ [0, 0],
91
+ [1, 0],
92
+ [0, CARD_RATIO],
93
+ [1, CARD_RATIO],
94
+ ]) {
95
+ const dx = cx - px;
96
+ const dy = cy - py;
97
+ const x = px + dx * c - dy * s + p.x;
98
+ const y = py + dx * s + dy * c;
49
99
  x0 = Math.min(x0, x);
50
100
  x1 = Math.max(x1, x);
51
101
  y0 = Math.min(y0, y);
52
102
  y1 = Math.max(y1, y);
53
103
  }
54
104
  }
55
- const r = (v) => Math.round(v * 1000) / 1000;
105
+ const r = (v) => Math.round(v * 1000) / 1000 || 0;
56
106
  return { left: r(-x0), right: r(x1 - 1), top: r(-y0), bottom: r(y1 - CARD_RATIO) };
57
107
  }
58
108
  /**
@@ -62,8 +112,10 @@ export function fanBounds(places) {
62
112
  export function fanRelative(places) {
63
113
  if (!places.length)
64
114
  return [];
65
- const a0 = (places[0].angle * Math.PI) / 180, c = Math.cos(a0), s = Math.sin(a0);
66
- const r = (v) => Math.round(v * 1000) / 1000;
115
+ const a0 = (places[0].angle * Math.PI) / 180;
116
+ const c = Math.cos(a0);
117
+ const s = Math.sin(a0);
118
+ const r = (v) => Math.round(v * 1000) / 1000 || 0;
67
119
  return places.map((p) => {
68
120
  const dx = p.x - places[0].x;
69
121
  return { angle: r(p.angle - places[0].angle), x: r(dx * c), y: r(-dx * s) };
@@ -71,23 +123,21 @@ export function fanRelative(places) {
71
123
  }
72
124
  /** The fan's own centre of gravity: where a card of an open fan has moved from its squared-up place, in card widths (before the pile's own turn). */
73
125
  export function fanShift(place) {
74
- const a = (place.angle * Math.PI) / 180, lever = (FAN_PIVOT - 0.5) * CARD_RATIO;
126
+ const a = (place.angle * Math.PI) / 180;
127
+ const lever = (FAN_PIVOT - 0.5) * CARD_RATIO;
75
128
  return { x: place.x + lever * Math.sin(a), y: lever * (1 - Math.cos(a)) };
76
129
  }
77
130
  export const SPEEDS = { slow: 1.6, normal: 1, quick: 0.6 };
78
131
  /** The base length of a move, 600 ms; `speed` stretches or shortens every move at once. */
79
132
  export const BASE_MS = 600;
80
- export function reducedMotion() {
81
- return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
82
- }
83
133
  /**
84
134
  * `animate="off"` turns every move off; `animate="on"` turns them on; with no `animate` a move is on only where `dflt` says (the pieces that
85
135
  * already moved before the option existed). The reader's reduced-motion setting always wins.
86
136
  */
87
137
  export function timing(host, dflt) {
88
- const a = host.getAttribute('animate');
89
- const asked = a === 'off' ? false : a === 'on' ? true : dflt;
90
- const scale = SPEEDS[host.getAttribute('speed') ?? 'normal'] ?? 1;
138
+ const a = host.getAttribute("animate");
139
+ const asked = a === "off" ? false : a === "on" ? true : dflt;
140
+ const scale = SPEEDS[host.getAttribute("speed") ?? "normal"] ?? 1;
91
141
  return { on: asked && !reducedMotion(), ms: Math.round(BASE_MS * scale) };
92
142
  }
93
143
  /**
@@ -100,18 +150,28 @@ export function timing(host, dflt) {
100
150
  * sine of the turn, which is exactly what keeps its lowest corner above the cards it passes. It is level again, and down on the pile, at the end.
101
151
  */
102
152
  export function flight(fly, turn, from, land, direction, ms, spin = 0) {
103
- const options = { duration: ms, easing: 'cubic-bezier(.3,.1,.2,1)' };
104
- const turns = land === 'face-down', half = (fly.offsetWidth || 200) / 2, z0 = from.z ?? 0, STEPS = 16, MARGIN = 1.5;
105
- const fly3d = [], turn3d = [];
153
+ const options = { duration: ms, easing: "cubic-bezier(.3,.1,.2,1)" };
154
+ const turns = land === "face-down";
155
+ const half = (fly.offsetWidth || 200) / 2;
156
+ const z0 = from.z ?? 0;
157
+ const STEPS = 16;
158
+ const MARGIN = 1.5;
159
+ const fly3d = [];
160
+ const turn3d = [];
106
161
  for (let k = 0; k <= STEPS; k++) {
107
- const p = k / STEPS, q = 1 - p, arc = Math.sin(Math.PI * p);
162
+ const p = k / STEPS;
163
+ const q = 1 - p;
164
+ const arc = Math.sin(Math.PI * p);
108
165
  // Over the pile it leaves the card stays as high as that pile until half way, then comes down to where it lands.
109
166
  const base = Math.max(0, z0 * Math.min(1, 2 * q)) + MARGIN * arc;
110
167
  const lift = (turns ? half + 6 : 80) * arc;
111
168
  const z = k === STEPS ? 0 : base + lift;
112
- fly3d.push({ offset: p, transform: `translate3d(${from.x * q}px,${from.y * q - 136 * p * q}px,${z}px) rotate(${spin * q}deg) scale(${1 + 0.07 * arc})` });
169
+ fly3d.push({
170
+ offset: p,
171
+ transform: `translate3d(${from.x * q}px,${from.y * q - 136 * p * q}px,${z}px) rotate(${spin * q}deg) scale(${1 + 0.07 * arc})`,
172
+ });
113
173
  if (turns)
114
- turn3d.push({ offset: p, transform: `rotateY(${(direction === 'off' ? p : q) * 180}deg)` });
174
+ turn3d.push({ offset: p, transform: `rotateY(${(direction === "off" ? p : q) * 180}deg)` });
115
175
  }
116
176
  const out = [fly.animate(fly3d, options)];
117
177
  if (turns)
package/dist/stack.d.ts CHANGED
@@ -1,9 +1,13 @@
1
1
  /**
2
- * Where the cards of a stack lie, from neat to messy, the same every time for the same seed.
2
+ * Where one card of a stack lies, as the stack's layout says: how far it is nudged across and down, in card widths, and how far it is turned.
3
3
  *
4
- * The idea and the three parameters (messiness, seed, depth) come from toranpu's pileLayout
5
- * (github.com/johnmorrisdotca/toranpu, `src/ui/layout.ts`, MIT). This is written again here for a stack that has a true
6
- * thickness: a card's height is not faked by moving it down the page, so only the nudge and the turn are laid out.
4
+ * @example
5
+ * ```ts
6
+ * import { stackLayout, type StackPlace } from "@johnmorrisdotca/fuda-cards";
7
+ *
8
+ * const place: StackPlace = stackLayout(5, { seed: 3 })[0];
9
+ * console.log(Object.keys(place)); // [ 'x', 'y', 'rotate' ]
10
+ * ```
7
11
  */
8
12
  export interface StackPlace {
9
13
  /** Across, in card widths from where a square card lies. */
@@ -13,6 +17,17 @@ export interface StackPlace {
13
17
  /** Turn in degrees. */
14
18
  rotate: number;
15
19
  }
20
+ /**
21
+ * How a stack is laid out: how messy, from which seed and how many cards deep.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { stackLayout, type StackLayoutOptions } from "@johnmorrisdotca/fuda-cards";
26
+ *
27
+ * const messy: StackLayoutOptions = { messiness: 0.8, seed: 7, depth: 5 };
28
+ * console.log(stackLayout(52, messy).length); // 5
29
+ * ```
30
+ */
16
31
  export interface StackLayoutOptions {
17
32
  /** From 0 (squared up) to 1 (very messy). Unless said, 0.3. */
18
33
  messiness?: number;
@@ -24,7 +39,28 @@ export interface StackLayoutOptions {
24
39
  /** A stream of numbers in [0, 1) fixed by a seed (mulberry32). */
25
40
  export declare function seededRandom(seed: number): () => number;
26
41
  /**
27
- * The cards under the top one of a stack of `count`, bottom first: the top `depth` of them, as the top card is the live one
28
- * and lies square. A card's place depends on its number from the bottom, so a card put on top moves none of those under it.
42
+ * Where the cards of a stack lie, from neat to messy, the same every time for the same seed: the cards under the top one, bottom first, at most
43
+ * `depth` of them, as the top card is the live one and lies square. A card's place depends on its number from the bottom, so a card put
44
+ * on top moves none of those under it.
45
+ *
46
+ * The idea and the three parameters (messiness, seed, depth) come from toranpu's pileLayout (github.com/johnmorrisdotca/toranpu,
47
+ * `src/ui/layout.ts`, MIT). This is written again here for a stack that has a true thickness: a card's height is not faked by moving it
48
+ * down the page, so only the nudge and the turn are laid out.
49
+ *
50
+ * @param count - How many cards the stack holds; with none or one there is nothing under the top.
51
+ * @param options - How messy, which seed and how deep. Unless said: 0.3, 1 and 10.
52
+ * @returns The places of the cards under the top one, bottom first; empty for a stack of one.
53
+ * @example
54
+ * ```ts
55
+ * import { stackLayout } from "@johnmorrisdotca/fuda-cards";
56
+ *
57
+ * const same = JSON.stringify(stackLayout(52, { seed: 7 })) === JSON.stringify(stackLayout(52, { seed: 7 }));
58
+ * console.log(same, stackLayout(1).length, stackLayout(52).length); // true 0 10
59
+ * ```
29
60
  */
30
61
  export declare function stackLayout(count: number, options?: StackLayoutOptions): StackPlace[];
62
+ /**
63
+ * The markup of the cards under the top one, bottom first, for a stack of `count` cards: each is the frame of the top card with
64
+ * nothing written on it, laid where `places` says and as many cards lower as its number from the bottom.
65
+ */
66
+ export declare function stillsHtml(places: StackPlace[], count: number): string;
package/dist/stack.js CHANGED
@@ -15,8 +15,24 @@ const round = (value) => {
15
15
  return Object.is(kept, -0) ? 0 : kept;
16
16
  };
17
17
  /**
18
- * The cards under the top one of a stack of `count`, bottom first: the top `depth` of them, as the top card is the live one
19
- * and lies square. A card's place depends on its number from the bottom, so a card put on top moves none of those under it.
18
+ * Where the cards of a stack lie, from neat to messy, the same every time for the same seed: the cards under the top one, bottom first, at most
19
+ * `depth` of them, as the top card is the live one and lies square. A card's place depends on its number from the bottom, so a card put
20
+ * on top moves none of those under it.
21
+ *
22
+ * The idea and the three parameters (messiness, seed, depth) come from toranpu's pileLayout (github.com/johnmorrisdotca/toranpu,
23
+ * `src/ui/layout.ts`, MIT). This is written again here for a stack that has a true thickness: a card's height is not faked by moving it
24
+ * down the page, so only the nudge and the turn are laid out.
25
+ *
26
+ * @param count - How many cards the stack holds; with none or one there is nothing under the top.
27
+ * @param options - How messy, which seed and how deep. Unless said: 0.3, 1 and 10.
28
+ * @returns The places of the cards under the top one, bottom first; empty for a stack of one.
29
+ * @example
30
+ * ```ts
31
+ * import { stackLayout } from "@johnmorrisdotca/fuda-cards";
32
+ *
33
+ * const same = JSON.stringify(stackLayout(52, { seed: 7 })) === JSON.stringify(stackLayout(52, { seed: 7 }));
34
+ * console.log(same, stackLayout(1).length, stackLayout(52).length); // true 0 10
35
+ * ```
20
36
  */
21
37
  export function stackLayout(count, options = {}) {
22
38
  const whole = Math.max(0, Math.floor(Number.isFinite(count) ? count : 0));
@@ -32,8 +48,17 @@ export function stackLayout(count, options = {}) {
32
48
  places.push({
33
49
  x: round((random() * 2 - 1) * nudge),
34
50
  y: round((random() * 2 - 1) * nudge * 0.8),
35
- rotate: round((random() * 2 - 1) * 14 * mess ** 1.3)
51
+ rotate: round((random() * 2 - 1) * 14 * mess ** 1.3),
36
52
  });
37
53
  }
38
54
  return places;
39
55
  }
56
+ /**
57
+ * The markup of the cards under the top one, bottom first, for a stack of `count` cards: each is the frame of the top card with
58
+ * nothing written on it, laid where `places` says and as many cards lower as its number from the bottom.
59
+ */
60
+ export function stillsHtml(places, count) {
61
+ return places
62
+ .map((place, at) => `<div class="still" style="--x:${place.x};--y:${place.y};--r:${place.rotate};--k:${count - places.length + at}"><div class="inner"></div><div class="shine"></div></div>`)
63
+ .join("");
64
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The version of this package, the same as `package.json`'s (a test holds the two together).
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { VERSION } from "@johnmorrisdotca/fuda-cards";
7
+ *
8
+ * console.log(/^\d+\.\d+\.\d+$/.test(VERSION)); // true
9
+ * ```
10
+ */
11
+ export declare const VERSION = "0.11.0";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The version of this package, the same as `package.json`'s (a test holds the two together).
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { VERSION } from "@johnmorrisdotca/fuda-cards";
7
+ *
8
+ * console.log(/^\d+\.\d+\.\d+$/.test(VERSION)); // true
9
+ * ```
10
+ */
11
+ export const VERSION = "0.11.0";
package/dist/wear.d.ts CHANGED
@@ -1,4 +1,14 @@
1
- /** How much of each kind of wear, from 0 (none) to 1 (the worst the preset names). */
1
+ /**
2
+ * How much of each kind of wear, from 0 (none) to 1 (the worst the preset names).
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { wearPreset, type WearLevels } from "@johnmorrisdotca/fuda-cards";
7
+ *
8
+ * const levels: WearLevels | undefined = wearPreset("near-mint")?.levels;
9
+ * console.log(levels?.corners, levels?.creases); // 0.18 0
10
+ * ```
11
+ */
2
12
  export interface WearLevels {
3
13
  corners: number;
4
14
  edges: number;
@@ -9,6 +19,17 @@ export interface WearLevels {
9
19
  /** How far the print sits off centre. */
10
20
  centering: number;
11
21
  }
22
+ /**
23
+ * A grading term with the levels it stands for and what it means in a few words.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { wearPresets, type WearPreset } from "@johnmorrisdotca/fuda-cards";
28
+ *
29
+ * const worst: WearPreset = wearPresets[wearPresets.length - 1];
30
+ * console.log(worst.id, worst.says); // damaged Chipped and folded corners, hard creases, staining.
31
+ * ```
32
+ */
12
33
  export interface WearPreset {
13
34
  id: string;
14
35
  name: string;
@@ -16,9 +37,42 @@ export interface WearPreset {
16
37
  /** What the condition term means in grading language, in a few words. */
17
38
  says: string;
18
39
  }
19
- /** From perfect to damaged. The names are the plain grading terms; the numbers are this package's reading of them (docs/wear.md). */
40
+ /**
41
+ * The seven grades, from perfect to damaged. The names are the plain grading terms; the numbers are this package's reading of them (docs/wear.md).
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import { wearPresets } from "@johnmorrisdotca/fuda-cards";
46
+ *
47
+ * console.log(wearPresets.map((preset) => preset.id).join(" "));
48
+ * // perfect mint near-mint lightly-played moderately-played heavily-played damaged
49
+ * ```
50
+ */
20
51
  export declare const wearPresets: readonly WearPreset[];
52
+ /**
53
+ * The grade with this id.
54
+ *
55
+ * @param id - A grade's id, such as `near-mint` (the `wear` attribute's value).
56
+ * @returns The grade, or `undefined` when there is none with that id.
57
+ * @example
58
+ * ```ts
59
+ * import { wearPreset } from "@johnmorrisdotca/fuda-cards";
60
+ *
61
+ * console.log(wearPreset("mint")?.name, wearPreset("pristine")); // Mint undefined
62
+ * ```
63
+ */
21
64
  export declare function wearPreset(id: string | null | undefined): WearPreset | undefined;
65
+ /**
66
+ * What a card asks of its wear: a grade or a number, a seed, and any level on its own.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { wearLevels, type WearOptions } from "@johnmorrisdotca/fuda-cards";
71
+ *
72
+ * const asked: WearOptions = { wear: "mint", corners: 0.5, seed: 4 };
73
+ * console.log(wearLevels(asked)?.corners); // 0.5
74
+ * ```
75
+ */
22
76
  export interface WearOptions {
23
77
  /** A preset's id, or a number from 0 to 1 for all the levels at once. */
24
78
  wear?: string | number | null;
@@ -30,9 +84,43 @@ export interface WearOptions {
30
84
  surface?: number;
31
85
  creases?: number;
32
86
  }
33
- /** The levels a card's wear asks for, or null when it asks for none (no wear, "perfect", an unknown name or all zero). */
87
+ /**
88
+ * The levels a card's wear asks for: the grade's or the number's, with any level named on its own laid over them.
89
+ *
90
+ * @param options - The wear asked for: a grade or a number from 0 to 1, and levels of corners, edges, surface and creases.
91
+ * @returns The levels, or `null` when the card asks for none: no wear, "perfect", an unknown name or every level zero.
92
+ * @example
93
+ * ```ts
94
+ * import { wearLevels } from "@johnmorrisdotca/fuda-cards";
95
+ *
96
+ * console.log(wearLevels({ wear: "perfect" })); // null
97
+ * console.log(wearLevels({ wear: 0.5 })?.surface); // 0.5
98
+ * ```
99
+ */
34
100
  export declare function wearLevels(options: WearOptions): WearLevels | null;
35
- export type CornerKind = 'sharp' | 'soft' | 'rounded' | 'chipped' | 'dog-eared';
101
+ /**
102
+ * How a corner is worn: sharp, softened, rounded, chipped or folded.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * import { wearLayout, wearPreset, type CornerKind } from "@johnmorrisdotca/fuda-cards";
107
+ *
108
+ * const kinds: CornerKind[] = wearLayout(wearPreset("damaged")!.levels, 3).corners.map((corner) => corner.kind);
109
+ * console.log(kinds.length); // 4
110
+ * ```
111
+ */
112
+ export type CornerKind = "sharp" | "soft" | "rounded" | "chipped" | "dog-eared";
113
+ /**
114
+ * One corner of a worn card: how it is worn and how far the damage reaches.
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * import { wearLayout, wearPreset, type WearCorner } from "@johnmorrisdotca/fuda-cards";
119
+ *
120
+ * const corner: WearCorner = wearLayout(wearPreset("lightly-played")!.levels).corners[0];
121
+ * console.log(typeof corner.size, corner.jag.length >= 0); // number true
122
+ * ```
123
+ */
36
124
  export interface WearCorner {
37
125
  kind: CornerKind;
38
126
  /** How far the damage reaches along each edge, in units of a 250 by 350 card. */
@@ -76,6 +164,17 @@ export interface WearFace {
76
164
  a: number;
77
165
  } | null;
78
166
  }
167
+ /**
168
+ * Every mark of a worn card: its corners, its centring, and the creases, scratches, specks and stain of each face.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * import { wearLayout, wearPreset, type WearFeatures } from "@johnmorrisdotca/fuda-cards";
173
+ *
174
+ * const features: WearFeatures = wearLayout(wearPreset("moderately-played")!.levels, 2);
175
+ * console.log(features.front.scratches.length > 0); // true
176
+ * ```
177
+ */
79
178
  export interface WearFeatures {
80
179
  levels: WearLevels;
81
180
  /** Top left, top right, bottom right, bottom left of the front. */
@@ -92,12 +191,51 @@ export declare const CARD_W = 250;
92
191
  export declare const CARD_H = 350;
93
192
  /** The radius of a perfect card's corner in these units (15 px of 300). */
94
193
  export declare const BASE_RADIUS = 12.5;
95
- /** Where every mark of a card lies. A pure function of the levels and the seed: no DOM, nothing at random. */
194
+ /**
195
+ * Where every mark of a card lies. A pure function of the levels and the seed: no DOM, nothing at random, so the same seed lays the same marks every time.
196
+ *
197
+ * @param levels - How much of each kind of wear (see {@link wearLevels}).
198
+ * @param seed - The seed that lays the marks out. Unless said, 1.
199
+ * @returns The corners, centring and marks of both faces.
200
+ * @example
201
+ * ```ts
202
+ * import { wearLayout, wearPreset } from "@johnmorrisdotca/fuda-cards";
203
+ *
204
+ * const levels = wearPreset("heavily-played")!.levels;
205
+ * console.log(JSON.stringify(wearLayout(levels, 5)) === JSON.stringify(wearLayout(levels, 5))); // true
206
+ * ```
207
+ */
96
208
  export declare function wearLayout(levels: WearLevels, seed?: number): WearFeatures;
97
- /** The outline of a face as an SVG path in the 250 by 350 box, corners as worn. */
98
- export declare function wearOutline(features: WearFeatures, face?: 'front' | 'back'): string;
99
- /** The SVG laid over one face: whitening, folds, creases, scratches, specks, stain and toning. Markup only, no script. */
100
- export declare function wearOverlaySvg(features: WearFeatures, face?: 'front' | 'back'): string;
209
+ /**
210
+ * The outline of a face as an SVG path in the 250 by 350 box, corners as worn.
211
+ *
212
+ * @param features - The marks of the card (see {@link wearLayout}).
213
+ * @param face - Which face; the back's corners are the front's seen from behind. Unless said, the front.
214
+ * @returns The path's `d` attribute.
215
+ * @example
216
+ * ```ts
217
+ * import { wearLayout, wearOutline, wearPreset } from "@johnmorrisdotca/fuda-cards";
218
+ *
219
+ * const features = wearLayout(wearPreset("damaged")!.levels, 1);
220
+ * console.log(wearOutline(features).startsWith("M"), wearOutline(features, "back") !== wearOutline(features)); // true true
221
+ * ```
222
+ */
223
+ export declare function wearOutline(features: WearFeatures, face?: "front" | "back"): string;
224
+ /**
225
+ * The SVG laid over one face: whitening, folds, creases, scratches, specks, stain and toning. Markup only, no script.
226
+ *
227
+ * @param features - The marks of the card (see {@link wearLayout}).
228
+ * @param face - Which face. Unless said, the front.
229
+ * @returns The `<svg>` markup, in the 250 by 350 box.
230
+ * @example
231
+ * ```ts
232
+ * import { wearLayout, wearOverlaySvg, wearPreset } from "@johnmorrisdotca/fuda-cards";
233
+ *
234
+ * const svg = wearOverlaySvg(wearLayout(wearPreset("near-mint")!.levels));
235
+ * console.log(svg.startsWith("<svg"), /<script|onload/i.test(svg)); // true false
236
+ * ```
237
+ */
238
+ export declare function wearOverlaySvg(features: WearFeatures, face?: "front" | "back"): string;
101
239
  /** Draw a card's wear into its shadow root; calling it again with other options replaces the first. */
102
240
  export declare function applyWear(root: ShadowRoot, options: WearOptions): boolean;
103
241
  /** Take a card's wear away, leaving the card as it was. */