Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat: implement selfie capture flow with method selection and photo upload
nirholas269f05c70f0a
diff --git a/create.html b/create.htmlindex aad4d01df..3bfe43808 100644--- a/create.html+++ b/create.html@@ -25,7 +25,8 @@}.back {display:inline-flex; align-items:center; gap:6px; color:var(--muted);- text-decoration:none; font-size:14px; padding:6px 10px; border-radius:8px;+ background:transparent; border:0; cursor:pointer;+ text-decoration:none; font:inherit; font-size:14px; padding:6px 10px; border-radius:8px;transition: background .15s, color .15s;}.back:hover { background:rgba(255,255,255,.04); color:var(--text); }@@ -33,13 +34,15 @@.brand a { color:var(--text); text-decoration:none; }.spacer { width:72px; }- main.stage {- max-width:1100px; margin:0 auto; padding:48px 24px 80px;- }- .heading { display:flex; align-items:center; justify-content:center; gap:10px; margin:8px 0 48px; }+ main.stage { max-width:1100px; margin:0 auto; padding:48px 24px 120px; }+ .step { display:none; }+ .step.active { display:block; }+ .heading { display:flex; align-items:center; justify-content:center; gap:10px; margin:8px 0 36px; }.heading h1 { font-size:22px; font-weight:500; margin:0; letter-spacing:-.01em; }.heading svg { width:22px; height:22px; color:var(--muted); }+ .subhead { text-align:center; color:var(--muted); font-size:13px; margin:-24px 0 32px; }+ /* ── Step 1: method choice ────────────────────────────────────────── */.choice-grid {display:grid; grid-template-columns:repeat(auto-fit, minmax(280px, 1fr));gap:22px; max-width:820px; margin:0 auto;@@ -53,6 +56,8 @@}.choice:hover { transform: translateY(-2px); border-color:#2c2d3d; box-shadow:0 12px 32px rgba(0,0,0,.4); }.choice:focus-visible { outline:2px solid var(--accent); outline-offset:2px; }+ .choice[disabled] { cursor:not-allowed; opacity:.45; }+ .choice[disabled]:hover { transform:none; box-shadow:none; border-color:var(--border); }.choice .illustration {position:absolute; inset:0; display:grid; place-items:center;background:linear-gradient(180deg, rgba(20,22,34,0) 0%, rgba(5,6,10,.6) 75%, rgba(5,6,10,.92) 100%);@@ -69,13 +74,11 @@display:grid; place-items:center;transition: transform .18s ease, background .18s;}- .choice:hover .arrow { transform: translateX(4px); background:var(--accent); }+ .choice:hover:not([disabled]) .arrow { transform: translateX(4px); background:var(--accent); }.choice[data-method="upload"] .illustration {background:linear-gradient(180deg, rgba(20,22,34,0) 40%, rgba(5,6,10,.6) 75%, rgba(5,6,10,.92) 100%);}- .hidden-input { display:none; }-.unsupported {margin-top:16px; padding:8px 12px; border-radius:8px;background:rgba(255,92,92,.08); border:1px solid rgba(255,92,92,.3);@@ -83,71 +86,319 @@}.unsupported.show { display:block; }- @media (max-width: 560px) {- main.stage { padding:24px 16px 64px; }+ /* ── Step 2: 3-photo capture ──────────────────────────────────────── */+ .slot-grid {+ display:grid; grid-template-columns:repeat(3, 1fr);+ gap:18px; max-width:720px; margin:0 auto 28px;+ }+ .slot {+ display:flex; flex-direction:column; align-items:center; gap:8px;+ }+ .slot-label { font-size:14px; color:var(--text); }+ .slot-frame {+ position:relative; width:100%; aspect-ratio: 3/4;+ border:1.5px dashed #3a3c4d; border-radius:14px;+ background:rgba(255,255,255,.01);+ display:grid; place-items:center; cursor:pointer; overflow:hidden;+ transition: border-color .15s, background .15s;+ }+ .slot-frame:hover, .slot-frame.drag { border-color:var(--accent); background:rgba(214,255,61,.04); }+ .slot-frame:focus-visible { outline:2px solid var(--accent); outline-offset:2px; }+ .slot-frame .plus {+ width:36px; height:36px; color:#555869;+ display:grid; place-items:center;+ }+ .slot-frame.filled { border-style:solid; border-color:#2c2d3d; cursor:default; }+ .slot-frame img.preview { width:100%; height:100%; object-fit:cover; display:block; }+ .slot-retake {+ position:absolute; top:6px; right:6px; width:28px; height:28px; border-radius:50%;+ background:rgba(10,10,12,.7); color:#fff; border:0; cursor:pointer;+ display:grid; place-items:center; backdrop-filter: blur(4px);+ }+ .slot-retake:hover { background:rgba(10,10,12,.9); }+ .slot-example {+ position:absolute; left:8px; bottom:8px; width:48px; height:60px;+ border-radius:8px; overflow:hidden; border:1.5px dashed #3a3c4d;+ background:#1a1b27; display:grid; place-items:center; font-size:10px; color:#555869;+ pointer-events:none;+ }+ .slot-frame.filled .slot-example { display:none; }+ .slot-example::after {+ content:''; position:absolute; top:-3px; right:-3px; width:8px; height:8px;+ border-radius:50%; background:var(--accent);+ }++ .tips {+ max-width:720px; margin:0 auto 32px;+ padding:16px 20px; border:1px solid var(--border); border-radius:14px;+ background:rgba(15,16,24,.6);+ }+ .tips .tips-head { display:flex; align-items:center; gap:8px; font-size:13px; margin:0 0 12px; }+ .tips .tips-head strong { color:var(--accent); letter-spacing:.02em; }+ .tips .tips-head .check {+ width:16px; height:16px; border-radius:50%; background:var(--accent); color:var(--accent-ink);+ display:grid; place-items:center; flex:0 0 auto;+ }+ .tips ul {+ list-style:none; padding:0; margin:0;+ display:grid; grid-template-columns:repeat(2, 1fr); gap:10px 22px;+ font-size:12.5px; color:var(--muted);+ }+ .tips li { display:flex; gap:8px; align-items:flex-start; }+ .tips li svg { flex:0 0 auto; width:14px; height:14px; color:var(--accent); margin-top:2px; }+ .tips li strong { color:var(--text); font-weight:500; }++ .selectors {+ max-width:720px; margin:0 auto 40px;+ display:flex; justify-content:center; gap:32px; flex-wrap:wrap;+ font-size:13px; color:var(--muted);+ }+ .selector-group { display:flex; align-items:center; gap:10px; }+ .seg {+ display:inline-flex; border:1px solid var(--border); border-radius:10px; overflow:hidden;+ background:rgba(15,16,24,.5);+ }+ .seg button {+ padding:6px 12px; background:transparent; color:var(--muted); border:0; cursor:pointer;+ font:inherit; font-size:13px; min-width:36px;+ display:inline-flex; align-items:center; justify-content:center; gap:4px;+ }+ .seg button[aria-pressed="true"] { background:var(--accent); color:var(--accent-ink); font-weight:600; }+ .seg button:focus-visible { outline:2px solid var(--accent); outline-offset:2px; }++ .submit-bar {+ position:fixed; left:0; right:0; bottom:0;+ padding:18px 24px 22px; display:flex; justify-content:center;+ background:linear-gradient(180deg, transparent, rgba(5,6,10,.95) 40%);+ pointer-events:none;+ }+ .submit-btn {+ pointer-events:auto; min-width:260px; padding:13px 24px; border:0; border-radius:12px;+ background:#2a2b3a; color:#777; font:inherit; font-size:15px; font-weight:600;+ cursor:not-allowed; transition: background .18s, color .18s, transform .12s;+ }+ .submit-btn.ready { background:var(--accent); color:var(--accent-ink); cursor:pointer; }+ .submit-btn.ready:hover { transform: translateY(-1px); filter:brightness(1.05); }+ .submit-btn.ready:active { transform: translateY(0); }++ /* ── Camera overlay ───────────────────────────────────────────────── */+ .cam-overlay {+ position:fixed; inset:0; z-index:40; background:rgba(5,6,10,.92);+ display:none; place-items:center; padding:24px;+ }+ .cam-overlay.open { display:grid; }+ .cam-modal {+ width:min(560px, 100%); background:var(--panel); border:1px solid var(--border);+ border-radius:18px; padding:20px; display:flex; flex-direction:column; gap:14px;+ }+ .cam-modal h3 { margin:0; font-size:16px; font-weight:500; text-align:center; }+ .cam-modal h3 span { color:var(--muted); margin-left:6px; font-size:13px; }+ .cam-stage {+ position:relative; width:100%; aspect-ratio: 3/4; border-radius:12px; overflow:hidden;+ background:#000;+ }+ .cam-stage video, .cam-stage canvas, .cam-stage img {+ position:absolute; inset:0; width:100%; height:100%; object-fit:cover;+ }+ .cam-stage video { transform: scaleX(-1); }+ .cam-frame-overlay {+ position:absolute; inset:0; pointer-events:none;+ background:+ radial-gradient(ellipse 60% 72% at 50% 46%, transparent 60%, rgba(5,6,10,.55) 100%);+ }+ .cam-frame-overlay::before {+ content:''; position:absolute; left:18%; right:18%; top:12%; bottom:18%;+ border:1.5px dashed rgba(255,255,255,.4); border-radius:50%;+ }+ .cam-actions { display:flex; gap:10px; justify-content:center; align-items:center; }+ .cam-btn {+ padding:10px 16px; border-radius:10px; border:1px solid var(--border);+ background:#1a1b27; color:var(--text); font:inherit; font-size:13px; cursor:pointer;+ }+ .cam-btn:hover { background:#22222e; }+ .cam-btn.primary { background:var(--accent); color:var(--accent-ink); border-color:transparent; font-weight:600; }+ .cam-btn.primary:hover { filter:brightness(1.05); }+ .cam-shutter {+ width:64px; height:64px; border-radius:50%; background:#fff; border:4px solid rgba(255,255,255,.25);+ cursor:pointer; padding:0;+ }+ .cam-shutter:hover { filter:brightness(.95); }+ .cam-error { color:#ffb3b3; font-size:12px; text-align:center; margin:0; }++ .hidden-input { display:none; }++ @media (max-width: 640px) {+ main.stage { padding:24px 16px 120px; }.heading h1 { font-size:18px; }.choice { aspect-ratio: 4/5; padding:20px; border-radius:18px; }.choice .label h2 { font-size:18px; }+ .slot-grid { grid-template-columns:repeat(3, 1fr); gap:10px; }+ .slot-label { font-size:12px; }+ .tips ul { grid-template-columns:1fr; }+ .submit-btn { min-width:0; width:100%; }}</style></head><body><header class="topbar">- <a class="back" href="/" aria-label="Back">+ <button class="back" id="back-btn" type="button" aria-label="Back"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="15 18 9 12 15 6"/></svg><span>Back</span>- </a>+ </button><div class="brand"><a href="/">3D Agent</a></div><div class="spacer" aria-hidden="true"></div></header><main class="stage">- <div class="heading">- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 7V5a1 1 0 0 1 1-1h2"/><path d="M4 17v2a1 1 0 0 0 1 1h2"/><path d="M20 7V5a1 1 0 0 0-1-1h-2"/><path d="M20 17v2a1 1 0 0 1-1 1h-2"/><circle cx="12" cy="12" r="3"/></svg>- <h1>Create new avatar</h1>- </div>+ <!-- Step 1: method choice -->+ <section class="step active" data-step="method">+ <div class="heading">+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 7V5a1 1 0 0 1 1-1h2"/><path d="M4 17v2a1 1 0 0 0 1 1h2"/><path d="M20 7V5a1 1 0 0 0-1-1h-2"/><path d="M20 17v2a1 1 0 0 1-1 1h-2"/><circle cx="12" cy="12" r="3"/></svg>+ <h1>Create new avatar</h1>+ </div>++ <div class="choice-grid" role="group" aria-label="Choose avatar input method">+ <button type="button" class="choice" data-method="camera" aria-label="Use camera to capture selfies">+ <div class="illustration" aria-hidden="true">+ <svg viewBox="0 0 160 200" fill="none" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round">+ <circle cx="80" cy="72" r="34" fill="rgba(255,255,255,.04)" />+ <path d="M28 180c0-28 23-46 52-46s52 18 52 46" fill="rgba(255,255,255,.04)" />+ <path d="M24 40 L24 52 M32 40 L40 40" />+ <path d="M136 40 L136 52 M128 40 L120 40" />+ <path d="M24 160 L24 148 M32 160 L40 160" />+ <path d="M136 160 L136 148 M128 160 L120 160" />+ </svg>+ </div>+ <div class="label">+ <h2>Use<br/>camera</h2>+ <span class="arrow" aria-hidden="true">+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>+ </span>+ </div>+ </button>- <div class="choice-grid" role="group" aria-label="Choose avatar input method">- <button type="button" class="choice" data-method="camera" aria-label="Use camera to capture selfies">- <div class="illustration" aria-hidden="true">- <svg viewBox="0 0 160 200" fill="none" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round">- <circle cx="80" cy="72" r="34" fill="rgba(255,255,255,.04)" />- <path d="M28 180c0-28 23-46 52-46s52 18 52 46" fill="rgba(255,255,255,.04)" />- <path d="M24 40 L24 52 M32 40 L40 40" />- <path d="M136 40 L136 52 M128 40 L120 40" />- <path d="M24 160 L24 148 M32 160 L40 160" />- <path d="M136 160 L136 148 M128 160 L120 160" />- </svg>+ <button type="button" class="choice" data-method="upload" aria-label="Upload photos from device">+ <div class="illustration" aria-hidden="true">+ <svg viewBox="0 0 160 200" fill="none" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round">+ <rect x="28" y="52" width="60" height="88" rx="6" fill="rgba(255,255,255,.04)" transform="rotate(-8 58 96)" />+ <rect x="50" y="34" width="60" height="88" rx="6" fill="rgba(255,255,255,.06)" stroke-dasharray="4 4" />+ <rect x="72" y="52" width="60" height="88" rx="6" fill="rgba(255,255,255,.04)" transform="rotate(8 102 96)" />+ </svg>+ </div>+ <div class="label">+ <h2>Upload<br/>from device</h2>+ <span class="arrow" aria-hidden="true">+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>+ </span>+ </div>+ </button>+ </div>++ <p class="unsupported" id="unsupported-msg">Camera not available on this device — use the upload option instead.</p>+ </section>++ <!-- Step 2: 3-photo capture -->+ <section class="step" data-step="capture">+ <div class="heading">+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="6" width="18" height="14" rx="2"/><path d="M8 6l2-3h4l2 3"/><path d="M12 10v6M9 13h6"/></svg>+ <h1>Upload 3 photos to create an avatar</h1>+ </div>+ <p class="subhead">Drag & drop or click to choose a file (jpeg or png)</p>++ <div class="slot-grid" id="slot-grid">+ <div class="slot">+ <span class="slot-label">1. Frontal</span>+ <div class="slot-frame" data-slot="frontal" role="button" tabindex="0" aria-label="Add frontal photo">+ <svg class="plus" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>+ <div class="slot-example" aria-hidden="true">ex</div>+ </div></div>- <div class="label">- <h2>Use<br/>camera</h2>- <span class="arrow" aria-hidden="true">- <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>- </span>+ <div class="slot">+ <span class="slot-label">2. Left</span>+ <div class="slot-frame" data-slot="left" role="button" tabindex="0" aria-label="Add left-profile photo">+ <svg class="plus" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>+ <div class="slot-example" aria-hidden="true">ex</div>+ </div></div>- </button>-- <button type="button" class="choice" data-method="upload" aria-label="Upload photos from device">- <div class="illustration" aria-hidden="true">- <svg viewBox="0 0 160 200" fill="none" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round">- <rect x="28" y="52" width="60" height="88" rx="6" fill="rgba(255,255,255,.04)" transform="rotate(-8 58 96)" />- <rect x="50" y="34" width="60" height="88" rx="6" fill="rgba(255,255,255,.06)" stroke-dasharray="4 4" />- <rect x="72" y="52" width="60" height="88" rx="6" fill="rgba(255,255,255,.04)" transform="rotate(8 102 96)" />- </svg>+ <div class="slot">+ <span class="slot-label">3. Right</span>+ <div class="slot-frame" data-slot="right" role="button" tabindex="0" aria-label="Add right-profile photo">+ <svg class="plus" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>+ <div class="slot-example" aria-hidden="true">ex</div>+ </div></div>- <div class="label">- <h2>Upload<br/>from device</h2>- <span class="arrow" aria-hidden="true">- <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>+ </div>++ <div class="tips">+ <p class="tips-head">+ <span class="check" aria-hidden="true">+ <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg></span>- </div>- </button>- </div>+ <strong>IMPORTANT</strong>+ <span>— Best results require selfie-distance shots in good lighting.</span>+ </p>+ <ul>+ <li><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="12" x2="21" y2="12"/><polyline points="8 17 3 12 8 7"/><polyline points="16 7 21 12 16 17"/></svg><span><strong>Selfie-distance</strong> — max ~0.7m from the face.</span></li>+ <li><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="3" y="6" width="18" height="14" rx="2"/><circle cx="12" cy="13" r="3.5"/></svg><span><strong>Same camera</strong> for all 3 photos.</span></li>+ <li><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="12" cy="12" r="9"/><path d="M9 10s1-2 3-2 3 2 3 2"/><line x1="9" y1="15" x2="15" y2="15"/></svg><span><strong>No distortion</strong> — long-distance shots won’t work.</span></li>+ <li><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M3 7l9-4 9 4"/><path d="M5 10v7"/><path d="M19 10v7"/></svg><span><strong>Glasses off</strong>, hair tied back.</span></li>+ <li><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="4" y="3" width="12" height="18" rx="1"/><line x1="8" y1="3" x2="8" y2="21"/></svg><span>Vertical images, <strong>3:4 or 9:16</strong>.</span></li>+ <li><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="12" cy="12" r="4"/><line x1="12" y1="2" x2="12" y2="4"/><line x1="12" y1="20" x2="12" y2="22"/><line x1="2" y1="12" x2="4" y2="12"/><line x1="20" y1="12" x2="22" y2="12"/></svg><span><strong>Good lighting</strong> on the face.</span></li>+ </ul>+ </div>- <p class="unsupported" id="unsupported-msg">Camera not available on this device — use the upload option instead.</p>+ <div class="selectors">+ <div class="selector-group">+ <span>Choose body type</span>+ <div class="seg" role="group" aria-label="Body type">+ <button type="button" data-body="male" aria-pressed="true" aria-label="Male body type">+ <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><circle cx="12" cy="5" r="3"/><path d="M8 22v-7H6l2.5-6.5h7L18 15h-2v7z"/></svg>+ </button>+ <button type="button" data-body="female" aria-pressed="false" aria-label="Female body type">+ <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><circle cx="12" cy="5" r="3"/><path d="M9 22l-2-7 2-6h6l2 6-2 7z"/></svg>+ </button>+ </div>+ </div>+ <div class="selector-group">+ <span>Choose avatar type</span>+ <div class="seg" role="group" aria-label="Avatar type">+ <button type="button" data-type="v1" aria-pressed="true">V1</button>+ <button type="button" data-type="v2" aria-pressed="false">V2</button>+ </div>+ </div>+ </div>+ </section></main>- <input type="file" id="upload-input" class="hidden-input" accept="image/jpeg,image/png" multiple />+ <div class="submit-bar" id="submit-bar" hidden>+ <button type="button" class="submit-btn" id="submit-btn" disabled>Upload 0/3</button>+ </div>++ <!-- Camera overlay -->+ <div class="cam-overlay" id="cam-overlay" role="dialog" aria-modal="true" aria-labelledby="cam-title">+ <div class="cam-modal">+ <h3 id="cam-title">Capture photo <span id="cam-slot-name">Frontal</span></h3>+ <div class="cam-stage" id="cam-stage">+ <video id="cam-video" autoplay playsinline muted></video>+ <div class="cam-frame-overlay" aria-hidden="true"></div>+ </div>+ <p class="cam-error" id="cam-error" hidden></p>+ <div class="cam-actions" id="cam-actions-live">+ <button type="button" class="cam-btn" data-cam-action="cancel">Cancel</button>+ <button type="button" class="cam-shutter" data-cam-action="shoot" aria-label="Take photo"></button>+ <button type="button" class="cam-btn" data-cam-action="switch" hidden>Switch</button>+ </div>+ <div class="cam-actions" id="cam-actions-review" hidden>+ <button type="button" class="cam-btn" data-cam-action="retake">Retake</button>+ <button type="button" class="cam-btn primary" data-cam-action="use">Use photo</button>+ </div>+ </div>+ </div>++ <!-- One hidden file input per slot — keeps click → picker reliable across browsers. -->+ <input type="file" class="hidden-input" data-slot-input="frontal" accept="image/jpeg,image/png" />+ <input type="file" class="hidden-input" data-slot-input="left" accept="image/jpeg,image/png" />+ <input type="file" class="hidden-input" data-slot-input="right" accept="image/jpeg,image/png" /><script type="module" src="/src/selfie-capture.js"></script></body>diff --git a/src/selfie-capture.js b/src/selfie-capture.jsindex 21946e6a3..eb93832ef 100644--- a/src/selfie-capture.js+++ b/src/selfie-capture.js@@ -1,59 +1,371 @@/*** Selfie capture flow — entry for /create.*- * Task 1 (this file): render the input-method choice step. When the user picks- * a method, stash it on sessionStorage + advance the UI placeholder. Later- * tasks wire up the 3-photo camera / upload step and the Avaturn pipeline.+ * Manages two steps:+ * 1. Method choice (camera vs upload)+ * 2. 3-photo capture (Frontal / Left / Right) + body/avatar type selectors+ *+ * On submit, dispatches a `selfie:submit` CustomEvent on document with+ * detail = { files: { frontal, left, right }, bodyType, avatarType, method }+ * which the Avaturn pipeline (task 3) will pick up.*/-const SESSION_KEY = 'selfie.method';+const SLOT_KEYS = /** @type {const} */ (['frontal', 'left', 'right']);-const choices = document.querySelectorAll('.choice[data-method]');-const uploadInput = /** @type {HTMLInputElement | null} */ (document.getElementById('upload-input'));-const unsupportedMsg = document.getElementById('unsupported-msg');+const state = {+ method: /** @type {'camera' | 'upload' | null} */ (null),+ bodyType: /** @type {'male' | 'female'} */ ('male'),+ avatarType: /** @type {'v1' | 'v2'} */ ('v1'),+ files: /** @type {Record<string, File | null>} */ ({ frontal: null, left: null, right: null }),+};const cameraSupported =typeof navigator !== 'undefined' &&- navigator.mediaDevices &&+ !!navigator.mediaDevices &&typeof navigator.mediaDevices.getUserMedia === 'function';+// ── DOM refs ───────────────────────────────────────────────────────────────+const steps = /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('.step[data-step]'));+const backBtn = /** @type {HTMLButtonElement} */ (document.getElementById('back-btn'));+const choiceButtons = /** @type {NodeListOf<HTMLButtonElement>} */ (document.querySelectorAll('.choice[data-method]'));+const unsupportedMsg = document.getElementById('unsupported-msg');+const slotFrames = /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('.slot-frame[data-slot]'));+const slotInputs = /** @type {NodeListOf<HTMLInputElement>} */ (document.querySelectorAll('input[data-slot-input]'));+const submitBar = document.getElementById('submit-bar');+const submitBtn = /** @type {HTMLButtonElement} */ (document.getElementById('submit-btn'));+const bodyBtns = /** @type {NodeListOf<HTMLButtonElement>} */ (document.querySelectorAll('[data-body]'));+const typeBtns = /** @type {NodeListOf<HTMLButtonElement>} */ (document.querySelectorAll('[data-type]'));++// ── Step 1: method picker ──────────────────────────────────────────────────if (!cameraSupported) {- const cameraChoice = document.querySelector('.choice[data-method="camera"]');- if (cameraChoice) {- cameraChoice.setAttribute('aria-disabled', 'true');- /** @type {HTMLButtonElement} */ (cameraChoice).disabled = true;- cameraChoice.style.opacity = '0.45';- cameraChoice.style.cursor = 'not-allowed';+ const camChoice = /** @type {HTMLButtonElement | null} */ (document.querySelector('.choice[data-method="camera"]'));+ if (camChoice) {+ camChoice.disabled = true;+ camChoice.setAttribute('aria-disabled', 'true');}unsupportedMsg?.classList.add('show');}-choices.forEach((btn) => {+choiceButtons.forEach((btn) => {btn.addEventListener('click', () => {- const method = btn.getAttribute('data-method');- if (!method) return;+ const method = /** @type {'camera'|'upload'|null} */ (btn.getAttribute('data-method'));+ if (!method || btn.disabled) return;if (method === 'camera' && !cameraSupported) return;+ state.method = method;+ goToStep('capture');+ });+});++backBtn.addEventListener('click', () => {+ const current = currentStep();+ if (current === 'capture') {+ goToStep('method');+ } else {+ window.location.assign('/');+ }+});++// ── Step 2: slot fill ──────────────────────────────────────────────────────+slotFrames.forEach((frame) => {+ const slot = frame.getAttribute('data-slot');+ if (!slot) return;- try {- sessionStorage.setItem(SESSION_KEY, method);- } catch (_) {- // sessionStorage may be blocked (private mode, etc.) — ignore, next step re-asks.+ const open = () => {+ if (state.files[slot]) return; // already filled — use retake button to replace+ if (state.method === 'camera') {+ openCamera(slot);+ } else {+ /** @type {HTMLInputElement | null} */ (+ document.querySelector(`input[data-slot-input="${slot}"]`)+ )?.click();}+ };- if (method === 'upload' && uploadInput) {- uploadInput.click();- return;+ frame.addEventListener('click', open);+ frame.addEventListener('keydown', (e) => {+ if (e.key === 'Enter' || e.key === ' ') {+ e.preventDefault();+ open();}+ });++ // Drag + drop+ frame.addEventListener('dragover', (e) => {+ e.preventDefault();+ frame.classList.add('drag');+ });+ frame.addEventListener('dragleave', () => frame.classList.remove('drag'));+ frame.addEventListener('drop', (e) => {+ e.preventDefault();+ frame.classList.remove('drag');+ const file = e.dataTransfer?.files?.[0];+ if (file && isImage(file)) setSlot(slot, file);+ });+});- // Camera branch: step 2 (3-photo capture UI) lands in the next task.- // For now signal intent so the placeholder is visible during dev.- console.info('[selfie] camera flow — capture UI pending (task 2)');+slotInputs.forEach((input) => {+ input.addEventListener('change', () => {+ const slot = input.getAttribute('data-slot-input');+ const file = input.files?.[0];+ if (slot && file && isImage(file)) setSlot(slot, file);+ input.value = '';+ });+});++// ── Selectors ──────────────────────────────────────────────────────────────+bodyBtns.forEach((btn) => {+ btn.addEventListener('click', () => {+ const val = /** @type {'male'|'female'} */ (btn.getAttribute('data-body'));+ state.bodyType = val;+ bodyBtns.forEach((b) => b.setAttribute('aria-pressed', String(b === btn)));+ });+});+typeBtns.forEach((btn) => {+ btn.addEventListener('click', () => {+ const val = /** @type {'v1'|'v2'} */ (btn.getAttribute('data-type'));+ state.avatarType = val;+ typeBtns.forEach((b) => b.setAttribute('aria-pressed', String(b === btn)));});});-uploadInput?.addEventListener('change', () => {- const files = uploadInput.files;- if (!files || files.length === 0) return;- // Step 2 picks up these files from the <input> reference in the next task.- console.info('[selfie] upload flow — %d file(s) selected; capture UI pending (task 2)', files.length);+// ── Submit ─────────────────────────────────────────────────────────────────+submitBtn.addEventListener('click', () => {+ if (!allSlotsFilled()) return;+ document.dispatchEvent(+ new CustomEvent('selfie:submit', {+ detail: {+ files: { ...state.files },+ bodyType: state.bodyType,+ avatarType: state.avatarType,+ method: state.method,+ },+ })+ );+ // Temporary visual ack until task 3 wires the pipeline.+ submitBtn.textContent = 'Sending…';+ submitBtn.disabled = true;+ submitBtn.classList.remove('ready');});++// ── Step machine ───────────────────────────────────────────────────────────+/** @returns {'method' | 'capture'} */+function currentStep() {+ const active = document.querySelector('.step.active');+ return /** @type {'method'|'capture'} */ (active?.getAttribute('data-step') || 'method');+}++/** @param {'method' | 'capture'} step */+function goToStep(step) {+ steps.forEach((el) => el.classList.toggle('active', el.getAttribute('data-step') === step));+ if (submitBar) submitBar.hidden = step !== 'capture';+ window.scrollTo({ top: 0, behavior: 'instant' in window ? 'instant' : 'auto' });+}++// ── Slot helpers ───────────────────────────────────────────────────────────+/** @param {File} file */+function isImage(file) {+ return /^image\/(jpeg|png)$/i.test(file.type || '');+}++/**+ * @param {string} slot+ * @param {File} file+ */+function setSlot(slot, file) {+ state.files[slot] = file;+ renderSlot(slot);+ updateSubmit();+}++/** @param {string} slot */+function clearSlot(slot) {+ state.files[slot] = null;+ renderSlot(slot);+ updateSubmit();+}++/** @param {string} slot */+function renderSlot(slot) {+ const frame = /** @type {HTMLElement | null} */ (document.querySelector(`.slot-frame[data-slot="${slot}"]`));+ if (!frame) return;+ const file = state.files[slot];++ // Clean up any existing preview / retake artefacts.+ frame.querySelector('img.preview')?.remove();+ frame.querySelector('.slot-retake')?.remove();++ if (!file) {+ frame.classList.remove('filled');+ return;+ }++ frame.classList.add('filled');++ const img = document.createElement('img');+ img.className = 'preview';+ img.alt = `${slot} photo preview`;+ img.src = URL.createObjectURL(file);+ img.onload = () => URL.revokeObjectURL(img.src);+ frame.appendChild(img);++ const retake = document.createElement('button');+ retake.type = 'button';+ retake.className = 'slot-retake';+ retake.setAttribute('aria-label', `Remove ${slot} photo`);+ retake.innerHTML =+ '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15A9 9 0 1 1 18.36 5.64L23 10"/></svg>';+ retake.addEventListener('click', (e) => {+ e.stopPropagation();+ clearSlot(slot);+ });+ frame.appendChild(retake);+}++function allSlotsFilled() {+ return SLOT_KEYS.every((k) => !!state.files[k]);+}++function updateSubmit() {+ const n = SLOT_KEYS.filter((k) => !!state.files[k]).length;+ submitBtn.textContent = n === 3 ? 'Submit' : `Upload ${n}/3`;+ const ready = n === 3;+ submitBtn.disabled = !ready;+ submitBtn.classList.toggle('ready', ready);+}++// ── Camera overlay ─────────────────────────────────────────────────────────+const camOverlay = document.getElementById('cam-overlay');+const camVideo = /** @type {HTMLVideoElement | null} */ (document.getElementById('cam-video'));+const camSlotName = document.getElementById('cam-slot-name');+const camError = document.getElementById('cam-error');+const camActionsLive = document.getElementById('cam-actions-live');+const camActionsReview = document.getElementById('cam-actions-review');++const cam = {+ stream: /** @type {MediaStream | null} */ (null),+ slot: /** @type {string | null} */ (null),+ pendingFile: /** @type {File | null} */ (null),+ pendingUrl: /** @type {string | null} */ (null),+};++/** @param {string} slot */+async function openCamera(slot) {+ if (!camOverlay || !camVideo) return;+ cam.slot = slot;+ if (camSlotName) camSlotName.textContent = slotPretty(slot);+ setCamMode('live');+ camError?.setAttribute('hidden', '');+ camOverlay.classList.add('open');++ try {+ cam.stream = await navigator.mediaDevices.getUserMedia({+ video: { facingMode: 'user', width: { ideal: 1280 }, height: { ideal: 1706 } },+ audio: false,+ });+ camVideo.srcObject = cam.stream;+ } catch (err) {+ console.warn('[selfie] camera error:', err);+ showCamError('Could not access camera. Check permissions and try again.');+ }+}++function closeCamera() {+ if (cam.stream) {+ cam.stream.getTracks().forEach((t) => t.stop());+ cam.stream = null;+ }+ if (camVideo) camVideo.srcObject = null;+ camOverlay?.classList.remove('open');+ clearPending();+ cam.slot = null;+}++function clearPending() {+ if (cam.pendingUrl) URL.revokeObjectURL(cam.pendingUrl);+ cam.pendingUrl = null;+ cam.pendingFile = null;+ // Remove any transient preview img in the stage.+ document.querySelector('#cam-stage img.preview')?.remove();+}++/** @param {'live' | 'review'} mode */+function setCamMode(mode) {+ if (mode === 'live') {+ camActionsLive?.removeAttribute('hidden');+ camActionsReview?.setAttribute('hidden', '');+ } else {+ camActionsLive?.setAttribute('hidden', '');+ camActionsReview?.removeAttribute('hidden');+ }+}++/** @param {string} msg */+function showCamError(msg) {+ if (!camError) return;+ camError.textContent = msg;+ camError.removeAttribute('hidden');+}++camOverlay?.addEventListener('click', (e) => {+ const target = /** @type {HTMLElement} */ (e.target);+ const action = target.closest('[data-cam-action]')?.getAttribute('data-cam-action');+ if (!action) return;+ if (action === 'cancel') closeCamera();+ else if (action === 'shoot') shoot();+ else if (action === 'retake') {+ clearPending();+ setCamMode('live');+ } else if (action === 'use') confirmCamShot();+});++async function shoot() {+ if (!camVideo || !cam.stream) return;+ const w = camVideo.videoWidth;+ const h = camVideo.videoHeight;+ if (!w || !h) return;+ const canvas = document.createElement('canvas');+ canvas.width = w;+ canvas.height = h;+ const ctx = canvas.getContext('2d');+ if (!ctx) return;+ // Un-mirror: the <video> is CSS-flipped for selfie feel, but the saved file+ // should match what the camera actually sees.+ ctx.drawImage(camVideo, 0, 0, w, h);++ const blob = await new Promise((res) => canvas.toBlob(res, 'image/jpeg', 0.92));+ if (!blob) {+ showCamError('Snapshot failed. Try again.');+ return;+ }+ const file = new File([blob], `${cam.slot || 'photo'}.jpg`, { type: 'image/jpeg' });+ cam.pendingFile = file;+ cam.pendingUrl = URL.createObjectURL(file);++ // Show preview over the video feed.+ const stage = document.getElementById('cam-stage');+ if (stage) {+ const img = document.createElement('img');+ img.className = 'preview';+ img.alt = 'captured preview';+ img.src = cam.pendingUrl;+ stage.appendChild(img);+ }+ setCamMode('review');+}++function confirmCamShot() {+ if (!cam.pendingFile || !cam.slot) return;+ const slot = cam.slot;+ const file = cam.pendingFile;+ closeCamera();+ setSlot(slot, file);+}++// ── Utilities ──────────────────────────────────────────────────────────────+/** @param {string} slot */+function slotPretty(slot) {+ if (slot === 'frontal') return 'Frontal';+ if (slot === 'left') return 'Left';+ if (slot === 'right') return 'Right';+ return slot;+}