collection share using screenshot pt1
This commit is contained in:
1 parent
3aa59eec16
commit
28f304faa2
4 files changed
+90
-8
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import html2canvas from 'html2canvas';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ScreenshotService {
|
||||
|
||||
constructor() { }
|
||||
|
||||
capture(elementId: string): Promise<string> {
|
||||
const element = document.getElementById(elementId);
|
||||
if (!element) {
|
||||
throw new Error('Element not found');
|
||||
}
|
||||
|
||||
return this.waitForImagesToLoad(element).then(() => {
|
||||
return html2canvas(element, {
|
||||
useCORS: true,
|
||||
logging: true, // Enables logging for debugging
|
||||
allowTaint: false // Set to false to respect CORS, true can be used if images are all same-origin and CORS isn't needed
|
||||
}).then(canvas => {
|
||||
return canvas.toDataURL('image/png');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private waitForImagesToLoad(element: HTMLElement): Promise<void> {
|
||||
const images = element.getElementsByTagName('img');
|
||||
const loadPromises = Array.from(images).map(img => {
|
||||
if (img.complete && img.naturalHeight !== 0) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
img.onload = () => resolve();
|
||||
img.onerror = () => reject(new Error('Image failed to load.'));
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all(loadPromises).then(() => {});
|
||||
}
|
||||
|
||||
resolve () {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user