-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
48 lines (42 loc) · 1.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import html2canvas from 'html2canvas';
import { changeDpiDataUrl, changeDpiBlob } from "changedpi"
export interface Options {
filter?: (node: Node) => boolean
bgcolor?: string
width?: number
height?: number
style?: {}
quality?: number
imagePlaceholder?: string
cacheBust?: boolean
}
const DPI = 72 * 2
export const toPng = (el: Node, options?: Options): Promise<string> => {
return html2canvas(el as any, options).then((canvas) =>{
return changeDpiDataUrl(canvas.toDataURL("image/png", 0.92), DPI); ;
});
}
export const toJpeg = (el: Node, options?: Options): Promise<string> => {
return html2canvas(el as any, options).then((canvas) =>{
return changeDpiDataUrl(canvas.toDataURL("image/jpeg", 0.92), DPI); ;
});
}
export const toBlob = (el: Node, options?: Options): Promise<Blob> => {
return new Promise((resolve, reject) => {
html2canvas(el as any, options).then((canvas) =>{
return canvas.toBlob(function(blob) {
changeDpiBlob(blob, DPI).then(function(blob: any){
resolve(blob);
})
},'image/jpeg', 0.92);
})
});
}
if (typeof window !== "undefined") {
// @ts-ignore
window.DomToImage = {
toPng,
toJpeg,
toBlob
}
}