| 1 | var DOMURL = self.URL || self.webkitURL || self; |
| 2 | |
| 3 | function getsvg(){ |
| 4 | canvas = Blockly.mainWorkspace.svgBlockCanvas_.cloneNode(true); |
| 5 | if (canvas.children[0] === undefined) throw "Couldn't find Blockly canvas." |
| 6 | |
| 7 | canvas.removeAttribute("transform"); |
| 8 | |
| 9 | var css = '<defs><style type="text/css" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[' + Blockly.Css.content + ']]></style></defs>'; |
| 10 | var bbox = document.getElementsByClassName("blocklyBlockCanvas")[0].getBBox(); |
| 11 | var content = new XMLSerializer().serializeToString(canvas); |
| 12 | |
| 13 | xml = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' |
| 14 | + bbox.width + '" height="' + bbox.height + '" viewBox=" ' + bbox.x + ' ' + bbox.y + ' ' + bbox.width + ' ' + bbox.height + '">' + |
| 15 | css + '">' + content + '</svg>'; |
| 16 | |
| 17 | return new Blob([xml], { type: 'image/svg+xml;base64' }); |
| 18 | } |
| 19 | |
| 20 | |
| 21 | function getrawsvg(){ |
| 22 | canvas = Blockly.mainWorkspace.svgBlockCanvas_.cloneNode(true); |
| 23 | if (canvas.children[0] === undefined) throw "Couldn't find Blockly canvas." |
| 24 | |
| 25 | canvas.removeAttribute("transform"); |
| 26 | |
| 27 | var css = '<defs><style type="text/css" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[' + Blockly.Css.content + ']]></style></defs>'; |
| 28 | var bbox = document.getElementsByClassName("blocklyBlockCanvas")[0].getBBox(); |
| 29 | var content = new XMLSerializer().serializeToString(canvas); |
| 30 | |
| 31 | xml = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' |
| 32 | + bbox.width + '" height="' + bbox.height + '" viewBox=" ' + bbox.x + ' ' + bbox.y + ' ' + bbox.width + ' ' + bbox.height + '">' + |
| 33 | css + '">' + content + '</svg>'; |
| 34 | return xml; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | function download(url, filename){ |
| 39 | let element = document.createElement('a') |
| 40 | element.href = url |
| 41 | element.download = filename; |
| 42 | element.click(); |
| 43 | DOMURL.revokeObjectURL(element.href) |
| 44 | } |
| 45 | |
| 46 | function exportSVG() { |
| 47 | download(DOMURL.createObjectURL(getsvg()),'blocks.svg'); |
| 48 | } |
| 49 | |
| 50 | function exportPNG(){ |
| 51 | var img = new Image(); |
| 52 | img.onload = function() { |
| 53 | var canvas = document.createElement('canvas'); |
| 54 | canvas.width = 800; |
| 55 | canvas.height = 600; |
| 56 | canvas.getContext("2d").drawImage(img, 0, 0); |
| 57 | download(canvas.toDataURL("image/png"),'blocks.png'); |
| 58 | }; |
| 59 | img.src = DOMURL.createObjectURL(getsvg()); |
| 60 | } |