Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas drawImage scaling

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.

like image 426
selanac82 Avatar asked May 31 '12 21:05

selanac82


People also ask

How do you scale a canvas?

You can scale your canvas with content by "bouncing" the content off a temporary canvas while you resize the original canvas. This save+redraw process is necessary because canvas content is automatically cleared when you resize the canvas width or height.


1 Answers

context.drawImage(imageObj, 0, 0, 100, 100 * imageObj.height / imageObj.width) 
like image 125
Gaurav Avatar answered Sep 21 '22 10:09

Gaurav