使用fabric.js发出镜像克隆对象位置问题

时间:2014-02-07 13:16:58

标签: javascript canvas fabricjs

我正在尝试使用fabric.js在画布上创建一个克隆对象,当我对原始对象进行克隆时,它会准确地放置克隆,但是当我克隆前一个克隆对象时没有准确地将它放在所有方向。

这是demo link

Html代码

 <canvas  id="c" height="400" width="500"></canvas>
 <button id="clone_left" name="left">Left</button>
 <button id="clone_right" name="right">right</button>
 <button id="clone_top" name="top">top</button>
 <button id="clone_bottom" name="bottom">bottom</button>

脚本

//Using i-text
var canvas = new fabric.Canvas('c');
var text = new fabric.IText('Sample', {
    left: 100,
    top: 100,
    fill: 'red'
});
canvas.add(text); 
document.getElementById('clone_left').addEventListener('click', function (e) {
  var obj = canvas.getActiveObject();
  if(obj.type=='i-text')
  {
        if (fabric.util.getKlass(obj.type).async) {
            obj.clone(function (clone) {
               clone.set({left: clone.get('left') + 210});
                canvas.add(clone);
            });
        } else {
            //alert("else");
            var clone = obj.clone();
          //lone.set({left: clone.get('left') + 370});
          clone.set("angle", "180").set('flipY', true); 
          clone.set({top: clone.get('top') + 52});
          canvas.add(clone);
          canvas.renderAll();
        }
  } 
});

document.getElementById('clone_right').addEventListener('click', function (e) {
  var obj = canvas.getActiveObject();
  if(obj.type=='i-text')
  {
        if (fabric.util.getKlass(obj.type).async) {
            obj.clone(function (clone) {
               clone.set({left: clone.get('left') + 210});
               clone.set("angle", "180").set('flipY', true);
               canvas.add(clone);
               //alert("if");
            });
        } else {
          //  alert("else right");
          var clone = obj.clone();
          clone.set({left: clone.get('left') + 250});
          clone.set("angle", "180").set('flipY', true); 
          clone.set({top: clone.get('top') + 52});
          canvas.add(clone);
        } 
     } 
});
document.getElementById('clone_top').addEventListener('click', function (e) {
 var obj = canvas.getActiveObject();
  if(obj.type=='i-text')
  {
        if (fabric.util.getKlass(obj.type).async) {
            obj.clone(function (clone) {
              clone.set("angle", "180").set('flipX', true);
              clone.set({top: clone.get('top') - 135});
              canvas.add(clone);
            });
        } else {
            //alert("else top");
            var clone = obj.clone();
        //clone.set("angle", "180").set('flipX', true);     
       // clone.set({top: clone.get('top') - 50});
          clone.set({left: clone.get('left') + 120});
          clone.set("angle", "180").set('flipX', true); 
          canvas.add(clone);
        } 
  }  
});

document.getElementById('clone_bottom').addEventListener('click', function (e) {
  var obj = canvas.getActiveObject();
  if(obj.type=='i-text')
  {
        if (fabric.util.getKlass(obj.type).async) {
            obj.clone(function (clone) {
              clone.set("angle", "-180").set('flipX', true);    
              clone.set({top: clone.get('top') + 135});
              canvas.add(clone);
            });
        } else {
            var clone = obj.clone();
        // clone.set("angle", "-180").set('flipX', true);   
        //clone.set({top: clone.get('top') + 50});
        clone.set("angle", "-180").set('flipX', true);  
        clone.set({top: clone.get('top') + 100});
        clone.set({left: clone.get('left') + 120});
        canvas.add(clone);
        } 
  } 

});

当我制作简单克隆时,它运作良好: working snap

但是当我克隆克隆对象时,它看起来像这样并且不在准确的位置。你可以在这里看到克隆对象由克隆底部制成,蓝色边角和蓝色箭头表示该对象的位置错误,实际上我希望克隆对象位于克隆对象下面。

Getting issue with position

1 个答案:

答案 0 :(得分:1)

错误的位置主要是由fabric.IText对象声明造成的。

var text = new fabric.IText('Sample', {
    left: 100, // this cause translation
    top: 100, // this cause translation
    fill: 'red'
});

我使用了fabric.util.object.clone而不是obj.clone函数,我已经删除了每个魔术数字的重现,现在按预期工作:

<强> http://jsfiddle.net/InferOn/3dr72sj2/4/embedded/result/

enter image description here

var canvas = new fabric.Canvas('c');
var text = new fabric.IText('Sample', {
    fill: 'red'
});

// the start positioning of text are set out object declaration
text.set({top: text.get('top') + 150}); 
text.set({left: text.get('left') + 200});
canvas.add(text);

document.getElementById('clone_left').addEventListener('click', function (e) {
    var obj = canvas.getActiveObject();
    if (obj.type == 'i-text') {
        var cl = fabric.util.object.clone(obj);
        cl.set('flipY', true);  
        // using the text object properties instead of magic numbers it is better to achieve the right measurement 
        cl.set({left: cl.get('left') - text.width});
        canvas.add(cl);

    }
});

document.getElementById('clone_right').addEventListener('click', function (e) {
    var obj = canvas.getActiveObject();
    if (obj.type == 'i-text') {
        var cl = fabric.util.object.clone(obj);
        cl.set('flipY', true);  
        cl.set({left: cl.get('left') + text.width});
        canvas.add(cl);

    }
});

document.getElementById('clone_bottom').addEventListener('click', function (e) {
    var obj = canvas.getActiveObject();
    if (obj.type == 'i-text') {
        var cl = fabric.util.object.clone(obj);
        cl.set('flipY', true);
        cl.set({top: cl.get('top') + text.height});
        canvas.add(cl);

    }
});
document.getElementById('clone_top').addEventListener('click', function (e) {
    var obj = canvas.getActiveObject();
    if (obj.type == 'i-text') {
        var cl = fabric.util.object.clone(obj);
        cl.set('flipY', true);
        cl.set({top: cl.get('top') - text.height});
        canvas.add(cl);

    }
});