Andrei
Andrei

Reputation: 73

How to find out absolute coordinates of Polygon in fabric js?

If I get points after scaling or movement, I get some wrong points. How can I find out absolute coordinates of Polygon in fabric js?

var onObjectScaling = function(eventObj) {
        if (target.type === 'dpolygon') {
            target.minX *= target.scaleX;
            target.minY *= target.scaleY;
            var maxX = target.minX;
            var maxY = target.minY;
            angular.forEach(target.points, function (v) {
                v.x *= target.scaleX;
                v.y *= target.scaleY;
                maxX = Math.max(maxX, v.x);
                maxY = Math.max(maxY, v.y);
            });
        }
};

Upvotes: 0

Views: 2459

Answers (2)

Cherishh
Cherishh

Reputation: 36

@Andrei's solution doesn't work for me.

Instead, I found two solutions, might be helpful to someone.

Before fabric 2.0, polygon points are relative to its center, you want to calculate coordinates by following this: How to get polygon points in Fabric.js

After 2.0, polygon points are absolute to canvas, and this will help: How does transforming points with a transformMatrix work in fabricJS?

In my case, I use 2.3.6 and the later solution works for me.

Upvotes: 0

Andrei
Andrei

Reputation: 73

finally, after some attempts I found a proper way to calculate absolute coordinates

angular.forEach(object.points, function(point) { 
   point.x = object.left + (point.x - object.minX); 
   point.y = object.top + (point.y - object.minY); 
});

Upvotes: 1

Related Questions