对象数据随机改变了Fabricjs

时间:2016-05-02 05:35:15

标签: javascript fabricjs

我有一段代码记录了鼠标点击并在最后绘制了一个多边形。 在绘制多边形之后,它会将坐标数组字符串化,以备将来处理。

我面临的问题是记录和绘制的内容以及字符串化的内容是两组完全不同的数据。

e.g。

(index):169 Mouse click at 543 179
(index):171 Object {x: 543, y: 179} contains value, match as per mouse click
(index):169 Mouse click at 310 592
(index):171 Object {x: 310, y: 592} contains value, match as per mouse click
(index):169 Mouse click at 735 480
(index):171 Object {x: 735, y: 480} contains value, match as per mouse click
(index):246 [{"x":20.5,"y":-206.5},{"x":-212.5,"y":206.5},{"x":212.5,"y":94.5}] is stringified value

JS

    var coords = [];
    var canvas = new fabric.Canvas('canvas');

    canvas.on('mouse:down', function (options) {
                getCoordinates(options);
        });



    $('.drawReady').click(function(){
           drawMyPolygon(coords, 1);
           stringifyTheData();
    });

function drawMyPolygon(retData, tmpId) {
            var polygon = new fabric.Polygon(retData, {
                fill: 'purple',
                selectable: true,
                lockMovementX: true,
                lockMovementY: true,
                id: tmpId
            });

            canvas.add(polygon);

        };
    function getCoordinates(options) {
      var pointer = canvas.getPointer(event.e);
      coords.push({x: pointer.x, y: pointer.y});
    }

    function stringifyTheData() {
      var retVal = JSON.stringify(coords);
    }

1 个答案:

答案 0 :(得分:1)

看起来您正在使用fabricJS早期版本1.6.0 在1.5.0中,fabricjs正在修改点阵列,而现在却没有。

cange包括在每个点上减去多边形的顶部,左边和半边宽度,使其原点居中。

在传递给多边形之前更新或克隆点数组。