+ 5

How to scale an image in canvas?

I want to scale this image making it the same size as the canvas, but without affecting its proportions. https://code.sololearn.com/WLJGi89dLrcO/?ref=app

18th Nov 2018, 4:50 AM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
3 ответов
+ 5
Is this what you want? var scale = Math.min(C.width / img.width, C.height / img.height); ctx.drawImage(img, 0, 0, img.width * scale, img.height * scale); But obviously, if the Canvas does not have the same aspect ratio as the image, the scaled image would never fit the Canvas perfectly. More on drawImage: https://www.w3schools.com/tags/canvas_drawimage.asp
18th Nov 2018, 5:48 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Thanks you! Yes, is that :D
18th Nov 2018, 6:02 AM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
+ 1
You're welcome! 😊
18th Nov 2018, 6:08 AM
Kishalaya Saha
Kishalaya Saha - avatar