Canvas drawImage scaling

selanac82 picture selanac82 · May 31, 2012 · Viewed 67.6k times · Source

I'm trying to scale an image proportionately to the canvas. I'm able to scale it with fixed width and height as so:

context.drawImage(imageObj, 0, 0, 100, 100)

But I only want to resize the width and have the height resize proportionately. Something like the following:

context.drawImage(imageObj, 0, 0, 100, auto)

I've looked everywhere I can think of and haven't seen if this is possible.

Answer

Gaurav picture Gaurav · Jun 1, 2012
context.drawImage(imageObj, 0, 0, 100, 100 * imageObj.height / imageObj.width)