I recently asked a question on how to draw an image larger than my canvas onto the canvas without cropping and save the image back into its original dimensions after performing some changes on it. I found the solution is to set the inline height
and width
attributes for the canvas larger than the image, and use css height
and width
properties to style the canvas to fit into your layout.
For example, assuming my images could range between 400px to 2000 px in dimensions. But I don't want the canvas to be so big, and I don't want the images to be cropped as well (because when saving the image with toDataURL
what is on the canvas is what is saved). This is what works:
//style
#mycanvas{
width: 400px;
height: 400px;
}
<canvas id="mycanvas" width="2000" height="2000" />
This works fine and my images come out well. But is this behavior buggy and bound to be changed/fixed in the future? I'm planning to use this solution for a task at hand.
Any guidance?
This behaviour is commonly used for controlling the size of a canvas:
See Canvas width and height in HTML5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With