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.
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
- An ordinary element. Nothing about it was written for canvas.
- One call per button.
show()andimg()only put the result on the page. - 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. - 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), onedrawElementImage()fills the canvas, and the element moves back. - 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.
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
copyElement(el, { html: true }): oneClipboardItemwith three types. The PNG is a promise, so the write starts inside the tap.printElement():@pagemargin boxes for the header, footer and page numbers.pickFolder(),saveToFolder()andwatchFolder().- The tile canvas.
- One tile per file, redrawn when
FileSystemObserverreports 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
- The body can be wrapped in a canvas for one frame. The snapshot is ready in the first rendering update after the canvas goes in: the
paintevent fires in that same frame, so the only thing to wait for is one frame. - The shadow-root route doesn't move any element. A shadow root on
<body>holdingcanvas > div[drawable] > slotreprojects every body child without touching the light DOM: no mutation records for the page, selectors likebody > mainkeep matching, focus, caret, iframes and scroll position stay put. Its cost is permanent: a shadow root can't be removed, so body keeps a bare<slot>afterwards and the page can never attach its own. - The move route needs
moveBefore(). With it, moving body's children into a canvas and back keeps focus, caret, form state and iframes alive. With plainappendthe iframes reload and focus is lost, and taking an iframe out of a canvas in the frame it was drawn crashed the tab. - CSS animations restart when an element becomes a canvas descendant, on every route. The library reads
currentTimebefore and seeks the restarted animations back. - Fixed and sticky need a stand-in viewport. A drawable element is a containing block, so fixed elements attach to it. Wrapping the content in a viewport-sized frame with an inner scroller set to the page's scroll offset gives a faithful viewport shot. In a full-page shot fixed elements land at the end of the document and sticky ones at their static position.
- Cross-origin iframes come out blank, by design, and so do cross-origin images without CORS. Same-origin iframes paint.
- Video doesn't have to be a screen recording.
exportVideo()pauses the element's animations, steps them one frame at a time and encodes each frame with WebCodecs, so a 60 fps file has all 60 frames a second in the right place on any machine. The WebM container is written by a hundred lines of script. - One clipboard item can hold the picture and the content.
copyElement(el, { html: true })writesimage/png,text/htmlandtext/plaintogether, andnavigator.clipboard.read()returns all three. The markup has to be read before the capture starts, because the capture adds temporary attributes to the element. @pagemargin boxes work for a printed capture. Chromium 157 keeps all three boxes (@top-center,@bottom-left,@bottom-rightwithcounter(page)andcounter(pages)) on the print document.- A folder can be the source and the destination.
FileSystemObserverreported a changed file 2 to 5 ms after the write in the test browser, once per write step. A sandboxedsrcdociframe is drawn only withallow-same-origin; with a baresandboxits origin is opaque and the canvas leaves it out. - Open: the folder picker and
FileSystemObserveron Android, and HEVC recording, need a real device. The test browser records VP8, VP9, AV1 and H.264 and has no HEVC encoder. - Output is rasterised again at the new size.
drawElementImage(el, 0, 0, w × 4, h × 4)replays the element's paint record at the new size, so a 4× PNG has sharp text and hairlines, and a printed page does too.