Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize image in canvas proportionately with respect to width in fabricjs?

The minimum width of the image should be 300 and the height should get scaled proportionately with respect to width.

Suppose the user uploads image size of 5ooX850, I will use

                Img.set({
                    scaleX: 300 / Img.width,
                });

But how to scale image height with respect to width?

like image 860
Abhinav Avatar asked Dec 19 '16 13:12

Abhinav


People also ask

How do you resize a canvas?

With the Select and Move Tool, click the canvas size label or border to select the canvas. Click the canvas border handles to resize the canvas dynamically. When you drag the border handles without using any keyboard modifiers, the canvas resizes non-uniformly. Hold Shift to constrain the resize proportions.

How do you crop an image on canvas using fabric JS?

JS and give the width to be cropped of the canvas image in pixels using the cropX property of the image object. Syntax: fabric. Image(image, { cropX:Number });

How do you make fabric canvas?

Let's see a code example to create a Canvas using FabricJS. Since FabricJS works on top of Canvas API, we will create an HTML element using <canvas> tag and give it an id. Further, we will pass that id to the FabricJS API so that it can initialize the FabricJS Canvas instance on the <canvas> tag.


1 Answers

Scale Y by the same ratio used to scale X

let scale = 300 / Img.width;

Img.set({
    scaleX: scale,
    scaleY: scale
});

With your example of 500 x 850, both X and Y would be 3/5 of their original measurements.

like image 178
J. Titus Avatar answered Oct 09 '22 19:10

J. Titus