cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <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>  

            
          
!
            
              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;
  }

            
          
!
            
              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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console