@card3d/element 0.0.0-stage → 0.1.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/README.md +167 -2
- package/THIRD_PARTY_NOTICES.md +1179 -0
- package/dist/index-B303IfOD.d.ts +178 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/src-K3h2YYOX.js +1067 -0
- package/dist/src-K3h2YYOX.js.map +1 -0
- package/dist/standalone/THIRD_PARTY_NOTICES.md +1179 -0
- package/dist/standalone/engine/pkg/__wasm_split.73fe2f969d09.js +807 -0
- package/dist/standalone/engine/pkg/card3d_web.js +2229 -0
- package/dist/standalone/engine/pkg/card3d_web_bg.f386ff807fa5.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_15.ec8d885c7794.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_16.533cbf75497e.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_17.49282dd0fb57.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_18.f08e478bfc75.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_19.252b3e655bf0.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_20.2135645f06f9.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_21.b69f209cbf1d.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_22.d01fdf3fbfd3.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_23.490d40276444.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_24.f87ca446a154.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_25.bc276836672b.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_26.8c5128c4d95d.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_27.2e1a9d9b4329.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_28.15e1ca7d6a05.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_29.234bfd94561f.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_30.146f89b81840.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_31.67785ea6505f.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_32.87b7fe82ba50.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_33.e64dabefe498.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_34.905609f7790c.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_35.9f2cc3541085.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_36.8cafa3f6942b.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_37.824b4655133d.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_38.0230c9fc343b.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_39.0b28f2a1a0f0.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_40.78e405691440.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_41.8351d32a502b.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_42.03c8eb733255.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_43.b7a6f5da40f6.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_44.a44e3211f51d.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_45.54b9109abd6f.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_46.fb1e9170016f.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_47.303189c3b6a5.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_48.45f7eb9ad895.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_49.0210635d1d25.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_50.a1a0fdea7949.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_51.340b369e909f.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_52.79cf8075bbd0.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_53.b2f1c9badb68.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_54.032ce9e3865b.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_55.0c5b98dd0235.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_56.08a66417eaf8.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_57.eabe031b6ffd.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_58.ebfefd151803.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_59.f278f2bd8d95.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_60.eb9000028d3f.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_61.91055dacd775.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_62.88209d8f5b60.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_63.bd17ed5120ca.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_64.c8585c12a4e2.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_65.9403859475e4.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_66.a202e753d66b.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_67.59791b869d30.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_68.407c9c862196.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_69.e380cb171387.wasm +0 -0
- package/dist/standalone/engine/pkg/chunk_70.e66bb3bbc245.wasm +0 -0
- package/dist/standalone/engine/pkg/split_ar.68b5b2be2b46.wasm +0 -0
- package/dist/standalone/engine/pkg/split_edge.00d82d000a8d.wasm +0 -0
- package/dist/standalone/engine/pkg/split_faces.fc97260e855d.wasm +0 -0
- package/dist/standalone/engine/pkg/split_finish.3252460bfde0.wasm +0 -0
- package/dist/standalone/engine/pkg/split_glass.7470ef357223.wasm +0 -0
- package/dist/standalone/engine/pkg/split_holo.b7b964052eaa.wasm +0 -0
- package/dist/standalone/engine/pkg/split_liquid_metal.b0b2fdc9fe88.wasm +0 -0
- package/dist/standalone/engine/pkg/split_marquee.bc89665fa1e2.wasm +0 -0
- package/dist/standalone/engine/pkg/split_print.664abab7225d.wasm +0 -0
- package/dist/standalone/engine/pkg/split_scene.2733c801c2fa.wasm +0 -0
- package/dist/standalone/engine/pkg/split_shape.13ffe276c27a.wasm +0 -0
- package/dist/standalone/engine/pkg/split_shine.347e541260b2.wasm +0 -0
- package/dist/standalone/engine/pkg/split_stickers.d8c2b6da0d03.wasm +0 -0
- package/dist/standalone/engine/pkg/split_vines.4993e1ada27b.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/__wasm_split.c8366a91c493.js +697 -0
- package/dist/standalone/engine/pkg-webgl/card3d_web.js +2086 -0
- package/dist/standalone/engine/pkg-webgl/card3d_web_bg.05633b9d172c.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_15.16f9f6b2cd00.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_16.dcb507b19850.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_17.223f1a3576b5.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_18.45e5283186ee.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_19.dca3d79f08cf.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_20.1dbf23a5f1b4.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_21.d686fedcd96a.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_22.afd6d52bc34c.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_23.be0a486c0df9.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_24.13c68be08fba.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_25.42fd72138f7b.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_26.75cb392ae1db.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_27.ee517bd39f46.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_28.679ecec09ee0.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_29.73198b420fd3.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_30.ac3e4d9765e6.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_31.497a1c4156b3.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_32.1e9d22404d20.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_33.6ec29be3d8bc.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_34.bd35d970d8a4.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_35.a46972b26786.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_36.b90f558426aa.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_37.10f3914ec0da.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_38.2587f7a0f3c9.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_39.dda11140353f.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_40.a3d630e9d4b3.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_41.86e03be66404.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_42.61a2d8a02d40.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_43.84d30ad16c43.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_44.de2413df8344.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_45.f568c4607d1a.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_46.a947bc338311.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_47.362d132966b9.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_48.57fba0df794d.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_49.30bee53b67b1.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_50.92dfcc5391bf.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_51.37e5c2f4ca0c.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_52.1b3f3e7b48cb.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_53.5aae94b7390c.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_54.36a632545c1a.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_55.5f640a369f0e.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_56.32c6c12a4fe6.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_57.e14584204333.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_58.37bcb0332d2a.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_59.2e9091910448.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/chunk_60.db24e95618fd.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_ar.1064e8fb9917.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_edge.887864936b7e.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_faces.d2c8fa3f4aaa.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_finish.2588df318d27.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_glass.6c26fae5b1e8.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_holo.8c0f9f897ef9.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_liquid_metal.24774c465610.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_marquee.c948f6a265d9.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_print.22cd4bc073e4.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_scene.d1f9aafc8306.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_shape.fb50f19c5fc9.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_shine.1f83f6a36061.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_stickers.977aae3b103e.wasm +0 -0
- package/dist/standalone/engine/pkg-webgl/split_vines.3ca138d7e9a3.wasm +0 -0
- package/dist/standalone/environments/cyclorama.webp +0 -0
- package/dist/standalone/environments/loft.webp +0 -0
- package/dist/standalone/environments/softbox.webp +0 -0
- package/dist/standalone/environments/sunset.webp +0 -0
- package/dist/standalone/environments/warm-hall.webp +0 -0
- package/dist/standalone/fonts/ibm-plex-mono-latin-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/ibm-plex-mono-latin-ext-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/inter-latin-ext-wght-normal.woff2 +0 -0
- package/dist/standalone/fonts/inter-latin-wght-normal.woff2 +0 -0
- package/dist/standalone/fonts/licenses/ibm-plex-mono-OFL.txt +93 -0
- package/dist/standalone/fonts/licenses/inter-OFL.txt +93 -0
- package/dist/standalone/fonts/licenses/manrope-OFL.txt +93 -0
- package/dist/standalone/fonts/licenses/playfair-display-OFL.txt +93 -0
- package/dist/standalone/fonts/licenses/space-grotesk-OFL.txt +93 -0
- package/dist/standalone/fonts/licenses/unbounded-OFL.txt +93 -0
- package/dist/standalone/fonts/manrope-latin-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/manrope-latin-ext-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/playfair-display-latin-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/playfair-display-latin-ext-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/space-grotesk-latin-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/space-grotesk-latin-ext-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/unbounded-latin-700-normal.woff2 +0 -0
- package/dist/standalone/fonts/unbounded-latin-ext-700-normal.woff2 +0 -0
- package/dist/standalone/metal-card-ar-BPF4toBh.js +2 -0
- package/dist/standalone/metal-card-marquee-DXQq_HbD.js +2 -0
- package/dist/standalone/metal-card.iife.js +20 -0
- package/dist/standalone/metal-card.js +24 -0
- package/dist/standalone.d.ts +16 -0
- package/dist/standalone.js +13 -0
- package/dist/standalone.js.map +1 -0
- package/package.json +62 -3
- package/skills/embed-metal-card/SKILL.md +147 -0
- package/skills/embed-metal-card/references/attributes.md +75 -0
- package/skills/embed-metal-card/references/serving-and-csp.md +58 -0
|
@@ -0,0 +1,1067 @@
|
|
|
1
|
+
import { CARD_FONTS, LIGHTING_PRESETS, arSupport, cloneDesign, decodeDesignToken, defaultDesign, ensureDesign, familyFromFileName, getPreset, lightingBackdropCss, loadCustomFont, normalizeRecipe, openQuickLook, parseGoogleFontSpec, presetExtras, presetRecipe, sanitizeFontLabel, sanitizeFontUrl, sanitizeScene, sceneViewerUrl } from "@card3d/core/model";
|
|
2
|
+
import { createEngineStage } from "@card3d/core/engine";
|
|
3
|
+
// src/attributes.ts
|
|
4
|
+
/**
|
|
5
|
+
* `<metal-card>` attribute parsing and design resolution. Pure (no DOM, no WebGL), so it is unit
|
|
6
|
+
* tested in Node and shared with `@card3d/react`.
|
|
7
|
+
*/
|
|
8
|
+
/** Turntable speed when `auto-rotate` is present without a number, degrees per second. */
|
|
9
|
+
const DEFAULT_AUTO_ROTATE = 20;
|
|
10
|
+
const MAX_AUTO_ROTATE = 360;
|
|
11
|
+
/**
|
|
12
|
+
* A boolean attribute: absent → `fallback`; present → true, except `"false"`, `"0"`, `"off"` and
|
|
13
|
+
* `"no"` (so `lazy="false"` reads as written).
|
|
14
|
+
*/
|
|
15
|
+
function parseBool(value, fallback) {
|
|
16
|
+
if (value === null || value === void 0) return fallback;
|
|
17
|
+
return !/^\s*(false|0|off|no)\s*$/i.test(value);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* `auto-rotate`: degrees per second (negative spins the other way). Absent → 0 (off); present
|
|
21
|
+
* without a usable number (`auto-rotate`, `auto-rotate="true"`) → `DEFAULT_AUTO_ROTATE`; `"false"` /
|
|
22
|
+
* `"0"` → 0. Clamped to ±360.
|
|
23
|
+
*/
|
|
24
|
+
function parseAutoRotate(value) {
|
|
25
|
+
if (value === null || value === void 0) return 0;
|
|
26
|
+
if (!parseBool(value, true)) return 0;
|
|
27
|
+
const n = Number(value.trim());
|
|
28
|
+
if (value.trim() === "" || !Number.isFinite(n)) return 20;
|
|
29
|
+
return Math.max(-360, Math.min(MAX_AUTO_ROTATE, n));
|
|
30
|
+
}
|
|
31
|
+
/** `interaction`: `drag` (default), `turntable` or `none` (`static` and `false` also mean none). */
|
|
32
|
+
function parseInteraction(value) {
|
|
33
|
+
const v = value?.trim().toLowerCase();
|
|
34
|
+
if (v === "turntable") return "turntable";
|
|
35
|
+
if (v === "none" || v === "static" || v === "false" || v === "off") return "none";
|
|
36
|
+
return "drag";
|
|
37
|
+
}
|
|
38
|
+
/** `fit`: `contain` (default: the card fills the element with a small margin, and stays whole as it turns) or `studio` (Card Studio's camera). */
|
|
39
|
+
function parseFit(value) {
|
|
40
|
+
return value?.trim().toLowerCase() === "studio" ? "studio" : "contain";
|
|
41
|
+
}
|
|
42
|
+
/** `quality`: `full` (default; up to 2× pixel ratio, finer textures) or `performance`. */
|
|
43
|
+
function parseQuality(value) {
|
|
44
|
+
return value?.trim().toLowerCase() === "performance" ? "performance" : "full";
|
|
45
|
+
}
|
|
46
|
+
/** A `#rgb` / `#rrggbb` colour, lower-cased and expanded; null otherwise. */
|
|
47
|
+
function parseHexColor(value) {
|
|
48
|
+
const v = value?.trim().toLowerCase() ?? "";
|
|
49
|
+
if (/^#[0-9a-f]{6}$/.test(v)) return v;
|
|
50
|
+
if (/^#[0-9a-f]{3}$/.test(v)) return `#${v[1]}${v[1]}${v[2]}${v[2]}${v[3]}${v[3]}`;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The `font` attribute: a `CARD_FONTS` id or label, case-insensitive (`space-grotesk`, `Space
|
|
55
|
+
* Grotesk`), or a Google Fonts family as `google:<Family>[@<weight>]` (`google:Fraunces`,
|
|
56
|
+
* `google:Playfair Display@900`). Null when it is neither.
|
|
57
|
+
*/
|
|
58
|
+
function parseFont(value) {
|
|
59
|
+
const v = value?.trim();
|
|
60
|
+
if (!v) return null;
|
|
61
|
+
if (/^google:/i.test(v)) return parseGoogleFontSpec(v);
|
|
62
|
+
const lower = v.toLowerCase();
|
|
63
|
+
return CARD_FONTS.find((f) => f.id === lower || f.label.toLowerCase() === lower)?.id ?? null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* `font-url` (+ `font` as its family name): a web font file (woff2 / woff / ttf / otf) at an https
|
|
67
|
+
* URL or a same-origin path. Null when the URL isn't allowed (`sanitizeFontUrl`).
|
|
68
|
+
*/
|
|
69
|
+
function parseFontUrl(url, family) {
|
|
70
|
+
const src = sanitizeFontUrl(url);
|
|
71
|
+
return src ? {
|
|
72
|
+
source: "url",
|
|
73
|
+
family: sanitizeFontLabel(family) ?? familyFromFileName(src),
|
|
74
|
+
src
|
|
75
|
+
} : null;
|
|
76
|
+
}
|
|
77
|
+
function parseLighting(value) {
|
|
78
|
+
const v = value?.trim().toLowerCase();
|
|
79
|
+
return LIGHTING_PRESETS.find((p) => p.id === v)?.id ?? null;
|
|
80
|
+
}
|
|
81
|
+
/** The `floor` attribute: null = leave the design's floor alone, false = none, else the floor. */
|
|
82
|
+
function parseFloor(value) {
|
|
83
|
+
if (value == null) return null;
|
|
84
|
+
const v = value.trim().toLowerCase();
|
|
85
|
+
if (v === "false" || v === "none" || v === "off" || v === "0") return false;
|
|
86
|
+
if (v === "" || v === "true" || v === "floor") return {};
|
|
87
|
+
const n = Number(v);
|
|
88
|
+
return Number.isFinite(n) ? { reflectivity: Math.min(1, Math.max(0, n)) } : null;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Apply the convenience attributes to a copy of `design`. Unknown values are ignored (and reported
|
|
92
|
+
* in `ignored`), never reset the field: `material="gold"` keeps the design's own material.
|
|
93
|
+
*/
|
|
94
|
+
function applyPatch(design, patch) {
|
|
95
|
+
const out = cloneDesign(design);
|
|
96
|
+
const ignored = [];
|
|
97
|
+
const enumField = (key) => {
|
|
98
|
+
const raw = patch[key]?.trim().toLowerCase();
|
|
99
|
+
if (!raw) return;
|
|
100
|
+
const next = normalizeRecipe({
|
|
101
|
+
...out.recipe,
|
|
102
|
+
[key]: raw
|
|
103
|
+
});
|
|
104
|
+
if (next[key] === raw) out.recipe = next;
|
|
105
|
+
else ignored.push(`${key}="${patch[key]}"`);
|
|
106
|
+
};
|
|
107
|
+
enumField("material");
|
|
108
|
+
enumField("texture");
|
|
109
|
+
enumField("pattern");
|
|
110
|
+
if (patch.color != null && patch.color.trim()) {
|
|
111
|
+
const c = parseHexColor(patch.color);
|
|
112
|
+
if (c) out.recipe = {
|
|
113
|
+
...out.recipe,
|
|
114
|
+
color: c
|
|
115
|
+
};
|
|
116
|
+
else ignored.push(`color="${patch.color}"`);
|
|
117
|
+
}
|
|
118
|
+
if (patch.name != null) out.cardholderName = patch.name;
|
|
119
|
+
if (patch.bio != null) out.cardholderBio = patch.bio;
|
|
120
|
+
if (patch.fontUrl != null && patch.fontUrl.trim()) {
|
|
121
|
+
const f = parseFontUrl(patch.fontUrl, patch.font);
|
|
122
|
+
if (f) out.font = f;
|
|
123
|
+
else ignored.push(`font-url="${patch.fontUrl}"`);
|
|
124
|
+
} else if (patch.font != null && patch.font.trim()) {
|
|
125
|
+
const f = parseFont(patch.font);
|
|
126
|
+
if (f) out.font = f;
|
|
127
|
+
else ignored.push(`font="${patch.font}"`);
|
|
128
|
+
}
|
|
129
|
+
if (patch.lighting != null && patch.lighting.trim()) {
|
|
130
|
+
const l = parseLighting(patch.lighting);
|
|
131
|
+
if (l) out.lighting = l;
|
|
132
|
+
else ignored.push(`lighting="${patch.lighting}"`);
|
|
133
|
+
}
|
|
134
|
+
const scene = { ...out.scene };
|
|
135
|
+
if (patch.environment != null && patch.environment.trim()) {
|
|
136
|
+
const env = patch.environment.trim();
|
|
137
|
+
if (env.toLowerCase() === "studio") delete scene.environment;
|
|
138
|
+
else {
|
|
139
|
+
const checked = sanitizeScene({ environment: /[:/.]/.test(env) ? env : env.toLowerCase() });
|
|
140
|
+
if (checked?.environment) scene.environment = checked.environment;
|
|
141
|
+
else ignored.push(`environment="${patch.environment}"`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const floor = parseFloor(patch.floor);
|
|
145
|
+
if (floor === false) delete scene.floor;
|
|
146
|
+
else if (floor) scene.floor = {
|
|
147
|
+
...scene.floor,
|
|
148
|
+
...floor
|
|
149
|
+
};
|
|
150
|
+
else if (patch.floor != null) ignored.push(`floor="${patch.floor}"`);
|
|
151
|
+
if (patch.environment != null || patch.floor != null) {
|
|
152
|
+
if (Object.keys(scene).length) out.scene = scene;
|
|
153
|
+
else delete out.scene;
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
design: out,
|
|
157
|
+
ignored
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/** The default design with a `CARD_PRESETS` finish (and, for effect presets, its effects). */
|
|
161
|
+
function presetDesign(id) {
|
|
162
|
+
const preset = getPreset(id.trim());
|
|
163
|
+
if (!preset) return null;
|
|
164
|
+
return {
|
|
165
|
+
...defaultDesign(),
|
|
166
|
+
recipe: presetRecipe(preset),
|
|
167
|
+
...presetExtras(preset)
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Resolve the design to show. The base is the most specific complete design given —
|
|
172
|
+
* `design` (property) › `design` attribute (JSON) › `share` › `src` › `preset` › the default card —
|
|
173
|
+
* and the convenience attributes (`material`, `texture`, `pattern`, `color`, `name`, `bio`, `font`,
|
|
174
|
+
* `font-url`, `lighting`) are applied over it. A source that can't be read is reported and the next one is used.
|
|
175
|
+
* Every design goes through `ensureDesign`, exactly like Card Studio's share links and imports.
|
|
176
|
+
*/
|
|
177
|
+
async function resolveDesign(s, fetchJson = defaultFetchJson) {
|
|
178
|
+
const warnings = [];
|
|
179
|
+
let base = null;
|
|
180
|
+
let source = "default";
|
|
181
|
+
const attempt = async (kind, what, get) => {
|
|
182
|
+
if (base) return;
|
|
183
|
+
try {
|
|
184
|
+
base = await get();
|
|
185
|
+
source = kind;
|
|
186
|
+
} catch (err) {
|
|
187
|
+
warnings.push(`${what}: ${err instanceof Error ? err.message : String(err)}`);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
if (s.design != null) await attempt("design", "design property", () => ensureDesign(s.design));
|
|
191
|
+
if (s.designJson?.trim()) await attempt("design", "design attribute", () => ensureDesign(JSON.parse(s.designJson)));
|
|
192
|
+
if (s.share?.trim()) await attempt("share", "share", () => decodeDesignToken(s.share));
|
|
193
|
+
if (s.src?.trim()) await attempt("src", `src "${s.src}"`, async () => ensureDesign(await fetchJson(s.src)));
|
|
194
|
+
if (s.preset?.trim()) await attempt("preset", `preset "${s.preset}"`, () => {
|
|
195
|
+
const d = presetDesign(s.preset);
|
|
196
|
+
if (!d) throw new Error("unknown preset id");
|
|
197
|
+
return d;
|
|
198
|
+
});
|
|
199
|
+
const { design, ignored } = applyPatch(base ?? defaultDesign(), s);
|
|
200
|
+
for (const i of ignored) warnings.push(`ignored ${i}`);
|
|
201
|
+
return {
|
|
202
|
+
design,
|
|
203
|
+
source,
|
|
204
|
+
warnings
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
async function defaultFetchJson(url) {
|
|
208
|
+
const r = await fetch(url);
|
|
209
|
+
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
|
210
|
+
return r.json();
|
|
211
|
+
}
|
|
212
|
+
/** The accessible name: the `label` attribute, else "<cardholder name>, 3D metal card". */
|
|
213
|
+
function accessibleLabel(label, design) {
|
|
214
|
+
if (label?.trim()) return label.trim();
|
|
215
|
+
const name = design?.cardholderName?.trim();
|
|
216
|
+
return name ? `${name}, 3D metal card` : "3D metal card";
|
|
217
|
+
}
|
|
218
|
+
// src/pool.ts
|
|
219
|
+
var ContextPool = class {
|
|
220
|
+
/** Default 12: leaves headroom under the browsers' 16 for the page's own WebGL. */
|
|
221
|
+
limit = 12;
|
|
222
|
+
live = /* @__PURE__ */ new Set();
|
|
223
|
+
waiting = /* @__PURE__ */ new Set();
|
|
224
|
+
/** Parked members whose context isn't gone yet: their slots stay taken. */
|
|
225
|
+
draining = 0;
|
|
226
|
+
get size() {
|
|
227
|
+
return this.live.size;
|
|
228
|
+
}
|
|
229
|
+
/** Parks `victim`; false when its slot frees only later (then the next waiting card gets it). */
|
|
230
|
+
evict(victim) {
|
|
231
|
+
this.live.delete(victim);
|
|
232
|
+
const later = victim.park();
|
|
233
|
+
if (!later) return true;
|
|
234
|
+
this.draining++;
|
|
235
|
+
later.finally(() => {
|
|
236
|
+
this.draining--;
|
|
237
|
+
this.grantNext();
|
|
238
|
+
});
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
/** A slot is free: the first visible waiting card takes it. */
|
|
242
|
+
grantNext() {
|
|
243
|
+
if (this.live.size + this.draining >= this.limit) return;
|
|
244
|
+
for (const w of this.waiting) {
|
|
245
|
+
if (!w.visible) continue;
|
|
246
|
+
this.waiting.delete(w);
|
|
247
|
+
this.live.add(w);
|
|
248
|
+
w.grant();
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** Whether `m` holds a slot now. */
|
|
253
|
+
holds(m) {
|
|
254
|
+
return this.live.has(m);
|
|
255
|
+
}
|
|
256
|
+
/** Ask for a context. True: take it now. False: queued, `grant()` follows when one frees up. */
|
|
257
|
+
request(m) {
|
|
258
|
+
if (this.live.has(m)) return true;
|
|
259
|
+
if (this.live.size + this.draining >= this.limit) {
|
|
260
|
+
let victim = null;
|
|
261
|
+
for (const o of this.live) if (!o.visible && (!victim || o.hiddenSince < victim.hiddenSince)) victim = o;
|
|
262
|
+
if (!victim || !this.evict(victim)) {
|
|
263
|
+
this.waiting.add(m);
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
this.waiting.delete(m);
|
|
268
|
+
this.live.add(m);
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* The member's context is gone (parked, disconnected) or it no longer wants one. `later`: the
|
|
273
|
+
* context goes when it settles (its slot stays taken until then).
|
|
274
|
+
*/
|
|
275
|
+
release(m, later) {
|
|
276
|
+
this.waiting.delete(m);
|
|
277
|
+
if (!this.live.delete(m)) return;
|
|
278
|
+
if (!later) return this.grantNext();
|
|
279
|
+
this.draining++;
|
|
280
|
+
later.finally(() => {
|
|
281
|
+
this.draining--;
|
|
282
|
+
this.grantNext();
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
/** A live member left the viewport: hand its slot to a visible card that is waiting, if any. */
|
|
286
|
+
rebalance() {
|
|
287
|
+
for (const w of this.waiting) {
|
|
288
|
+
if (!w.visible) continue;
|
|
289
|
+
let victim = null;
|
|
290
|
+
for (const o of this.live) if (!o.visible && (!victim || o.hiddenSince < victim.hiddenSince)) victim = o;
|
|
291
|
+
if (!victim) return;
|
|
292
|
+
if (!this.evict(victim)) return;
|
|
293
|
+
this.waiting.delete(w);
|
|
294
|
+
this.live.add(w);
|
|
295
|
+
w.grant();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/** A visible member stopped wanting a context it was waiting for. */
|
|
299
|
+
cancel(m) {
|
|
300
|
+
this.waiting.delete(m);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
const contextPool = new ContextPool();
|
|
304
|
+
// src/element.ts
|
|
305
|
+
/**
|
|
306
|
+
* `<metal-card>`: a 3D metal card from @card3d/core as a custom element (docs: packages/element/README.md),
|
|
307
|
+
* drawn by the card engine (`createEngineStage`: WebGPU, else its WebGL 2 build).
|
|
308
|
+
*/
|
|
309
|
+
/** Attributes that change the design (re-resolved together, debounced). */
|
|
310
|
+
const DESIGN_ATTRIBUTES = [
|
|
311
|
+
"design",
|
|
312
|
+
"share",
|
|
313
|
+
"src",
|
|
314
|
+
"preset",
|
|
315
|
+
"material",
|
|
316
|
+
"texture",
|
|
317
|
+
"pattern",
|
|
318
|
+
"color",
|
|
319
|
+
"name",
|
|
320
|
+
"bio",
|
|
321
|
+
"font",
|
|
322
|
+
"font-url",
|
|
323
|
+
"lighting",
|
|
324
|
+
"environment",
|
|
325
|
+
"floor"
|
|
326
|
+
];
|
|
327
|
+
const OBSERVED = [
|
|
328
|
+
...DESIGN_ATTRIBUTES,
|
|
329
|
+
"interaction",
|
|
330
|
+
"auto-rotate",
|
|
331
|
+
"quality",
|
|
332
|
+
"fit",
|
|
333
|
+
"lazy",
|
|
334
|
+
"paused",
|
|
335
|
+
"label",
|
|
336
|
+
"backdrop",
|
|
337
|
+
"ar",
|
|
338
|
+
"ar-src"
|
|
339
|
+
];
|
|
340
|
+
/** Drag sensitivity of the turntable (degrees per element width), as the stage's own drag. */
|
|
341
|
+
const YAW_PER_WIDTH = 220;
|
|
342
|
+
const FLIP_MS = 620;
|
|
343
|
+
/** Auto-rotation waits this long after the last interaction. */
|
|
344
|
+
const RESUME_AFTER_MS = 1500;
|
|
345
|
+
const DRAG_SLOP_PX = 6;
|
|
346
|
+
const CLICK_MS = 800;
|
|
347
|
+
const STYLE = `
|
|
348
|
+
:host { display: inline-block; position: relative; width: 360px; max-width: 100%; aspect-ratio: 8 / 5; vertical-align: top; contain: layout paint; -webkit-tap-highlight-color: transparent; }
|
|
349
|
+
:host([hidden]) { display: none; }
|
|
350
|
+
:host(:focus-visible) { outline: 2px solid Highlight; outline: 2px solid -webkit-focus-ring-color; outline-offset: 2px; }
|
|
351
|
+
.bg, canvas, .still { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
|
|
352
|
+
.bg { border-radius: inherit; }
|
|
353
|
+
.still { pointer-events: none; }
|
|
354
|
+
.ar { position: absolute; z-index: 2; right: 10px; bottom: 10px; display: inline-flex; align-items: center; gap: 6px; padding: 6px 11px 6px 9px; border: 0; border-radius: 999px; font: 600 12px/1.2 system-ui, -apple-system, sans-serif; color: #111; background: rgba(255, 255, 255, 0.86); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); cursor: pointer; -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); }
|
|
355
|
+
.ar[hidden] { display: none; }
|
|
356
|
+
.ar:focus-visible { outline: 2px solid Highlight; outline-offset: 2px; }
|
|
357
|
+
.ar[aria-busy="true"] { opacity: 0.7; cursor: progress; }
|
|
358
|
+
.ar svg { width: 15px; height: 15px; }
|
|
359
|
+
`;
|
|
360
|
+
const AR_ICON = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round" aria-hidden="true"><path d="M12 2.8 20 7.4v9.2L12 21.2 4 16.6V7.4z"/><path d="M4 7.4 12 12l8-4.6M12 12v9.2"/></svg>`;
|
|
361
|
+
const prefersReducedMotion = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
362
|
+
const expoOut = (k) => k >= 1 ? 1 : 1 - Math.pow(2, -10 * k);
|
|
363
|
+
const ElementBase = typeof HTMLElement !== "undefined" ? HTMLElement : function MetalCardElementBase() {};
|
|
364
|
+
/**
|
|
365
|
+
* The design's custom face shader layers in GLSL (from the three.js engine's era): the engine draws
|
|
366
|
+
* WGSL only, so it skips them and draws the rest of the card. Their indexes in `design.shaders`.
|
|
367
|
+
*/
|
|
368
|
+
const glslLayers = (design) => (design.shaders ?? []).flatMap((l, i) => l.id === "custom" && l.language !== "wgsl" && l.enabled !== false ? [i] : []);
|
|
369
|
+
/**
|
|
370
|
+
* `<metal-card>`. Design: `design` (JSON), `share` (a Card Studio share link / `#d=` hash / token),
|
|
371
|
+
* `src` (URL of a design JSON), `preset`, plus `material`, `texture`, `pattern`, `color`, `name`,
|
|
372
|
+
* `bio`, `font` (an id, `google:<Family>[@weight]`, or with `font-url` a web font file's family),
|
|
373
|
+
* `lighting` over it; or the `design` property. Behaviour: `interaction` (drag |
|
|
374
|
+
* turntable | none), `auto-rotate` (deg/s), `quality`, `fit` (contain | studio), `lazy`, `paused`, `label`, `backdrop`.
|
|
375
|
+
* Events: `metal-card-ready` (detail { design, stage }) whenever a design is on screen,
|
|
376
|
+
* `metal-card-error` (detail { message }) when an input can't be used.
|
|
377
|
+
*/
|
|
378
|
+
var MetalCardElement = class extends ElementBase {
|
|
379
|
+
static get observedAttributes() {
|
|
380
|
+
return [...OBSERVED];
|
|
381
|
+
}
|
|
382
|
+
/** Most cards with a live WebGL context at once, page-wide (default 12). See README "Many cards". */
|
|
383
|
+
static get maxContexts() {
|
|
384
|
+
return contextPool.limit;
|
|
385
|
+
}
|
|
386
|
+
static set maxContexts(n) {
|
|
387
|
+
if (Number.isFinite(n) && n >= 1) contextPool.limit = Math.floor(n);
|
|
388
|
+
}
|
|
389
|
+
/** Cards holding a WebGL context right now. */
|
|
390
|
+
static get liveContexts() {
|
|
391
|
+
return contextPool.size;
|
|
392
|
+
}
|
|
393
|
+
#shadow = null;
|
|
394
|
+
#bg = null;
|
|
395
|
+
#canvas = null;
|
|
396
|
+
#still = null;
|
|
397
|
+
#stage = null;
|
|
398
|
+
/** A disposed engine stage whose GPU context isn't gone yet (it was still starting). */
|
|
399
|
+
#releasing = null;
|
|
400
|
+
#design = null;
|
|
401
|
+
#designProp = void 0;
|
|
402
|
+
#resolving = 0;
|
|
403
|
+
#debounce;
|
|
404
|
+
#ready = false;
|
|
405
|
+
#observing = false;
|
|
406
|
+
#io = null;
|
|
407
|
+
#ro = null;
|
|
408
|
+
#mq = null;
|
|
409
|
+
#visible = false;
|
|
410
|
+
#hiddenSince = 0;
|
|
411
|
+
#waiting = false;
|
|
412
|
+
/** Interaction state carried across a park (the context released while off screen). */
|
|
413
|
+
#saved = null;
|
|
414
|
+
#yaw = 0;
|
|
415
|
+
#velocity = 0;
|
|
416
|
+
#flip = null;
|
|
417
|
+
#holdUntil = 0;
|
|
418
|
+
#frame = 0;
|
|
419
|
+
#last = 0;
|
|
420
|
+
#drag = null;
|
|
421
|
+
#cleanup = [];
|
|
422
|
+
#arButton = null;
|
|
423
|
+
/** The USDZ made for the design on screen (Quick Look), kept while that design is shown. */
|
|
424
|
+
#usdz = null;
|
|
425
|
+
/**
|
|
426
|
+
* The design on screen (resolved from the attributes, null until resolved). Setting it takes any
|
|
427
|
+
* design object Card Studio can open, or its JSON; it wins over the `design` / `share` / `src` /
|
|
428
|
+
* `preset` attributes, and the convenience attributes still apply over it.
|
|
429
|
+
*/
|
|
430
|
+
get design() {
|
|
431
|
+
return this.#design;
|
|
432
|
+
}
|
|
433
|
+
set design(value) {
|
|
434
|
+
if (typeof value === "string") try {
|
|
435
|
+
value = value.trim() ? JSON.parse(value) : void 0;
|
|
436
|
+
} catch {}
|
|
437
|
+
this.#designProp = value ?? void 0;
|
|
438
|
+
this.#scheduleDesign(0);
|
|
439
|
+
}
|
|
440
|
+
/** Camera framing (the `fit` attribute): "contain" (default) fills the element; "studio" is Card Studio's camera. */
|
|
441
|
+
get fit() {
|
|
442
|
+
return parseFit(this.getAttribute("fit"));
|
|
443
|
+
}
|
|
444
|
+
set fit(value) {
|
|
445
|
+
this.setAttribute("fit", value === "studio" ? "studio" : "contain");
|
|
446
|
+
}
|
|
447
|
+
/** The live stage (null while the card has no GPU context: not yet visible, parked, or disconnected). */
|
|
448
|
+
get stage() {
|
|
449
|
+
return this.#stage;
|
|
450
|
+
}
|
|
451
|
+
/** True once a design is on screen (until the next design change or the context is released). */
|
|
452
|
+
get ready() {
|
|
453
|
+
return this.#ready;
|
|
454
|
+
}
|
|
455
|
+
/** Turn the card over. */
|
|
456
|
+
flip() {
|
|
457
|
+
if (this.#stage?.interaction) this.#stage.interaction.toggleFlip();
|
|
458
|
+
else this.#flipTurntable();
|
|
459
|
+
}
|
|
460
|
+
/** Back to the front, facing the viewer. */
|
|
461
|
+
reset() {
|
|
462
|
+
this.#stage?.interaction?.reset();
|
|
463
|
+
this.#flip = null;
|
|
464
|
+
this.#velocity = 0;
|
|
465
|
+
this.#yaw = 0;
|
|
466
|
+
this.#stage?.setRotation([
|
|
467
|
+
0,
|
|
468
|
+
0,
|
|
469
|
+
0
|
|
470
|
+
]);
|
|
471
|
+
}
|
|
472
|
+
get visible() {
|
|
473
|
+
return this.#visible;
|
|
474
|
+
}
|
|
475
|
+
get hiddenSince() {
|
|
476
|
+
return this.#hiddenSince;
|
|
477
|
+
}
|
|
478
|
+
/** The pool needs this card's context: keep the last frame as a still and release it. */
|
|
479
|
+
park() {
|
|
480
|
+
const stage = this.#stage;
|
|
481
|
+
if (!stage) return this.#destroyStage();
|
|
482
|
+
const i = stage.interaction;
|
|
483
|
+
this.#saved = i ? {
|
|
484
|
+
rot: { ...i.rot },
|
|
485
|
+
flipped: i.flipped
|
|
486
|
+
} : null;
|
|
487
|
+
this.#keepStill(stage);
|
|
488
|
+
return this.#destroyStage();
|
|
489
|
+
}
|
|
490
|
+
grant() {
|
|
491
|
+
this.#waiting = false;
|
|
492
|
+
if (this.isConnected && !this.#stage) this.#start();
|
|
493
|
+
else contextPool.release(this);
|
|
494
|
+
}
|
|
495
|
+
connectedCallback() {
|
|
496
|
+
this.#ensureShadow();
|
|
497
|
+
if (!this.#observing) this.#observe();
|
|
498
|
+
this.#updateLabel();
|
|
499
|
+
if (!this.#design) this.#scheduleDesign(0);
|
|
500
|
+
this.#maybeStart();
|
|
501
|
+
}
|
|
502
|
+
disconnectedCallback() {
|
|
503
|
+
queueMicrotask(() => {
|
|
504
|
+
if (!this.isConnected) this.#teardown();
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
attributeChangedCallback(name, old, value) {
|
|
508
|
+
if (old === value) return;
|
|
509
|
+
if (DESIGN_ATTRIBUTES.includes(name)) {
|
|
510
|
+
this.#scheduleDesign(30);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
switch (name) {
|
|
514
|
+
case "interaction":
|
|
515
|
+
this.#ready = false;
|
|
516
|
+
if (this.#stage) contextPool.release(this, this.#destroyStage());
|
|
517
|
+
this.#yaw = 0;
|
|
518
|
+
this.#maybeStart();
|
|
519
|
+
break;
|
|
520
|
+
case "quality":
|
|
521
|
+
this.#stage?.setQuality(parseQuality(value));
|
|
522
|
+
break;
|
|
523
|
+
case "fit":
|
|
524
|
+
this.#stage?.setFit(parseFit(value));
|
|
525
|
+
break;
|
|
526
|
+
case "paused":
|
|
527
|
+
this.#syncPaused();
|
|
528
|
+
break;
|
|
529
|
+
case "lazy":
|
|
530
|
+
this.#maybeStart();
|
|
531
|
+
break;
|
|
532
|
+
case "label":
|
|
533
|
+
this.#updateLabel();
|
|
534
|
+
break;
|
|
535
|
+
case "backdrop":
|
|
536
|
+
this.#updateBackdrop();
|
|
537
|
+
break;
|
|
538
|
+
case "auto-rotate":
|
|
539
|
+
this.#kick();
|
|
540
|
+
break;
|
|
541
|
+
case "ar":
|
|
542
|
+
case "ar-src": this.#updateAr();
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
#ensureShadow() {
|
|
546
|
+
if (this.#shadow) return;
|
|
547
|
+
this.#shadow = this.attachShadow({ mode: "open" });
|
|
548
|
+
const style = document.createElement("style");
|
|
549
|
+
style.textContent = STYLE;
|
|
550
|
+
this.#bg = document.createElement("div");
|
|
551
|
+
this.#bg.className = "bg";
|
|
552
|
+
this.#bg.setAttribute("part", "backdrop");
|
|
553
|
+
this.#shadow.append(style, this.#bg);
|
|
554
|
+
this.#updateAr();
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Show the AR button where AR is available: iOS / iPadOS (Quick Look; the USDZ is made in the page,
|
|
558
|
+
* or `ios-src`) and Android with `ar-src` (Scene Viewer needs a hosted GLB). Hidden elsewhere.
|
|
559
|
+
*/
|
|
560
|
+
#updateAr() {
|
|
561
|
+
if (!this.#shadow) return;
|
|
562
|
+
const support = this.hasAttribute("ar") && parseBool(this.getAttribute("ar"), true) ? arSupport() : {
|
|
563
|
+
quickLook: false,
|
|
564
|
+
sceneViewer: false
|
|
565
|
+
};
|
|
566
|
+
if (!(support.quickLook || support.sceneViewer && !!this.getAttribute("ar-src")?.trim())) {
|
|
567
|
+
if (this.#arButton) this.#arButton.hidden = true;
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if (!this.#arButton) {
|
|
571
|
+
const b = document.createElement("button");
|
|
572
|
+
b.type = "button";
|
|
573
|
+
b.className = "ar";
|
|
574
|
+
b.setAttribute("part", "ar-button");
|
|
575
|
+
b.innerHTML = `${AR_ICON}<span>View in AR</span>`;
|
|
576
|
+
b.addEventListener("pointerdown", (e) => {
|
|
577
|
+
e.stopPropagation();
|
|
578
|
+
if (arSupport().quickLook) this.#usdzUrl()?.catch(() => {});
|
|
579
|
+
});
|
|
580
|
+
b.addEventListener("click", (e) => {
|
|
581
|
+
e.stopPropagation();
|
|
582
|
+
this.viewInAr();
|
|
583
|
+
});
|
|
584
|
+
b.addEventListener("keydown", (e) => e.stopPropagation());
|
|
585
|
+
this.#arButton = b;
|
|
586
|
+
this.#shadow.append(b);
|
|
587
|
+
}
|
|
588
|
+
this.#arButton.hidden = false;
|
|
589
|
+
}
|
|
590
|
+
/** The USDZ of the design on screen (made once per design on the stage's GPU; the engine's AR part loads on first use). */
|
|
591
|
+
#usdzUrl() {
|
|
592
|
+
const stage = this.#stage;
|
|
593
|
+
const design = this.#design;
|
|
594
|
+
if (!stage || !design) return null;
|
|
595
|
+
if (this.#usdz?.design === design) return this.#usdz.url;
|
|
596
|
+
this.#releaseUsdz();
|
|
597
|
+
const url = stage.exportAr({ format: "usdz" }).then((b) => new Uint8Array(b)).then((b) => URL.createObjectURL(new Blob([b], { type: "model/vnd.usdz+zip" })));
|
|
598
|
+
this.#usdz = {
|
|
599
|
+
design,
|
|
600
|
+
url
|
|
601
|
+
};
|
|
602
|
+
return url;
|
|
603
|
+
}
|
|
604
|
+
#releaseUsdz() {
|
|
605
|
+
const old = this.#usdz;
|
|
606
|
+
this.#usdz = null;
|
|
607
|
+
old?.url.then((u) => URL.revokeObjectURL(u), () => {});
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Open the card in AR: Quick Look on iOS / iPadOS (`ios-src`, or a USDZ made from the card on screen),
|
|
611
|
+
* Scene Viewer on Android (`ar-src`, a hosted GLB). Call from a user gesture. Resolves false when this
|
|
612
|
+
* device has no AR viewer for the card.
|
|
613
|
+
*/
|
|
614
|
+
async viewInAr() {
|
|
615
|
+
const support = arSupport();
|
|
616
|
+
const b = this.#arButton;
|
|
617
|
+
if (support.quickLook) {
|
|
618
|
+
const iosSrc = this.getAttribute("ios-src")?.trim();
|
|
619
|
+
try {
|
|
620
|
+
b?.setAttribute("aria-busy", "true");
|
|
621
|
+
const url = iosSrc || await this.#usdzUrl();
|
|
622
|
+
if (!url) return false;
|
|
623
|
+
openQuickLook(url);
|
|
624
|
+
this.dispatchEvent(new CustomEvent("metal-card-ar", { detail: {
|
|
625
|
+
viewer: "quick-look",
|
|
626
|
+
url
|
|
627
|
+
} }));
|
|
628
|
+
return true;
|
|
629
|
+
} catch (err) {
|
|
630
|
+
const message = `AR export failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
631
|
+
console.warn(`<metal-card>: ${message}`);
|
|
632
|
+
this.dispatchEvent(new CustomEvent("metal-card-error", { detail: { message } }));
|
|
633
|
+
return false;
|
|
634
|
+
} finally {
|
|
635
|
+
b?.removeAttribute("aria-busy");
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
const glb = this.getAttribute("ar-src")?.trim();
|
|
639
|
+
if (support.sceneViewer && glb) {
|
|
640
|
+
const url = sceneViewerUrl(new URL(glb, location.href).href, { title: this.#design?.cardholderName || "Card" });
|
|
641
|
+
this.dispatchEvent(new CustomEvent("metal-card-ar", { detail: {
|
|
642
|
+
viewer: "scene-viewer",
|
|
643
|
+
url
|
|
644
|
+
} }));
|
|
645
|
+
location.href = url;
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
return false;
|
|
649
|
+
}
|
|
650
|
+
#observe() {
|
|
651
|
+
this.#observing = true;
|
|
652
|
+
if (typeof IntersectionObserver !== "undefined") {
|
|
653
|
+
this.#io = new IntersectionObserver((entries) => this.#setVisible(entries[entries.length - 1].isIntersecting), { rootMargin: "200px" });
|
|
654
|
+
this.#io.observe(this);
|
|
655
|
+
} else this.#visible = true;
|
|
656
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
657
|
+
this.#ro = new ResizeObserver(() => this.#maybeStart());
|
|
658
|
+
this.#ro.observe(this);
|
|
659
|
+
}
|
|
660
|
+
if (typeof matchMedia === "function") {
|
|
661
|
+
this.#mq = matchMedia("(prefers-reduced-motion: reduce)");
|
|
662
|
+
this.#mq.addEventListener("change", this.#onMotionPreference);
|
|
663
|
+
}
|
|
664
|
+
const on = (type, fn) => {
|
|
665
|
+
this.addEventListener(type, fn);
|
|
666
|
+
this.#cleanup.push(() => this.removeEventListener(type, fn));
|
|
667
|
+
};
|
|
668
|
+
on("pointerdown", (e) => this.#onPointerDown(e));
|
|
669
|
+
on("pointermove", (e) => this.#onPointerMove(e));
|
|
670
|
+
on("pointerup", (e) => this.#onPointerUp(e));
|
|
671
|
+
on("pointercancel", () => this.#endDrag());
|
|
672
|
+
on("keydown", (e) => this.#onKey(e));
|
|
673
|
+
const onVisibility = () => this.#syncPaused();
|
|
674
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
675
|
+
this.#cleanup.push(() => document.removeEventListener("visibilitychange", onVisibility));
|
|
676
|
+
}
|
|
677
|
+
#teardown() {
|
|
678
|
+
clearTimeout(this.#debounce);
|
|
679
|
+
this.#resolving++;
|
|
680
|
+
this.#observing = false;
|
|
681
|
+
this.#io?.disconnect();
|
|
682
|
+
this.#ro?.disconnect();
|
|
683
|
+
this.#mq?.removeEventListener("change", this.#onMotionPreference);
|
|
684
|
+
this.#io = this.#ro = this.#mq = null;
|
|
685
|
+
for (const fn of this.#cleanup.splice(0)) fn();
|
|
686
|
+
cancelAnimationFrame(this.#frame);
|
|
687
|
+
this.#frame = 0;
|
|
688
|
+
if (this.#waiting) contextPool.cancel(this);
|
|
689
|
+
this.#waiting = false;
|
|
690
|
+
if (this.#stage) contextPool.release(this, this.#destroyStage());
|
|
691
|
+
this.#still?.remove();
|
|
692
|
+
this.#still = null;
|
|
693
|
+
this.#visible = false;
|
|
694
|
+
this.#saved = null;
|
|
695
|
+
this.#releaseUsdz();
|
|
696
|
+
}
|
|
697
|
+
#onMotionPreference = () => this.#kick();
|
|
698
|
+
#setVisible(visible) {
|
|
699
|
+
if (visible === this.#visible) return;
|
|
700
|
+
this.#visible = visible;
|
|
701
|
+
if (!visible) {
|
|
702
|
+
this.#hiddenSince = performance.now();
|
|
703
|
+
this.#syncPaused();
|
|
704
|
+
contextPool.rebalance();
|
|
705
|
+
} else {
|
|
706
|
+
this.#syncPaused();
|
|
707
|
+
this.#maybeStart();
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
#lazy() {
|
|
711
|
+
return parseBool(this.getAttribute("lazy"), true);
|
|
712
|
+
}
|
|
713
|
+
#interaction() {
|
|
714
|
+
return parseInteraction(this.getAttribute("interaction"));
|
|
715
|
+
}
|
|
716
|
+
#maybeStart() {
|
|
717
|
+
if (this.#stage || this.#waiting || !this.isConnected || !this.#shadow) return;
|
|
718
|
+
if (this.#lazy() && !this.#visible) return;
|
|
719
|
+
if (!this.clientWidth || !this.clientHeight) return;
|
|
720
|
+
if (contextPool.request(this)) this.#start();
|
|
721
|
+
else this.#waiting = true;
|
|
722
|
+
}
|
|
723
|
+
#start() {
|
|
724
|
+
const canvas = document.createElement("canvas");
|
|
725
|
+
canvas.setAttribute("part", "canvas");
|
|
726
|
+
canvas.setAttribute("aria-hidden", "true");
|
|
727
|
+
if (this.#still) this.#shadow.insertBefore(canvas, this.#still);
|
|
728
|
+
else this.#shadow.append(canvas);
|
|
729
|
+
this.#canvas = canvas;
|
|
730
|
+
this.#stage = createEngineStage(canvas, {
|
|
731
|
+
quality: parseQuality(this.getAttribute("quality")),
|
|
732
|
+
fit: parseFit(this.getAttribute("fit")),
|
|
733
|
+
interactive: this.#interaction() === "drag",
|
|
734
|
+
intro: false,
|
|
735
|
+
element: this,
|
|
736
|
+
label: accessibleLabel(this.getAttribute("label"), this.#design),
|
|
737
|
+
onCanvas: (next) => {
|
|
738
|
+
next.setAttribute("part", "canvas");
|
|
739
|
+
next.setAttribute("aria-hidden", "true");
|
|
740
|
+
this.#canvas = next;
|
|
741
|
+
}
|
|
742
|
+
});
|
|
743
|
+
this.#started();
|
|
744
|
+
}
|
|
745
|
+
#started() {
|
|
746
|
+
this.#syncPaused();
|
|
747
|
+
this.#applyInteractionMode();
|
|
748
|
+
if (this.#design) this.#show(this.#design);
|
|
749
|
+
}
|
|
750
|
+
/** Disposes the stage; a promise when its context goes only later (an engine still starting). */
|
|
751
|
+
#destroyStage() {
|
|
752
|
+
const stage = this.#stage;
|
|
753
|
+
if (!stage) return this.#releasing ?? void 0;
|
|
754
|
+
this.#stage = null;
|
|
755
|
+
this.#ready = false;
|
|
756
|
+
stage.dispose();
|
|
757
|
+
this.#canvas?.remove();
|
|
758
|
+
this.#canvas = null;
|
|
759
|
+
const released = stage.released;
|
|
760
|
+
this.#releasing = released;
|
|
761
|
+
released.finally(() => {
|
|
762
|
+
if (this.#releasing === released) this.#releasing = null;
|
|
763
|
+
});
|
|
764
|
+
return released;
|
|
765
|
+
}
|
|
766
|
+
/** Copy the stage's current frame into a 2D canvas that stays on screen while it has no context. */
|
|
767
|
+
#keepStill(stage) {
|
|
768
|
+
const src = this.#canvas;
|
|
769
|
+
if (!src || !src.width || !src.height) return;
|
|
770
|
+
const still = this.#still ?? document.createElement("canvas");
|
|
771
|
+
still.className = "still";
|
|
772
|
+
still.setAttribute("aria-hidden", "true");
|
|
773
|
+
still.width = src.width;
|
|
774
|
+
still.height = src.height;
|
|
775
|
+
stage.render();
|
|
776
|
+
const ctx = still.getContext("2d");
|
|
777
|
+
if (!ctx) return;
|
|
778
|
+
ctx.clearRect(0, 0, still.width, still.height);
|
|
779
|
+
ctx.drawImage(src, 0, 0);
|
|
780
|
+
if (!still.isConnected) this.#shadow.append(still);
|
|
781
|
+
this.#still = still;
|
|
782
|
+
}
|
|
783
|
+
#sources() {
|
|
784
|
+
const a = (n) => this.getAttribute(n);
|
|
785
|
+
return {
|
|
786
|
+
design: this.#designProp,
|
|
787
|
+
designJson: a("design"),
|
|
788
|
+
share: a("share"),
|
|
789
|
+
src: a("src"),
|
|
790
|
+
preset: a("preset"),
|
|
791
|
+
material: a("material"),
|
|
792
|
+
texture: a("texture"),
|
|
793
|
+
pattern: a("pattern"),
|
|
794
|
+
color: a("color"),
|
|
795
|
+
name: a("name"),
|
|
796
|
+
bio: a("bio"),
|
|
797
|
+
font: a("font"),
|
|
798
|
+
fontUrl: a("font-url"),
|
|
799
|
+
lighting: a("lighting"),
|
|
800
|
+
environment: a("environment"),
|
|
801
|
+
floor: a("floor")
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
#scheduleDesign(ms) {
|
|
805
|
+
clearTimeout(this.#debounce);
|
|
806
|
+
this.#debounce = setTimeout(() => void this.#resolveDesign(), ms);
|
|
807
|
+
}
|
|
808
|
+
async #resolveDesign() {
|
|
809
|
+
const mine = ++this.#resolving;
|
|
810
|
+
const { design, warnings } = await resolveDesign(this.#sources());
|
|
811
|
+
if (mine !== this.#resolving) return;
|
|
812
|
+
for (const message of warnings) {
|
|
813
|
+
console.warn(`<metal-card>: ${message}`);
|
|
814
|
+
this.dispatchEvent(new CustomEvent("metal-card-error", { detail: { message } }));
|
|
815
|
+
}
|
|
816
|
+
this.#design = design;
|
|
817
|
+
this.#updateLabel();
|
|
818
|
+
this.#updateBackdrop();
|
|
819
|
+
if (this.#stage) await this.#show(design);
|
|
820
|
+
}
|
|
821
|
+
async #show(design) {
|
|
822
|
+
const stage = this.#stage;
|
|
823
|
+
if (!stage) return;
|
|
824
|
+
this.#ready = false;
|
|
825
|
+
await stage.setDesign(design);
|
|
826
|
+
if (stage !== this.#stage || design !== this.#design) return;
|
|
827
|
+
this.#reportFont(design);
|
|
828
|
+
this.#reportGlsl(design);
|
|
829
|
+
const i = stage.interaction;
|
|
830
|
+
if (i && this.#saved) i.restore(this.#saved.rot, this.#saved.flipped);
|
|
831
|
+
this.#saved = null;
|
|
832
|
+
stage.setRotation([
|
|
833
|
+
0,
|
|
834
|
+
this.#yaw * Math.PI / 180,
|
|
835
|
+
0
|
|
836
|
+
]);
|
|
837
|
+
this.#updateLabel();
|
|
838
|
+
this.#kick();
|
|
839
|
+
requestAnimationFrame(() => {
|
|
840
|
+
if (stage !== this.#stage) return;
|
|
841
|
+
if (stage.paused) stage.render();
|
|
842
|
+
this.#still?.remove();
|
|
843
|
+
this.#still = null;
|
|
844
|
+
this.#ready = true;
|
|
845
|
+
this.dispatchEvent(new CustomEvent("metal-card-ready", { detail: {
|
|
846
|
+
design,
|
|
847
|
+
stage
|
|
848
|
+
} }));
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
/** The design this card last reported GLSL layers for (once per design, not per context). */
|
|
852
|
+
#glslReported = null;
|
|
853
|
+
/** Custom GLSL face layers, which the engine skips (it draws WGSL): say so once per design. */
|
|
854
|
+
#reportGlsl(design) {
|
|
855
|
+
const skipped = glslLayers(design);
|
|
856
|
+
if (!skipped.length || this.#glslReported === design) return;
|
|
857
|
+
this.#glslReported = design;
|
|
858
|
+
const n = skipped.length;
|
|
859
|
+
const message = `face shader layer${n > 1 ? "s" : ""} ${skipped.join(", ")}: custom GLSL, which the card engine doesn't run (it runs WGSL); ${n > 1 ? "they are" : "it is"} skipped and the rest of the card is drawn`;
|
|
860
|
+
console.warn(`<metal-card>: ${message}`);
|
|
861
|
+
this.dispatchEvent(new CustomEvent("metal-card-error", { detail: { message } }));
|
|
862
|
+
}
|
|
863
|
+
/** A custom font that couldn't be loaded (the card shows the default font): say so once per design. */
|
|
864
|
+
#reportFont(design) {
|
|
865
|
+
const font = design.font;
|
|
866
|
+
if (!font || typeof font !== "object") return;
|
|
867
|
+
loadCustomFont(font).then((r) => {
|
|
868
|
+
if (r.ok || design !== this.#design) return;
|
|
869
|
+
this.dispatchEvent(new CustomEvent("metal-card-error", { detail: { message: `font "${font.family}" (${font.source}): ${r.error}; showing the default font` } }));
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
#updateLabel() {
|
|
873
|
+
const label = accessibleLabel(this.getAttribute("label"), this.#design);
|
|
874
|
+
const i = this.#stage?.interaction;
|
|
875
|
+
if (i) {
|
|
876
|
+
this.removeAttribute("aria-roledescription");
|
|
877
|
+
i.setLabel(label);
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
if (this.#interaction() === "turntable") {
|
|
881
|
+
this.setAttribute("role", "button");
|
|
882
|
+
if (!this.hasAttribute("tabindex")) this.tabIndex = 0;
|
|
883
|
+
this.setAttribute("aria-roledescription", "3D card");
|
|
884
|
+
this.setAttribute("aria-label", `${label}; drag or use the arrow keys to turn it, Enter to flip`);
|
|
885
|
+
} else {
|
|
886
|
+
this.removeAttribute("aria-roledescription");
|
|
887
|
+
this.setAttribute("role", this.#interaction() === "none" ? "img" : "button");
|
|
888
|
+
this.setAttribute("aria-label", label);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
#updateBackdrop() {
|
|
892
|
+
if (!this.#bg) return;
|
|
893
|
+
this.#bg.style.background = this.hasAttribute("backdrop") && parseBool(this.getAttribute("backdrop"), true) ? lightingBackdropCss(this.#design?.lighting) : "";
|
|
894
|
+
}
|
|
895
|
+
#shouldPause() {
|
|
896
|
+
return parseBool(this.getAttribute("paused"), false) || !this.#visible || typeof document !== "undefined" && document.hidden;
|
|
897
|
+
}
|
|
898
|
+
#syncPaused() {
|
|
899
|
+
this.#stage?.setPaused(this.#shouldPause());
|
|
900
|
+
this.#kick();
|
|
901
|
+
}
|
|
902
|
+
#applyInteractionMode() {
|
|
903
|
+
const mode = this.#interaction();
|
|
904
|
+
if (mode === "turntable") this.style.touchAction = "pan-y";
|
|
905
|
+
else if (mode === "none") {
|
|
906
|
+
this.style.touchAction = "";
|
|
907
|
+
this.removeAttribute("tabindex");
|
|
908
|
+
}
|
|
909
|
+
this.#updateLabel();
|
|
910
|
+
}
|
|
911
|
+
#autoRotateSpeed() {
|
|
912
|
+
return prefersReducedMotion() ? 0 : parseAutoRotate(this.getAttribute("auto-rotate"));
|
|
913
|
+
}
|
|
914
|
+
/** (Re)start the motion loop when something needs it. */
|
|
915
|
+
#kick() {
|
|
916
|
+
if (this.#frame || !this.#stage || this.#stage.paused) return;
|
|
917
|
+
if (!this.#autoRotateSpeed() && !this.#flip && !this.#velocity) return;
|
|
918
|
+
this.#last = performance.now();
|
|
919
|
+
this.#frame = requestAnimationFrame(this.#tick);
|
|
920
|
+
}
|
|
921
|
+
#tick = (now) => {
|
|
922
|
+
this.#frame = 0;
|
|
923
|
+
const stage = this.#stage;
|
|
924
|
+
if (!stage || stage.paused) return;
|
|
925
|
+
const dt = Math.min(.1, Math.max(0, (now - this.#last) / 1e3));
|
|
926
|
+
this.#last = now;
|
|
927
|
+
const speed = this.#autoRotateSpeed();
|
|
928
|
+
let active = false;
|
|
929
|
+
if (this.#flip) {
|
|
930
|
+
const k = Math.min(1, (performance.now() - this.#flip.t0) / FLIP_MS);
|
|
931
|
+
this.#yaw = this.#flip.from + (this.#flip.to - this.#flip.from) * expoOut(k);
|
|
932
|
+
if (k >= 1) this.#flip = null;
|
|
933
|
+
active = true;
|
|
934
|
+
} else if (this.#drag) active = true;
|
|
935
|
+
else if (this.#velocity) {
|
|
936
|
+
this.#yaw += this.#velocity * dt;
|
|
937
|
+
this.#velocity *= Math.exp(-4 * dt);
|
|
938
|
+
if (Math.abs(this.#velocity) < 2) this.#velocity = 0;
|
|
939
|
+
active = true;
|
|
940
|
+
} else if (speed && performance.now() >= this.#holdUntil) {
|
|
941
|
+
this.#yaw = (this.#yaw + speed * dt) % 360;
|
|
942
|
+
active = true;
|
|
943
|
+
} else if (speed) active = true;
|
|
944
|
+
stage.setRotation([
|
|
945
|
+
0,
|
|
946
|
+
this.#yaw * Math.PI / 180,
|
|
947
|
+
0
|
|
948
|
+
]);
|
|
949
|
+
if (active) this.#frame = requestAnimationFrame(this.#tick);
|
|
950
|
+
};
|
|
951
|
+
#hold() {
|
|
952
|
+
this.#holdUntil = performance.now() + RESUME_AFTER_MS;
|
|
953
|
+
}
|
|
954
|
+
#flipTurntable() {
|
|
955
|
+
if (!this.#stage) return;
|
|
956
|
+
this.#velocity = 0;
|
|
957
|
+
const from = this.#yaw;
|
|
958
|
+
const to = Math.round((from + 180) / 180) * 180;
|
|
959
|
+
this.#hold();
|
|
960
|
+
if (prefersReducedMotion()) {
|
|
961
|
+
this.#yaw = to;
|
|
962
|
+
this.#stage.setRotation([
|
|
963
|
+
0,
|
|
964
|
+
to * Math.PI / 180,
|
|
965
|
+
0
|
|
966
|
+
]);
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
this.#flip = {
|
|
970
|
+
from,
|
|
971
|
+
to,
|
|
972
|
+
t0: performance.now()
|
|
973
|
+
};
|
|
974
|
+
this.#kick();
|
|
975
|
+
}
|
|
976
|
+
#onPointerDown(e) {
|
|
977
|
+
this.#holdUntil = 1e400;
|
|
978
|
+
if (this.#interaction() !== "turntable" || e.button !== 0 || !this.#stage) return;
|
|
979
|
+
this.setPointerCapture?.(e.pointerId);
|
|
980
|
+
const t = performance.now();
|
|
981
|
+
this.#flip = null;
|
|
982
|
+
this.#velocity = 0;
|
|
983
|
+
this.#drag = {
|
|
984
|
+
id: e.pointerId,
|
|
985
|
+
x: e.clientX,
|
|
986
|
+
y: e.clientY,
|
|
987
|
+
yaw: this.#yaw,
|
|
988
|
+
t,
|
|
989
|
+
lastX: e.clientX,
|
|
990
|
+
lastT: t,
|
|
991
|
+
moved: false
|
|
992
|
+
};
|
|
993
|
+
this.#kick();
|
|
994
|
+
}
|
|
995
|
+
#onPointerMove(e) {
|
|
996
|
+
const d = this.#drag;
|
|
997
|
+
if (!d || e.pointerId !== d.id) return;
|
|
998
|
+
const dx = e.clientX - d.x;
|
|
999
|
+
if (!d.moved && Math.hypot(dx, e.clientY - d.y) < DRAG_SLOP_PX) return;
|
|
1000
|
+
d.moved = true;
|
|
1001
|
+
const t = performance.now();
|
|
1002
|
+
const w = this.clientWidth || 1;
|
|
1003
|
+
const yaw = d.yaw + dx / w * YAW_PER_WIDTH;
|
|
1004
|
+
const dtm = (t - d.lastT) / 1e3;
|
|
1005
|
+
if (dtm > 0) this.#velocity = .6 * this.#velocity + .4 * ((e.clientX - d.lastX) / w * YAW_PER_WIDTH / dtm);
|
|
1006
|
+
d.lastX = e.clientX;
|
|
1007
|
+
d.lastT = t;
|
|
1008
|
+
this.#yaw = yaw;
|
|
1009
|
+
this.#stage?.setRotation([
|
|
1010
|
+
0,
|
|
1011
|
+
yaw * Math.PI / 180,
|
|
1012
|
+
0
|
|
1013
|
+
]);
|
|
1014
|
+
}
|
|
1015
|
+
#onPointerUp(e) {
|
|
1016
|
+
const d = this.#drag;
|
|
1017
|
+
this.#hold();
|
|
1018
|
+
if (!d || e.pointerId !== d.id) return;
|
|
1019
|
+
this.#endDrag();
|
|
1020
|
+
if (!d.moved && performance.now() - d.t <= CLICK_MS) {
|
|
1021
|
+
this.#velocity = 0;
|
|
1022
|
+
this.#flipTurntable();
|
|
1023
|
+
} else if (prefersReducedMotion() || performance.now() - d.lastT > 80) this.#velocity = 0;
|
|
1024
|
+
this.#kick();
|
|
1025
|
+
}
|
|
1026
|
+
#endDrag() {
|
|
1027
|
+
const d = this.#drag;
|
|
1028
|
+
this.#drag = null;
|
|
1029
|
+
this.#hold();
|
|
1030
|
+
if (d && this.hasPointerCapture?.(d.id)) this.releasePointerCapture(d.id);
|
|
1031
|
+
}
|
|
1032
|
+
#onKey(e) {
|
|
1033
|
+
if (this.#interaction() !== "turntable" || !this.#stage) return;
|
|
1034
|
+
if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
|
|
1035
|
+
e.preventDefault();
|
|
1036
|
+
this.#flip = null;
|
|
1037
|
+
this.#velocity = 0;
|
|
1038
|
+
this.#yaw += e.key === "ArrowRight" ? 15 : -15;
|
|
1039
|
+
this.#hold();
|
|
1040
|
+
this.#stage.setRotation([
|
|
1041
|
+
0,
|
|
1042
|
+
this.#yaw * Math.PI / 180,
|
|
1043
|
+
0
|
|
1044
|
+
]);
|
|
1045
|
+
this.#kick();
|
|
1046
|
+
} else if (e.key === "Enter" || e.key === " ") {
|
|
1047
|
+
e.preventDefault();
|
|
1048
|
+
this.#flipTurntable();
|
|
1049
|
+
} else if (e.key === "Home" || e.key === "Escape") {
|
|
1050
|
+
e.preventDefault();
|
|
1051
|
+
this.reset();
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
/** Define the element (idempotent; a no-op outside the browser). */
|
|
1056
|
+
function register(tag = "metal-card") {
|
|
1057
|
+
if (typeof window === "undefined" || typeof customElements === "undefined") return;
|
|
1058
|
+
if (!customElements.get(tag)) customElements.define(tag, MetalCardElement);
|
|
1059
|
+
}
|
|
1060
|
+
// src/index.ts
|
|
1061
|
+
/**
|
|
1062
|
+
* @card3d/element: the `<metal-card>` custom element. Importing this module registers it.
|
|
1063
|
+
*/
|
|
1064
|
+
register();
|
|
1065
|
+
export { applyPatch as a, parseFit as c, parseInteraction as d, parseLighting as f, resolveDesign as h, accessibleLabel as i, parseFont as l, presetDesign as m, register as n, parseAutoRotate as o, parseQuality as p, DEFAULT_AUTO_ROTATE as r, parseBool as s, MetalCardElement as t, parseHexColor as u };
|
|
1066
|
+
|
|
1067
|
+
//# sourceMappingURL=src-K3h2YYOX.js.map
|