Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <g id="group" opacity="1" style="pointer-events:inherit">
- <rect x="69.7" y="409.9" class="" width="44.5" height="38.1" id="rectID" style="pointer-events:inherit" />
- <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>
- </g>
- <g id="group" opacity="1" style="pointer-events:inherit">
- <rect x="69.7" y="409.9" class="st0" width="44.5" height="38.1" id="rectID" style="pointer-events:inherit" />
- <symbol xmlns="http://www.w3.org/1999/xhtml" id="Symbol_54" viewBox="24 -17 23 6">
- <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>
- </symbol>
- <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>
- </g>
- x = svg.getElementsByTagName('g');
- for (i = 0; i < x.length; i++) {
- SVGTextID = x[i].lastChild.getAttribute("id");
- SVGRectID = x[i].firstChild.getAttribute("id");
- let theText = document.getElementById(SVGTextID);
- let theRect = document.getElementById(SVGRectID);
- // create a new symbol element
- let sym = document.createElement('symbol');
- let symbolID ="Symbol_"+i;
- sym.setAttribute("id", symbolID);
- x[i].appendChild(sym);
- // get the size of the bounding box for the text
- let bbText = theText.getBBox();
- // set the attribute viewBox for the symbol
- sym.setAttributeNS(null, "viewBox", `${bbText.x} ${bbText.y} ${bbText.width} ${bbText.height}`)
- // clone the text and append it to the symbol
- let txt = theText.cloneNode(true);
- sym.appendChild(txt);
- // remove the text
- theText.parentNode.removeChild(theText);
- // create a use element
- let useElem = document.createElement('use');
- // the use element is using the symbol
- useElem.setAttribute('xlink:href', symbolID);
- // also is using the rect's attributes
- useElem.setAttribute('x', theRect.getAttribute("x"));
- useElem.setAttribute('y', theRect.getAttribute("y"));
- useElem.setAttribute('width', theRect.getAttribute("width"));
- useElem.setAttribute('height', theRect.getAttribute("height"));
- x[i].appendChild(useElem);
- }
Add Comment
Please, Sign In to add comment