@johnmorrisdotca/fuda-cards 0.10.1 → 0.12.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 (96) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE.md +16 -0
  3. package/README.md +106 -33
  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 +36 -67
  25. package/dist/collection-element.js +283 -484
  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 +154 -11
  37. package/dist/collection.js +140 -34
  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 +87 -4
  45. package/dist/live-3d.js +120 -52
  46. package/dist/materials.d.ts +117 -6
  47. package/dist/materials.js +246 -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 +138 -134
  62. package/docs/collection-displays.md +4 -4
  63. package/docs/collection-input.md +5 -5
  64. package/docs/framework-integration.md +1 -1
  65. package/docs/guides/architecture.md +8 -8
  66. package/docs/guides/bundlers.md +60 -0
  67. package/docs/guides/card-element.md +26 -2
  68. package/docs/guides/collections.md +2 -2
  69. package/docs/guides/embed-in-an-iframe.md +39 -0
  70. package/docs/guides/frameworks.md +93 -66
  71. package/docs/guides/getting-started.md +4 -2
  72. package/docs/guides/html-and-cdn.md +86 -0
  73. package/docs/guides/ja/architecture.md +4 -4
  74. package/docs/guides/ja/bundlers.md +60 -0
  75. package/docs/guides/ja/card-element.md +26 -2
  76. package/docs/guides/ja/collections.md +2 -2
  77. package/docs/guides/ja/embed-in-an-iframe.md +39 -0
  78. package/docs/guides/ja/frameworks.md +93 -66
  79. package/docs/guides/ja/getting-started.md +4 -2
  80. package/docs/guides/ja/html-and-cdn.md +86 -0
  81. package/docs/guides/ja/live-3d.md +37 -24
  82. package/docs/guides/ja/materials-and-stacks.md +4 -4
  83. package/docs/guides/ja/troubleshooting.md +2 -2
  84. package/docs/guides/ja/typescript-and-node.md +45 -0
  85. package/docs/guides/ja/upgrading.md +7 -2
  86. package/docs/guides/ja/wear.md +1 -1
  87. package/docs/guides/live-3d.md +37 -24
  88. package/docs/guides/materials-and-stacks.md +4 -4
  89. package/docs/guides/troubleshooting.md +2 -2
  90. package/docs/guides/typescript-and-node.md +45 -0
  91. package/docs/guides/upgrading.md +7 -2
  92. package/docs/guides/wear.md +1 -1
  93. package/docs/live-3d.md +1 -18
  94. package/docs/materials.md +15 -11
  95. package/docs/wear.md +4 -12
  96. package/package.json +57 -10
package/dist/pile-card.js CHANGED
@@ -5,8 +5,8 @@
5
5
  *
6
6
  * It shares everything with the collection's deck through `pile.js`: the fan's layout, how a card lands and how it flies.
7
7
  */
8
- import { fanBounds, fanLayout, fanRelative, fanShift, flight, normalizeLanding, timing } from './pile.js';
9
- import { stackLayout } from './stack.js';
8
+ import { fanBounds, fanLayout, fanRelative, fanShift, flight, normalizeLanding, timing } from "./pile.js";
9
+ import { stackLayout, stillsHtml } from "./stack.js";
10
10
  const states = new WeakMap();
11
11
  /** The gap between the stack and the pile at its side, in card widths (the collection's deck uses the same 0.28). */
12
12
  const GAP = 0.28;
@@ -16,28 +16,35 @@ const PILE_CSS = `:host{position:relative}.rotation{position:relative}:host(:not
16
16
  :host([flipped]) .card.fanned{transform:translate(calc(var(--fx,0) * 100cqw),0) rotate(calc(var(--fa,0) * 1deg)) rotateY(180deg)}
17
17
  .still{transition:transform var(--fuda-move,.65s) cubic-bezier(.2,.8,.2,1),transform-origin var(--fuda-move,.65s) cubic-bezier(.2,.8,.2,1);transition-delay:calc(var(--j,0) * var(--stag,22ms))}
18
18
  .card.fanned .still{transform-origin:50% 165%;transform:translate3d(calc(var(--rx,0) * 100cqw),calc(var(--ry,0) * 100cqw),calc(var(--t) / -2 - var(--j,1) * 1.5px)) rotate(calc(var(--ra,0) * 1deg));transition-delay:calc(var(--j,0) * var(--stag,28ms))}
19
- .still.far{display:none}.still.faced{background:none;border:0;transform-style:preserve-3d}.still .face,.flyer .face,.discard .face{position:absolute;inset:0}
19
+ .still.far{display:none}.still.faced{background:none;border:0;padding:0;transform-style:preserve-3d}.still.faced>.inner,.still.faced>.shine{display:none}.still .face,.flyer .face,.discard .face{position:absolute;inset:0}
20
20
  .art>img.copy{width:100%;height:100%;object-fit:contain;display:block}:host([motion="pan"]) .art>img.copy{object-fit:cover;object-position:var(--fuda-focus-x,50%) var(--fuda-focus-y,25%)}
21
21
  .discard{position:absolute;left:0;top:0;width:100%;height:100%;transform-style:preserve-3d;translate:calc(var(--dx,1.28) * 100cqw) 0;transition:translate var(--fuda-move,.65s) cubic-bezier(.2,.8,.2,1);pointer-events:none}
22
22
  .discard .card{transition:none}.discard[data-land=face-down] .card{transform:rotateY(180deg)}
23
- .flyer{position:absolute;left:0;top:0;width:100%;height:100%;transform-style:preserve-3d;pointer-events:none;--t:calc(var(--mm,.3) * 100cqw / 63)}.flyer .fly,.flyer .turn{position:absolute;inset:0;transform-style:preserve-3d}
23
+ .flyer{position:absolute;left:0;top:0;width:100%;height:100%;transform-style:preserve-3d;pointer-events:none;--t:calc(var(--mm,.325) * 100cqw / 63)}.flyer .fly,.flyer .turn{position:absolute;inset:0;transform-style:preserve-3d}
24
24
  .bar{position:absolute;left:50%;top:calc(100% + 14px);translate:-50% 0;display:flex;gap:.5rem;flex-wrap:wrap;justify-content:center;width:max-content;max-width:min(100vw - 24px,22rem);filter:none;font:600 .9rem system-ui,sans-serif}
25
25
  .bar button{box-sizing:border-box;min-height:48px;min-width:48px;padding:.45rem .9rem;font:inherit;color:#f6f4f1;background:#2a2d3c;border:2px solid #8a8ca0;border-radius:.7rem;cursor:pointer}.bar button:hover{border-color:#ffd27e}.bar button[aria-pressed=true]{background:#ffd27e;border-color:#ffd27e;color:#241a05}.bar button:disabled{opacity:.45;cursor:default}.bar button:focus-visible{outline:3px solid #ffd27e;outline-offset:3px}
26
26
  @media(prefers-reduced-motion:reduce){.card,.still,.discard{transition:none}}`;
27
- function num(host, name, fallback) { const n = Number.parseFloat(host.getAttribute(name) ?? ''); return Number.isFinite(n) ? n : fallback; }
27
+ function num(host, name, fallback) {
28
+ const n = Number.parseFloat(host.getAttribute(name) ?? "");
29
+ return Number.isFinite(n) ? n : fallback;
30
+ }
28
31
  /** Copies of the card's front and back faces for a card that is not the live one: the picture as an `img` copy (a 3D viewer is never copied), no ids, no Animate button. */
29
32
  function faceCopies(host, root) {
30
- const front = root.querySelector('.card > .front').cloneNode(true), back = root.querySelector('.card > .back').cloneNode(true);
33
+ const front = root.querySelector(".card > .front").cloneNode(true);
34
+ const back = root.querySelector(".card > .back").cloneNode(true);
31
35
  for (const el of [front, back]) {
32
- el.querySelectorAll('[id]').forEach((e) => e.removeAttribute('id'));
33
- el.querySelectorAll('.motion-button').forEach((e) => e.remove());
36
+ for (const marked of el.querySelectorAll("[id]"))
37
+ marked.removeAttribute("id");
38
+ for (const button of el.querySelectorAll(".motion-button"))
39
+ button.remove();
34
40
  }
35
- const slot = front.querySelector('slot'), img = host.querySelector('img');
41
+ const slot = front.querySelector("slot");
42
+ const img = host.querySelector("img");
36
43
  if (slot) {
37
44
  if (img) {
38
45
  const copy = img.cloneNode();
39
- copy.removeAttribute('id');
40
- copy.className = 'copy';
46
+ copy.removeAttribute("id");
47
+ copy.className = "copy";
41
48
  slot.replaceWith(copy);
42
49
  }
43
50
  else
@@ -46,15 +53,16 @@ function faceCopies(host, root) {
46
53
  return [front, back];
47
54
  }
48
55
  function stillsMarkup(count, host) {
49
- const messiness = num(host, 'stack-messiness', 0.3), seed = num(host, 'stack-seed', 1) + 7, depth = Math.min(52, Math.max(0, Math.floor(num(host, 'stack-depth', 10))));
56
+ const messiness = num(host, "stack-messiness", 0.3);
57
+ const seed = num(host, "stack-seed", 1) + 7;
58
+ const depth = Math.min(52, Math.max(0, Math.floor(num(host, "stack-depth", 10))));
50
59
  if (count < 2)
51
- return '';
52
- const places = stackLayout(count, { messiness, seed, depth });
53
- return places.map((p, at) => `<i class="still" style="--x:${p.x};--y:${p.y};--r:${p.rotate};--k:${count - places.length + at}"></i>`).join('');
60
+ return "";
61
+ return stillsHtml(stackLayout(count, { messiness, seed, depth }), count);
54
62
  }
55
63
  /** The pile of cards taken off: a thick block of `count` cards like the stack, its top card face up or turned over, at the stack's side. */
56
64
  function drawDiscard(host, root, state, count, land, dx) {
57
- const rotation = root.querySelector('.rotation');
65
+ const rotation = root.querySelector(".rotation");
58
66
  if (count < 1) {
59
67
  state.discard?.remove();
60
68
  state.discard = undefined;
@@ -62,53 +70,60 @@ function drawDiscard(host, root, state, count, land, dx) {
62
70
  }
63
71
  let box = state.discard;
64
72
  if (!box) {
65
- box = document.createElement('div');
66
- box.className = 'discard';
73
+ box = document.createElement("div");
74
+ box.className = "discard";
67
75
  rotation.append(box);
68
76
  state.discard = box;
69
77
  }
70
78
  box.dataset.land = land;
71
- box.style.setProperty('--dx', String(dx));
72
- const live = root.querySelector('.card'), copy = live.cloneNode(true);
73
- copy.className = 'card';
74
- copy.removeAttribute('style');
79
+ box.style.setProperty("--dx", String(dx));
80
+ const live = root.querySelector(".card");
81
+ const copy = live.cloneNode(true);
82
+ copy.className = "card";
83
+ copy.removeAttribute("style");
75
84
  const [front, back] = faceCopies(host, root);
76
- copy.querySelector('.front').replaceWith(front);
77
- copy.querySelector('.back').replaceWith(back);
85
+ copy.querySelector(".front").replaceWith(front);
86
+ copy.querySelector(".back").replaceWith(back);
78
87
  const mm = host.cardThicknessMm;
79
- copy.style.setProperty('--mm', String(mm * count));
80
- copy.style.setProperty('--mm1', String(mm));
81
- copy.classList.toggle('stacked', count > 1);
82
- copy.querySelector('.stills').innerHTML = stillsMarkup(count, host);
88
+ copy.style.setProperty("--mm", String(mm * count));
89
+ copy.style.setProperty("--mm1", String(mm));
90
+ copy.classList.toggle("stacked", count > 1);
91
+ copy.querySelector(".stills").innerHTML = stillsMarkup(count, host);
83
92
  box.replaceChildren(copy);
84
93
  }
85
94
  function fanState(host, root, on) {
86
- const card = root.querySelector('.card'), stills = Array.from(root.querySelectorAll('.card > .stills > .still'));
87
- card.classList.toggle('fanned', on);
88
- const n = Math.min(10, 1 + stills.length), places = fanLayout(n, num(host, 'stack-seed', 1), 0.34), rel = fanRelative(places);
95
+ const card = root.querySelector(".card");
96
+ const stills = Array.from(root.querySelectorAll(".card > .stills > .still"));
97
+ card.classList.toggle("fanned", on);
98
+ const n = Math.min(10, 1 + stills.length);
99
+ const places = fanLayout(n, num(host, "stack-seed", 1), 0.34);
100
+ const rel = fanRelative(places);
89
101
  if (on) {
90
- card.style.setProperty('--fx', String(places[0].x));
91
- card.style.setProperty('--fa', String(places[0].angle));
102
+ card.style.setProperty("--fx", String(places[0].x));
103
+ card.style.setProperty("--fa", String(places[0].angle));
92
104
  const [front, back] = stills.length ? faceCopies(host, root) : [null, null];
93
105
  stills.forEach((still, at) => {
94
106
  const i = stills.length - at;
95
- still.classList.toggle('far', i >= n);
107
+ still.classList.toggle("far", i >= n);
96
108
  if (i >= n)
97
109
  return;
98
- still.style.setProperty('--rx', String(rel[i].x));
99
- still.style.setProperty('--ry', String(rel[i].y));
100
- still.style.setProperty('--ra', String(rel[i].angle));
101
- still.style.setProperty('--j', String(i));
102
- if (!still.classList.contains('faced') || still.dataset.sig !== host.innerHTML + host.getAttribute('title')) {
103
- still.classList.add('faced');
104
- still.dataset.sig = host.innerHTML + host.getAttribute('title');
110
+ still.style.setProperty("--rx", String(rel[i].x));
111
+ still.style.setProperty("--ry", String(rel[i].y));
112
+ still.style.setProperty("--ra", String(rel[i].angle));
113
+ still.style.setProperty("--j", String(i));
114
+ if (!still.classList.contains("faced") || still.dataset.sig !== host.innerHTML + host.getAttribute("title")) {
115
+ still.classList.add("faced");
116
+ still.dataset.sig = host.innerHTML + host.getAttribute("title");
105
117
  const [f, b] = [front.cloneNode(true), back.cloneNode(true)];
106
118
  still.replaceChildren(f, b);
107
119
  }
108
120
  });
109
121
  }
110
122
  else {
111
- stills.forEach((still, at) => { still.style.setProperty('--j', String(stills.length - at)); still.classList.remove('far'); });
123
+ stills.forEach((still, at) => {
124
+ still.style.setProperty("--j", String(stills.length - at));
125
+ still.classList.remove("far");
126
+ });
112
127
  }
113
128
  return { places, bounds: on ? fanBounds(places) : { left: 0, right: 0, top: 0, bottom: 0 } };
114
129
  }
@@ -119,59 +134,83 @@ export function applyPile(host, root) {
119
134
  state = { taken: 0, token: 0 };
120
135
  states.set(host, state);
121
136
  }
122
- if (!root.querySelector('style[data-pile]')) {
123
- const style = document.createElement('style');
124
- style.dataset.pile = '';
137
+ if (!root.querySelector("style[data-pile]")) {
138
+ const style = document.createElement("style");
139
+ style.dataset.pile = "";
125
140
  style.textContent = PILE_CSS;
126
141
  root.append(style);
127
142
  }
128
- const t = timing(host, true), land = normalizeLanding(host.getAttribute('deck-land'));
129
- host.style.setProperty('--fuda-move', t.on ? `${t.ms}ms` : '0s');
143
+ const t = timing(host, true);
144
+ const land = normalizeLanding(host.getAttribute("deck-land"));
145
+ host.style.setProperty("--fuda-move", t.on ? `${t.ms}ms` : "0s");
130
146
  if (t.on)
131
- host.style.removeProperty('--stag');
147
+ host.style.removeProperty("--stag");
132
148
  else
133
- host.style.setProperty('--stag', '0ms');
134
- const total = host.stackCount, taken = Math.min(total - 1, Math.max(0, Math.floor(num(host, 'taken-off', 0)))), fan = host.fanOpen;
135
- const was = state.taken, token = ++state.token;
149
+ host.style.setProperty("--stag", "0ms");
150
+ const total = host.stackCount;
151
+ const taken = Math.min(total - 1, Math.max(0, Math.floor(num(host, "taken-off", 0))));
152
+ const fan = host.fanOpen;
153
+ const was = state.taken;
154
+ const token = ++state.token;
136
155
  const { places, bounds } = fanState(host, root, fan);
137
156
  if (!fan) {
138
- const faced = Array.from(root.querySelectorAll('.card > .stills > .still.faced'));
157
+ const faced = Array.from(root.querySelectorAll(".card > .stills > .still.faced"));
139
158
  // The copies of the faces go once the cards have closed (at once when nothing is animated).
140
- const clean = () => { if (!host.fanOpen)
141
- faced.forEach((s) => { s.classList.remove('faced'); s.replaceChildren(); delete s.dataset.sig; }); };
159
+ const clean = () => {
160
+ if (!host.fanOpen)
161
+ faced.forEach((s) => {
162
+ s.classList.remove("faced");
163
+ s.replaceChildren();
164
+ delete s.dataset.sig;
165
+ });
166
+ };
142
167
  if (faced.length) {
143
168
  if (t.on)
144
- void Promise.all(root.querySelector('.card').getAnimations({ subtree: true }).map((a) => a.finished.catch(() => undefined))).then(clean);
169
+ void Promise.all(root
170
+ .querySelector(".card")
171
+ .getAnimations({ subtree: true })
172
+ .map((a) => a.finished.catch(() => undefined))).then(clean);
145
173
  else
146
174
  clean();
147
175
  }
148
176
  }
149
- const dx = 1 + GAP + bounds.right, mmPx = (host.cardThicknessMm * host.offsetWidth) / 63;
177
+ const dx = 1 + GAP + bounds.right;
178
+ const mmPx = (host.cardThicknessMm * host.offsetWidth) / 63;
150
179
  // The pile at the side: where it is, how many cards and how its top card lies. While a card flies to it, it holds the cards that were there before.
151
180
  const moving = taken !== was && t.on && host.offsetWidth > 0;
152
181
  state.flyer?.remove();
153
182
  state.flyer = undefined;
154
183
  drawDiscard(host, root, state, moving && taken > was ? was : taken, land, dx);
155
184
  if (moving) {
156
- const off = taken > was, rotation = root.querySelector('.rotation'), [front, back] = faceCopies(host, root);
157
- const flyer = document.createElement('div');
158
- flyer.className = 'flyer';
159
- flyer.style.setProperty('--mm', String(host.cardThicknessMm));
160
- const fly = document.createElement('div');
161
- fly.className = 'fly';
162
- const turn = document.createElement('div');
163
- turn.className = 'turn';
185
+ const off = taken > was;
186
+ const rotation = root.querySelector(".rotation");
187
+ const [front, back] = faceCopies(host, root);
188
+ const flyer = document.createElement("div");
189
+ flyer.className = "flyer";
190
+ flyer.style.setProperty("--mm", String(host.cardThicknessMm));
191
+ const fly = document.createElement("div");
192
+ fly.className = "fly";
193
+ const turn = document.createElement("div");
194
+ turn.className = "turn";
164
195
  turn.append(front, back);
165
196
  fly.append(turn);
166
197
  flyer.append(fly);
167
198
  rotation.append(flyer);
168
199
  state.flyer = flyer;
169
- const w = host.offsetWidth, heldBefore = off ? total - was : total - taken, zStack = (heldBefore * mmPx) / 2, zPile = (Math.max(1, off ? taken : was) * mmPx) / 2;
170
- const lead = fan && places.length > 1 ? places[0] : null, shift = lead ? fanShift(lead) : { x: 0, y: 0 };
200
+ const w = host.offsetWidth;
201
+ const heldBefore = off ? total - was : total - taken;
202
+ const zStack = (heldBefore * mmPx) / 2;
203
+ const zPile = (Math.max(1, off ? taken : was) * mmPx) / 2;
204
+ const lead = fan && places.length > 1 ? places[0] : null;
205
+ const shift = lead ? fanShift(lead) : { x: 0, y: 0 };
171
206
  // The flyer sits at its landing place; it flies from (off) or to (back) the stack, where an open fan holds the top card.
172
- flyer.style.transform = off ? `translate3d(${dx * w}px,0,${zPile}px)` : `translate3d(${shift.x * w}px,${shift.y * w}px,${zStack}px) rotate(${lead ? lead.angle : 0}deg)`;
173
- const away = off ? { x: -dx * w + shift.x * w, y: shift.y * w, z: zStack - zPile } : { x: dx * w - shift.x * w, y: -shift.y * w, z: zPile - zStack };
174
- const anims = flight(fly, turn, away, land, off ? 'off' : 'back', Math.round(t.ms * 1.15), lead ? (off ? lead.angle : -lead.angle) : 0);
207
+ flyer.style.transform = off
208
+ ? `translate3d(${dx * w}px,0,${zPile}px)`
209
+ : `translate3d(${shift.x * w}px,${shift.y * w}px,${zStack}px) rotate(${lead ? lead.angle : 0}deg)`;
210
+ const away = off
211
+ ? { x: -dx * w + shift.x * w, y: shift.y * w, z: zStack - zPile }
212
+ : { x: dx * w - shift.x * w, y: -shift.y * w, z: zPile - zStack };
213
+ const anims = flight(fly, turn, away, land, off ? "off" : "back", Math.round(t.ms * 1.15), lead ? (off ? lead.angle : -lead.angle) : 0);
175
214
  void Promise.all(anims.map((a) => a.finished)).then(() => {
176
215
  if (token !== state.token)
177
216
  return;
@@ -182,32 +221,40 @@ export function applyPile(host, root) {
182
221
  }
183
222
  state.taken = taken;
184
223
  // The bar of three buttons, when asked for.
185
- if (host.hasAttribute('pile-controls')) {
224
+ if (host.hasAttribute("pile-controls")) {
186
225
  if (!state.bar) {
187
- const bar = document.createElement('div');
188
- bar.className = 'bar';
189
- bar.setAttribute('role', 'group');
190
- bar.setAttribute('aria-label', 'Pile');
191
- const make = (label, run) => { const b = document.createElement('button'); b.type = 'button'; b.textContent = label; b.addEventListener('click', run); return b; };
192
- const fanButton = make('Fan', () => { if (host.getAttribute('fan') === 'open')
193
- host.removeAttribute('fan');
194
- else
195
- host.setAttribute('fan', 'open'); });
196
- fanButton.dataset.role = 'fan';
197
- fanButton.setAttribute('aria-pressed', 'false');
198
- const off = make('Take a card off', () => host.takeOff());
199
- off.dataset.role = 'off';
200
- const back = make('Put it back', () => host.putBack());
201
- back.dataset.role = 'back';
226
+ const bar = document.createElement("div");
227
+ bar.className = "bar";
228
+ bar.setAttribute("role", "group");
229
+ bar.setAttribute("aria-label", "Pile");
230
+ const make = (label, run) => {
231
+ const b = document.createElement("button");
232
+ b.type = "button";
233
+ b.textContent = label;
234
+ b.addEventListener("click", run);
235
+ return b;
236
+ };
237
+ const fanButton = make("Fan", () => {
238
+ if (host.getAttribute("fan") === "open")
239
+ host.removeAttribute("fan");
240
+ else
241
+ host.setAttribute("fan", "open");
242
+ });
243
+ fanButton.dataset.role = "fan";
244
+ fanButton.setAttribute("aria-pressed", "false");
245
+ const off = make("Take a card off", () => host.takeOff());
246
+ off.dataset.role = "off";
247
+ const back = make("Put it back", () => host.putBack());
248
+ back.dataset.role = "back";
202
249
  bar.append(fanButton, off, back);
203
250
  root.append(bar);
204
251
  state.bar = bar;
205
252
  }
206
253
  const q = (role) => state.bar.querySelector(`[data-role=${role}]`);
207
- q('fan').setAttribute('aria-pressed', String(fan));
208
- q('fan').textContent = fan ? 'Close the fan' : 'Fan open';
209
- q('off').disabled = taken >= total - 1;
210
- q('back').disabled = taken < 1;
254
+ q("fan").setAttribute("aria-pressed", String(fan));
255
+ q("fan").textContent = fan ? "Close the fan" : "Fan open";
256
+ q("off").disabled = taken >= total - 1;
257
+ q("back").disabled = taken < 1;
211
258
  }
212
259
  else if (state.bar) {
213
260
  state.bar.remove();
@@ -217,7 +264,7 @@ export function applyPile(host, root) {
217
264
  const next = { left: bounds.left, right, top: bounds.top, bottom: bounds.bottom };
218
265
  const old = host.pileBounds;
219
266
  host.pileBounds = next;
220
- host.style.setProperty('--pivot', String((next.right - next.left) / 2));
267
+ host.style.setProperty("--pivot", String((next.right - next.left) / 2));
221
268
  if (old.left !== next.left || old.right !== next.right || old.top !== next.top || old.bottom !== next.bottom)
222
- host.dispatchEvent(new CustomEvent('fuda-pile', { detail: next }));
269
+ host.dispatchEvent(new CustomEvent("fuda-pile", { detail: next }));
223
270
  }
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)