Skip to content

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

Colour the code samples on the landing page

shinshin86594395abf1b0
diff --git a/site/index.html b/site/index.html
index b9c04a6..0091a31 100644
--- a/site/index.html
+++ b/site/index.html
@@ -209,7 +209,7 @@
</ul>
<div class="code-card">
<div class="code-heading"><h3>Install the runtime</h3><button type="button" class="copy-button" data-copy="install-code" aria-describedby="install-feedback" disabled>Copy</button></div>
- <pre tabindex="0" aria-label="Install command"><code id="install-code">npm install mesh-avatar</code></pre>
+ <pre tabindex="0" aria-label="Install command"><code id="install-code" data-lang="shell">npm install mesh-avatar</code></pre>
<p id="install-feedback" class="copy-feedback" role="status"></p>
</div>
<p>Place your <code>.mavatar</code> file in your app’s public assets. Add the canvas to your HTML and run the JavaScript from your app’s module entry.</p>
@@ -219,12 +219,12 @@
<div class="embed-code">
<div class="code-card">
<div class="code-heading"><h3>Add a canvas <span>HTML</span></h3><button type="button" class="copy-button" data-copy="canvas-code" aria-describedby="canvas-feedback" disabled>Copy</button></div>
- <pre tabindex="0" aria-label="Canvas HTML"><code id="canvas-code">&lt;canvas id="avatar" width="720" height="960"&gt;&lt;/canvas&gt;</code></pre>
+ <pre tabindex="0" aria-label="Canvas HTML"><code id="canvas-code" data-lang="html">&lt;canvas id="avatar" width="720" height="960"&gt;&lt;/canvas&gt;</code></pre>
<p id="canvas-feedback" class="copy-feedback" role="status"></p>
</div>
<div class="code-card">
<div class="code-heading"><h3>Load and control it <span>JavaScript</span></h3><button type="button" class="copy-button" data-copy="avatar-code" aria-describedby="avatar-feedback" disabled>Copy</button></div>
- <pre tabindex="0" aria-label="JavaScript runtime example"><code id="avatar-code">import { loadMeshAvatar } from 'mesh-avatar';
+ <pre tabindex="0" aria-label="JavaScript runtime example"><code id="avatar-code" data-lang="js">import { loadMeshAvatar } from 'mesh-avatar';
const canvas = document.querySelector('#avatar');
const avatar = await loadMeshAvatar(
diff --git a/site/ja/index.html b/site/ja/index.html
index 3987418..5eb6c3d 100644
--- a/site/ja/index.html
+++ b/site/ja/index.html
@@ -209,7 +209,7 @@
</ul>
<div class="code-card">
<div class="code-heading"><h3>ランタイムをインストール</h3><button type="button" class="copy-button" data-copy="install-code" aria-describedby="install-feedback" disabled>コピー</button></div>
- <pre tabindex="0" aria-label="インストールコマンド"><code id="install-code">npm install mesh-avatar</code></pre>
+ <pre tabindex="0" aria-label="インストールコマンド"><code id="install-code" data-lang="shell">npm install mesh-avatar</code></pre>
<p id="install-feedback" class="copy-feedback" role="status"></p>
</div>
<p><code>.mavatar</code> ファイルをアプリの公開アセットに置きます。HTML に canvas を追加し、アプリのモジュールから JavaScript を実行します。</p>
@@ -219,12 +219,12 @@
<div class="embed-code">
<div class="code-card">
<div class="code-heading"><h3>canvas を追加 <span>HTML</span></h3><button type="button" class="copy-button" data-copy="canvas-code" aria-describedby="canvas-feedback" disabled>コピー</button></div>
- <pre tabindex="0" aria-label="canvas の HTML"><code id="canvas-code">&lt;canvas id="avatar" width="720" height="960"&gt;&lt;/canvas&gt;</code></pre>
+ <pre tabindex="0" aria-label="canvas の HTML"><code id="canvas-code" data-lang="html">&lt;canvas id="avatar" width="720" height="960"&gt;&lt;/canvas&gt;</code></pre>
<p id="canvas-feedback" class="copy-feedback" role="status"></p>
</div>
<div class="code-card">
<div class="code-heading"><h3>読み込んで動かす <span>JavaScript</span></h3><button type="button" class="copy-button" data-copy="avatar-code" aria-describedby="avatar-feedback" disabled>コピー</button></div>
- <pre tabindex="0" aria-label="JavaScript の使用例"><code id="avatar-code">import { loadMeshAvatar } from 'mesh-avatar';
+ <pre tabindex="0" aria-label="JavaScript の使用例"><code id="avatar-code" data-lang="js">import { loadMeshAvatar } from 'mesh-avatar';
const canvas = document.querySelector('#avatar');
const avatar = await loadMeshAvatar(
diff --git a/site/src/highlight.ts b/site/src/highlight.ts
new file mode 100644
index 0000000..9a01efa
--- /dev/null
+++ b/site/src/highlight.ts
@@ -0,0 +1,58 @@
+// Minimal syntax colouring for the fixed code samples on the page.
+// Tokens are wrapped in spans, so copying the element's textContent is unchanged.
+
+type Rule = [kind: string, pattern: RegExp];
+
+const rules: Record<string, Rule[]> = {
+ js: [
+ ['comment', /\/\/[^\n]*/y],
+ ['string', /'[^'\n]*'|"[^"\n]*"/y],
+ ['keyword', /\b(?:import|from|const|let|await|async|return|new)\b/y],
+ ['literal', /\b(?:true|false|null|undefined)\b/y],
+ ['number', /\b\d+(?:\.\d+)?\b/y],
+ ['function', /\b[A-Za-z_$][\w$]*(?=\()/y],
+ ['property', /\b[A-Za-z_$][\w$]*(?=\s*:)/y],
+ ],
+ html: [
+ ['tag', /<\/?[a-z][\w-]*|\/?>/y],
+ ['attr', /\b[a-z-]+(?==)/y],
+ ['string', /"[^"\n]*"/y],
+ ],
+ shell: [
+ ['keyword', /^(?:npm|npx)\b/y],
+ ['string', /\b(?:install|run)\b/y],
+ ],
+};
+
+function highlight(code: HTMLElement, language: string) {
+ const text = code.textContent ?? '';
+ const fragment = document.createDocumentFragment();
+ let plain = '';
+ let index = 0;
+ const flush = () => { if (plain) fragment.append(plain); plain = ''; };
+ while (index < text.length) {
+ let matched = false;
+ for (const [kind, pattern] of rules[language]) {
+ pattern.lastIndex = index;
+ const match = pattern.exec(text);
+ if (!match || !match[0]) continue;
+ flush();
+ const span = document.createElement('span');
+ span.className = `tok-${kind}`;
+ span.textContent = match[0];
+ fragment.append(span);
+ index += match[0].length;
+ matched = true;
+ break;
+ }
+ if (!matched) plain += text[index++];
+ }
+ flush();
+ code.replaceChildren(fragment);
+}
+
+export function highlightCodeSamples() {
+ for (const code of document.querySelectorAll<HTMLElement>('code[data-lang]')) {
+ if (code.dataset.lang && code.dataset.lang in rules) highlight(code, code.dataset.lang);
+ }
+}
diff --git a/site/src/main.ts b/site/src/main.ts
index b4606a6..57c79bd 100644
--- a/site/src/main.ts
+++ b/site/src/main.ts
@@ -1,4 +1,5 @@
import { LIVE_EXPRESSIONS, loadMeshAvatar, type LiveExpression, type MeshAvatar } from 'mesh-avatar';
+import { highlightCodeSamples } from './highlight';
import { copy, language } from './messages';
import overviewImage from '../../docs/images/en/01-overview.png';
import handlesImage from '../../docs/images/en/02-edit-handles.png';
@@ -30,6 +31,8 @@ for (const image of document.querySelectorAll<HTMLImageElement>('img[data-screen
if (link) link.href = image.src;
}
+highlightCodeSamples();
+
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 b646e28..8af55d5 100644
--- a/site/src/style.css
+++ b/site/src/style.css
@@ -119,6 +119,13 @@ h3 { margin: 0; font-size: 18px; font-weight: 650; line-height: 1.4; letter-spac
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
pre { margin: 0; padding: 20px 18px; overflow-x: auto; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; line-height: 1.8; tab-size: 2; }
pre:focus-visible { outline: 2px solid #7aa2f7; outline-offset: -4px; }
+.tok-comment { color: #7f8ea3; }
+.tok-keyword { color: #c4a7f5; }
+.tok-string { color: #a5d6a7; }
+.tok-number, .tok-literal { color: #f6c177; }
+.tok-function { color: #82b1ff; }
+.tok-property, .tok-attr { color: #f5b8c4; }
+.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; }
.site-footer { padding-block: 40px; font-size: 12px; }