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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div style="display: flex; flex-wrap: wrap; width: 605px">
  <div id="original" class="render">
    <i class="fas fa-fan fa-spin"></i>
    <label>Original</label>
  </div>
  <div id="canvasResize" class="render">
    <i class="fas fa-fan fa-spin"></i>
    <label>Direct resize</label>
  </div>
  <div id="canvasResizeQuality" class="render">
    <i class="fas fa-fan fa-spin"></i>
    <label>Direct resize with high quality</label>
  </div>
  <div id="multiStepResize" class="render">
    <i class="fas fa-fan fa-spin"></i>
    <label>Multi step resize</label>
  </div>
  <div id="imageBitmapResize" class="render">
    <i class="fas fa-fan fa-spin"></i>
    <label>createImageBitmap</label>
  </div>
  <div id="paintDotnet" class="render">
    <i class="fas fa-fan fa-spin"></i>
    <label>Paint.NET</label>
  </div>
</div>
              
            
!

CSS

              
                .render {
  width: 301px;
  height: 450px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  background-color: #f6f6f7;
  margin-bottom: 20px;
}

#original {
  img {
    width: 100%;
  }
}
              
            
!

JS

              
                main();

async function main() {
	const url = 'https://i.imgur.com/9nhVLM1.png';

  const img = await loadImage(url);
  const methods = [canvasResize, multiStepResize, canvasResizeQuality, imageBitmapResize];

  for (resize of methods) {
    const canvas = await resize(img, img.width / 4, img.height / 4);
    render(canvas, resize.name)
  }
  
  const paintDotnet = new Image();
  paintDotnet.src = 'https://i.imgur.com/3GNNovE.png';
  render(img, 'original');
  render(paintDotnet, 'paintDotnet');
}

function render(img, target) {
  let renderImg;

  if (typeof OffscreenCanvas !== "undefined" && img instanceof OffscreenCanvas) {
    renderImg = document.createElement('canvas');
    renderImg.width = img.width;
    renderImg.height = img.height;
    renderImg.getContext('2d').drawImage(img, 0, 0);
  } else {
    renderImg = img;
  }

  const el = document.getElementById(target);
  el.replaceChild(renderImg, el.querySelector('i'));
}

function canvasResize(img, width, height, quality = null) {
	const canvas = newCanvas(width, height);

  const ctx = canvas.getContext('2d');
  if (quality) {
    ctx.imageSmoothingEnabled = true;
    ctx.imageSmoothingQuality = quality;
  }
  ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height);

  return Promise.resolve(canvas);
}

function canvasResizeQuality(img, width, height) {
  return canvasResize(img, width, height, 'high');
}

async function multiStepResize(img, width, height) {
	let canvas = img;

	while (canvas.width * .5 > width) {
    canvas = await canvasResize(canvas, canvas.width / 2, canvas.height / 2);
  }

  return canvasResize(canvas, width, height);
}

function imageBitmapResize(img, width, height) {
  return createImageBitmap(img, 0, 0, img.width, img.height, {
    resizeWidth: width,
    resizeHeight: height,
    resizeQuality: 'high',
  }).then((data) => {
    const canvas = newCanvas(width, height);
    canvas.getContext('2d').drawImage(data, 0, 0);

    return canvas;
  });
}

function newCanvas(width, height) {
  if (typeof OffscreenCanvas !== "undefined") {
    return new OffscreenCanvas(width, height);
  }

  const canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;

  return canvas;
}

function loadImage(url) {
  const img = new Image();
  img.crossOrigin = 'Anonymous';

  return new Promise((resolve, reject) => {
    img.onload = function() {
      resolve (this);
    };

    img.error = reject;

    img.src = url;
  })
}

              
            
!
999px

Console