@johnmorrisdotca/fuda-cards 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -3
- package/dist/collection-element.d.ts +23 -3
- package/dist/collection-element.js +203 -76
- package/dist/element.d.ts +19 -0
- package/dist/element.js +35 -6
- package/dist/pile-card.d.ts +17 -0
- package/dist/pile-card.js +223 -0
- package/dist/pile.d.ts +45 -2
- package/dist/pile.js +68 -8
- package/dist/zoom.d.ts +8 -0
- package/dist/zoom.js +34 -3
- package/docs/collection-displays.md +6 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,6 +62,16 @@ The card has thickness, drawn to scale at the 63 mm wide card size: set `thickne
|
|
|
62
62
|
|
|
63
63
|
The top card is the live card. The block under it is one piece, and with `stack-messiness` above 0 (0 neat to 1 very messy, default 0.3) the `stack-depth` cards under the top one (default 10, at most 52) are drawn as flat stills, nudged and turned from `stack-seed`: the same seed gives the same stack, and a card put on top moves none of those under it. A stack is one 3D scene whatever its count, and it draws nothing while it is at rest: a test checks that a 52-card stack requests no frames at rest. The idea of a pile from neat to messy and its parameters come from [toranpu](https://github.com/johnmorrisdotca/toranpu)'s `<toranpu-pile>`. In script, `card.stackCount` and `card.stackHeight` (mm) are read-only, and the package exports `cardMaterials`, `stackPresets`, `stackHeightMm` and `stackLayout`.
|
|
64
64
|
|
|
65
|
+
### Fan a stack, take a card off it
|
|
66
|
+
|
|
67
|
+
A stack of more than one card is a pile like the collection's deck, with the same fan and the same landing. `fan="open"` spreads up to ten cards in an arc (every card drawn with its face and the picture) and closes back exactly where they began. `taken-off="3"` puts the top three cards on a pile at the stack's side, as thick as the cards in it: `card.takeOff()` flies the top card across and `card.putBack()` brings it back, and `deck-land="face-up"` (the default) or `"face-down"` (turned over sideways, showing the back) says how it lands. `pile-controls` draws three buttons (Fan, Take a card off, Put it back; each at least 48 px) under the card; leave about 70 px of room below it. `animate="off"` and `speed="slow|quick"` are the same as in the collection. The card turns with the pile (the angle, tilt and Auto-turn work as before) and an open fan or a pile at the side is part of what turns: `card.pileBounds` says how far they reach beyond the card, in card widths, and `fuda-pile` is sent when it changes, so a page can fit its view.
|
|
68
|
+
|
|
69
|
+
```html
|
|
70
|
+
<fuda-card title="Deck" stack="52" pile-controls></fuda-card>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The pile code is a separate module (`dist/pile-card.js`) that `<fuda-card>` loads only when a stack asks for `fan`, `taken-off` or `pile-controls`; a card without them is drawn exactly as before and never requests it.
|
|
74
|
+
|
|
65
75
|
### Wear: cards that are not perfect
|
|
66
76
|
|
|
67
77
|
`wear="lightly-played"` draws the card after use: corners that are soft, rounded, chipped or folded (a dog-ear), whitening on the edges, creases, scratches, specks, staining, yellowing and a print that sits off centre. The presets are the plain grading terms: `mint`, `near-mint`, `lightly-played`, `moderately-played`, `heavily-played` and `damaged`; `perfect`, no attribute at all, or an unknown name draws nothing. `wear-seed` fixes where the marks lie, so a card looks the same every time it is drawn. `wear-corners`, `wear-edges`, `wear-surface` and `wear-creases` (0 to 1) change one kind on its own, and `wear="0.5"` sets them all at once.
|
|
@@ -116,11 +126,13 @@ Run `pnpm dev` and open `/demo/collections-component.html`. Import `@johnmorrisd
|
|
|
116
126
|
|
|
117
127
|
**Choosing a display.** The twelve displays are a row of buttons across the top, each with a small picture of its layout and its name, at least 48 px high, one chosen, wrapping on a phone (4 to a row, 6 on a desk). It is a radio group: one tab stop, the arrow keys move the choice. There is no drop-down. The colours follow the reader's light or dark setting; set `theme="light"` or `theme="dark"` to choose for them.
|
|
118
128
|
|
|
119
|
-
**The deck.** `view="deck"` draws a deck of up to ten cards (with a seeded, slightly messy lie: `deck-messiness` 0 to 1, default 0.3, and `deck-seed`) and, beside it, a pile. **Next** flips the top card off the deck: it lifts, flies across and lands on the pile at the deck's other side; **Previous** brings it back. Where it lands is a choice: `deck-land="face-up"` (still facing up, the default) or `face-down` ("Turned over": it turns over sideways about its vertical axis, like turning a page, so the pile shows the card's `backImage`, or the package's own navy back when it has none). A card never lands with its picture on its head. The 0.6.0 value `upside-down` is still accepted and means `face-down`. The same card element flies, so a card is never drawn twice; with reduced motion it moves without flying. **Rotate
|
|
129
|
+
**The deck, and the other piles.** Four displays draw a pile of cards, and each does everything a pile does: it turns in 3D, it fans open and closed, and a card comes off it onto a pile at its side. They are the Overlapping Deck (`view="deck"`), Random Draw, the Jukebox Carousel and the Flip Tower (all three drew a pile of three cards before 0.7.0, so they are now the same pile with their own look); the Fan of Cards is the same pile with only its fan. Rolodex and Spotlight Carousel spread their cards out instead of piling them, so they are carousels and stay as they were. `view="deck"` draws a deck of up to ten cards (with a seeded, slightly messy lie: `deck-messiness` 0 to 1, default 0.3, and `deck-seed`) and, beside it, a pile. **Next** flips the top card off the deck: it lifts, flies across and lands on the pile at the deck's other side; **Previous** brings it back. Where it lands is a choice: `deck-land="face-up"` (still facing up, the default) or `face-down` ("Turned over": it turns over sideways about its vertical axis, like turning a page, so the pile shows the card's `backImage`, or the package's own navy back when it has none). A card never lands with its picture on its head. The 0.6.0 value `upside-down` is still accepted and means `face-down`. The same card element flies, so a card is never drawn twice; with reduced motion it moves without flying. **Rotate a pile**: a pile's view controls have the angle, tilt and Auto-turn (the other displays are flat), and the whole table, both piles, turns in 3D: seen from behind you see the backs of the deck and the faces of a face-down pile.
|
|
130
|
+
|
|
131
|
+
**The fan.** Every pile fans open and closes with the **Fan** buttons right under the view (or `fan="open"`): up to ten cards spread in an arc like a hand of playing cards so that most of each card shows, and closing puts each card back exactly where it started. The pile beside the deck moves out of the way and the view fits the open fan (a phone included). A card can still be taken off an open fan, and off a pile that was fanned and closed. `view="fan"` is the same pile with only its fan: `fan-size` (2 to 10) cards from the current one, Next moves the window on. The fan is in the address (`?fan=open`).
|
|
120
132
|
|
|
121
|
-
**
|
|
133
|
+
**Animate.** Opening and closing the fan, taking a card off, moving to the next card (Rolodex, Spotlight, the fan), changing a binder page and switching display all move; the wall, story and atlas pan to the card you move to. `animate="off"` turns all of it off (every move then ends at once, in exactly the same place), and `speed="slow"` or `"quick"` makes every move longer or shorter. `animate` is **on** when the attribute is missing, as the flight of a card taken off already was in 0.6.0; set `animate="off"` for the jump you had before. The reader's reduced-motion setting always wins. Moves are CSS transforms and one-shot animations that end by themselves: nothing runs while the view is idle. The Animate and Speed buttons are under the view and in the address (`?animate=off&speed=quick`).
|
|
122
134
|
|
|
123
|
-
**Zoom, Fit and move in every view.** Under each view sit the `@johnmorrisdotca/kazoku-ui` view controls: **−**, **2×**, **+** and **Fit**, and, once zoomed in, four arrows to move the view. Ctrl or Command with the wheel (or a trackpad pinch) and a two-finger pinch zoom about the pointer; zoomed in, a drag or the wheel moves the view, even when the drag starts on a card; the keys `+`, `−` and `0` work with the view focused. **Fit** shows every card the view draws (the whole binder or case page, every card of the wall, the story and the atlas). The wall, story and atlas open at half size or larger so a card can be read, and the others open at Fit. A tap or click on a card still opens it, and zooming, moving or paging never resets the other: turning to the next card or page keeps your zoom, and zooming keeps the card or page you are on. The
|
|
135
|
+
**Zoom, Fit and move in every view.** Under each view sit the `@johnmorrisdotca/kazoku-ui` view controls: **−**, **2×**, **+** and **Fit**, and, once zoomed in, four arrows to move the view. Ctrl or Command with the wheel (or a trackpad pinch) and a two-finger pinch zoom about the pointer; zoomed in, a drag or the wheel moves the view, even when the drag starts on a card; the keys `+`, `−` and `0` work with the view focused. **Fit** shows every card the view draws (the whole binder or case page, every card of the wall, the story and the atlas). The wall, story and atlas open at half size or larger so a card can be read, and the others open at Fit. A tap or click on a card still opens it, and zooming, moving or paging never resets the other: turning to the next card or page keeps your zoom, and zooming keeps the card or page you are on. The piles (see above) also turn in 3D; the other displays use zoom and move only. Zoom itself is not animated. Add the boolean `address` attribute to keep the zoom, the display, the fan, the landing, Animate and Speed in the page address (`?view=fan&fan=open&land=face-down&zoom=2`); a link to it opens the same view. Set `--fuda-stage-height` on the element to change the height of the view (default: the window height less 440 px, between 300 and 620 px) so the controls stay on screen.
|
|
124
136
|
|
|
125
137
|
`@johnmorrisdotca/kazoku-ui` is a runtime dependency of the package (it was a development dependency until 0.2.0). Choice buttons, the light and dark colours and the address are written in the package, because kazoku-ui has no shared part for them yet. A bundler resolves it. On a plain HTML page without a bundler, add an import map next to the module script, with the package served from `node_modules` or a copy of its `dist`:
|
|
126
138
|
|
|
@@ -11,8 +11,11 @@ export declare class FudaCollection extends HTMLElement {
|
|
|
11
11
|
private tiles?;
|
|
12
12
|
private landing?;
|
|
13
13
|
private fanChoice?;
|
|
14
|
+
private animateChoice?;
|
|
15
|
+
private speedChoice?;
|
|
14
16
|
private flip;
|
|
15
17
|
private addressTimer;
|
|
18
|
+
private fanPlaces;
|
|
16
19
|
static get observedAttributes(): string[];
|
|
17
20
|
set cards(value: CollectionCard[]);
|
|
18
21
|
get cards(): CollectionCard[];
|
|
@@ -26,8 +29,10 @@ export declare class FudaCollection extends HTMLElement {
|
|
|
26
29
|
private get land();
|
|
27
30
|
private get fanOpen();
|
|
28
31
|
private get stacked();
|
|
32
|
+
private get piled();
|
|
33
|
+
private get turnable();
|
|
29
34
|
private q;
|
|
30
|
-
/** With `address`, the display, the fan
|
|
35
|
+
/** 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. */
|
|
31
36
|
private readAddress;
|
|
32
37
|
private writeAddress;
|
|
33
38
|
private move;
|
|
@@ -35,12 +40,27 @@ export declare class FudaCollection extends HTMLElement {
|
|
|
35
40
|
private open;
|
|
36
41
|
private renderDialog;
|
|
37
42
|
private canDraw;
|
|
43
|
+
/** The one length every move takes, in a custom property the stylesheet reads: `animate="off"` and reduced motion make it 0, `speed` stretches it. */
|
|
44
|
+
private timeVars;
|
|
45
|
+
private syncChoices;
|
|
38
46
|
private build;
|
|
39
|
-
/** 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. */
|
|
47
|
+
/** 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. */
|
|
40
48
|
private pile;
|
|
49
|
+
/** 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. */
|
|
50
|
+
private fanFor;
|
|
51
|
+
/** 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. */
|
|
52
|
+
private fanVars;
|
|
53
|
+
/** `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. */
|
|
54
|
+
private setFan;
|
|
41
55
|
private renderDeck;
|
|
42
|
-
/** 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. */
|
|
56
|
+
/** 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. */
|
|
43
57
|
private playFlip;
|
|
44
58
|
private renderFan;
|
|
59
|
+
/** Where every card is on screen now, by data index. */
|
|
60
|
+
private snapshot;
|
|
61
|
+
/** 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. */
|
|
62
|
+
private enter;
|
|
63
|
+
/** 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. */
|
|
64
|
+
private play;
|
|
45
65
|
private render;
|
|
46
66
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { collectionViews, parseCollection, randomDrawIndex, movePage } from './collection.js';
|
|
2
|
-
import { deckLandings, fanLayout, flight, normalizeLanding } from './pile.js';
|
|
2
|
+
import { deckLandings, fanBounds, fanLayout, fanShift, flight, normalizeLanding, timing } from './pile.js';
|
|
3
3
|
import { zoomView } from './zoom.js';
|
|
4
4
|
import { createChoice, CHOICE_CSS, CHOICE_VARS } from './choice.js';
|
|
5
5
|
import { stackLayout } from './stack.js';
|
|
6
6
|
const STYLE = `<style>
|
|
7
7
|
:host{display:block;${CHOICE_VARS.dark};color:var(--fc-text);font:16px system-ui}@media(prefers-color-scheme:light){:host(:not([theme="dark"])){${CHOICE_VARS.light}}}:host([theme="light"]){${CHOICE_VARS.light}}:host([theme="dark"]){${CHOICE_VARS.dark}}
|
|
8
|
-
*{box-sizing:border-box}.shell{background:var(--fc-surface);padding:1rem;border-radius:1rem;display:grid;gap:.9rem}.nav{display:flex;gap:.6rem;align-items:center;justify-content:center;flex-wrap:wrap}.opts{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,
|
|
8
|
+
*{box-sizing:border-box}.shell{background:var(--fc-surface);padding:1rem;border-radius:1rem;display:grid;gap:.9rem}.nav{display:flex;gap:.6rem;align-items:center;justify-content:center;flex-wrap:wrap}.opts{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,13.5rem),1fr));gap:.9rem}.opts:not(:has(.choice:not([hidden]))){display:none}.opts .choice{--choice-min:4.6rem}
|
|
9
9
|
button,input{font:inherit;color:inherit}.nav button,dialog button{min-height:48px;min-width:48px;background:var(--fc-btn);border:2px solid var(--fc-btn-border);border-radius:.7rem;padding:.5rem 1rem;cursor:pointer}.nav button:disabled{opacity:.45;cursor:default}.nav input{min-height:48px;background:var(--fc-btn);border:2px solid var(--fc-btn-border);border-radius:.7rem;padding:.5rem .8rem}
|
|
10
10
|
button:focus-visible,input:focus-visible,.card:focus-visible{outline:3px solid var(--fc-focus);outline-offset:3px}${CHOICE_CSS}
|
|
11
11
|
.displays .choice-list{grid-template-columns:repeat(3,minmax(0,1fr))}@media(min-width:340px){.displays .choice-list{grid-template-columns:repeat(4,minmax(0,1fr))}.displays .choice-list button{min-height:50px;padding:.3rem .25rem;font-size:.7rem}.displays .choice-list svg{width:28px;height:18px}}@media(min-width:800px){.displays .choice-list{grid-template-columns:repeat(6,minmax(0,1fr))}.displays .choice-list button{min-height:56px;padding:.45rem .7rem;font-size:.8rem}.displays .choice-list svg{width:34px;height:22px}}
|
|
@@ -18,18 +18,26 @@ button:focus-visible,input:focus-visible,.card:focus-visible{outline:3px solid v
|
|
|
18
18
|
.story{display:flex;flex-direction:column;align-items:center;gap:1rem;padding:1rem}.story .card{width:min(75%,300px);flex:0 0 90%}
|
|
19
19
|
.plane{position:absolute;left:0;top:0;display:grid;gap:18px;grid-template-columns:repeat(var(--atlas-cols,5),140px);padding:30px}.plane .card{height:195px}
|
|
20
20
|
.stage.jukebox{border:12px solid #aa7147;box-shadow:inset 0 0 20px #fbb579}.stage.spotlight{background:radial-gradient(ellipse at center,#e5ce99,#454054 40%,#171822 78%)}
|
|
21
|
-
.stage.deck{perspective:1800px}.v.deck{--w:200px
|
|
22
|
-
.pile .
|
|
21
|
+
.stage:is(.deck,.random,.jukebox,.tower,.fan){perspective:1800px}.v.deck{--w:200px;--gap:56px;--fl:0;--fr:0;--ft:0;--fb:0;--ease:cubic-bezier(.2,.8,.2,1);transform-style:preserve-3d}
|
|
22
|
+
.v.deck .pile,.v.deck .slot{position:absolute;left:50%;top:50%;width:var(--w);aspect-ratio:2.5/3.5;margin:calc(var(--w) * -.7) 0 0 calc(var(--w) * -.5);transform-style:preserve-3d}.slot{border:2px dashed var(--fc-btn-border);border-radius:5%;opacity:.55}
|
|
23
|
+
.v.deck .deck-pile{translate:calc(((var(--fl) - var(--fr) - 1) * var(--w) - var(--gap)) / 2) 0}.v.deck .discard-pile,.v.deck .slot{translate:calc(((var(--fl) + var(--fr) + 1) * var(--w) + var(--gap)) / 2) 0}.v.deck.fanonly .deck-pile{translate:calc((var(--fl) - var(--fr)) * var(--w) / 2) 0}
|
|
24
|
+
.v.deck .deck-pile,.v.deck .discard-pile,.v.deck .slot{transition:translate var(--t,0s) var(--ease)}
|
|
25
|
+
.extent{position:absolute;left:50%;top:50%;width:calc((var(--fl) + var(--fr) + 2) * var(--w) + var(--gap));height:calc((1.4 + var(--ft) + var(--fb)) * var(--w));translate:-50% calc(-50% + (var(--fb) - var(--ft)) * var(--w) / 2);visibility:hidden;pointer-events:none}.fanonly .extent{width:calc((var(--fl) + var(--fr) + 1) * var(--w))}
|
|
26
|
+
.pile .card{position:absolute;inset:0;display:block;transform-style:preserve-3d;transform-origin:50% 50%;transform:translate3d(calc(var(--x,0) * var(--w)),calc(var(--y,0) * var(--w)),calc(var(--k,0) * .9px)) rotate(calc(var(--r,0) * 1deg));transition:transform var(--t,0s) var(--ease),transform-origin var(--t,0s) var(--ease);transition-delay:calc(var(--j,0) * var(--stag,22ms))}.pile .card:not(.top){pointer-events:none}.pile .card.top{cursor:pointer}
|
|
27
|
+
.fan-open .deck-pile .card{transform-origin:50% 165%;transform:translate3d(calc(var(--fx,0) * var(--w)),0,calc(var(--k,0) * .9px)) rotate(calc(var(--fa,0) * 1deg));transition-delay:calc(var(--i,0) * var(--stag,28ms));pointer-events:auto;cursor:pointer}.fan-open .deck-pile .card:hover,.fan-open .deck-pile .card:focus-visible{translate:0 -14px}
|
|
23
28
|
.fly,.turn{position:absolute;inset:0;transform-style:preserve-3d}.face{position:absolute;inset:0;backface-visibility:hidden;-webkit-backface-visibility:hidden;border-radius:5%;overflow:hidden}.face img{display:block;width:100%;height:100%;object-fit:contain;border-radius:3%;box-shadow:0 4px 10px #0008}.face.back{transform:rotateY(180deg)}
|
|
24
29
|
.generic-back{display:grid;place-items:center;background:radial-gradient(ellipse at center,#b78745 0 12%,#152d46 13% 27%,#386988 28% 31%,#10233b 32% 62%,#d2b074 63% 66%,#182c45 67%);border:6px solid #b39158;color:#f7e9bf;font:bold 18px Georgia;letter-spacing:.12em;box-shadow:0 4px 10px #0008}
|
|
25
|
-
.pile.discard[data-land=face-down] .card .turn{transform:rotateY(180deg)}
|
|
26
|
-
.v.fan{--w:190px;display:grid;place-items:end center;padding-bottom:1rem}.fan .card{grid-area:1/1;position:relative;width:var(--w);aspect-ratio:2.5/3.5;height:auto;transform-origin:50% 165%;z-index:var(--z);transform:translate(calc(var(--cx) * var(--w)),calc(var(--cy) * var(--w))) rotate(calc(var(--cr) * 1deg));transition:transform .7s cubic-bezier(.2,.8,.2,1);transition-delay:calc((var(--n) - var(--i)) * 22ms)}.fan.open .card{transform:translate(calc(var(--x) * var(--w)),0) rotate(calc(var(--a) * 1deg));transition-delay:calc(var(--i) * 28ms)}.fan.open .card:hover,.fan.open .card:focus-visible{translate:0 -14px}.fan .card img{border-radius:6%}
|
|
30
|
+
.pile.discard[data-land=face-down] .card .turn{transform:rotateY(180deg)}.ghost{pointer-events:none}
|
|
27
31
|
dialog{color:var(--fc-text);background:var(--fc-dialog);border:1px solid var(--fc-btn-border);border-radius:1rem;text-align:center;max-width:95vw;max-height:95vh}dialog::backdrop{background:#000c}.detail{display:block;height:min(70vh,600px);max-width:80vw;object-fit:contain;margin:1rem auto}.empty{text-align:center;padding:3rem}
|
|
28
|
-
@media(max-width:600px){.stage{height:var(--fuda-stage-height,clamp(220px,calc(100svh - 570px),620px))}}@media(max-width:500px){.grid{grid-template-columns:repeat(min(2,var(--columns,3)),minmax(0,1fr))}.shell{padding:.7rem}.v.deck{--w:150px
|
|
32
|
+
@media(max-width:600px){.stage{height:var(--fuda-stage-height,clamp(220px,calc(100svh - 570px),620px))}}@media(max-width:500px){.opts{grid-template-columns:repeat(2,minmax(0,1fr))}.opts .speed{grid-column:1/-1}.v.deck{--gap:28px}.grid{grid-template-columns:repeat(min(2,var(--columns,3)),minmax(0,1fr))}.shell{padding:.7rem}.v.deck{--w:150px}}@media(prefers-reduced-motion:reduce){*{transition:none!important}}
|
|
29
33
|
</style>`;
|
|
30
|
-
const SHELL = `<section class="shell"><div class="displays"></div><div class="stage" tabindex="0" role="group" aria-label="Collection"><div class="v"></div></div><div class="
|
|
34
|
+
const SHELL = `<section class="shell"><div class="displays"></div><div class="stage" tabindex="0" role="group" aria-label="Collection"><div class="v"></div></div><div class="nav"><button class="prev">← Previous</button><span class="count" aria-live="polite"></span><button class="next">Next →</button><label class="draw-filter" hidden>Filter cards <input type="search" aria-label="Filter cards for random draw"></label><button class="draw" hidden>Draw a card</button></div><div class="opts"></div><dialog aria-label="Card detail"></dialog></section>`;
|
|
31
35
|
const pages = ['binder', 'case'];
|
|
32
36
|
const scrolling = ['wall', 'story', 'atlas'];
|
|
37
|
+
/** The displays that draw a pile of cards: they turn in 3D, fan open and closed, and a card comes off them onto a pile at their side. The Fan of Cards is the same pile with only the fan. */
|
|
38
|
+
const pileViews = ['deck', 'random', 'jukebox', 'tower'];
|
|
39
|
+
/** The displays whose cards sit on top of each other: moving to another card glides the cards that stay and lets the one that leaves go. */
|
|
40
|
+
const gliding = ['rolodex', 'spotlight', 'fan'];
|
|
33
41
|
const rects = (cols, rows, w, h, gx, gy, x0 = 1, y0 = 1) => Array.from({ length: cols * rows }, (_, k) => `<rect x="${x0 + (k % cols) * gx}" y="${y0 + Math.floor(k / cols) * gy}" width="${w}" height="${h}" rx="1" opacity="${0.55 + ((k % cols + Math.floor(k / cols)) % 2) * 0.4}"/>`).join('');
|
|
34
42
|
const ICONS = {
|
|
35
43
|
deck: '<rect x="9" y="4" width="14" height="14" rx="2" opacity=".45"/><rect x="6" y="2" width="14" height="14" rx="2" opacity=".7"/><rect x="3" y="0" width="14" height="14" rx="2"/>',
|
|
@@ -46,8 +54,8 @@ const ICONS = {
|
|
|
46
54
|
fan: [-30, -15, 0, 15, 30].map((a, i) => `<rect x="14" y="1" width="7" height="13" rx="1.2" transform="rotate(${a} 17.5 21)" opacity="${0.45 + i * 0.1}"/>`).join('')
|
|
47
55
|
};
|
|
48
56
|
const icon = (view) => `<svg viewBox="0 0 34 22" aria-hidden="true">${ICONS[view]}</svg>`;
|
|
49
|
-
const
|
|
50
|
-
const
|
|
57
|
+
const ADDRESS_KEYS = ['view', 'fan', 'land', 'animate', 'speed'];
|
|
58
|
+
const NAMES = { land: 'deck-land' };
|
|
51
59
|
export class FudaCollection extends HTMLElement {
|
|
52
60
|
root = this.attachShadow({ mode: 'open' });
|
|
53
61
|
data = [];
|
|
@@ -60,9 +68,12 @@ export class FudaCollection extends HTMLElement {
|
|
|
60
68
|
tiles;
|
|
61
69
|
landing;
|
|
62
70
|
fanChoice;
|
|
71
|
+
animateChoice;
|
|
72
|
+
speedChoice;
|
|
63
73
|
flip = null;
|
|
64
74
|
addressTimer = 0;
|
|
65
|
-
|
|
75
|
+
fanPlaces = [];
|
|
76
|
+
static get observedAttributes() { return ['view', 'page-size', 'columns', 'fan', 'fan-size', 'deck-land', 'deck-messiness', 'deck-seed', 'theme', 'animate', 'speed']; }
|
|
66
77
|
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(); }
|
|
67
78
|
get cards() { return this.data.map(c => ({ ...c, ...(c.metadata ? { metadata: { ...c.metadata } } : {}) })); }
|
|
68
79
|
/** The card on top of the deck, or the first card of the page or fan (counting from 0). */
|
|
@@ -70,24 +81,37 @@ export class FudaCollection extends HTMLElement {
|
|
|
70
81
|
connectedCallback() { if (this.hasAttribute('address'))
|
|
71
82
|
this.readAddress(); this.render(); }
|
|
72
83
|
disconnectedCallback() { clearTimeout(this.addressTimer); }
|
|
73
|
-
attributeChangedCallback(name) {
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
76
|
-
|
|
84
|
+
attributeChangedCallback(name) {
|
|
85
|
+
this.pageSize = Math.min(100, Math.max(1, Math.floor(Number(this.getAttribute('page-size')) || 9)));
|
|
86
|
+
if (name === 'fan' && this.setFan())
|
|
87
|
+
return;
|
|
88
|
+
if (name === 'animate' || name === 'speed') {
|
|
89
|
+
this.timeVars();
|
|
90
|
+
this.syncChoices();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (name === 'view' || name === 'page-size' || name === 'columns') {
|
|
94
|
+
this.index = 0;
|
|
95
|
+
this.flip = null;
|
|
96
|
+
}
|
|
97
|
+
this.render(name === 'view', name === 'view' ? { kind: 'view', dir: 1 } : undefined);
|
|
98
|
+
}
|
|
77
99
|
get currentView() { let v = this.getAttribute('view'); return collectionViews.some(x => x.id === v) ? v : 'deck'; }
|
|
78
100
|
/** 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. */
|
|
79
101
|
get land() { return normalizeLanding(this.getAttribute('deck-land')); }
|
|
80
102
|
get fanOpen() { return this.getAttribute('fan') === 'open'; }
|
|
81
103
|
get stacked() { return ![...pages, ...scrolling].includes(this.currentView); }
|
|
104
|
+
get piled() { return pileViews.includes(this.currentView); }
|
|
105
|
+
get turnable() { return this.piled || this.currentView === 'fan'; }
|
|
82
106
|
q(selector) { return this.root.querySelector(selector); }
|
|
83
|
-
/** With `address`, the display, the fan
|
|
107
|
+
/** 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. */
|
|
84
108
|
readAddress() { try {
|
|
85
109
|
const params = new URLSearchParams(location.search);
|
|
86
110
|
for (const key of ADDRESS_KEYS) {
|
|
87
111
|
const value = params.get(key);
|
|
88
112
|
if (value === null)
|
|
89
113
|
continue;
|
|
90
|
-
const attr = key
|
|
114
|
+
const attr = NAMES[key] ?? key;
|
|
91
115
|
if (this.getAttribute(attr) !== value)
|
|
92
116
|
this.setAttribute(attr, value);
|
|
93
117
|
}
|
|
@@ -96,7 +120,7 @@ export class FudaCollection extends HTMLElement {
|
|
|
96
120
|
writeAddress() { if (!this.hasAttribute('address'))
|
|
97
121
|
return; clearTimeout(this.addressTimer); this.addressTimer = window.setTimeout(() => { try {
|
|
98
122
|
const url = new URL(location.href);
|
|
99
|
-
const want = { view: this.currentView === 'deck' ? null : this.currentView, fan: this.fanOpen ? 'open' : null, land: this.land === 'face-up' ? null : this.land };
|
|
123
|
+
const want = { view: this.currentView === 'deck' ? null : this.currentView, fan: this.fanOpen ? 'open' : null, land: this.land === 'face-up' ? null : this.land, animate: this.getAttribute('animate') === 'off' ? 'off' : null, speed: ['slow', 'quick'].includes(this.getAttribute('speed') ?? '') ? this.getAttribute('speed') : null };
|
|
100
124
|
for (const [k, v] of Object.entries(want)) {
|
|
101
125
|
if (v === null)
|
|
102
126
|
url.searchParams.delete(k);
|
|
@@ -115,12 +139,12 @@ export class FudaCollection extends HTMLElement {
|
|
|
115
139
|
}
|
|
116
140
|
else {
|
|
117
141
|
const next = (this.index + n + this.data.length) % this.data.length;
|
|
118
|
-
this.flip =
|
|
142
|
+
this.flip = this.piled && Math.abs(n) === 1 && next === this.index + n ? (n > 0 ? 'off' : 'back') : null;
|
|
119
143
|
this.index = next;
|
|
120
144
|
}
|
|
121
|
-
this.render();
|
|
145
|
+
this.render(false, { kind: 'move', dir: n > 0 ? 1 : -1 });
|
|
122
146
|
}
|
|
123
|
-
image(c) { let button = document.createElement('button'); button.className = 'card'; button.setAttribute('aria-label', `Inspect ${c.name}`); let img = document.createElement('img'); img.src = c.image; img.alt = c.name; img.loading = 'lazy'; img.onerror = () => { img.replaceWith(document.createTextNode(`Image unavailable: ${c.name}`)); }; button.append(img); button.onclick = () => this.open(c); return button; }
|
|
147
|
+
image(c, at) { let button = document.createElement('button'); button.className = 'card'; button.dataset.i = String(at); button.setAttribute('aria-label', `Inspect ${c.name}`); let img = document.createElement('img'); img.src = c.image; img.alt = c.name; img.loading = 'lazy'; img.onerror = () => { img.replaceWith(document.createTextNode(`Image unavailable: ${c.name}`)); }; button.append(img); button.onclick = () => this.open(c); return button; }
|
|
124
148
|
open(c) { this.selected = c; this.back = false; this.renderDialog(); }
|
|
125
149
|
renderDialog() { let dlg = this.q('dialog'); dlg.replaceChildren(); let close = document.createElement('button'); close.textContent = 'Close'; close.onclick = () => dlg.close(); dlg.append(close); if (this.selected) {
|
|
126
150
|
let img = document.createElement('img');
|
|
@@ -139,48 +163,62 @@ export class FudaCollection extends HTMLElement {
|
|
|
139
163
|
} if (!dlg.open)
|
|
140
164
|
dlg.showModal(); }
|
|
141
165
|
canDraw() { const query = this.drawFilter.trim().toLocaleLowerCase(); return this.data.some(c => c.name.toLocaleLowerCase().includes(query)); }
|
|
166
|
+
/** The one length every move takes, in a custom property the stylesheet reads: `animate="off"` and reduced motion make it 0, `speed` stretches it. */
|
|
167
|
+
timeVars() { const t = timing(this, true), shell = this.root.querySelector('.shell'); if (!shell)
|
|
168
|
+
return t; shell.style.setProperty('--t', t.on ? `${t.ms}ms` : '0s'); if (t.on)
|
|
169
|
+
shell.style.removeProperty('--stag');
|
|
170
|
+
else
|
|
171
|
+
shell.style.setProperty('--stag', '0ms'); return t; }
|
|
172
|
+
syncChoices() { this.animateChoice?.set(this.getAttribute('animate') === 'off' ? 'off' : 'on'); this.speedChoice?.set(['slow', 'quick'].includes(this.getAttribute('speed') ?? '') ? this.getAttribute('speed') : 'normal'); }
|
|
142
173
|
build() {
|
|
143
174
|
if (this.zoom)
|
|
144
175
|
return;
|
|
145
176
|
this.root.innerHTML = STYLE + SHELL;
|
|
177
|
+
const keep = (attr, value) => { if (value === null)
|
|
178
|
+
this.removeAttribute(attr);
|
|
179
|
+
else
|
|
180
|
+
this.setAttribute(attr, value); this.writeAddress(); };
|
|
146
181
|
this.tiles = createChoice({ label: 'Display', layout: 'tiles', value: this.currentView, options: collectionViews.map(v => ({ value: v.id, label: v.label, icon: icon(v.id) })), onChange: value => { this.setAttribute('view', value); this.writeAddress(); } });
|
|
147
182
|
this.q('.displays').append(this.tiles.element);
|
|
148
183
|
this.landing = createChoice({ label: 'A card taken off lands', value: this.land, options: deckLandings.map(l => ({ value: l.id, label: l.label })), onChange: value => { this.setAttribute('deck-land', value); this.writeAddress(); } });
|
|
149
|
-
this.fanChoice = createChoice({ label: 'Fan', value: this.fanOpen ? 'open' : 'closed', options: [{ value: 'closed', label: 'Closed
|
|
184
|
+
this.fanChoice = createChoice({ label: 'Fan', value: this.fanOpen ? 'open' : 'closed', options: [{ value: 'closed', label: 'Closed' }, { value: 'open', label: 'Open' }], onChange: value => { if (value === 'open')
|
|
150
185
|
this.setAttribute('fan', 'open');
|
|
151
186
|
else
|
|
152
187
|
this.removeAttribute('fan'); this.writeAddress(); } });
|
|
153
|
-
this.
|
|
188
|
+
this.animateChoice = createChoice({ label: 'Animate', value: 'on', options: [{ value: 'on', label: 'On' }, { value: 'off', label: 'Off' }], onChange: value => keep('animate', value === 'off' ? 'off' : 'on') });
|
|
189
|
+
this.speedChoice = createChoice({ label: 'Speed', value: 'normal', options: [{ value: 'slow', label: 'Slow' }, { value: 'normal', label: 'Normal' }, { value: 'quick', label: 'Quick' }], onChange: value => keep('speed', value) });
|
|
190
|
+
this.q('.opts').append(this.landing.element, this.fanChoice.element, this.animateChoice.element, this.speedChoice.element);
|
|
191
|
+
this.speedChoice.element.classList.add('speed');
|
|
154
192
|
this.q('.prev').onclick = () => this.move(-1);
|
|
155
193
|
this.q('.next').onclick = () => this.move(1);
|
|
156
194
|
const input = this.q('.draw-filter input'), draw = this.q('.draw');
|
|
157
195
|
input.oninput = () => { this.drawFilter = input.value; draw.disabled = !this.canDraw(); };
|
|
158
196
|
draw.onclick = () => { const next = randomDrawIndex(this.data, this.index, this.drawFilter); if (next === null)
|
|
159
|
-
return; this.index = next; this.render(); };
|
|
197
|
+
return; this.index = next; this.render(false, { kind: 'move', dir: 1 }); };
|
|
160
198
|
const stage = this.q('.stage');
|
|
161
199
|
this.zoom = zoomView(stage, this.q('.v'), { address: this.hasAttribute('address'), items: '.card,.slot', theme: this.getAttribute('theme') ?? undefined });
|
|
162
200
|
let startX = 0;
|
|
163
201
|
stage.addEventListener('touchstart', e => { if (e.touches.length === 1)
|
|
164
202
|
startX = e.touches[0].clientX; }, { passive: true });
|
|
165
|
-
stage.addEventListener('touchend', e => { if (!this.stacked || this.
|
|
203
|
+
stage.addEventListener('touchend', e => { if (!this.stacked || this.piled || this.zoom.zoomed() || e.changedTouches.length !== 1)
|
|
166
204
|
return; const dx = e.changedTouches[0].clientX - startX; if (Math.abs(dx) > 45) {
|
|
167
205
|
if (e.cancelable)
|
|
168
206
|
e.preventDefault();
|
|
169
207
|
this.move(dx < 0 ? 1 : -1);
|
|
170
208
|
} }, { passive: false });
|
|
171
|
-
stage.addEventListener('keydown', (e) => { if (!this.stacked || this.
|
|
209
|
+
stage.addEventListener('keydown', (e) => { if (!this.stacked || this.piled || (e.key !== 'ArrowRight' && e.key !== 'ArrowLeft'))
|
|
172
210
|
return; e.preventDefault(); this.move(e.key === 'ArrowRight' ? 1 : -1); this.q('.stack .card:last-child')?.focus(); });
|
|
173
211
|
}
|
|
174
|
-
/** 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. */
|
|
175
|
-
pile(kind, positions) {
|
|
212
|
+
/** 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. */
|
|
213
|
+
pile(kind, positions, fan = []) {
|
|
176
214
|
const pile = document.createElement('div');
|
|
177
215
|
pile.className = `pile ${kind}-pile${kind === 'discard' ? ' discard' : ''}`;
|
|
178
216
|
pile.dataset.land = this.land;
|
|
179
217
|
const messy = Math.min(1, Math.max(0, Number(this.getAttribute('deck-messiness') ?? 0.3)));
|
|
180
218
|
const seed = Number(this.getAttribute('deck-seed') ?? 1) || 1;
|
|
181
|
-
const depth = Math.min(
|
|
219
|
+
const depth = Math.min(positions.length - 1, fan.length ? fan.length - 1 : 9);
|
|
182
220
|
const shown = positions.slice(positions.length - 1 - depth);
|
|
183
|
-
const places = stackLayout(positions.length, { messiness: messy, seed, depth
|
|
221
|
+
const places = stackLayout(positions.length, { messiness: messy, seed, depth });
|
|
184
222
|
shown.forEach((at, j) => {
|
|
185
223
|
const top = j === shown.length - 1, c = this.data[at], place = places[j];
|
|
186
224
|
const el = document.createElement('div');
|
|
@@ -192,6 +230,13 @@ export class FudaCollection extends HTMLElement {
|
|
|
192
230
|
el.style.setProperty('--r', String(place.rotate));
|
|
193
231
|
}
|
|
194
232
|
el.style.setProperty('--k', String(j));
|
|
233
|
+
el.style.setProperty('--j', String(shown.length - 1 - j));
|
|
234
|
+
const f = fan[shown.length - 1 - j];
|
|
235
|
+
if (f) {
|
|
236
|
+
el.style.setProperty('--fx', String(f.x));
|
|
237
|
+
el.style.setProperty('--fa', String(f.angle));
|
|
238
|
+
el.style.setProperty('--i', String(shown.length - 1 - j));
|
|
239
|
+
}
|
|
195
240
|
const fly = document.createElement('div');
|
|
196
241
|
fly.className = 'fly';
|
|
197
242
|
const turn = document.createElement('div');
|
|
@@ -220,11 +265,11 @@ export class FudaCollection extends HTMLElement {
|
|
|
220
265
|
turn.append(front, back);
|
|
221
266
|
fly.append(turn);
|
|
222
267
|
el.append(fly);
|
|
268
|
+
el.onclick = () => this.open(c);
|
|
223
269
|
if (top) {
|
|
224
270
|
el.setAttribute('role', 'button');
|
|
225
|
-
el.tabIndex = 0;
|
|
226
271
|
el.setAttribute('aria-label', `Inspect ${c.name}`);
|
|
227
|
-
el.
|
|
272
|
+
el.tabIndex = 0;
|
|
228
273
|
el.onkeydown = e => { if (e.key === 'Enter' || e.key === ' ') {
|
|
229
274
|
e.preventDefault();
|
|
230
275
|
this.open(c);
|
|
@@ -234,60 +279,133 @@ export class FudaCollection extends HTMLElement {
|
|
|
234
279
|
});
|
|
235
280
|
return pile;
|
|
236
281
|
}
|
|
237
|
-
|
|
282
|
+
/** 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. */
|
|
283
|
+
fanFor(count) { const narrow = (this.clientWidth || innerWidth) < 520; return fanLayout(count, Number(this.getAttribute('deck-seed') ?? 1) || 1, narrow ? 0.3 : 0.34); }
|
|
284
|
+
/** 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. */
|
|
285
|
+
fanVars(v, open) {
|
|
286
|
+
const b = open && this.fanPlaces.length > 1 ? fanBounds(this.fanPlaces) : { left: 0, right: 0, top: 0, bottom: 0 };
|
|
287
|
+
v.classList.toggle('fan-open', open && this.fanPlaces.length > 1);
|
|
288
|
+
for (const [k, x] of [['--fl', b.left], ['--fr', b.right], ['--ft', b.top], ['--fb', b.bottom]])
|
|
289
|
+
v.style.setProperty(k, String(x));
|
|
290
|
+
}
|
|
291
|
+
/** `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. */
|
|
292
|
+
setFan() {
|
|
293
|
+
if (!this.zoom || !(this.piled || this.currentView === 'fan'))
|
|
294
|
+
return false;
|
|
295
|
+
const v = this.q('.v');
|
|
296
|
+
if (!v.classList.contains('deck') || !this.data.length)
|
|
297
|
+
return false;
|
|
298
|
+
const t = this.timeVars();
|
|
299
|
+
this.zoom.animated(() => { this.fanVars(v, this.fanOpen); this.zoom.refresh(); }, t.on ? t.ms : 0);
|
|
300
|
+
this.syncChoices();
|
|
301
|
+
this.fanChoice.set(this.fanOpen ? 'open' : 'closed');
|
|
302
|
+
this.writeAddress();
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
renderDeck(v, fanOnly = false) {
|
|
238
306
|
const n = this.data.length, deckPositions = [], discardPositions = [];
|
|
239
|
-
// 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.
|
|
240
|
-
for (let p = 0; p < n - this.index; p++)
|
|
241
|
-
deckPositions.push(n - 1 - p);
|
|
242
|
-
for (let p = 0; p < this.index; p++)
|
|
243
|
-
discardPositions.push(p);
|
|
244
307
|
v.classList.add('deck');
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
308
|
+
if (fanOnly) {
|
|
309
|
+
const size = Math.min(10, Math.max(2, Math.floor(Number(this.getAttribute('fan-size')) || 10))), count = Math.min(size, n);
|
|
310
|
+
// The fan window: `count` cards from the first one, wrapping round; the top card is the first and lies on the left when open.
|
|
311
|
+
for (let p = count - 1; p >= 0; p--)
|
|
312
|
+
deckPositions.push((this.index + p) % n);
|
|
313
|
+
v.classList.add('fanonly');
|
|
314
|
+
this.fanPlaces = this.fanFor(count);
|
|
315
|
+
v.append(this.pile('deck', deckPositions, this.fanPlaces));
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
// 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.
|
|
319
|
+
for (let p = 0; p < n - this.index; p++)
|
|
320
|
+
deckPositions.push(n - 1 - p);
|
|
321
|
+
for (let p = 0; p < this.index; p++)
|
|
322
|
+
discardPositions.push(p);
|
|
323
|
+
this.fanPlaces = this.fanFor(Math.min(10, deckPositions.length));
|
|
324
|
+
const slot = () => { const s = document.createElement('div'); s.className = 'slot'; return s; };
|
|
325
|
+
// 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.
|
|
326
|
+
v.append(deckPositions.length ? this.pile('deck', deckPositions, this.fanPlaces) : slot(), discardPositions.length ? this.pile('discard', discardPositions) : slot());
|
|
327
|
+
}
|
|
328
|
+
const extent = document.createElement('div');
|
|
329
|
+
extent.className = 'extent';
|
|
330
|
+
v.append(extent);
|
|
331
|
+
this.fanVars(v, this.fanOpen);
|
|
251
332
|
}
|
|
252
|
-
/** 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. */
|
|
253
|
-
playFlip(direction, v) {
|
|
333
|
+
/** 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. */
|
|
334
|
+
playFlip(direction, v, ms) {
|
|
254
335
|
const deck = v.querySelector('.deck-pile'), discard = v.querySelector('.discard-pile');
|
|
255
336
|
const moved = direction === 'off' ? discard?.querySelector('.card.top') : deck?.querySelector('.card.top');
|
|
256
337
|
if (!moved || !deck || !discard)
|
|
257
338
|
return;
|
|
258
|
-
const
|
|
339
|
+
const style = getComputedStyle(v), w = parseFloat(style.getPropertyValue('--w')) || 200, gap = parseFloat(style.getPropertyValue('--gap')) || 56, fr = parseFloat(v.style.getPropertyValue('--fr')) || 0;
|
|
340
|
+
const dx = (1 + fr) * w + gap, dy = 0;
|
|
259
341
|
const fly = moved.querySelector('.fly'), turn = moved.querySelector('.turn');
|
|
260
|
-
const
|
|
261
|
-
|
|
342
|
+
const lead = this.fanOpen && this.fanPlaces.length > 1 ? this.fanPlaces[0] : null, shift = lead ? fanShift(lead) : { x: 0, y: 0 };
|
|
343
|
+
const away = { x: shift.x * w, y: shift.y * w }, spin = lead ? lead.angle : 0;
|
|
344
|
+
// Taking a card off an open fan starts it where the fan holds it; bringing it back ends there.
|
|
345
|
+
const from = direction === 'off' ? { x: -dx + away.x, y: -dy + away.y } : { x: dx - away.x, y: dy - away.y };
|
|
346
|
+
flight(fly, turn, from, this.land, direction, ms, direction === 'off' ? spin : -spin);
|
|
262
347
|
}
|
|
263
|
-
renderFan(v) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
348
|
+
renderFan(v) { this.renderDeck(v, true); }
|
|
349
|
+
/** Where every card is on screen now, by data index. */
|
|
350
|
+
snapshot(v) { const m = new Map(); v.querySelectorAll('[data-i]:not(.ghost)').forEach(el => m.set(el.dataset.i, el.getBoundingClientRect())); return m; }
|
|
351
|
+
/** 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. */
|
|
352
|
+
enter(els, ms, dir = 0, stagger = 18) { let k = 0; for (const el of els)
|
|
353
|
+
el.animate([{ opacity: 0, translate: `${dir * 36}px 0`, scale: .94, offset: 0 }], { duration: ms, delay: Math.min(k++, 12) * stagger, easing: 'cubic-bezier(.2,.8,.2,1)', fill: 'backwards' }); }
|
|
354
|
+
/** 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. */
|
|
355
|
+
play(how, before, left, view, v, ms) {
|
|
356
|
+
if (how.kind === 'view') {
|
|
357
|
+
this.enter(Array.from(v.querySelectorAll('.card,.slot')), ms * 0.7);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (pages.includes(view)) {
|
|
361
|
+
this.enter(Array.from(v.querySelectorAll('.card')), ms * 0.7, how.dir);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (scrolling.includes(view)) {
|
|
365
|
+
const el = v.querySelector(`[data-i="${this.index}"]`);
|
|
366
|
+
if (el)
|
|
367
|
+
this.zoom.reveal(el, ms);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (!gliding.includes(view) || this.zoom.turned())
|
|
371
|
+
return;
|
|
372
|
+
const scale = v.offsetWidth ? v.getBoundingClientRect().width / v.offsetWidth : 1;
|
|
373
|
+
const kept = new Set();
|
|
374
|
+
for (const el of Array.from(v.querySelectorAll('.card[data-i]'))) {
|
|
375
|
+
const was = before.get(el.dataset.i), now = el.getBoundingClientRect();
|
|
376
|
+
if (!was || !now.width) {
|
|
377
|
+
this.enter([el], ms * 0.7, how.dir);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
kept.add(el.dataset.i);
|
|
381
|
+
const dx = (was.left + was.width / 2 - now.left - now.width / 2) / scale, dy = (was.top + was.height / 2 - now.top - now.height / 2) / scale;
|
|
382
|
+
el.animate([{ translate: `${dx}px ${dy}px`, scale: was.width / now.width, offset: 0 }], { duration: ms, easing: 'cubic-bezier(.2,.8,.2,1)' });
|
|
383
|
+
}
|
|
384
|
+
const host = v.querySelector('.deck-pile') ?? v;
|
|
385
|
+
for (const old of left) {
|
|
386
|
+
if (kept.has(old.dataset.i))
|
|
387
|
+
continue;
|
|
388
|
+
old.classList.add('ghost');
|
|
389
|
+
old.removeAttribute('role');
|
|
390
|
+
old.tabIndex = -1;
|
|
391
|
+
host.append(old);
|
|
392
|
+
const done = () => old.remove();
|
|
393
|
+
old.animate([{ opacity: 1, translate: '0 0', offset: 0 }, { opacity: 0, translate: `${-how.dir * 60}px 0`, offset: 1 }], { duration: ms * 0.8, easing: 'ease-in' }).finished.then(done, done);
|
|
394
|
+
}
|
|
280
395
|
}
|
|
281
|
-
render(reset = false) {
|
|
396
|
+
render(reset = false, how) {
|
|
282
397
|
if (!this.isConnected)
|
|
283
398
|
return;
|
|
284
399
|
this.build();
|
|
285
400
|
let view = this.currentView, stage = this.q('.stage'), v = this.q('.v');
|
|
401
|
+
const t = this.timeVars(), playing = t.on && how && this.data.length > 0;
|
|
402
|
+
const before = playing ? this.snapshot(v) : new Map(), left = playing && how.kind === 'move' && gliding.includes(view) ? Array.from(v.querySelectorAll('.card:not(.ghost)')) : [];
|
|
286
403
|
this.tiles.set(view);
|
|
287
404
|
this.landing.set(this.land);
|
|
288
405
|
this.fanChoice.set(this.fanOpen ? 'open' : 'closed');
|
|
289
|
-
this.
|
|
290
|
-
this.
|
|
406
|
+
this.syncChoices();
|
|
407
|
+
this.landing.hidden = !this.piled;
|
|
408
|
+
this.fanChoice.hidden = !(this.piled || view === 'fan');
|
|
291
409
|
this.q('.count').textContent = this.data.length ? (pages.includes(view) ? `Page ${Math.floor(this.index / this.pageSize) + 1} / ${Math.ceil(this.data.length / this.pageSize)}` : `${this.index + 1} / ${this.data.length}`) : 'No cards';
|
|
292
410
|
for (const selector of ['.prev', '.next'])
|
|
293
411
|
this.q(selector).disabled = !this.data.length;
|
|
@@ -300,12 +418,15 @@ export class FudaCollection extends HTMLElement {
|
|
|
300
418
|
v.className = 'v';
|
|
301
419
|
v.replaceChildren();
|
|
302
420
|
v.style.removeProperty('--columns');
|
|
421
|
+
for (const k of ['--fl', '--fr', '--ft', '--fb'])
|
|
422
|
+
v.style.removeProperty(k);
|
|
303
423
|
this.zoom.minOpen = scrolling.includes(view) ? .5 : 0;
|
|
304
|
-
this.zoom.setTurnable(
|
|
424
|
+
this.zoom.setTurnable(this.turnable && this.data.length > 0);
|
|
425
|
+
this.zoom.items = this.piled || view === 'fan' ? '.extent' : '.card,.slot';
|
|
305
426
|
if (!this.data.length) {
|
|
306
427
|
v.innerHTML = '<p class="empty">No cards in this collection.</p>';
|
|
307
428
|
}
|
|
308
|
-
else if (
|
|
429
|
+
else if (this.piled)
|
|
309
430
|
this.renderDeck(v);
|
|
310
431
|
else if (view === 'fan')
|
|
311
432
|
this.renderFan(v);
|
|
@@ -314,32 +435,38 @@ export class FudaCollection extends HTMLElement {
|
|
|
314
435
|
const columns = Math.min(10, Math.max(1, Math.floor(Number(this.getAttribute('columns')) || 3)));
|
|
315
436
|
v.style.setProperty('--columns', String(columns));
|
|
316
437
|
let start = Math.floor(this.index / this.pageSize) * this.pageSize;
|
|
317
|
-
this.data.slice(start, start + this.pageSize).forEach(c => v.append(this.image(c)));
|
|
438
|
+
this.data.slice(start, start + this.pageSize).forEach((c, k) => v.append(this.image(c, start + k)));
|
|
318
439
|
}
|
|
319
440
|
else if (view === 'wall' || view === 'story') {
|
|
320
441
|
v.classList.add(view);
|
|
321
|
-
this.data.forEach(c => v.append(this.image(c)));
|
|
442
|
+
this.data.forEach((c, k) => v.append(this.image(c, k)));
|
|
322
443
|
}
|
|
323
444
|
else if (view === 'atlas') {
|
|
324
445
|
v.classList.add('atlas');
|
|
325
446
|
let plane = document.createElement('div');
|
|
326
447
|
plane.className = 'plane';
|
|
327
|
-
this.data.forEach(c => plane.append(this.image(c)));
|
|
448
|
+
this.data.forEach((c, k) => plane.append(this.image(c, k)));
|
|
328
449
|
v.append(plane);
|
|
329
450
|
}
|
|
330
451
|
else {
|
|
331
452
|
v.classList.add('stack', view);
|
|
332
453
|
let count = Math.min(3, this.data.length);
|
|
333
454
|
for (let d = count - 1; d >= 0; d--) {
|
|
334
|
-
|
|
455
|
+
const at = (this.index + d) % this.data.length;
|
|
456
|
+
let c = this.data[at], el = this.image(c, at);
|
|
335
457
|
let offset = view === 'rolodex' ? d * 42 : view === 'spotlight' ? d * 70 : d * 14;
|
|
336
|
-
let rotation = view === 'rolodex' ? d * 12 :
|
|
458
|
+
let rotation = view === 'rolodex' ? d * 12 : d * 5;
|
|
337
459
|
el.style.transform = `translateX(calc(-50% + ${offset}px)) rotate(${rotation}deg) scale(${1 - d * .08})`;
|
|
338
460
|
el.style.opacity = String(1 - d * .2);
|
|
339
461
|
v.append(el);
|
|
340
462
|
}
|
|
341
463
|
}
|
|
342
464
|
this.zoom.refresh(reset);
|
|
465
|
+
if (this.flip && this.piled && t.on && this.data.length > 1)
|
|
466
|
+
this.playFlip(this.flip, v, Math.round(t.ms * 1.15));
|
|
467
|
+
this.flip = null;
|
|
468
|
+
if (playing)
|
|
469
|
+
this.play(how, before, left, view, v, t.ms);
|
|
343
470
|
}
|
|
344
471
|
}
|
|
345
472
|
if (!customElements.get('fuda-collection'))
|
package/dist/element.d.ts
CHANGED
|
@@ -22,6 +22,25 @@ export declare class FudaCard extends HTMLElement {
|
|
|
22
22
|
get stackCount(): number;
|
|
23
23
|
/** Height of the stack in millimetres: the count times a card's thickness. */
|
|
24
24
|
get stackHeight(): number;
|
|
25
|
+
/** How many cards are still in the stack: `stack` less the `taken-off` ones (at least one stays). */
|
|
26
|
+
get heldCount(): number;
|
|
27
|
+
/** True while the stack is fanned open (`fan="open"`, with more than one card). */
|
|
28
|
+
get fanOpen(): boolean;
|
|
29
|
+
/** How far the pile reaches beyond the card's own box, in card widths: an open fan and the pile of cards taken off. It is zero until a pile feature is used. Pages use it to fit the view; `fuda-pile` is sent when it changes. */
|
|
30
|
+
pileBounds: {
|
|
31
|
+
left: number;
|
|
32
|
+
right: number;
|
|
33
|
+
top: number;
|
|
34
|
+
bottom: number;
|
|
35
|
+
};
|
|
36
|
+
/** Take the top card (or `count` cards) off the stack: it flies to a pile at the stack's side and lands as `deck-land` says. At least one card stays. */
|
|
37
|
+
takeOff(count?: number): void;
|
|
38
|
+
/** Bring the card (or `count` cards) back from the pile onto the stack. */
|
|
39
|
+
putBack(count?: number): void;
|
|
40
|
+
pileUsed: boolean;
|
|
41
|
+
private pileModule?;
|
|
42
|
+
/** Fanning, the pile of cards taken off and the controls are drawn by a module that is loaded only when a stack asks for them; a card without them never loads it. */
|
|
43
|
+
private applyPile;
|
|
25
44
|
private wearKey;
|
|
26
45
|
private wearToken;
|
|
27
46
|
private wearModule?;
|
package/dist/element.js
CHANGED
|
@@ -11,7 +11,7 @@ const reducedMotion = () => typeof matchMedia === 'function' && matchMedia('(pre
|
|
|
11
11
|
document.addEventListener('pointerdown', event => { if (focusedCard && !event.composedPath().includes(focusedCard))
|
|
12
12
|
focusedCard.setFocused(false); }, true);
|
|
13
13
|
export class FudaCard extends HTMLElement {
|
|
14
|
-
static observedAttributes = ['title', 'category', 'subtitle', 'description', 'number', 'rarity', 'motion', 'playing', 'thickness', 'autoplay', 'material', 'stack', 'stack-messiness', 'stack-seed', 'stack-depth', 'wear', 'wear-seed', 'wear-corners', 'wear-edges', 'wear-surface', 'wear-creases'];
|
|
14
|
+
static observedAttributes = ['title', 'category', 'subtitle', 'description', 'number', 'rarity', 'motion', 'playing', 'thickness', 'autoplay', 'material', 'stack', 'stack-messiness', 'stack-seed', 'stack-depth', 'wear', 'wear-seed', 'wear-corners', 'wear-edges', 'wear-surface', 'wear-creases', 'fan', 'taken-off', 'deck-land', 'animate', 'speed', 'pile-controls'];
|
|
15
15
|
focused = false;
|
|
16
16
|
hushed = false;
|
|
17
17
|
focusedAtPress;
|
|
@@ -97,6 +97,35 @@ export class FudaCard extends HTMLElement {
|
|
|
97
97
|
get stackCount() { const n = Math.floor(Number.parseFloat(this.getAttribute('stack') ?? '')); return n >= 1 ? Math.min(1000, n) : 1; }
|
|
98
98
|
/** Height of the stack in millimetres: the count times a card's thickness. */
|
|
99
99
|
get stackHeight() { return stackHeightMm(this.stackCount, this.cardThicknessMm); }
|
|
100
|
+
/** How many cards are still in the stack: `stack` less the `taken-off` ones (at least one stays). */
|
|
101
|
+
get heldCount() { const n = this.stackCount, t = Math.floor(Number.parseFloat(this.getAttribute('taken-off') ?? '')); return t > 0 ? Math.max(1, n - t) : n; }
|
|
102
|
+
/** True while the stack is fanned open (`fan="open"`, with more than one card). */
|
|
103
|
+
get fanOpen() { return this.getAttribute('fan') === 'open' && this.heldCount > 1; }
|
|
104
|
+
/** How far the pile reaches beyond the card's own box, in card widths: an open fan and the pile of cards taken off. It is zero until a pile feature is used. Pages use it to fit the view; `fuda-pile` is sent when it changes. */
|
|
105
|
+
pileBounds = { left: 0, right: 0, top: 0, bottom: 0 };
|
|
106
|
+
/** Take the top card (or `count` cards) off the stack: it flies to a pile at the stack's side and lands as `deck-land` says. At least one card stays. */
|
|
107
|
+
takeOff(count = 1) { const t = Math.max(0, Math.floor(Number.parseFloat(this.getAttribute('taken-off') ?? '')) || 0); this.setAttribute('taken-off', String(Math.min(this.stackCount - 1, t + Math.max(1, count)))); }
|
|
108
|
+
/** Bring the card (or `count` cards) back from the pile onto the stack. */
|
|
109
|
+
putBack(count = 1) { const t = Math.max(0, Math.floor(Number.parseFloat(this.getAttribute('taken-off') ?? '')) || 0); if (t - Math.max(1, count) > 0)
|
|
110
|
+
this.setAttribute('taken-off', String(t - Math.max(1, count)));
|
|
111
|
+
else
|
|
112
|
+
this.removeAttribute('taken-off'); }
|
|
113
|
+
pileUsed = false;
|
|
114
|
+
pileModule;
|
|
115
|
+
/** Fanning, the pile of cards taken off and the controls are drawn by a module that is loaded only when a stack asks for them; a card without them never loads it. */
|
|
116
|
+
applyPile() {
|
|
117
|
+
const asked = this.stackCount > 1 && (this.getAttribute('fan') === 'open' || this.hasAttribute('taken-off') || this.hasAttribute('pile-controls'));
|
|
118
|
+
if (!asked && !this.pileModule)
|
|
119
|
+
return;
|
|
120
|
+
if (asked)
|
|
121
|
+
this.pileUsed = true;
|
|
122
|
+
const draw = (m) => { if (!this.shadowRoot)
|
|
123
|
+
return; this.pileModule = m; m.applyPile(this, this.shadowRoot); };
|
|
124
|
+
if (this.pileModule)
|
|
125
|
+
draw(this.pileModule);
|
|
126
|
+
else
|
|
127
|
+
void import('./pile-card.js').then(draw);
|
|
128
|
+
}
|
|
100
129
|
wearKey = '';
|
|
101
130
|
wearToken = 0;
|
|
102
131
|
wearModule;
|
|
@@ -127,7 +156,7 @@ export class FudaCard extends HTMLElement {
|
|
|
127
156
|
drawStills(card, count) {
|
|
128
157
|
const num = (name, fallback) => { const n = Number.parseFloat(this.getAttribute(name) ?? ''); return Number.isFinite(n) ? n : fallback; };
|
|
129
158
|
const messiness = num('stack-messiness', 0.3), seed = num('stack-seed', 1), depth = Math.min(52, Math.max(0, Math.floor(num('stack-depth', 10))));
|
|
130
|
-
const places = count > 1 && messiness > 0 ? stackLayout(count, { messiness, seed, depth }) : [];
|
|
159
|
+
const places = count > 1 && (messiness > 0 || this.pileUsed) ? stackLayout(count, { messiness, seed, depth }) : [];
|
|
131
160
|
const key = places.length ? JSON.stringify([count, messiness, seed, depth]) : '';
|
|
132
161
|
if (key === this.stillsKey)
|
|
133
162
|
return;
|
|
@@ -137,16 +166,16 @@ export class FudaCard extends HTMLElement {
|
|
|
137
166
|
holder.innerHTML = places.map((p, at) => `<i class="still" style="--x:${p.x};--y:${p.y};--r:${p.rotate};--k:${count - places.length + at}"></i>`).join('');
|
|
138
167
|
}
|
|
139
168
|
render() { const card = this.shadowRoot?.querySelector('.card'); if (card) {
|
|
140
|
-
const mm = this.cardThicknessMm, count = this.
|
|
141
|
-
card.style.setProperty('--mm', String(mm * count));
|
|
169
|
+
const mm = this.cardThicknessMm, count = this.heldCount, fan = this.fanOpen, material = cardMaterial(this.getAttribute('material'));
|
|
170
|
+
card.style.setProperty('--mm', String(fan ? mm : mm * count));
|
|
142
171
|
card.style.setProperty('--mm1', String(mm));
|
|
143
|
-
card.classList.toggle('stacked', count > 1);
|
|
172
|
+
card.classList.toggle('stacked', count > 1 && !fan);
|
|
144
173
|
if (material)
|
|
145
174
|
card.style.setProperty('--edge', material.edge);
|
|
146
175
|
else
|
|
147
176
|
card.style.removeProperty('--edge');
|
|
148
177
|
this.drawStills(card, count);
|
|
149
|
-
} this.applyWear(); const button = this.shadowRoot?.querySelector('.motion-button'); if (button) {
|
|
178
|
+
} this.applyWear(); this.applyPile(); const button = this.shadowRoot?.querySelector('.motion-button'); if (button) {
|
|
150
179
|
button.hidden = this.getAttribute('motion') !== 'pan';
|
|
151
180
|
const active = this.isMoving;
|
|
152
181
|
this.toggleAttribute('moving', active);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface PileHost extends HTMLElement {
|
|
2
|
+
stackCount: number;
|
|
3
|
+
heldCount: number;
|
|
4
|
+
fanOpen: boolean;
|
|
5
|
+
cardThicknessMm: number;
|
|
6
|
+
pileBounds: {
|
|
7
|
+
left: number;
|
|
8
|
+
right: number;
|
|
9
|
+
top: number;
|
|
10
|
+
bottom: number;
|
|
11
|
+
};
|
|
12
|
+
takeOff(count?: number): void;
|
|
13
|
+
putBack(count?: number): void;
|
|
14
|
+
}
|
|
15
|
+
/** Draw the pile features for the stack's current attributes: `fan`, `taken-off`, `deck-land`, `animate`, `speed` and `pile-controls`. */
|
|
16
|
+
export declare function applyPile(host: PileHost, root: ShadowRoot): void;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pile features of `<fuda-card stack>`: the stack fans open and closes, the top card comes off onto a pile at its side and goes back,
|
|
3
|
+
* and an optional bar of three buttons does it. Loaded by `element.js` only when a stack asks for one of them, so a card without them
|
|
4
|
+
* is exactly what it was.
|
|
5
|
+
*
|
|
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
|
+
*/
|
|
8
|
+
import { fanBounds, fanLayout, fanRelative, fanShift, flight, normalizeLanding, timing } from './pile.js';
|
|
9
|
+
import { stackLayout } from './stack.js';
|
|
10
|
+
const states = new WeakMap();
|
|
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
|
+
const GAP = 0.28;
|
|
13
|
+
const PILE_CSS = `:host{position:relative}.rotation{position:relative}:host(:not([card-spin=corner])) .rotation{transform-origin:calc(50% + var(--pivot,0) * 100cqw) 50%}
|
|
14
|
+
.card{transition:transform var(--fuda-move,.65s) cubic-bezier(.2,.8,.2,1),transform-origin var(--fuda-move,.65s) cubic-bezier(.2,.8,.2,1)}
|
|
15
|
+
.card.fanned{transform-origin:50% 165%;transform:translate(calc(var(--fx,0) * 100cqw),0) rotate(calc(var(--fa,0) * 1deg))}
|
|
16
|
+
:host([flipped]) .card.fanned{transform:translate(calc(var(--fx,0) * 100cqw),0) rotate(calc(var(--fa,0) * 1deg)) rotateY(180deg)}
|
|
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
|
+
.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}
|
|
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
|
+
.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
|
+
.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}
|
|
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
|
+
.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
|
+
@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; }
|
|
28
|
+
/** 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
|
+
function faceCopies(host, root) {
|
|
30
|
+
const front = root.querySelector('.card > .front').cloneNode(true), back = root.querySelector('.card > .back').cloneNode(true);
|
|
31
|
+
for (const el of [front, back]) {
|
|
32
|
+
el.querySelectorAll('[id]').forEach((e) => e.removeAttribute('id'));
|
|
33
|
+
el.querySelectorAll('.motion-button').forEach((e) => e.remove());
|
|
34
|
+
}
|
|
35
|
+
const slot = front.querySelector('slot'), img = host.querySelector('img');
|
|
36
|
+
if (slot) {
|
|
37
|
+
if (img) {
|
|
38
|
+
const copy = img.cloneNode();
|
|
39
|
+
copy.removeAttribute('id');
|
|
40
|
+
copy.className = 'copy';
|
|
41
|
+
slot.replaceWith(copy);
|
|
42
|
+
}
|
|
43
|
+
else
|
|
44
|
+
slot.remove();
|
|
45
|
+
}
|
|
46
|
+
return [front, back];
|
|
47
|
+
}
|
|
48
|
+
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))));
|
|
50
|
+
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('');
|
|
54
|
+
}
|
|
55
|
+
/** 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
|
+
function drawDiscard(host, root, state, count, land, dx) {
|
|
57
|
+
const rotation = root.querySelector('.rotation');
|
|
58
|
+
if (count < 1) {
|
|
59
|
+
state.discard?.remove();
|
|
60
|
+
state.discard = undefined;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
let box = state.discard;
|
|
64
|
+
if (!box) {
|
|
65
|
+
box = document.createElement('div');
|
|
66
|
+
box.className = 'discard';
|
|
67
|
+
rotation.append(box);
|
|
68
|
+
state.discard = box;
|
|
69
|
+
}
|
|
70
|
+
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');
|
|
75
|
+
const [front, back] = faceCopies(host, root);
|
|
76
|
+
copy.querySelector('.front').replaceWith(front);
|
|
77
|
+
copy.querySelector('.back').replaceWith(back);
|
|
78
|
+
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);
|
|
83
|
+
box.replaceChildren(copy);
|
|
84
|
+
}
|
|
85
|
+
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);
|
|
89
|
+
if (on) {
|
|
90
|
+
card.style.setProperty('--fx', String(places[0].x));
|
|
91
|
+
card.style.setProperty('--fa', String(places[0].angle));
|
|
92
|
+
const [front, back] = stills.length ? faceCopies(host, root) : [null, null];
|
|
93
|
+
stills.forEach((still, at) => {
|
|
94
|
+
const i = stills.length - at;
|
|
95
|
+
still.classList.toggle('far', i >= n);
|
|
96
|
+
if (i >= n)
|
|
97
|
+
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');
|
|
105
|
+
const [f, b] = [front.cloneNode(true), back.cloneNode(true)];
|
|
106
|
+
still.replaceChildren(f, b);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
stills.forEach((still, at) => { still.style.setProperty('--j', String(stills.length - at)); still.classList.remove('far'); });
|
|
112
|
+
}
|
|
113
|
+
return { places, bounds: on ? fanBounds(places) : { left: 0, right: 0, top: 0, bottom: 0 } };
|
|
114
|
+
}
|
|
115
|
+
/** Draw the pile features for the stack's current attributes: `fan`, `taken-off`, `deck-land`, `animate`, `speed` and `pile-controls`. */
|
|
116
|
+
export function applyPile(host, root) {
|
|
117
|
+
let state = states.get(host);
|
|
118
|
+
if (!state) {
|
|
119
|
+
state = { taken: 0, token: 0 };
|
|
120
|
+
states.set(host, state);
|
|
121
|
+
}
|
|
122
|
+
if (!root.querySelector('style[data-pile]')) {
|
|
123
|
+
const style = document.createElement('style');
|
|
124
|
+
style.dataset.pile = '';
|
|
125
|
+
style.textContent = PILE_CSS;
|
|
126
|
+
root.append(style);
|
|
127
|
+
}
|
|
128
|
+
const t = timing(host, true), land = normalizeLanding(host.getAttribute('deck-land'));
|
|
129
|
+
host.style.setProperty('--fuda-move', t.on ? `${t.ms}ms` : '0s');
|
|
130
|
+
if (t.on)
|
|
131
|
+
host.style.removeProperty('--stag');
|
|
132
|
+
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;
|
|
136
|
+
const { places, bounds } = fanState(host, root, fan);
|
|
137
|
+
if (!fan) {
|
|
138
|
+
const faced = Array.from(root.querySelectorAll('.card > .stills > .still.faced'));
|
|
139
|
+
// 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; }); };
|
|
142
|
+
if (faced.length) {
|
|
143
|
+
if (t.on)
|
|
144
|
+
void Promise.all(root.querySelector('.card').getAnimations({ subtree: true }).map((a) => a.finished.catch(() => undefined))).then(clean);
|
|
145
|
+
else
|
|
146
|
+
clean();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const dx = 1 + GAP + bounds.right, mmPx = (host.cardThicknessMm * host.offsetWidth) / 63;
|
|
150
|
+
// 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
|
+
const moving = taken !== was && t.on && host.offsetWidth > 0;
|
|
152
|
+
state.flyer?.remove();
|
|
153
|
+
state.flyer = undefined;
|
|
154
|
+
drawDiscard(host, root, state, moving && taken > was ? was : taken, land, dx);
|
|
155
|
+
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';
|
|
164
|
+
turn.append(front, back);
|
|
165
|
+
fly.append(turn);
|
|
166
|
+
flyer.append(fly);
|
|
167
|
+
rotation.append(flyer);
|
|
168
|
+
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 };
|
|
171
|
+
// 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);
|
|
175
|
+
void Promise.all(anims.map((a) => a.finished)).then(() => {
|
|
176
|
+
if (token !== state.token)
|
|
177
|
+
return;
|
|
178
|
+
flyer.remove();
|
|
179
|
+
state.flyer = undefined;
|
|
180
|
+
drawDiscard(host, root, state, taken, land, dx);
|
|
181
|
+
}, () => undefined);
|
|
182
|
+
}
|
|
183
|
+
state.taken = taken;
|
|
184
|
+
// The bar of three buttons, when asked for.
|
|
185
|
+
if (host.hasAttribute('pile-controls')) {
|
|
186
|
+
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';
|
|
202
|
+
bar.append(fanButton, off, back);
|
|
203
|
+
root.append(bar);
|
|
204
|
+
state.bar = bar;
|
|
205
|
+
}
|
|
206
|
+
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;
|
|
211
|
+
}
|
|
212
|
+
else if (state.bar) {
|
|
213
|
+
state.bar.remove();
|
|
214
|
+
state.bar = undefined;
|
|
215
|
+
}
|
|
216
|
+
const right = Math.max(bounds.right, taken > 0 || (moving && taken < was) ? dx : 0);
|
|
217
|
+
const next = { left: bounds.left, right, top: bounds.top, bottom: bounds.bottom };
|
|
218
|
+
const old = host.pileBounds;
|
|
219
|
+
host.pileBounds = next;
|
|
220
|
+
host.style.setProperty('--pivot', String((next.right - next.left) / 2));
|
|
221
|
+
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 }));
|
|
223
|
+
}
|
package/dist/pile.d.ts
CHANGED
|
@@ -19,7 +19,49 @@ export interface FanPlace {
|
|
|
19
19
|
closedX: number;
|
|
20
20
|
closedY: number;
|
|
21
21
|
}
|
|
22
|
-
|
|
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. */
|
|
23
|
+
export declare function fanLayout(count: number, seed?: number, step?: number): FanPlace[];
|
|
24
|
+
/** 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
|
+
export declare const CARD_RATIO: number;
|
|
26
|
+
export declare const FAN_PIVOT = 1.65;
|
|
27
|
+
/**
|
|
28
|
+
* How far an open fan reaches beyond the box of one squared-up card, in card widths: `left` and `right` past the box's sides, `top` and
|
|
29
|
+
* `bottom` past its top and bottom (never negative). Pages use it to leave room and to fit the view.
|
|
30
|
+
*/
|
|
31
|
+
export declare function fanBounds(places: FanPlace[]): {
|
|
32
|
+
left: number;
|
|
33
|
+
right: number;
|
|
34
|
+
top: number;
|
|
35
|
+
bottom: number;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* The cards of a fan as they lie relative to the first one, for a pile whose first card is the one that carries the rest (a stack's live top
|
|
39
|
+
* card, with the other cards laid inside it). Card by card: the turn and the shift, in card widths, that put it where the fan wants it.
|
|
40
|
+
*/
|
|
41
|
+
export declare function fanRelative(places: FanPlace[]): Array<{
|
|
42
|
+
angle: number;
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
}>;
|
|
46
|
+
/** 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). */
|
|
47
|
+
export declare function fanShift(place: FanPlace): {
|
|
48
|
+
x: number;
|
|
49
|
+
y: number;
|
|
50
|
+
};
|
|
51
|
+
/** How long things take: the animation is on (the page did not turn it off and the reader did not ask for reduced motion) and how long a move lasts, in milliseconds. */
|
|
52
|
+
export interface Timing {
|
|
53
|
+
on: boolean;
|
|
54
|
+
ms: number;
|
|
55
|
+
}
|
|
56
|
+
export declare const SPEEDS: Record<string, number>;
|
|
57
|
+
/** The base length of a move, 600 ms; `speed` stretches or shortens every move at once. */
|
|
58
|
+
export declare const BASE_MS = 600;
|
|
59
|
+
export declare function reducedMotion(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* `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
|
+
* already moved before the option existed). The reader's reduced-motion setting always wins.
|
|
63
|
+
*/
|
|
64
|
+
export declare function timing(host: Element, dflt: boolean): Timing;
|
|
23
65
|
/**
|
|
24
66
|
* A card taken off a pile flies to the pile at its side, and turns over sideways on the way if it lands face down; coming back it
|
|
25
67
|
* does the same in reverse. `fly` carries the card across, `turn` carries the turn. `from` is how far the card is from where it ends.
|
|
@@ -27,4 +69,5 @@ export declare function fanLayout(count: number, seed?: number): FanPlace[];
|
|
|
27
69
|
export declare function flight(fly: HTMLElement, turn: HTMLElement, from: {
|
|
28
70
|
x: number;
|
|
29
71
|
y: number;
|
|
30
|
-
|
|
72
|
+
z?: number;
|
|
73
|
+
}, land: 'face-up' | 'face-down', direction: 'off' | 'back', ms: number, spin?: number): Animation[];
|
package/dist/pile.js
CHANGED
|
@@ -12,17 +12,18 @@ export const deckLandings = [
|
|
|
12
12
|
export function normalizeLanding(value) {
|
|
13
13
|
return value === 'face-down' || value === 'upside-down' || value === 'turned-over' ? 'face-down' : 'face-up';
|
|
14
14
|
}
|
|
15
|
-
|
|
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. */
|
|
16
|
+
export function fanLayout(count, seed = 1, step = 0.34) {
|
|
16
17
|
const n = Math.max(0, Math.floor(count));
|
|
17
18
|
if (!n)
|
|
18
19
|
return [];
|
|
19
|
-
const spread = Math.min(110, 11 * (n - 1)),
|
|
20
|
+
const spread = Math.min(110, 11 * (n - 1)), turn = n > 1 ? spread / (n - 1) : 0;
|
|
20
21
|
const nudge = stackLayout(n, { messiness: 0.25, seed, depth: n });
|
|
21
22
|
return Array.from({ length: n }, (_, i) => {
|
|
22
23
|
const under = i >= 1 ? nudge[i - 1] : undefined;
|
|
23
24
|
return {
|
|
24
|
-
angle: Math.round((-spread / 2 + i *
|
|
25
|
-
x: Math.round((i - (n - 1) / 2) *
|
|
25
|
+
angle: Math.round((-spread / 2 + i * turn) * 100) / 100,
|
|
26
|
+
x: Math.round((i - (n - 1) / 2) * step * 1000) / 1000,
|
|
26
27
|
y: 0,
|
|
27
28
|
closedAngle: under ? under.rotate / 3 : 0,
|
|
28
29
|
closedX: under ? under.x : 0,
|
|
@@ -30,16 +31,75 @@ export function fanLayout(count, seed = 1) {
|
|
|
30
31
|
};
|
|
31
32
|
});
|
|
32
33
|
}
|
|
34
|
+
/** 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. */
|
|
35
|
+
export const CARD_RATIO = 3.5 / 2.5;
|
|
36
|
+
export const FAN_PIVOT = 1.65;
|
|
37
|
+
/**
|
|
38
|
+
* How far an open fan reaches beyond the box of one squared-up card, in card widths: `left` and `right` past the box's sides, `top` and
|
|
39
|
+
* `bottom` past its top and bottom (never negative). Pages use it to leave room and to fit the view.
|
|
40
|
+
*/
|
|
41
|
+
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;
|
|
44
|
+
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;
|
|
49
|
+
x0 = Math.min(x0, x);
|
|
50
|
+
x1 = Math.max(x1, x);
|
|
51
|
+
y0 = Math.min(y0, y);
|
|
52
|
+
y1 = Math.max(y1, y);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const r = (v) => Math.round(v * 1000) / 1000;
|
|
56
|
+
return { left: r(-x0), right: r(x1 - 1), top: r(-y0), bottom: r(y1 - CARD_RATIO) };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The cards of a fan as they lie relative to the first one, for a pile whose first card is the one that carries the rest (a stack's live top
|
|
60
|
+
* card, with the other cards laid inside it). Card by card: the turn and the shift, in card widths, that put it where the fan wants it.
|
|
61
|
+
*/
|
|
62
|
+
export function fanRelative(places) {
|
|
63
|
+
if (!places.length)
|
|
64
|
+
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;
|
|
67
|
+
return places.map((p) => {
|
|
68
|
+
const dx = p.x - places[0].x;
|
|
69
|
+
return { angle: r(p.angle - places[0].angle), x: r(dx * c), y: r(-dx * s) };
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** 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
|
+
export function fanShift(place) {
|
|
74
|
+
const a = (place.angle * Math.PI) / 180, lever = (FAN_PIVOT - 0.5) * CARD_RATIO;
|
|
75
|
+
return { x: place.x + lever * Math.sin(a), y: lever * (1 - Math.cos(a)) };
|
|
76
|
+
}
|
|
77
|
+
export const SPEEDS = { slow: 1.6, normal: 1, quick: 0.6 };
|
|
78
|
+
/** The base length of a move, 600 ms; `speed` stretches or shortens every move at once. */
|
|
79
|
+
export const BASE_MS = 600;
|
|
80
|
+
export function reducedMotion() {
|
|
81
|
+
return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* `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
|
+
* already moved before the option existed). The reader's reduced-motion setting always wins.
|
|
86
|
+
*/
|
|
87
|
+
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;
|
|
91
|
+
return { on: asked && !reducedMotion(), ms: Math.round(BASE_MS * scale) };
|
|
92
|
+
}
|
|
33
93
|
/**
|
|
34
94
|
* A card taken off a pile flies to the pile at its side, and turns over sideways on the way if it lands face down; coming back it
|
|
35
95
|
* does the same in reverse. `fly` carries the card across, `turn` carries the turn. `from` is how far the card is from where it ends.
|
|
36
96
|
*/
|
|
37
|
-
export function flight(fly, turn, from, land, direction, ms) {
|
|
97
|
+
export function flight(fly, turn, from, land, direction, ms, spin = 0) {
|
|
38
98
|
const options = { duration: ms, easing: 'cubic-bezier(.3,.1,.2,1)' };
|
|
39
99
|
const out = [fly.animate([
|
|
40
|
-
{ transform: `translate3d(${from.x}px,${from.y}px
|
|
41
|
-
{ transform: `translate3d(${from.x / 2}px,${from.y / 2 - 34}px
|
|
42
|
-
{ transform: 'translate3d(0,0,0)' }
|
|
100
|
+
{ transform: `translate3d(${from.x}px,${from.y}px,${from.z ?? 0}px) rotate(${spin}deg)` },
|
|
101
|
+
{ transform: `translate3d(${from.x / 2}px,${from.y / 2 - 34}px,${(from.z ?? 0) / 2 + 80}px) rotate(${spin / 2}deg) scale(1.07)`, offset: 0.5 },
|
|
102
|
+
{ transform: 'translate3d(0,0,0) rotate(0deg)' }
|
|
43
103
|
], options)];
|
|
44
104
|
if (land === 'face-down')
|
|
45
105
|
out.push(turn.animate(direction === 'off' ? [{ transform: 'none' }, { transform: 'rotateY(180deg)' }] : [{ transform: 'rotateY(180deg)' }, { transform: 'none' }], options));
|
package/dist/zoom.d.ts
CHANGED
|
@@ -37,6 +37,14 @@ export interface ZoomView {
|
|
|
37
37
|
zoomed(): boolean;
|
|
38
38
|
/** Let the reader turn the view in 3D (angle, tilt, Auto-turn) or only zoom and move it. Off by default. */
|
|
39
39
|
setTurnable(on: boolean): void;
|
|
40
|
+
/** The selector of the items that must be visible at Fit. It can change with the display. */
|
|
41
|
+
items: string;
|
|
42
|
+
/** Run a change that moves the view (a refit, a pan) so that the view glides to its new place over `ms` instead of jumping; 0 jumps. */
|
|
43
|
+
animated(change: () => void, ms: number): void;
|
|
44
|
+
/** Move the view so the item is in the middle of the stage, gliding over `ms` (0 jumps). Does nothing when the item is not in the view. */
|
|
45
|
+
reveal(item: Element, ms: number): void;
|
|
46
|
+
/** True while the view is turned in 3D. */
|
|
47
|
+
turned(): boolean;
|
|
40
48
|
}
|
|
41
49
|
/**
|
|
42
50
|
* Zoom, Fit and pan for a flat view. `stage` clips, `view` is its absolutely positioned child that holds the items and is
|
package/dist/zoom.js
CHANGED
|
@@ -7,7 +7,6 @@ const TAPPABLE = 'button:not(:disabled), a[href], [role=button]';
|
|
|
7
7
|
* moved and scaled. Fit shows every item at once; the reader's zoom and pan are never reset by a change of content.
|
|
8
8
|
*/
|
|
9
9
|
export function zoomView(stage, view, options = {}) {
|
|
10
|
-
const items = options.items ?? '.card';
|
|
11
10
|
const controls = document.createElement('kazoku-view-controls');
|
|
12
11
|
controls.setAttribute('flat', '');
|
|
13
12
|
controls.setAttribute('ignore', '');
|
|
@@ -23,14 +22,21 @@ export function zoomView(stage, view, options = {}) {
|
|
|
23
22
|
let box = { w: 0, h: 0, cx: 0, cy: 0, bw: 0, bh: 0 };
|
|
24
23
|
let fit = 1;
|
|
25
24
|
let turnable = false;
|
|
26
|
-
const self = { controls, minOpen: 0, refresh, reset: () => refresh(true), zoomed: () => controls.state.zoom > ZOOMED, setTurnable };
|
|
25
|
+
const self = { controls, minOpen: 0, items: options.items ?? '.card', refresh, reset: () => refresh(true), zoomed: () => controls.state.zoom > ZOOMED, setTurnable, animated, reveal, turned: () => turnable && Boolean(controls.state.yaw || controls.state.tilt) };
|
|
26
|
+
let glide;
|
|
27
|
+
/** A glide that is still running would be measured as if it were the layout, so a new measure ends it first. */
|
|
28
|
+
function settleGlide() { if (glide) {
|
|
29
|
+
glide.cancel();
|
|
30
|
+
glide = undefined;
|
|
31
|
+
} }
|
|
27
32
|
function measure() {
|
|
33
|
+
settleGlide();
|
|
28
34
|
const previous = view.style.transform;
|
|
29
35
|
view.style.transform = 'none';
|
|
30
36
|
const origin = stage.getBoundingClientRect();
|
|
31
37
|
const left = origin.left + stage.clientLeft, top = origin.top + stage.clientTop;
|
|
32
38
|
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
33
|
-
for (const item of Array.from(view.querySelectorAll(items))) {
|
|
39
|
+
for (const item of Array.from(view.querySelectorAll(self.items))) {
|
|
34
40
|
const r = item.getBoundingClientRect();
|
|
35
41
|
if (!r.width || !r.height)
|
|
36
42
|
continue;
|
|
@@ -64,6 +70,31 @@ export function zoomView(stage, view, options = {}) {
|
|
|
64
70
|
controls.toggleAttribute('flat', !on);
|
|
65
71
|
draw(controls.state);
|
|
66
72
|
}
|
|
73
|
+
const EASE = 'cubic-bezier(.2,.8,.2,1)';
|
|
74
|
+
function animated(change, ms) {
|
|
75
|
+
const from = ms > 0 ? getComputedStyle(view).transform : '';
|
|
76
|
+
change();
|
|
77
|
+
const to = view.style.transform;
|
|
78
|
+
if (ms > 0 && from && from !== 'none' && to && from !== to) {
|
|
79
|
+
settleGlide();
|
|
80
|
+
glide = view.animate([{ transform: from }, { transform: to }], { duration: ms, easing: EASE });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function reveal(item, ms) {
|
|
84
|
+
animated(() => {
|
|
85
|
+
measure();
|
|
86
|
+
const previous = view.style.transform;
|
|
87
|
+
view.style.transform = 'none';
|
|
88
|
+
const origin = stage.getBoundingClientRect(), r = item.getBoundingClientRect();
|
|
89
|
+
view.style.transform = previous;
|
|
90
|
+
if (!r.width)
|
|
91
|
+
return;
|
|
92
|
+
const s = fit * controls.state.zoom;
|
|
93
|
+
const px = r.left + r.width / 2 - origin.left - stage.clientLeft, py = r.top + r.height / 2 - origin.top - stage.clientTop;
|
|
94
|
+
controls.set({ panX: -s * (px - box.cx), panY: -s * (py - box.cy) });
|
|
95
|
+
draw(controls.state);
|
|
96
|
+
}, ms);
|
|
97
|
+
}
|
|
67
98
|
function refresh(open = false) {
|
|
68
99
|
measure();
|
|
69
100
|
if (!box.w || !box.h)
|
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Fuda supplies reusable frames for a caller's cards and reusable ways to display collections on websites. The hosting site supplies the card data and artwork or interactive content. Initial versions of twelve layouts now exist in `<fuda-collection>`. They are prototypes; the detailed capabilities below remain design goals unless described as implemented in the README.
|
|
4
4
|
|
|
5
|
-
## Choosing a display, the
|
|
5
|
+
## Choosing a display, piles, the fan and animation (0.6.0, 0.7.0)
|
|
6
6
|
|
|
7
7
|
- **Display buttons.** All twelve displays are buttons on screen at once, in a row that spans the width, each with a small picture of its layout. No drop-down. At least 48 px high; 4 to a row on a phone, 6 on a desk; arrow keys move the choice; light and dark follow the reader.
|
|
8
|
-
- **
|
|
9
|
-
- **
|
|
10
|
-
-
|
|
8
|
+
- **Piles (0.7.0).** The Overlapping Deck, Random Draw, the Jukebox Carousel and the Flip Tower (the last three drew a pile of three cards) are the same pile with their own look. Every pile turns in 3D (angle, tilt, Auto-turn), fans open and closed, and has a card taken off it. `<fuda-card stack>` is a pile too. Rolodex and Spotlight spread their cards and are carousels, not piles.
|
|
9
|
+
- **Deck.** A deck and a pile. Next flips the top card off to the pile on the deck's other side, Previous brings it back. The landing is a choice: still facing up, or turned over (a sideways turn about the card's vertical axis, like turning a page, showing the back image or the package's own back). A card never lands with its picture on its head; the 0.6.0 value `upside-down` still works and means turned over. One element flies, so a card is never drawn twice.
|
|
10
|
+
- **Fan.** Every pile fans open and closed with a control right under the view: up to ten cards in an arc, most of each card showing, closing back exactly. `view="fan"` is the same pile with only the fan. In the address (`?fan=open`).
|
|
11
|
+
- **Animate (0.7.0).** `animate="off"` and `speed` for every move: the fan, a card taken off, Next in the carousels, a binder page turn, a switch of display, a pan to the card in the wall, story and atlas. On when the attribute is missing; reduced motion always wins. One shared timing, CSS transforms and one-shot animations, nothing running when idle. Moves with it on and off end in exactly the same place.
|
|
12
|
+
- Not done: dealing more than one card at a time, a pile that fills up with the whole discard history (the pile shows the last ten), wear on the cards of a fan.
|
|
11
13
|
|
|
12
14
|
## Zoom, Fit and move (every view)
|
|
13
15
|
|