@@ -3,6 +3,7 @@ import * as htmlToImage from 'html-to-image';
3
3
import html2canvas from 'html2canvas' ;
4
4
5
5
const DEFAULT_SCALE = 4 ;
6
+ const CANVAS_SIZE = 3000 ;
6
7
7
8
const removeMapControllers = ( el : Document ) => {
8
9
const elementList = el . querySelectorAll (
@@ -11,14 +12,25 @@ const removeMapControllers = (el: Document) => {
11
12
Array . from ( elementList ) . forEach ( ( el ) => el . remove ( ) ) ;
12
13
} ;
13
14
15
+ const resizeCanvas = ( originalCanvas : HTMLCanvasElement , targetWidth : number , targetHeight : number ) : HTMLCanvasElement => {
16
+ const resizedCanvas = document . createElement ( 'canvas' ) ;
17
+ resizedCanvas . width = targetWidth ;
18
+ resizedCanvas . height = targetHeight ;
19
+ const ctx = resizedCanvas . getContext ( '2d' ) ;
20
+ if ( ctx ) {
21
+ ctx . drawImage ( originalCanvas , 0 , 0 , targetWidth , targetHeight ) ;
22
+ }
23
+ return resizedCanvas ;
24
+ } ;
25
+
14
26
export const widgetToImageH2I = ( fileName : string , widgetElement : HTMLElement ) => {
15
27
usingHtml2Image ( fileName , widgetElement ) ;
16
28
} ;
17
29
18
30
// https://github.com/bubkoo/html-to-image
19
31
const usingHtml2Image = ( fileName : string , widgetElement : HTMLElement ) => {
20
- const canvasWidth = 3300 ;
21
- const canvasHeight = 3300 ;
32
+ const canvasWidth = CANVAS_SIZE ;
33
+ const canvasHeight = CANVAS_SIZE ;
22
34
23
35
htmlToImage . toPng ( widgetElement , {
24
36
canvasWidth,
@@ -36,15 +48,15 @@ export const widgetToImageH2C = (fileName: string, widgetElement: HTMLElement, s
36
48
// https://github.com/niklasvh/html2canvas
37
49
// upgrade versions carefully
38
50
const usingHtml2Canvas = ( fileName : string , widgetElement : HTMLElement , scale : number ) => {
39
-
40
51
html2canvas ( widgetElement , {
41
52
useCORS : true , // to allow loading maps
42
53
imageTimeout : 3000 ,
43
54
scale,
44
55
onclone : ( el ) => removeMapControllers ( el ) ,
45
56
} )
46
57
. then ( function ( canvas ) {
47
- return canvas . toDataURL ( 'image/png' , 1.0 ) ;
58
+ const resizedCanvas = resizeCanvas ( canvas , CANVAS_SIZE , CANVAS_SIZE ) ;
59
+ return resizedCanvas . toDataURL ( 'image/png' , 1.0 ) ;
48
60
} )
49
61
. then ( function ( blob : any ) {
50
62
saveAs ( blob , `${ fileName } .png` ) ;
0 commit comments