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: a PNGTuber</title>
<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 { display: block; width: 100%; height: 100% }
.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>
<canvas id="stage"></canvas>
<div class="bar">
<label>open pictures… <input id="pick" type="file" accept="image/*" multiple hidden></label>
<button id="mic">microphone</button>
<button id="cam">camera</button>
</div>
<p id="status" role="status"></p>
<script type="module">
import { BodyPlayer } from '../dist/body.js'
import { PngBody } from '../dist/png.js'
import { cameraButton } from './camera.js'
// a PNGTuber: pictures named closed, open, and if you have them blink and blink-open (any image type). With none
// picked, a face drawn here stands in
const player = new BodyPlayer(document.getElementById('stage'), new PngBody())
const status = document.getElementById('status')
window.player = player
/** a stand-in picture: a round face, its eyes open or shut, its mouth closed or open */
async function face(eyes, mouth) {
const c = new OffscreenCanvas(400, 480), g = c.getContext('2d')
g.fillStyle = '#ffd6c4'; g.beginPath(); g.ellipse(200, 250, 160, 170, 0, 0, 7); g.fill()
g.fillStyle = '#3b2b4a'; g.beginPath(); g.ellipse(200, 130, 170, 90, 0, Math.PI, 0); g.fill()
g.strokeStyle = g.fillStyle = '#3b2b4a'; g.lineWidth = 9; g.lineCap = 'round'
for (const x of [140, 260]) {
g.beginPath()
if (eyes) g.ellipse(x, 240, 14, 22, 0, 0, 7), g.fill(); else g.moveTo(x - 18, 242), g.lineTo(x + 18, 242), g.stroke()
}
g.beginPath()
if (mouth) { g.fillStyle = '#b5474f'; g.ellipse(200, 330, 34, 28, 0, 0, 7); g.fill() } else { g.moveTo(170, 330); g.quadraticCurveTo(200, 348, 230, 330); g.stroke() }
return c.convertToBlob()
}
async function show(files) {
try {
const m = await player.load(files)
status.textContent = `${m.name} · ${m.format}`
} catch (e) { if (e.name !== 'AbortError') status.textContent = e.message } // (another was picked meanwhile)
}
document.getElementById('pick').onchange = (e) => e.target.files.length && show(e.target.files)
show({ closed: await face(true, false), open: await face(true, true), blink: await face(false, false), 'blink-open': await face(false, true) })
let mic = null
document.getElementById('mic').onclick = async (e) => {
if (mic) { mic.stop(); mic = null } else {
const { startLipSync } = await import('../dist/voice.js')
mic = await startLipSync(player).catch((err) => { status.textContent = err.message; return null })
}
e.target.setAttribute('aria-pressed', String(!!mic))
}
window.camera = cameraButton(document.getElementById('cam'), player, status)
</script>
</html>