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

              
                <div class="card">
  <p>Original image</p>
  <img id="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAANlBMVEX///8AAAAtKSJeRS5cXmyFZjrhW0WjhkHkdnrBmUnfpk3Rsknlu0zyyk3/1ErV09D+2kz/31C9TFjFAAAAAXRSTlMAQObYZgAAAytJREFUeNq9V9t24yAMXKAyFUhU/P/P7gBOYieOzTbnrF7aXDQZ3Ubynz//y/yn/t595O8+BfCw0w/dJYETgP7pOUYIZwArAse3X3FZwxnAQJCq7yC8mZwCdIRs5R0E1VKju8oS11JKlXDwIVsx9Vd5JgVAsfpKwisAxF22SpIOUfMzQjAA80SzxQFRNbykADmYaFcXUpIWhoRARCj+eD82BmGu44nXOJqZcnAjh0X97NCEyOVuFVVxDgUuyc3PXfv+3axyfyPOD26sZWc1Z0QQ5gfXrNoOwURK8v9AQCPrnkRRmiegRl8//ASQ5xWR0Eb+i56CmCeAmlvsk1kGhvXenK6BoyVj9rU5556I/BO11GkGflna6BZFH/aWlPDzFa1OF5E6QCnMHKPW1sHeoy5+PoIOUDnAKJsaB1LLs33swrJIH34gYRpJSgtoPofIQY/A+Bu2hNHVNnLo3AQPNwbJUgP4pl4NSxhqTzELzSShbgDyYINo2KAONYeJWZbulJv/iCYty5LGgF61gwsUI+fWQNr8h0rDfxmzVeW8mj43GYMzIGridSIV/mtiur6djuFtdDSjfOsrhJCGv7TugtS+28K0ESLLd1mTLKs/hqRLbRGOByh7JVMeK0IQTk8lvB7wANJMTxgooG1EjMeG0DHUpkL8JBG1AmOXA1HYjYDsBE0SeSnyJFPQTvbbIqZuklCBvP0i3iHnTQepnWDvdn5bS92y2pN/aDmWBq/v/XsiRWQN+8E/tX2P/TgIbiBebw60gpVnA/0+ZWUFuMdxcLOEWl5tKLpXpCBSfJCo5I4umRdrl4l3zrcUtL/YvUCwI3/8SjkEwOnCbCjlekVAG+Tw5qJ6DOC46Xx61LwRcW/V6AgApDWF67VcjgDUO0pMHC8VMR5F0I4jFyKB9aWaiR0DRMj7zGYKxwSgDe1g+8Vls4nBhzC9EQ4MLTO1Utj0DQOe3Ktt3m+CojJMexJmr6vbtCVJD5Pp1drkZPjgNODb/42Vn3PPFTEkCDZmDo8fgW4Q0U/xzxm/TMFvbnegNCYi5CaWav/lo20pViX+/sG5hUOc+bNHb3f2yPcXb7RKPgK7Q1AAAAAASUVORK5CYII=" />
  <p>or select custom image:</p>
  <input id="input" type="file" />
</div>

<div class="card">
  <p>Lanczos Scale</p>
  <div>
    <input id="numWidth" type="number" value="128"  />x
    <input id="numHeight" type="number" value="128" disabled />
  </div>
  <div>
    <label>
      <input id="cbProportions" type="checkbox" checked />
      Constrain proportions
    </label>
  </div>
  <div>
    <label>
      <input name="filterSize" data-size="1" type="radio" checked/> Lanczos1
    </label>
    &nbsp;
    <label>
      <input name="filterSize" data-size="2" type="radio" /> Lanczos2
    </label>
    &nbsp;
    <label>
      <input name="filterSize" data-size="3" type="radio" /> Lanczos3
    </label>
  </div>
  <canvas id="canvasResult" height="16"></canvas>
</div>
              
            
!

CSS

              
                canvas {
  border: solid 1px #999;
  background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="8" height="8" viewBox="0 0 8 8"%3E%3Cg fill="%239C92AC" fill-opacity="0.4"%3E%3Cpath fill-rule="evenodd" d="M0 0h4v4H0V0zm4 4h4v4H4V4z"/%3E%3C/g%3E%3C/svg%3E');
}
.red {
  color: red;
}
.todo {
  border: dashed 1px red;
}

p {
  margin: 5px 0 3px;
}

.card {
  margin: 15px 5px;
}

input[type="range"] {
  vertical-align: middle;
}

input[type="number"] {
  max-width: 80px;
}
              
            
!

JS

              
                // https://github.com/mudcube/Lanczos.js
function lanczos(size, x) {
  if (x >= size || x <= -size) return 0;
  if (x === 0) return 1;
  const xpi = x * Math.PI;
  return size * Math.sin(xpi) * Math.sin(xpi / size) / (xpi * xpi);
}

function createCache(cachePrecision, filterSize) {
  const cache = {};
  const max = filterSize * filterSize * cachePrecision;
  const iPrecision = 1.0 / cachePrecision;
  for (let cacheKey = 0; cacheKey < max; cacheKey++) {
    const value = lanczos(filterSize, Math.sqrt(cacheKey * iPrecision));
    cache[cacheKey] = value < 0 ? 0 : value;
  }
  return cache;
};

function processImage(inImg) {
  const width = inImg.width;
  const height = inImg.height;
  const src = new Uint32Array(inImg.data.buffer);
  
  const filterSize = $("input:radio[name='filterSize']").filter(':checked').attr('data-size');
  const isConstrainProportions = $("#cbProportions").prop('checked');
  const newWidth = getValue($("#numWidth").val());
  const newHeight = isConstrainProportions
        ? Math.round(height * newWidth / width)
        : getValue($("#numHeight").val());
  
  $("#numHeight").prop("disabled", isConstrainProportions);
  $("#numWidth").val(newWidth);
  $("#numHeight").val(newHeight);
  
  processCanvas('canvasResult', newWidth, newHeight, function(dst) {
    const values = [];
    const sx = newWidth / width;
    const sy = newHeight / height;
    const xMax = width - 1;
    const yMax = height - 1;
    const csx = Math.min(1, sx) * Math.min(1, sx);
    const csy = Math.min(1, sy) * Math.min(1, sy);
    const cachePrecision = 1000;
    const cache = createCache(cachePrecision, filterSize);
    let x1et, y1et;
    let y = newHeight;
    while (y--) {
      const sourcePixelY = (y + 0.5) * (1.0 / sy);
      let y1b = sourcePixelY - filterSize;
      if (y1b < 0) y1b = 0;
      let y1e = y1et = sourcePixelY + filterSize;
      if (y1e != y1et) y1e = y1et + 1;
      if (y1e > yMax) y1e = yMax;
      const cy = y * (1.0 / newHeight) - sourcePixelY;
      const y3 = y * newWidth;
      let x = newWidth;
      while (x--) {
        const sourcePixelX = (x + 0.5) * (1.0 / sx);
        let x1b = sourcePixelX - filterSize;
        if (x1b < 0) x1b = 0;
        let x1e = x1et = sourcePixelX + filterSize;
        if (x1e != x1et) x1e = x1et + 1;
        if (x1e > xMax) x1e = xMax;
        const cx = x * (1.0 / newWidth) - sourcePixelX;
        ///
        let total = 0;
        let i = 0;
        for (let y1 = y1b >> 0; y1 <= y1e; y1++) {
          const distanceY = (y1 + cy) * (y1 + cy) * csy;
          for (let x1 = x1b >> 0; x1 <= x1e; x1++) {
            total += values[i++] = cache[(((x1 + cx) * (x1 + cx) * csx + distanceY) * cachePrecision) >> 0] || 0;
          }
        }
        total = 1.0 / total;
        ///
        let a = 0;
        let r = 0;
        let g = 0;
        let b = 0;
        i = 0;
        for (let y1 = y1b >> 0; y1 <= y1e; y1++) {
          const y2 = y1 * width;
          for (let x1 = x1b >> 0; x1 <= x1e; x1++) {
            const value = values[i++] * total;
            const pix = src[((y2 + x1) >> 0)];
            r += (pix & 0xFF) * value;
            g += ((pix >> 8) & 0xFF) * value;
            b += ((pix >> 16) & 0xFF) * value;
            a += (((pix >> 24) & 0xFF)) * value;
          }
        }
        dst[((x + y3) >> 0)] = (a << 24) | (b << 16) | (g << 8) | r;
      }
    }
  });
}

function getValue(str) {
  let value = parseInt(str) || 1;
  if (value < 1) return 1;
  else if (value > 999) return 999;
  else return value;
}

function getImageData(el) {
  const canvas = document.createElement('canvas');
  const context = canvas.getContext('2d');
  const img = document.getElementById(el);
  canvas.width = img.width;
  canvas.height = img.height;
  context.drawImage(img, 0, 0);
  return context.getImageData(0, 0, img.width, img.height);
}

function processCanvas(canvasId, width, height, func) {
  const canvas = document.getElementById(canvasId);
  canvas.width = width;
  canvas.height = height;
  const ctx = canvas.getContext('2d');
  const outImg = ctx.createImageData(width, height);
  const dst = new Uint32Array(outImg.data.buffer);
  func(dst);
  ctx.putImageData(outImg, 0, 0);
}

document.getElementById('input').addEventListener('change', function() {
  if (this.files && this.files[0]) {
    var img = document.getElementById('img');
    img.src = URL.createObjectURL(this.files[0]);
    img.onload = update;
  }
});


$('input[type="number"]').on('input change', update);
$('#cbProportions').change(update);
$('input[name="filterSize"]').on('click change', update);

function update(e) {
  processImage(getImageData('img'));
}

update();
              
            
!
999px

Console