import { asciify, asciifyGif, asciifyVideo, type AsciifyVideoOptions } from 'asciify-engine'; /** Downloadable app helper, not an asciify-engine export. Returns cleanup immediately. */ export function mountAscii( host: HTMLElement, src: string, kind: 'image' | 'gif' | 'video' = 'image', options: AsciifyVideoOptions = {}, ) { // A fresh surface prevents a slow previous request from repainting the current view. const canvas = document.createElement('canvas'); canvas.width = 800; canvas.height = 600; canvas.style.display = 'block'; canvas.style.maxWidth = '100%'; canvas.setAttribute('aria-hidden', 'true'); host.append(canvas); let disposed = false; let stop: (() => void) | undefined; let errorMessage: HTMLElement | undefined; const config = { fontSize: 7, ...options }; const pending = kind === 'video' ? asciifyVideo(src, canvas, { fitTo: host, objectFit: 'contain', fps: 24, maxRenderDimension: 720, ...config }) : kind === 'gif' ? asciifyGif(src, canvas, config) : asciify(src, canvas, config); void pending.then(cleanup => { if (typeof cleanup !== 'function') return; if (disposed) cleanup(); else stop = cleanup; }).catch(() => { if (disposed) return; errorMessage = document.createElement('p'); errorMessage.setAttribute('role', 'status'); errorMessage.textContent = 'The artwork could not be loaded.'; host.append(errorMessage); }); return () => { disposed = true; stop?.(); stop = undefined; canvas.remove(); errorMessage?.remove(); }; }