@johnmorrisdotca/fuda-cards 0.9.1 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +382 -58
- package/dist/collection-element.d.ts +13 -0
- package/dist/collection-element.js +20 -2
- package/dist/live-3d.d.ts +43 -0
- package/dist/live-3d.js +161 -0
- package/docs/API.md +793 -0
- package/docs/collection-displays.md +1 -1
- package/docs/guides/accessibility-and-theming.md +62 -0
- package/docs/guides/architecture.md +53 -0
- package/docs/guides/binder-and-piles.md +83 -0
- package/docs/guides/card-element.md +65 -0
- package/docs/guides/collections.md +86 -0
- package/docs/guides/frameworks.md +105 -0
- package/docs/guides/getting-started.md +94 -0
- package/docs/guides/ja/accessibility-and-theming.md +62 -0
- package/docs/guides/ja/architecture.md +53 -0
- package/docs/guides/ja/binder-and-piles.md +83 -0
- package/docs/guides/ja/card-element.md +65 -0
- package/docs/guides/ja/collections.md +86 -0
- package/docs/guides/ja/frameworks.md +105 -0
- package/docs/guides/ja/getting-started.md +94 -0
- package/docs/guides/ja/live-3d.md +75 -0
- package/docs/guides/ja/materials-and-stacks.md +73 -0
- package/docs/guides/ja/motion-and-spin.md +73 -0
- package/docs/guides/ja/troubleshooting.md +60 -0
- package/docs/guides/ja/upgrading.md +56 -0
- package/docs/guides/ja/wear.md +63 -0
- package/docs/guides/live-3d.md +75 -0
- package/docs/guides/materials-and-stacks.md +73 -0
- package/docs/guides/motion-and-spin.md +73 -0
- package/docs/guides/troubleshooting.md +60 -0
- package/docs/guides/upgrading.md +56 -0
- package/docs/guides/wear.md +63 -0
- package/docs/live-3d.md +59 -0
- package/package.json +29 -6
|
@@ -1,4 +1,10 @@
|
|
|
1
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. */
|
|
4
|
+
export interface CollectionLive {
|
|
5
|
+
cards: LiveCards<never>;
|
|
6
|
+
item(card: CollectionCard): LiveItem<never> | undefined;
|
|
7
|
+
}
|
|
2
8
|
export declare class FudaCollection extends HTMLElement {
|
|
3
9
|
private root;
|
|
4
10
|
private data;
|
|
@@ -18,9 +24,16 @@ export declare class FudaCollection extends HTMLElement {
|
|
|
18
24
|
private flip;
|
|
19
25
|
private addressTimer;
|
|
20
26
|
private fanPlaces;
|
|
27
|
+
private liveSetup;
|
|
28
|
+
private liveOff;
|
|
21
29
|
static get observedAttributes(): string[];
|
|
22
30
|
set cards(value: CollectionCard[]);
|
|
23
31
|
get cards(): CollectionCard[];
|
|
32
|
+
/** 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
|
+
set live(value: CollectionLive | null);
|
|
34
|
+
get live(): CollectionLive | null;
|
|
35
|
+
/** The cards are drawn again on every move, so their live views are made again too: the pictures stay in the shared cache. */
|
|
36
|
+
private attachLive;
|
|
24
37
|
/** The card on top of the deck, or the first card of the page or fan (counting from 0). */
|
|
25
38
|
get position(): number;
|
|
26
39
|
connectedCallback(): void;
|
|
@@ -76,14 +76,30 @@ export class FudaCollection extends HTMLElement {
|
|
|
76
76
|
flip = null;
|
|
77
77
|
addressTimer = 0;
|
|
78
78
|
fanPlaces = [];
|
|
79
|
+
liveSetup = null;
|
|
80
|
+
liveOff = [];
|
|
79
81
|
static get observedAttributes() { return ['view', 'page-size', 'columns', 'fan', 'fan-size', 'deck-land', 'deck-messiness', 'deck-seed', 'theme', 'animate', 'speed', 'binder', 'cover', 'sheet', 'pockets']; }
|
|
80
82
|
set cards(value) { this.data = parseCollection(value); this.index = Math.min(this.index, Math.max(0, this.data.length - 1)); this.flip = null; this.render(); }
|
|
81
83
|
get cards() { return this.data.map(c => ({ ...c, ...(c.metadata ? { metadata: { ...c.metadata } } : {}) })); }
|
|
84
|
+
/** 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. */
|
|
85
|
+
set live(value) { this.liveSetup = value; this.render(); }
|
|
86
|
+
get live() { return this.liveSetup; }
|
|
87
|
+
/** The cards are drawn again on every move, so their live views are made again too: the pictures stay in the shared cache. */
|
|
88
|
+
attachLive(v) { for (const off of this.liveOff)
|
|
89
|
+
off(); this.liveOff = []; const setup = this.liveSetup; if (!setup)
|
|
90
|
+
return; for (const el of Array.from(v.querySelectorAll('.card[data-i]:not(.ghost)'))) {
|
|
91
|
+
const c = this.data[Number(el.dataset.i)], item = c && setup.item(c);
|
|
92
|
+
if (!item)
|
|
93
|
+
continue;
|
|
94
|
+
const still = el.querySelector('img') ?? undefined;
|
|
95
|
+
this.liveOff.push(setup.cards.add(el, item, still ? { still } : {}));
|
|
96
|
+
} }
|
|
82
97
|
/** The card on top of the deck, or the first card of the page or fan (counting from 0). */
|
|
83
98
|
get position() { return this.index; }
|
|
84
99
|
connectedCallback() { if (this.hasAttribute('address'))
|
|
85
100
|
this.readAddress(); this.render(); }
|
|
86
|
-
disconnectedCallback() { clearTimeout(this.addressTimer);
|
|
101
|
+
disconnectedCallback() { clearTimeout(this.addressTimer); for (const off of this.liveOff)
|
|
102
|
+
off(); this.liveOff = []; }
|
|
87
103
|
attributeChangedCallback(name) {
|
|
88
104
|
this.pageSize = Math.min(100, Math.max(1, Math.floor(Number(this.getAttribute('page-size')) || 9)));
|
|
89
105
|
if (name === 'fan' && this.setFan())
|
|
@@ -351,9 +367,10 @@ export class FudaCollection extends HTMLElement {
|
|
|
351
367
|
}
|
|
352
368
|
/** 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. */
|
|
353
369
|
playFlip(direction, v, ms) {
|
|
370
|
+
// 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.
|
|
354
371
|
const deck = v.querySelector('.deck-pile'), discard = v.querySelector('.discard-pile');
|
|
355
372
|
const moved = direction === 'off' ? discard?.querySelector('.card.top') : deck?.querySelector('.card.top');
|
|
356
|
-
if (!moved
|
|
373
|
+
if (!moved)
|
|
357
374
|
return;
|
|
358
375
|
const style = getComputedStyle(v), w = parseFloat(style.getPropertyValue('--w')) || 200, gap = parseFloat(style.getPropertyValue('--gap')) || 56, fr = parseFloat(v.style.getPropertyValue('--fr')) || 0;
|
|
359
376
|
const dx = (1 + fr) * w + gap, dy = 0;
|
|
@@ -554,6 +571,7 @@ export class FudaCollection extends HTMLElement {
|
|
|
554
571
|
v.append(el);
|
|
555
572
|
}
|
|
556
573
|
}
|
|
574
|
+
this.attachLive(v);
|
|
557
575
|
this.zoom.refresh(reset);
|
|
558
576
|
if (this.flip && this.piled && t.on && this.data.length > 1)
|
|
559
577
|
this.playFlip(this.flip, v, Math.round(t.ms * 1.15));
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { BuildContext, RendererStats, Scene3D, SharedRenderer, SharedRendererOptions, ThreeLike } from '@johnmorrisdotca/kazoku-ui/renderer';
|
|
2
|
+
/** A scene for a card. `object` is what turns about its upright axis while the card is in focus. */
|
|
3
|
+
export interface LiveScene extends Scene3D {
|
|
4
|
+
object?: {
|
|
5
|
+
rotation: {
|
|
6
|
+
y: number;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
/** What a card shows in 3D: a key (items with the same key share their pictures) and a scene made from shared assets (`assets.acquire`). */
|
|
11
|
+
export interface LiveItem<T extends ThreeLike = ThreeLike> {
|
|
12
|
+
key: string;
|
|
13
|
+
build(context: BuildContext<T>): LiveScene | Promise<LiveScene>;
|
|
14
|
+
}
|
|
15
|
+
export interface LiveCardsOptions<T extends ThreeLike = ThreeLike> extends Omit<SharedRendererOptions<T>, 'scheduler' | 'assets' | 'clock' | 'document'> {
|
|
16
|
+
/** Seconds for one turn of the card in focus (12). 0 does not turn it. */
|
|
17
|
+
turnSeconds?: number;
|
|
18
|
+
/** How long the pointer has to rest on a card before it goes live, in milliseconds (150), so moving the pointer across a page of cards starts nothing. A press or the keyboard does not wait. */
|
|
19
|
+
dwellMs?: number;
|
|
20
|
+
/** How far outside the view a card with no picture of its own gets its still made, in pixels (300). */
|
|
21
|
+
lookAheadPx?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface AddOptions {
|
|
24
|
+
/** A picture the card already has (an `<img>` of its own), kept until the card is in focus. With one, the card costs no 3D code until then. */
|
|
25
|
+
still?: HTMLImageElement;
|
|
26
|
+
}
|
|
27
|
+
export interface LiveCards<T extends ThreeLike = ThreeLike> {
|
|
28
|
+
/** Make a card live on focus: a `<fuda-card>` or any element with a size. Gives back the function that takes it off again. */
|
|
29
|
+
add(card: HTMLElement, item: LiveItem<T>, options?: AddOptions): () => void;
|
|
30
|
+
/** Put this card in focus: it takes the context, and the card that had it freezes to a picture at its last angle. False if it did not go live (another took it, reduced motion, no WebGL). */
|
|
31
|
+
focus(card: HTMLElement): Promise<boolean>;
|
|
32
|
+
/** No card in focus: the live one freezes to a picture and nothing draws. */
|
|
33
|
+
blur(): void;
|
|
34
|
+
/** The card in focus, if any. */
|
|
35
|
+
readonly focused: HTMLElement | undefined;
|
|
36
|
+
/** The shared renderer's own figures; undefined until it has been needed. */
|
|
37
|
+
readonly stats: RendererStats | undefined;
|
|
38
|
+
/** The shared renderer; undefined until it has been needed. */
|
|
39
|
+
readonly renderer: SharedRenderer<T> | undefined;
|
|
40
|
+
/** Take every card off and let go of the renderer. */
|
|
41
|
+
dispose(): void;
|
|
42
|
+
}
|
|
43
|
+
export declare function createLiveCards<T extends ThreeLike = ThreeLike>(options: LiveCardsOptions<T>): LiveCards<T>;
|
package/dist/live-3d.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/** Stills are shared per this many degrees, so a card frozen at any angle shows a picture at its angle without one picture per degree. */
|
|
2
|
+
const STILL_STEP = 5;
|
|
3
|
+
const reducedMotion = () => typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
4
|
+
export function createLiveCards(options) {
|
|
5
|
+
const { turnSeconds = 12, dwellMs = 150, lookAheadPx = 300, ...rendererOptions } = options;
|
|
6
|
+
const entries = new Map();
|
|
7
|
+
let renderer;
|
|
8
|
+
let loading;
|
|
9
|
+
let focusedEntry;
|
|
10
|
+
let disposed = false;
|
|
11
|
+
let asked = 0;
|
|
12
|
+
/** The renderer's module comes in only when a card first needs it: a page of pictures never loads it. */
|
|
13
|
+
const needRenderer = () => (loading ??= import('@johnmorrisdotca/kazoku-ui/renderer').then(({ createSharedRenderer }) => (renderer = createSharedRenderer(rendererOptions))));
|
|
14
|
+
/** The view of a card, made when first needed. The item's key carries the card's angle, so its still shows the angle it stopped at. */
|
|
15
|
+
const viewOf = (entry) => {
|
|
16
|
+
if (entry.view)
|
|
17
|
+
return Promise.resolve(entry.view);
|
|
18
|
+
return (entry.making ??= needRenderer().then((shared) => {
|
|
19
|
+
if (disposed || !entries.has(entry.card))
|
|
20
|
+
return undefined;
|
|
21
|
+
const turning = {
|
|
22
|
+
get key() { return `${entry.item.key}@${((Math.round(entry.angle / STILL_STEP) * STILL_STEP) % 360 + 360) % 360}`; },
|
|
23
|
+
build: async (context) => {
|
|
24
|
+
const scene = await entry.item.build(context);
|
|
25
|
+
const turn = () => { if (scene.object)
|
|
26
|
+
scene.object.rotation.y = (entry.angle * Math.PI) / 180; };
|
|
27
|
+
turn();
|
|
28
|
+
return { ...scene, update(dt, now) {
|
|
29
|
+
const more = scene.update?.(dt, now) === true;
|
|
30
|
+
if (!turnSeconds || !scene.object || reducedMotion())
|
|
31
|
+
return more;
|
|
32
|
+
entry.angle += (360 * dt) / turnSeconds;
|
|
33
|
+
turn();
|
|
34
|
+
return true;
|
|
35
|
+
} };
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return (entry.view = shared.view(entry.host, turning, entry.poster ? { still: entry.poster } : {}));
|
|
39
|
+
}));
|
|
40
|
+
};
|
|
41
|
+
const putPosterBack = (entry) => { if (entry.poster && entry.posterSrc && !entry.poster.getAttribute('src'))
|
|
42
|
+
entry.poster.src = entry.posterSrc; };
|
|
43
|
+
/** A card near the view shows its picture. One with a picture of its own needs nothing; the renderer makes the others', one at a time. */
|
|
44
|
+
const prepare = (entry) => {
|
|
45
|
+
if (entry.view) {
|
|
46
|
+
if (entry.poster && !entry.poster.getAttribute('src') && !entry.view.live)
|
|
47
|
+
putPosterBack(entry);
|
|
48
|
+
if (!entry.poster)
|
|
49
|
+
void entry.view.showStill();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!entry.poster)
|
|
53
|
+
void viewOf(entry).then((view) => view?.showStill());
|
|
54
|
+
};
|
|
55
|
+
const near = typeof IntersectionObserver === 'function' ? new IntersectionObserver((seen) => {
|
|
56
|
+
for (const { target, isIntersecting } of seen) {
|
|
57
|
+
const entry = entries.get(target);
|
|
58
|
+
if (entry && isIntersecting)
|
|
59
|
+
prepare(entry);
|
|
60
|
+
}
|
|
61
|
+
}, { rootMargin: `${lookAheadPx}px` }) : undefined;
|
|
62
|
+
async function focus(card) {
|
|
63
|
+
const entry = entries.get(card);
|
|
64
|
+
if (!entry || disposed || reducedMotion())
|
|
65
|
+
return false;
|
|
66
|
+
const mine = ++asked;
|
|
67
|
+
if (focusedEntry === entry && entry.view?.live)
|
|
68
|
+
return true;
|
|
69
|
+
focusedEntry = entry;
|
|
70
|
+
const view = await viewOf(entry);
|
|
71
|
+
if (!view || mine !== asked)
|
|
72
|
+
return false;
|
|
73
|
+
return view.focus();
|
|
74
|
+
}
|
|
75
|
+
function blur() {
|
|
76
|
+
asked += 1;
|
|
77
|
+
const entry = focusedEntry;
|
|
78
|
+
focusedEntry = undefined;
|
|
79
|
+
entry?.view?.blur();
|
|
80
|
+
}
|
|
81
|
+
// Who is in focus: the card under the pointer after a short rest, the one holding keyboard focus, the one last pressed. A press elsewhere ends it.
|
|
82
|
+
document.addEventListener('pointerdown', onPressAnywhere, true);
|
|
83
|
+
function onPressAnywhere(event) {
|
|
84
|
+
if (focusedEntry && !event.composedPath().includes(focusedEntry.card))
|
|
85
|
+
blur();
|
|
86
|
+
}
|
|
87
|
+
function listen(entry) {
|
|
88
|
+
const { card } = entry;
|
|
89
|
+
let timer;
|
|
90
|
+
const wait = () => { clearTimeout(timer); timer = undefined; };
|
|
91
|
+
const enter = (e) => { if (e.pointerType === 'touch')
|
|
92
|
+
return; wait(); timer = setTimeout(() => { void focus(card); }, dwellMs); };
|
|
93
|
+
const leave = (e) => { if (e.pointerType === 'touch')
|
|
94
|
+
return; wait(); if (focusedEntry === entry && !card.matches(':focus-within'))
|
|
95
|
+
blur(); };
|
|
96
|
+
const press = () => { wait(); void focus(card); };
|
|
97
|
+
const focusIn = () => { wait(); void focus(card); };
|
|
98
|
+
const focusOut = (e) => { if (focusedEntry === entry && !card.contains(e.relatedTarget) && !card.matches(':hover'))
|
|
99
|
+
blur(); };
|
|
100
|
+
card.addEventListener('pointerenter', enter);
|
|
101
|
+
card.addEventListener('pointerleave', leave);
|
|
102
|
+
card.addEventListener('pointerdown', press);
|
|
103
|
+
card.addEventListener('focusin', focusIn);
|
|
104
|
+
card.addEventListener('focusout', focusOut);
|
|
105
|
+
return () => {
|
|
106
|
+
wait();
|
|
107
|
+
card.removeEventListener('pointerenter', enter);
|
|
108
|
+
card.removeEventListener('pointerleave', leave);
|
|
109
|
+
card.removeEventListener('pointerdown', press);
|
|
110
|
+
card.removeEventListener('focusin', focusIn);
|
|
111
|
+
card.removeEventListener('focusout', focusOut);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function add(card, item, addOptions = {}) {
|
|
115
|
+
entries.get(card)?.off();
|
|
116
|
+
const host = document.createElement('div');
|
|
117
|
+
host.className = 'fuda-live';
|
|
118
|
+
// It lies over the art and takes no press, so everything on the card still works. `.art ::slotted(*)` keeps it unseen until the card is ready.
|
|
119
|
+
host.style.cssText = 'position:absolute;inset:0;pointer-events:none';
|
|
120
|
+
if (getComputedStyle(card).position === 'static')
|
|
121
|
+
card.style.position = 'relative';
|
|
122
|
+
card.append(host);
|
|
123
|
+
const entry = { card, item, host, angle: 0, off: () => { }, ...(addOptions.still ? { poster: addOptions.still, posterSrc: addOptions.still.getAttribute('src') ?? undefined } : {}) };
|
|
124
|
+
const unlisten = listen(entry);
|
|
125
|
+
entries.set(card, entry);
|
|
126
|
+
near?.observe(card);
|
|
127
|
+
if (!near)
|
|
128
|
+
prepare(entry);
|
|
129
|
+
entry.off = () => {
|
|
130
|
+
if (entries.get(card) !== entry)
|
|
131
|
+
return;
|
|
132
|
+
if (focusedEntry === entry)
|
|
133
|
+
focusedEntry = undefined;
|
|
134
|
+
unlisten();
|
|
135
|
+
near?.unobserve(card);
|
|
136
|
+
entries.delete(card);
|
|
137
|
+
entry.view?.dispose();
|
|
138
|
+
putPosterBack(entry);
|
|
139
|
+
if (entry.poster)
|
|
140
|
+
entry.poster.style.visibility = '';
|
|
141
|
+
host.remove();
|
|
142
|
+
};
|
|
143
|
+
return entry.off;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
add,
|
|
147
|
+
focus,
|
|
148
|
+
blur,
|
|
149
|
+
get focused() { return focusedEntry?.card; },
|
|
150
|
+
get stats() { return renderer?.stats; },
|
|
151
|
+
get renderer() { return renderer; },
|
|
152
|
+
dispose() {
|
|
153
|
+
disposed = true;
|
|
154
|
+
for (const entry of [...entries.values()])
|
|
155
|
+
entry.off();
|
|
156
|
+
document.removeEventListener('pointerdown', onPressAnywhere, true);
|
|
157
|
+
near?.disconnect();
|
|
158
|
+
renderer?.dispose();
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|