Visual UI for Explainroo. Explainer videos and product demos made by your AI agent. Free and open source: a local voice (Kokoro), word timing (Whisper) and a canvas renderer turn a script into a narrated MP4.
import JSZip from 'jszip';
import { Project } from '../types/explainroo';
export function generateScriptMd(project: Project): string {
let md = `---
title: "${project.title}"
look: "${project.look}"
music: "${project.musicTrack}"
voiceRate: ${project.voiceRate}
voicePitch: ${project.voicePitch}
autoDuck: ${project.autoDuck}
---\n\n`;
project.scenes.forEach((scene, idx) => {
md += `## Scene ${idx + 1}: ${scene.title}\n\n`;
md += `${scene.narration}\n\n`;
if (scene.elements.length > 0) {
md += `<!-- Visual Elements Anchors:\n`;
scene.elements.forEach((el) => {
md += ` - [${el.type}] "${el.label || el.type}" triggers on word: "${el.triggerWord || 'start'}"\n`;
});
md += `-->\n\n`;
}
});
return md;
}
export function generateScenesJs(project: Project): string {
let js = `// Generated by Explainroo UI (No-Code Visual Builder)
// Compatible with Explainroo CLI specifications: node bin/explainroo.js render
export const config = {
width: 1280,
height: 720,
fps: 30,
look: '${project.look}',
music: '${project.musicTrack}',
};
export const scenes = [\n`;
project.scenes.forEach((scene, sIdx) => {
js += ` {\n`;
js += ` id: '${scene.id}',\n`;
js += ` title: '${scene.title.replace(/'/g, "\\'")}',\n`;
js += ` durationSeconds: ${scene.durationSeconds},\n`;
js += ` elements: [\n`;
scene.elements.forEach((el) => {
js += ` {\n`;
js += ` id: '${el.id}',\n`;
js += ` type: '${el.type}',\n`;
js += ` x: ${el.x},\n`;
js += ` y: ${el.y},\n`;
js += ` width: ${el.width},\n`;
js += ` height: ${el.height},\n`;
js += ` label: '${(el.label || '').replace(/'/g, "\\'")}',\n`;
js += ` color: '${el.color}',\n`;
js += ` animation: '${el.animation}',\n`;
js += ` triggerWord: '${el.triggerWord || ''}',\n`;
js += ` triggerTimeSec: ${el.triggerTimeSec || 0},\n`;
js += ` },\n`;
});
js += ` ],\n`;
js += ` draw(ctx, rc, t) {\n`;
js += ` // Custom render callback for Explainroo CLI headless renderer\n`;
js += ` // Coordinates correspond to 1280x720 canvas canvas\n`;
js += ` }\n`;
js += ` }${sIdx < project.scenes.length - 1 ? ',' : ''}\n`;
});
js += `];\n\nexport default scenes;\n`;
return js;
}
export function generateConfigJson(project: Project): string {
return JSON.stringify(
{
name: project.title,
version: '1.0.0',
description: project.description,
engine: 'explainroo-v2',
render: {
width: 1280,
height: 720,
fps: 30,
look: project.look,
autoDucking: project.autoDuck,
backgroundMusic: project.musicTrack,
},
},
null,
2
);
}
export function generateStandaloneHtml(project: Project): string {
const safeProjectJson = JSON.stringify(project).replace(/<\/script>/gi, '<\\/script>');
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${project.title.replace(/</g, '<')} - Explainroo Player</title>
<script src="https://unpkg.com/[email protected]/bundled/rough.js"></script>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background-color: #0b0f19;
color: #f1f5f9;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 16px;
}
.player-wrap {
width: 100%;
max-width: 1100px;
background: #111827;
border: 1px solid #1f2937;
border-radius: 20px;
overflow: hidden;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7);
display: flex;
flex-direction: column;
}
.header {
padding: 12px 20px;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid #1f2937;
background: #0f172a;
}
.brand {
display: flex;
align-items: center;
gap: 10px;
font-weight: 800;
font-size: 14px;
color: #fff;
}
.brand span { color: #f59e0b; }
.badge {
font-size: 11px;
padding: 3px 8px;
background: rgba(245, 158, 11, 0.15);
border: 1px solid rgba(245, 158, 11, 0.3);
color: #fbbf24;
border-radius: 9999px;
font-family: monospace;
}
.stage {
position: relative;
width: 100%;
aspect-ratio: 16 / 9;
background: #090d16;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
object-fit: contain;
display: block;
}
.subtitles {
position: absolute;
bottom: 16px;
left: 16px;
right: 16px;
display: flex;
justify-content: center;
pointer-events: none;
z-index: 10;
}
.subtitles-pill {
background: rgba(15, 23, 42, 0.9);
backdrop-filter: blur(8px);
border: 1px solid rgba(51, 65, 85, 0.8);
padding: 8px 18px;
border-radius: 9999px;
max-width: 90%;
text-align: center;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 6px;
font-size: 13px;
font-weight: 600;
color: #cbd5e1;
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
}
.subtitles-pill .active {
background: #fbbf24;
color: #020617;
padding: 1px 6px;
border-radius: 4px;
font-weight: 900;
transform: scale(1.08);
box-shadow: 0 0 16px rgba(251, 191, 36, 0.8);
transition: all 0.1s ease;
}
.subtitles-pill .past { color: #64748b; }
.controls {
background: #0f172a;
border-top: 1px solid #1f2937;
padding: 12px 20px;
display: flex;
flex-direction: column;
gap: 10px;
}
.timeline-row {
display: flex;
align-items: center;
gap: 12px;
}
.time-lbl {
font-family: monospace;
font-size: 11px;
color: #94a3b8;
width: 44px;
}
.time-lbl.right { text-align: right; }
.slider {
flex: 1;
height: 6px;
-webkit-appearance: none;
appearance: none;
background: #334155;
border-radius: 9999px;
outline: none;
cursor: pointer;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 14px;
height: 14px;
border-radius: 50%;
background: #f59e0b;
cursor: pointer;
}
.buttons-row {
display: flex;
align-items: center;
justify-content: space-between;
}
.btn-group {
display: flex;
align-items: center;
gap: 8px;
}
button {
background: #1e293b;
border: 1px solid #334155;
color: #e2e8f0;
padding: 6px 14px;
border-radius: 8px;
font-size: 12px;
font-weight: 600;
cursor: pointer;
display: flex;
align-items: center;
gap: 6px;
transition: background 0.15s;
}
button:hover { background: #334155; color: #fff; }
button.primary {
background: #f59e0b;
color: #020617;
border-color: #f59e0b;
font-weight: 800;
}
button.primary:hover { background: #d97706; }
.scene-title {
font-size: 12px;
color: #94a3b8;
font-weight: 500;
}
</style>
</head>
<body>
<div class="player-wrap">
<div class="header">
<div class="brand">
<span>🦘</span> Explainroo <span>UI</span>
<span class="badge">Standalone Offline Player</span>
</div>
<div class="scene-title" id="sceneIndicator">Scene 1 / 1</div>
</div>
<div class="stage">
<canvas id="playerCanvas" width="1280" height="720"></canvas>
<div class="subtitles" id="subtitlesWrap" style="display: none;">
<div class="subtitles-pill" id="subtitlesPill"></div>
</div>
</div>
<div class="controls">
<div class="timeline-row">
<span class="time-lbl" id="curTime">0.0s</span>
<input type="range" class="slider" id="scrubber" min="0" max="10" step="0.05" value="0">
<span class="time-lbl right" id="totalTime">0.0s</span>
</div>
<div class="buttons-row">
<div class="btn-group">
<button class="primary" id="playBtn">▶ Play</button>
<button id="prevBtn">⏮ Prev</button>
<button id="nextBtn">Next ⏭</button>
<button id="restartBtn">↺ Restart</button>
</div>
<div class="btn-group">
<button id="voiceBtn">🔊 Voice: ON</button>
<button id="fsBtn">⛶ Fullscreen</button>
</div>
</div>
</div>
</div>
<script>
const project = ${safeProjectJson};
let currentSceneIdx = 0;
let currentTime = 0;
let isPlaying = false;
let voiceEnabled = true;
let lastTimestamp = 0;
let liveWordIdx = -1;
const canvas = document.getElementById('playerCanvas');
const ctx = canvas.getContext('2d');
const playBtn = document.getElementById('playBtn');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const restartBtn = document.getElementById('restartBtn');
const voiceBtn = document.getElementById('voiceBtn');
const fsBtn = document.getElementById('fsBtn');
const scrubber = document.getElementById('scrubber');
const curTimeLbl = document.getElementById('curTime');
const totalTimeLbl = document.getElementById('totalTime');
const sceneIndicator = document.getElementById('sceneIndicator');
const subtitlesWrap = document.getElementById('subtitlesWrap');
const subtitlesPill = document.getElementById('subtitlesPill');
const THEMES = {
whiteboard: { bg: '#faf9f5', fg: '#0f172a', font: 'system-ui' },
darktech: { bg: '#090d16', fg: '#38bdf8', font: 'system-ui' },
blueprint: { bg: '#0c2340', fg: '#ffffff', font: 'monospace' },
chalkboard: { bg: '#15251d', fg: '#f8fafc', font: 'system-ui' }
};
function getActiveScene() {
return project.scenes[currentSceneIdx] || project.scenes[0];
}
function renderScene(time) {
const scene = getActiveScene();
const theme = THEMES[project.look] || THEMES.whiteboard;
const w = canvas.width;
const h = canvas.height;
ctx.save();
ctx.fillStyle = theme.bg;
ctx.fillRect(0, 0, w, h);
// Grid
ctx.strokeStyle = project.look === 'blueprint' ? 'rgba(255,255,255,0.08)' : 'rgba(0,0,0,0.04)';
ctx.lineWidth = 1;
for (let x = 0; x < w; x += 40) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, h); ctx.stroke(); }
for (let y = 0; y < h; y += 40) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(w, y); ctx.stroke(); }
const rc = window.rough ? window.rough.canvas(canvas) : null;
// Render elements
const elements = [...(scene.elements || [])].sort((a,b) => (a.order||0) - (b.order||0));
elements.forEach(el => {
const trigger = el.triggerTimeSec || 0;
if (time < trigger) return;
const progress = Math.min(1, Math.max(0, (time - trigger) / 0.5));
ctx.save();
if (el.animation === 'pop') {
ctx.translate(el.x + el.width/2, el.y + el.height/2);
ctx.scale(progress, progress);
ctx.translate(-(el.x + el.width/2), -(el.y + el.height/2));
}
const color = el.color || theme.fg;
const opts = { stroke: color, strokeWidth: el.strokeWidth || 2.5, roughness: 1.2 };
if (rc) {
if (el.type === 'rectangle' || el.type === 'browser_window' || el.type === 'terminal_window') {
rc.rectangle(el.x, el.y, el.width, el.height, opts);
} else if (el.type === 'circle' || el.type === 'database') {
const rad = Math.min(el.width, el.height)/2;
rc.circle(el.x + rad, el.y + rad, rad * 2, opts);
} else if (el.type === 'arrow') {
rc.line(el.x, el.y, el.x + el.width, el.y + el.height, opts);
} else {
rc.rectangle(el.x, el.y, el.width, el.height, opts);
}
} else {
ctx.strokeStyle = color;
ctx.lineWidth = el.strokeWidth || 2.5;
ctx.strokeRect(el.x, el.y, el.width, el.height);
}
if (el.label) {
ctx.font = 'bold 16px ' + theme.font;
ctx.fillStyle = color;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(el.label, el.x + el.width/2, el.y + el.height/2);
}
ctx.restore();
});
ctx.restore();
updateSubtitles(scene, time);
}
function updateSubtitles(scene, time) {
if (!scene.narration) {
subtitlesWrap.style.display = 'none';
return;
}
subtitlesWrap.style.display = 'flex';
const words = scene.narration.split(/\\s+/).filter(Boolean);
const duration = scene.durationSeconds || 7;
const progress = Math.min(0.999, Math.max(0, time / duration));
const fallbackIdx = Math.min(words.length - 1, Math.floor(progress * words.length));
const activeIdx = (isPlaying && liveWordIdx >= 0) ? liveWordIdx : fallbackIdx;
const windowSize = Math.min(words.length, 7);
const half = Math.floor(windowSize / 2);
let startIdx = Math.max(0, activeIdx - half);
let endIdx = Math.min(words.length, startIdx + windowSize);
if (endIdx - startIdx < windowSize) {
startIdx = Math.max(0, endIdx - windowSize);
}
let html = '';
for (let i = startIdx; i < endIdx; i++) {
const cls = i === activeIdx ? 'active' : (i < activeIdx ? 'past' : '');
html += '<span class="' + cls + '">' + words[i] + '</span> ';
}
subtitlesPill.innerHTML = html;
}
function speakNarration(text) {
if (!voiceEnabled || !window.speechSynthesis || !text) return;
window.speechSynthesis.cancel();
const u = new SpeechSynthesisUtterance(text);
u.rate = project.voiceRate || 1.0;
u.pitch = project.voicePitch || 1.0;
u.onboundary = (e) => {
if (e.name === 'word') {
const before = text.slice(0, e.charIndex).trim();
liveWordIdx = before ? before.split(/\\s+/).filter(Boolean).length : 0;
}
};
u.onend = () => { liveWordIdx = -1; };
u.onerror = () => { liveWordIdx = -1; };
window.speechSynthesis.speak(u);
}
function stopSpeaking() {
if (window.speechSynthesis) window.speechSynthesis.cancel();
liveWordIdx = -1;
}
function setScene(idx) {
currentSceneIdx = Math.max(0, Math.min(project.scenes.length - 1, idx));
currentTime = 0;
stopSpeaking();
const scene = getActiveScene();
scrubber.max = scene.durationSeconds || 7;
totalTimeLbl.textContent = (scene.durationSeconds || 7).toFixed(1) + 's';
sceneIndicator.textContent = 'Scene ' + (currentSceneIdx + 1) + ' / ' + project.scenes.length + ' · ' + scene.title;
renderScene(0);
if (isPlaying) {
speakNarration(scene.narration);
}
}
function loop(timestamp) {
if (!isPlaying) return;
if (!lastTimestamp) lastTimestamp = timestamp;
const delta = (timestamp - lastTimestamp) / 1000;
lastTimestamp = timestamp;
currentTime += delta;
const scene = getActiveScene();
const duration = scene.durationSeconds || 7;
if (currentTime >= duration) {
if (currentSceneIdx < project.scenes.length - 1) {
setScene(currentSceneIdx + 1);
} else {
currentTime = duration;
isPlaying = false;
playBtn.textContent = '▶ Play';
stopSpeaking();
}
}
curTimeLbl.textContent = currentTime.toFixed(1) + 's';
scrubber.value = currentTime;
renderScene(currentTime);
requestAnimationFrame(loop);
}
playBtn.addEventListener('click', () => {
isPlaying = !isPlaying;
if (isPlaying) {
playBtn.textContent = '⏸ Pause';
lastTimestamp = performance.now();
const scene = getActiveScene();
if (currentTime >= (scene.durationSeconds || 7)) currentTime = 0;
speakNarration(scene.narration);
requestAnimationFrame(loop);
} else {
playBtn.textContent = '▶ Play';
stopSpeaking();
}
});
prevBtn.addEventListener('click', () => setScene(currentSceneIdx - 1));
nextBtn.addEventListener('click', () => setScene(currentSceneIdx + 1));
restartBtn.addEventListener('click', () => {
currentTime = 0;
stopSpeaking();
if (isPlaying) speakNarration(getActiveScene().narration);
renderScene(0);
});
voiceBtn.addEventListener('click', () => {
voiceEnabled = !voiceEnabled;
voiceBtn.textContent = voiceEnabled ? '🔊 Voice: ON' : '🔇 Voice: OFF';
if (!voiceEnabled) stopSpeaking();
else if (isPlaying) speakNarration(getActiveScene().narration);
});
fsBtn.addEventListener('click', () => {
if (!document.fullscreenElement) document.documentElement.requestFullscreen();
else document.exitFullscreen();
});
scrubber.addEventListener('input', (e) => {
currentTime = parseFloat(e.target.value);
curTimeLbl.textContent = currentTime.toFixed(1) + 's';
renderScene(currentTime);
});
// Initial load
setScene(0);
</script>
</body>
</html>`;
}
export async function downloadProjectZip(project: Project) {
const zip = new JSZip();
zip.file('script.md', generateScriptMd(project));
zip.file('scenes.js', generateScenesJs(project));
zip.file('explainroo.config.json', generateConfigJson(project));
zip.file('standalone-player.html', generateStandaloneHtml(project));
zip.file('project.explainroo.json', JSON.stringify(project, null, 2));
zip.file(
'README.md',
`# ${project.title}
Generated with **Explainroo UI** — Visual Explainer Video Studio.
## Included Files:
1. **standalone-player.html** - Complete zero-install offline interactive video player! Double-click to open in any web browser.
2. **project.explainroo.json** - Complete project backup to reload back into Explainroo UI.
3. **script.md** - Narration voiceover script with word anchor markers.
4. **scenes.js** - Diagram layout and animation coordinate triggers.
5. **explainroo.config.json** - CLI configuration file.
## Quick CLI Render
If you have the Explainroo CLI installed:
\`\`\`bash
npm install -g explainroo
explainroo render
\`\`\`
`
);
const blob = await zip.generateAsync({ type: 'blob' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${project.title.toLowerCase().replace(/[^a-z0-9]/g, '-')}-explainroo.zip`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
export function downloadStandaloneHtml(project: Project) {
const htmlContent = generateStandaloneHtml(project);
downloadTextFile(
`${project.title.toLowerCase().replace(/[^a-z0-9]/g, '-')}-player.html`,
htmlContent
);
}
export function downloadProjectJson(project: Project) {
const jsonContent = JSON.stringify(project, null, 2);
downloadTextFile(
`${project.title.toLowerCase().replace(/[^a-z0-9]/g, '-')}.explainroo.json`,
jsonContent
);
}
export function downloadTextFile(filename: string, content: string) {
const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}