I have a canvas of 600 x 400 pixels, which returns an ImageData with data length of 960,000 (600*400*4). Is there any way to downscale both width & height say 10 times, I would like to get as a result an ImageData whose data length is 9600 (60*40*4).
const canvas = document.getElementsByTagName("canvas")[0]
var ctx = canvas.getContext("2d");
const origImageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
origImageData
// => ImageData {data: Uint8ClampedArray(960000), width: 600, height: 400}
const smallImageData = downscale(origImageData, 60, 40);
smallImageData
// => ImageData {data: Uint8ClampedArray(9600), width: 60, height: 40}
I need resulting ImageData.data array for further manipulation. This method would be called in a loop so it would be good if its fast.
Edit This is the suggested approach, which I'm not sure it's correct:
var canvas2 = document.createElement('canvas');
canvas2.width = canvas.width/10;
canvas2.height = canvas.height/10;
var ctx2 = canvas2.getContext('2d');
// Step that I found confusing
// Is the new image data being created?
ctx2.putImageData(origImageData, 0, 0);
// Which image data I'm getting here resized or part of original ?
ctx2.getImageData(0,0, canvas2.width, canvas2.height)
Edit 2 It doesn't seem to be working, small canvas isn't resized, but only a cropped https://codepen.io/bobiblazeski/full/drrQoB