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

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.

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

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SVG to canvas to file</title>
</head>
<body>
  <div>
    <p>This Pen intends to show a problem when writing an SVG to a canvas and then downloading that canvas to PNG when devicePixelRatio is something other than 1. On a MacBook Pro with retina display devicePixelRatio is 2. In order for the canvas not to be blurry it needs to be scaled up by a factor of 2. When drawn to the screen it can easily be scaled down again with CSS, creating a sharp image. However, downloading the canvas as a PNG will then result in an image twice the intended size.</p>
    <p><strong>My question: is there a way to generate an SVG to PNG that is both sharp and the same size as the SVG?</strong></p>
  </div>
  <div id="my-div">
    <div style="display: inline-block; vertical-align: top">
      <p>SVG</p>
      <svg id="my-svg" width="300" height="300" viewBox="0 0 300 300">
        <circle cx="75" cy="75" r="70" fill="Coral" stroke="Firebrick" stroke-width="5"></circle>
        <circle cx="225" cy="75" r="70" fill="blue" stroke="SkyBlue" stroke-width="5"></circle>
        <circle cx="75" cy="225" r="70" fill="SkyBlue" stroke="SteelBlue" stroke-width="5"></circle>
        <circle cx="225" cy="225" r="70" fill="Firebrick" stroke="Coral" stroke-width="5"></circle>
        <circle cx="150" cy="150" r="90" fill="SteelBlue" stroke="MediumAquamarine" stroke-width="5"></circle>
        <text x="30" y="30" font-size="40" fill="#191919">Hello World!</text>
        <text x="30" y="60" font-size="40" fill="#FFF">Hello World!</text>
        <text x="30" y="90" font-size="40" fill="#191919">Hello World!</text>
        <text x="30" y="120" font-size="40" fill="#FFF">Hello World!</text>
        <text id="my-text" x="30" y="150" font-size="40" fill="#191919">Hello World!</text>
        <text x="30" y="180" font-size="40" fill="#FFF">Hello World!</text>
        <text x="30" y="210" font-size="40" fill="#191919">Hello World!</text>
        <text x="30" y="240" font-size="40" fill="#FFF">Hello World!</text>
        <text x="30" y="270" font-size="40" fill="#191919">Hello World!</text>
        <text x="30" y="300" font-size="40" fill="#FFF">Hello World!</text>
      </svg>      
    </div>
    <div id="my-target-1" style="display: inline-block; vertical-align: top">
      <p>Canvas with no scaling</p>
      <canvas id="my-canvas-1"></canvas>
      <p>Image from canvas</p>
    </div>
    <div id="my-target-2" style="display: inline-block; vertical-align: top">
      <p>Canvas with scaling</p>
      <canvas id="my-canvas-2"></canvas>
      <p>Image from canvas</p>
    </div>
  </div>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                let svg = document.getElementById('my-svg');
let canvas1 = document.getElementById('my-canvas-1');
let canvas2 = document.getElementById('my-canvas-2');
let text = document.getElementById('my-text');
let div = document.getElementById('my-div');
let target1 = document.getElementById('my-target-1');
let target2 = document.getElementById('my-target-2');
let dpr = window.devicePixelRatio;
text.textContent = `dpr: ${dpr}`;
if (dpr === 1) {
	alert('This Fiddle is intended to show a problem when devicePixelRatio !== 1.');
}
let svgToCanvas = (svg, canvas, scale) => {
	let p = new Promise(function (resolve, reject) {
  	let w = svg.getAttribute('width');
    let h = svg.getAttribute('height');
    canvas.width = w * scale;
    canvas.height = h * scale;
    canvas.style.width = `${w}px`;
    canvas.style.height = `${h}px`;
    let ctx = canvas.getContext('2d');
    ctx.scale(scale, scale);
    let img = new Image();
    svg_xml = (new XMLSerializer()).serializeToString(svg);
    img.onload = function () {
      ctx.drawImage(img, 0, 0);
      resolve(canvas);
    }
    img.src = 'data:image/svg+xml;base64,' + btoa(svg_xml);
  });
  return p;
};
let doTheBusiness = (canvas, target, scale, name, click) => {
	let url = canvas.toDataURL();
  let img = document.createElement('img');
  img.width = canvas.width / scale;
  img.height = canvas.height / scale;
  img.src = url;
  target.appendChild(img);
  let p = document.createElement('p');
	let a = document.createElement('a');
  a.download = name;
  a.href = url;
  a.textContent = 'Download image';
  p.appendChild(a);
  target.appendChild(p);
  if (click) {
	  a.click();  
  }
  // a.click();
  // document.body.removeChild(a);
  // delete a;
};
svgToCanvas(svg, canvas1, 1).then(canvas => 
doTheBusiness(canvas, target1, 1, 'correctSizePngButBlurry.png'));
svgToCanvas(svg, canvas2, dpr).then(canvas => doTheBusiness(canvas, target2, dpr, 'wrongSizePngButNotBlurry.png'));
              
            
!
999px

Console