Skip to content

Turn one illustration into an animated 2D mesh avatar with a coding agent and a local editor

Show the code behind the live preview in a dialog

shinshin86f06564785649
diff --git a/site/index.html b/site/index.html
index 807ae8d..8de7ea6 100644
--- a/site/index.html
+++ b/site/index.html
@@ -77,7 +77,7 @@
<p id="talk-hint" class="demo-note">Try talking — this demo has no audio.</p>
</div>
</div>
- <figcaption>Running here with <a href="https://www.npmjs.com/package/mesh-avatar">mesh-avatar</a>.</figcaption>
+ <figcaption>Running here with <a href="https://www.npmjs.com/package/mesh-avatar">mesh-avatar</a>. <button type="button" class="view-code" data-open-dialog="code-dialog" aria-haspopup="dialog"><svg viewBox="0 0 16 16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M5.5 4 2 8l3.5 4M10.5 4 14 8l-3.5 4" /></svg>View the code</button></figcaption>
</figure>
</section>
<section id="features" class="content-section" aria-labelledby="features-title">
@@ -269,5 +269,54 @@ avatar.setParameters({});
<p>This is not an official AITuber OnAir product.</p>
</div>
</footer>
+ <dialog id="code-dialog" class="code-dialog" aria-labelledby="code-dialog-title">
+ <div class="code-card">
+ <div class="code-heading">
+ <h2 id="code-dialog-title">How this preview works <span>JavaScript</span></h2>
+ <div class="code-actions">
+ <button type="button" class="copy-button" data-copy="demo-code" aria-describedby="demo-feedback" disabled>Copy</button>
+ <button type="button" class="dialog-close" data-close-dialog aria-label="Close"><span aria-hidden="true">×</span></button>
+ </div>
+ </div>
+ <pre tabindex="0" aria-label="JavaScript for this preview"><code id="demo-code" data-lang="js">import { loadMeshAvatar } from 'mesh-avatar';
+
+const canvas = document.querySelector('#avatar');
+const avatar = await loadMeshAvatar(canvas, 'miko.mavatar', { padTop: -0.04 });
+
+// Expression buttons switch the face.
+for (const button of document.querySelectorAll('[data-expression]')) {
+ button.addEventListener('click', () =&gt; {
+ avatar.setExpression(button.dataset.expression);
+ });
+}
+
+// Talk toggles speaking; this demo sends a silent loudness curve instead of audio.
+let speaking = false;
+document.querySelector('#talk').addEventListener('click', () =&gt; {
+ speaking = !speaking;
+ avatar.setSpeaking(speaking);
+ if (!speaking) avatar.setVoiceLevel(0);
+});
+
+// Turn the head and eyes toward the pointer (x and y run from -1 to 1).
+const hero = document.querySelector('.hero');
+hero.addEventListener('pointermove', (event) =&gt; {
+ const rect = hero.getBoundingClientRect();
+ const x = (event.clientX - rect.left) / rect.width * 2 - 1;
+ const y = 1 - (event.clientY - rect.top) / rect.height * 2;
+ avatar.setParameters({ angleX: x * 12, angleY: y * 8, gazeX: x * 0.7, gazeY: y * 0.55 });
+});
+hero.addEventListener('pointerleave', () =&gt; avatar.setParameters({}));
+
+// While talking, send a loudness value (0–1) every frame.
+function frame(time) {
+ if (speaking) avatar.setVoiceLevel(Math.abs(Math.sin(time / 90)));
+ requestAnimationFrame(frame);
+}
+requestAnimationFrame(frame);</code></pre>
+ <p id="demo-feedback" class="copy-feedback" role="status"></p>
+ <p class="dialog-note">Simplified from this page’s script. <a href="https://github.com/shinshin86/mesh-avatar-studio/blob/main/site/src/main.ts">Full source on GitHub <span aria-hidden="true">↗</span></a></p>
+ </div>
+ </dialog>
</body>
</html>
diff --git a/site/ja/index.html b/site/ja/index.html
index ed5d63f..b0a0f0e 100644
--- a/site/ja/index.html
+++ b/site/ja/index.html
@@ -77,7 +77,7 @@
<p id="talk-hint" class="demo-note">口の動きだけを試せます。音声は流れません。</p>
</div>
</div>
- <figcaption>このプレビューも <a href="https://www.npmjs.com/package/mesh-avatar">mesh-avatar</a> で動いています。</figcaption>
+ <figcaption>このプレビューも <a href="https://www.npmjs.com/package/mesh-avatar">mesh-avatar</a> で動いています。 <button type="button" class="view-code" data-open-dialog="code-dialog" aria-haspopup="dialog"><svg viewBox="0 0 16 16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M5.5 4 2 8l3.5 4M10.5 4 14 8l-3.5 4" /></svg>コードを見る</button></figcaption>
</figure>
</section>
<section id="features" class="content-section" aria-labelledby="features-title">
@@ -269,5 +269,54 @@ avatar.setParameters({});
<p>本プロジェクトは AITuber OnAir の公式製品ではありません。</p>
</div>
</footer>
+ <dialog id="code-dialog" class="code-dialog" aria-labelledby="code-dialog-title">
+ <div class="code-card">
+ <div class="code-heading">
+ <h2 id="code-dialog-title">このプレビューのしくみ <span>JavaScript</span></h2>
+ <div class="code-actions">
+ <button type="button" class="copy-button" data-copy="demo-code" aria-describedby="demo-feedback" disabled>コピー</button>
+ <button type="button" class="dialog-close" data-close-dialog aria-label="閉じる"><span aria-hidden="true">×</span></button>
+ </div>
+ </div>
+ <pre tabindex="0" aria-label="このプレビューの JavaScript"><code id="demo-code" data-lang="js">import { loadMeshAvatar } from 'mesh-avatar';
+
+const canvas = document.querySelector('#avatar');
+const avatar = await loadMeshAvatar(canvas, 'miko.mavatar', { padTop: -0.04 });
+
+// 表情ボタンで顔を切り替えます。
+for (const button of document.querySelectorAll('[data-expression]')) {
+ button.addEventListener('click', () =&gt; {
+ avatar.setExpression(button.dataset.expression);
+ });
+}
+
+// 口パクの切り替え。このデモは音声の代わりに、無音の音量カーブを送ります。
+let speaking = false;
+document.querySelector('#talk').addEventListener('click', () =&gt; {
+ speaking = !speaking;
+ avatar.setSpeaking(speaking);
+ if (!speaking) avatar.setVoiceLevel(0);
+});
+
+// ポインターの方へ顔と視線を向けます(x と y は -1〜1)。
+const hero = document.querySelector('.hero');
+hero.addEventListener('pointermove', (event) =&gt; {
+ const rect = hero.getBoundingClientRect();
+ const x = (event.clientX - rect.left) / rect.width * 2 - 1;
+ const y = 1 - (event.clientY - rect.top) / rect.height * 2;
+ avatar.setParameters({ angleX: x * 12, angleY: y * 8, gazeX: x * 0.7, gazeY: y * 0.55 });
+});
+hero.addEventListener('pointerleave', () =&gt; avatar.setParameters({}));
+
+// 話している間は、毎フレーム音量(0〜1)を送ります。
+function frame(time) {
+ if (speaking) avatar.setVoiceLevel(Math.abs(Math.sin(time / 90)));
+ requestAnimationFrame(frame);
+}
+requestAnimationFrame(frame);</code></pre>
+ <p id="demo-feedback" class="copy-feedback" role="status"></p>
+ <p class="dialog-note">このページのスクリプトを簡略化したものです。<a href="https://github.com/shinshin86/mesh-avatar-studio/blob/main/site/src/main.ts">全文を GitHub で見る <span aria-hidden="true">↗</span></a></p>
+ </div>
+ </dialog>
</body>
</html>
diff --git a/site/src/main.ts b/site/src/main.ts
index 57c79bd..ab563a1 100644
--- a/site/src/main.ts
+++ b/site/src/main.ts
@@ -33,6 +33,14 @@ for (const image of document.querySelectorAll<HTMLImageElement>('img[data-screen
highlightCodeSamples();
+for (const opener of document.querySelectorAll<HTMLButtonElement>('[data-open-dialog]')) {
+ const dialog = document.getElementById(opener.dataset.openDialog!) as HTMLDialogElement;
+ opener.addEventListener('click', () => dialog.showModal());
+ dialog.querySelector('[data-close-dialog]')?.addEventListener('click', () => dialog.close());
+ // A click on the backdrop lands on the dialog element itself.
+ dialog.addEventListener('click', event => { if (event.target === dialog) dialog.close(); });
+}
+
for (const button of document.querySelectorAll<HTMLButtonElement>('button[data-copy]')) {
const code = document.getElementById(button.dataset.copy!)!;
const feedback = document.getElementById(button.getAttribute('aria-describedby')!)!;
diff --git a/site/src/style.css b/site/src/style.css
index 8af55d5..767529a 100644
--- a/site/src/style.css
+++ b/site/src/style.css
@@ -111,8 +111,8 @@ h3 { margin: 0; font-size: 18px; font-weight: 650; line-height: 1.4; letter-spac
.embed-code { display: grid; gap: 16px; min-width: 0; }
.code-card { --code-bg: #1b2738; --code-line: #2e3d52; --code-ink: #e3e9f1; --code-muted: #93a3b8; min-width: 0; overflow: hidden; border: 1px solid var(--code-bg); border-radius: var(--radius); background: var(--code-bg); color: var(--code-ink); box-shadow: 0 8px 24px rgb(27 39 56 / 12%); }
.code-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 56px; padding: 12px 18px; border-bottom: 1px solid var(--code-line); }
-.code-heading h3 { font-size: 13px; letter-spacing: 0; }
-.code-heading h3 span { margin-left: 8px; font-size: 11px; font-weight: 400; color: var(--code-muted); }
+.code-heading h2, .code-heading h3 { margin: 0; font-size: 13px; letter-spacing: 0; }
+.code-heading h2 span, .code-heading h3 span { margin-left: 8px; font-size: 11px; font-weight: 400; color: var(--code-muted); }
.copy-button { flex-shrink: 0; padding: 6px 10px; min-width: 64px; min-height: 32px; border: 1px solid var(--code-line); border-radius: 6px; background: transparent; color: var(--code-ink); font-size: 12px; cursor: pointer; }
.copy-button:hover:not(:disabled) { border-color: #7aa2f7; color: #fff; }
.copy-button:disabled { opacity: .5; cursor: default; }
@@ -128,6 +128,21 @@ pre:focus-visible { outline: 2px solid #7aa2f7; outline-offset: -4px; }
.tok-tag { color: #7dcfdb; }
.copy-feedback { margin: 0; padding: 0 18px; color: var(--code-muted); font-size: 11px; line-height: 1.5; }
.copy-feedback:not(:empty) { padding-bottom: 12px; }
+.view-code { display: inline-flex; align-items: center; gap: 6px; margin-left: 8px; padding: 4px 10px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; vertical-align: middle; }
+.view-code svg { width: 14px; height: 14px; color: var(--accent); }
+.view-code:hover { border-color: var(--accent); color: var(--accent); }
+.code-dialog { width: min(720px, calc(100% - 32px)); max-height: calc(100dvh - 48px); padding: 0; border: 0; border-radius: var(--radius); background: transparent; overflow: visible; }
+.code-dialog::backdrop { background: rgb(15 23 42 / 55%); backdrop-filter: blur(2px); }
+.code-dialog[open] { animation: dialog-in .18s ease-out; }
+.code-dialog .code-card { display: flex; flex-direction: column; max-height: calc(100dvh - 48px); box-shadow: 0 24px 60px rgb(15 23 42 / 35%); }
+.code-dialog pre { flex: 1; min-height: 0; overflow: auto; white-space: pre; overflow-wrap: normal; }
+.code-actions { display: flex; align-items: center; gap: 8px; }
+.dialog-close { display: grid; place-items: center; width: 32px; height: 32px; padding: 0; border: 1px solid var(--code-line); border-radius: 6px; background: transparent; color: var(--code-ink); font-size: 18px; line-height: 1; cursor: pointer; }
+.dialog-close:hover { border-color: #7aa2f7; color: #fff; }
+.dialog-note { margin: 0; padding: 12px 18px 16px; border-top: 1px solid var(--code-line); color: var(--code-muted); font-size: 12px; line-height: 1.6; }
+.dialog-note a { color: #9db8f5; }
+.dialog-note a:hover { color: #fff; }
+@keyframes dialog-in { from { opacity: 0; transform: translateY(8px); } }
.site-footer { padding-block: 40px; font-size: 12px; }
.footer-top { display: flex; justify-content: space-between; align-items: start; gap: 24px; padding-bottom: 28px; }
.footer-brand { color: var(--ink); font-size: 16px; font-weight: 650; }
@@ -175,7 +190,7 @@ pre:focus-visible { outline: 2px solid #7aa2f7; outline-offset: -4px; }
h3 { font-size: 17px; }
.screenshot figcaption { padding-inline: 16px; font-size: 11px; }
.code-heading { padding-inline: 14px; }
- .code-heading h3 span { display: block; margin-left: 0; }
+ .code-heading h2 span, .code-heading h3 span { display: block; margin-left: 0; }
pre { padding: 16px 14px; font-size: 11px; }
.copy-feedback { padding-inline: 14px; }
.footer-top { flex-direction: column; }
@@ -184,4 +199,5 @@ pre:focus-visible { outline: 2px solid #7aa2f7; outline-offset: -4px; }
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { scroll-behavior: auto; }
+ .code-dialog[open] { animation: none; }
}