简体   繁体   English

在fabric.js中找到活动对象的坐标

[英]Find Coordinates of the Active Object in fabric.js

Using fabric js i draw a different shapes such as circle ,rectangle .I tried 我使用fabric js绘制了不同的形状,例如圆形,矩形。

the rectangle is drawn by fabric.rect() method 矩形是通过fabric.rect() method绘制的

 canvas.getActiveObject().get('points');

but it returns undefined 但它返回undefined

I have also gone through this post 我也看过这篇文章

How to get polygon points in Fabric.js 如何在Fabric.js中获取多边形点

but i cannot solve the issue 但我无法解决问题

    var object = canvas.getActiveObject();
    var objectCenter = object.getCenterPoint();
    var translatedPoints = object.get('points');
    console.log(object);
    console.log(translatedPoints);
    translatedPoints.map(function(p) {
        var pt =  {
            x: objectCenter.x + p.x,
            y: objectCenter.y + p.y
        };
           console.log(pt);

Is there any way to find the coordinates of the rectangle or other active objects drawn by similar methods 有没有办法找到用类似方法绘制的矩形或其他活动对象的坐标

You can use this code: 您可以使用以下代码:

var obj = canvas.getActiveObject();
alert(obj.left + "," + obj.top);

If you want to get the data for all objects on your canvas, you can create a JSON object using the following code. 如果要获取画布上所有对象的数据,则可以使用以下代码创建JSON对象。 Coordinates for all of your objects will be included as properties in the output (along with object height & width - per Chirag's question in the previous answer). 您所有对象的坐标将作为属性包含在输出中(以及对象的高度和宽度-根据上一个答案中的Chirag的问题)。

function serializeCanvas(c) {
    var canvasJson = JSON.stringify(c);
    console.log(canvasJson);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM