组合可编辑对象?

时间:2012-06-24 19:50:20

标签: javascript fabricjs

有没有办法在fabric.Group中拥有可编辑的对象,以便可以移动其中一个对象?

1 个答案:

答案 0 :(得分:0)

注意:我没有单独测试此代码。

这样的事情可能是一种解决方法,至少我们可以稍微检测一下子对象。

var $canvas = {};
$canvas.activePaths = [];

function onMouseDown = function (options) {

    var mousePos = canvas.getPointer(options.e);
    mousePos.x = parseInt(mousePos.x);
    mousePos.y = parseInt(mousePos.y);

    var width =  canvas.getWidth();
    var height = canvas.getHeight();

    var pixels = canvas.getContext().getImageData(0, 0, width, height);
    var pixel = (mousePos.x + mousePos.y * pixels.width) * 4;

    var activePathsColor = new fabric['Color']('rgb(' + pixels.data[pixel] + ',' + pixels.data[pixel + 1] + ',' + pixels.data[pixel + 2] + ')');
    var colorHex = '#' + activePathsColor.toHex().toLowerCase();

    var activeObject = canvas.getActiveObject();
    var activePath = [];

    if(activeObject.type == 'group') {

          for (var i = 0; i < activeObject.getObjects().length; i++) {
            var path = activeObject.getObjects()[i];
            if (path.getFill().toLowerCase() == colorHex) {
                $canvas.activePaths.push(path);
            }
        }

    }

}

function warpActiveObject() {
    for(var i = 0; i < $canvas.activePaths.length; i++) {
        canvas.add(
            new fabric.Rect({
                left: $canvas.getActiveObj().left + ($canvas.activePaths[i].left),
                top: $canvas.getActiveObj().top + ($canvas.activePaths[i].top),
                fill: '',
                width: $canvas.activePaths[0].width,
                height: $canvas.activePaths[0].height,
                stroke: 'white', 
                opacity: 0.5 
            })
    );
    }
}