Created
December 6, 2019 14:24
-
-
Save bumbeishvili/90a60789960a7c7b744b5869f4160731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function downloadImage(isSvg) { | |
| // Retrieve svg node | |
| const svgNode = attrs.svg.node(); | |
| if (isSvg) { | |
| let source = serializeString(svgNode); | |
| //add xml declaration | |
| source = '<?xml version="1.0" standalone="no"?>\r\n' + source; | |
| //convert svg source to URI data scheme. | |
| var url = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source); | |
| saveAs(url, "graph.svg") | |
| } else { | |
| // Get image quality index (basically, index you can zoom in) | |
| const quality = attrs.imageDownloadScale; | |
| // Create image | |
| const image = new Image; | |
| image.onload = () => { | |
| // Create image canvas | |
| const canvas = document.createElement('canvas') | |
| // Set width and height based on SVG node | |
| const rect = svgNode.getBoundingClientRect(); | |
| canvas.width = rect.width * quality; | |
| canvas.height = rect.height * quality; | |
| // Draw background | |
| const context = canvas.getContext('2d') | |
| context.fillStyle = '#FAFAFA'; | |
| context.fillRect(0, 0, rect.width * quality, rect.height * quality); | |
| context.drawImage(image, 0, 0, rect.width * quality, rect.height * quality); | |
| // Set some image metadata | |
| let dt = canvas.toDataURL('image/png'); | |
| dt = dt.replace(/^data:image\/[^;]*/, 'data:application/octet-stream'); | |
| dt = dt.replace(/^data:application\/octet-stream/, 'data:application/octet-stream;headers=Content-Disposition%3A%20attachment%3B%20filename=download.png'); | |
| // Invoke saving function | |
| saveAs(dt, 'graph.png') | |
| }; | |
| const blob = new Blob([serializeString(svgNode)], { type: "image/svg+xml" }); | |
| // Set image source | |
| image.src = URL.createObjectURL(blob); | |
| } | |
| // This function invokes save window | |
| function saveAs(uri, filename) { | |
| // create link | |
| var link = document.createElement('a'); | |
| if (typeof link.download === 'string') { | |
| document.body.appendChild(link); // Firefox requires the link to be in the body | |
| link.download = filename; | |
| link.href = uri; | |
| link.click(); | |
| document.body.removeChild(link); // remove the link when done | |
| } else { | |
| location.replace(uri); | |
| } | |
| } | |
| // This function serializes SVG and sets all necessary attributes | |
| function serializeString(svg) { | |
| const xmlns = "http://www.w3.org/2000/xmlns/"; | |
| const xlinkns = "http://www.w3.org/1999/xlink"; | |
| const svgns = "http://www.w3.org/2000/svg"; | |
| svg = svg.cloneNode(true); | |
| const fragment = window.location.href + "#"; | |
| const walker: any = document.createTreeWalker(svg, NodeFilter.SHOW_ELEMENT, null, false); | |
| while (walker.nextNode()) { | |
| for (const attr of walker.currentNode.attributes) { | |
| if (attr.value.includes(fragment)) { | |
| attr.value = attr.value.replace(fragment, "#"); | |
| } | |
| } | |
| } | |
| svg.setAttributeNS(xmlns, "xmlns", svgns); | |
| svg.setAttributeNS(xmlns, "xmlns:xlink", xlinkns); | |
| const serializer = new XMLSerializer; | |
| const string = serializer.serializeToString(svg); | |
| return string; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment