Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scottymeyers/05a10a996d9673543b708fd31eca8049 to your computer and use it in GitHub Desktop.
Save scottymeyers/05a10a996d9673543b708fd31eca8049 to your computer and use it in GitHub Desktop.
ImageData resize on Canvas
export async function resizeImageData (imageData, width, height) {
const resizeWidth = width >> 0
const resizeHeight = height >> 0
const ibm = await window.createImageBitmap(imageData, 0, 0, imageData.width, imageData.height, {
resizeWidth, resizeHeight
})
const canvas = document.createElement('canvas')
canvas.width = resizeWidth
canvas.height = resizeHeight
const ctx = canvas.getContext('2d')
ctx.scale(resizeWidth / imageData.width, resizeHeight / imageData.height)
ctx.drawImage(ibm, 0, 0)
return ctx.getImageData(0, 0, resizeWidth, resizeHeight)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment