Rectangle 27 0

javascript How to maintain the aspect ratio while scaling the image on HTML5 Canvas?


centeredScaling: true
lockUniScaling: true

I will typically want the image to scale from the center, and thus set this as well:

The basis behind this is to create two elements - one being your image, and the second being your clipping mask (a shape typically, such as a rectangle, polygon, etc.) than then is applied as the clipTo property to your image element. This solution is covered in: Multiple clipping areas on Fabric.js canvas

To "crop" the image however; this takes more effort as this isn't a default functionality offered by Fabric JS. I've done a fair bit of work around this and have yet to come up with a bullet-proof solution that works for text, images, rectangles, polygons, circles, rotation angles, etc.

To ensure that resizing is done proportinatly, you can simply add the following properties to your image object:

Note
Rectangle 27 0

javascript How to maintain the aspect ratio while scaling the image on HTML5 Canvas?


canvas {
    -ff-transform: scale(2); 
    -ms-transform: scale(2); 
    -webkit-transform: scale(2); 
}

As you're using HTML5, you could just apply a CSS3 transformation to the canvas with:

Here canvas is my container and i'm adding new image inside canvas so i need to adjust the particular image width and height not the entire canvas. while adjusting the image width or height i need to adjust the image based on aspect ratio and crop the image as per the canvas image object width and height.

The OP is talking about canvas objects not canvas itself, so I don't think it applies here.

Using the CSS3 transform property with the scale method should resize the canvas, and maintaining you're aspect ratio. It's also

Note
Rectangle 27 0

javascript How to maintain the aspect ratio while scaling the image on HTML5 Canvas?


centeredScaling: true
lockUniScaling: true

I will typically want the image to scale from the center, and thus set this as well:

The basis behind this is to create two elements - one being your image, and the second being your clipping mask (a shape typically, such as a rectangle, polygon, etc.) than then is applied as the clipTo property to your image element. This solution is covered in: Multiple clipping areas on Fabric.js canvas

To "crop" the image however; this takes more effort as this isn't a default functionality offered by Fabric JS. I've done a fair bit of work around this and have yet to come up with a bullet-proof solution that works for text, images, rectangles, polygons, circles, rotation angles, etc.

To ensure that resizing is done proportinatly, you can simply add the following properties to your image object:

Note
Rectangle 27 0

javascript How to maintain the aspect ratio while scaling the image on HTML5 Canvas?


centeredScaling: true
lockUniScaling: true

I will typically want the image to scale from the center, and thus set this as well:

The basis behind this is to create two elements - one being your image, and the second being your clipping mask (a shape typically, such as a rectangle, polygon, etc.) than then is applied as the clipTo property to your image element. This solution is covered in: Multiple clipping areas on Fabric.js canvas

To "crop" the image however; this takes more effort as this isn't a default functionality offered by Fabric JS. I've done a fair bit of work around this and have yet to come up with a bullet-proof solution that works for text, images, rectangles, polygons, circles, rotation angles, etc.

To ensure that resizing is done proportinatly, you can simply add the following properties to your image object:

Note