Element capture: any element to PNG, video or print

One call turns an ordinary element into pixels: captureElement(el), recordElement(el), exportVideo(el), printElement(el), and capturePage() for a whole ordinary page. The library puts a temporary drawable canvas in the element's place for one frame. I wanted to know what survives that, and the tests below say what does and what doesn't.

Support ticket #4821

Updated .

Type in the field, then capture. The element takes input the whole time, including while it's being recorded.

Output appears here.
import { captureElement, capturePage, recordElement, exportVideo, printElement } from '/lib/element-capture.js';

const png   = await captureElement(document.querySelector('#ticket'), { scale: 4 });
const page  = await capturePage({ full: true });          // no canvas in the page's markup
const print = await captureElement(el, { dpi: 300 });     // print-quality PNG that knows its size
const rec   = await recordElement(el, { fps: 30 });  const webm = await rec.stop();
const video = await exportVideo(el, { fps: 60 });         // WebCodecs: every animation frame, not a screen recording
await printElement(el);                                   // paginated, 200 dpi
Show the code an element to PNG, video or print
  1. An ordinary element. Nothing about it was written for canvas.
  2. One call per button. show() and img() only put the result on the page.
  3. Inside captureElement(): pick a strategy. An element already inside a drawable canvas is drawn in place. Any other element is moved into a temporary canvas for a frame.
  4. Inside: the temporary canvas. A drawable canvas takes the element's place and size, the element moves into it with moveBefore() (which keeps focus, iframes and animations), one drawElementImage() fills the canvas, and the element moves back.
  5. The whole library, with video recording, WebCodecs export and print.

A · Elements captureElement, recordElement, printElement

B · A whole ordinary page temporarily wrap body in a canvas

Each row loads a plain page (sticky header, fixed badge, content below the fold, a same-origin and a cross-origin iframe) in the frame below, scrolls it, focuses its input, and calls capturePage() with one route. A row passes when a correct image came back and scroll position, focus, caret and form value survived. Side effects are listed either way.

Twelve whole-page captures in the frame above. They scroll and focus that page, so they wait for you.

C · A folder of HTML files pickFolder, watchFolder, saveToFolder

Each .html file in a folder becomes a tile in the canvas below. Save a file in your editor and its tile redraws. "Save a PNG of every tile" writes the pictures back into the same folder. The private folder is storage that belongs to this site in your browser; it comes with three sample files and works where there is no folder picker.

No folder open.

Show the code clipboard, print margins and folders
  1. copyElement(el, { html: true }): one ClipboardItem with three types. The PNG is a promise, so the write starts inside the tap.
  2. printElement(): @page margin boxes for the header, footer and page numbers.
  3. pickFolder(), saveToFolder() and watchFolder().
  4. The tile canvas.
  5. One tile per file, redrawn when FileSystemObserver reports a change. Each file is shown in an <iframe sandbox="allow-same-origin">: its scripts don't run, and it stays same-origin so the canvas may draw it.

Findings