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="container">
  <h1>JPG+PNG to SVG Mask</h1>
  
  <h3><a href="https://codepen.io/shshaw/full/LVKEdv" 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="http://peterhrynkow.com/how-to-compress-a-png-like-a-jpeg/">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="https://codepen.io/shshaw/pen/IDbqC" 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>

  <ul>
    <li>One as your primary image, named whatever <small>(Try <a href="http://brokensquare.com/Code/assets/face.jpg" 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="http://brokensquare.com/Code/assets/face-mask.png" target="_blank" download="face-mask.png">this one</a>)</em></small> with <code>-mask</code> or <code>-alpha</code> in the filename .</li>
  </ul>

  <h2>Upload:</h2>
  <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>
  </label>

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

<h2>Example:</h2>
<div class="examples">
  <a href="http://brokensquare.com/Code/assets/face.jpg" target="_blank" download="face.jpg" class="example"><strong>Image:</strong> <img src="http://brokensquare.com/Code/assets/face.jpg" /></a>
  <a href="http://brokensquare.com/Code/assets/face-mask.png" target="_blank" download="face-mask.png" class="example"><strong>Mask:</strong> <img src="http://brokensquare.com/Code/assets/face-mask.png" /></a>
  
  <a href="https://cl.ly/1L433346393M/download/face.svg" target="_blank" download="face-mask.png" class="example output">
  <strong>Output</strong>
    <img src="http://brokensquare.com/Code/assets/face.svg" />
  </a>
</div>

</div>

            
          
!
            
              @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:hover,
.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;}
            
          
!
            
              console.clear();

// SVG alpha mask idea from https://gist.github.com/peterhry/afac4906f2b330509fc7
var template = '<svg preserveAspectRatio="xMinYMin" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/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 ) {
    console.table(images);
  
    var output = document.getElementById("output"),
        code = document.getElementById("code"),
        preview = document.getElementById("preview"),
        formatted = template;

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

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

    code.innerHTML = formatted.replace(/</g,"&lt;").replace(/>/g,"&gt;");

    output.style.display = "block";
  
    // Reset images
    images = {
      "mask": { "loaded": false },
      "image": { "loaded": false }
    };
  
    return formatted;
  }
}


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

  var img = document.createElement('img');
  
  img.src = src;
  img.style.display = "none";
  document.body.appendChild(img);
  
  // Wait for img to load to get accurate width & height
  img.onload = function () {
    image.data.height = this.height;
    image.data.width = this.width;
    image.loaded = true;
    document.body.removeChild(this);
    svgOutput();
  };
  
  return image;
}


// Image to Data URI conversion adapted from http://www.techmcq.com/article/Converting-an-image-into-data-URI-using-JavaScript-FileReader/61
function fileSelected(evt) {
  var files = evt.target.files;

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

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

      if ( f.name.indexOf("-mask") > 0 || f.name.indexOf("-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 = f.name;
        
        images[role].onload = function(){
          imageToObject(this);
          svgOutput();
        }
        
        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);
            
          
!
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