Guest User

Untitled

a guest
Nov 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. <g id="group" opacity="1" style="pointer-events:inherit">
  2. <rect x="69.7" y="409.9" class="" width="44.5" height="38.1" id="rectID" style="pointer-events:inherit" />
  3. <text transform="matrix(1 0 0 1 52.9853 441.265)" class="st11 st13" id="textID" y="-12" x="25" style="pointer-events:inherit">Some Text Some Text Some Text</text>
  4. </g>
  5.  
  6. <g id="group" opacity="1" style="pointer-events:inherit">
  7. <rect x="69.7" y="409.9" class="st0" width="44.5" height="38.1" id="rectID" style="pointer-events:inherit" />
  8. <symbol xmlns="http://www.w3.org/1999/xhtml" id="Symbol_54" viewBox="24 -17 23 6">
  9. <text xmlns="http://www.w3.org/2000/svg" transform="matrix(1 0 0 1 52.9853 441.265)" class="st11 st13" id="textID" y="-12" x="25" style="pointer-events:inherit">Some Text Some Text Some Text Some Text</text>
  10. </symbol>
  11. <use xmlns="http://www.w3.org/1999/xhtml" xlink:href="Symbol_54" x="69.7" y="409.9" width="44.5" height="38.1"></use>
  12. </g>
  13.  
  14. x = svg.getElementsByTagName('g');
  15. for (i = 0; i < x.length; i++) {
  16. SVGTextID = x[i].lastChild.getAttribute("id");
  17. SVGRectID = x[i].firstChild.getAttribute("id");
  18.  
  19. let theText = document.getElementById(SVGTextID);
  20. let theRect = document.getElementById(SVGRectID);
  21.  
  22. // create a new symbol element
  23. let sym = document.createElement('symbol');
  24. let symbolID ="Symbol_"+i;
  25. sym.setAttribute("id", symbolID);
  26. x[i].appendChild(sym);
  27.  
  28. // get the size of the bounding box for the text
  29. let bbText = theText.getBBox();
  30. // set the attribute viewBox for the symbol
  31. sym.setAttributeNS(null, "viewBox", `${bbText.x} ${bbText.y} ${bbText.width} ${bbText.height}`)
  32.  
  33. // clone the text and append it to the symbol
  34. let txt = theText.cloneNode(true);
  35. sym.appendChild(txt);
  36.  
  37. // remove the text
  38. theText.parentNode.removeChild(theText);
  39.  
  40. // create a use element
  41. let useElem = document.createElement('use');
  42. // the use element is using the symbol
  43. useElem.setAttribute('xlink:href', symbolID);
  44. // also is using the rect's attributes
  45. useElem.setAttribute('x', theRect.getAttribute("x"));
  46. useElem.setAttribute('y', theRect.getAttribute("y"));
  47. useElem.setAttribute('width', theRect.getAttribute("width"));
  48. useElem.setAttribute('height', theRect.getAttribute("height"));
  49.  
  50. x[i].appendChild(useElem);
  51. }
Add Comment
Please, Sign In to add comment