| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Fission</title> |
| 7 | <style> |
| 8 | :root { color-scheme: dark; --bg: #141416; --panel: #1c1c20; --line: #2c2c33; --fg: #e6e6ea; --dim: #8b8b96; --accent: #7aa2ff; --warn: #ffb454; --bad: #ff6b6b; } |
| 9 | * { box-sizing: border-box; } |
| 10 | body { margin: 0; height: 100vh; display: grid; grid-template: "bar bar" auto "stage side" 1fr / 1fr 340px; background: var(--bg); color: var(--fg); font: 13px/1.4 system-ui, sans-serif; } |
| 11 | button, select { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 5px 10px; } |
| 12 | button:hover:not(:disabled) { border-color: var(--accent); cursor: pointer; } |
| 13 | button:disabled { opacity: .45; } |
| 14 | header { grid-area: bar; display: flex; gap: 8px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--line); } |
| 15 | header h1 { font-size: 14px; margin: 0 8px 0 0; letter-spacing: .08em; } |
| 16 | #status { margin-left: auto; color: var(--dim); font-variant-numeric: tabular-nums; } |
| 17 | #banner { grid-column: 1 / -1; display: none; padding: 10px 14px; background: #3a1d1d; color: #ffd0d0; border-bottom: 1px solid #6a2c2c; } |
| 18 | main { grid-area: stage; display: flex; flex-direction: column; min-height: 0; padding: 12px; gap: 10px; } |
| 19 | #stage { flex: 1; min-height: 0; display: grid; place-items: center; border: 1px dashed var(--line); border-radius: 8px; position: relative; overflow: hidden; } |
| 20 | #stage.drop { border-color: var(--accent); background: #1a2036; } |
| 21 | canvas { max-width: 100%; max-height: 100%; background: #000; } |
| 22 | #hint { position: absolute; color: var(--dim); pointer-events: none; text-align: center; } |
| 23 | #transport { display: flex; gap: 10px; align-items: center; } |
| 24 | #scrub { flex: 1; accent-color: var(--accent); } |
| 25 | #frame { min-width: 110px; font-variant-numeric: tabular-nums; } |
| 26 | aside { grid-area: side; border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; } |
| 27 | aside h2 { margin: 0; padding: 8px 12px; font-size: 11px; letter-spacing: .1em; text-transform: uppercase; color: var(--dim); } |
| 28 | #flow { width: 100%; height: 240px; background: var(--panel); border-block: 1px solid var(--line); } |
| 29 | #flow .edge { fill: none; stroke-width: 1.5; opacity: .8; } |
| 30 | #flow g.node { cursor: pointer; } |
| 31 | #flow rect { fill: #262630; stroke: var(--line); } |
| 32 | #flow g.node:hover rect { stroke: var(--accent); } |
| 33 | #flow g.sel rect { stroke: var(--accent); stroke-width: 2; } |
| 34 | #flow g.bypassed rect { stroke-dasharray: 3 2; opacity: .6; } |
| 35 | #flow text { fill: var(--fg); font-size: 10px; pointer-events: none; } |
| 36 | #nodes { flex: 1; overflow: auto; margin: 0; padding: 0; list-style: none; } |
| 37 | #nodes li { display: flex; justify-content: space-between; gap: 8px; padding: 5px 12px; cursor: pointer; border-bottom: 1px solid var(--line); } |
| 38 | #nodes li:hover { background: var(--panel); } |
| 39 | #nodes li.sel { background: #212a44; } |
| 40 | #nodes .kind { color: var(--dim); } |
| 41 | #nodes .missing { color: var(--bad); } |
| 42 | #notes { padding: 8px 12px; color: var(--warn); font-size: 12px; border-top: 1px solid var(--line); } |
| 43 | #notes:empty { display: none; } |
| 44 | input[type=file] { display: none; } |
| 45 | </style> |
| 46 | </head> |
| 47 | <body> |
| 48 | <header> |
| 49 | <h1>FISSION</h1> |
| 50 | <button id="demo">Demo</button> |
| 51 | <button id="open-comp">Open .comp…</button> |
| 52 | <button id="open-folder">Open folder…</button> |
| 53 | <select id="comps" hidden title="Compositions in the folder"></select> |
| 54 | <span id="status"></span> |
| 55 | </header> |
| 56 | <div id="banner" role="alert"></div> |
| 57 | <main> |
| 58 | <div id="stage"> |
| 59 | <canvas id="canvas" width="960" height="540"></canvas> |
| 60 | <div id="hint">Drop a <b>.comp</b> or a project folder here</div> |
| 61 | </div> |
| 62 | <div id="transport"> |
| 63 | <button id="play" disabled>Play</button> |
| 64 | <input id="scrub" type="range" min="0" max="0" value="0" step="1" disabled> |
| 65 | <span id="frame">–</span> |
| 66 | <select id="node" disabled title="Node to view"></select> |
| 67 | </div> |
| 68 | </main> |
| 69 | <aside> |
| 70 | <h2>Flow</h2> |
| 71 | <svg id="flow" viewBox="0 0 340 240" role="img" aria-label="Flow graph"></svg> |
| 72 | <h2>Nodes</h2> |
| 73 | <ul id="nodes"></ul> |
| 74 | <div id="notes"></div> |
| 75 | </aside> |
| 76 | <input id="file-comp" type="file" accept=".comp,.setting,.txt,.ttf,.otf,.ttc,image/*" multiple> |
| 77 | <input id="file-folder" type="file" webkitdirectory multiple> |
| 78 | <script type="module"> |
| 79 | import init, { Fission } from './pkg/fission_web.js'; |
| 80 | |
| 81 | const $ = (id) => document.getElementById(id); |
| 82 | const MEDIA_EXT = /\.(png|jpe?g|tiff?|webp|bmp|gif|exr|tga)$/i; |
| 83 | const FONT_EXT = /\.(ttf|otf|ttc)$/i; |
| 84 | const MAX_FILE_BYTES = 256 * 1024 * 1024; |
| 85 | const PORT_COLOR = { Background: '#e6b450', Input: '#e6b450', Foreground: '#6fcf97', EffectMask: '#7aa2ff' }; |
| 86 | |
| 87 | const fail = (message) => { $('banner').textContent = message; $('banner').style.display = 'block'; }; |
| 88 | |
| 89 | let app = null; |
| 90 | let info = null; |
| 91 | let sources = new Map(); |
| 92 | let frame = 0, playing = false, playStart = 0, playOrigin = 0, lastDrawn = null; |
| 93 | let dirty = true, fps = 0, lastTick = 0, evalMs = 0, broken = false; |
| 94 | |
| 95 | async function boot() { |
| 96 | if (!navigator.gpu) { |
| 97 | fail('This browser has no WebGPU. Use a current Chrome, Edge or Safari, or enable WebGPU in Firefox.'); |
| 98 | return; |
| 99 | } |
| 100 | try { |
| 101 | await init(); |
| 102 | app = await Fission.create($('canvas')); |
| 103 | } catch (e) { |
| 104 | fail(`Could not start the renderer: ${e.message ?? e}`); |
| 105 | return; |
| 106 | } |
| 107 | loadComp('Demo', Fission.demo_comp()); |
| 108 | requestAnimationFrame(tick); |
| 109 | } |
| 110 | |
| 111 | function loadComp(name, text) { |
| 112 | if (broken) return; |
| 113 | try { |
| 114 | info = JSON.parse(app.load_comp(text, name)); |
| 115 | } catch (e) { |
| 116 | fail(`${name}: ${e.message ?? e}`); |
| 117 | return; |
| 118 | } |
| 119 | $('banner').style.display = 'none'; |
| 120 | $('hint').hidden = true; |
| 121 | const [start, end] = info.range; |
| 122 | frame = start; playing = false; lastDrawn = null; dirty = true; |
| 123 | $('scrub').min = start; $('scrub').max = end; $('scrub').value = start; |
| 124 | for (const id of ['play', 'scrub', 'node']) $(id).disabled = false; |
| 125 | $('play').textContent = 'Play'; |
| 126 | $('node').replaceChildren(...info.nodes.map((n) => new Option(`${n.name} (${n.kind})`, n.id))); |
| 127 | $('node').value = info.target ?? info.nodes[0]?.id; |
| 128 | refreshGraph(); |
| 129 | } |
| 130 | |
| 131 | function refreshGraph() { |
| 132 | info = JSON.parse(app.describe()); |
| 133 | drawFlow(); |
| 134 | const ul = $('nodes'); |
| 135 | ul.replaceChildren(...info.nodes.map((n) => { |
| 136 | const li = document.createElement('li'); |
| 137 | li.dataset.id = n.id; |
| 138 | li.classList.toggle('sel', n.id === info.target); |
| 139 | const miss = n.clips.filter((c) => !c.found).length; |
| 140 | li.innerHTML = `<span>${esc(n.name)}${miss ? ` <span class="missing">${miss} missing</span>` : ''}</span><span class="kind">${esc(n.kind)}</span>`; |
| 141 | li.onclick = () => selectNode(n.id); |
| 142 | return li; |
| 143 | })); |
| 144 | const missing = info.nodes.flatMap((n) => n.clips.filter((c) => !c.found).map((c) => c.file.split(/[\\/]/).pop())); |
| 145 | $('notes').textContent = missing.length ? `Missing media: ${[...new Set(missing)].join(', ')}. Drop the project folder to supply it.` : ''; |
| 146 | } |
| 147 | |
| 148 | function selectNode(id) { |
| 149 | app.set_node(id); |
| 150 | info.target = id; |
| 151 | $('node').value = id; |
| 152 | dirty = true; |
| 153 | for (const li of $('nodes').children) li.classList.toggle('sel', Number(li.dataset.id) === id); |
| 154 | drawFlow(); |
| 155 | } |
| 156 | |
| 157 | function drawFlow() { |
| 158 | const svg = $('flow'); |
| 159 | const [W, H, PAD, BW, BH] = [340, 240, 14, 56, 20]; |
| 160 | const xs = info.nodes.map((n) => n.pos[0]), ys = info.nodes.map((n) => n.pos[1]); |
| 161 | const [x0, y0] = [Math.min(...xs), Math.min(...ys)]; |
| 162 | const sx = (W - 2 * PAD - BW) / (Math.max(...xs) - x0 || 1), sy = (H - 2 * PAD - BH) / (Math.max(...ys) - y0 || 1); |
| 163 | const at = new Map(info.nodes.map((n) => [n.name, [PAD + (n.pos[0] - x0) * Math.min(sx, 1.2), PAD + (n.pos[1] - y0) * Math.min(sy, 1.2)]])); |
| 164 | let out = ''; |
| 165 | for (const n of info.nodes) for (const [port, src] of n.from) { |
| 166 | const a = at.get(src), b = at.get(n.name); |
| 167 | if (!a || !b) continue; |
| 168 | const [ax, ay, bx, by] = [a[0] + BW, a[1] + BH / 2, b[0], b[1] + BH / 2]; |
| 169 | const mx = (ax + bx) / 2; |
| 170 | out += `<path class="edge" stroke="${PORT_COLOR[port] ?? '#8b8b96'}" d="M${ax},${ay} C${mx},${ay} ${mx},${by} ${bx},${by}"/>`; |
| 171 | } |
| 172 | for (const n of info.nodes) { |
| 173 | const [x, y] = at.get(n.name); |
| 174 | const cls = ['node', n.id === info.target ? 'sel' : '', n.bypassed ? 'bypassed' : ''].join(' '); |
| 175 | out += `<g class="${cls}" data-id="${n.id}"><rect x="${x}" y="${y}" width="${BW}" height="${BH}" rx="4"/><text x="${x + 5}" y="${y + 13}">${esc(n.name.slice(0, 11))}</text></g>`; |
| 176 | } |
| 177 | svg.innerHTML = out; |
| 178 | svg.querySelectorAll('g.node').forEach((g) => { g.onclick = () => selectNode(Number(g.dataset.id)); }); |
| 179 | } |
| 180 | |
| 181 | const esc = (s) => s.replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' })[c]); |
| 182 | |
| 183 | function tick(now) { |
| 184 | requestAnimationFrame(tick); |
| 185 | if (!info || broken) return; |
| 186 | const [start, end] = info.range; |
| 187 | if (playing) { |
| 188 | const len = end - start + 1; |
| 189 | frame = start + (playOrigin - start + Math.floor(((now - playStart) / 1000) * info.fps)) % len; |
| 190 | $('scrub').value = frame; |
| 191 | } |
| 192 | if (dirty || frame !== lastDrawn) { |
| 193 | let ms; |
| 194 | try { |
| 195 | ms = app.render(frame); |
| 196 | } catch (e) { |
| 197 | broken = true; |
| 198 | fail(`Render failed: ${e.message ?? e}. Reload the page; details are in the console.`); |
| 199 | return; |
| 200 | } |
| 201 | const gpuError = app.gpu_error(); |
| 202 | if (gpuError) fail(`GPU error: ${gpuError}`); |
| 203 | // `undefined`: the surface was not ready; stay dirty and try again next frame. |
| 204 | if (ms !== undefined) { |
| 205 | evalMs = ms; |
| 206 | if (lastTick) fps += ((1000 / (now - lastTick)) - fps) * 0.15; |
| 207 | lastTick = now; |
| 208 | lastDrawn = frame; dirty = false; |
| 209 | const bad = app.unsupported(); |
| 210 | $('notes').dataset.unsupported = bad.join(', '); |
| 211 | } |
| 212 | } else if (!playing) { |
| 213 | lastTick = 0; |
| 214 | } |
| 215 | const unsupported = $('notes').dataset.unsupported; |
| 216 | $('frame').textContent = `f ${frame} / ${end}`; |
| 217 | $('status').textContent = `${info.name} · ${info.width}×${info.height} · ${info.fps} fps · ${playing ? fps.toFixed(0) + ' fps · ' : ''}${evalMs.toFixed(1)} ms cpu${unsupported ? ' · unsupported: ' + unsupported : ''}`; |
| 218 | } |
| 219 | |
| 220 | function setPlaying(on) { |
| 221 | playing = on; |
| 222 | $('play').textContent = on ? 'Pause' : 'Play'; |
| 223 | playStart = performance.now(); playOrigin = frame; lastTick = 0; |
| 224 | } |
| 225 | |
| 226 | $('play').onclick = () => setPlaying(!playing); |
| 227 | $('scrub').oninput = (e) => { frame = Number(e.target.value); if (playing) setPlaying(true); dirty = true; }; |
| 228 | $('node').onchange = (e) => selectNode(Number(e.target.value)); |
| 229 | $('demo').onclick = () => { app.clear_files(); sources.clear(); $('comps').hidden = true; loadComp('Demo', Fission.demo_comp()); }; |
| 230 | $('open-comp').onclick = () => $('file-comp').click(); |
| 231 | $('open-folder').onclick = () => $('file-folder').click(); |
| 232 | $('comps').onchange = (e) => { const f = sources.get(e.target.value); if (f) f.text().then((t) => loadComp(e.target.value.replace(/\.comp$/i, ''), t)); }; |
| 233 | addEventListener('keydown', (e) => { |
| 234 | if (e.target.matches('select, input[type=range]') || !info) return; |
| 235 | if (e.code === 'Space') { e.preventDefault(); setPlaying(!playing); } |
| 236 | else if (e.code === 'ArrowLeft' || e.code === 'ArrowRight') { |
| 237 | const [start, end] = info.range; |
| 238 | frame = Math.min(end, Math.max(start, frame + (e.code === 'ArrowRight' ? 1 : -1))); |
| 239 | $('scrub').value = frame; if (playing) setPlaying(false); dirty = true; |
| 240 | } |
| 241 | }); |
| 242 | |
| 243 | $('file-comp').onchange = (e) => ingest([...e.target.files].map((file) => ({ path: file.name, file }))); |
| 244 | $('file-folder').onchange = (e) => ingest([...e.target.files].map((file) => ({ path: file.webkitRelativePath || file.name, file }))); |
| 245 | |
| 246 | const stage = $('stage'); |
| 247 | addEventListener('dragover', (e) => { e.preventDefault(); stage.classList.add('drop'); }); |
| 248 | addEventListener('dragleave', (e) => { if (!e.relatedTarget) stage.classList.remove('drop'); }); |
| 249 | addEventListener('drop', async (e) => { |
| 250 | e.preventDefault(); |
| 251 | stage.classList.remove('drop'); |
| 252 | const entries = [...e.dataTransfer.items].map((i) => i.webkitGetAsEntry?.()).filter(Boolean); |
| 253 | ingest(await walk(entries)); |
| 254 | }); |
| 255 | |
| 256 | async function walk(entries) { |
| 257 | const out = []; |
| 258 | const visit = async (entry) => { |
| 259 | if (entry.isFile) { |
| 260 | out.push({ path: entry.fullPath.replace(/^\//, ''), file: await new Promise((ok, no) => entry.file(ok, no)) }); |
| 261 | } else if (entry.isDirectory) { |
| 262 | const reader = entry.createReader(); |
| 263 | for (let batch; (batch = await new Promise((ok, no) => reader.readEntries(ok, no))).length;) { |
| 264 | await Promise.all(batch.map(visit)); |
| 265 | } |
| 266 | } |
| 267 | }; |
| 268 | await Promise.all(entries.map(visit)); |
| 269 | return out; |
| 270 | } |
| 271 | |
| 272 | async function ingest(files) { |
| 273 | if (!app) return; |
| 274 | const comps = files.filter(({ path }) => /\.comp$/i.test(path)); |
| 275 | if (comps.length) { app.clear_files(); sources.clear(); } |
| 276 | let dropped = 0; |
| 277 | for (const { path, file } of files) { |
| 278 | if (FONT_EXT.test(path) && file.size <= 32 * 1024 * 1024) { |
| 279 | app.add_font(new Uint8Array(await file.arrayBuffer())); |
| 280 | continue; |
| 281 | } |
| 282 | if (!MEDIA_EXT.test(path) || file.size > MAX_FILE_BYTES) continue; |
| 283 | if (!app.add_file(path, new Uint8Array(await file.arrayBuffer()))) dropped++; |
| 284 | } |
| 285 | if (comps.length) { |
| 286 | comps.sort((a, b) => a.path.localeCompare(b.path)); |
| 287 | for (const { path, file } of comps) sources.set(path, file); |
| 288 | $('comps').replaceChildren(...comps.map(({ path }) => new Option(path, path))); |
| 289 | $('comps').hidden = comps.length < 2; |
| 290 | const first = comps[0]; |
| 291 | loadComp(first.path.replace(/\.comp$/i, ''), await first.file.text()); |
| 292 | } else if (info) { |
| 293 | refreshGraph(); |
| 294 | dirty = true; |
| 295 | } |
| 296 | if (dropped) fail(`${dropped} file(s) skipped: dropped media is past the 512 MB memory limit.`); |
| 297 | } |
| 298 | |
| 299 | boot(); |
| 300 | </script> |
| 301 | </body> |
| 302 | </html> |