Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const imageDoc = parser.parseFromString(imageSvgContent, "image/svg+xml");
- const imageRoot = imageDoc.documentElement;
- let scaleX = 1;
- let scaleY = 1;
- let origWidth = 0;
- let origHeight = 0;
- let viewBoxX = 0;
- let viewBoxY = 0;
- if (img.width && img.height) {
- const origWidthAttr = imageRoot.getAttribute("width");
- const origHeightAttr = imageRoot.getAttribute("height");
- if (origWidthAttr && origHeightAttr) {
- origWidth = parseFloat(origWidthAttr);
- origHeight = parseFloat(origHeightAttr);
- }
- if (!origWidth || !origHeight) {
- const viewBox = imageRoot.getAttribute("viewBox");
- if (viewBox) {
- const parts = viewBox.split(/\s+|,/);
- if (parts.length === 4) {
- viewBoxX = parseFloat(parts[0]);
- viewBoxY = parseFloat(parts[1]);
- origWidth = parseFloat(parts[2]);
- origHeight = parseFloat(parts[3]);
- }
- }
- }
- if (origWidth && origHeight) {
- scaleX = img.width / origWidth;
- scaleY = img.height / origHeight;
- }
- const translateX = (img.x ?? 0) - viewBoxX * scaleX;
- const translateY = (img.y ?? 0) - viewBoxY * scaleY;
- console.log(viewBoxX, viewBoxY)
- const g = doc.createElementNS("http://www.w3.org/2000/svg", "g");
- g.setAttribute(
- "transform",
- `translate(${translateX}, ${translateY}) scale(${scaleX}, ${scaleY})`
- );
- Array.from(imageRoot.childNodes).forEach((child) => {
- g.appendChild(doc.importNode(child, true));
- });
- additionalGroup.appendChild(g);
- }
- doc.documentElement.appendChild(additionalGroup);
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement