(
element: HTMLElement,
options: ScreenshotOptions = {}
)
| 45 | * Take a screenshot of a DOM element |
| 46 | */ |
| 47 | export async function screenshotElement( |
| 48 | element: HTMLElement, |
| 49 | options: ScreenshotOptions = {} |
| 50 | ): Promise<Blob> { |
| 51 | const { scale = 2, backgroundColor = '#ffffff' } = options |
| 52 | |
| 53 | const canvas = await html2canvas(element, { |
| 54 | scale, |
| 55 | backgroundColor, |
| 56 | useCORS: true, |
| 57 | logging: false, |
| 58 | onclone: (_doc, clonedElement) => { |
| 59 | // Sanitize colors in the cloned element before rendering |
| 60 | sanitizeColors(clonedElement) |
| 61 | } |
| 62 | }) |
| 63 | |
| 64 | return new Promise((resolve, reject) => { |
| 65 | canvas.toBlob( |
| 66 | (blob) => (blob ? resolve(blob) : reject(new Error('Failed to create blob'))), |
| 67 | 'image/png' |
| 68 | ) |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Download a Blob as a file |
no test coverage detected