css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="ms-Grid" dir="ltr">
  <h1 class="ms-font-su">Render Office Fabric UI Icons into Canvas</h1>
  <p>This is a simple tool to render an icon from the <a class="ms-fontColor-blueLight" href="https://developer.microsoft.com/en-us/fabric#/styles/icons">Office Fabric UI icon font</a> into an HTML <code>&lt;canvas&gt;</code> with a background color. Right-click and save the image to use it.</p>
  <div class="ms-Grid-row">
    <div class="ms-Grid-col ms-sm12 ms-lg6">
      <h2 class="ms-font-xxl">Icon/Canvas Specifications</h2>
      <form id="form">
        <div class="ms-Grid">
          <div class="ms-Grid-row">
            <div class="ms-Grid-col ms-md6">
              <label for="font-class">Icon name</label>
              <input type="text" name="fontClass" id="font-class" placeholder="e.g. ms-Icon ms-Icon-Warning" value="Contact">
            </div>
            <div class="ms-Grid-col ms-md6">
              <label for="font-size">Font size (px)</label>
              <input type="number" step="1" min="1" name="fontSize" id="font-size" placeholder="e.g. 60" value="56">
            </div>
          </div>
          <div class="ms-Grid-row">
            <div class="ms-Grid-col ms-md6">
              <label for="image-width">Image width (px)</label>
              <input type="number" step="1" min="0" name="imageWidth" id="image-width" placeholder="e.g. 80" value="92">
            </div>
            <div class="ms-Grid-col ms-md6">
              <label for="image-height">Image height (px)</label>
              <input type="number" step="1" min="0" name="imageHeight" id="image-height" placeholder="e.g. 80" value="92">
            </div>
          </div>
          <div class="ms-Grid-row">
            <div class="ms-Grid-col ms-md6">
              <label for="left-offset">Left offset</label>
              <input type="number" step="1" name="leftOffset" id="left-offset" placeholder="e.g. 40" value="46">
            </div>
            <div class="ms-Grid-col ms-md6">
              <label for="top-offset">Top offset</label>
              <input type="number" step="1" name="topOffset" id="top-offset" placeholder="e.g. 40" value="46">
            </div>
          </div>
          <div class="ms-Grid-row">
            <div class="ms-Grid-col ms-md6">
              <label for="bg-color">Background color</label>
              <input type="text" name="bgColor" id="bg-color" placeholder="e.g. #777777" value=#777777>
            </div>
            <div class="ms-Grid-col ms-md6">
              <label for="icon-color">Icon color</label>
              <input type="text" name="iconColor" id="icon-color" placeholder="e.g. #FFFFFF" value=#FFFFFF>
            </div>
          </div>
          <div class="ms-Grid-row">
            <div class="ms-Grid-col ms-sm12">
              <label><input type="checkbox" checked name="shape" id="shape"> Use a circle as the background fill</label>
            </div>
          </div>
        </div>

        <input type="submit" class="ms-button ms-bgColor-themeDark ms-bgColor-themeDarker--hover ms-fontColor-white" value="Render Font Icon">
        
        <p>If the icon does not render immediately, wait a few seconds and press the <b>Render</b> button again; the webfont may still be loading.</p>
        
      </form>
    </div>
    <div class="ms-Grid-col ms-sm12 ms-lg6">
      <h2 class="ms-font-xxl">Result</h2>
      
      <div class="canvas-container">
        <canvas id="canvas" width="92" height="92"></canvas>
      </div>
      
      <p><a id="download-link" class="ms-button ms-bgColor-themeDark ms-bgColor-themeDarker--hover ms-fontColor-white" target="_blank"><i class="ms-Icon ms-Icon--Download"></i> Download the image</a></p>
      <label for="dataURL">Data URL</label>
      <input id="dataURL" type="text">
    </div>
  </div>
</div>
            
          
!
            
              html {
  box-sizing: border-box;
}
*, *:before, *:after {
  box-sizing: inherit;
}
body {
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;
}
.ms-Grid {
  margin: 0 auto;
  padding: 0 16px;
  max-width: 1280px;
}
.ms-Grid-row {
  margin-left: -16px;
  margin-right: -16px;
}
.ms-Grid-col {
  padding: 0 16px;
}
label {
  display: block;
  margin-bottom: 0.5em;
}
input {
  border: none;
  display: block;
  margin-bottom: 2em;
  padding: 5px;
  width: 100%;
}
input[type="checkbox"] {
  display: inline-block;
  padding: 0;
  width: auto;
}
input[type="button"],
input[type="submit"],
.ms-button {
  cursor: pointer;
  display: inline-block;
  padding: 0.75em 2em;
  text-decoration: none;
  width: auto;
  
  .ms-Icon {
    transform: translateY(2px);
  }
}
.canvas-container {
  background-color: white;
  display: inline-block;
  margin-bottom: 1em;
  padding: 10px;
  width: auto;
}
#canvas {
  color: black;
  font-family: FabricMDL2Icons;
}
            
          
!
            
              const form = document.getElementById("form");
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const download = document.getElementById("download-link");
const dataURL = document.getElementById("dataURL");

function getFontIconCharacter(className, pseudo = ':before') {
  var testI = document.createElement('i');
  var char;

  testI.className = `ms-Icon ms-Icon--${className}`;
  document.body.appendChild(testI);

  char = window.getComputedStyle(testI, pseudo).getPropertyValue('content').replace(/'|"/g, '');

  testI.remove();
  return char;
}

function drawCircle() {
  var centerX = canvas.width / 2;
  var centerY = canvas.height / 2;
  var radius = canvas.width / 2;
  context.beginPath();
  context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  context.fillStyle = document.getElementById("bg-color").value || "#777777";
  context.fill();
}

function drawRect() {
  context.fillStyle = document.getElementById("bg-color").value || "#777777";
  context.fillRect(0, 0, canvas.width, canvas.height);
}

function drawIcon() {
  canvas.width = parseInt(document.getElementById("image-width").value, 10) || 92;
  canvas.height = parseInt(document.getElementById("image-height").value, 10) || 92;
  context.clearRect(0, 0, canvas.width, canvas.height);
  if (document.getElementById("shape").checked) {
    drawCircle();
  } else {
    drawRect();
  }  
  context.fillStyle = document.getElementById("icon-color").value || "#FFFFFF";
  let fontClass = document.getElementById("font-class").value,
      fontSize = document.getElementById("font-size").value || 280,
      topOffset = document.getElementById("top-offset").value || 210,
      leftOffset = document.getElementById("left-offset").value || 210;
  context.font = fontSize + "px FabricMDL2Icons";
  context.textAlign = "center";
  context.textBaseline = "middle";
  context.fillText(getFontIconCharacter(fontClass), parseInt(leftOffset, 10), parseInt(topOffset, 10));
  dataURL.value = canvas.toDataURL();
}

window.addEventListener('load', function() {
  drawIcon();
});

document.addEventListener('DOMContentLoaded', function() {
  context.font = "10px FabricMDL2Icons";
  context.fillText("...", 0, 0);
});

form.addEventListener("submit", function(event) {
  event.preventDefault();
  drawIcon();
});

download.addEventListener("click", function(event) {
  if (typeof this.download !== "undefined") {
    this.href = canvas.toDataURL();
    this.download = `${document.getElementById("font-class").value}.png`;
  } else {
    event.preventDefault();
    alert("Your browser does not support downloading a canvas image. Please right-click on the image to save it.");
  }
});

dataURL.addEventListener("focus", function(event) {
  dataURL.select();
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console