Sha256: 45843c81de39c76fa533c5431d07a7e4631c6ffd1cdd66e9a4057cc7b81583d4
Contents?: true
Size: 1.15 KB
Versions: 51
Compression:
Stored size: 1.15 KB
Contents
import html2canvas from "html2canvas"; import { stripExtension } from "./path"; import { isObject } from "./lang"; import { blobToFile } from "./file"; const screenshotDefaultOptions = { useCORS: true, logging: false, }; function generateScreenshotFilename(name = null, ext = null) { name = [stripExtension(name || "screenshot"), new Date().getTime()].join("-"); return ext ? `${name}.${ext}` : name; } // Attempts to convert a DOM element into a static image. // Returns a File object. async function captureElementScreenshot(el, filename, opts = {}) { if (isObject(filename)) { filename = null; opts = filename; } const screenshotFilename = generateScreenshotFilename(filename, "jpg"); const screenshotOptions = { ...screenshotDefaultOptions, ...opts, }; const canvas = await html2canvas(el, screenshotOptions); const imageBlob = await new Promise((resolve, reject) => { canvas.toBlob((result) => { return result ? resolve(result) : reject(new Error("Screenshot could not be created")); }); }); return blobToFile(imageBlob, screenshotFilename, "image/jpg"); } export { captureElementScreenshot };
Version data entries
51 entries across 51 versions & 1 rubygems