fabric js特定对象取消选择组

时间:2018-03-09 19:45:01

标签: javascript jquery fabricjs

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

    CANVAS_SELECTED_OBJECTS = [];

    CANVAS_SELECTED_GROUP = {};

    $('body').find('.dt_inline_menu').removeClass('show');


   // for(obj in activeObject._objects)
    for(var i = 0; i < activeObject._objects.length; i++){ 

      debugger;
    if(activeObject._objects && activeObject._objects[i].type == 'rect' && activeObject._objects[i].IsImprintArea){

            console.log(':::::::::::::::: papa ::::::::::::::::::::::');
            console.log(activeObject._objects[i]); 
            alert('papa');
            console.log(':::::::::::::::: papa ::::::::::::::::::::::'); 

    }
    else
    {
      console.log(':::::::::::::::: NO papa ::::::::::::::::::::::'); 
        DT_SELECTED_TYPE = DT_FABRIC_OBJECTS[activeObject._objects[0].get('type')];   
        CANVAS_SELECTED_GROUP = activeObject; 
        $("#"+UI_TEXT_FONT_ITALIC).prop('disabled', false);
        $("#"+UI_TEXT_FONT_BOLD).prop('disabled', false);  
    } 
   } 
  };

1 个答案:

答案 0 :(得分:0)

使用removeWithUpdate()addWithUpdate()分别删除和添加到组对象。

<强> 样本

&#13;
&#13;
var canvas = new fabric.Canvas('canvas');
var circle = new fabric.Circle({
  left: 20,
  radius:100,
  top: 50,
  fill:'red'
});
var text1 = new fabric.Text('Hello',{
 left:10,
 top:10
});
var text2 = new fabric.Text('World',{
 left:10,
 top:80
});
var rect = new fabric.Rect({
 left:50,top:50,width:100,height:100,fill:'green'
});
var group = new fabric.Group([text1,circle,rect],{
 left:50,top:50
})
canvas.add(group);

function removeFromGroup(){
 group.removeWithUpdate(text1);
 canvas.renderAll();
}

function addToGroup(){
 group.addWithUpdate(text2);
 canvas.renderAll();
}
&#13;
canvas {
  border:1px solid #000;
}
&#13;
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<button onclick='removeFromGroup()'>remove</button>
<button onclick='addToGroup()'>Add</button>
<canvas id="canvas" height="400" width="400"></canvas>
&#13;
&#13;
&#13;