Skip to content

Instantly share code, notes, and snippets.

@hungtcs
Last active February 2, 2024 05:25
Show Gist options
  • Save hungtcs/4966a39f60f49fd27375e6895c26b0c6 to your computer and use it in GitHub Desktop.
Save hungtcs/4966a39f60f49fd27375e6895c26b0c6 to your computer and use it in GitHub Desktop.
Capture elements with html2canvas
async function capture(element, options = {}) {
const module = await import('https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.esm.js')
const html2canvas = module.default;
return await html2canvas(element, options);
}
async function canvasToBlob(canvas, type, quality) {
return await new Promise((resolve) => {
canvas.toBlob(blob => resolve(blob), type, quality);
});
}
function canvasToDataURL(canvas, type, quality) {
return canvas.toDataURL(type, quality);
}
async function captureBlob(element, options = {}) {
const canvas = await capture(element, options);
return canvasToBlob(canvas, options.type, options.quality);
}
async function captureDataURL(element, options = {}) {
const canvas = await capture(element, options);
return canvasToDataURL(canvas);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment