Sachin Vairagi
Sachin Vairagi

Reputation: 5334

fabric js - how to calculate area after scaling?

I need to calculate area of a circle after scaling, following are the codes I am using to calculate it -

function calculateArea(obj){
        switch (obj.type){
        case 'circle':
             alert((Math.PI*obj.radius*obj.radius).toFixed(2));
        break;
        case 'rect':
            return obj.width*obj.height;
        break;
      }
    } 

This is working fine if I just create a circle and click on 'calculate area' button. But if I resize circle and then calculate area, it still showing the old area that is at the time of it's creation.

I found object:scaling event and getting width and height after scaling as -

canvas.on("object:scaling", function(e){
var obj = e.target
console.log(obj.width*obj.scaleX);
console.log(obj.height* e.target.scaleY);
}); 

but don't know how to use these values to calculate updated area, any suggestion please?

Upvotes: 0

Views: 895

Answers (1)

Mosè Raguzzini
Mosè Raguzzini

Reputation: 15831

Just use

getScaledWidth() → {Number}
Returns width of an object bounding box counting transformations before 2.0 it was named getWidth();

Using this method will return always the scaled width or height (with getScaledHeight()) in pixel.

Upvotes: 2

Related Questions