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

Version Path
coveragebook_components-0.16.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.15.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.14.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.13.1 app/assets/js/helpers/screenshot.js
coveragebook_components-0.13.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.12.2 app/assets/js/helpers/screenshot.js
coveragebook_components-0.12.1 app/assets/js/helpers/screenshot.js
coveragebook_components-0.12.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.11.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.10.1.beta.2 app/assets/js/helpers/screenshot.js
coveragebook_components-0.10.1.beta.1 app/assets/js/helpers/screenshot.js
coveragebook_components-0.10.1.beta.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.10.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.9.1 app/assets/js/helpers/screenshot.js
coveragebook_components-0.9.0 app/assets/js/helpers/screenshot.js
coveragebook_components-0.8.9 app/assets/js/helpers/screenshot.js
coveragebook_components-0.8.8 app/assets/js/helpers/screenshot.js
coveragebook_components-0.8.7 app/assets/js/helpers/screenshot.js
coveragebook_components-0.8.6 app/assets/js/helpers/screenshot.js
coveragebook_components-0.8.5 app/assets/js/helpers/screenshot.js