Skip to content

Commit 108689a

Browse files
authored
Merge pull request #1194 from data-for-change/dev
merge dev into master
2 parents b46aef2 + 6788b2c commit 108689a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/services/to-image.service.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as htmlToImage from 'html-to-image';
33
import html2canvas from 'html2canvas';
44

55
const DEFAULT_SCALE = 4;
6+
const CANVAS_SIZE = 3000;
67

78
const removeMapControllers = (el: Document) => {
89
const elementList = el.querySelectorAll(
@@ -11,14 +12,25 @@ const removeMapControllers = (el: Document) => {
1112
Array.from(elementList).forEach((el) => el.remove());
1213
};
1314

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+
1426
export const widgetToImageH2I = (fileName: string, widgetElement: HTMLElement) => {
1527
usingHtml2Image(fileName, widgetElement);
1628
};
1729

1830
// https://github.com/bubkoo/html-to-image
1931
const usingHtml2Image = (fileName: string, widgetElement: HTMLElement) => {
20-
const canvasWidth = 3300;
21-
const canvasHeight = 3300;
32+
const canvasWidth = CANVAS_SIZE;
33+
const canvasHeight = CANVAS_SIZE;
2234

2335
htmlToImage.toPng(widgetElement, {
2436
canvasWidth,
@@ -36,15 +48,15 @@ export const widgetToImageH2C = (fileName: string, widgetElement: HTMLElement, s
3648
// https://github.com/niklasvh/html2canvas
3749
// upgrade versions carefully
3850
const usingHtml2Canvas = (fileName: string, widgetElement: HTMLElement, scale: number) => {
39-
4051
html2canvas(widgetElement, {
4152
useCORS: true, // to allow loading maps
4253
imageTimeout: 3000,
4354
scale,
4455
onclone: (el) => removeMapControllers(el),
4556
})
4657
.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);
4860
})
4961
.then(function (blob: any) {
5062
saveAs(blob, `${fileName}.png`);

0 commit comments

Comments
 (0)