如何选择所有形状,我的意思是必须始终激活形状,这样我就不需要点击任何形状

时间:2016-07-14 09:32:25

标签: fabricjs

我试过下面的代码:

canvas.setActiveGroup(new fabric.Group(canvas.getObjects())).renderAll();

但它选择整个组作为单个实体。

1 个答案:

答案 0 :(得分:0)

这里我有一个解决方案,在鼠标悬停时我选择鼠标指针经过的元素。

here is the fiddle



window.canvas = new fabric.Canvas('c', {perPixelTargetFind: true});

var shadow = {
    color: 'rgba(0,0,0,0.6)',
    blur: 20,    
    offsetX: 10,
    offsetY: 10,
    opacity: 0.6,
    fillShadow: true, 
    strokeShadow: true 
}

 var rect = new fabric.Rect({
        left: 120,
        top: 100,
        fill:  "#FF0000",
        stroke: "#000",
        width: 100,
        height: 100,
        strokeWidth: 10, 
        opacity: .8      
    });


 var rect1 = new fabric.Rect({
        left: 0,
        top: 100,
        fill:  "#FF0000",
        stroke: "#000",
        width: 100,
        height: 100,
        strokeWidth: 10, 
        opacity: .8      
    });

canvas.on('mouse:move', function(e){
	console.log('over1',e.target)
  if(e.target && e.target !== canvas.getActiveObject())
  canvas.setActiveObject(e.target)
})

rect.setShadow(shadow);
canvas.add(rect);
canvas.add(rect1);

<canvas id="c" width="300" height="300"></canvas>
&#13;
&#13;
&#13;

here is the fiddle