@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
@@ -0,0 +1,123 @@
1
+ // Drawing the binder: two facing sheets of pockets, the page turn, and the page count.
2
+ import { binderCovers, buildLeaf, buildSide, sideCount, spreadOf, spreadSides } from "./binder.js";
3
+ export function syncBinderChoices(host, view) {
4
+ const show = view === "binder";
5
+ const cur = (attr, dflt, ok) => {
6
+ const v = host.getAttribute(attr);
7
+ return v && ok.includes(v) ? v : dflt;
8
+ };
9
+ const vals = {
10
+ binder: cur("binder", "ring", ["ring", "zip"]),
11
+ cover: cur("cover", "green", binderCovers.map((c) => c.id)),
12
+ sheet: cur("sheet", "clear", ["clear", "black"]),
13
+ pockets: cur("pockets", "9", ["4", "9", "12"]),
14
+ };
15
+ for (const [k, h] of Object.entries(host.binderChoices)) {
16
+ h.hidden = !show;
17
+ h.set(vals[k]);
18
+ }
19
+ }
20
+ export function binderPageCount(host) {
21
+ const per = host.binderLay().per;
22
+ const sides = sideCount(host.data.length, per);
23
+ const [l, r] = spreadSides(spreadOf(host.index, per), sides);
24
+ const a = (l < 0 ? r : l) + 1;
25
+ const b = (r < 0 ? l : r) + 1;
26
+ return a === b ? `Page ${a} / ${sides}` : `Pages ${a}–${b} / ${sides}`;
27
+ }
28
+ /** The binder: a ring or zip binder open on two facing sheets of pockets. A page turn swings the leaf about the spine, bending as it goes, and shows the pockets on its back. */
29
+ export function renderBinder(host, v, t) {
30
+ const layout = host.binderLay();
31
+ const n = host.data.length;
32
+ const sides = sideCount(n, layout.per);
33
+ const spread = spreadOf(host.index, layout.per);
34
+ const sheet = host.getAttribute("sheet") === "black" ? "black" : "clear";
35
+ const zip = host.getAttribute("binder") === "zip";
36
+ const cover = binderCovers.find((c) => c.id === host.getAttribute("cover")) ?? binderCovers[0];
37
+ v.classList.add("binder");
38
+ v.style.setProperty("--cover", cover.color);
39
+ const pocket = (index, live) => {
40
+ const c = host.data[index];
41
+ const el = document.createElement("div");
42
+ el.className = "pocket";
43
+ if (live)
44
+ el.append(host.image(c, index));
45
+ else {
46
+ const img = document.createElement("img");
47
+ img.src = c.image;
48
+ img.alt = "";
49
+ img.style.cssText = "width:86%;height:92%;object-fit:contain;border-radius:3%;box-shadow:0 2px 5px #0007";
50
+ el.append(img);
51
+ }
52
+ return el;
53
+ };
54
+ const [L, R] = spreadSides(spread, sides);
55
+ const turn = host.turn;
56
+ host.turn = null;
57
+ const flat = turn && t.on && Math.abs(turn.to - turn.from) === 1 ? turn : null;
58
+ const [oldL, oldR] = flat ? spreadSides(flat.from, sides) : [L, R];
59
+ const [newL, newR] = flat ? spreadSides(flat.to, sides) : [L, R];
60
+ const forward = !!flat && flat.to > flat.from;
61
+ const book = document.createElement("div");
62
+ book.className = `book ${zip ? "b-zip" : "b-ring"}`;
63
+ const inside = document.createElement("div");
64
+ inside.className = "inside";
65
+ const left = document.createElement("div");
66
+ const right = document.createElement("div");
67
+ left.className = "half";
68
+ right.className = "half";
69
+ const put = (half, side, where) => {
70
+ half.replaceChildren();
71
+ if (side >= 0)
72
+ half.append(buildSide(side, where, sheet, layout, n, pocket));
73
+ };
74
+ // While a leaf swings the half it has not reached yet shows the page it is leaving; the other half already shows the page it uncovers.
75
+ put(left, flat ? (forward ? oldL : newL) : L, "l");
76
+ put(right, flat ? (forward ? newR : oldR) : R, "r");
77
+ const spine = document.createElement("div");
78
+ spine.className = "spine";
79
+ for (const y of [18, 50, 82]) {
80
+ const ring = document.createElement("i");
81
+ ring.className = "ring";
82
+ ring.style.top = `${y}%`;
83
+ spine.append(ring);
84
+ }
85
+ inside.append(left, right, spine);
86
+ const tab = document.createElement("i");
87
+ tab.className = "tab";
88
+ book.append(inside, tab);
89
+ v.append(book);
90
+ if (flat) {
91
+ const w = 300;
92
+ const strip = 34;
93
+ const { leaf, slices } = buildLeaf(forward ? oldR : newR, forward ? newL : oldL, sheet, layout, n, pocket, w, strip);
94
+ inside.append(leaf);
95
+ const ms = Math.round(t.ms * 1.4);
96
+ const ease = "cubic-bezier(.45,.05,.3,1)";
97
+ const a = forward ? [0, -180] : [-180, 0];
98
+ leaf.style.transform = `rotateY(${a[1]}deg)`;
99
+ const swing = leaf.animate([{ transform: `rotateY(${a[0]}deg)` }, { transform: `rotateY(${a[1]}deg)` }], {
100
+ duration: ms,
101
+ easing: ease,
102
+ fill: "forwards",
103
+ });
104
+ // Each slice leans on the one before it, so the sheet bends as it swings (the free edge trails) and straightens at the end.
105
+ const bend = forward ? 8 : -8;
106
+ slices.forEach((s, k) => {
107
+ if (k)
108
+ s.animate([
109
+ { transform: "rotateY(0deg)" },
110
+ { transform: `rotateY(${bend}deg)`, offset: 0.5 },
111
+ { transform: "rotateY(0deg)" },
112
+ ], { duration: ms, easing: ease });
113
+ });
114
+ const finish = () => {
115
+ if (!leaf.isConnected)
116
+ return;
117
+ put(left, newL, "l");
118
+ put(right, newR, "r");
119
+ leaf.remove();
120
+ };
121
+ swing.finished.then(finish, finish);
122
+ }
123
+ }
@@ -0,0 +1,2 @@
1
+ import type { CollectionHost } from "./collection-host.js";
2
+ export declare function buildControls(host: CollectionHost): void;
@@ -0,0 +1,143 @@
1
+ // The shell's controls: the display tiles, the option rows, Previous and Next, the draw filter, touch and the arrow keys.
2
+ import { binderCovers } from "./binder.js";
3
+ import { createChoice } from "./choice.js";
4
+ import { collectionViews, randomDrawIndex } from "./collection.js";
5
+ import { writeAddress } from "./collection-address.js";
6
+ import { icon } from "./collection-icons.js";
7
+ import { COLLECTION_CSS, COLLECTION_SHELL } from "./collection-styles.js";
8
+ import { deckLandings } from "./pile.js";
9
+ import { zoomView } from "./zoom.js";
10
+ export function buildControls(host) {
11
+ if (host.zoom)
12
+ return;
13
+ host.root.innerHTML = `<style>${COLLECTION_CSS}</style>${COLLECTION_SHELL}`;
14
+ const keep = (attr, value) => {
15
+ if (value === null)
16
+ host.removeAttribute(attr);
17
+ else
18
+ host.setAttribute(attr, value);
19
+ writeAddress(host);
20
+ };
21
+ host.tiles = createChoice({
22
+ label: "Display",
23
+ layout: "tiles",
24
+ value: host.currentView,
25
+ options: collectionViews.map((v) => ({ value: v.id, label: v.label, icon: icon(v.id) })),
26
+ onChange: (value) => {
27
+ host.setAttribute("view", value);
28
+ writeAddress(host);
29
+ },
30
+ });
31
+ host.q(".displays").append(host.tiles.element);
32
+ host.landing = createChoice({
33
+ label: "A card taken off lands",
34
+ value: host.land,
35
+ options: deckLandings.map((l) => ({ value: l.id, label: l.label })),
36
+ onChange: (value) => {
37
+ host.setAttribute("deck-land", value);
38
+ writeAddress(host);
39
+ },
40
+ });
41
+ host.fanChoice = createChoice({
42
+ label: "Fan",
43
+ value: host.fanOpen ? "open" : "closed",
44
+ options: [
45
+ { value: "closed", label: "Closed" },
46
+ { value: "open", label: "Open" },
47
+ ],
48
+ onChange: (value) => {
49
+ if (value === "open")
50
+ host.setAttribute("fan", "open");
51
+ else
52
+ host.removeAttribute("fan");
53
+ writeAddress(host);
54
+ },
55
+ });
56
+ host.animateChoice = createChoice({
57
+ label: "Animate",
58
+ value: "on",
59
+ options: [
60
+ { value: "on", label: "On" },
61
+ { value: "off", label: "Off" },
62
+ ],
63
+ onChange: (value) => keep("animate", value === "off" ? "off" : "on"),
64
+ });
65
+ host.speedChoice = createChoice({
66
+ label: "Speed",
67
+ value: "normal",
68
+ options: [
69
+ { value: "slow", label: "Slow" },
70
+ { value: "normal", label: "Normal" },
71
+ { value: "quick", label: "Quick" },
72
+ ],
73
+ onChange: (value) => keep("speed", value),
74
+ });
75
+ const bin = (attr, label, value, options) => {
76
+ const h = createChoice({
77
+ label,
78
+ value,
79
+ options,
80
+ onChange: (v) => {
81
+ host.setAttribute(attr, v);
82
+ writeAddress(host);
83
+ },
84
+ });
85
+ host.binderChoices[attr] = h;
86
+ return h.element;
87
+ };
88
+ host.q(".opts").append(host.landing.element, host.fanChoice.element, bin("binder", "Binder", "ring", [
89
+ { value: "ring", label: "Ring" },
90
+ { value: "zip", label: "Zip" },
91
+ ]), bin("cover", "Cover", "green", binderCovers.map((c) => ({ value: c.id, label: c.label }))), bin("sheet", "Sheet", "clear", [
92
+ { value: "clear", label: "Clear" },
93
+ { value: "black", label: "Black" },
94
+ ]), bin("pockets", "Pockets", "9", [
95
+ { value: "4", label: "4" },
96
+ { value: "9", label: "9" },
97
+ { value: "12", label: "12" },
98
+ ]), host.animateChoice.element, host.speedChoice.element);
99
+ host.speedChoice.element.classList.add("speed");
100
+ host.q(".prev").onclick = () => host.move(-1);
101
+ host.q(".next").onclick = () => host.move(1);
102
+ const input = host.q(".draw-filter input");
103
+ const draw = host.q(".draw");
104
+ input.oninput = () => {
105
+ host.drawFilter = input.value;
106
+ draw.disabled = !host.canDraw();
107
+ };
108
+ draw.onclick = () => {
109
+ const next = randomDrawIndex(host.data, host.index, host.drawFilter);
110
+ if (next === null)
111
+ return;
112
+ host.index = next;
113
+ host.render(false, { kind: "move", dir: 1 });
114
+ };
115
+ const stage = host.q(".stage");
116
+ host.zoom = zoomView(stage, host.q(".v"), {
117
+ address: host.hasAttribute("address"),
118
+ items: ".card,.slot",
119
+ theme: host.getAttribute("theme") ?? undefined,
120
+ });
121
+ let startX = 0;
122
+ stage.addEventListener("touchstart", (e) => {
123
+ if (e.touches.length === 1)
124
+ startX = e.touches[0].clientX;
125
+ }, { passive: true });
126
+ stage.addEventListener("touchend", (e) => {
127
+ if (!host.stacked || host.piled || host.zoom.zoomed() || e.changedTouches.length !== 1)
128
+ return;
129
+ const dx = e.changedTouches[0].clientX - startX;
130
+ if (Math.abs(dx) > 45) {
131
+ if (e.cancelable)
132
+ e.preventDefault();
133
+ host.move(dx < 0 ? 1 : -1);
134
+ }
135
+ }, { passive: false });
136
+ stage.addEventListener("keydown", (e) => {
137
+ if (!host.stacked || host.piled || (e.key !== "ArrowRight" && e.key !== "ArrowLeft"))
138
+ return;
139
+ e.preventDefault();
140
+ host.move(e.key === "ArrowRight" ? 1 : -1);
141
+ host.q(".stack .card:last-child")?.focus();
142
+ });
143
+ }
@@ -0,0 +1,11 @@
1
+ import type { CollectionHost } from "./collection-host.js";
2
+ import { type FanPlace } from "./pile.js";
3
+ /** One pile of the deck. `positions` are data indexes from the bottom of the pile to its top; the cards under the top one lie nudged and turned from the seed and the top one lies square. A deck's cards also carry where they lie when the fan is open. */
4
+ export declare function drawPile(host: CollectionHost, kind: "deck" | "discard", positions: number[], fan?: FanPlace[]): HTMLElement;
5
+ /** Where the fan's cards lie for a pile of this many cards. A phone's fan lies closer across and leans on its angle. */
6
+ export declare function fanFor(host: CollectionHost, count: number): FanPlace[];
7
+ /** Open the fan (or close it) on the pile that is on screen, without drawing it again, so every card glides: the open fan's reach is put in custom properties that move the piles apart and the Fit box. */
8
+ export declare function applyFan(host: CollectionHost, v: HTMLElement, open: boolean): void;
9
+ export declare function renderDeck(host: CollectionHost, v: HTMLElement, fanOnly?: boolean): void;
10
+ /** The card that just moved flies between the two piles, turning over sideways on the way if it lands face down. The same element, so the card is never drawn twice. An open fan sends the card from where the fan holds it. */
11
+ export declare function playFlip(host: CollectionHost, direction: "off" | "back", v: HTMLElement, ms: number): void;
@@ -0,0 +1,165 @@
1
+ // Drawing the deck: its piles of cards, the fan, and the flight of a card between the two piles.
2
+ import { isCard } from "./collection.js";
3
+ import { fanBounds, fanLayout, fanShift, flight } from "./pile.js";
4
+ import { stackLayout } from "./stack.js";
5
+ /** One pile of the deck. `positions` are data indexes from the bottom of the pile to its top; the cards under the top one lie nudged and turned from the seed and the top one lies square. A deck's cards also carry where they lie when the fan is open. */
6
+ export function drawPile(host, kind, positions, fan = []) {
7
+ const pile = document.createElement("div");
8
+ pile.className = `pile ${kind}-pile${kind === "discard" ? " discard" : ""}`;
9
+ pile.dataset.land = host.land;
10
+ const messy = Math.min(1, Math.max(0, Number(host.getAttribute("deck-messiness") ?? 0.3)));
11
+ const seed = Number(host.getAttribute("deck-seed") ?? 1) || 1;
12
+ const depth = Math.min(positions.length - 1, fan.length ? fan.length - 1 : 9);
13
+ const shown = positions.slice(positions.length - 1 - depth);
14
+ const places = stackLayout(positions.length, { messiness: messy, seed, depth });
15
+ shown.forEach((at, j) => {
16
+ const top = j === shown.length - 1;
17
+ const c = host.data[at];
18
+ const place = places[j];
19
+ const el = document.createElement("div");
20
+ const flat = !isCard(c);
21
+ el.className = `card${top ? " top" : ""}${flat ? " flat" : ""}`;
22
+ el.dataset.i = String(at);
23
+ if (place && !top) {
24
+ el.style.setProperty("--x", String(place.x));
25
+ el.style.setProperty("--y", String(place.y));
26
+ el.style.setProperty("--r", String(place.rotate));
27
+ }
28
+ el.style.setProperty("--k", String(j));
29
+ el.style.setProperty("--j", String(shown.length - 1 - j));
30
+ const f = fan[shown.length - 1 - j];
31
+ if (f) {
32
+ el.style.setProperty("--fx", String(f.x));
33
+ el.style.setProperty("--fa", String(f.angle));
34
+ el.style.setProperty("--i", String(shown.length - 1 - j));
35
+ }
36
+ const fly = document.createElement("div");
37
+ fly.className = "fly";
38
+ const turn = document.createElement("div");
39
+ turn.className = "turn";
40
+ const front = document.createElement("div");
41
+ front.className = "face front";
42
+ const img = document.createElement("img");
43
+ img.src = c.image;
44
+ img.alt = top ? c.name : "";
45
+ img.loading = "lazy";
46
+ img.onerror = () => {
47
+ img.replaceWith(document.createTextNode(`Image unavailable: ${c.name}`));
48
+ };
49
+ front.append(img);
50
+ turn.append(front);
51
+ // An object has no back to show: only a card gets the picture of its back (or the package's own).
52
+ if (!flat) {
53
+ const back = document.createElement("div");
54
+ back.className = "face back";
55
+ if (c.backImage) {
56
+ const b = document.createElement("img");
57
+ b.src = c.backImage;
58
+ b.alt = "";
59
+ b.loading = "lazy";
60
+ back.append(b);
61
+ }
62
+ else {
63
+ back.classList.add("generic-back");
64
+ back.textContent = "FUDA";
65
+ }
66
+ turn.append(back);
67
+ }
68
+ fly.append(turn);
69
+ el.append(fly);
70
+ el.onclick = () => host.open(c);
71
+ if (top) {
72
+ el.setAttribute("role", "button");
73
+ el.setAttribute("aria-label", `Inspect ${c.name}`);
74
+ el.tabIndex = 0;
75
+ el.onkeydown = (e) => {
76
+ if (e.key === "Enter" || e.key === " ") {
77
+ e.preventDefault();
78
+ host.open(c);
79
+ }
80
+ };
81
+ }
82
+ pile.append(el);
83
+ });
84
+ return pile;
85
+ }
86
+ /** Where the fan's cards lie for a pile of this many cards. A phone's fan lies closer across and leans on its angle. */
87
+ export function fanFor(host, count) {
88
+ const narrow = (host.clientWidth || innerWidth) < 520;
89
+ return fanLayout(count, Number(host.getAttribute("deck-seed") ?? 1) || 1, narrow ? 0.3 : 0.34);
90
+ }
91
+ /** Open the fan (or close it) on the pile that is on screen, without drawing it again, so every card glides: the open fan's reach is put in custom properties that move the piles apart and the Fit box. */
92
+ export function applyFan(host, v, open) {
93
+ const b = open && host.fanPlaces.length > 1 ? fanBounds(host.fanPlaces) : { left: 0, right: 0, top: 0, bottom: 0 };
94
+ v.classList.toggle("fan-open", open && host.fanPlaces.length > 1);
95
+ for (const [k, x] of [
96
+ ["--fl", b.left],
97
+ ["--fr", b.right],
98
+ ["--ft", b.top],
99
+ ["--fb", b.bottom],
100
+ ])
101
+ v.style.setProperty(k, String(x));
102
+ }
103
+ export function renderDeck(host, v, fanOnly = false) {
104
+ const n = host.data.length;
105
+ const deckPositions = [];
106
+ const discardPositions = [];
107
+ v.classList.add("deck");
108
+ if (fanOnly) {
109
+ const size = Math.min(10, Math.max(2, Math.floor(Number(host.getAttribute("fan-size")) || 10)));
110
+ const count = Math.min(size, n);
111
+ // The fan window: `count` cards from the first one, wrapping round; the top card is the first and lies on the left when open.
112
+ for (let p = count - 1; p >= 0; p--)
113
+ deckPositions.push((host.index + p) % n);
114
+ v.classList.add("fanonly");
115
+ host.fanPlaces = fanFor(host, count);
116
+ v.append(drawPile(host, "deck", deckPositions, host.fanPlaces));
117
+ }
118
+ else {
119
+ // The deck is the cards from the top card down (bottom first); the pile is the cards already flipped off, the last one on top.
120
+ for (let p = 0; p < n - host.index; p++)
121
+ deckPositions.push(n - 1 - p);
122
+ for (let p = 0; p < host.index; p++)
123
+ discardPositions.push(p);
124
+ host.fanPlaces = fanFor(host, Math.min(10, deckPositions.length));
125
+ const slot = () => {
126
+ const s = document.createElement("div");
127
+ s.className = "slot";
128
+ return s;
129
+ };
130
+ // The deck is on the left and the pile on its other side; an empty pile is a dashed slot, so Fit does not jump when the first card lands.
131
+ v.append(deckPositions.length ? drawPile(host, "deck", deckPositions, host.fanPlaces) : slot(), discardPositions.length ? drawPile(host, "discard", discardPositions) : slot());
132
+ }
133
+ const extent = document.createElement("div");
134
+ extent.className = "extent";
135
+ v.append(extent);
136
+ applyFan(host, v, host.fanOpen);
137
+ }
138
+ /** The card that just moved flies between the two piles, turning over sideways on the way if it lands face down. The same element, so the card is never drawn twice. An open fan sends the card from where the fan holds it. */
139
+ export function playFlip(host, direction, v, ms) {
140
+ // Either pile may be an empty dashed slot (the last card put back leaves the pile empty): the card still flies the same distance, from where the pile was.
141
+ const deck = v.querySelector(".deck-pile");
142
+ const discard = v.querySelector(".discard-pile");
143
+ const moved = direction === "off"
144
+ ? discard?.querySelector(".card.top")
145
+ : deck?.querySelector(".card.top");
146
+ if (!moved)
147
+ return;
148
+ const style = getComputedStyle(v);
149
+ const w = parseFloat(style.getPropertyValue("--w")) || 200;
150
+ const gap = parseFloat(style.getPropertyValue("--gap")) || 56;
151
+ const fr = parseFloat(v.style.getPropertyValue("--fr")) || 0;
152
+ const dx = (1 + fr) * w + gap;
153
+ const dy = 0;
154
+ const fly = moved.querySelector(".fly");
155
+ const turn = moved.querySelector(".turn");
156
+ const lead = host.fanOpen && host.fanPlaces.length > 1 ? host.fanPlaces[0] : null;
157
+ const shift = lead ? fanShift(lead) : { x: 0, y: 0 };
158
+ const away = { x: shift.x * w, y: shift.y * w };
159
+ const spin = lead ? lead.angle : 0;
160
+ // Taking a card off an open fan starts it where the fan holds it; bringing it back ends there.
161
+ // The piles' cards lie 0.9 px apart in depth (ten at most), so the flying card starts just above the higher of them and ends at its own place.
162
+ const z = 9.6 - (Number.parseFloat(moved.style.getPropertyValue("--k")) || 0) * 0.9;
163
+ const from = direction === "off" ? { x: -dx + away.x, y: -dy + away.y, z } : { x: dx - away.x, y: dy - away.y, z };
164
+ flight(fly, turn, from, moved.classList.contains("flat") ? "face-up" : host.land, direction, ms, direction === "off" ? spin : -spin);
165
+ }
@@ -0,0 +1,2 @@
1
+ import type { CollectionHost } from "./collection-host.js";
2
+ export declare function renderDetail(host: CollectionHost): void;
@@ -0,0 +1,28 @@
1
+ export function renderDetail(host) {
2
+ const dlg = host.q("dialog");
3
+ dlg.replaceChildren();
4
+ const close = document.createElement("button");
5
+ close.textContent = "Close";
6
+ close.onclick = () => dlg.close();
7
+ dlg.append(close);
8
+ if (host.selected) {
9
+ const img = document.createElement("img");
10
+ img.className = "detail";
11
+ img.src = host.back && host.selected.backImage ? host.selected.backImage : host.selected.image;
12
+ img.alt = `${host.selected.name}, ${host.back ? "back" : "front"}`;
13
+ const label = document.createElement("p");
14
+ label.textContent = host.selected.name;
15
+ dlg.append(img, label);
16
+ if (host.selected.backImage) {
17
+ const flip = document.createElement("button");
18
+ flip.textContent = host.back ? "Show front" : "Show back";
19
+ flip.onclick = () => {
20
+ host.back = !host.back;
21
+ renderDetail(host);
22
+ };
23
+ dlg.append(flip);
24
+ }
25
+ }
26
+ if (!dlg.open)
27
+ dlg.showModal();
28
+ }
@@ -1,34 +1,47 @@
1
- import { type CollectionCard } from './collection.js';
2
- import type { LiveCards, LiveItem } from './live-3d.js';
3
- /** Live 3D in a collection's cards: the shared cards from `createLiveCards` and the 3D item of a card, if it has one. See docs/live-3d.md. */
1
+ import { type CollectionCard, type CollectionCardInput } from "./collection.js";
2
+ import type { LiveCards, LiveItem } from "./live-3d.js";
3
+ /**
4
+ * Live 3D in a collection's cards: the shared cards from `createLiveCards` and the 3D item of a card, if it has one. See docs/live-3d.md.
5
+ *
6
+ * @example
7
+ * ```ts no-run
8
+ * import type { CollectionLive } from "@johnmorrisdotca/fuda-cards/collection-element";
9
+ * import { createLiveCards } from "@johnmorrisdotca/fuda-cards/live-3d";
10
+ *
11
+ * const live: CollectionLive = { cards: createLiveCards({ load: () => import("three") }), item: () => undefined };
12
+ * console.log(typeof live.item);
13
+ * ```
14
+ */
4
15
  export interface CollectionLive {
5
16
  cards: LiveCards<never>;
6
17
  item(card: CollectionCard): LiveItem<never> | undefined;
7
18
  }
19
+ /**
20
+ * `<fuda-collection>`: a whole collection in one of ten displays (a deck, a rolodex, binder pages, a display case, a random draw, a story scroll, a gallery wall,
21
+ * a spotlight, a card table, a fan), with zoom, Fit and Centre, the choices kept in the page address and a card detail on a press. Give it
22
+ * `cards` (see {@link parseCollection}) and a `view`.
23
+ *
24
+ * @example
25
+ * ```ts no-run
26
+ * import "@johnmorrisdotca/fuda-cards/collection-element";
27
+ * import type { FudaCollection } from "@johnmorrisdotca/fuda-cards/collection-element";
28
+ *
29
+ * const collection = document.createElement("fuda-collection") as FudaCollection;
30
+ * collection.setAttribute("view", "binder");
31
+ * collection.cards = { cards: [{ name: "Explorer One", image: "one.jpg" }] };
32
+ * document.body.append(collection);
33
+ * ```
34
+ */
8
35
  export declare class FudaCollection extends HTMLElement {
9
- private root;
10
- private data;
11
- private index;
12
- private pageSize;
13
- private selected;
14
- private back;
15
- private drawFilter;
16
- private zoom;
17
- private tiles?;
18
- private landing?;
19
- private fanChoice?;
20
- private animateChoice?;
21
- private speedChoice?;
22
- private binderChoices;
23
- private turn;
24
- private flip;
25
- private addressTimer;
26
- private fanPlaces;
27
36
  private liveSetup;
28
37
  private liveOff;
29
38
  static get observedAttributes(): string[];
30
- set cards(value: CollectionCard[]);
31
- get cards(): CollectionCard[];
39
+ set cards(value: readonly CollectionCardInput[] | {
40
+ readonly cards: readonly CollectionCardInput[];
41
+ });
42
+ get cards(): readonly CollectionCardInput[] | {
43
+ readonly cards: readonly CollectionCardInput[];
44
+ };
32
45
  /** Live 3D: the card in focus turns in 3D on the page's one WebGL context, and every other card stays its picture. Nothing 3D loads until a card is focused. */
33
46
  set live(value: CollectionLive | null);
34
47
  get live(): CollectionLive | null;
@@ -39,52 +52,8 @@ export declare class FudaCollection extends HTMLElement {
39
52
  connectedCallback(): void;
40
53
  disconnectedCallback(): void;
41
54
  attributeChangedCallback(name: string): void;
42
- private get currentView();
43
- /** How a card taken off lands: still facing up, or turned over sideways. The 0.6.0 value `upside-down` is still accepted and means turned over. */
44
- private get land();
45
- private get fanOpen();
46
- private get stacked();
47
- private get piled();
48
- /** Only a card turns: a collection of objects (no card in it) never turns, and in a mixed one the objects stay square to the reader while the cards turn. */
49
- private get hasCards();
50
- private get turnable();
51
- private q;
52
- /** With `address`, the display, the fan, the landing, the animation and its speed are kept in the page's address (`?view=fan&fan=open&land=face-down&animate=off`), and a link to it opens the same. */
53
- private readAddress;
54
- private writeAddress;
55
- private move;
56
- private binderLay;
57
55
  /** The first card of an opening of the binder: the right-hand page's first card, or the left-hand page's when the right-hand page is past the end. */
58
56
  private spreadFirst;
59
- private image;
60
- private open;
61
- private renderDialog;
62
- private canDraw;
63
- /** The one length every move takes, in a custom property the stylesheet reads: `animate="off"` and reduced motion make it 0, `speed` stretches it. */
64
- private timeVars;
65
- private syncChoices;
66
- private build;
67
- /** One pile of the deck. `positions` are data indexes from the bottom of the pile to its top; the cards under the top one lie nudged and turned from the seed and the top one lies square. A deck's cards also carry where they lie when the fan is open. */
68
- private pile;
69
- /** Where the fan's cards lie for a pile of this many cards. A phone's fan lies closer across and leans on its angle. */
70
- private fanFor;
71
- /** Open the fan (or close it) on the pile that is on screen, without drawing it again, so every card glides: the open fan's reach is put in custom properties that move the piles apart and the Fit box. */
72
- private fanVars;
73
57
  /** `fan` changed: when a pile is on screen, move its cards to or from the fan in place (animated) and fit the view to it. Returns false when there is no pile to move. */
74
58
  private setFan;
75
- private renderDeck;
76
- /** The card that just moved flies between the two piles, turning over sideways on the way if it lands face down. The same element, so the card is never drawn twice. An open fan sends the card from where the fan holds it. */
77
- private playFlip;
78
- private renderFan;
79
- /** Where every card is on screen now, by data index. */
80
- private snapshot;
81
- /** Cards that arrive grow and fade in, a little to the side the reader is heading. Only an opening keyframe: they end on the card's own style. */
82
- private enter;
83
- /** After a card or page change or a switch of display: glide what stays, let what goes leave, bring in what comes. Nothing runs afterwards: each is one animation that ends by itself. */
84
- private play;
85
- private syncBinder;
86
- private binderCount;
87
- /** The binder: a ring or zip binder open on two facing sheets of pockets. A page turn swings the leaf about the spine, bending as it goes, and shows the pockets on its back. */
88
- private renderBinder;
89
- private render;
90
59
  }