-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportPNG.js
27 lines (25 loc) · 1017 Bytes
/
exportPNG.js
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
document.getElementById('exportPNGButton').addEventListener('click', function() {
const svgElement = document.getElementById('svgOverlay').querySelector('svg');
const serializer = new XMLSerializer();
const svgStr = serializer.serializeToString(svgElement);
imgWidth = 4096;
imgHeight = 4096;
const canvas = document.createElement('canvas');
canvas.width = imgWidth;
canvas.height = imgHeight;
const ctx = canvas.getContext('2d');
const svgBlob = new Blob([svgStr], {type: "image/svg+xml;charset=utf-8"});
const DOMURL = window.URL || window.webkitURL || window;
const url = DOMURL.createObjectURL(svgBlob);
const svgImg = new Image();
svgImg.onload = function () {
ctx.drawImage(svgImg, 0, 0, imgWidth, imgHeight);
const dataUrl = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.download = 'Galactic Frontier.png';
link.href = dataUrl;
link.click();
DOMURL.revokeObjectURL(url);
};
svgImg.src = url;
});