Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

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.

HTML

              
                <div class="myDiv">
  <form class="svg-test-form">
  <div class="fields">
    <fieldset>
      <legend>SVG size</legend>
      <div><label><input type="radio" name="img-type" value="50" checked=""> 50x50</label></div>
      <div><label><input type="radio" name="img-type" value="100"> 100x100</label></div>
      <div><label><input type="checkbox" name="use-viewbox"> Use viewBox</label></div>
    </fieldset>
    <fieldset>
      <legend><code>&lt;img&gt;</code> size</legend>
      <div><label><input type="radio" name="img-size" value="0" checked=""> Unset</label></div>
      <div><label><input type="radio" name="img-size" value="50"> 50x50</label></div>
      <div><label><input type="radio" name="img-size" value="100"> 100x100</label></div>
    </fieldset>
    <fieldset>
      <legend><code>&lt;img&gt;</code> CSS size</legend>
      <div><label><input type="radio" name="img-css-size" value="0" checked=""> Unset</label></div>
      <div><label><input type="radio" name="img-css-size" value="50"> 50x50</label></div>
      <div><label><input type="radio" name="img-css-size" value="100"> 100x100</label></div>
      <div><label><input type="checkbox" name="add-to-dom"> Add to &lt;body&gt;</label></div>
    </fieldset>
    <fieldset>
      <legend><code>&lt;canvas&gt;</code> size</legend>
      <div><label><input type="radio" name="canvas-size" value="50" checked=""> 50x50</label></div>
      <div><label><input type="radio" name="canvas-size" value="100"> 100x100</label></div>
    </fieldset>
    <fieldset>
      <legend><code>drawImage</code> size</legend>
      <div><label><input type="radio" name="drawimage-size" value="50" checked=""> 50x50</label></div>
      <div><label><input type="radio" name="drawimage-size" value="100"> 100x100</label></div>
    </fieldset>
    <fieldset>
      <legend>Context transform</legend>
      <div><label><input type="radio" name="context-transform" value="0.5"> 0.5x</label></div>
      <div><label><input type="radio" name="context-transform" value="1" checked=""> 1x</label></div>
      <div><label><input type="radio" name="context-transform" value="2"> 2x</label></div>
      <div><label><input type="radio" name="context-transform" value="5"> 5x</label></div>
    </fieldset>
  </div>
  <div class="svg-test-output trans-tile"><canvas width="50" height="50"></canvas></div>
</form>
</div>  

              
            
!

CSS

              
                body {
  margin: 0;
}

.myDiv {
  width: 768px;
  margin: 0 auto;
}

  .svg-test-form {
    font-size: 0.9em;
    margin: 0 -20px;
  }

  @media (min-width: 530px) {
    .svg-test-form {
      margin: 0 -64px 0 -32px;
    }  
  }

  .svg-test-output {
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0;
    margin-bottom: 10px;
    overflow: hidden;
  }

  .svg-test-output canvas {
    display: block;
    outline: 1px solid rgba(0,0,0,0.2);
  }

  .svg-test-form .fields {
    display: flex;
    flex-flow: row wrap;
    background: #e0eaf5;
    padding: 10px 0 0 20px;
  }

  .svg-test-form fieldset {
    border: none;
    padding: 0;
    margin: 10px 0;
    min-width: 50%;
  }

  @media (min-width: 500px) {
    .svg-test-form fieldset {
      min-width: 33%;
    }  
  }

  .svg-test-form legend {
    font-size: 1em;
    font-weight: bold;
  }

              
            
!

JS

              
                function loadImg(url, width, height) {
  return new Promise(function(resolve, reject) {
    var img = new Image();

    if (width) {
      img.width = width;
    }

    if (height) {
      img.height = height;
    }

    img.src = url;
    img.onload = function() {
      resolve(img);
    };
    img.onerror = function() {
      reject(Error('Image load failed'))
    };
  });
}

(function() {
  var svgImgs = {
    fixed50: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/fixed50.72ff2442c6d1.svg',
    fixed100: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/fixed100.4b1cb7cb9384.svg',
    viewbox50: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/viewbox50.bc248d9428e2.svg',
    viewbox100: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/viewbox100.297cc24d9e8c.svg',
  };

  function createCanvas(width, height) {
    var canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    return canvas;
  }

  function drawImgOnCanvas(canvas, img, width, height, scale) {
    var context = canvas.getContext('2d');
    context.scale(scale, scale);
    context.drawImage(img, 0, 0, width, height);
  }

  var svgTestForm = document.querySelector('.svg-test-form');
  var svgTestOutput = document.querySelector('.svg-test-output');

  function processForm() {
    var svgSize = svgTestForm.querySelector('[name=img-type]:checked').value;
    var imgSize = Number(svgTestForm.querySelector('[name=img-size]:checked').value);
    var imgCssSize = Number(svgTestForm.querySelector('[name=img-css-size]:checked').value);
    var addImgToDom = !!svgTestForm.querySelector('[name=add-to-dom]:checked');
    var useViewbox = !!svgTestForm.querySelector('[name=use-viewbox]:checked');
    var canvasSize = Number(svgTestForm.querySelector('[name=canvas-size]:checked').value);
    var drawImageSize = Number(svgTestForm.querySelector('[name=drawimage-size]:checked').value);
    var contextTransform = Number(svgTestForm.querySelector('[name=context-transform]:checked').value);
    var imgUrl;

    if (useViewbox) {
      imgUrl = svgImgs['viewbox' + svgSize];
    }
    else {
      imgUrl = svgImgs['fixed' + svgSize];
    }

    svgTestOutput.innerHTML = '';

    loadImg(imgUrl, imgSize, imgSize).then(img => {
      var el = img;

      if (imgCssSize) {
        img.style.width = img.style.height = imgCssSize + 'px';
      }

      if (addImgToDom) {
        document.body.appendChild(img);
      }

      el = createCanvas(canvasSize, canvasSize);
      drawImgOnCanvas(el, img, drawImageSize, drawImageSize, contextTransform);

      if (addImgToDom) {
        document.body.removeChild(img);
      }

      svgTestOutput.appendChild(el);
    });
  }

  processForm();

  svgTestForm.addEventListener('change', function() {
    processForm();
  });
}());

              
            
!
999px

Console