It's not certain without seeing how you set fromX/toX, but it looks like you're not giving getImageData the proper parameters.
context.getImageData
has these parameters:
- x: starting x coordinate to extract,
- y: starting y coordinate to extract,
- width: extract a block this many pixels wide,
- height: extract a block this many pixels high
[ Addition: based on additional info ]
If you want equal sized chunks of pixel data, your first call is
var data=getImageData(0,0, 20, canvas.height).data;
And your second call would be:
// Note: the width arguement remains at 20, not increase to 40
var data=getImageData(20,0, 20, canvas.height).data;