Frameworks
The engine only requires the browser Canvas API — no framework-specific internals. The examples use the small application helper below to clean up on source changes and unmount, including when loading resolves late. Vue examples use Vue 3; Svelte examples use Svelte 5.
Shared lifecycle helper
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();
};
}
'use client';
import { useEffect, useRef } from 'react';
import { mountAscii } from './mount-ascii';
export function AsciiMedia({ src }: { src: string }) {
const host = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!host.current) return;
return mountAscii(host.current, src, 'image');
}, [src]);
return <div ref={host} role="img" aria-label="ASCII artwork"
style={{ position: 'relative', minHeight: 300 }} />;
}