33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
class ExportImage {
|
|
/**
|
|
* Exports the target element to a svg and downloads it.
|
|
* @param {Element} svgEl A DOM object of the SVG.
|
|
* @param {string} name The target file name
|
|
*/
|
|
exportToSvg(svgEl, name) {
|
|
svgEl.setAttribute("xmlns", "http://www.w3.org/2000/svg");
|
|
var svgData = svgEl.outerHTML;
|
|
var preface = '<?xml version="1.0" standalone="no"?>\r\n';
|
|
var svgBlob = new Blob([preface, svgData], { type: "image/svg+xml;charset=utf-8" });
|
|
var svgUrl = URL.createObjectURL(svgBlob);
|
|
var downloadLink = document.createElement("a");
|
|
downloadLink.href = svgUrl;
|
|
downloadLink.download = name;
|
|
document.body.appendChild(downloadLink);
|
|
downloadLink.click();
|
|
document.body.removeChild(downloadLink);
|
|
}
|
|
|
|
/**
|
|
* Exports the target element to a png image and downloads it.
|
|
* @param {Element} targetElement A simple DOM object in the form of a canvas
|
|
* @param {string} name The target file name
|
|
*/
|
|
exportToPng(targetElement, name) {
|
|
var link = document.getElementById('link');
|
|
link.setAttribute('download', name);
|
|
link.setAttribute('href', targetElement.toDataURL("image/png").replace("image/png", "image/octet-stream"));
|
|
link.click();
|
|
}
|
|
}
|