A lightweight 2.5D character player for the web, with lip sync and webcam face tracking.
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LivLiv Player: demo</title>
<!-- only face tracking needs this: where the browser finds @mediapipe/tasks-vision without a bundler -->
<script type="importmap">{ "imports": { "@mediapipe/tasks-vision": "https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/vision_bundle.mjs" } }</script>
<style>
html, body { margin: 0; height: 100%; background: #20222b; color: #eef; font: 14px system-ui, sans-serif }
canvas { position: relative; display: block; width: 100%; height: 100% }
#cover { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; pointer-events: none }
.bar { position: fixed; z-index: 1; left: 12px; right: 12px; top: 12px; display: flex; flex-wrap: wrap; gap: 6px }
button, label { font: inherit; padding: 6px 11px; border: 0; border-radius: 8px; background: #ffffff1c; color: inherit; cursor: pointer }
button[aria-pressed="true"] { background: #ff4d5e }
#status { position: fixed; z-index: 1; left: 12px; bottom: 12px; margin: 0; opacity: .8 }
</style>
<img id="cover" alt="" src="../characters/d03ft/cover.webp">
<script>if (new URLSearchParams(location.search).has('model')) document.getElementById('cover').removeAttribute('src')</script>
<canvas id="stage"></canvas>
<div class="bar" id="bar"></div>
<p id="status" role="status"></p>
<script type="module">
import { Player } from '../dist/index.js'
import { cameraButton } from './camera.js'
import { loadWithFallback, loading, needWebGL, sameSite, watchContext } from './loading.js'
needWebGL(Player)
const canvas = document.getElementById('stage'), cover = document.getElementById('cover')
const player = new Player(canvas)
watchContext(player)
const bar = document.getElementById('bar'), status = document.getElementById('status')
window.player = player // to try things in the console
const button = (text, onClick) => {
const b = document.createElement('button'); b.textContent = text; b.onclick = () => onClick(b); bar.append(b); return b
}
// a model: a featured character, ?model=<URL of a model.json or its folder, on this site>, or a file picked below
async function show(source) {
status.textContent = ''
const done = loading(canvas, cover, source)
try {
const m = await loadWithFallback(player, source, { onProgress: done.progress })
status.textContent = `${m.name} · ${m.format} ${m.version} · mark: ${m.watermark ?? 'none'}${m.meta?.license ? ` · ${m.meta.license.id}` : ''}`
controls(m)
} catch (e) {
if (e.name === 'AbortError') return // another was picked meanwhile
status.textContent = e.message
// a VRM has its own page here
if (/VRM/.test(e.message)) { const a = document.createElement('a'); a.href = 'vrm.html'; a.style.color = 'inherit'; a.textContent = ' Open the VRM page'; status.append(a) }
controls(player.model)
} finally { done() }
}
function controls(m) {
bar.replaceChildren()
const pick = document.createElement('label')
pick.innerHTML = 'open a folder… <input type="file" webkitdirectory hidden>'
pick.querySelector('input').onchange = (e) => e.target.files.length && show(e.target.files)
const one = document.createElement('label') // a .livliv package (one file)
one.innerHTML = 'open a .livliv… <input type="file" accept=".livliv,.zip,.vrm,.glb" hidden>'
one.querySelector('input').onchange = (e) => e.target.files.length && show(e.target.files[0])
bar.append(pick, one)
if (!m) return
const exprs = [{ id: null, label: 'neutral' }, ...m.expressions].map((x) => button(x.label, (b) => {
player.setExpression(x.id)
exprs.forEach((o) => o.setAttribute('aria-pressed', String(o === b)))
}))
for (const a of ['nod', 'shake', 'tilt', 'hop']) button(a, () => player.act(a))
let mic = null
button('microphone', async (b) => {
if (mic) { mic.stop(); mic = null } else {
const { startLipSync } = await import('../dist/voice.js')
mic = await startLipSync(player).catch((e) => { status.textContent = e.message; return null })
}
b.setAttribute('aria-pressed', String(!!mic))
})
window.camera?.running?.stop() // a new model, new buttons: the old button's tracking ends with it
window.camera = cameraButton(button('camera'), player, status)
}
const asked = new URLSearchParams(location.search).get('model')
if (!asked || sameSite(asked)) show(asked || '../characters/d03ft.livliv')
else {
status.textContent = '?model= opens files on this site only. For another file, save it and use the buttons above.'
controls(null)
}
</script>
</html>