cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

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.

            
              <form action="">
  <input id="blahfile" type="file" onchange="handleChange()">
</form>
<p>Image resized in: <span id="timer"></span></p>
<p>
  <img src="" id="fillme" />
</p>
            
          
!
            
              // Returns a promise that resolves to the cropped
// image as a datauri
const cropImage = function(inputDataURI, width) {
  return new Promise((resolve, reject) => {
    if (inputDataURI.slice(0,10) !== 'data:image') {
      reject(new Error('Not an image.'));
    }
    const c = document.createElement('canvas');
    c.width = c.height = width || 100;
    const ctx = c.getContext('2d');
    const i = document.createElement('img');
    i.addEventListener('load', function() {
      ctx.drawImage(i, ...getWidthHeight(i, width));
      resolve(c.toDataURL('image/jpeg', 0.8));
    });
    i.src = inputDataURI;
  });
}

const getWidthHeight = function(img, side) {
  const { width, height } = img;
  if (width === height) {
    return [0, 0, side, side];
  } else if (width < height) {
    const rat = height / width;
    const top = ((side * rat) - side) / 2;
    return [0, -1 * top, side, side * rat]
  } else {
    const rat = width / height;
    const left = ((side * rat) - side) / 2;
    return [-1 * left, 0, side * rat, side]
  }
}

function handleChange() {
  let start, end;
  const q = document.getElementById('fillme');
  const file = document.getElementById('blahfile').files[0];
  const reader = new FileReader();
  reader.addEventListener('load', function() {
    var uri = reader.result;
    console.log(uri);
    start = Date.now();
    cropImage(uri, 100)
    .then((img) => {
      q.src = img;
      end = Date.now();
      const timerSpan = document.getElementById('timer');
      timerSpan.innerHTML = `${end - start}ms`;
    })
    .catch(e => {
      console.log(e);
    });
  }, false);
  if (file) {
    reader.readAsDataURL(file);
  }
}
            
          
!
999px
Loading ..................

Console