Skip to content

Instantly share code, notes, and snippets.

@mauriciomassaia
Created March 20, 2018 07:15
Show Gist options
  • Select an option

  • Save mauriciomassaia/b9e7ef6667a622b104c00249f77f8c03 to your computer and use it in GitHub Desktop.

Select an option

Save mauriciomassaia/b9e7ef6667a622b104c00249f77f8c03 to your computer and use it in GitHub Desktop.

Revisions

  1. mauriciomassaia created this gist Mar 20, 2018.
    14 changes: 14 additions & 0 deletions image-data-resize.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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)
    }