Pen Settings



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


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


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.


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.


                <div class="container">
  <h1>JPG+PNG to SVG Mask</h1>
  <h3><a href="" target="_blank" class="jpng">For an easier process, give <u>JPNG.svg</u> a try.</a></h3>
  <p><strong>Combine the transparency of a PNG with the compression of a JPEG.</strong> Based on the idea from <a href="">Using SVG to Shrink Your PNGs</a>, but adapted to use data URIs instead of external images. Include on your page as inline SVG, using an <code>&lt;img src="image.svg"/&gt;</code> tag, or as a <code>background-image</code>.</p>
  <p><small><em>Tested in the latest versions of Chrome, Firefox and Safari. This SVG technique's compatibility via an <code>&lt;img /&gt;</code> tag or as a <code>background-image</code> may not be perfect. See <a href="" target="_blank">this pen</a> to test on your browser. Inline seems to be the best option for compatibility, in which case you should use external assets so that they can be cached. Please fork or comment to improve.</em></small></p>
  <p>To get started, upload two images:</p>

    <li>One as your primary image, named whatever <small>(Try <a href="" target="_blank" download="face.jpg">this one</a>)</small></li>
    <li>One as a mask <small><em>(Black and white. PNG is usually best, like <a href="" target="_blank" download="face-mask.png">this one</a>)</em></small> with <code>-mask</code> or <code>-alpha</code> in the filename .</li>

  <label for="files" id="upload">Images: <input type="file" id="files" name="files" multiple> <br />
    <small><em>Make sure the mask has '-mask' or '-alpha' in the filename.</em></small>

  <div id="output" style="display: none;">
    <p>Copy and paste code to use inline:</p>
    <textarea id="code"></textarea>

    <p>Or click the preview below to download the SVG:</p>
    <div id="preview"></div>

<div class="examples">
  <a href="" target="_blank" download="face.jpg" class="example"><strong>Image:</strong> <img src="" /></a>
  <a href="" target="_blank" download="face-mask.png" class="example"><strong>Mask:</strong> <img src="" /></a>
  <a href="" target="_blank" download="face-mask.png" class="example output">
    <img src="" />




                @import "lesshat";

html, body { margin: 0; padding: 0; min-height: 100%; }
.container { padding: 20px; margin: 0 auto; }
h1 { margin: 0; }

.jpng { display: block; color: #4E1E1E; text-decoration: none; background: #FBE6A2; padding: 0.75em 1em; text-align: center; }
.jpng:focus { outline: solid 3px; }

#upload { 
  background: #EEE;
  text-align: center;
  padding: 20px;
  display: block;
  cursor: pointer;
#upload input { cursor: pointer; margin-bottom: 10px; }

#output { display: none; }

#code {
  font-family: monospace;
  font-size: 12px;
  display: block;
  width: 100%;
  height: 150px;
  overflow: auto;
  background: #DDD;
  border: none;
  word-break: keep-all;

.examples { text-align: center; }
.example { display: inline-block; width: 30%; text-decoration: none; }
.example img { max-width: 100%; height: auto;}



// SVG alpha mask idea from
var template = '<svg preserveAspectRatio="xMinYMin" version="1.1" xmlns="" xmlns:xlink="" viewBox="0 0 {{image.width}} {{image.height}}">\n	<defs>\n		<mask id="mask">\n		<image width="{{mask.width}}" height="{{mask.height}}" xlink:href="{{mask.src}}"></image>\n 		</mask>\n	</defs>\n	<image mask="url(#mask)" id="image" width="{{image.width}}" height="{{image.height}}" xlink:href="{{image.src}}"></image>\n</svg>',
    images = {
      "mask": { "loaded": false },
      "image": { "loaded": false }

function svgOutput() {
  // Wait until mask and image are loaded
  if ( images.mask.loaded === true && images.image.loaded === true ) {
    var output = document.getElementById("output"),
        code = document.getElementById("code"),
        preview = document.getElementById("preview"),
        formatted = template;

    formatted = formatted.replace(/{{image.height}}/g,;
    formatted = formatted.replace(/{{image.width}}/g,;
    formatted = formatted.replace(/{{image.src}}/g,;
    formatted = formatted.replace(/{{mask.width}}/g,;
    formatted = formatted.replace(/{{mask.height}}/g,;
    formatted = formatted.replace(/{{mask.src}}/g,;

    // Give a preview the user can click to download
    preview.innerHTML = "<a href='data:Application/octet-stream,"+ encodeURIComponent(formatted) +"' download='" +\.[^/.]+$/, "") + ".svg'>" +formatted + "</a>";

    code.innerHTML = formatted.replace(/</g,"&lt;").replace(/>/g,"&gt;"); = "block";
    // Reset images
    images = {
      "mask": { "loaded": false },
      "image": { "loaded": false }
    return formatted;

function imageToObject(image) {
  var src = image.result;
  var type = image.type; = {}; = src.replace("data:;","data:" + type + ";");

  var img = document.createElement('img');
  img.src = src; = "none";
  // Wait for img to load to get accurate width & height
  img.onload = function () { = this.height; = this.width;
    image.loaded = true;
  return image;

// Image to Data URI conversion adapted from
function fileSelected(evt) {
  var files =;

  for (var i = 0; i < files.length; i++) {

    var f = files[i],
        role = "image";
    if ( f.type.indexOf("image") == 0 ) {

      if ("-mask") > 0 ||"-alpha") > 0 ) {
        role = "mask";

      if ( images[role].loaded === false ) {

        images[role] = new FileReader();
        images[role].loaded = "pending";
        images[role].type = f.type;
        images[role].name =;
        images[role].onload = function(){
        images[role].readAsDataURL(f); //on successful read, fr.onload function will be called and that will populate the result in fileContent container
  //attach change event of file control
document.getElementById('files').addEventListener('change', fileSelected, false);