Last active
February 2, 2024 05:25
-
-
Save hungtcs/4966a39f60f49fd27375e6895c26b0c6 to your computer and use it in GitHub Desktop.
Capture elements with html2canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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