@johnmorrisdotca/fuda-cards 0.9.2 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE.md +14 -0
- package/README.md +466 -60
- package/dist/binder.d.ts +4 -6
- package/dist/binder.js +32 -23
- package/dist/card-markup.d.ts +2 -0
- package/dist/card-markup.js +59 -0
- package/dist/card-options.d.ts +131 -0
- package/dist/card-options.js +48 -0
- package/dist/card-styles.d.ts +2 -0
- package/dist/card-styles.js +314 -0
- package/dist/choice.d.ts +1 -1
- package/dist/choice.js +27 -23
- package/dist/collection-address.d.ts +8 -0
- package/dist/collection-address.js +52 -0
- package/dist/collection-binder-view.d.ts +9 -0
- package/dist/collection-binder-view.js +123 -0
- package/dist/collection-controls.d.ts +2 -0
- package/dist/collection-controls.js +143 -0
- package/dist/collection-deck.d.ts +11 -0
- package/dist/collection-deck.js +165 -0
- package/dist/collection-dialog.d.ts +2 -0
- package/dist/collection-dialog.js +28 -0
- package/dist/collection-element.d.ts +41 -63
- package/dist/collection-element.js +288 -472
- package/dist/collection-host.d.ts +5 -0
- package/dist/collection-host.js +1 -0
- package/dist/collection-icons.d.ts +3 -0
- package/dist/collection-icons.js +19 -0
- package/dist/collection-motion.d.ts +11 -0
- package/dist/collection-motion.js +72 -0
- package/dist/collection-styles.d.ts +4 -0
- package/dist/collection-styles.js +336 -0
- package/dist/collection-views.d.ts +8 -0
- package/dist/collection-views.js +8 -0
- package/dist/collection.d.ts +140 -11
- package/dist/collection.js +139 -33
- package/dist/element-define.d.ts +2 -0
- package/dist/element-define.js +3 -0
- package/dist/element.d.ts +20 -1
- package/dist/element.js +289 -98
- package/dist/index.d.ts +9 -52
- package/dist/index.js +5 -5
- package/dist/live-3d.d.ts +126 -0
- package/dist/live-3d.js +229 -0
- package/dist/materials.d.ts +117 -6
- package/dist/materials.js +191 -28
- package/dist/motion.d.ts +2 -0
- package/dist/motion.js +4 -0
- package/dist/pile-card.js +138 -91
- package/dist/pile.d.ts +61 -8
- package/dist/pile.js +89 -29
- package/dist/stack.d.ts +42 -6
- package/dist/stack.js +28 -3
- package/dist/version.d.ts +11 -0
- package/dist/version.js +11 -0
- package/dist/wear.d.ts +147 -9
- package/dist/wear.js +273 -89
- package/dist/zoom.d.ts +1 -1
- package/dist/zoom.js +73 -42
- package/docs/API.md +783 -0
- package/docs/collection-displays.md +1 -1
- package/docs/guides/accessibility-and-theming.md +62 -0
- package/docs/guides/architecture.md +53 -0
- package/docs/guides/binder-and-piles.md +83 -0
- package/docs/guides/bundlers.md +60 -0
- package/docs/guides/card-element.md +65 -0
- package/docs/guides/collections.md +86 -0
- package/docs/guides/embed-in-an-iframe.md +39 -0
- package/docs/guides/frameworks.md +132 -0
- package/docs/guides/getting-started.md +96 -0
- package/docs/guides/html-and-cdn.md +69 -0
- package/docs/guides/ja/accessibility-and-theming.md +62 -0
- package/docs/guides/ja/architecture.md +53 -0
- package/docs/guides/ja/binder-and-piles.md +83 -0
- package/docs/guides/ja/bundlers.md +60 -0
- package/docs/guides/ja/card-element.md +65 -0
- package/docs/guides/ja/collections.md +86 -0
- package/docs/guides/ja/embed-in-an-iframe.md +39 -0
- package/docs/guides/ja/frameworks.md +132 -0
- package/docs/guides/ja/getting-started.md +96 -0
- package/docs/guides/ja/html-and-cdn.md +69 -0
- package/docs/guides/ja/live-3d.md +75 -0
- package/docs/guides/ja/materials-and-stacks.md +73 -0
- package/docs/guides/ja/motion-and-spin.md +73 -0
- package/docs/guides/ja/troubleshooting.md +60 -0
- package/docs/guides/ja/typescript-and-node.md +45 -0
- package/docs/guides/ja/upgrading.md +56 -0
- package/docs/guides/ja/wear.md +63 -0
- package/docs/guides/live-3d.md +75 -0
- package/docs/guides/materials-and-stacks.md +73 -0
- package/docs/guides/motion-and-spin.md +73 -0
- package/docs/guides/troubleshooting.md +60 -0
- package/docs/guides/typescript-and-node.md +45 -0
- package/docs/guides/upgrading.md +56 -0
- package/docs/guides/wear.md +63 -0
- package/docs/live-3d.md +59 -0
- package/package.json +81 -11
package/dist/wear.js
CHANGED
|
@@ -9,32 +9,101 @@
|
|
|
9
9
|
*
|
|
10
10
|
* This module is loaded by `<fuda-card>` only when a card asks for wear, so a perfect card carries none of it.
|
|
11
11
|
*/
|
|
12
|
-
import { seededRandom } from
|
|
12
|
+
import { seededRandom } from "./stack.js";
|
|
13
13
|
const L = (corners, edges, surface, creases, toning, centering) => ({ corners, edges, surface, creases, toning, centering });
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* The seven grades, from perfect to damaged. The names are the plain grading terms; the numbers are this package's reading of them (docs/wear.md).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { wearPresets } from "@johnmorrisdotca/fuda-cards";
|
|
20
|
+
*
|
|
21
|
+
* console.log(wearPresets.map((preset) => preset.id).join(" "));
|
|
22
|
+
* // perfect mint near-mint lightly-played moderately-played heavily-played damaged
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
15
25
|
export const wearPresets = [
|
|
16
|
-
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
{
|
|
26
|
+
{
|
|
27
|
+
id: "perfect",
|
|
28
|
+
name: "Perfect",
|
|
29
|
+
levels: L(0, 0, 0, 0, 0, 0),
|
|
30
|
+
says: "No imperfections, even close up. Draws nothing.",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: "mint",
|
|
34
|
+
name: "Mint",
|
|
35
|
+
levels: L(0.05, 0.05, 0.06, 0, 0, 0.1),
|
|
36
|
+
says: "Nothing to the naked eye; a flaw or two only on close inspection.",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: "near-mint",
|
|
40
|
+
name: "Near mint",
|
|
41
|
+
levels: L(0.18, 0.2, 0.2, 0, 0, 0.2),
|
|
42
|
+
says: "Sharp corners, a trace of edge whitening, a few light scratches.",
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: "lightly-played",
|
|
46
|
+
name: "Lightly played",
|
|
47
|
+
levels: L(0.38, 0.45, 0.4, 0, 0.05, 0.3),
|
|
48
|
+
says: "Very slight corner rounding, whitening on one or two edges, scratches seen when tilted.",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "moderately-played",
|
|
52
|
+
name: "Moderately played",
|
|
53
|
+
levels: L(0.62, 0.68, 0.62, 0.08, 0.15, 0.45),
|
|
54
|
+
says: "Soft or rounded corners, whitening on several edges, obvious scratches, some gloss lost.",
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: "heavily-played",
|
|
58
|
+
name: "Heavily played",
|
|
59
|
+
levels: L(0.82, 0.9, 0.85, 0.3, 0.3, 0.6),
|
|
60
|
+
says: "Corners that have lost their shape, heavy whitening, deep scratches, minor creases.",
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: "damaged",
|
|
64
|
+
name: "Damaged",
|
|
65
|
+
levels: L(1, 1, 1, 0.85, 0.5, 0.8),
|
|
66
|
+
says: "Chipped and folded corners, hard creases, staining.",
|
|
67
|
+
},
|
|
23
68
|
];
|
|
69
|
+
/**
|
|
70
|
+
* The grade with this id.
|
|
71
|
+
*
|
|
72
|
+
* @param id - A grade's id, such as `near-mint` (the `wear` attribute's value).
|
|
73
|
+
* @returns The grade, or `undefined` when there is none with that id.
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { wearPreset } from "@johnmorrisdotca/fuda-cards";
|
|
77
|
+
*
|
|
78
|
+
* console.log(wearPreset("mint")?.name, wearPreset("pristine")); // Mint undefined
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
24
81
|
export function wearPreset(id) {
|
|
25
82
|
return wearPresets.find((preset) => preset.id === id);
|
|
26
83
|
}
|
|
27
84
|
const clamp01 = (value) => (Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : 0);
|
|
28
85
|
const round = (value) => Math.round(value * 100) / 100;
|
|
29
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* The levels a card's wear asks for: the grade's or the number's, with any level named on its own laid over them.
|
|
88
|
+
*
|
|
89
|
+
* @param options - The wear asked for: a grade or a number from 0 to 1, and levels of corners, edges, surface and creases.
|
|
90
|
+
* @returns The levels, or `null` when the card asks for none: no wear, "perfect", an unknown name or every level zero.
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { wearLevels } from "@johnmorrisdotca/fuda-cards";
|
|
94
|
+
*
|
|
95
|
+
* console.log(wearLevels({ wear: "perfect" })); // null
|
|
96
|
+
* console.log(wearLevels({ wear: 0.5 })?.surface); // 0.5
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
30
99
|
export function wearLevels(options) {
|
|
31
100
|
const asked = options.wear;
|
|
32
101
|
let base = L(0, 0, 0, 0, 0, 0);
|
|
33
|
-
if (typeof asked ===
|
|
102
|
+
if (typeof asked === "number" && Number.isFinite(asked)) {
|
|
34
103
|
const n = clamp01(asked);
|
|
35
104
|
base = L(n, n, n, n * n, n * 0.5, n);
|
|
36
105
|
}
|
|
37
|
-
else if (typeof asked ===
|
|
106
|
+
else if (typeof asked === "string" && asked.trim() !== "") {
|
|
38
107
|
const preset = wearPreset(asked.trim());
|
|
39
108
|
if (preset)
|
|
40
109
|
base = preset.levels;
|
|
@@ -42,7 +111,7 @@ export function wearLevels(options) {
|
|
|
42
111
|
return wearLevels({ ...options, wear: Number(asked) });
|
|
43
112
|
}
|
|
44
113
|
const levels = { ...base };
|
|
45
|
-
for (const key of [
|
|
114
|
+
for (const key of ["corners", "edges", "surface", "creases"]) {
|
|
46
115
|
const given = options[key];
|
|
47
116
|
if (given !== undefined && Number.isFinite(given))
|
|
48
117
|
levels[key] = clamp01(given);
|
|
@@ -56,22 +125,34 @@ export const BASE_RADIUS = 12.5;
|
|
|
56
125
|
function cornerFor(severity, random) {
|
|
57
126
|
const s = clamp01(severity);
|
|
58
127
|
if (s < 0.1)
|
|
59
|
-
return { kind:
|
|
128
|
+
return { kind: "sharp", size: BASE_RADIUS, jag: [] };
|
|
60
129
|
if (s < 0.35)
|
|
61
|
-
return { kind:
|
|
130
|
+
return { kind: "soft", size: round(BASE_RADIUS + s * 14), jag: [] };
|
|
62
131
|
if (s < 0.6)
|
|
63
|
-
return { kind:
|
|
132
|
+
return { kind: "rounded", size: round(17 + s * 22), jag: [] };
|
|
64
133
|
if (s < 0.8) {
|
|
65
134
|
const size = round(7 + s * 14);
|
|
66
|
-
return {
|
|
135
|
+
return {
|
|
136
|
+
kind: "chipped",
|
|
137
|
+
size,
|
|
138
|
+
jag: [
|
|
139
|
+
round(0.3 + random() * 0.25),
|
|
140
|
+
round(0.25 + random() * 0.3),
|
|
141
|
+
round(0.5 + random() * 0.35),
|
|
142
|
+
round(0.1 + random() * 0.2),
|
|
143
|
+
],
|
|
144
|
+
};
|
|
67
145
|
}
|
|
68
|
-
return { kind:
|
|
146
|
+
return { kind: "dog-eared", size: round(15 + s * 22), jag: [] };
|
|
69
147
|
}
|
|
70
148
|
function faceFor(levels, random) {
|
|
71
149
|
const edge = [];
|
|
72
150
|
const flecks = Math.round(levels.edges * 30);
|
|
73
151
|
for (let i = 0; i < flecks; i++) {
|
|
74
|
-
const side = Math.floor(random() * 4)
|
|
152
|
+
const side = Math.floor(random() * 4);
|
|
153
|
+
const at = 0.08 + random() * 0.84;
|
|
154
|
+
const len = 2 + random() * (3 + levels.edges * 12);
|
|
155
|
+
const depth = 0.5 + random() * (0.8 + levels.edges * 2.4);
|
|
75
156
|
const a = round(0.7 + random() * 0.3);
|
|
76
157
|
if (side === 0)
|
|
77
158
|
edge.push({ x: round(at * CARD_W), y: 0, w: round(len), h: round(depth), a });
|
|
@@ -85,26 +166,65 @@ function faceFor(levels, random) {
|
|
|
85
166
|
const scratches = [];
|
|
86
167
|
const lines = Math.round(levels.surface * 9);
|
|
87
168
|
for (let i = 0; i < lines; i++) {
|
|
88
|
-
const x1 = 15 + random() * 220
|
|
89
|
-
|
|
169
|
+
const x1 = 15 + random() * 220;
|
|
170
|
+
const y1 = 15 + random() * 320;
|
|
171
|
+
const angle = random() * Math.PI;
|
|
172
|
+
const len = 14 + random() * (30 + levels.surface * 60);
|
|
173
|
+
scratches.push({
|
|
174
|
+
x1: round(x1),
|
|
175
|
+
y1: round(y1),
|
|
176
|
+
x2: round(Math.min(245, Math.max(5, x1 + Math.cos(angle) * len))),
|
|
177
|
+
y2: round(Math.min(345, Math.max(5, y1 + Math.sin(angle) * len))),
|
|
178
|
+
a: round(0.15 + random() * 0.35),
|
|
179
|
+
});
|
|
90
180
|
}
|
|
91
181
|
const specks = [];
|
|
92
182
|
const dots = Math.round(levels.surface * 26);
|
|
93
183
|
for (let i = 0; i < dots; i++)
|
|
94
|
-
specks.push({
|
|
184
|
+
specks.push({
|
|
185
|
+
x: round(8 + random() * 234),
|
|
186
|
+
y: round(8 + random() * 334),
|
|
187
|
+
r: round(0.3 + random() * 0.9),
|
|
188
|
+
a: round(0.25 + random() * 0.4),
|
|
189
|
+
light: random() < 0.5,
|
|
190
|
+
});
|
|
95
191
|
const creases = [];
|
|
96
192
|
const count = levels.creases <= 0.04 ? 0 : Math.min(4, Math.ceil(levels.creases * 3));
|
|
97
193
|
for (let i = 0; i < count; i++) {
|
|
98
|
-
const horizontal = random() < 0.5
|
|
194
|
+
const horizontal = random() < 0.5;
|
|
195
|
+
const along = 0.15 + random() * 0.7;
|
|
196
|
+
const other = 0.15 + random() * 0.7;
|
|
99
197
|
const heavy = levels.creases > 0.5 && i === 0;
|
|
100
|
-
const x1 = horizontal ? 0 : along * CARD_W
|
|
101
|
-
const
|
|
198
|
+
const x1 = horizontal ? 0 : along * CARD_W;
|
|
199
|
+
const y1 = horizontal ? along * CARD_H : 0;
|
|
200
|
+
const x2 = horizontal ? CARD_W : other * CARD_W;
|
|
201
|
+
const y2 = horizontal ? other * CARD_H : CARD_H;
|
|
102
202
|
creases.push({ x1: round(x1), y1: round(y1), x2: round(x2), y2: round(y2), a: round(0.5 + random() * 0.4), heavy });
|
|
103
203
|
}
|
|
104
|
-
const stain = levels.creases > 0.5 || levels.toning > 0.4
|
|
204
|
+
const stain = levels.creases > 0.5 || levels.toning > 0.4
|
|
205
|
+
? {
|
|
206
|
+
x: round(40 + random() * 170),
|
|
207
|
+
y: round(60 + random() * 230),
|
|
208
|
+
r: round(25 + random() * 45),
|
|
209
|
+
a: round(0.12 + levels.toning * 0.25),
|
|
210
|
+
}
|
|
211
|
+
: null;
|
|
105
212
|
return { edge, creases, scratches, specks, stain };
|
|
106
213
|
}
|
|
107
|
-
/**
|
|
214
|
+
/**
|
|
215
|
+
* Where every mark of a card lies. A pure function of the levels and the seed: no DOM, nothing at random, so the same seed lays the same marks every time.
|
|
216
|
+
*
|
|
217
|
+
* @param levels - How much of each kind of wear (see {@link wearLevels}).
|
|
218
|
+
* @param seed - The seed that lays the marks out. Unless said, 1.
|
|
219
|
+
* @returns The corners, centring and marks of both faces.
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* import { wearLayout, wearPreset } from "@johnmorrisdotca/fuda-cards";
|
|
223
|
+
*
|
|
224
|
+
* const levels = wearPreset("heavily-played")!.levels;
|
|
225
|
+
* console.log(JSON.stringify(wearLayout(levels, 5)) === JSON.stringify(wearLayout(levels, 5))); // true
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
108
228
|
export function wearLayout(levels, seed = 1) {
|
|
109
229
|
const base = (Number.isFinite(seed) ? seed : 1) * 2654435761;
|
|
110
230
|
const corners = [];
|
|
@@ -113,25 +233,44 @@ export function wearLayout(levels, seed = 1) {
|
|
|
113
233
|
corners.push(cornerFor(levels.corners * (0.35 + random() * 0.95), random));
|
|
114
234
|
}
|
|
115
235
|
// A card has at most one folded corner and two chipped ones; the rest are only rounded, as real damage rarely falls on all four.
|
|
116
|
-
let folded = 0
|
|
236
|
+
let folded = 0;
|
|
237
|
+
let chipped = 0;
|
|
117
238
|
corners.forEach((corner, index) => {
|
|
118
|
-
if (corner.kind ===
|
|
119
|
-
corners[index] = { kind:
|
|
120
|
-
if (corners[index].kind ===
|
|
121
|
-
corners[index] = { kind:
|
|
239
|
+
if (corner.kind === "dog-eared" && ++folded > 1)
|
|
240
|
+
corners[index] = { kind: "chipped", size: round(corner.size * 0.45), jag: [0.4, 0.35, 0.6, 0.2] };
|
|
241
|
+
if (corners[index].kind === "chipped" && ++chipped > 2)
|
|
242
|
+
corners[index] = { kind: "rounded", size: round(corner.size * 1.6), jag: [] };
|
|
122
243
|
});
|
|
123
244
|
const place = seededRandom((base + 7919) >>> 0);
|
|
124
|
-
const centering = {
|
|
125
|
-
|
|
245
|
+
const centering = {
|
|
246
|
+
x: round((place() * 2 - 1) * levels.centering * 7),
|
|
247
|
+
y: round((place() * 2 - 1) * levels.centering * 5),
|
|
248
|
+
};
|
|
249
|
+
return {
|
|
250
|
+
levels,
|
|
251
|
+
corners,
|
|
252
|
+
centering,
|
|
253
|
+
front: faceFor(levels, seededRandom((base + 1000003) >>> 0)),
|
|
254
|
+
back: faceFor({ ...levels, creases: levels.creases }, seededRandom((base + 2000003) >>> 0)),
|
|
255
|
+
};
|
|
126
256
|
}
|
|
127
257
|
/** Points of one corner from where it leaves the incoming edge to where it joins the outgoing edge, in the corner's frame (a along the way out, b back along the way in). */
|
|
128
258
|
function cornerPoints(corner) {
|
|
129
|
-
const s = corner.kind ===
|
|
130
|
-
if (corner.kind ===
|
|
131
|
-
return [
|
|
132
|
-
|
|
259
|
+
const s = corner.kind === "sharp" ? BASE_RADIUS : corner.size;
|
|
260
|
+
if (corner.kind === "dog-eared")
|
|
261
|
+
return [
|
|
262
|
+
[0, s],
|
|
263
|
+
[s, 0],
|
|
264
|
+
];
|
|
265
|
+
if (corner.kind === "chipped") {
|
|
133
266
|
const [p, q, r, t] = corner.jag;
|
|
134
|
-
return [
|
|
267
|
+
return [
|
|
268
|
+
[0, s],
|
|
269
|
+
[s * p, s * (1 - q) * 0.9],
|
|
270
|
+
[s * q * 0.5, s * r * 0.5],
|
|
271
|
+
[s * (1 - t), s * t],
|
|
272
|
+
[s, 0],
|
|
273
|
+
];
|
|
135
274
|
}
|
|
136
275
|
const points = [];
|
|
137
276
|
for (let step = 0; step <= 6; step++) {
|
|
@@ -145,33 +284,66 @@ const frames = [
|
|
|
145
284
|
{ x: 0, y: 0, a: [1, 0], b: [0, 1] },
|
|
146
285
|
{ x: CARD_W, y: 0, a: [0, 1], b: [-1, 0] },
|
|
147
286
|
{ x: CARD_W, y: CARD_H, a: [-1, 0], b: [0, -1] },
|
|
148
|
-
{ x: 0, y: CARD_H, a: [0, -1], b: [1, 0] }
|
|
287
|
+
{ x: 0, y: CARD_H, a: [0, -1], b: [1, 0] },
|
|
149
288
|
];
|
|
150
289
|
const mirrored = [1, 0, 3, 2];
|
|
151
|
-
const at = (frame, a, b) => [
|
|
290
|
+
const at = (frame, a, b) => [
|
|
291
|
+
round(frame.x + frame.a[0] * a + frame.b[0] * b),
|
|
292
|
+
round(frame.y + frame.a[1] * a + frame.b[1] * b),
|
|
293
|
+
];
|
|
152
294
|
/** The corner a face draws at position `index` (0 top left, clockwise as seen on that face). */
|
|
153
295
|
function faceCorner(features, index, face) {
|
|
154
|
-
return features.corners[face ===
|
|
296
|
+
return features.corners[face === "front" ? index : mirrored[index]];
|
|
155
297
|
}
|
|
156
|
-
/**
|
|
157
|
-
|
|
298
|
+
/**
|
|
299
|
+
* The outline of a face as an SVG path in the 250 by 350 box, corners as worn.
|
|
300
|
+
*
|
|
301
|
+
* @param features - The marks of the card (see {@link wearLayout}).
|
|
302
|
+
* @param face - Which face; the back's corners are the front's seen from behind. Unless said, the front.
|
|
303
|
+
* @returns The path's `d` attribute.
|
|
304
|
+
* @example
|
|
305
|
+
* ```ts
|
|
306
|
+
* import { wearLayout, wearOutline, wearPreset } from "@johnmorrisdotca/fuda-cards";
|
|
307
|
+
*
|
|
308
|
+
* const features = wearLayout(wearPreset("damaged")!.levels, 1);
|
|
309
|
+
* console.log(wearOutline(features).startsWith("M"), wearOutline(features, "back") !== wearOutline(features)); // true true
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
export function wearOutline(features, face = "front") {
|
|
158
313
|
const points = [];
|
|
159
314
|
frames.forEach((frame, index) => {
|
|
160
315
|
for (const [a, b] of cornerPoints(faceCorner(features, index, face)))
|
|
161
316
|
points.push(at(frame, a, b));
|
|
162
317
|
});
|
|
163
|
-
return `M${points.map(([x, y]) => `${x} ${y}`).join(
|
|
318
|
+
return `M${points.map(([x, y]) => `${x} ${y}`).join("L")}Z`;
|
|
164
319
|
}
|
|
165
|
-
function line(m, stroke, width, opacity, extra =
|
|
166
|
-
const nx = -(m.y2 - m.y1)
|
|
167
|
-
const
|
|
320
|
+
function line(m, stroke, width, opacity, extra = "", bend = 0) {
|
|
321
|
+
const nx = -(m.y2 - m.y1);
|
|
322
|
+
const ny = m.x2 - m.x1;
|
|
323
|
+
const len = Math.hypot(nx, ny) || 1;
|
|
324
|
+
const way = bend
|
|
325
|
+
? `Q${round((m.x1 + m.x2) / 2 + (nx / len) * bend)} ${round((m.y1 + m.y2) / 2 + (ny / len) * bend)} ${m.x2} ${m.y2}`
|
|
326
|
+
: `L${m.x2} ${m.y2}`;
|
|
168
327
|
return `<path d="M${m.x1} ${m.y1}${way}" stroke="${stroke}" stroke-width="${width}" opacity="${opacity}" fill="none" stroke-linecap="round"${extra}/>`;
|
|
169
328
|
}
|
|
170
|
-
/**
|
|
171
|
-
|
|
172
|
-
|
|
329
|
+
/**
|
|
330
|
+
* The SVG laid over one face: whitening, folds, creases, scratches, specks, stain and toning. Markup only, no script.
|
|
331
|
+
*
|
|
332
|
+
* @param features - The marks of the card (see {@link wearLayout}).
|
|
333
|
+
* @param face - Which face. Unless said, the front.
|
|
334
|
+
* @returns The `<svg>` markup, in the 250 by 350 box.
|
|
335
|
+
* @example
|
|
336
|
+
* ```ts
|
|
337
|
+
* import { wearLayout, wearOverlaySvg, wearPreset } from "@johnmorrisdotca/fuda-cards";
|
|
338
|
+
*
|
|
339
|
+
* const svg = wearOverlaySvg(wearLayout(wearPreset("near-mint")!.levels));
|
|
340
|
+
* console.log(svg.startsWith("<svg"), /<script|onload/i.test(svg)); // true false
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
export function wearOverlaySvg(features, face = "front") {
|
|
344
|
+
const data = face === "front" ? features.front : features.back;
|
|
173
345
|
const levels = features.levels;
|
|
174
|
-
const paper =
|
|
346
|
+
const paper = "#fffaf0";
|
|
175
347
|
const out = [];
|
|
176
348
|
out.push('<defs><radialGradient id="t" cx="50%" cy="48%" r="75%"><stop offset="45%" stop-color="#b8863a" stop-opacity="0"/><stop offset="100%" stop-color="#7a4a14" stop-opacity="1"/></radialGradient><linearGradient id="f" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#fff" stop-opacity="0.5"/><stop offset="1" stop-color="#000" stop-opacity="0.35"/></linearGradient><radialGradient id="s"><stop offset="0" stop-color="#6b4513" stop-opacity="1"/><stop offset="100%" stop-color="#6b4513" stop-opacity="0"/></radialGradient></defs>');
|
|
177
349
|
if (levels.toning > 0)
|
|
@@ -179,14 +351,18 @@ export function wearOverlaySvg(features, face = 'front') {
|
|
|
179
351
|
if (data.stain)
|
|
180
352
|
out.push(`<circle cx="${data.stain.x}" cy="${data.stain.y}" r="${data.stain.r}" fill="url(#s)" opacity="${data.stain.a}" style="mix-blend-mode:multiply"/>`);
|
|
181
353
|
for (const f of data.specks)
|
|
182
|
-
out.push(`<circle cx="${f.x}" cy="${f.y}" r="${f.r}" fill="${f.light ?
|
|
354
|
+
out.push(`<circle cx="${f.x}" cy="${f.y}" r="${f.r}" fill="${f.light ? "#fff" : "#2b1b08"}" opacity="${f.a}"/>`);
|
|
183
355
|
for (const m of data.scratches)
|
|
184
|
-
out.push(line(m,
|
|
356
|
+
out.push(line(m, "#fff", 0.45, m.a));
|
|
185
357
|
for (const m of data.creases) {
|
|
186
|
-
const nx = -(m.y2 - m.y1)
|
|
358
|
+
const nx = -(m.y2 - m.y1);
|
|
359
|
+
const ny = m.x2 - m.x1;
|
|
360
|
+
const len = Math.hypot(nx, ny) || 1;
|
|
361
|
+
const ox = round((nx / len) * 0.9);
|
|
362
|
+
const oy = round((ny / len) * 0.9);
|
|
187
363
|
const bend = round((m.a - 0.7) * 14);
|
|
188
|
-
out.push(line({ ...m, x1: round(m.x1 + ox), y1: round(m.y1 + oy), x2: round(m.x2 + ox), y2: round(m.y2 + oy) },
|
|
189
|
-
out.push(line(m,
|
|
364
|
+
out.push(line({ ...m, x1: round(m.x1 + ox), y1: round(m.y1 + oy), x2: round(m.x2 + ox), y2: round(m.y2 + oy) }, "#2a1a05", m.heavy ? 1.3 : 0.8, round(m.a * 0.4), "", bend));
|
|
365
|
+
out.push(line(m, "#fff", m.heavy ? 1.1 : 0.6, round(m.a * 0.55), "", bend));
|
|
190
366
|
if (m.heavy)
|
|
191
367
|
out.push(line(m, paper, 1.6, 0.8, ' stroke-dasharray="9 5 3 11 6 4"', bend));
|
|
192
368
|
}
|
|
@@ -195,26 +371,27 @@ export function wearOverlaySvg(features, face = 'front') {
|
|
|
195
371
|
frames.forEach((frame, index) => {
|
|
196
372
|
const corner = faceCorner(features, index, face);
|
|
197
373
|
const pts = cornerPoints(corner).map(([a, b]) => at(frame, a, b));
|
|
198
|
-
if (corner.kind ===
|
|
374
|
+
if (corner.kind === "sharp") {
|
|
199
375
|
if (levels.corners > 0.02)
|
|
200
376
|
out.push(`<circle cx="${at(frame, 1.3, 1.3)[0]}" cy="${at(frame, 1.3, 1.3)[1]}" r="1.3" fill="${paper}" opacity="0.8"/>`);
|
|
201
377
|
return;
|
|
202
378
|
}
|
|
203
|
-
if (corner.kind ===
|
|
204
|
-
const s = corner.size
|
|
205
|
-
const
|
|
379
|
+
if (corner.kind === "dog-eared") {
|
|
380
|
+
const s = corner.size;
|
|
381
|
+
const fold = [at(frame, 0, s), at(frame, s, 0), at(frame, s, s)];
|
|
382
|
+
const flap = face === "front" ? "#183450" : "#e8c46a";
|
|
206
383
|
const shade = [at(frame, 0, s), at(frame, s, 0), at(frame, s + 2.2, s + 2.2)];
|
|
207
|
-
out.push(`<polygon points="${shade.map((p) => p.join(
|
|
208
|
-
out.push(`<polygon points="${fold.map((p) => p.join(
|
|
209
|
-
out.push(`<polygon points="${fold.map((p) => p.join(
|
|
210
|
-
out.push(`<path d="M${pts.map((p) => p.join(
|
|
384
|
+
out.push(`<polygon points="${shade.map((p) => p.join(",")).join(" ")}" fill="#000" opacity="0.3"/>`);
|
|
385
|
+
out.push(`<polygon points="${fold.map((p) => p.join(",")).join(" ")}" fill="${flap}" stroke="${paper}" stroke-width="0.8"/>`);
|
|
386
|
+
out.push(`<polygon points="${fold.map((p) => p.join(",")).join(" ")}" fill="url(#f)" opacity="0.55"/>`);
|
|
387
|
+
out.push(`<path d="M${pts.map((p) => p.join(" ")).join("L")}" stroke="${paper}" stroke-width="1.2" fill="none"/>`);
|
|
211
388
|
return;
|
|
212
389
|
}
|
|
213
|
-
out.push(`<path d="M${pts.map((p) => p.join(
|
|
390
|
+
out.push(`<path d="M${pts.map((p) => p.join(" ")).join("L")}" stroke="${paper}" stroke-width="${corner.kind === "chipped" ? 2.2 : 1.6}" opacity="0.85" fill="none" stroke-linejoin="round"/>`);
|
|
214
391
|
});
|
|
215
|
-
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${CARD_W} ${CARD_H}" preserveAspectRatio="none" aria-hidden="true">${out.join(
|
|
392
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${CARD_W} ${CARD_H}" preserveAspectRatio="none" aria-hidden="true">${out.join("")}</svg>`;
|
|
216
393
|
}
|
|
217
|
-
const STYLE =
|
|
394
|
+
const STYLE = ".wear{position:absolute;pointer-events:none;display:block}.wear svg{display:block;width:100%;height:100%}";
|
|
218
395
|
const maskUrl = (path) => `url("data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${CARD_W} ${CARD_H}" preserveAspectRatio="none"><path d="${path}" fill="#000"/></svg>`)}")`;
|
|
219
396
|
/** Draw a card's wear into its shadow root; calling it again with other options replaces the first. */
|
|
220
397
|
export function applyWear(root, options) {
|
|
@@ -223,29 +400,29 @@ export function applyWear(root, options) {
|
|
|
223
400
|
if (!levels)
|
|
224
401
|
return false;
|
|
225
402
|
const features = wearLayout(levels, options.seed ?? 1);
|
|
226
|
-
const style = document.createElement(
|
|
227
|
-
style.dataset.wear =
|
|
403
|
+
const style = document.createElement("style");
|
|
404
|
+
style.dataset.wear = "";
|
|
228
405
|
style.textContent = STYLE;
|
|
229
406
|
root.prepend(style);
|
|
230
|
-
for (const face of [
|
|
407
|
+
for (const face of ["front", "back"]) {
|
|
231
408
|
const element = root.querySelector(`.face.${face}`);
|
|
232
409
|
if (!element)
|
|
233
410
|
continue;
|
|
234
|
-
const holder = document.createElement(
|
|
235
|
-
holder.className =
|
|
411
|
+
const holder = document.createElement("div");
|
|
412
|
+
holder.className = "wear";
|
|
236
413
|
holder.dataset.wear = face;
|
|
237
|
-
holder.style.inset = face ===
|
|
414
|
+
holder.style.inset = face === "front" ? "-2px" : "-11px";
|
|
238
415
|
holder.innerHTML = wearOverlaySvg(features, face);
|
|
239
416
|
element.append(holder);
|
|
240
417
|
const mask = maskUrl(wearOutline(features, face));
|
|
241
|
-
element.style.setProperty(
|
|
242
|
-
element.style.setProperty(
|
|
243
|
-
element.style.setProperty(
|
|
244
|
-
element.style.setProperty(
|
|
245
|
-
element.style.setProperty(
|
|
246
|
-
element.style.setProperty(
|
|
418
|
+
element.style.setProperty("-webkit-mask-image", mask);
|
|
419
|
+
element.style.setProperty("mask-image", mask);
|
|
420
|
+
element.style.setProperty("-webkit-mask-size", "100% 100%");
|
|
421
|
+
element.style.setProperty("mask-size", "100% 100%");
|
|
422
|
+
element.style.setProperty("-webkit-mask-repeat", "no-repeat");
|
|
423
|
+
element.style.setProperty("mask-repeat", "no-repeat");
|
|
247
424
|
}
|
|
248
|
-
const front = root.querySelector(
|
|
425
|
+
const front = root.querySelector(".face.front");
|
|
249
426
|
if (front) {
|
|
250
427
|
const { x, y } = features.centering;
|
|
251
428
|
front.style.padding = `${13 + y}px ${13 - x}px ${13 - y}px ${13 + x}px`;
|
|
@@ -254,15 +431,22 @@ export function applyWear(root, options) {
|
|
|
254
431
|
}
|
|
255
432
|
/** Take a card's wear away, leaving the card as it was. */
|
|
256
433
|
export function clearWear(root) {
|
|
257
|
-
root.querySelector(
|
|
258
|
-
for (const holder of Array.from(root.querySelectorAll(
|
|
434
|
+
root.querySelector("style[data-wear]")?.remove();
|
|
435
|
+
for (const holder of Array.from(root.querySelectorAll(".wear")))
|
|
259
436
|
holder.remove();
|
|
260
|
-
for (const element of Array.from(root.querySelectorAll(
|
|
261
|
-
for (const name of [
|
|
437
|
+
for (const element of Array.from(root.querySelectorAll(".face"))) {
|
|
438
|
+
for (const name of [
|
|
439
|
+
"-webkit-mask-image",
|
|
440
|
+
"mask-image",
|
|
441
|
+
"-webkit-mask-size",
|
|
442
|
+
"mask-size",
|
|
443
|
+
"-webkit-mask-repeat",
|
|
444
|
+
"mask-repeat",
|
|
445
|
+
])
|
|
262
446
|
element.style.removeProperty(name);
|
|
263
|
-
if (element.classList.contains(
|
|
264
|
-
element.style.removeProperty(
|
|
265
|
-
if (!element.getAttribute(
|
|
266
|
-
element.removeAttribute(
|
|
447
|
+
if (element.classList.contains("front"))
|
|
448
|
+
element.style.removeProperty("padding");
|
|
449
|
+
if (!element.getAttribute("style"))
|
|
450
|
+
element.removeAttribute("style");
|
|
267
451
|
}
|
|
268
452
|
}
|
package/dist/zoom.d.ts
CHANGED