Skip to content

Instantly share code, notes, and snippets.

@egeste
Created March 25, 2017 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egeste/3c048676c2217fa6fb42d700f5005f93 to your computer and use it in GitHub Desktop.
Save egeste/3c048676c2217fa6fb42d700f5005f93 to your computer and use it in GitHub Desktop.
Partial code example of iterative image resampling
// This canvas is only used for initial rendering and resampling
const imageCanvas = document.createElement('canvas')
imageCanvas.width = width
imageCanvas.height = height
// If we have constraints, resize/resample the provided image
if (constraints) {
// Get the percentage difference between the desired and actual dimensions
// We can calculate it based on width, since we a locked aspect
const percentageWidth = (width / constraints.width) * 100
const absolutePercentage = Math.abs(percentageWidth)
const percentageRounded = Math.round(absolutePercentage * 100)
const resampleCount = Math.min(percentageRounded, 100)
const resampleInterval = percentageWidth / resampleCount
for (var i = 1; i <= resampleCount; i++) {
const mw = (width / (resampleInterval * i))
const mh = (height / (resampleInterval * i))
imageLayer.drawImage(imageCanvas, 0, 0, mw, mh, 0, 0, mw, mh)
}
}
context.drawImage(imageCanvas,
0, 0, width, height,
0, 0, canvas.width, canvas.height
)
window.open(canvas.toDataURL())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment