exportsvg.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. {
  2. const svgns = "http://www.w3.org/2000/svg";
  3. const textAttributes = new Set([
  4. 'color',
  5. 'dominant-baseline',
  6. 'font-family',
  7. 'font-size',
  8. 'font-size-adjust',
  9. 'font-stretch',
  10. 'font-style',
  11. 'font-variant',
  12. 'font-weight',
  13. 'direction',
  14. 'letter-spacing',
  15. 'text-decoration',
  16. 'text-anchor',
  17. 'text-decoration',
  18. 'text-rendering',
  19. 'unicode-bidi',
  20. 'word-spacing',
  21. 'writing-mode',
  22. 'user-select',
  23. ]);
  24. function copyTextStyles(styles, svgElement){
  25. for (const textProperty of textAttributes) {
  26. const value = styles.getPropertyValue(textProperty)
  27. if (value) {
  28. svgElement.setAttribute(textProperty, value)
  29. }
  30. }
  31. // tspan uses fill, CSS uses color
  32. svgElement.setAttribute('fill', styles.color)
  33. }
  34. function handleSVGElement(svg, element){
  35. const contentContainer = Document.createElementNS(svgns, 'g');
  36. contentContainer.innerHTML = element.innerHTML;
  37. contentContainer.dataset.viewBox = element.getAttribute('viewBox');
  38. contentContainer.dataset.width = element.getAttribute('width');
  39. contentContainer.dataset.height = element.getAttribute('height');
  40. let viewBoxTransformMatrix = element.getScreenCTM();
  41. contentContainer.transform.baseVal.appendItem( contentContainer.transform.baseVal.createSVGTransformFromMatrix(viewBoxTransformMatrix));
  42. svg.appendChild(contentContainer);
  43. }
  44. function createSVG(svg, node){
  45. let rect;
  46. if(Node.TEXT_NODE === node.nodeType) {
  47. let str = node.textContent;
  48. if(str.match(/^\s+$/)) return;
  49. rect = node.parentElement.getBoundingClientRect();
  50. const text = document.createElementNS(svgns, 'text');
  51. text.innerHTML = str;
  52. text.setAttribute("x", rect.left);
  53. text.setAttribute("y", rect.top);
  54. svg.appendChild(text);
  55. return;
  56. }
  57. if(Node.ELEMENT_NODE!=node.nodeType) return;
  58. let element = node;
  59. if("SCRIPT" == element.tagName ||
  60. "STYLE" == element.tagName) return;
  61. rect = element.getBoundingClientRect();
  62. if("SVG" == element.tagName) {
  63. handleSVGElement(svg,element);
  64. /*
  65. let clone = element.cloneNode(true);
  66. clone.setAttribute("x", rect.left);
  67. clone.setAttribute("y", rect.top);
  68. clone.setAttribute("width", rect.width);
  69. clone.setAttribute("height", rect.height);
  70. svg.appendChild(clone);
  71. */
  72. return;
  73. }
  74. //const styles = window.getComputedStyle(element);
  75. let children = element.childNodes;
  76. let nChildren = children.length;
  77. // Get the HTML element's style
  78. //var style = window.getComputedStyle(element);
  79. //svg.setAttribute("style", style);
  80. // Iterate through the HTML element's children
  81. for (let i = 0; i < nChildren; i++) {
  82. createSVG(svg, children[i]);
  83. }
  84. }
  85. // Get the HTML element to be converted
  86. let element = document.body;
  87. // Append the SVG element to the HTML document
  88. let svg = document.createElementNS(svgns,'svg');
  89. let rect = element.getBoundingClientRect();
  90. // svg.setAttribute("x", rect.left);
  91. // svg.setAttribute("y", rect.top);
  92. svg.setAttribute("width", rect.width);
  93. svg.setAttribute("height", rect.height);
  94. createSVG(svg, element);
  95. //document.body.appendChild(svg);
  96. // Create a Blob of the string
  97. let svgS = new XMLSerializer().serializeToString(svg);
  98. const blob = new Blob([svgS], { type: 'text/plain' });
  99. // Create a link element
  100. const link = document.createElement('a');
  101. // Create the url
  102. link.href = window.URL.createObjectURL(blob);
  103. // Set the file name
  104. link.download = 'uweb.svg';
  105. // Append the link to the DOM
  106. document.body.appendChild(link);
  107. // Click the link
  108. link.click();
  109. // Remove the link from the DOM
  110. document.body.removeChild(link);
  111. }