Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
  <h1>SVG Iconifier</h1>
  <p>Transforms an SVG into a small and simple 24x24 SVG that has fill="currentColor" and only one single path element.</p>
</header>
<div class="outer">
  <div>
    Source SVG<br />
    <textarea rows="10" cols="50" placeholder="paste SVG source here ..."></textarea><br />
    <!--button>convert to simple icon</button-->

  </div>

  <div>Preview<br />
    <div id="resultContainer"></div>
  </div>
  <div>Result SVG<br /><textarea id="resultCode" rows="10" cols="50" readonly></textarea><br /><br />Result path only<br /><textarea id="resultPath" rows="5" cols="50" readonly></textarea></div>
</div>
              
            
!

CSS

              
                body {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: Roboto, sans-serif;
  color: #369;
}

header {
  text-align: center;
}

.outer {
  display: flex;
  gap: 2rem;
}

#resultContainer {
  width: 240px;
  height: 240px;
  border: #ccc solid 1px;

  --color: #eee;
  background-image: linear-gradient(45deg, var(--color) 25%, transparent 25%),
    linear-gradient(-45deg, var(--color) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--color) 75%),
    linear-gradient(-45deg, transparent 75%, var(--color) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function () {
  const textarea = document.querySelector("textarea");
  const button = document.querySelector("button");
  const resultContainer = document.querySelector("#resultContainer");
  const resultCode = document.querySelector("#resultCode");
  const resultPath = document.querySelector("#resultPath");

  function update() {
    var mySvgString = textarea.value;
    var div = document.createElement("div");
    div.innerHTML = mySvgString;
    var mySvg = div.querySelector("svg");
    var viewBox = mySvg.getAttribute("viewBox");
    var [x, y, w, h] = viewBox
      ? viewBox.split(/\s+/).map((i) => parseFloat(i))
      : [];
    var [width, height] = [
      w || mySvg.getAttribute("width"),
      h || mySvg.getAttribute("height")
    ];
    mySvg
      .querySelectorAll("line, polyline, polygon, rect, ellipse, circle, glyph")
      .forEach((shape) => SVGPathCommander.shapeToPath(shape, true));

    mySvg.querySelectorAll("path").forEach((shape) => {
      var pathArray = SVGPathCommander.parsePathString(shape.getAttribute("d"));
      if (pathArray[0][0] === "m") pathArray[0][0] = "M";
      //pathArray.map(item => { if (item[0] === 'm') item[0] = 'M'; return item; });
      //console.log(pathArray);
      shape.setAttribute("d", SVGPathCommander.pathToString(pathArray));
    });
    //console.log(mySvg.outerHTML);
    var myPath = [...mySvg.querySelectorAll("path")]
      .map((shape) => shape.getAttribute("d"))
      .join(" Z ");
    var scale = 24 / Math.max(width, height);
    var myResult = new SVGPathCommander(myPath)
      .transform({
        translate: [(24 - width * scale) / 2, (24 - height * scale) / 2], // X axis translation
        //rotate: 15, // Z axis rotation
        scale: scale, // uniform scale on X, Y, Z axis
        //skew: 15, // skew 15deg on the X axis
        origin: [0, 0] // if not specified, it will calculate a bounding box to determine a proper `transform-origin`
      })
      .toString();
    //console.log(myResult, { x, y, w, h, width, height });

    while (resultContainer.firstChild)
      resultContainer.removeChild(resultContainer.firstChild);

    const newSvg = document.createElementNS(
      "http://www.w3.org/2000/svg",
      "svg"
    );
    newSvg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
    newSvg.setAttribute("viewBox", "0 0 24 24");
    const newSvgPath = document.createElementNS(
      "http://www.w3.org/2000/svg",
      "path"
    );
    newSvgPath.setAttribute("fill", "currentColor");
    newSvgPath.setAttribute("d", myResult);
    newSvg.appendChild(newSvgPath);
    resultContainer.appendChild(newSvg);
    resultCode.value = newSvg.outerHTML;
    resultPath.value = myResult;
  }

  textarea.addEventListener("input", update);
  button?.addEventListener("click", update);
});

              
            
!
999px

Console